/** * Recursive function prepend a terms parent name and slug to the hierarchy string. * * @access public * @since 8.5.5 * * @param object $term * @param string $text */ private function _buildHierarchy($term, &$text) { if ($term->parent) { $parent = cnTerm::get($term->parent, $term->taxonomy); if (0 == strlen($text)) { $text = "{$parent->name}|{$parent->slug}"; } else { $text = "{$parent->name}|{$parent->slug}" . ' > ' . $text; } $this->_buildHierarchy($parent, $text); } }
/** * Retrieve category parents with separator. * * NOTE: This is the Connections equivalent of @see get_category_parents() in WordPress core ../wp-includes/category-template.php * * @access public * @since 8.5.18 * @static * * @param int $id Category ID. * @param array $atts The attributes array. { * * @type bool $link Whether to format as link or as a string. * Default: FALSE * @type string $separator How to separate categories. * Default: '/' * @type bool $nicename Whether to use nice name for display. * Default: FALSE * @type array $visited Already linked to categories to prevent duplicates. * Default: array() * @type bool $force_home Default: FALSE * @type int $home_id Default: The page set as the directory home page. * } * * @return string|WP_Error A list of category parents on success, WP_Error on failure. */ public static function getCategoryParents($id, $atts = array()) { $defaults = array('link' => FALSE, 'separator' => '/', 'nicename' => FALSE, 'visited' => array(), 'force_home' => FALSE, 'home_id' => cnSettingsAPI::get('connections', 'connections_home_page', 'page_id')); $atts = cnSanitize::args($atts, $defaults); $chain = ''; $parent = cnTerm::get($id, 'category'); if (is_wp_error($parent)) { return $parent; } if ($atts['nicename']) { $name = $parent->slug; } else { $name = $parent->name; } if ($parent->parent && $parent->parent != $parent->term_id && !in_array($parent->parent, $atts['visited'])) { $atts['visited'][] = $parent->parent; $chain .= self::getCategoryParents($parent->parent, $atts); } if ($atts['link']) { $chain .= '<a href="' . esc_url(cnTerm::permalink($parent->term_id, 'category', $atts)) . '">' . $name . '</a>' . $atts['separator']; } else { $chain .= $name . esc_html($atts['separator']); } return $chain; }
/** * Start the element output. * * @see Walker::start_el() * * @since 8.1.6 * * @uses esc_attr() * @uses number_format_i18n() * @uses cnTerm::get() * * @param string $output Passed by reference. Used to append additional content. * @param object $term Term object. * @param int $depth Depth of category in reference to parents. Default 0. * @param array $args An array of arguments. @see CN_Walker_Term_List::render() * @param int $id ID of the current term. */ public function start_el(&$output, $term, $depth = 0, $args = array(), $id = 0) { $indent = str_repeat("\t", $depth); $count = $args['show_count'] ? '<span class="cn-cat-count"> (' . esc_html(number_format_i18n($term->count)) . ')</span>' : ''; $url = cnTerm::permalink($term, 'category', $args); $html = sprintf('<a href="%1$s" title="%2$s">%3$s</a>', $url, esc_attr($term->name), esc_html($term->name) . $count); /** * Allows extensions to alter the HTML of term list item. * * @since 8.5.18 * * @param string $html The HTML. * @param cnTerm_Object $term The current term. * @param int $depth Depth of category. Used for tab indentation. * @param array $args The method attributes. */ $html = apply_filters('cn_term_list_item', $html, $term, $depth, $args); $class = array('cat-item', 'cat-item-' . $term->term_id, 'cn-cat-parent'); $termChildren = cnTerm::getTaxonomyTerms($term->taxonomy, array('parent' => $term->term_id, 'hide_empty' => FALSE, 'fields' => 'count')); if (!empty($termChildren)) { $class[] = 'cn-cat-has-children'; } if (!empty($args['current_category'])) { if (is_numeric($args['current_category'])) { $_current_category = cnTerm::get($args['current_category'], $term->taxonomy); // cnTerm::get() can return NULL || an instance of WP_Error, so, lets check for that. if (is_null($_current_category) || is_wp_error($_current_category)) { $_current_category = new stdClass(); $_current_category->parent = 0; } } else { $_current_category = new stdClass(); $_current_category->parent = 0; } if ($term->slug == $args['current_category']) { $class[] = ' current-cat'; } elseif ($term->term_id == $args['current_category']) { $class[] = ' current-cat'; } elseif ($term->term_id == $_current_category->parent) { $class[] = ' current-cat-parent'; } } /** * Allows extensions to add/remove class names to the current term list item. * * @since 8.5.18 * * @param array $class The array of class names. * @param cnTerm_Object $term The current term. * @param int $depth Depth of category. Used for tab indentation. * @param array $args The method attributes. */ $class = apply_filters('cn_term_list_item_class', $class, $term, $depth, $args); $class = cnSanitize::htmlClass($class); $output .= "{$indent}<li" . ' class="' . cnFunction::escAttributeDeep($class) . '"' . ">{$html}"; // Do not add EOL here, it'll add unwanted whitespace if terms are inline. }
/** * Recursive method to prepare to render a table row maintaining parent/child term relationships. * * @access private * @since 8.2 * * @param string $taxonomy * @param array $terms * @param array $children * @param int $start * @param int $per_page * @param int $count * @param int $parent * @param int $level */ private function _rows($taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0) { $end = $start + $per_page; foreach ($terms as $key => $term) { if ($count >= $end) { break; } if ($term->parent != $parent && empty($_REQUEST['s'])) { continue; } // If the page starts in a subtree, print the parents. if ($count == $start && $term->parent > 0 && empty($_REQUEST['s'])) { $my_parents = $parent_ids = array(); $p = $term->parent; while ($p) { $my_parent = cnTerm::get($p, $taxonomy); $my_parents[] = $my_parent; $p = $my_parent->parent; if (in_array($p, $parent_ids)) { // Prevent parent loops. break; } $parent_ids[] = $p; } unset($parent_ids); $num_parents = count($my_parents); while ($my_parent = array_pop($my_parents)) { echo "\t"; $this->single_row($my_parent, $level - $num_parents); $num_parents--; } } if ($count >= $start) { echo "\t"; $this->single_row($term, $level); } ++$count; unset($terms[$key]); if (isset($children[$term->term_id]) && empty($_REQUEST['s'])) { $this->_rows($taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1); } } }
/** * Prepare links for the request. * * @since 8.5.26 * * @param object $term Term object. * * @return array Links for the given term. */ protected function prepare_links($term) { $base = $this->namespace . '/' . $this->base; $links = array('self' => array('href' => rest_url(trailingslashit($base) . $term->term_id)), 'collection' => array('href' => rest_url($base)), 'about' => array('href' => rest_url(sprintf('cn-api/v1/taxonomies/%s', $this->taxonomy)))); if ($term->parent) { $parent_term = cnTerm::get((int) $term->parent, $term->taxonomy); if ($parent_term) { $links['up'] = array('href' => rest_url(trailingslashit($base) . $parent_term->term_id), 'embeddable' => TRUE); } } //$taxonomy_obj = get_taxonomy( $term->taxonomy ); //if ( empty( $taxonomy_obj->object_type ) ) { // // return $links; //} //$post_type_links = array(); //foreach ( $taxonomy_obj->object_type as $type ) { // // $post_type_object = get_post_type_object( $type ); // // if ( empty( $post_type_object->show_in_rest ) ) { // // continue; // } // // $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; // $post_type_links[] = array( // 'href' => add_query_arg( $this->rest_base, $term->term_id, rest_url( sprintf( 'wp/v2/%s', $rest_base ) ) ), // ); //} //if ( ! empty( $post_type_links ) ) { // // $links['https://api.w.org/post_type'] = $post_type_links; //} return $links; }
/** * @deprecated 8.1.6 Use {@see cnTerm::get()} instead. * @see cnTerm::get() * * @param $id * @param $taxonomy * * @return mixed|null|WP_Error */ public function getTerm($id, $taxonomy) { return cnTerm::get($id, $taxonomy); }
/** * Breakout the header columns based on the field type. * * @access private * @since 8.5.1 * * @param array $atts * * @return string */ private function explodeBreakoutHeader($atts) { $header = ''; switch ($atts['type']) { case 0: $header .= $this->escapeAndQuote($this->exportBreakoutHeaderField($atts)) . ','; break; // Explode all field columns and types... // Explode all field columns and types... case 1: $breakoutFields = $this->getFieldsToExport($atts); $breakoutTypes = $this->getTypesToExport($atts); foreach ($breakoutTypes as $type) { foreach ($breakoutFields as $field) { $header .= $this->escapeAndQuote($this->exportBreakoutHeaderField($atts, $field, $type)) . ','; } } break; // Joined from another table // Joined from another table case 2: $header .= $this->escapeAndQuote($atts['header']) . ','; break; // Breakout a list in the header... // Breakout a list in the header... case 3: $count = $this->getTermCount('category'); // Finally, write a list of fields for each category... for ($i = 0; $i < $count + 1; $i++) { $header .= $this->escapeAndQuote('Category') . ','; } break; case 4: $fields = explode(';', $atts['fields']); foreach ($fields as $field) { $header .= $this->escapeAndQuote($this->exportBreakoutHeaderField($atts, $field)) . ','; } break; case 5: $header .= $this->escapeAndQuote($this->exportBreakoutHeaderField($atts)) . ','; break; case 6: if (is_numeric($atts['child_of'])) { $term = cnTerm::get($atts['child_of']); $header .= $this->escapeAndQuote($term->name) . ','; } break; } return $header; }
/** * Start the element output. * * @see Walker::start_el() * * @since 8.1.6 * * @uses esc_attr() * @uses number_format_i18n() * @uses cnTerm::get() * * @param string $output Passed by reference. Used to append additional content. * @param object $term Term object. * @param int $depth Depth of category in reference to parents. Default 0. * @param array $args An array of arguments. @see CN_Walker_Term_List::render() * @param int $id ID of the current term. */ public function start_el(&$output, $term, $depth = 0, $args = array(), $id = 0) { $indent = str_repeat("\t", $depth); $count = $args['show_count'] ? ' (' . number_format_i18n($term->count) . ')' : ''; $url = cnTerm::permalink($term, 'category'); $link = sprintf('<a href="%1$s" title="%2$s">%3$s</a>', $url, esc_attr($term->name), esc_html($term->name . $count)); $class = 'cat-item cat-item-' . $term->term_id . ' cn-cat-parent'; if (!empty($args['current_category'])) { if (is_numeric($args['current_category'])) { $_current_category = cnTerm::get($args['current_category'], $term->taxonomy); } else { $_current_category = new stdClass(); $_current_category->parent = 0; } if ($term->slug == $args['current_category']) { $class .= ' current-cat'; } elseif ($term->term_id == $args['current_category']) { $class .= ' current-cat'; } elseif ($term->term_id == $_current_category->parent) { $class .= ' current-cat-parent'; } } $output .= "{$indent}<li" . ' class="' . $class . '"' . ">{$link}</li>" . PHP_EOL; }
/** * Returns category by ID. * * @param integer $id * @return object */ public function category($id) { return cnTerm::get($id, 'category'); }