| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Internals |
| 6 |
* @since 1.5.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
use Yoast\WP\SEO\Config\Schema_Types; |
| 10 |
use Yoast\WP\SEO\Helpers\Schema\Article_Helper; |
| 11 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 12 |
|
| 13 |
/** |
| 14 |
* This class implements defaults and value validation for all WPSEO Post Meta values. |
| 15 |
* |
| 16 |
* Some guidelines: |
| 17 |
* - To update a meta value, you can just use update_post_meta() with the full (prefixed) meta key |
| 18 |
* or the convenience method WPSEO_Meta::set_value() with the internal key. |
| 19 |
* All updates will be automatically validated. |
| 20 |
* Meta values will only be saved to the database if they are *not* the same as the default to |
| 21 |
* keep database load low. |
| 22 |
* - To retrieve a WPSEO meta value, you **must** use WPSEO_Meta::get_value() which will always return a |
| 23 |
* string value, either the saved value or the default. |
| 24 |
* This method can also retrieve a complete set of WPSEO meta values for one specific post, see |
| 25 |
* the method documentation for the parameters. |
| 26 |
* |
| 27 |
* {@internal Unfortunately there isn't a filter available to hook into before returning the results |
| 28 |
* for get_post_meta(), get_post_custom() and the likes. That would have been the |
| 29 |
* preferred solution.}} |
| 30 |
* |
| 31 |
* {@internal All WP native get_meta() results get cached internally, so no need to cache locally.}} |
| 32 |
* {@internal Use $key when the key is the WPSEO internal name (without prefix), $meta_key when it |
| 33 |
* includes the prefix.}} |
| 34 |
*/ |
| 35 |
class WPSEO_Meta { |
| 36 |
|
| 37 |
/** |
| 38 |
* Prefix for all WPSEO meta values in the database. |
| 39 |
* |
| 40 |
* {@internal If at any point this would change, quite apart from an upgrade routine, |
| 41 |
* this also will need to be changed in the wpml-config.xml file.}} |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
public static $meta_prefix = '_yoast_wpseo_'; |
| 46 |
|
| 47 |
/** |
| 48 |
* Prefix for all WPSEO meta value form field names and ids. |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
public static $form_prefix = 'yoast_wpseo_'; |
| 53 |
|
| 54 |
/** |
| 55 |
* Allowed length of the meta description. |
| 56 |
* |
| 57 |
* @var int |
| 58 |
*/ |
| 59 |
public static $meta_length = 156; |
| 60 |
|
| 61 |
/** |
| 62 |
* Reason the meta description is not the default length. |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
public static $meta_length_reason = ''; |
| 67 |
|
| 68 |
/** |
| 69 |
* Meta box field definitions for the meta box form. |
| 70 |
* |
| 71 |
* {@internal |
| 72 |
* - Titles, help texts, description text and option labels are added via a translate_meta_boxes() method |
| 73 |
* in the relevant child classes (WPSEO_Metabox and WPSEO_Social_admin) as they are only needed there. |
| 74 |
* - Beware: even though the meta keys are divided into subsets, they still have to be uniquely named!}} |
| 75 |
* |
| 76 |
* @var array |
| 77 |
* Array format: |
| 78 |
* (required) 'type' => (string) field type. i.e. text / textarea / checkbox / |
| 79 |
* radio / select / multiselect / upload etc. |
| 80 |
* (required) 'title' => (string) table row title. |
| 81 |
* (recommended) 'default_value' => (string|array) default value for the field. |
| 82 |
* IMPORTANT: |
| 83 |
* - if the field has options, the default has to be the |
| 84 |
* key of one of the options. |
| 85 |
* - if the field is a text field, the default **has** to be |
| 86 |
* an empty string as otherwise the user can't save |
| 87 |
* an empty value/delete the meta value. |
| 88 |
* - if the field is a checkbox, the only valid values |
| 89 |
* are 'on' or 'off'. |
| 90 |
* (semi-required) 'options' => (array) options for used with (multi-)select and radio |
| 91 |
* fields, required if that's the field type. |
| 92 |
* key = (string) value which will be saved to db. |
| 93 |
* value = (string) text label for the option. |
| 94 |
* (optional) 'autocomplete' => (bool) whether autocomplete is on for text fields, |
| 95 |
* defaults to true. |
| 96 |
* (optional) 'class' => (string) classname(s) to add to the actual <input> tag. |
| 97 |
* (optional) 'description' => (string) description to show underneath the field. |
| 98 |
* (optional) 'expl' => (string) label for a checkbox. |
| 99 |
* (optional) 'help' => (string) help text to show on mouse over ? image. |
| 100 |
* (optional) 'rows' => (int) number of rows for a textarea, defaults to 3. |
| 101 |
* (optional) 'placeholder' => (string) Currently only used by add-on plugins. |
| 102 |
* (optional) 'serialized' => (bool) whether the value is expected to be serialized, |
| 103 |
* i.e. an array or object, defaults to false. |
| 104 |
* Currently only used by add-on plugins. |
| 105 |
*/ |
| 106 |
public static $meta_fields = [ |
| 107 |
'general' => [ |
| 108 |
'focuskw' => [ |
| 109 |
'type' => 'hidden', |
| 110 |
'title' => '', |
| 111 |
], |
| 112 |
'title' => [ |
| 113 |
'type' => 'hidden', |
| 114 |
'title' => '', // Translation added later. |
| 115 |
'default_value' => '', |
| 116 |
'description' => '', // Translation added later. |
| 117 |
'help' => '', // Translation added later. |
| 118 |
], |
| 119 |
'metadesc' => [ |
| 120 |
'type' => 'hidden', |
| 121 |
'title' => '', // Translation added later. |
| 122 |
'default_value' => '', |
| 123 |
'class' => 'metadesc', |
| 124 |
'rows' => 2, |
| 125 |
'description' => '', // Translation added later. |
| 126 |
'help' => '', // Translation added later. |
| 127 |
], |
| 128 |
'linkdex' => [ |
| 129 |
'type' => 'hidden', |
| 130 |
'title' => 'linkdex', |
| 131 |
'default_value' => '0', |
| 132 |
'description' => '', |
| 133 |
], |
| 134 |
'content_score' => [ |
| 135 |
'type' => 'hidden', |
| 136 |
'title' => 'content_score', |
| 137 |
'default_value' => '0', |
| 138 |
'description' => '', |
| 139 |
], |
| 140 |
'is_cornerstone' => [ |
| 141 |
'type' => 'hidden', |
| 142 |
'title' => 'is_cornerstone', |
| 143 |
'default_value' => 'false', |
| 144 |
'description' => '', |
| 145 |
], |
| 146 |
], |
| 147 |
'advanced' => [ |
| 148 |
'meta-robots-noindex' => [ |
| 149 |
'type' => 'hidden', |
| 150 |
'title' => '', // Translation added later. |
| 151 |
'default_value' => '0', // = post-type default. |
| 152 |
'options' => [ |
| 153 |
'0' => '', // Post type default - translation added later. |
| 154 |
'2' => '', // Index - translation added later. |
| 155 |
'1' => '', // No-index - translation added later. |
| 156 |
], |
| 157 |
], |
| 158 |
'meta-robots-nofollow' => [ |
| 159 |
'type' => 'hidden', |
| 160 |
'title' => '', // Translation added later. |
| 161 |
'default_value' => '0', // = follow. |
| 162 |
'options' => [ |
| 163 |
'0' => '', // Follow - translation added later. |
| 164 |
'1' => '', // No-follow - translation added later. |
| 165 |
], |
| 166 |
], |
| 167 |
'meta-robots-adv' => [ |
| 168 |
'type' => 'hidden', |
| 169 |
'title' => '', // Translation added later. |
| 170 |
'default_value' => '', |
| 171 |
'description' => '', // Translation added later. |
| 172 |
'options' => [ |
| 173 |
'noimageindex' => '', // Translation added later. |
| 174 |
'noarchive' => '', // Translation added later. |
| 175 |
'nosnippet' => '', // Translation added later. |
| 176 |
], |
| 177 |
], |
| 178 |
'bctitle' => [ |
| 179 |
'type' => 'hidden', |
| 180 |
'title' => '', // Translation added later. |
| 181 |
'default_value' => '', |
| 182 |
'description' => '', // Translation added later. |
| 183 |
], |
| 184 |
'canonical' => [ |
| 185 |
'type' => 'hidden', |
| 186 |
'title' => '', // Translation added later. |
| 187 |
'default_value' => '', |
| 188 |
'description' => '', // Translation added later. |
| 189 |
], |
| 190 |
'redirect' => [ |
| 191 |
'type' => 'url', |
| 192 |
'title' => '', // Translation added later. |
| 193 |
'default_value' => '', |
| 194 |
'description' => '', // Translation added later. |
| 195 |
], |
| 196 |
], |
| 197 |
'social' => [], |
| 198 |
'schema' => [ |
| 199 |
'schema_page_type' => [ |
| 200 |
'type' => 'hidden', |
| 201 |
'title' => '', |
| 202 |
'options' => Schema_Types::PAGE_TYPES, |
| 203 |
], |
| 204 |
'schema_article_type' => [ |
| 205 |
'type' => 'hidden', |
| 206 |
'title' => '', |
| 207 |
'hide_on_pages' => true, |
| 208 |
'options' => Schema_Types::ARTICLE_TYPES, |
| 209 |
], |
| 210 |
], |
| 211 |
/* Fields we should validate & save, but not show on any form. */ |
| 212 |
'non_form' => [ |
| 213 |
'linkdex' => [ |
| 214 |
'type' => null, |
| 215 |
'default_value' => '0', |
| 216 |
], |
| 217 |
'zapier_trigger_sent' => [ |
| 218 |
'type' => null, |
| 219 |
'default_value' => '0', |
| 220 |
], |
| 221 |
], |
| 222 |
]; |
| 223 |
|
| 224 |
/** |
| 225 |
* Helper property - reverse index of the definition array. |
| 226 |
* |
| 227 |
* Format: [full meta key including prefix] => array |
| 228 |
* ['subset'] => (string) primary index |
| 229 |
* ['key'] => (string) internal key |
| 230 |
* |
| 231 |
* @var array |
| 232 |
*/ |
| 233 |
public static $fields_index = []; |
| 234 |
|
| 235 |
/** |
| 236 |
* Helper property - array containing only the defaults in the format: |
| 237 |
* [full meta key including prefix] => (string) default value |
| 238 |
* |
| 239 |
* @var array |
| 240 |
*/ |
| 241 |
public static $defaults = []; |
| 242 |
|
| 243 |
/** |
| 244 |
* Helper property to define the social network meta field definitions - networks. |
| 245 |
* |
| 246 |
* @var array |
| 247 |
*/ |
| 248 |
private static $social_networks = [ |
| 249 |
'opengraph' => 'opengraph', |
| 250 |
'twitter' => 'twitter', |
| 251 |
]; |
| 252 |
|
| 253 |
/** |
| 254 |
* Helper property to define the social network meta field definitions - fields and their type. |
| 255 |
* |
| 256 |
* @var array |
| 257 |
*/ |
| 258 |
private static $social_fields = [ |
| 259 |
'title' => 'hidden', |
| 260 |
'description' => 'hidden', |
| 261 |
'image' => 'hidden', |
| 262 |
'image-id' => 'hidden', |
| 263 |
]; |
| 264 |
|
| 265 |
/** |
| 266 |
* Register our actions and filters. |
| 267 |
* |
| 268 |
* @return void |
| 269 |
*/ |
| 270 |
public static function init() { |
| 271 |
foreach ( self::$social_networks as $option => $network ) { |
| 272 |
if ( WPSEO_Options::get( $option, false ) === true ) { |
| 273 |
foreach ( self::$social_fields as $box => $type ) { |
| 274 |
self::$meta_fields['social'][ $network . '-' . $box ] = [ |
| 275 |
'type' => $type, |
| 276 |
'title' => '', // Translation added later. |
| 277 |
'default_value' => '', |
| 278 |
'description' => '', // Translation added later. |
| 279 |
]; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
unset( $option, $network, $box, $type ); |
| 284 |
|
| 285 |
/** |
| 286 |
* Allow add-on plugins to register their meta fields for management by this class. |
| 287 |
* Calls to add_filter() must be made before plugins_loaded prio 14. |
| 288 |
*/ |
| 289 |
$extra_fields = apply_filters( 'add_extra_wpseo_meta_fields', [] ); |
| 290 |
if ( is_array( $extra_fields ) ) { |
| 291 |
self::$meta_fields = self::array_merge_recursive_distinct( $extra_fields, self::$meta_fields ); |
| 292 |
} |
| 293 |
unset( $extra_fields ); |
| 294 |
|
| 295 |
foreach ( self::$meta_fields as $subset => $field_group ) { |
| 296 |
foreach ( $field_group as $key => $field_def ) { |
| 297 |
|
| 298 |
register_meta( |
| 299 |
'post', |
| 300 |
self::$meta_prefix . $key, |
| 301 |
[ 'sanitize_callback' => [ __CLASS__, 'sanitize_post_meta' ] ] |
| 302 |
); |
| 303 |
|
| 304 |
// Set the $fields_index property for efficiency. |
| 305 |
self::$fields_index[ self::$meta_prefix . $key ] = [ |
| 306 |
'subset' => $subset, |
| 307 |
'key' => $key, |
| 308 |
]; |
| 309 |
|
| 310 |
// Set the $defaults property for efficiency. |
| 311 |
if ( isset( $field_def['default_value'] ) ) { |
| 312 |
self::$defaults[ self::$meta_prefix . $key ] = $field_def['default_value']; |
| 313 |
} |
| 314 |
else { |
| 315 |
// Meta will always be a string, so let's make the meta meta default also a string. |
| 316 |
self::$defaults[ self::$meta_prefix . $key ] = ''; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
unset( $subset, $field_group, $key, $field_def ); |
| 321 |
|
| 322 |
self::filter_schema_article_types(); |
| 323 |
|
| 324 |
add_filter( 'update_post_metadata', [ __CLASS__, 'remove_meta_if_default' ], 10, 5 ); |
| 325 |
add_filter( 'add_post_metadata', [ __CLASS__, 'dont_save_meta_if_default' ], 10, 4 ); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Retrieve the meta box form field definitions for the given tab and post type. |
| 330 |
* |
| 331 |
* @param string $tab Tab for which to retrieve the field definitions. |
| 332 |
* @param string $post_type Post type of the current post. |
| 333 |
* |
| 334 |
* @return array Array containing the meta box field definitions. |
| 335 |
*/ |
| 336 |
public static function get_meta_field_defs( $tab, $post_type = 'post' ) { |
| 337 |
if ( ! isset( self::$meta_fields[ $tab ] ) ) { |
| 338 |
return []; |
| 339 |
} |
| 340 |
|
| 341 |
$field_defs = self::$meta_fields[ $tab ]; |
| 342 |
|
| 343 |
switch ( $tab ) { |
| 344 |
case 'non-form': |
| 345 |
// Prevent non-form fields from being passed to forms. |
| 346 |
$field_defs = []; |
| 347 |
break; |
| 348 |
|
| 349 |
case 'advanced': |
| 350 |
global $post; |
| 351 |
|
| 352 |
if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) && WPSEO_Options::get( 'disableadvanced_meta' ) ) { |
| 353 |
return []; |
| 354 |
} |
| 355 |
|
| 356 |
$post_type = ''; |
| 357 |
if ( isset( $post->post_type ) ) { |
| 358 |
$post_type = $post->post_type; |
| 359 |
} |
| 360 |
elseif ( ! isset( $post->post_type ) && isset( $_GET['post_type'] ) ) { |
| 361 |
$post_type = sanitize_text_field( $_GET['post_type'] ); |
| 362 |
} |
| 363 |
|
| 364 |
if ( $post_type === '' ) { |
| 365 |
return []; |
| 366 |
} |
| 367 |
|
| 368 |
/* Adjust the no-index text strings based on the post type. */ |
| 369 |
$post_type_object = get_post_type_object( $post_type ); |
| 370 |
|
| 371 |
$field_defs['meta-robots-noindex']['title'] = sprintf( $field_defs['meta-robots-noindex']['title'], $post_type_object->labels->singular_name ); |
| 372 |
$field_defs['meta-robots-noindex']['options']['0'] = sprintf( $field_defs['meta-robots-noindex']['options']['0'], ( ( WPSEO_Options::get( 'noindex-' . $post_type, false ) === true ) ? $field_defs['meta-robots-noindex']['options']['1'] : $field_defs['meta-robots-noindex']['options']['2'] ), $post_type_object->label ); |
| 373 |
$field_defs['meta-robots-nofollow']['title'] = sprintf( $field_defs['meta-robots-nofollow']['title'], $post_type_object->labels->singular_name ); |
| 374 |
|
| 375 |
/* Don't show the breadcrumb title field if breadcrumbs aren't enabled. */ |
| 376 |
if ( WPSEO_Options::get( 'breadcrumbs-enable', false ) !== true && ! current_theme_supports( 'yoast-seo-breadcrumbs' ) ) { |
| 377 |
unset( $field_defs['bctitle'] ); |
| 378 |
} |
| 379 |
|
| 380 |
global $post; |
| 381 |
|
| 382 |
if ( empty( $post->ID ) || ( ! empty( $post->ID ) && self::get_value( 'redirect', $post->ID ) === '' ) ) { |
| 383 |
unset( $field_defs['redirect'] ); |
| 384 |
} |
| 385 |
break; |
| 386 |
|
| 387 |
case 'schema': |
| 388 |
if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_edit_advanced_metadata' ) && WPSEO_Options::get( 'disableadvanced_meta' ) ) { |
| 389 |
return []; |
| 390 |
} |
| 391 |
|
| 392 |
$field_defs['schema_page_type']['default'] = WPSEO_Options::get( 'schema-page-type-' . $post_type ); |
| 393 |
|
| 394 |
$article_helper = new Article_Helper(); |
| 395 |
if ( $article_helper->is_article_post_type( $post_type ) ) { |
| 396 |
$default_schema_article_type = WPSEO_Options::get( 'schema-article-type-' . $post_type ); |
| 397 |
|
| 398 |
/** This filter is documented in inc/options/class-wpseo-option-titles.php */ |
| 399 |
$allowed_article_types = apply_filters( 'wpseo_schema_article_types', Schema_Types::ARTICLE_TYPES ); |
| 400 |
|
| 401 |
if ( ! \array_key_exists( $default_schema_article_type, $allowed_article_types ) ) { |
| 402 |
$default_schema_article_type = WPSEO_Options::get_default( 'wpseo_titles', 'schema-article-type-' . $post_type ); |
| 403 |
} |
| 404 |
$field_defs['schema_article_type']['default'] = $default_schema_article_type; |
| 405 |
} |
| 406 |
else { |
| 407 |
unset( $field_defs['schema_article_type'] ); |
| 408 |
} |
| 409 |
|
| 410 |
break; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Filter the WPSEO metabox form field definitions for a tab. |
| 415 |
* {tab} can be 'general', 'advanced' or 'social'. |
| 416 |
* |
| 417 |
* @param array $field_defs Metabox form field definitions. |
| 418 |
* @param string $post_type Post type of the post the metabox is for, defaults to 'post'. |
| 419 |
* |
| 420 |
* @return array |
| 421 |
*/ |
| 422 |
return apply_filters( 'wpseo_metabox_entries_' . $tab, $field_defs, $post_type ); |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Validate the post meta values. |
| 427 |
* |
| 428 |
* @param mixed $meta_value The new value. |
| 429 |
* @param string $meta_key The full meta key (including prefix). |
| 430 |
* |
| 431 |
* @return string Validated meta value. |
| 432 |
*/ |
| 433 |
public static function sanitize_post_meta( $meta_value, $meta_key ) { |
| 434 |
$field_def = self::$meta_fields[ self::$fields_index[ $meta_key ]['subset'] ][ self::$fields_index[ $meta_key ]['key'] ]; |
| 435 |
$clean = self::$defaults[ $meta_key ]; |
| 436 |
|
| 437 |
switch ( true ) { |
| 438 |
case ( $meta_key === self::$meta_prefix . 'linkdex' ): |
| 439 |
$int = WPSEO_Utils::validate_int( $meta_value ); |
| 440 |
if ( $int !== false && $int >= 0 ) { |
| 441 |
$clean = strval( $int ); // Convert to string to make sure default check works. |
| 442 |
} |
| 443 |
break; |
| 444 |
|
| 445 |
case ( $field_def['type'] === 'checkbox' ): |
| 446 |
// Only allow value if it's one of the predefined options. |
| 447 |
if ( in_array( $meta_value, [ 'on', 'off' ], true ) ) { |
| 448 |
$clean = $meta_value; |
| 449 |
} |
| 450 |
break; |
| 451 |
|
| 452 |
|
| 453 |
case ( $field_def['type'] === 'select' || $field_def['type'] === 'radio' ): |
| 454 |
// Only allow value if it's one of the predefined options. |
| 455 |
if ( isset( $field_def['options'][ $meta_value ] ) ) { |
| 456 |
$clean = $meta_value; |
| 457 |
} |
| 458 |
break; |
| 459 |
|
| 460 |
|
| 461 |
case ( $field_def['type'] === 'hidden' && $meta_key === self::$meta_prefix . 'meta-robots-adv' ): |
| 462 |
$clean = self::validate_meta_robots_adv( $meta_value ); |
| 463 |
break; |
| 464 |
|
| 465 |
|
| 466 |
case ( $field_def['type'] === 'url' || $meta_key === self::$meta_prefix . 'canonical' ): |
| 467 |
// Validate as url(-part). |
| 468 |
$url = WPSEO_Utils::sanitize_url( $meta_value ); |
| 469 |
if ( $url !== '' ) { |
| 470 |
$clean = $url; |
| 471 |
} |
| 472 |
break; |
| 473 |
|
| 474 |
|
| 475 |
case ( $field_def['type'] === 'upload' && in_array( $meta_key, [ self::$meta_prefix . 'opengraph-image', self::$meta_prefix . 'twitter-image' ], true ) ): |
| 476 |
// Validate as url. |
| 477 |
$url = WPSEO_Utils::sanitize_url( $meta_value, [ 'http', 'https', 'ftp', 'ftps' ] ); |
| 478 |
if ( $url !== '' ) { |
| 479 |
$clean = $url; |
| 480 |
} |
| 481 |
break; |
| 482 |
|
| 483 |
case ( $field_def['type'] === 'hidden' && $meta_key === self::$meta_prefix . 'is_cornerstone' ): |
| 484 |
$clean = $meta_value; |
| 485 |
|
| 486 |
/* |
| 487 |
* This used to be a checkbox, then became a hidden input. |
| 488 |
* To make sure the value remains consistent, we cast 'true' to '1'. |
| 489 |
*/ |
| 490 |
if ( $meta_value === 'true' ) { |
| 491 |
$clean = '1'; |
| 492 |
} |
| 493 |
break; |
| 494 |
|
| 495 |
case ( $field_def['type'] === 'hidden' && isset( $field_def['options'] ) ): |
| 496 |
// Only allow value if it's one of the predefined options. |
| 497 |
if ( isset( $field_def['options'][ $meta_value ] ) ) { |
| 498 |
$clean = $meta_value; |
| 499 |
} |
| 500 |
break; |
| 501 |
|
| 502 |
case ( $field_def['type'] === 'textarea' ): |
| 503 |
if ( is_string( $meta_value ) ) { |
| 504 |
// Remove line breaks and tabs. |
| 505 |
// @todo [JRF => Yoast] Verify that line breaks and the likes aren't allowed/recommended in meta header fields. |
| 506 |
$meta_value = str_replace( [ "\n", "\r", "\t", ' ' ], ' ', $meta_value ); |
| 507 |
$clean = WPSEO_Utils::sanitize_text_field( trim( $meta_value ) ); |
| 508 |
} |
| 509 |
break; |
| 510 |
|
| 511 |
case ( $field_def['type'] === 'multiselect' ): |
| 512 |
$clean = $meta_value; |
| 513 |
break; |
| 514 |
|
| 515 |
|
| 516 |
case ( $field_def['type'] === 'text' ): |
| 517 |
default: |
| 518 |
if ( is_string( $meta_value ) ) { |
| 519 |
$clean = WPSEO_Utils::sanitize_text_field( trim( $meta_value ) ); |
| 520 |
} |
| 521 |
|
| 522 |
break; |
| 523 |
} |
| 524 |
|
| 525 |
$clean = apply_filters( 'wpseo_sanitize_post_meta_' . $meta_key, $clean, $meta_value, $field_def, $meta_key ); |
| 526 |
|
| 527 |
return $clean; |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* Validate a meta-robots-adv meta value. |
| 532 |
* |
| 533 |
* @todo [JRF => Yoast] Verify that this logic for the prioritisation is correct. |
| 534 |
* |
| 535 |
* @param array|string $meta_value The value to validate. |
| 536 |
* |
| 537 |
* @return string Clean value. |
| 538 |
*/ |
| 539 |
public static function validate_meta_robots_adv( $meta_value ) { |
| 540 |
$clean = self::$meta_fields['advanced']['meta-robots-adv']['default_value']; |
| 541 |
$options = self::$meta_fields['advanced']['meta-robots-adv']['options']; |
| 542 |
|
| 543 |
if ( is_string( $meta_value ) ) { |
| 544 |
$meta_value = explode( ',', $meta_value ); |
| 545 |
} |
| 546 |
|
| 547 |
if ( is_array( $meta_value ) && $meta_value !== [] ) { |
| 548 |
$meta_value = array_map( 'trim', $meta_value ); |
| 549 |
|
| 550 |
// Individual selected entries. |
| 551 |
$cleaning = []; |
| 552 |
foreach ( $meta_value as $value ) { |
| 553 |
if ( isset( $options[ $value ] ) ) { |
| 554 |
$cleaning[] = $value; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
if ( $cleaning !== [] ) { |
| 559 |
$clean = implode( ',', $cleaning ); |
| 560 |
} |
| 561 |
unset( $cleaning, $value ); |
| 562 |
} |
| 563 |
|
| 564 |
return $clean; |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* Prevent saving of default values and remove potential old value from the database if replaced by a default. |
| 569 |
* |
| 570 |
* @param bool $check The current status to allow updating metadata for the given type. |
| 571 |
* @param int $object_id ID of the current object for which the meta is being updated. |
| 572 |
* @param string $meta_key The full meta key (including prefix). |
| 573 |
* @param string $meta_value New meta value. |
| 574 |
* @param string $prev_value The old meta value. |
| 575 |
* |
| 576 |
* @return bool|null True = stop saving, null = continue saving. |
| 577 |
*/ |
| 578 |
public static function remove_meta_if_default( $check, $object_id, $meta_key, $meta_value, $prev_value = '' ) { |
| 579 |
/* If it's one of our meta fields, check against default. */ |
| 580 |
if ( isset( self::$fields_index[ $meta_key ] ) && self::meta_value_is_default( $meta_key, $meta_value ) === true ) { |
| 581 |
if ( $prev_value !== '' ) { |
| 582 |
delete_post_meta( $object_id, $meta_key, $prev_value ); |
| 583 |
} |
| 584 |
else { |
| 585 |
delete_post_meta( $object_id, $meta_key ); |
| 586 |
} |
| 587 |
|
| 588 |
return true; // Stop saving the value. |
| 589 |
} |
| 590 |
|
| 591 |
return $check; // Go on with the normal execution (update) in meta.php. |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
* Prevent adding of default values to the database. |
| 596 |
* |
| 597 |
* @param bool $check The current status to allow adding metadata for the given type. |
| 598 |
* @param int $object_id ID of the current object for which the meta is being added. |
| 599 |
* @param string $meta_key The full meta key (including prefix). |
| 600 |
* @param string $meta_value New meta value. |
| 601 |
* |
| 602 |
* @return bool|null True = stop saving, null = continue saving. |
| 603 |
*/ |
| 604 |
public static function dont_save_meta_if_default( $check, $object_id, $meta_key, $meta_value ) { |
| 605 |
/* If it's one of our meta fields, check against default. */ |
| 606 |
if ( isset( self::$fields_index[ $meta_key ] ) && self::meta_value_is_default( $meta_key, $meta_value ) === true ) { |
| 607 |
return true; // Stop saving the value. |
| 608 |
} |
| 609 |
|
| 610 |
return $check; // Go on with the normal execution (add) in meta.php. |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Is the given meta value the same as the default value ? |
| 615 |
* |
| 616 |
* @param string $meta_key The full meta key (including prefix). |
| 617 |
* @param mixed $meta_value The value to check. |
| 618 |
* |
| 619 |
* @return bool |
| 620 |
*/ |
| 621 |
public static function meta_value_is_default( $meta_key, $meta_value ) { |
| 622 |
return ( isset( self::$defaults[ $meta_key ] ) && $meta_value === self::$defaults[ $meta_key ] ); |
| 623 |
} |
| 624 |
|
| 625 |
/** |
| 626 |
* Get a custom post meta value. |
| 627 |
* |
| 628 |
* Returns the default value if the meta value has not been set. |
| 629 |
* |
| 630 |
* {@internal Unfortunately there isn't a filter available to hook into before returning |
| 631 |
* the results for get_post_meta(), get_post_custom() and the likes. That |
| 632 |
* would have been the preferred solution.}} |
| 633 |
* |
| 634 |
* @param string $key Internal key of the value to get (without prefix). |
| 635 |
* @param int $postid Post ID of the post to get the value for. |
| 636 |
* |
| 637 |
* @return string All 'normal' values returned from get_post_meta() are strings. |
| 638 |
* Objects and arrays are possible, but not used by this plugin |
| 639 |
* and therefore discarted (except when the special 'serialized' field def |
| 640 |
* value is set to true - only used by add-on plugins for now). |
| 641 |
* Will return the default value if no value was found. |
| 642 |
* Will return empty string if no default was found (not one of our keys) or |
| 643 |
* if the post does not exist. |
| 644 |
*/ |
| 645 |
public static function get_value( $key, $postid = 0 ) { |
| 646 |
global $post; |
| 647 |
|
| 648 |
$postid = absint( $postid ); |
| 649 |
if ( $postid === 0 ) { |
| 650 |
if ( ( isset( $post ) && is_object( $post ) ) && ( isset( $post->post_status ) && $post->post_status !== 'auto-draft' ) ) { |
| 651 |
$postid = $post->ID; |
| 652 |
} |
| 653 |
else { |
| 654 |
return ''; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
$custom = get_post_custom( $postid ); // Array of strings or empty array. |
| 659 |
$table_key = self::$meta_prefix . $key; |
| 660 |
|
| 661 |
// Populate the field_def using the field_index lookup array. |
| 662 |
$field_def = []; |
| 663 |
if ( isset( self::$fields_index[ $table_key ] ) ) { |
| 664 |
$field_def = self::$meta_fields[ self::$fields_index[ $table_key ]['subset'] ][ self::$fields_index[ $table_key ]['key'] ]; |
| 665 |
} |
| 666 |
|
| 667 |
// Check if we have a custom post meta entry. |
| 668 |
if ( isset( $custom[ $table_key ][0] ) ) { |
| 669 |
$unserialized = maybe_unserialize( $custom[ $table_key ][0] ); |
| 670 |
|
| 671 |
// Check if it is already unserialized. |
| 672 |
if ( $custom[ $table_key ][0] === $unserialized ) { |
| 673 |
return $custom[ $table_key ][0]; |
| 674 |
} |
| 675 |
|
| 676 |
// Check whether we need to unserialize it. |
| 677 |
if ( isset( $field_def['serialized'] ) && $field_def['serialized'] === true ) { |
| 678 |
// Ok, serialize value expected/allowed. |
| 679 |
return $unserialized; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
// Meta was either not found or found, but object/array while not allowed to be. |
| 684 |
if ( isset( self::$defaults[ self::$meta_prefix . $key ] ) ) { |
| 685 |
// Update the default value to the current post type. |
| 686 |
switch ( $key ) { |
| 687 |
case 'schema_page_type': |
| 688 |
case 'schema_article_type': |
| 689 |
return ''; |
| 690 |
} |
| 691 |
|
| 692 |
return self::$defaults[ self::$meta_prefix . $key ]; |
| 693 |
} |
| 694 |
|
| 695 |
/* |
| 696 |
* Shouldn't ever happen, means not one of our keys as there will always be a default available |
| 697 |
* for all our keys. |
| 698 |
*/ |
| 699 |
return ''; |
| 700 |
} |
| 701 |
|
| 702 |
/** |
| 703 |
* Update a meta value for a post. |
| 704 |
* |
| 705 |
* @param string $key The internal key of the meta value to change (without prefix). |
| 706 |
* @param mixed $meta_value The value to set the meta to. |
| 707 |
* @param int $post_id The ID of the post to change the meta for. |
| 708 |
* |
| 709 |
* @return bool Whether the value was changed. |
| 710 |
*/ |
| 711 |
public static function set_value( $key, $meta_value, $post_id ) { |
| 712 |
/* |
| 713 |
* Slash the data, because `update_metadata` will unslash it and we have already unslashed it. |
| 714 |
* Related issue: https://github.com/Yoast/YoastSEO.js/issues/2158 |
| 715 |
*/ |
| 716 |
$meta_value = wp_slash( $meta_value ); |
| 717 |
|
| 718 |
return update_post_meta( $post_id, self::$meta_prefix . $key, $meta_value ); |
| 719 |
} |
| 720 |
|
| 721 |
/** |
| 722 |
* Deletes a meta value for a post. |
| 723 |
* |
| 724 |
* @param string $key The internal key of the meta value to change (without prefix). |
| 725 |
* @param int $post_id The ID of the post to delete the meta for. |
| 726 |
* |
| 727 |
* @return bool Whether the delete was successful or not. |
| 728 |
*/ |
| 729 |
public static function delete( $key, $post_id ) { |
| 730 |
return delete_post_meta( $post_id, self::$meta_prefix . $key ); |
| 731 |
} |
| 732 |
|
| 733 |
/** |
| 734 |
* Used for imports, this functions imports the value of $old_metakey into $new_metakey for those post |
| 735 |
* where no WPSEO meta data has been set. |
| 736 |
* Optionally deletes the $old_metakey values. |
| 737 |
* |
| 738 |
* @param string $old_metakey The old key of the meta value. |
| 739 |
* @param string $new_metakey The new key, usually the WPSEO meta key (including prefix). |
| 740 |
* @param bool $delete_old Whether to delete the old meta key/value-sets. |
| 741 |
* |
| 742 |
* @return void |
| 743 |
*/ |
| 744 |
public static function replace_meta( $old_metakey, $new_metakey, $delete_old = false ) { |
| 745 |
global $wpdb; |
| 746 |
|
| 747 |
/* |
| 748 |
* Get only those rows where no wpseo meta values exist for the same post |
| 749 |
* (with the exception of linkdex as that will be set independently of whether the post has been edited). |
| 750 |
* |
| 751 |
* {@internal Query is pretty well optimized this way.}} |
| 752 |
*/ |
| 753 |
$query = $wpdb->prepare( |
| 754 |
" |
| 755 |
SELECT `a`.* |
| 756 |
FROM {$wpdb->postmeta} AS a |
| 757 |
WHERE `a`.`meta_key` = %s |
| 758 |
AND NOT EXISTS ( |
| 759 |
SELECT DISTINCT `post_id` , count( `meta_id` ) AS count |
| 760 |
FROM {$wpdb->postmeta} AS b |
| 761 |
WHERE `a`.`post_id` = `b`.`post_id` |
| 762 |
AND `meta_key` LIKE %s |
| 763 |
AND `meta_key` <> %s |
| 764 |
GROUP BY `post_id` |
| 765 |
) |
| 766 |
;", |
| 767 |
$old_metakey, |
| 768 |
$wpdb->esc_like( self::$meta_prefix . '%' ), |
| 769 |
self::$meta_prefix . 'linkdex' |
| 770 |
); |
| 771 |
$oldies = $wpdb->get_results( $query ); |
| 772 |
|
| 773 |
if ( is_array( $oldies ) && $oldies !== [] ) { |
| 774 |
foreach ( $oldies as $old ) { |
| 775 |
update_post_meta( $old->post_id, $new_metakey, $old->meta_value ); |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
// Delete old keys. |
| 780 |
if ( $delete_old === true ) { |
| 781 |
delete_post_meta_by_key( $old_metakey ); |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
* General clean-up of the saved meta values. |
| 787 |
* - Remove potentially lingering old meta keys; |
| 788 |
* - Remove all default and invalid values. |
| 789 |
* |
| 790 |
* @return void |
| 791 |
*/ |
| 792 |
public static function clean_up() { |
| 793 |
global $wpdb; |
| 794 |
|
| 795 |
/* |
| 796 |
* Clean up '_yoast_wpseo_meta-robots'. |
| 797 |
* |
| 798 |
* Retrieve all '_yoast_wpseo_meta-robots' meta values and convert if no new values found. |
| 799 |
* |
| 800 |
* {@internal Query is pretty well optimized this way.}} |
| 801 |
* |
| 802 |
* @todo [JRF => Yoast] Find out all possible values which the old '_yoast_wpseo_meta-robots' could contain |
| 803 |
* to convert the data correctly. |
| 804 |
*/ |
| 805 |
$query = $wpdb->prepare( |
| 806 |
" |
| 807 |
SELECT `a`.* |
| 808 |
FROM {$wpdb->postmeta} AS a |
| 809 |
WHERE `a`.`meta_key` = %s |
| 810 |
AND NOT EXISTS ( |
| 811 |
SELECT DISTINCT `post_id` , count( `meta_id` ) AS count |
| 812 |
FROM {$wpdb->postmeta} AS b |
| 813 |
WHERE `a`.`post_id` = `b`.`post_id` |
| 814 |
AND ( `meta_key` = %s |
| 815 |
OR `meta_key` = %s ) |
| 816 |
GROUP BY `post_id` |
| 817 |
) |
| 818 |
;", |
| 819 |
self::$meta_prefix . 'meta-robots', |
| 820 |
self::$meta_prefix . 'meta-robots-noindex', |
| 821 |
self::$meta_prefix . 'meta-robots-nofollow' |
| 822 |
); |
| 823 |
$oldies = $wpdb->get_results( $query ); |
| 824 |
|
| 825 |
if ( is_array( $oldies ) && $oldies !== [] ) { |
| 826 |
foreach ( $oldies as $old ) { |
| 827 |
$old_values = explode( ',', $old->meta_value ); |
| 828 |
foreach ( $old_values as $value ) { |
| 829 |
if ( $value === 'noindex' ) { |
| 830 |
update_post_meta( $old->post_id, self::$meta_prefix . 'meta-robots-noindex', 1 ); |
| 831 |
} |
| 832 |
elseif ( $value === 'nofollow' ) { |
| 833 |
update_post_meta( $old->post_id, self::$meta_prefix . 'meta-robots-nofollow', 1 ); |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
unset( $query, $oldies, $old, $old_values, $value ); |
| 839 |
|
| 840 |
// Delete old keys. |
| 841 |
delete_post_meta_by_key( self::$meta_prefix . 'meta-robots' ); |
| 842 |
|
| 843 |
/* |
| 844 |
* Remove all default values and (most) invalid option values. |
| 845 |
* Invalid option values for the multiselect (meta-robots-adv) field will be dealt with seperately. |
| 846 |
* |
| 847 |
* {@internal Some of the defaults have changed in v1.5, but as the defaults will |
| 848 |
* be removed and new defaults will now automatically be passed when no |
| 849 |
* data found, this update is automatic (as long as we remove the old |
| 850 |
* values which we do in the below routine).}} |
| 851 |
* |
| 852 |
* {@internal Unfortunately we can't use the normal delete_meta() with key/value combination |
| 853 |
* as '' (empty string) values will be ignored and would result in all metas |
| 854 |
* with that key being deleted, not just the empty fields. |
| 855 |
* Still, the below implementation is largely based on the delete_meta() function.}} |
| 856 |
*/ |
| 857 |
$query = []; |
| 858 |
|
| 859 |
foreach ( self::$meta_fields as $subset => $field_group ) { |
| 860 |
foreach ( $field_group as $key => $field_def ) { |
| 861 |
if ( ! isset( $field_def['default_value'] ) ) { |
| 862 |
continue; |
| 863 |
} |
| 864 |
|
| 865 |
if ( isset( $field_def['options'] ) && is_array( $field_def['options'] ) && $field_def['options'] !== [] ) { |
| 866 |
$valid = $field_def['options']; |
| 867 |
// Remove the default value from the valid options. |
| 868 |
unset( $valid[ $field_def['default_value'] ] ); |
| 869 |
$valid = array_keys( $valid ); |
| 870 |
|
| 871 |
$query[] = $wpdb->prepare( |
| 872 |
"( meta_key = %s AND meta_value NOT IN ( '" . implode( "','", esc_sql( $valid ) ) . "' ) )", |
| 873 |
self::$meta_prefix . $key |
| 874 |
); |
| 875 |
unset( $valid ); |
| 876 |
} |
| 877 |
elseif ( is_string( $field_def['default_value'] ) && $field_def['default_value'] !== '' ) { |
| 878 |
$query[] = $wpdb->prepare( |
| 879 |
'( meta_key = %s AND meta_value = %s )', |
| 880 |
self::$meta_prefix . $key, |
| 881 |
$field_def['default_value'] |
| 882 |
); |
| 883 |
} |
| 884 |
else { |
| 885 |
$query[] = $wpdb->prepare( |
| 886 |
"( meta_key = %s AND meta_value = '' )", |
| 887 |
self::$meta_prefix . $key |
| 888 |
); |
| 889 |
} |
| 890 |
} |
| 891 |
} |
| 892 |
unset( $subset, $field_group, $key, $field_def ); |
| 893 |
|
| 894 |
$query = "SELECT meta_id FROM {$wpdb->postmeta} WHERE " . implode( ' OR ', $query ) . ';'; |
| 895 |
$meta_ids = $wpdb->get_col( $query ); |
| 896 |
|
| 897 |
if ( is_array( $meta_ids ) && $meta_ids !== [] ) { |
| 898 |
// WP native action. |
| 899 |
do_action( 'delete_post_meta', $meta_ids, null, null, null ); |
| 900 |
|
| 901 |
$query = "DELETE FROM {$wpdb->postmeta} WHERE meta_id IN( " . implode( ',', $meta_ids ) . ' )'; |
| 902 |
$count = $wpdb->query( $query ); |
| 903 |
|
| 904 |
if ( $count ) { |
| 905 |
foreach ( $meta_ids as $object_id ) { |
| 906 |
wp_cache_delete( $object_id, 'post_meta' ); |
| 907 |
} |
| 908 |
|
| 909 |
// WP native action. |
| 910 |
do_action( 'deleted_post_meta', $meta_ids, null, null, null ); |
| 911 |
} |
| 912 |
} |
| 913 |
unset( $query, $meta_ids, $count, $object_id ); |
| 914 |
|
| 915 |
/* |
| 916 |
* Deal with the multiselect (meta-robots-adv) field. |
| 917 |
* |
| 918 |
* Removes invalid option combinations, such as 'none,noarchive'. |
| 919 |
* |
| 920 |
* Default values have already been removed, so we should have a small result set and |
| 921 |
* (hopefully) even smaller set of invalid results. |
| 922 |
*/ |
| 923 |
$query = $wpdb->prepare( |
| 924 |
"SELECT meta_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s", |
| 925 |
self::$meta_prefix . 'meta-robots-adv' |
| 926 |
); |
| 927 |
$oldies = $wpdb->get_results( $query ); |
| 928 |
|
| 929 |
if ( is_array( $oldies ) && $oldies !== [] ) { |
| 930 |
foreach ( $oldies as $old ) { |
| 931 |
$clean = self::validate_meta_robots_adv( $old->meta_value ); |
| 932 |
|
| 933 |
if ( $clean !== $old->meta_value ) { |
| 934 |
if ( $clean !== self::$meta_fields['advanced']['meta-robots-adv']['default_value'] ) { |
| 935 |
update_metadata_by_mid( 'post', $old->meta_id, $clean ); |
| 936 |
} |
| 937 |
else { |
| 938 |
delete_metadata_by_mid( 'post', $old->meta_id ); |
| 939 |
} |
| 940 |
} |
| 941 |
} |
| 942 |
} |
| 943 |
unset( $query, $oldies, $old, $clean ); |
| 944 |
|
| 945 |
do_action( 'wpseo_meta_clean_up' ); |
| 946 |
} |
| 947 |
|
| 948 |
/** |
| 949 |
* Recursively merge a variable number of arrays, using the left array as base, |
| 950 |
* giving priority to the right array. |
| 951 |
* |
| 952 |
* Difference with native array_merge_recursive(): |
| 953 |
* array_merge_recursive converts values with duplicate keys to arrays rather than |
| 954 |
* overwriting the value in the first array with the duplicate value in the second array. |
| 955 |
* |
| 956 |
* array_merge_recursive_distinct does not change the data types of the values in the arrays. |
| 957 |
* Matching keys' values in the second array overwrite those in the first array, as is the |
| 958 |
* case with array_merge. |
| 959 |
* |
| 960 |
* Freely based on information found on http://www.php.net/manual/en/function.array-merge-recursive.php |
| 961 |
* |
| 962 |
* {@internal Should be moved to a general utility class.}} |
| 963 |
* |
| 964 |
* @return array |
| 965 |
*/ |
| 966 |
public static function array_merge_recursive_distinct() { |
| 967 |
|
| 968 |
$arrays = func_get_args(); |
| 969 |
if ( count( $arrays ) < 2 ) { |
| 970 |
if ( $arrays === [] ) { |
| 971 |
return []; |
| 972 |
} |
| 973 |
else { |
| 974 |
return $arrays[0]; |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
$merged = array_shift( $arrays ); |
| 979 |
|
| 980 |
foreach ( $arrays as $array ) { |
| 981 |
foreach ( $array as $key => $value ) { |
| 982 |
if ( is_array( $value ) && ( isset( $merged[ $key ] ) && is_array( $merged[ $key ] ) ) ) { |
| 983 |
$merged[ $key ] = self::array_merge_recursive_distinct( $merged[ $key ], $value ); |
| 984 |
} |
| 985 |
else { |
| 986 |
$merged[ $key ] = $value; |
| 987 |
} |
| 988 |
} |
| 989 |
unset( $key, $value ); |
| 990 |
} |
| 991 |
|
| 992 |
return $merged; |
| 993 |
} |
| 994 |
|
| 995 |
/** |
| 996 |
* Counts the total of all the keywords being used for posts except the given one. |
| 997 |
* |
| 998 |
* @param string $keyword The keyword to be counted. |
| 999 |
* @param int $post_id The id of the post to which the keyword belongs. |
| 1000 |
* |
| 1001 |
* @return array |
| 1002 |
*/ |
| 1003 |
public static function keyword_usage( $keyword, $post_id ) { |
| 1004 |
|
| 1005 |
if ( empty( $keyword ) ) { |
| 1006 |
return []; |
| 1007 |
} |
| 1008 |
|
| 1009 |
/** |
| 1010 |
* The indexable repository. |
| 1011 |
* |
| 1012 |
* @var Indexable_Repository |
| 1013 |
*/ |
| 1014 |
$repository = YoastSEO()->classes->get( Indexable_Repository::class ); |
| 1015 |
|
| 1016 |
$post_ids = $repository->query() |
| 1017 |
->select( 'object_id' ) |
| 1018 |
->where( 'primary_focus_keyword', $keyword ) |
| 1019 |
->where( 'object_type', 'post' ) |
| 1020 |
->where_not_equal( 'object_id', $post_id ) |
| 1021 |
->limit( 2 ) |
| 1022 |
->find_array(); |
| 1023 |
|
| 1024 |
$callback = static function ( $row ) { |
| 1025 |
return (int) $row['object_id']; |
| 1026 |
}; |
| 1027 |
$post_ids = array_map( $callback, $post_ids ); |
| 1028 |
|
| 1029 |
/* |
| 1030 |
* If Yoast SEO Premium is active, get the additional keywords as well. |
| 1031 |
* We only check for the additional keywords if we've not already found two. |
| 1032 |
* In that case there's no use for an additional query as we already know |
| 1033 |
* that the keyword has been used multiple times before. |
| 1034 |
*/ |
| 1035 |
if ( YoastSEO()->helpers->product->is_premium() && count( $post_ids ) < 2 ) { |
| 1036 |
$query = [ |
| 1037 |
'meta_query' => [ |
| 1038 |
[ |
| 1039 |
'key' => '_yoast_wpseo_focuskeywords', |
| 1040 |
'value' => sprintf( '"keyword":"%s"', $keyword ), |
| 1041 |
'compare' => 'LIKE', |
| 1042 |
], |
| 1043 |
], |
| 1044 |
'post__not_in' => [ $post_id ], |
| 1045 |
'fields' => 'ids', |
| 1046 |
'post_type' => 'any', |
| 1047 |
|
| 1048 |
/* |
| 1049 |
* We only need to return zero, one or two results: |
| 1050 |
* - Zero: keyword hasn't been used before |
| 1051 |
* - One: Keyword has been used once before |
| 1052 |
* - Two or more: Keyword has been used twice or more before |
| 1053 |
*/ |
| 1054 |
'posts_per_page' => 2, |
| 1055 |
]; |
| 1056 |
$get_posts = new WP_Query( $query ); |
| 1057 |
$post_ids = array_merge( $post_ids, $get_posts->posts ); |
| 1058 |
} |
| 1059 |
|
| 1060 |
return $post_ids; |
| 1061 |
} |
| 1062 |
|
| 1063 |
/** |
| 1064 |
* Filter the schema article types. |
| 1065 |
* |
| 1066 |
* @return void |
| 1067 |
*/ |
| 1068 |
public static function filter_schema_article_types() { |
| 1069 |
/** This filter is documented in inc/options/class-wpseo-option-titles.php */ |
| 1070 |
self::$meta_fields['schema']['schema_article_type']['options'] = apply_filters( 'wpseo_schema_article_types', self::$meta_fields['schema']['schema_article_type']['options'] ); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|