From e570c38ef3c8f74cdc89aa5736171291822c9245 Mon Sep 17 00:00:00 2001 From: Giovanni Cascione <43221199+spleen1981@users.noreply.github.com> Date: Fri, 25 Mar 2022 23:17:42 +0100 Subject: [PATCH] QTS: refactor language setup with internal `q_config` (#1130) --- .../slugs/includes/class-qtranslate-slug.php | 80 +++++-------------- 1 file changed, 19 insertions(+), 61 deletions(-) diff --git a/modules/slugs/includes/class-qtranslate-slug.php b/modules/slugs/includes/class-qtranslate-slug.php index 60a74a96..7e38cd3a 100644 --- a/modules/slugs/includes/class-qtranslate-slug.php +++ b/modules/slugs/includes/class-qtranslate-slug.php @@ -10,13 +10,6 @@ class QtranslateSlug { */ protected $options; - /** - * Array with old data system. - * @var bool - */ - //TODO: seems to be unused: remove - private $old_data = null; - /** * Stores permalink_structure option, for save queries to db. * @var string @@ -29,25 +22,6 @@ class QtranslateSlug { */ private $lang = false; - /** - * Variable for current language. - */ - //TODO: Check why not using QTX directly - private $current_lang = false; - - /** - * Variable for default language. - */ - //TODO: Check why not using QTX directly - private $default_language = false; - - /** - * Array of enabled languages. - * @var array - */ - //TODO: Check why not using QTX directly - private $enabled_languages = array(); - /** * Slug in meta_key name in meta tables. * @var string @@ -167,10 +141,6 @@ function init() { if ( is_admin() ) { include_once( dirname( __FILE__ ) . '/qtranslate-slug-settings.php' ); } - // until we get a proper function, this will make it for it. - $this->current_lang = $q_config['language']; - $this->enabled_languages = $q_config['enabled_languages']; - $this->default_language = $q_config['default_language']; if ( is_admin() ) { // add filters @@ -240,12 +210,14 @@ function init() { * @global array $q_config available languages */ public function qtranslate_slug_header_extended() { + global $q_config; if ( is_404() ) { return; } + //TODO: double check following comment: // taken from qtx but see our #341 ticket for clarification - echo '' . PHP_EOL; - foreach ( $this->get_enabled_languages() as $language ) { + echo '' . PHP_EOL; + foreach ( $q_config['enabled_languages'] as $language ) { echo '' . "\n"; } @@ -499,6 +471,7 @@ public function query_vars( $query_vars ) { * @return array $query processed */ function filter_request( $query ) { + global $q_config; global $wp; // FIXME: why is this here? it breaks custom variables getter // https://wordpress.org/support/topic/cant-retrieve-public-query-variables @@ -618,7 +591,7 @@ function filter_request( $query ) { if ( isset( $function ) ) { // parse all languages links - foreach ( $this->get_enabled_languages() as $lang ) { + foreach ( $q_config['enabled_languages'] as $lang ) { $this->lang = $lang; $this->current_url[ $lang ] = esc_url( apply_filters( 'qts_url_args', call_user_func( $function, $id ) ) ); @@ -661,6 +634,7 @@ public function get_current_url( $lang = false ) { * @return string Home url link with optional path appended. */ public function home_url( $url, $path, $scheme, $blog_id ) { + global $q_config; if ( ! in_array( $scheme, array( 'http', 'https' ) ) ) { $scheme = is_ssl() && ! is_admin() ? 'https' : 'http'; } @@ -1124,7 +1098,7 @@ public function draw_meta_box( $post ) { echo "" . PHP_EOL; echo "" . PHP_EOL; - foreach ( $this->enabled_languages as $lang ): + foreach ( $q_config['enabled_languages'] as $lang ): $slug = get_post_meta( $post->ID, $this->get_meta_key( $lang ), true ); @@ -1263,6 +1237,7 @@ public function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, * @return void */ public function save_postdata( $post_id, $post = null ) { + global $q_config; if ( is_null( $post ) ) { $post = get_post( $post_id ); } @@ -1274,7 +1249,7 @@ public function save_postdata( $post_id, $post = null ) { || ( ! current_user_can( $post_type_object->cap->edit_post, $post_id ) ) ) { // check permission return; } - foreach ( $this->get_enabled_languages() as $lang ) { + foreach ( $q_config['enabled_languages'] as $lang ) { // check required because it is not available inside quick edit if ( isset( $_POST["qts_{$lang}_slug"] ) ) { @@ -1295,7 +1270,7 @@ public function show_add_term_fields( $term ) { global $q_config; echo "
" . PHP_EOL; - foreach ( $this->enabled_languages as $lang ) { + foreach ( $q_config['enabled_languages'] as $lang ) { echo "
" . PHP_EOL; $slug = ( is_object( $term ) ) ? get_metadata( 'term', $term->term_id, $this->get_meta_key( $lang ), true ) : ''; $value = ( $slug ) ? htmlspecialchars( $slug, ENT_QUOTES ) : ''; @@ -1315,7 +1290,7 @@ public function show_edit_term_fields( $term ) { global $q_config; echo "
" . PHP_EOL; - foreach ( $this->enabled_languages as $lang ) { + foreach ( $q_config['enabled_languages'] as $lang ) { $slug = ( is_object( $term ) ) ? get_metadata( 'term', $term->term_id, $this->get_meta_key( $lang ), true ) : ''; $value = ( $slug ) ? htmlspecialchars( $slug, ENT_QUOTES ) : ''; echo "" . PHP_EOL; @@ -1337,7 +1312,7 @@ public function show_edit_term_fields( $term ) { public function validate_term_slug( $slug, $term, $lang ) { $term_name = trim( qtranxf_use( $lang, $term->name, false, true ) ); if ( $term_name === '' ) { - $term_name = trim( qtranxf_use( $this->default_language, $term->name ) ); + $term_name = trim( qtranxf_use( $q_config['default_language'], $term->name ) ); } $slug = trim( $slug ); $slug = $slug === '' ? sanitize_title( $term_name ) : sanitize_title( $slug ); @@ -1397,6 +1372,7 @@ public function unique_term_slug( $slug, $term, $lang ) { * @return void */ public function save_term( $term_id, $tt_id, $taxonomy ) { + global $q_config; $cur_screen = get_current_screen(); if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) // check autosave || ( ! current_user_can( 'edit_posts' ) ) // check permission @@ -1406,7 +1382,7 @@ public function save_term( $term_id, $tt_id, $taxonomy ) { } $term = get_term( $term_id, $taxonomy ); - foreach ( $this->get_enabled_languages() as $lang ) { + foreach ( $q_config['enabled_languages'] as $lang ) { $meta_name = $this->get_meta_key( $lang ); //condition is needed in case term is added through ajax e.g. in post edit page $term_slug = isset( $_POST["qts_{$lang}_slug"] ) ? $_POST["qts_{$lang}_slug"] : ''; @@ -1507,28 +1483,9 @@ public function get_public_post_types() { /** * Return the current / temp language. */ - //TODO: Check why not using QTX directly private function get_lang() { - //TODO: check, $this->lang is never supposed to be true... - return ( $this->lang ) ? $this->lang : $this->current_lang; - } - - /** - * Return the current / temp language. - * we store and use it all the way! - */ - //TODO: Check why not using QTX directly. Also it seems to be unused - private function get_currentlang() { - return $this->current_lang; - } - - /** - * Return the enabled languages. - * we store and use it all the way! - */ - //TODO: Check why not using QTX directly - private function get_enabled_languages() { - return $this->enabled_languages; + global $q_config; + return ( $this->lang ) ? $this->lang : $q_config['language']; } /** @@ -1548,9 +1505,10 @@ private function activate() { * @param string $name name of extra permastruct */ private function generate_extra_rules( $name = false ) { + global $q_config; global $wp_rewrite; - foreach ( $this->get_enabled_languages() as $lang ): + foreach ( $q_config['enabled_languages'] as $lang ): if ( $base = $this->get_base_slug( $name, $lang ) ): $struct = $wp_rewrite->extra_permastructs[ $name ]; if ( is_array( $struct ) ) {