Skip to content

Commit

Permalink
QTS: refactor language setup with internal q_config (#1130)
Browse files Browse the repository at this point in the history
  • Loading branch information
spleen1981 authored Mar 25, 2022
1 parent 8fe8f76 commit e570c38
Showing 1 changed file with 19 additions and 61 deletions.
80 changes: 19 additions & 61 deletions modules/slugs/includes/class-qtranslate-slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 '<link hreflang="x-default" href="' . esc_url( $this->get_current_url( $this->default_language ) ) . '" rel="alternate" />' . PHP_EOL;
foreach ( $this->get_enabled_languages() as $language ) {
echo '<link hreflang="x-default" href="' . esc_url( $this->get_current_url( $q_config['default_language'] ) ) . '" rel="alternate" />' . PHP_EOL;
foreach ( $q_config['enabled_languages'] as $language ) {

echo '<link hreflang="' . $language . '" href="' . esc_url( $this->get_current_url( $language ) ) . '" rel="alternate" />' . "\n";
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ) ) );
Expand Down Expand Up @@ -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';
}
Expand Down Expand Up @@ -1124,7 +1098,7 @@ public function draw_meta_box( $post ) {
echo "<table style=\"width:100%\">" . PHP_EOL;
echo "<input type=\"hidden\" name=\"qts_nonce\" id=\"qts_nonce\" value=\"" . wp_create_nonce( 'qts_nonce' ) . "\" />" . 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 );

Expand Down Expand Up @@ -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 );
}
Expand All @@ -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"] ) ) {
Expand All @@ -1295,7 +1270,7 @@ public function show_add_term_fields( $term ) {
global $q_config;

echo "<div id=\"form-field term-slug-wrap\">" . PHP_EOL;
foreach ( $this->enabled_languages as $lang ) {
foreach ( $q_config['enabled_languages'] as $lang ) {
echo "<div class=\"form-field\">" . PHP_EOL;
$slug = ( is_object( $term ) ) ? get_metadata( 'term', $term->term_id, $this->get_meta_key( $lang ), true ) : '';
$value = ( $slug ) ? htmlspecialchars( $slug, ENT_QUOTES ) : '';
Expand All @@ -1315,7 +1290,7 @@ public function show_edit_term_fields( $term ) {
global $q_config;

echo "<table class=\"form-table\">" . 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 "<tr class=\"form-field term-slug-wrap\">" . PHP_EOL;
Expand All @@ -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 );
Expand Down Expand Up @@ -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
Expand All @@ -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"] : '';
Expand Down Expand Up @@ -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'];
}

/**
Expand All @@ -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 ) ) {
Expand Down

0 comments on commit e570c38

Please sign in to comment.