File "class-acf-field-relationship.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/advanced-custom-fields/includes/fields/class-acf-field-relationship.php
File size: 12.74 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

if (!class_exists('acf_field_relationship')) {
    class acf_field_relationship extends acf_field
    {
        function initialize()
        {
            $this->name = 'relationship';
            $this->label = __('Relationship', 'acf');
            $this->category = 'relational';
            $this->defaults = array('post_type' => array(), 'taxonomy' => array(), 'min' => 0, 'max' => 0, 'filters' => array('search', 'post_type', 'taxonomy'), 'elements' => array(), 'return_format' => 'object');
            add_action('wp_ajax_acf/fields/relationship/query', array($this, 'ajax_query'));
            add_action('wp_ajax_nopriv_acf/fields/relationship/query', array($this, 'ajax_query'));
        }
        function input_admin_enqueue_scripts()
        {
            acf_localize_text(array('Maximum values reached ( {max} values )' => __('Maximum values reached ( {max} values )', 'acf'), 'Loading' => __('Loading', 'acf'), 'No matches found' => __('No matches found', 'acf')));
        }
        function ajax_query()
        {
            if (!acf_verify_ajax()) {
                die;
            }
            $response = $this->get_ajax_query($_POST);
            acf_send_ajax_results($response);
        }
        function get_ajax_query($options = array())
        {
            $options = wp_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 1, 'post_type' => '', 'taxonomy' => ''));
            $field = acf_get_field($options['field_key']);
            if (!$field) {
                return false;
            }
            $results = array();
            $args = array();
            $s = false;
            $is_search = false;
            $args['posts_per_page'] = 20;
            $args['paged'] = intval($options['paged']);
            if ($options['s'] !== '') {
                $s = wp_unslash(strval($options['s']));
                $args['s'] = $s;
                $is_search = true;
            }
            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();
            }
            if (!empty($options['taxonomy'])) {
                $term = acf_decode_taxonomy_term($options['taxonomy']);
                $args['tax_query'] = array();
                $args['tax_query'][] = array('taxonomy' => $term['taxonomy'], 'field' => 'slug', 'terms' => $term['term']);
            } elseif (!empty($field['taxonomy'])) {
                $terms = acf_decode_taxonomy_terms($field['taxonomy']);
                $args['tax_query'] = array('relation' => 'OR');
                foreach ($terms as $k => $v) {
                    $args['tax_query'][] = array('taxonomy' => $k, 'field' => 'slug', 'terms' => $v);
                }
            }
            $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']);
            $groups = acf_get_grouped_posts($args);
            if (empty($groups)) {
                return false;
            }
            foreach (array_keys($groups) as $group_title) {
                $posts = acf_extract_var($groups, $group_title);
                $data = array('text' => $group_title, 'children' => array());
                foreach (array_keys($posts) as $post_id) {
                    $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id']);
                }
                if ($is_search && empty($args['orderby']) && isset($args['s'])) {
                    $posts = acf_order_by_search($posts, $args['s']);
                }
                foreach (array_keys($posts) as $post_id) {
                    $data['children'][] = $this->get_post_result($post_id, $posts[$post_id]);
                }
                $results[] = $data;
            }
            if (count($args['post_type']) == 1) {
                $results = $results[0]['children'];
            }
            $response = array('results' => $results, 'limit' => $args['posts_per_page']);
            return $response;
        }
        function get_post_result($id, $text)
        {
            $result = array('id' => $id, 'text' => $text);
            return $result;
        }
        function get_post_title($post, $field, $post_id = 0, $is_search = 0)
        {
            if (!$post_id) {
                $post_id = acf_get_form_data('post_id');
            }
            $title = acf_get_post_title($post, $is_search);
            if (acf_in_array('featured_image', $field['elements'])) {
                $class = 'thumbnail';
                $thumbnail = acf_get_post_thumbnail($post->ID, array(17, 17));
                if ($thumbnail['type'] == 'icon') {
                    $class .= ' -' . $thumbnail['type'];
                }
                $title = '<div class="' . $class . '">' . $thumbnail['html'] . '</div>' . $title;
            }
            $title = apply_filters('acf/fields/relationship/result', $title, $post, $field, $post_id);
            $title = apply_filters('acf/fields/relationship/result/name=' . $field['_name'], $title, $post, $field, $post_id);
            $title = apply_filters('acf/fields/relationship/result/key=' . $field['key'], $title, $post, $field, $post_id);
            return $title;
        }
        function render_field($field)
        {
            $post_type = acf_get_array($field['post_type']);
            $taxonomy = acf_get_array($field['taxonomy']);
            $filters = acf_get_array($field['filters']);
            $filter_count = count($filters);
            $filter_post_type_choices = array();
            $filter_taxonomy_choices = array();
            if (in_array('post_type', $filters)) {
                $filter_post_type_choices = array('' => __('Select post type', 'acf')) + acf_get_pretty_post_types($post_type);
            }
            if (in_array('taxonomy', $filters)) {
                $term_choices = array();
                $filter_taxonomy_choices = array('' => __('Select taxonomy', 'acf'));
                if ($taxonomy) {
                    $terms = acf_get_encoded_terms($taxonomy);
                    $term_choices = acf_get_choices_from_terms($terms, 'slug');
                } else {
                    $term_args = array();
                    if ($post_type) {
                        $term_args['taxonomy'] = acf_get_taxonomies(array('post_type' => $post_type));
                    }
                    $terms = acf_get_grouped_terms($term_args);
                    $term_choices = acf_get_choices_from_grouped_terms($terms, 'slug');
                }
                $filter_taxonomy_choices = $filter_taxonomy_choices + $term_choices;
            }
            $atts = array('id' => $field['id'], 'class' => "acf-relationship {$field['class']}", 'data-min' => $field['min'], 'data-max' => $field['max'], 'data-s' => '', 'data-paged' => 1, 'data-post_type' => '', 'data-taxonomy' => '');
            ?>
<div <?php 
            acf_esc_attr_e($atts);
            ?>>
	
			<?php 
            acf_hidden_input(array('name' => $field['name'], 'value' => ''));
            ?>
	
			<?php 
            if ($filter_count) {
                ?>
	<div class="filters -f<?php 
                echo esc_attr($filter_count);
                ?>">
				<?php 
                if (in_array('search', $filters)) {
                    ?>
		<div class="filter -search">
					<?php 
                    acf_text_input(array('placeholder' => __('Search...', 'acf'), 'data-filter' => 's'));
                    ?>
		</div>
					<?php 
                }
                if (in_array('post_type', $filters)) {
                    ?>
		<div class="filter -post_type">
					<?php 
                    acf_select_input(array('choices' => $filter_post_type_choices, 'data-filter' => 'post_type'));
                    ?>
		</div>
					<?php 
                }
                if (in_array('taxonomy', $filters)) {
                    ?>
		<div class="filter -taxonomy">
					<?php 
                    acf_select_input(array('choices' => $filter_taxonomy_choices, 'data-filter' => 'taxonomy'));
                    ?>
		</div>
				<?php 
                }
                ?>		
	</div>
			<?php 
            }
            ?>
	
	<div class="selection">
		<div class="choices">
			<ul class="acf-bl list choices-list"></ul>
		</div>
		<div class="values">
			<ul class="acf-bl list values-list">
				<?php 
            if (!empty($field['value'])) {
                $posts = acf_get_posts(array('post__in' => $field['value'], 'post_type' => $field['post_type']));
                foreach ($posts as $post) {
                    ?>
					<li>
						<?php 
                    acf_hidden_input(array('name' => $field['name'] . '[]', 'value' => $post->ID));
                    ?>
						<span data-id="<?php 
                    echo esc_attr($post->ID);
                    ?>" class="acf-rel-item">
								<?php 
                    echo acf_esc_html($this->get_post_title($post, $field));
                    ?>
							<a href="#" class="acf-icon -minus small dark" data-name="remove_item"></a>
						</span>
					</li>
						<?php 
                }
                ?>
				<?php 
            }
            ?>
			</ul>
		</div>
	</div>
</div>
			<?php 
        }
        function render_field_settings($field)
        {
            $field['min'] = empty($field['min']) ? '' : $field['min'];
            $field['max'] = empty($field['max']) ? '' : $field['max'];
            acf_render_field_setting($field, array('label' => __('Filter by Post Type', 'acf'), 'instructions' => '', 'type' => 'select', 'name' => 'post_type', 'choices' => acf_get_pretty_post_types(), 'multiple' => 1, 'ui' => 1, 'allow_null' => 1, 'placeholder' => __('All post types', 'acf')));
            acf_render_field_setting($field, array('label' => __('Filter by Taxonomy', 'acf'), 'instructions' => '', 'type' => 'select', 'name' => 'taxonomy', 'choices' => acf_get_taxonomy_terms(), 'multiple' => 1, 'ui' => 1, 'allow_null' => 1, 'placeholder' => __('All taxonomies', 'acf')));
            acf_render_field_setting($field, array('label' => __('Filters', 'acf'), 'instructions' => '', 'type' => 'checkbox', 'name' => 'filters', 'choices' => array('search' => __('Search', 'acf'), 'post_type' => __('Post Type', 'acf'), 'taxonomy' => __('Taxonomy', 'acf'))));
            acf_render_field_setting($field, array('label' => __('Elements', 'acf'), 'instructions' => __('Selected elements will be displayed in each result', 'acf'), 'type' => 'checkbox', 'name' => 'elements', 'choices' => array('featured_image' => __('Featured Image', 'acf'))));
            acf_render_field_setting($field, array('label' => __('Minimum posts', 'acf'), 'instructions' => '', 'type' => 'number', 'name' => 'min'));
            acf_render_field_setting($field, array('label' => __('Maximum posts', 'acf'), 'instructions' => '', 'type' => 'number', 'name' => 'max'));
            acf_render_field_setting($field, array('label' => __('Return Format', 'acf'), 'instructions' => '', 'type' => 'radio', 'name' => 'return_format', 'choices' => array('object' => __('Post Object', 'acf'), 'id' => __('Post ID', 'acf')), 'layout' => 'horizontal'));
        }
        function format_value($value, $post_id, $field)
        {
            if (empty($value)) {
                return $value;
            }
            $value = acf_get_array($value);
            $value = array_map('intval', $value);
            if ($field['return_format'] == 'object') {
                $value = acf_get_posts(array('post__in' => $value, 'post_type' => $field['post_type']));
            }
            return $value;
        }
        function validate_value($valid, $value, $field, $input)
        {
            if (empty($value) || !is_array($value)) {
                $value = array();
            }
            if (count($value) < $field['min']) {
                $valid = _n('%1$s requires at least %2$s selection', '%1$s requires at least %2$s selections', $field['min'], 'acf');
                $valid = sprintf($valid, $field['label'], $field['min']);
            }
            return $valid;
        }
        function update_value($value, $post_id, $field)
        {
            if (empty($value)) {
                return $value;
            }
            if (acf_is_sequential_array($value)) {
                $value = array_map('acf_idval', $value);
                $value = array_map('strval', $value);
            } else {
                $value = acf_idval($value);
            }
            return $value;
        }
    }
    acf_register_field_type('acf_field_relationship');
}