| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'WpssoIntegLangQtranslateXt' ) ) { |
| 14 |
|
| 15 |
class WpssoIntegLangQtranslateXt { |
| 16 |
|
| 17 |
private $p; // Wpsso class object. |
| 18 |
|
| 19 |
public function __construct( &$plugin ) { |
| 20 |
|
| 21 |
$this->p =& $plugin; |
| 22 |
|
| 23 |
if ( $this->p->debug->enabled ) { |
| 24 |
|
| 25 |
$this->p->debug->mark(); |
| 26 |
} |
| 27 |
|
| 28 |
global $wp_version; |
| 29 |
|
| 30 |
$wp_min_version = '6.2'; |
| 31 |
|
| 32 |
if ( version_compare( $wp_version, $wp_min_version, '<' ) ) { |
| 33 |
|
| 34 |
$notice_msg = sprintf( __( 'The qTranslate-XT integration module requires %1$s version %2$s or better.', |
| 35 |
'wpsso' ), 'WordPress', $wp_min_version ) . ' '; |
| 36 |
|
| 37 |
$notice_msg .= sprintf( __( 'Please update to the latest %1$s version (or at least version %2$s).', |
| 38 |
'wpsso' ), 'WordPress', $wp_min_version ) . ' '; |
| 39 |
|
| 40 |
$notice_key = 'notice-recommend-version-wp-' . $wp_min_version; |
| 41 |
|
| 42 |
$this->p->notice->err( $notice_msg, $user_id = null, $notice_key ); |
| 43 |
} |
| 44 |
|
| 45 |
/* |
| 46 |
* Include front-end functions like qtranxf_translate_post(). |
| 47 |
*/ |
| 48 |
require_once QTRANSLATE_DIR . '/src/frontend.php'; |
| 49 |
|
| 50 |
add_action( 'change_locale', array( $this, 'locale_changed' ), -1000, 1 ); |
| 51 |
|
| 52 |
$this->p->util->add_plugin_filters( $this, array( |
| 53 |
'convert_text' => array( |
| 54 |
'product_title' => 1, |
| 55 |
), |
| 56 |
'convert_url' => array( |
| 57 |
'canonical_url' => 1, |
| 58 |
'oembed_url' => 1, |
| 59 |
'product_url' => 1, |
| 60 |
), |
| 61 |
'post_public_ids_posts_args' => 1, |
| 62 |
'post_public_ids_suppress_filters' => '__return_false', |
| 63 |
'the_content_filter_content' => '__return_true', |
| 64 |
'the_excerpt_filter_excerpt' => '__return_true', |
| 65 |
) ); |
| 66 |
|
| 67 |
$this->p->util->add_plugin_filters( $this, array( |
| 68 |
'convert_url' => array( |
| 69 |
'get_home_url' => 1, |
| 70 |
), |
| 71 |
'available_locales' => 1, |
| 72 |
'get_post_object' => 2, |
| 73 |
), $prio = 100, $ext = 'sucom' ); |
| 74 |
} |
| 75 |
|
| 76 |
/* |
| 77 |
* The qtranxf_localeForCurrentLanguage() function uses a static cache variable and does not recognize locale |
| 78 |
* changes. If the locale changes, then we must to unhook the 'qtranxf_localeForCurrentLanguage' filter to get the |
| 79 |
* the new locale from get_locale(). |
| 80 |
*/ |
| 81 |
public function locale_changed( $locale ) { |
| 82 |
|
| 83 |
if ( $this->p->debug->enabled ) { |
| 84 |
|
| 85 |
$this->p->debug->mark(); |
| 86 |
} |
| 87 |
|
| 88 |
remove_filter( 'locale', 'qtranxf_localeForCurrentLanguage', 99 ); |
| 89 |
} |
| 90 |
|
| 91 |
public function filter_convert_text( $text ) { |
| 92 |
|
| 93 |
if ( $this->p->debug->enabled ) { |
| 94 |
|
| 95 |
$this->p->debug->mark(); |
| 96 |
} |
| 97 |
|
| 98 |
$locale = SucomUtilWP::get_locale(); |
| 99 |
|
| 100 |
if ( $lang = qtranxf_match_language_locale( $locale ) ) { |
| 101 |
|
| 102 |
$text = qtranxf_use_language( $lang, $text, $show_available = false, $show_empty = false ); |
| 103 |
} |
| 104 |
|
| 105 |
return $text; |
| 106 |
} |
| 107 |
|
| 108 |
public function filter_convert_url( $url ) { |
| 109 |
|
| 110 |
if ( $this->p->debug->enabled ) { |
| 111 |
|
| 112 |
$this->p->debug->mark(); |
| 113 |
} |
| 114 |
|
| 115 |
$locale = SucomUtilWP::get_locale(); |
| 116 |
|
| 117 |
if ( $lang = qtranxf_match_language_locale( $locale ) ) { |
| 118 |
|
| 119 |
$url = qtranxf_get_url_for_language( $url, $lang, $showLanguage = true ); |
| 120 |
} |
| 121 |
|
| 122 |
return $url; |
| 123 |
} |
| 124 |
|
| 125 |
public function filter_post_public_ids_posts_args( $posts_args ) { |
| 126 |
|
| 127 |
if ( $this->p->debug->enabled ) { |
| 128 |
|
| 129 |
$this->p->debug->mark(); |
| 130 |
} |
| 131 |
|
| 132 |
global $q_config; |
| 133 |
|
| 134 |
if ( ! empty( $q_config[ 'hide_untranslated' ] ) ) { |
| 135 |
|
| 136 |
$locale = SucomUtilWP::get_locale(); |
| 137 |
|
| 138 |
if ( $lang = qtranxf_match_language_locale( $locale ) ) { |
| 139 |
|
| 140 |
$posts_args[ 's' ] = '[:' . $lang . ']'; |
| 141 |
$posts_args[ 'search_columns' ] = array( 'post_title', 'post_excerpt', 'post_content' ); // Since WP v6.2. |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return $posts_args; |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* Remove locales that are not configured. |
| 150 |
*/ |
| 151 |
public function filter_available_locales( $locales ) { |
| 152 |
|
| 153 |
if ( $this->p->debug->enabled ) { |
| 154 |
|
| 155 |
$this->p->debug->mark(); |
| 156 |
} |
| 157 |
|
| 158 |
foreach ( $locales as $num => $locale ) { |
| 159 |
|
| 160 |
if ( ! $lang = qtranxf_match_language_locale( $locale ) ) { |
| 161 |
|
| 162 |
unset( $locales[ $num ] ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return $locales; |
| 167 |
} |
| 168 |
|
| 169 |
public function filter_get_post_object( $post_object, $use_post ) { |
| 170 |
|
| 171 |
if ( is_object( $post_object ) ) { |
| 172 |
|
| 173 |
if ( $this->p->debug->enabled ) { |
| 174 |
|
| 175 |
$this->p->debug->log( get_class( $post_object ) . ' object use_post = ' . SucomUtil::get_use_post_string( $use_post ) ); |
| 176 |
} |
| 177 |
|
| 178 |
if ( $post_object instanceof WP_Post ) { |
| 179 |
|
| 180 |
$locale = SucomUtilWP::get_locale(); |
| 181 |
|
| 182 |
if ( $lang = qtranxf_match_language_locale( $locale ) ) { |
| 183 |
|
| 184 |
if ( $this->p->debug->enabled ) { |
| 185 |
|
| 186 |
$this->p->debug->log( 'calling qtranxf_translate_post() for ' .$lang . ' language' ); |
| 187 |
} |
| 188 |
|
| 189 |
qtranxf_translate_post( $post_object, $lang ); |
| 190 |
|
| 191 |
} elseif ( $this->p->debug->enabled ) { |
| 192 |
|
| 193 |
$this->p->debug->log( 'skipping qtranxf_translate_post() - no language for ' . $locale . ' locale' ); |
| 194 |
} |
| 195 |
|
| 196 |
} elseif ( $this->p->debug->enabled ) { |
| 197 |
|
| 198 |
$this->p->debug->log( 'skipping qtranxf_translate_post() - object not an instance of WP_Post' ); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return $post_object; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|