class-wpseo-custom-fields.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO
  6. */
  7. /**
  8. * WPSEO_Custom_Fields
  9. */
  10. class WPSEO_Custom_Fields {
  11. /**
  12. * @var array Cache the custom fields.
  13. */
  14. protected static $custom_fields = null;
  15. /**
  16. * Retrieves the custom field names as an array.
  17. *
  18. * @see WordPress core: wp-admin/includes/template.php. Reused query from it.
  19. *
  20. * @return array The custom fields.
  21. */
  22. public static function get_custom_fields() {
  23. global $wpdb;
  24. // Use cached value if available.
  25. if ( ! is_null( self::$custom_fields ) ) {
  26. return self::$custom_fields;
  27. }
  28. self::$custom_fields = array();
  29. /**
  30. * Filters the number of custom fields to retrieve for the drop-down
  31. * in the Custom Fields meta box.
  32. *
  33. * @param int $limit Number of custom fields to retrieve. Default 30.
  34. */
  35. $limit = apply_filters( 'postmeta_form_limit', 30 );
  36. $sql = "SELECT DISTINCT meta_key
  37. FROM $wpdb->postmeta
  38. WHERE meta_key NOT BETWEEN '_' AND '_z'
  39. HAVING meta_key NOT LIKE %s
  40. ORDER BY meta_key
  41. LIMIT %d";
  42. $fields = $wpdb->get_col( $wpdb->prepare( $sql, $wpdb->esc_like( '_' ) . '%', $limit ) );
  43. if ( is_array( $fields ) ) {
  44. self::$custom_fields = array_map( array( 'WPSEO_Custom_Fields', 'add_custom_field_prefix' ), $fields );
  45. }
  46. return self::$custom_fields;
  47. }
  48. /**
  49. * Adds the cf_ prefix to a field.
  50. *
  51. * @param string $field The field to prefix.
  52. *
  53. * @return string The prefixed field.
  54. */
  55. private static function add_custom_field_prefix( $field ) {
  56. return 'cf_' . $field;
  57. }
  58. }