Example #1
0
/**
 * Updates an existing Category or creates a new Category.
 *
 * @since 2.0.0
 * @since 2.5.0 $wp_error parameter was added.
 * @since 3.0.0 The 'taxonomy' argument was added.
 *
 * @param array $catarr {
 *     Array of arguments for inserting a new category.
 *
 *     @type int        $cat_ID               Categoriy ID. A non-zero value updates an existing category.
 *                                            Default 0.
 *     @type string     $taxonomy             Taxonomy slug. Defualt 'category'.
 *     @type string     $cat_nam              Category name. Default empty.
 *     @type string     $category_description Category description. Default empty.
 *     @type string     $category_nicename    Category nice (display) name. Default empty.
 *     @type int|string $category_parent      Category parent ID. Default empty.
 * }
 * @param bool  $wp_error Optional. Default false.
 * @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure,
 *                    depending on param $wp_error.
 */
function wp_insert_category($catarr, $wp_error = false)
{
    $cat_defaults = array('cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
    $catarr = wp_parse_args($catarr, $cat_defaults);
    if (trim($catarr['cat_name']) == '') {
        if (!$wp_error) {
            return 0;
        } else {
            return new WP_Error('cat_name', __('You did not enter a category name.'));
        }
    }
    $catarr['cat_ID'] = (int) $catarr['cat_ID'];
    // Are we updating or creating?
    $update = !empty($catarr['cat_ID']);
    $name = $catarr['cat_name'];
    $description = $catarr['category_description'];
    $slug = $catarr['category_nicename'];
    $parent = (int) $catarr['category_parent'];
    if ($parent < 0) {
        $parent = 0;
    }
    if (empty($parent) || !term_exists($parent, $catarr['taxonomy']) || $catarr['cat_ID'] && term_is_ancestor_of($catarr['cat_ID'], $parent, $catarr['taxonomy'])) {
        $parent = 0;
    }
    $args = compact('name', 'slug', 'parent', 'description');
    if ($update) {
        $catarr['cat_ID'] = wp_update_term($catarr['cat_ID'], $catarr['taxonomy'], $args);
    } else {
        $catarr['cat_ID'] = wp_insert_term($catarr['cat_name'], $catarr['taxonomy'], $args);
    }
    if (is_wp_error($catarr['cat_ID'])) {
        if ($wp_error) {
            return $catarr['cat_ID'];
        } else {
            return 0;
        }
    }
    return $catarr['cat_ID']['term_id'];
}
/**
 * Check if a term is an ancestor of another term.
 *
 * You can use either an id or the term object for both parameters.
 *
 * @since 3.4.0
 *
 * @param int|object $term1    ID or object to check if this is the parent term.
 * @param int|object $term2    The child term.
 * @param string     $taxonomy Taxonomy name that $term1 and `$term2` belong to.
 * @return bool Whether `$term2` is a child of `$term1`.
 */
function term_is_ancestor_of($term1, $term2, $taxonomy)
{
    if (!isset($term1->term_id)) {
        $term1 = get_term($term1, $taxonomy);
    }
    if (!isset($term2->parent)) {
        $term2 = get_term($term2, $taxonomy);
    }
    if (empty($term1->term_id) || empty($term2->parent)) {
        return false;
    }
    if ($term2->parent == $term1->term_id) {
        return true;
    }
    return term_is_ancestor_of($term1, get_term($term2->parent, $taxonomy), $taxonomy);
}
	/**
	 * @group category.php
	 */
	function test_term_is_ancestor_of( ) {
		$term = rand_str();
		$term2 = rand_str();

		$t = wp_insert_term( $term, 'category' );
		$this->assertInternalType( 'array', $t );
		$t2 = wp_insert_term( $term, 'category', array( 'parent' => $t['term_id'] ) );
		$this->assertInternalType( 'array', $t2 );
		if ( function_exists( 'term_is_ancestor_of' ) ) {
			$this->assertTrue( term_is_ancestor_of( $t['term_id'], $t2['term_id'], 'category' ) );
			$this->assertFalse( term_is_ancestor_of( $t2['term_id'], $t['term_id'], 'category' ) );
		}
		$this->assertTrue( cat_is_ancestor_of( $t['term_id'], $t2['term_id']) );
		$this->assertFalse( cat_is_ancestor_of( $t2['term_id'], $t['term_id']) );

		wp_delete_term($t['term_id'], 'category');
		wp_delete_term($t2['term_id'], 'category');
	}
Example #4
0
/**
 * Check if a category is an ancestor of another category.
 *
 * You can use either an id or the category object for both parameters. If you
 * use an integer the category will be retrieved.
 *
 * @since 2.1.0
 *
 * @param int|object $cat1 ID or object to check if this is the parent category.
 * @param int|object $cat2 The child category.
 * @return bool Whether $cat2 is child of $cat1
 */
function cat_is_ancestor_of($cat1, $cat2)
{
    return term_is_ancestor_of($cat1, $cat2, 'category');
}
function list_materials_($term)
{
    $term_id = wp_get_post_terms($term, 'download_category', array("fields" => "ids"));
    if (is_array($term_id)) {
        $term_id = $term_id[0];
    }
    echo '<style type="text/css">
			.con_{
				background-position:left top; 
				background-repeat:repeat-x; 
				height:26px; width:40px; 
				float:left; 
				position:absolute; 
				bottom:2px; 
				left:2px; 
				z-index: 99999;
				background-color: rgba(251, 250, 255, 0.9); 
				border:1px solid;
				border-color:rgba(251, 250, 255, 0.1);
				width:266px;
				padding:0px 3px 0px 3px;
				
				}
				.sml-box{
				height:25px; width:40px; 
				background-size: contain;
    			background-repeat: no-repeat;
   				background-position: center center;
				}
			.music_{
				background-image:url(' . get_template_directory_uri() . '/images/spectrum.png); 
			}
			.let_{
				width:266px;
				
			}
			.des_{
				/*background-image:url(' . get_template_directory_uri() . '/images/painting.png);*/
				background-size: contain;
    			background-repeat: no-repeat;
    			background-position: center center;
			}
			.phy{
				/*background-image:url(' . get_template_directory_uri() . '/images/painting.png);*/
				background-size: contain;
    			background-repeat: no-repeat;
    			background-position: center center;
							}
			.let1{
				/*background-image:url(' . get_template_directory_uri() . '/images/painting.png);*/
				background-size: contain;
    			background-repeat: no-repeat;
    			background-position: center center;
			}
			.sml-box{
				margin:1px 2% 1px 2%;
				width:21%;
				float:left;
				height:23px;
			}
			.let1{
				background-image:url(' . get_template_directory_uri() . '/images/let1.png); 
			}
			.let2{
				background-image:url(' . get_template_directory_uri() . '/images/let2.png); 
			}
			.des1{
				background-image:url(' . get_template_directory_uri() . '/images/des1.png); 
			}
			.des2{
				background-image:url(' . get_template_directory_uri() . '/images/des2.png); 
			}
			.des3{
				background-image:url(' . get_template_directory_uri() . '/images/des3.png); 
			}
			.des4{
				background-image:url(' . get_template_directory_uri() . '/images/des4.png); 
			}
			.phy1{
				background-image:url(' . get_template_directory_uri() . '/images/phy1.png); 
				background-color: rgba(251, 250, 255, 0.3); 
			}
			.phy2{
				background-image:url(' . get_template_directory_uri() . '/images/phy2.png); 
				background-color: rgba(251, 250, 255, 0.3); 
			}
				</style>';
    if (term_is_ancestor_of(10, $term_id, 'download_category')) {
        ?>
		<div title="Music" class="con_ music_"></div>
	<?php 
    } elseif (term_is_ancestor_of(25, $term_id, 'download_category')) {
        $_xx = '<div class="sml-box let1"></div><div class="sml-box let2"></div><div class="sml-box let1"></div><div class="sml-box let2"></div>';
        $terms_d = wp_get_post_terms($term, 'download_tag', array("fields" => "ids"));
        $_ob = get_term_by('id', absint($terms_d[0]), 'download_tag');
        if ($_ob->name !== '') {
            $_cn = explode(',', $_ob->name);
            $_cn = array_filter($_cn);
            if (isset($_cn[0])) {
                echo '<div title="Literature" class="con_ let_">' . $_xx . '</div>';
            } else {
                echo '<div title="Literature" class="con_ let_">' . $_xx . '</div>';
            }
        } else {
            echo '<div title="Literature" class="con_ let_">' . $_xx . '</div>';
        }
    } elseif (term_is_ancestor_of(76, $term_id, 'download_category')) {
        if (_isphysical($term) != true) {
            ?>
		<div title="Design" class="con_ des_"><div class="sml-box des1"></div><div class="sml-box des2"></div><div class="sml-box des3"></div><div class="sml-box des4"></div></div>
        <?php 
        } else {
            ?>
            <div title="Design" class="con_ phy"><div class="sml-box phy1"></div><div class="sml-box phy2"></div><div class="sml-box phy1"></div><div class="sml-box phy2"></div></div>
            <?php 
        }
    }
}
Example #6
0
 /**
  * Retrieve a sidebar associated with a root category level category. If
  * this is called from an archive page then it will look for the category
  * that is the parent of the current. If called from a post it will find the
  * first category the post is in.
  *
  * @param string $name Name of the sidebar to be output
  *
  * @return bool    True if we found one otherwise false.
  */
 function icit_dynamic_category_sidebar($name = 'sidebar')
 {
     if (!is_string($name)) {
         return false;
     }
     $name = !empty($name) ? $name : 'Sidebar';
     $id = 'category' . sanitize_title($name) . '-%d';
     $found_one = false;
     if (is_single() || is_category()) {
         // Step over each category looking for sidebar to use
         $archives = get_categories(array('hierarchical' => false, 'parent' => 0, 'hide_empty' => false));
         foreach ($archives as $i => $category) {
             if ($found_one) {
                 break;
             }
             $sidebar_id = sprintf($id, $category->term_id);
             if (is_active_sidebar($sidebar_id)) {
                 // Is the category one of the root cats or is the cat a child of the root cat.
                 if (is_category($category->term_id) || is_category() && term_is_ancestor_of($category->term_id, get_queried_object_id(), 'category')) {
                     $found_one = dynamic_sidebar($sidebar_id);
                 } elseif (is_single() && in_category($category->term_id)) {
                     $found_one = dynamic_sidebar($sidebar_id);
                 } elseif (is_single()) {
                     $post_cats = wp_list_pluck((array) get_the_terms(get_the_ID(), 'category'), 'term_id');
                     foreach ($post_cats as $p_cat) {
                         if ($found_one) {
                             continue;
                         }
                         if (term_is_ancestor_of($category->term_id, $p_cat, 'category')) {
                             $found_one = dynamic_sidebar($sidebar_id);
                         }
                     }
                 }
             }
         }
     }
     return $found_one;
 }
Example #7
0
         $curr_cat_ids_selected[] = $catalog_category_term->term_id;
     }
 }
 foreach ($catalog_categories as $term) {
     if (!empty($curr_include) && !in_array($term->slug, $curr_include)) {
         continue;
     }
     if (isset($term->children)) {
         $pf_children = $term->children;
     } else {
         $pf_children = array();
     }
     $curr_id_found = false;
     if (isset($curr_cat_ids_selected) && !empty($pf_children)) {
         foreach ($curr_cat_ids_selected as $curr_cat_id_selected) {
             if (term_is_ancestor_of((int) $term->term_id, (int) $curr_cat_id_selected, 'product_cat')) {
                 $curr_id_found = true;
             }
         }
     }
     if ($curr_options['wc_settings_prdctfltr_cat_mode'] == 'subcategories' && !empty($curr_cat_selected) && $curr_id_found == false && !in_array($term->term_id, $curr_cat_ids_selected)) {
         continue;
     }
     $pf_adoptive_class = '';
     if ($curr_options['wc_settings_prdctfltr_cat_adoptive'] == 'yes' && isset($output_terms['product_cat']) && !empty($output_terms['product_cat']) && !array_key_exists($term->slug, $output_terms['product_cat'])) {
         $pf_adoptive_class = ' pf_adoptive_hide';
     }
     printf('<label class="%6$s%4$s%8$s"><input type="checkbox" value="%1$s"%3$s /><span>%2$s%7$s</span>%5$s</label>', $term->slug, $term->name, in_array($term->slug, $curr_cat_selected) ? ' checked' : '', in_array($term->slug, $curr_cat_selected) ? ' prdctfltr_active' : '', !empty($pf_children) ? '<i class="prdctfltr-plus"></i>' : '', $pf_adoptive_class, $curr_options['wc_settings_prdctfltr_show_counts'] == 'no' ? '' : ' <span class="prdctfltr_count">' . (isset($output_terms['product_cat']) && isset($output_terms['product_cat'][$term->slug]) && $output_terms['product_cat'][$term->slug] != $term->count ? $output_terms['product_cat'][$term->slug] . '/' . $term->count : $term->count) . '</span>', !empty($pf_children) && in_array($term->slug, $curr_cat_selected) ? ' prdctfltr_clicked' : '');
     if ($curr_options['wc_settings_prdctfltr_cat_hierarchy'] == 'yes' && !empty($pf_children)) {
         printf('<div class="prdctfltr_sub" data-sub="%1$s">', $term->slug);
         foreach ($pf_children as $sub) {
Example #8
0
 /**
  * Updates an existing Category or creates a new Category.
  *
  * @since 2.0.0
  *
  * @param mixed $catarr See defaults below. Set 'cat_ID' to a non-zero value to update an existing category. The 'taxonomy' key was added in 3.0.0.
  * @param bool $wp_error Optional, since 2.5.0. Set this to true if the caller handles WP_Error return values.
  * @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure, depending on param $wp_error.
  */
 protected function wp_insert_category($catarr, $wp_error = false)
 {
     $cat_defaults = array('cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
     $catarr = wp_parse_args($catarr, $cat_defaults);
     extract($catarr, EXTR_SKIP);
     if (trim($cat_name) == '') {
         if (!$wp_error) {
             return 0;
         } else {
             return new WP_Error('cat_name', __('You did not enter a category name.'));
         }
     }
     $cat_ID = (int) $cat_ID;
     // Are we updating or creating?
     if (!empty($cat_ID)) {
         $update = true;
     } else {
         $update = false;
     }
     $name = $cat_name;
     $description = $category_description;
     $slug = $category_nicename;
     $parent = $category_parent;
     $parent = (int) $parent;
     if ($parent < 0) {
         $parent = 0;
     }
     if (empty($parent) || !term_exists($parent, $taxonomy) || $cat_ID && term_is_ancestor_of($cat_ID, $parent, $taxonomy)) {
         $parent = 0;
     }
     $args = compact('name', 'slug', 'parent', 'description');
     if ($update) {
         $cat_ID = wp_update_term($cat_ID, $taxonomy, $args);
     } else {
         $cat_ID = wp_insert_term($cat_name, $taxonomy, $args);
     }
     if (is_wp_error($cat_ID)) {
         if ($wp_error) {
             return $cat_ID;
         } else {
             return 0;
         }
     }
     return $cat_ID['term_id'];
 }
Example #9
0
     }
 }
 foreach ($catalog_categories as $term) {
     $decode_slug = $term->slug;
     if (!empty($curr_include) && !in_array($decode_slug, $curr_include)) {
         continue;
     }
     if (isset($term->children)) {
         $pf_children = $term->children;
     } else {
         $pf_children = array();
     }
     $curr_id_found = false;
     if (isset($curr_cat_ids_selected) && !empty($pf_children)) {
         foreach ($curr_cat_ids_selected as $curr_cat_id_selected) {
             if (term_is_ancestor_of((int) $term->term_id, (int) $curr_cat_id_selected, $curr_fo['filter'])) {
                 $curr_id_found = true;
             }
         }
     }
     if (isset($curr_fo['settings']['mode']) && $curr_fo['settings']['mode'] == 'subcategories' && !empty($curr_cat_selected) && $curr_id_found == false && !in_array($term->term_id, $curr_cat_ids_selected)) {
         continue;
     }
     if ($curr_fo['settings']['customization'] == '') {
         $term_count = $curr_options['wc_settings_prdctfltr_show_counts'] == 'no' || $term->count == '0' ? '' : ' <span class="prdctfltr_count">' . (isset($output_terms[$curr_fo['filter']]) && isset($output_terms[$curr_fo['filter']][$term->slug]) && $output_terms[$curr_fo['filter']][$term->slug] != $term->count ? $output_terms[$curr_fo['filter']][$term->slug] . '/' . $term->count : $term->count) . '</span>';
         switch ($curr_term_style) {
             case 'pf_attr_text':
                 $curr_insert = $term->name . $term_count;
                 break;
             case 'pf_attr_imgtext':
                 $curr_img = wp_get_attachment_image(get_woocommerce_term_meta($term->term_id, $curr_fo['filter'] . '_thumbnail_id_photo', true), 'shop_thumbnail');