| 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( 'WpssoIntegLangPolylang' ) ) { |
| 14 |
|
| 15 |
class WpssoIntegLangPolylang { |
| 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 |
$this->p->util->add_plugin_filters( $this, array( |
| 29 |
'home_url' => 2, |
| 30 |
'sitemaps_alternates' => 2, |
| 31 |
) ); |
| 32 |
|
| 33 |
$this->p->util->add_plugin_filters( $this, array( |
| 34 |
'available_feed_locale_names' => 1, |
| 35 |
'get_locale' => 2, |
| 36 |
), $prio = 200, $ext = 'sucom' ); // Note the 'sucom' filter prefix. |
| 37 |
} |
| 38 |
|
| 39 |
public function filter_home_url( $url, $mod ) { |
| 40 |
|
| 41 |
if ( function_exists( 'pll_home_url' ) ) { // Just in case. |
| 42 |
|
| 43 |
return pll_home_url(); |
| 44 |
} |
| 45 |
|
| 46 |
return $url; |
| 47 |
} |
| 48 |
|
| 49 |
public function filter_sitemaps_alternates( $alternates, $mod ) { |
| 50 |
|
| 51 |
if ( $this->p->debug->enabled ) { |
| 52 |
|
| 53 |
$this->p->debug->mark(); |
| 54 |
} |
| 55 |
|
| 56 |
if ( $mod[ 'is_post' ] ) { |
| 57 |
|
| 58 |
$translations = pll_get_post_translations( $mod[ 'id' ] ); |
| 59 |
|
| 60 |
if ( $this->p->debug->enabled ) { |
| 61 |
|
| 62 |
$this->p->debug->log_arr( 'translations', $translations ); |
| 63 |
} |
| 64 |
|
| 65 |
foreach ( $translations as $lang_code => $post_id ) { |
| 66 |
|
| 67 |
$transl_mod = $this->p->post->get_mod( $post_id ); |
| 68 |
|
| 69 |
$alternates[] = array( |
| 70 |
'href' => $this->p->util->get_canonical_url( $transl_mod ), |
| 71 |
'hreflang' => $this->p->schema->get_schema_lang( $transl_mod ), |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
} elseif ( $mod[ 'is_term' ] ) { |
| 76 |
|
| 77 |
$translations = pll_get_term_translations( $mod[ 'id' ] ); |
| 78 |
|
| 79 |
if ( $this->p->debug->enabled ) { |
| 80 |
|
| 81 |
$this->p->debug->log_arr( 'translations', $translations ); |
| 82 |
} |
| 83 |
|
| 84 |
foreach ( $translations as $lang_code => $term_id ) { |
| 85 |
|
| 86 |
$transl_mod = $this->p->term->get_mod( $term_id ); |
| 87 |
|
| 88 |
$alternates[] = array( |
| 89 |
'href' => $this->p->util->get_canonical_url( $transl_mod ), |
| 90 |
'hreflang' => $this->p->schema->get_schema_lang( $transl_mod ), |
| 91 |
); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return $alternates; |
| 96 |
} |
| 97 |
|
| 98 |
public function filter_available_feed_locale_names( $locale_names ) { |
| 99 |
|
| 100 |
$languages = pll_languages_list( array( 'fields' => array() ) ); |
| 101 |
|
| 102 |
$locale_names = array(); |
| 103 |
|
| 104 |
foreach ( $languages as $lang_obj ) { |
| 105 |
|
| 106 |
$locale_names[ $lang_obj->locale ] = $lang_obj->name; |
| 107 |
} |
| 108 |
|
| 109 |
return $locale_names; |
| 110 |
} |
| 111 |
|
| 112 |
/* |
| 113 |
* Argument can also be a numeric post ID, to return the language of that post. |
| 114 |
*/ |
| 115 |
public function filter_get_locale( $wp_locale, $mixed = 'current' ) { |
| 116 |
|
| 117 |
if ( $this->p->debug->enabled ) { |
| 118 |
|
| 119 |
$this->p->debug->log( 'wp locale = ' . $wp_locale ); |
| 120 |
} |
| 121 |
|
| 122 |
$pll_locale = false; |
| 123 |
|
| 124 |
switch ( true ) { |
| 125 |
|
| 126 |
case ( is_array( $mixed ) ): |
| 127 |
|
| 128 |
if ( $mixed[ 'id' ] > 0 ) { // Just in case. |
| 129 |
|
| 130 |
if ( $mixed[ 'is_post' ] ) { |
| 131 |
|
| 132 |
$pll_locale = $this->get_post_language( $mixed[ 'id' ] ); |
| 133 |
|
| 134 |
} elseif ( $mixed[ 'is_term' ] ) { |
| 135 |
|
| 136 |
$pll_locale = $this->get_term_language( $mixed[ 'id' ] ); |
| 137 |
} |
| 138 |
|
| 139 |
if ( $this->p->debug->enabled ) { |
| 140 |
|
| 141 |
$this->p->debug->log( 'pll_locale for ' . $mixed[ 'name' ] . ' id ' . $mixed[ 'id' ] . ' = ' . $pll_locale ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
break; |
| 146 |
|
| 147 |
case ( is_numeric( $mixed ) ): |
| 148 |
|
| 149 |
if ( $mixed > 0 ) { // Just in case. |
| 150 |
|
| 151 |
$pll_locale = $this->get_post_language( $mixed ); |
| 152 |
|
| 153 |
if ( $this->p->debug->enabled ) { |
| 154 |
|
| 155 |
$this->p->debug->log( 'pll_locale for post ID ' . $mixed . ' = ' . $pll_locale ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
break; |
| 160 |
|
| 161 |
case ( 'default' === $mixed ): |
| 162 |
|
| 163 |
if ( function_exists( 'pll_default_language' ) ) { |
| 164 |
|
| 165 |
$pll_locale = pll_default_language( 'locale' ); |
| 166 |
|
| 167 |
if ( $this->p->debug->enabled ) { |
| 168 |
|
| 169 |
$this->p->debug->log( 'pll_locale for default = ' . $pll_locale ); |
| 170 |
} |
| 171 |
|
| 172 |
} elseif ( $this->p->debug->enabled ) { |
| 173 |
|
| 174 |
$this->p->debug->log( 'pll_default_language function not found' ); |
| 175 |
} |
| 176 |
|
| 177 |
break; |
| 178 |
|
| 179 |
case ( 'current' === $mixed ): |
| 180 |
|
| 181 |
if ( function_exists( 'pll_current_language' ) ) { |
| 182 |
|
| 183 |
$pll_locale = pll_current_language( 'locale' ); |
| 184 |
|
| 185 |
if ( $this->p->debug->enabled ) { |
| 186 |
|
| 187 |
$this->p->debug->log( 'pll_locale for current = ' . $pll_locale ); |
| 188 |
} |
| 189 |
|
| 190 |
} elseif ( $this->p->debug->enabled ) { |
| 191 |
|
| 192 |
$this->p->debug->log( 'pll_current_language function not found' ); |
| 193 |
} |
| 194 |
|
| 195 |
break; |
| 196 |
} |
| 197 |
|
| 198 |
return $pll_locale ? $pll_locale : $wp_locale; |
| 199 |
} |
| 200 |
|
| 201 |
private function get_post_language( $post_id ) { |
| 202 |
|
| 203 |
$pll_locale = ''; |
| 204 |
|
| 205 |
if ( function_exists( 'pll_get_post_language' ) ) { // Since PLL v1.5.4. |
| 206 |
|
| 207 |
$pll_locale = pll_get_post_language( $post_id, 'locale' ); |
| 208 |
|
| 209 |
} else { |
| 210 |
|
| 211 |
global $polylang; |
| 212 |
|
| 213 |
if ( isset( $polylang->model->post ) ) { |
| 214 |
|
| 215 |
$pll_lang = $polylang->model->post->get_language( $post_id ); |
| 216 |
|
| 217 |
} elseif ( isset( $polylang->model ) ) { |
| 218 |
|
| 219 |
$pll_lang = $polylang->model->get_post_language( $post_id ); |
| 220 |
} |
| 221 |
|
| 222 |
if ( ! empty( $pll_lang ) ) { |
| 223 |
|
| 224 |
$pll_locale = $pll_lang->locale; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return $pll_locale; |
| 229 |
} |
| 230 |
|
| 231 |
private function get_term_language( $term_id ) { |
| 232 |
|
| 233 |
$pll_locale = ''; |
| 234 |
|
| 235 |
if ( function_exists( 'pll_get_term_language' ) ) { // Since PLL v1.5.4. |
| 236 |
|
| 237 |
$pll_locale = pll_get_term_language( $term_id, 'locale' ); |
| 238 |
|
| 239 |
} else { |
| 240 |
|
| 241 |
global $polylang; |
| 242 |
|
| 243 |
if ( isset( $polylang->model->term ) ) { |
| 244 |
|
| 245 |
$pll_lang = $polylang->model->term->get_language( $term_id ); |
| 246 |
|
| 247 |
} elseif ( isset( $polylang->model ) ) { |
| 248 |
|
| 249 |
$pll_lang = $polylang->model->get_term_language( $term_id ); |
| 250 |
} |
| 251 |
|
| 252 |
if ( ! empty( $pll_lang ) ) { |
| 253 |
|
| 254 |
$pll_locale = $pll_lang->locale; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
return $pll_locale; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|