예제 #1
0
 public function __construct()
 {
     $args = func_get_args();
     call_user_func_array(array($this, 'parent::__construct'), $args);
     $this->terms = get_terms($this->field_id, array('hide_empty' => 0));
     $this->ordered_terms = array();
     if (!is_wp_error($this->terms)) {
         /**
          * Re-order the terms hierarchically.
          */
         wpas_sort_terms_hierarchicaly($this->terms, $this->ordered_terms);
     }
 }
예제 #2
0
 public function __construct($field_id, $field)
 {
     /* Call the parent constructor */
     parent::__construct($field_id, $field);
     $args = func_get_args();
     call_user_func_array(array($this, 'parent::__construct'), $args);
     $this->terms = get_terms($this->field_id, array('hide_empty' => 0));
     $this->ordered_terms = array();
     $this->field_args['select2'] = isset($this->field_args['select2']) ? (bool) $this->field_args['select2'] : false;
     if (!is_wp_error($this->terms)) {
         /**
          * Re-order the terms hierarchically.
          */
         wpas_sort_terms_hierarchicaly($this->terms, $this->ordered_terms);
     }
     if (true === $this->field_args['select2']) {
         add_filter('wpas_cf_field_class', array($this, 'add_select2_class'), 10, 2);
     }
 }
예제 #3
0
/**
 * Recursively sort an array of taxonomy terms hierarchically. Child categories will be
 * placed under a 'children' member of their parent term.
 *
 * @since  3.0.1
 *
 * @param Array   $cats     taxonomy term objects to sort
 * @param Array   $into     result array to put them in
 * @param integer $parentId the current parent ID to put them in
 *
 * @link   http://wordpress.stackexchange.com/a/99516/16176
 */
function wpas_sort_terms_hierarchicaly(&$cats = array(), &$into = array(), $parentId = 0)
{
    foreach ($cats as $i => $cat) {
        if ($cat->parent == $parentId) {
            $into[$cat->term_id] = $cat;
            unset($cats[$i]);
        }
    }
    foreach ($into as $topCat) {
        $topCat->children = array();
        wpas_sort_terms_hierarchicaly($cats, $topCat->children, $topCat->term_id);
    }
}
예제 #4
0
    /**
     * "Fake" taxonomy select.
     * 
     * @param  array $field Field options
     * @since  3.0.0
     */
    public static function taxonomy($field)
    {
        global $post;
        $field_id = 'wpas_' . $field['name'];
        $label = wpas_get_field_title($field);
        $current = get_the_terms($post->ID, sanitize_text_field($field['name']));
        $terms = get_terms(sanitize_text_field($field['name']), array('hide_empty' => 0));
        $value = '';
        $ordered_terms = array();
        if (is_array($current)) {
            foreach ($current as $term) {
                $value = $term->slug;
            }
        }
        /* In case the taxonomy does not exist */
        if (is_wp_error($terms)) {
            return;
        }
        /**
         * Re-order the terms hierarchically.
         */
        wpas_sort_terms_hierarchicaly($terms, $ordered_terms);
        ?>

		<div <?php 
        wpas_get_field_container_class($field_id);
        ?>
 id="<?php 
        echo $field_id;
        ?>
_container">
			<label for="<?php 
        echo $field_id;
        ?>
"><strong><?php 
        echo $label;
        ?>
</strong></label>

			<?php 
        if (!is_admin() || current_user_can($field['args']['capability'])) {
            ?>

				<select name="<?php 
            echo $field_id;
            ?>
" id="<?php 
            echo $field_id;
            ?>
" <?php 
            wpas_get_field_class($field_id);
            ?>
>
					<option value=""><?php 
            _e('Please select', 'wpas');
            ?>
</option>

					<?php 
            foreach ($ordered_terms as $term) {
                wpas_hierarchical_taxonomy_dropdown_options($term, $value);
            }
            ?>

				</select>

			<?php 
        } else {
            ?>
				<p id="<?php 
            echo $field_id;
            ?>
"><?php 
            echo $value;
            ?>
</p>
			<?php 
        }
        if (isset($field['args']['desc']) && '' != $field['args']['desc'] && WPAS_FIELDS_DESC) {
            ?>
<p class="<?php 
            echo is_admin() ? 'description' : 'wpas-help-block';
            ?>
"><?php 
            echo wp_kses($field['args']['desc']);
            ?>
</p><?php 
        }
        ?>
		</div>

	<?php 
    }