Skip to content

Commit

Permalink
Refactor core file structure and init loaders (#1303)
Browse files Browse the repository at this point in the history
The `core` and `utils` files are too large and difficult to maintain.
Hooks are created while loading some PHP files which is hard to control.
Break `core` and `utils` into smaller pieces.
Refactor `qtranxf_init_language()` to make some init calls more explicit.
Refactor `core/hooks/frontend` loaders to move all creations of hooks into functions.
Deprecate legacy `qtranxf_init` (no-op).
  • Loading branch information
herrvigg authored Mar 30, 2023
1 parent 4e32d61 commit 9dcd83f
Show file tree
Hide file tree
Showing 15 changed files with 1,540 additions and 1,543 deletions.
5 changes: 3 additions & 2 deletions qtranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@
* Designed as interface for other plugin integration. The documentation is available at
* https://github.com/qtranslate/qtranslate-xt/wiki/Integration-Guide/
*/
define( 'QTX_VERSION', '3.13.1.dev.3' );
define( 'QTX_VERSION', '3.14.0.dev.1' );

if ( ! defined( 'QTRANSLATE_FILE' ) ) {
define( 'QTRANSLATE_FILE', __FILE__ );
define( 'QTRANSLATE_DIR', __DIR__ );
}

require_once QTRANSLATE_DIR . '/src/class_translator.php';
require_once QTRANSLATE_DIR . '/src/init.php';
add_action( 'plugins_loaded', 'qtranxf_init_language', 2 ); // User is not authenticated yet, high priority needed.

if ( is_admin() ) {
require_once QTRANSLATE_DIR . '/src/admin/activation_hook.php';
Expand Down
2 changes: 0 additions & 2 deletions src/admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -897,5 +897,3 @@ function qtranxf_admin_load() {
add_filter( 'gutenberg_use_widgets_block_editor', '__return_false', 99 );
add_filter( 'use_widgets_block_editor', '__return_false', 99 );
}

qtranxf_admin_load();
2 changes: 0 additions & 2 deletions src/admin/admin_class_translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,3 @@ function multilingual_term( $term, $term_default = null, $taxonomy = null ) {
return qtranxf_get_terms_joined( $terms, $taxonomy );
}
}

add_filter( 'wp_translator', 'QTX_Translator_Admin::get_translator' );
3 changes: 0 additions & 3 deletions src/class_translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
}

require_once QTRANSLATE_DIR . '/src/i18n-interface.php';
require_once QTRANSLATE_DIR . '/src/utils.php';
require_once QTRANSLATE_DIR . '/src/core.php';
require_once QTRANSLATE_DIR . '/src/taxonomy.php';

/**
* Implementation of WP_Translator interface.
Expand Down
1,090 changes: 0 additions & 1,090 deletions src/core.php

This file was deleted.

60 changes: 22 additions & 38 deletions src/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
exit;
}

add_filter( 'wp_translator', 'QTX_Translator::get_translator' );

function qtranxf_get_front_page_config() {
static $page_configs;//cache
static $page_configs;
if ( $page_configs ) {
return $page_configs;
}
Expand Down Expand Up @@ -59,8 +57,6 @@ function qtranxf_wp_head() {
}
}

add_action( 'wp_head', 'qtranxf_wp_head' );

/**
* Moved line '<meta name="generator"' to a separate action.
* Developers may use code
Expand All @@ -72,8 +68,6 @@ function qtranxf_wp_head_meta_generator() {
echo '<meta name="generator" content="qTranslate-XT ' . QTX_VERSION . '" />' . PHP_EOL;
}

add_action( 'wp_head', 'qtranxf_wp_head_meta_generator' );

function qtranxf_wp_get_nav_menu_items( $items, $menu, $args ) {
global $q_config;
$language = $q_config['language'];
Expand Down Expand Up @@ -184,8 +178,6 @@ function qtranxf_wp_get_nav_menu_items( $items, $menu, $args ) {
return $items;
}

add_filter( 'wp_get_nav_menu_items', 'qtranxf_wp_get_nav_menu_items', 20, 3 );

function qtranxf_add_language_menu_item( &$items, &$menu_order, &$itemid, $key, $language ) {
global $q_config;
$item = $items[ $key ];
Expand Down Expand Up @@ -444,8 +436,6 @@ function qtranxf_filter_options() {
}
}

qtranxf_filter_options();

/**
* @since 3.4.7
*/
Expand Down Expand Up @@ -527,8 +517,6 @@ function qtranxf_postsFilter( $posts, $query ) {
return $posts;
}

add_filter( 'the_posts', 'qtranxf_postsFilter', 5, 2 );

