| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* WPSEO_Custom_Fields. |
| 10 |
*/ |
| 11 |
class WPSEO_Custom_Fields { |
| 12 |
|
| 13 |
/** |
| 14 |
* Custom fields cache. |
| 15 |
* |
| 16 |
* @var array|null |
| 17 |
*/ |
| 18 |
protected static $custom_fields = null; |
| 19 |
|
| 20 |
/** |
| 21 |
* Retrieves the custom field names as an array. |
| 22 |
* |
| 23 |
* @link WordPress core: wp-admin/includes/template.php. Reused query from it. |
| 24 |
* |
| 25 |
* @return array The custom fields. |
| 26 |
*/ |
| 27 |
public static function get_custom_fields() { |
| 28 |
global $wpdb; |
| 29 |
|
| 30 |
// Use cached value if available. |
| 31 |
if ( self::$custom_fields !== null ) { |
| 32 |
return self::$custom_fields; |
| 33 |
} |
| 34 |
|
| 35 |
self::$custom_fields = []; |
| 36 |
|
| 37 |
/** |
| 38 |
* Filters the number of custom fields to retrieve for the drop-down |
| 39 |
* in the Custom Fields meta box. |
| 40 |
* |
| 41 |
* @param int $limit Number of custom fields to retrieve. Default 30. |
| 42 |
*/ |
| 43 |
$limit = apply_filters( 'postmeta_form_limit', 30 ); |
| 44 |
|
| 45 |
/** |
| 46 |
* Filter: 'wpseo_custom_fields_pre_query' - Filters the custom-fields lookup before the database query runs. |
| 47 |
* |
| 48 |
* Returning a non-null array short-circuits the default `SELECT DISTINCT meta_key` |
| 49 |
* query against `wp_postmeta`. On very large postmeta tables this can be a way |
| 50 |
* to supply a pre-cached list or an alternative query to improve loading times. |
| 51 |
* |
| 52 |
* @param string[]|null $custom_fields Pre-computed list of meta_key names, or null to run the default query. |
| 53 |
* @param int $limit The configured result limit; honor it if running a custom query. |
| 54 |
*/ |
| 55 |
$fields = apply_filters( 'wpseo_custom_fields_pre_query', null, $limit ); |
| 56 |
|
| 57 |
if ( ! is_array( $fields ) ) { |
| 58 |
$sql = "SELECT DISTINCT meta_key |
| 59 |
FROM $wpdb->postmeta |
| 60 |
WHERE meta_key NOT BETWEEN '_' AND '_z' AND SUBSTRING(meta_key, 1, 1) != '_' |
| 61 |
LIMIT %d"; |
| 62 |
$fields = $wpdb->get_col( $wpdb->prepare( $sql, $limit ) ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Filters the custom fields that are auto-completed and replaced as replacement variables |
| 67 |
* in the meta box and sidebar. |
| 68 |
* |
| 69 |
* @param string[] $fields The custom field names. |
| 70 |
*/ |
| 71 |
$fields = apply_filters( 'wpseo_replacement_variables_custom_fields', $fields ); |
| 72 |
|
| 73 |
if ( is_array( $fields ) ) { |
| 74 |
self::$custom_fields = array_map( [ 'WPSEO_Custom_Fields', 'add_custom_field_prefix' ], $fields ); |
| 75 |
} |
| 76 |
|
| 77 |
return self::$custom_fields; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Adds the cf_ prefix to a field. |
| 82 |
* |
| 83 |
* @param string $field The field to prefix. |
| 84 |
* |
| 85 |
* @return string The prefixed field. |
| 86 |
*/ |
| 87 |
private static function add_custom_field_prefix( $field ) { |
| 88 |
return 'cf_' . $field; |
| 89 |
} |
| 90 |
} |
| 91 |
|