class-wpseo-custom-taxonomies.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO
  6. */
  7. /**
  8. * WPSEO_Custom_Taxonomies.
  9. */
  10. class WPSEO_Custom_Taxonomies {
  11. /**
  12. * Custom taxonomies cache.
  13. *
  14. * @var array
  15. */
  16. protected static $custom_taxonomies = null;
  17. /**
  18. * Gets the names of the custom taxonomies, prepends 'ct_' and 'ct_desc', and returns them in an array.
  19. *
  20. * @return array The custom taxonomy prefixed names.
  21. */
  22. public static function get_custom_taxonomies() {
  23. // Use cached value if available.
  24. if ( ! is_null( self::$custom_taxonomies ) ) {
  25. return self::$custom_taxonomies;
  26. }
  27. self::$custom_taxonomies = [];
  28. $args = [
  29. 'public' => true,
  30. '_builtin' => false,
  31. ];
  32. $custom_taxonomies = get_taxonomies( $args, 'names', 'and' );
  33. if ( is_array( $custom_taxonomies ) ) {
  34. foreach ( $custom_taxonomies as $custom_taxonomy ) {
  35. array_push(
  36. self::$custom_taxonomies,
  37. self::add_custom_taxonomies_prefix( $custom_taxonomy ),
  38. self::add_custom_taxonomies_description_prefix( $custom_taxonomy )
  39. );
  40. }
  41. }
  42. return self::$custom_taxonomies;
  43. }
  44. /**
  45. * Adds the ct_ prefix to a taxonomy.
  46. *
  47. * @param string $taxonomy The taxonomy to prefix.
  48. *
  49. * @return string The prefixed taxonomy.
  50. */
  51. private static function add_custom_taxonomies_prefix( $taxonomy ) {
  52. return 'ct_' . $taxonomy;
  53. }
  54. /**
  55. * Adds the ct_desc_ prefix to a taxonomy.
  56. *
  57. * @param string $taxonomy The taxonomy to prefix.
  58. *
  59. * @return string The prefixed taxonomy.
  60. */
  61. private static function add_custom_taxonomies_description_prefix( $taxonomy ) {
  62. return 'ct_desc_' . $taxonomy;
  63. }
  64. }