function acf_location_rule_match_category_ancestor($match, $rule, $options)
{
    // most of this copied directly from acf post category rule
    $terms = $options['post_taxonomy'];
    $data = acf_decode_taxonomy_term($rule['value']);
    $term = get_term_by('slug', $data['term'], $data['taxonomy']);
    if (!$term && is_numeric($data['term'])) {
        $term = get_term_by('id', $data['term'], $data['taxonomy']);
    }
    // this is where it's different than ACf
    // get terms so we can look at the parents
    if (is_array($terms)) {
        foreach ($terms as $index => $term_id) {
            $terms[$index] = get_term_by('id', intval($term_id), $term->taxonomy);
        }
    }
    if (!is_array($terms) && $options['post_id']) {
        $terms = wp_get_post_terms(intval($options['post_id']), $term->taxonomy);
    }
    if (!is_array($terms)) {
        $terms = array($terms);
    }
    $terms = array_filter($terms);
    $match = false;
    // collect a list of ancestors
    $ancestors = array();
    if (count($terms)) {
        foreach ($terms as $term_to_check) {
            $ancestors = array_merge(get_ancestors($term_to_check->term_id, $term->taxonomy));
        }
        // end foreach terms
    }
    // end if
    // see if the rule matches any term ancetor
    if ($term && in_array($term->term_id, $ancestors)) {
        $match = true;
    }
    if ($rule['operator'] == '!=') {
        // reverse the result
        $match = !$match;
    }
    return $match;
}
예제 #2
0
function acf_decode_taxonomy_terms($terms = false)
{
    // load all taxonomies if not specified in args
    if (!$terms) {
        $terms = acf_get_taxonomy_terms();
    }
    // vars
    $r = array();
    foreach ($terms as $term) {
        // vars
        $data = acf_decode_taxonomy_term($term);
        // create empty array
        if (!array_key_exists($data['taxonomy'], $r)) {
            $r[$data['taxonomy']] = array();
        }
        // append to taxonomy
        $r[$data['taxonomy']][] = $data['term'];
    }
    // return
    return $r;
}
예제 #3
0
 function get_choices($options = array())
 {
     // defaults
     $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'post_type' => '', 'taxonomy' => '', 'lang' => false, 'field_key' => '', 'paged' => 1));
     // vars
     $r = array();
     $args = array();
     // paged
     $args['posts_per_page'] = 20;
     $args['paged'] = $options['paged'];
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         return false;
     }
     // WPML
     if ($options['lang']) {
         global $sitepress;
         if (!empty($sitepress)) {
             $sitepress->switch_lang($options['lang']);
         }
     }
     // update $args
     if (!empty($options['post_type'])) {
         $args['post_type'] = acf_force_type_array($options['post_type']);
     } elseif (!empty($field['post_type'])) {
         $args['post_type'] = acf_force_type_array($field['post_type']);
     } else {
         $args['post_type'] = acf_get_post_types();
     }
     // update taxonomy
     $taxonomies = array();
     if (!empty($options['taxonomy'])) {
         $term = acf_decode_taxonomy_term($options['taxonomy']);
         // append to $args
         $args['tax_query'] = array(array('taxonomy' => $term['taxonomy'], 'field' => 'slug', 'terms' => $term['term']));
     } elseif (!empty($field['taxonomy'])) {
         $taxonomies = acf_decode_taxonomy_terms($field['taxonomy']);
         // append to $args
         $args['tax_query'] = array();
         // now create the tax queries
         foreach ($taxonomies as $taxonomy => $terms) {
             $args['tax_query'][] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms);
         }
     }
     // search
     if ($options['s']) {
         $args['s'] = $options['s'];
     }
     // filters
     $args = apply_filters('acf/fields/relationship/query', $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/relationship/query/name=' . $field['name'], $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/relationship/query/key=' . $field['key'], $args, $field, $options['post_id']);
     // get posts grouped by post type
     $groups = acf_get_grouped_posts($args);
     if (!empty($groups)) {
         foreach (array_keys($groups) as $group_title) {
             // vars
             $posts = acf_extract_var($groups, $group_title);
             $titles = array();
             // data
             $data = array('text' => $group_title, 'children' => array());
             foreach (array_keys($posts) as $post_id) {
                 // override data
                 $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id']);
             }
             // order by search
             if (!empty($args['s'])) {
                 $posts = acf_order_by_search($posts, $args['s']);
             }
             // append to $data
             foreach (array_keys($posts) as $post_id) {
                 $data['children'][] = array('id' => $post_id, 'text' => $posts[$post_id]);
             }
             // append to $r
             $r[] = $data;
         }
         // optgroup or single
         $post_types = acf_force_type_array($args['post_type']);
         // add as optgroup or results
         if (count($post_types) == 1) {
             $r = $r[0]['children'];
         }
     }
     // return
     return $r;
 }
