123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- if ( ! class_exists( 'ACF_Location_Post_Taxonomy' ) ) :
- class ACF_Location_Post_Taxonomy extends ACF_Location {
-
- public function initialize() {
- $this->name = 'post_taxonomy';
- $this->label = __( 'Post Taxonomy', 'acf' );
- $this->category = 'post';
- $this->object_type = 'post';
- }
-
- public function match( $rule, $screen, $field_group ) {
-
- if ( isset( $screen['post_id'] ) ) {
- $post_id = $screen['post_id'];
- } elseif ( isset( $screen['attachment_id'] ) ) {
- $post_id = $screen['attachment_id'];
- } else {
- return false;
- }
-
- $term = acf_get_term( $rule['value'] );
- if ( ! $term || is_wp_error( $term ) ) {
- return false;
- }
-
- if ( isset( $screen['post_terms'] ) ) {
- $post_terms = acf_maybe_get( $screen['post_terms'], $term->taxonomy, array() );
- } else {
- $post_terms = wp_get_post_terms( $post_id, $term->taxonomy, array( 'fields' => 'ids' ) );
- }
-
- if ( ! $post_terms && $term->taxonomy == 'category' ) {
- $post_terms = array( 1 );
- }
-
- $result = ( in_array( $term->term_id, $post_terms ) || in_array( $term->slug, $post_terms ) );
-
- if ( $rule['operator'] === '!=' ) {
- return ! $result;
- }
- return $result;
- }
-
- public function get_values( $rule ) {
- return acf_get_taxonomy_terms();
- }
-
- public function get_object_subtype( $rule ) {
- if ( $rule['operator'] === '==' ) {
- $term = acf_decode_term( $rule['value'] );
- if ( $term ) {
- $taxonomy = get_taxonomy( $term['taxonomy'] );
- if ( $taxonomy ) {
- return $taxonomy->object_type;
- }
- }
- }
- return '';
- }
- }
-
- acf_register_location_type( 'ACF_Location_Post_Taxonomy' );
- endif;
|