class-acf-location-post-taxonomy.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Location_Post_Taxonomy' ) ) :
  6. class ACF_Location_Post_Taxonomy extends ACF_Location {
  7. /**
  8. * Initializes props.
  9. *
  10. * @date 5/03/2014
  11. * @since 5.0.0
  12. *
  13. * @param void
  14. * @return void
  15. */
  16. public function initialize() {
  17. $this->name = 'post_taxonomy';
  18. $this->label = __( 'Post Taxonomy', 'acf' );
  19. $this->category = 'post';
  20. $this->object_type = 'post';
  21. }
  22. /**
  23. * Matches the provided rule against the screen args returning a bool result.
  24. *
  25. * @date 9/4/20
  26. * @since 5.9.0
  27. *
  28. * @param array $rule The location rule.
  29. * @param array $screen The screen args.
  30. * @param array $field_group The field group settings.
  31. * @return bool
  32. */
  33. public function match( $rule, $screen, $field_group ) {
  34. // Check screen args.
  35. if ( isset( $screen['post_id'] ) ) {
  36. $post_id = $screen['post_id'];
  37. } elseif ( isset( $screen['attachment_id'] ) ) {
  38. $post_id = $screen['attachment_id'];
  39. } else {
  40. return false;
  41. }
  42. // Get WP_Term from rule value.
  43. $term = acf_get_term( $rule['value'] );
  44. if ( ! $term || is_wp_error( $term ) ) {
  45. return false;
  46. }
  47. // Get terms connected to post.
  48. if ( isset( $screen['post_terms'] ) ) {
  49. $post_terms = acf_maybe_get( $screen['post_terms'], $term->taxonomy, array() );
  50. } else {
  51. $post_terms = wp_get_post_terms( $post_id, $term->taxonomy, array( 'fields' => 'ids' ) );
  52. }
  53. // If no post terms are found, and we are dealing with the "category" taxonomy, treat as default "Uncategorized" category.
  54. if ( ! $post_terms && $term->taxonomy == 'category' ) {
  55. $post_terms = array( 1 );
  56. }
  57. // Search $post_terms for a match.
  58. $result = ( in_array( $term->term_id, $post_terms ) || in_array( $term->slug, $post_terms ) );
  59. // Reverse result for "!=" operator.
  60. if ( $rule['operator'] === '!=' ) {
  61. return ! $result;
  62. }
  63. return $result;
  64. }
  65. /**
  66. * Returns an array of possible values for this rule type.
  67. *
  68. * @date 9/4/20
  69. * @since 5.9.0
  70. *
  71. * @param array $rule A location rule.
  72. * @return array
  73. */
  74. public function get_values( $rule ) {
  75. return acf_get_taxonomy_terms();
  76. }
  77. /**
  78. * Returns the object_subtype connected to this location.
  79. *
  80. * @date 1/4/20
  81. * @since 5.9.0
  82. *
  83. * @param array $rule A location rule.
  84. * @return string|array
  85. */
  86. public function get_object_subtype( $rule ) {
  87. if ( $rule['operator'] === '==' ) {
  88. $term = acf_decode_term( $rule['value'] );
  89. if ( $term ) {
  90. $taxonomy = get_taxonomy( $term['taxonomy'] );
  91. if ( $taxonomy ) {
  92. return $taxonomy->object_type;
  93. }
  94. }
  95. }
  96. return '';
  97. }
  98. }
  99. // initialize
  100. acf_register_location_type( 'ACF_Location_Post_Taxonomy' );
  101. endif; // class_exists check