예제 #4
0
 function rule_match_post_taxonomy($match, $rule, $options)
 {
     // bail early if not a post
     if (!$options['post_id']) {
         return false;
     }
     // vars
     $terms = $options['post_taxonomy'];
     // get term data
     // - selected term may have a numeric slug '123' (user reported on forum), so always check slug first
     $data = acf_decode_taxonomy_term($rule['value']);
     $term = get_term_by('slug', $data['term'], $data['taxonomy']);
     // attempt get term via ID (ACF4 uses ID)
     if (!$term && is_numeric($data['term'])) {
         $term = get_term_by('id', $data['term'], $data['taxonomy']);
     }
     // bail early if no term
     if (!$term) {
         return false;
     }
     // post type
     if (!$options['post_type']) {
         $options['post_type'] = get_post_type($options['post_id']);
     }
     // get terms
     // - allow an empty array (sent via JS) to avoid loading the real post's terms
     if (!is_array($terms)) {
         $terms = wp_get_post_terms($options['post_id'], $term->taxonomy, array('fields' => 'ids'));
     }
     // If no terms, this is a new post and should be treated as if it has the "Uncategorized" (1) category ticked
     if (empty($terms)) {
         if (is_object_in_taxonomy($options['post_type'], 'category')) {
             $terms = array(1);
         }
     }
     // compare
     if ($rule['operator'] == "==") {
         $match = in_array($term->term_id, $terms);
     } elseif ($rule['operator'] == "!=") {
         $match = !in_array($term->term_id, $terms);
     }
     // return
     return $match;
 }
예제 #5
0
function acf_decode_taxonomy_terms($strings = false)
{
    // bail early if no terms
    if (empty($strings)) {
        return false;
    }
    // vars
    $terms = array();
    // loop
    foreach ($strings as $string) {
        // vars
        $data = acf_decode_taxonomy_term($string);
        $taxonomy = $data['taxonomy'];
        $term = $data['term'];
        // create empty array
        if (!isset($terms[$taxonomy])) {
            $terms[$taxonomy] = array();
        }
        // append
        $terms[$taxonomy][] = $term;
    }
    // return
    return $terms;
}
예제 #6
0
 function rule_match_post_taxonomy($match, $rule, $options)
 {
     // validate
     if (!$options['post_id']) {
         return false;
     }
     // vars
     $terms = $options['post_taxonomy'];
     // get term data
     $data = acf_decode_taxonomy_term($rule['value']);
     $field = is_numeric($data['term']) ? 'id' : 'slug';
     $term = get_term_by($field, $data['term'], $data['taxonomy']);
     // validate term
     if (empty($term)) {
         return false;
     }
     // post type
     if (!$options['post_type']) {
         $options['post_type'] = get_post_type($options['post_id']);
     }
     // get terms
     if (!$options['ajax']) {
         $terms = wp_get_post_terms($options['post_id'], $term->taxonomy, array('fields' => 'ids'));
     }
     // If no terms, this is a new post and should be treated as if it has the "Uncategorized" (1) category ticked
     if (empty($terms)) {
         if (is_object_in_taxonomy($options['post_type'], 'category')) {
             $terms = array(1);
         }
     }
     // compare
     if ($rule['operator'] == "==") {
         $match = in_array($term->term_id, $terms);
     } elseif ($rule['operator'] == "!=") {
         $match = !in_array($term->term_id, $terms);
     }
     // return
     return $match;
 }
예제 #7
0
 function get_ajax_query($options = array())
 {
     // defaults
     $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 1, 'post_type' => '', 'taxonomy' => ''));
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         return false;
     }
     // vars
     $results = array();
     $args = array();
     $s = false;
     $is_search = false;
     // paged
     $args['posts_per_page'] = 20;
     $args['paged'] = $options['paged'];
     // search
     if ($options['s'] !== '') {
         // strip slashes (search may be integer)
         $s = wp_unslash(strval($options['s']));
         // update vars
         $args['s'] = $s;
         $is_search = true;
     }
     // post_type
     if (!empty($options['post_type'])) {
         $args['post_type'] = acf_get_array($options['post_type']);
     } elseif (!empty($field['post_type'])) {
         $args['post_type'] = acf_get_array($field['post_type']);
     } else {
         $args['post_type'] = acf_get_post_types();
     }
     // taxonomy
     if (!empty($options['taxonomy'])) {
         // vars
         $term = acf_decode_taxonomy_term($options['taxonomy']);
         // tax query
         $args['tax_query'] = array();
         // append
         $args['tax_query'][] = array('taxonomy' => $term['taxonomy'], 'field' => 'slug', 'terms' => $term['term']);
     } elseif (!empty($field['taxonomy'])) {
         // vars
         $terms = acf_decode_taxonomy_terms($field['taxonomy']);
         // append to $args
         $args['tax_query'] = array();
         // now create the tax queries
         foreach ($terms as $k => $v) {
             $args['tax_query'][] = array('taxonomy' => $k, 'field' => 'slug', 'terms' => $v);
         }
     }
     // filters
     $args = apply_filters('acf/fields/relationship/query', $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/relationship/query/name=' . $field['name'], $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/relationship/query/key=' . $field['key'], $args, $field, $options['post_id']);
     // get posts grouped by post type
     $groups = acf_get_grouped_posts($args);
     // bail early if no posts
     if (empty($groups)) {
         return false;
     }
     // loop
     foreach (array_keys($groups) as $group_title) {
         // vars
         $posts = acf_extract_var($groups, $group_title);
         // data
         $data = array('text' => $group_title, 'children' => array());
         // convert post objects to post titles
         foreach (array_keys($posts) as $post_id) {
             $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id']);
         }
         // order posts by search
         if ($is_search && empty($args['orderby'])) {
             $posts = acf_order_by_search($posts, $args['s']);
         }
         // append to $data
         foreach (array_keys($posts) as $post_id) {
             $data['children'][] = $this->get_post_result($post_id, $posts[$post_id]);
         }
         // append to $results
         $results[] = $data;
     }
     // add as optgroup or results
     if (count($args['post_type']) == 1) {
         $results = $results[0]['children'];
     }
     // vars
     $response = array('results' => $results, 'limit' => $args['posts_per_page']);
     // return
     return $response;
 }