| 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( 'WpssoIntegDataAbstractSeoMeta' ) ) { |
| 14 |
|
| 15 |
abstract class WpssoIntegDataAbstractSeoMeta { |
| 16 |
|
| 17 |
protected $p; // Wpsso class object. |
| 18 |
|
| 19 |
protected $plugin_avail_key = ''; |
| 20 |
|
| 21 |
protected $import_opt_keys = array(); |
| 22 |
|
| 23 |
protected $opt_keys_active_plugin = array( |
| 24 |
'og_title' => true, |
| 25 |
'og_desc' => true, |
| 26 |
'og_img_id' => true, |
| 27 |
'og_img_url' => true, |
| 28 |
'tc_title' => true, |
| 29 |
'tc_desc' => true, |
| 30 |
'tc_sum_img_id' => true, |
| 31 |
'tc_sum_img_url' => true, |
| 32 |
'tc_lrg_img_id' => true, |
| 33 |
'tc_lrg_img_url' => true, |
| 34 |
'schema_article_section' => true, |
| 35 |
); |
| 36 |
|
| 37 |
protected $opt_keys_inactive_plugin = array( |
| 38 |
'primary_term_id' => true, |
| 39 |
'seo_title' => true, |
| 40 |
'seo_desc' => true, |
| 41 |
'schema_title' => true, |
| 42 |
'schema_title_bc' => true, |
| 43 |
'schema_desc' => true, |
| 44 |
'schema_reading_mins' => true, |
| 45 |
'canonical_url' => true, |
| 46 |
'redirect_url' => true, |
| 47 |
'robots_noarchive' => true, |
| 48 |
'robots_nofollow' => true, |
| 49 |
'robots_noindex' => true, |
| 50 |
); |
| 51 |
|
| 52 |
protected $opt_meta_keys = array( |
| 53 |
'post' => array(), |
| 54 |
'term' => array(), |
| 55 |
'user' => array(), |
| 56 |
); |
| 57 |
|
| 58 |
protected $cache_imported_meta = array(); |
| 59 |
|
| 60 |
public function __construct( &$plugin ) { |
| 61 |
|
| 62 |
$this->p =& $plugin; |
| 63 |
|
| 64 |
if ( $this->p->debug->enabled ) { |
| 65 |
|
| 66 |
$this->p->debug->mark(); |
| 67 |
} |
| 68 |
|
| 69 |
if ( $this->plugin_avail_key ) { // Just in case. |
| 70 |
|
| 71 |
if ( empty( $this->p->avail[ 'seo' ][ $this->plugin_avail_key ] ) ) { // SEO plugin is not active. |
| 72 |
|
| 73 |
$this->import_opt_keys = array_merge( $this->opt_keys_active_plugin, $this->opt_keys_inactive_plugin ); |
| 74 |
|
| 75 |
} else { |
| 76 |
|
| 77 |
$this->import_opt_keys = $this->opt_keys_active_plugin; // SEO plugin is active. |
| 78 |
} |
| 79 |
|
| 80 |
$this->p->util->add_plugin_filters( $this, array( |
| 81 |
'save_post_options' => 3, |
| 82 |
'save_term_options' => 3, |
| 83 |
'save_user_options' => 3, |
| 84 |
'get_post_options' => 3, |
| 85 |
'get_term_options' => 3, |
| 86 |
'get_user_options' => 3, |
| 87 |
) ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public function filter_save_post_options( array $md_opts, $post_id, array $mod ) { |
| 92 |
|
| 93 |
if ( $this->p->debug->enabled ) { |
| 94 |
|
| 95 |
$this->p->debug->mark(); |
| 96 |
} |
| 97 |
|
| 98 |
$this->cache_imported_meta[ 'post' ] = array(); |
| 99 |
|
| 100 |
$md_opts = $this->filter_get_post_options( $md_opts, $post_id, $mod ); |
| 101 |
|
| 102 |
if ( ! empty( $this->cache_imported_meta[ 'post' ] ) ) { |
| 103 |
|
| 104 |
foreach( $this->cache_imported_meta[ 'post' ] as $meta_key => $bool ) { |
| 105 |
|
| 106 |
delete_metadata( 'post', $post_id, $meta_key ); |
| 107 |
} |
| 108 |
|
| 109 |
unset( $this->cache_imported_meta[ 'post' ] ); |
| 110 |
} |
| 111 |
|
| 112 |
return $md_opts; |
| 113 |
} |
| 114 |
|
| 115 |
public function filter_get_post_options( array $md_opts, $post_id, array $mod ) { |
| 116 |
|
| 117 |
if ( $this->p->debug->enabled ) { |
| 118 |
|
| 119 |
$this->p->debug->mark(); |
| 120 |
} |
| 121 |
|
| 122 |
foreach ( $this->import_opt_keys as $opt_key => $bool ) { |
| 123 |
|
| 124 |
if ( ! empty( $this->opt_meta_keys[ 'post' ][ $opt_key ] ) ) { // Boolean is true. |
| 125 |
|
| 126 |
/* |
| 127 |
* Example: |
| 128 |
* |
| 129 |
* $opt_key = 'primary_term_id' |
| 130 |
* $meta_key = '_yoast_wpseo_primary_%%post_primary_tax_slug%%' |
| 131 |
*/ |
| 132 |
$meta_key = $this->opt_meta_keys[ 'post' ][ $opt_key ]; |
| 133 |
|
| 134 |
$meta_key = $this->p->util->inline->replace_variables( $meta_key, $mod ); |
| 135 |
|
| 136 |
/* |
| 137 |
* Skip options that have a custom value. An empty string and 'none' are not custom values. |
| 138 |
*/ |
| 139 |
if ( isset( $md_opts[ $opt_key ] ) && '' !== $md_opts[ $opt_key ] && 'none' !== $md_opts[ $opt_key ] ) { |
| 140 |
|
| 141 |
continue; // Keep custom options value. |
| 142 |
|
| 143 |
} elseif ( $this->add_mod_post_meta( $mod, $md_opts, $opt_key, $meta_key ) ) { |
| 144 |
|
| 145 |
$this->cache_imported_meta[ 'post' ][ $meta_key ] = true; |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return $md_opts; |
| 151 |
} |
| 152 |
|
| 153 |
protected function add_mod_post_meta( array $mod, &$md_opts, $opt_key, $meta_key ) { |
| 154 |
|
| 155 |
if ( $this->p->debug->enabled ) { |
| 156 |
|
| 157 |
$this->p->debug->mark(); |
| 158 |
} |
| 159 |
|
| 160 |
if ( empty( $meta_key ) ) { // Just in case. |
| 161 |
|
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
$value = (string) get_metadata( 'post', $mod[ 'id' ], $meta_key, $single = true ); |
| 166 |
|
| 167 |
if ( '' === $value ) { |
| 168 |
|
| 169 |
if ( $this->p->debug->enabled ) { |
| 170 |
|
| 171 |
$this->p->debug->log( 'post meta ' . $meta_key . ' is empty' ); |
| 172 |
} |
| 173 |
|
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
$value = $this->maybe_convert_value( $mod, $opt_key, $meta_key, $value ); |
| 178 |
|
| 179 |
if ( $this->p->debug->enabled ) { |
| 180 |
|
| 181 |
$this->p->debug->log( 'adding ' . $opt_key . ' from post meta ' . $meta_key ); |
| 182 |
} |
| 183 |
|
| 184 |
$md_opts[ $opt_key ] = $value; |
| 185 |
|
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
public function filter_save_term_options( array $md_opts, $term_id, array $mod ) { |
| 190 |
|
| 191 |
if ( $this->p->debug->enabled ) { |
| 192 |
|
| 193 |
$this->p->debug->mark(); |
| 194 |
} |
| 195 |
|
| 196 |
$this->cache_imported_meta[ 'term' ] = array(); |
| 197 |
|
| 198 |
$md_opts = $this->filter_get_term_options( $md_opts, $term_id, $mod ); |
| 199 |
|
| 200 |
if ( ! empty( $this->cache_imported_meta[ 'term' ] ) ) { |
| 201 |
|
| 202 |
foreach( $this->cache_imported_meta[ 'term' ] as $meta_key => $bool ) { |
| 203 |
|
| 204 |
delete_metadata( 'term', $term_id, $meta_key ); |
| 205 |
} |
| 206 |
|
| 207 |
unset( $this->cache_imported_meta[ 'term' ] ); |
| 208 |
} |
| 209 |
|
| 210 |
return $md_opts; |
| 211 |
} |
| 212 |
|
| 213 |
public function filter_get_term_options( array $md_opts, $term_id, array $mod ) { |
| 214 |
|
| 215 |
if ( $this->p->debug->enabled ) { |
| 216 |
|
| 217 |
$this->p->debug->mark(); |
| 218 |
} |
| 219 |
|
| 220 |
foreach ( $this->import_opt_keys as $opt_key => $bool ) { |
| 221 |
|
| 222 |
if ( ! empty( $this->opt_meta_keys[ 'term' ][ $opt_key ] ) ) { |
| 223 |
|
| 224 |
$meta_key = $this->opt_meta_keys[ 'term' ][ $opt_key ]; |
| 225 |
|
| 226 |
/* |
| 227 |
* Skip options that have a custom value. An empty string and 'none' are not custom values. |
| 228 |
*/ |
| 229 |
if ( isset( $md_opts[ $opt_key ] ) && '' !== $md_opts[ $opt_key ] && 'none' !== $md_opts[ $opt_key ] ) { |
| 230 |
|
| 231 |
continue; // Keep custom options value. |
| 232 |
|
| 233 |
} elseif ( $this->add_mod_term_meta( $mod, $md_opts, $opt_key, $meta_key ) ) { |
| 234 |
|
| 235 |
$this->cache_imported_meta[ 'term' ][ $meta_key ] = true; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
return $md_opts; |
| 241 |
} |
| 242 |
|
| 243 |
protected function add_mod_term_meta( array $mod, &$md_opts, $opt_key, $meta_key, $term_opts = null ) { |
| 244 |
|
| 245 |
if ( $this->p->debug->enabled ) { |
| 246 |
|
| 247 |
$this->p->debug->mark(); |
| 248 |
} |
| 249 |
|
| 250 |
if ( empty( $meta_key ) ) { // Just in case. |
| 251 |
|
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
if ( null === $term_opts ) { |
| 256 |
|
| 257 |
$value = (string) get_metadata( 'term', $mod[ 'id' ], $meta_key, $single = true ); |
| 258 |
|
| 259 |
} else { |
| 260 |
|
| 261 |
$value = isset( $term_opts[ $meta_key ] ) ? (string) $term_opts[ $meta_key ] : ''; |
| 262 |
} |
| 263 |
|
| 264 |
if ( '' === $value ) { |
| 265 |
|
| 266 |
if ( $this->p->debug->enabled ) { |
| 267 |
|
| 268 |
$this->p->debug->log( 'term meta ' . $meta_key . ' is empty' ); |
| 269 |
} |
| 270 |
|
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
$value = $this->maybe_convert_value( $mod, $opt_key, $meta_key, $value ); |
| 275 |
|
| 276 |
if ( $this->p->debug->enabled ) { |
| 277 |
|
| 278 |
$this->p->debug->log( 'adding ' . $opt_key . ' from term meta ' . $meta_key ); |
| 279 |
} |
| 280 |
|
| 281 |
$md_opts[ $opt_key ] = $value; |
| 282 |
|
| 283 |
return true; |
| 284 |
} |
| 285 |
|
| 286 |
public function filter_save_user_options( array $md_opts, $user_id, array $mod ) { |
| 287 |
|
| 288 |
if ( $this->p->debug->enabled ) { |
| 289 |
|
| 290 |
$this->p->debug->mark(); |
| 291 |
} |
| 292 |
|
| 293 |
$this->cache_imported_meta[ 'user' ] = array(); |
| 294 |
|
| 295 |
$md_opts = $this->filter_get_user_options( $md_opts, $user_id, $mod ); |
| 296 |
|
| 297 |
if ( ! empty( $this->cache_imported_meta[ 'user' ] ) ) { |
| 298 |
|
| 299 |
foreach( $this->cache_imported_meta[ 'user' ] as $meta_key => $bool ) { |
| 300 |
|
| 301 |
delete_metadata( 'user', $user_id, $meta_key ); |
| 302 |
} |
| 303 |
|
| 304 |
unset( $this->cache_imported_meta[ 'user' ] ); |
| 305 |
} |
| 306 |
|
| 307 |
return $md_opts; |
| 308 |
} |
| 309 |
|
| 310 |
public function filter_get_user_options( array $md_opts, $user_id, array $mod ) { |
| 311 |
|
| 312 |
if ( $this->p->debug->enabled ) { |
| 313 |
|
| 314 |
$this->p->debug->mark(); |
| 315 |
} |
| 316 |
|
| 317 |
foreach ( $this->import_opt_keys as $opt_key => $bool ) { |
| 318 |
|
| 319 |
if ( ! empty( $this->opt_meta_keys[ 'user' ][ $opt_key ] ) ) { |
| 320 |
|
| 321 |
$meta_key = $this->opt_meta_keys[ 'user' ][ $opt_key ]; |
| 322 |
|
| 323 |
/* |
| 324 |
* Skip options that have a custom value. An empty string and 'none' are not custom values. |
| 325 |
*/ |
| 326 |
if ( isset( $md_opts[ $opt_key ] ) && '' !== $md_opts[ $opt_key ] && 'none' !== $md_opts[ $opt_key ] ) { |
| 327 |
|
| 328 |
continue; // Keep custom options value. |
| 329 |
|
| 330 |
} elseif ( $this->add_mod_user_meta( $mod, $md_opts, $opt_key, $meta_key ) ) { |
| 331 |
|
| 332 |
$this->cache_imported_meta[ 'user' ][ $meta_key ] = true; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
return $md_opts; |
| 338 |
} |
| 339 |
|
| 340 |
protected function add_mod_user_meta( array $mod, &$md_opts, $opt_key, $meta_key ) { |
| 341 |
|
| 342 |
if ( $this->p->debug->enabled ) { |
| 343 |
|
| 344 |
$this->p->debug->mark(); |
| 345 |
} |
| 346 |
|
| 347 |
if ( empty( $meta_key ) ) { // Just in case. |
| 348 |
|
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
$value = (string) get_metadata( 'user', $mod[ 'id' ], $meta_key, $single = true ); |
| 353 |
|
| 354 |
if ( '' === $value ) { |
| 355 |
|
| 356 |
if ( $this->p->debug->enabled ) { |
| 357 |
|
| 358 |
$this->p->debug->log( 'user meta ' . $meta_key . ' is empty' ); |
| 359 |
} |
| 360 |
|
| 361 |
return false; |
| 362 |
} |
| 363 |
|
| 364 |
$value = $this->maybe_convert_value( $mod, $opt_key, $meta_key, $value ); |
| 365 |
|
| 366 |
if ( $this->p->debug->enabled ) { |
| 367 |
|
| 368 |
$this->p->debug->log( 'adding ' . $opt_key . ' from user meta ' . $meta_key ); |
| 369 |
} |
| 370 |
|
| 371 |
$md_opts[ $opt_key ] = $value; |
| 372 |
|
| 373 |
return true; |
| 374 |
} |
| 375 |
|
| 376 |
protected function maybe_convert_value( array $mod, $opt_key, $meta_key, $value ) { |
| 377 |
|
| 378 |
if ( $this->p->debug->enabled ) { |
| 379 |
|
| 380 |
$this->p->debug->mark(); |
| 381 |
} |
| 382 |
|
| 383 |
$value = $this->maybe_convert_vars( $value, $mod ); |
| 384 |
|
| 385 |
switch ( $opt_key ) { |
| 386 |
|
| 387 |
case 'robots_noarchive': |
| 388 |
case 'robots_nofollow': |
| 389 |
case 'robots_noindex': |
| 390 |
|
| 391 |
if ( '1' === $value || 'on' === $value ) { |
| 392 |
|
| 393 |
return 1; |
| 394 |
|
| 395 |
} elseif ( '-1' === $value || 'off' === $value ) { |
| 396 |
|
| 397 |
return 0; |
| 398 |
|
| 399 |
} elseif ( 'robots_noindex' === $opt_key && 'noindex' === $value ) { |
| 400 |
|
| 401 |
return 1; |
| 402 |
|
| 403 |
} elseif ( 'robots_noindex' === $opt_key && 'index' === $value ) { |
| 404 |
|
| 405 |
return 0; |
| 406 |
} |
| 407 |
|
| 408 |
break; |
| 409 |
} |
| 410 |
|
| 411 |
return $value; |
| 412 |
} |
| 413 |
|
| 414 |
protected function maybe_convert_vars( $value, array $mod ) { |
| 415 |
|
| 416 |
if ( $this->p->debug->enabled ) { |
| 417 |
|
| 418 |
$this->p->debug->mark(); |
| 419 |
} |
| 420 |
|
| 421 |
return $value; |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|