/** allow all filters within WP_Query - many other add_filters may not be needed now? */
function qtranxf_pre_get_posts( $query ) {//WP_Query
if ( isset( $query->query_vars['post_type'] ) ) {
Expand All @@ -542,8 +530,6 @@ function qtranxf_pre_get_posts( $query ) {//WP_Query
$query->query_vars['suppress_filters'] = false;
}

add_action( 'pre_get_posts', 'qtranxf_pre_get_posts', 99 );

/**
* since 3.1-b3 new query to pass empty content and content without closing tags (sliders, galleries and other special kind of posts that never get translated)
*/
Expand Down Expand Up @@ -650,8 +636,6 @@ function qtranxf_get_attachment_image_attributes( $attr, $attachment = null, $si
return $attr;
}

add_filter( 'wp_get_attachment_image_attributes', 'qtranxf_get_attachment_image_attributes', 5, 3 );

function qtranxf_home_url( $url, $path, $orig_scheme, $blog_id ) {
global $q_config;
$lang = $q_config['language'];
Expand All @@ -669,8 +653,6 @@ function qtranxf_esc_html( $text ) {
return qtranxf_useCurrentLanguageIfNotFoundShowEmpty( $text );
}

add_filter( 'esc_html', 'qtranxf_esc_html', 0 );

if ( ! function_exists( 'qtranxf_trim_words' ) ) {
// filter added in qtranslate_hooks.php
function qtranxf_trim_words( $text, $num_words, $more, $original_text ) {
Expand Down Expand Up @@ -823,8 +805,6 @@ function qtranxf_filter_postmeta( $original_value, $object_id, $meta_key = '', $
return qtranxf_translate_metadata( 'post', $original_value, $object_id, $meta_key, $single );
}

add_filter( 'get_post_metadata', 'qtranxf_filter_postmeta', 5, 4 );

/**
* @since 3.2.9.9.6
* Delete translated post_meta cache for all languages on cache update.
Expand All @@ -834,7 +814,6 @@ function qtranxf_updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value
qtranxf_cache_delete_metadata( 'post', $object_id );
}

add_action( 'updated_postmeta', 'qtranxf_updated_postmeta', 5, 4 );

/**
* @since 3.4 translation of usermeta
Expand All @@ -843,8 +822,6 @@ function qtranxf_filter_usermeta( $original_value, $object_id, $meta_key = '', $
return qtranxf_translate_metadata( 'user', $original_value, $object_id, $meta_key, $single );
}

add_filter( 'get_user_metadata', 'qtranxf_filter_usermeta', 5, 4 );

/**
* @since 3.4
* Delete translated user_meta cache for all languages on cache update.
Expand All @@ -854,8 +831,6 @@ function qtranxf_updated_usermeta( $meta_id, $object_id, $meta_key, $meta_value
qtranxf_cache_delete_metadata( 'user', $object_id );
}

add_action( 'updated_usermeta', 'qtranxf_updated_usermeta', 5, 4 );

function qtranxf_checkCanonical( $redirect_url, $requested_url ) {
global $q_config;
$lang = $q_config['language'];
Expand All @@ -865,8 +840,6 @@ function qtranxf_checkCanonical( $redirect_url, $requested_url ) {
return $redirect_url_lang;
}

add_filter( 'redirect_canonical', 'qtranxf_checkCanonical', 10, 2 );

/**
* @since 3.2.8 moved here from _hooks.php
*/
Expand All @@ -893,14 +866,32 @@ function qtranxf_pagenum_link( $url ) {
return qtranxf_convertURL( $url_fixed );
}

add_filter( 'get_pagenum_link', 'qtranxf_pagenum_link' );

/**
* @since 3.3.7
*/
function qtranxf_add_front_filters() {
global $q_config;

add_action( 'wp_head', 'qtranxf_wp_head' );
add_action( 'wp_head', 'qtranxf_wp_head_meta_generator' );
add_filter( 'wp_get_nav_menu_items', 'qtranxf_wp_get_nav_menu_items', 20, 3 );
add_filter( 'wp_get_attachment_image_attributes', 'qtranxf_get_attachment_image_attributes', 5, 3 );
add_filter( 'esc_html', 'qtranxf_esc_html', 0 );
add_filter( 'the_posts', 'qtranxf_postsFilter', 5, 2 );
add_action( 'pre_get_posts', 'qtranxf_pre_get_posts', 99 );
add_filter( 'get_post_metadata', 'qtranxf_filter_postmeta', 5, 4 );
add_action( 'updated_postmeta', 'qtranxf_updated_postmeta', 5, 4 );
add_filter( 'get_user_metadata', 'qtranxf_filter_usermeta', 5, 4 );
add_action( 'updated_usermeta', 'qtranxf_updated_usermeta', 5, 4 );
add_filter( 'redirect_canonical', 'qtranxf_checkCanonical', 10, 2 );
add_filter( 'get_pagenum_link', 'qtranxf_pagenum_link' );

// Time critical filters, not needed on admin side.
// In particular, they break WPBakery Visual Composer in raw Editor Mode.
add_filter( 'gettext', 'qtranxf_gettext', 0 );
add_filter( 'gettext_with_context', 'qtranxf_gettext_with_context', 0 );
add_filter( 'ngettext', 'qtranxf_ngettext', 0 );

if ( $q_config['hide_untranslated'] ) {
add_filter( 'wp_list_pages_excludes', 'qtranxf_excludePages' );//moved here from _hooks.php since 3.2.8
add_filter( 'posts_where_request', 'qtranxf_excludeUntranslatedPosts', 10, 2 );
Expand All @@ -925,12 +916,5 @@ function qtranxf_add_front_filters() {
add_filter( 'home_url', 'qtranxf_home_url', 0, 4 );
}

// Hooks (execution time critical filters)
// since 3.2.9.9.4 gettext* filters moved to frontend.php
// they should not be needed on admin side and they, in particular, broke WPBakery Visual Composer in raw Editor Mode.
add_filter( 'gettext', 'qtranxf_gettext', 0 );
add_filter( 'gettext_with_context', 'qtranxf_gettext_with_context', 0 );
add_filter( 'ngettext', 'qtranxf_ngettext', 0 );
qtranxf_filter_options();
}

qtranxf_add_front_filters();
19 changes: 2 additions & 17 deletions src/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
exit;
}

require_once QTRANSLATE_DIR . '/src/date_time.php';

/**
* locale for current language and set it on PHP.
*/
function qtranxf_localeForCurrentLanguage( $locale ) {
static $locale_lang;//cache
static $locale_lang;
if ( ! empty( $locale_lang ) ) {
return $locale_lang;
}
Expand Down Expand Up @@ -123,24 +121,17 @@ function qtranxf_ngettext( $translated_text ) {
* @return void
*/
function qtranxf_add_main_filters() {
// Hooks defined differently in admin and frontend
add_filter( 'wp_trim_words', 'qtranxf_trim_words', 0, 4 );

// Hooks (execution time critical filters)
add_filter( 'sanitize_title', 'qtranxf_useRawTitle', 0, 3 );

add_filter( 'comment_moderation_subject', 'qtranxf_useDefaultLanguage', 0 );
add_filter( 'comment_moderation_text', 'qtranxf_useDefaultLanguage', 0 );

// since 3.1 changed priority from 0 to 100, since other plugins,
// like https://wordpress.org/plugins/siteorigin-panels generate additional content, which also needs to be translated.
add_filter( 'the_content', 'qtranxf_useCurrentLanguageIfNotFoundShowAvailable', 100 );
add_filter( 'the_excerpt', 'qtranxf_useCurrentLanguageIfNotFoundShowAvailable', 0 );
add_filter( 'the_excerpt_rss', 'qtranxf_useCurrentLanguageIfNotFoundShowAvailable', 0 );

add_filter( 'locale', 'qtranxf_localeForCurrentLanguage', 99 );
add_filter( 'core_version_check_locale', 'qtranxf_versionLocale' );

add_filter( 'post_title', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', 0 );
add_filter( 'tag_rows', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', 0 );
add_filter( 'wp_list_categories', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', 0 );
Expand All @@ -160,18 +151,12 @@ function qtranxf_add_main_filters() {
add_filter( 'link_name', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', 0 );
add_filter( 'link_description', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', 0 );
add_filter( 'the_author', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', 0 );

add_filter( 'comment_notification_text', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage' );
add_filter( 'comment_notification_headers', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage' );
add_filter( 'comment_notification_subject', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage' );

add_filter( 'oembed_response_data', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage' );
add_filter( 'pre_option_rss_language', 'qtranxf_getLanguage', 0 );

add_filter( '_wp_post_revision_field_post_title', 'qtranxf_showAllSeparated', 0 );
add_filter( '_wp_post_revision_field_post_content', 'qtranxf_showAllSeparated', 0 );
add_filter( '_wp_post_revision_field_post_excerpt', 'qtranxf_showAllSeparated', 0 );

add_filter( 'oembed_response_data', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage' );

qtranxf_add_date_time_filters();
}
Loading

0 comments on commit 9dcd83f

Please sign in to comment.