Skip to content

Commit

Permalink
Merge branch 'master' into add/get_post_single_term_info
Browse files Browse the repository at this point in the history
  • Loading branch information
mthaichi authored Nov 15, 2023
2 parents 360601c + c2abd28 commit 9a36815
Showing 1 changed file with 120 additions and 6 deletions.
126 changes: 120 additions & 6 deletions src/VkTermColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
* @package vektor-inc/vk-term-color
* @license GPL-2.0+
*
* @version 0.6.4
* @version 0.6.6
*/


namespace VektorInc\VK_Term_Color;

/**
Expand Down Expand Up @@ -290,7 +289,118 @@ public static function get_post_single_term_info ($post) {
}

/**
* Term名とカラーを取得
* Term 一つ分のHTMLを出力
* 対象のタームを指定して出力したい場合に使用します。
*
* @param object $term .
* @param array $args .
* @return string $html .
*/
public static function get_post_single_term_html( $term, $args = array() ) {

$args_default = array(
'single_element' => '',
'single_class' => '',
'single_inner_class' => 'btn btn-sm',
'link' => false,
'color' => true,
);
$args = wp_parse_args( $args, $args_default );

$single_class = '';
if ( ! empty( $args['single_class'] ) ) {
$single_class = ' class="' . esc_attr( $args['single_class'] ) . '"';
}
$single_inner_class = '';
if ( ! empty( $args['single_inner_class'] ) ) {
$single_inner_class = ' class="' . esc_attr( $args['single_inner_class'] ) . '"';
}

$term_name = esc_html( $term->name );
$term_url = esc_url( get_term_link( $term ) );

if ( $args['color'] ) {
$term_color = self::get_term_color( $term->term_id );
$term_color = ( $term_color ) ? ' style="color:#fff;background-color:' . $term_color . '"' : '';
} else {
$term_color = '';
}

$single_term_html = '';

if ( ! empty( $args['single_element'] ) ) {
$single_term_html .= '<' . $args['single_element'] . $single_class . '>';
}

if ( $args['link'] ) {
$single_term_html .= '<a' . $single_inner_class . $term_color . ' href="' . esc_url( $term_url ) . '">';
} else {
$single_term_html .= '<span' . $single_inner_class . $term_color . '>';
}

$single_term_html .= $term_name;

if ( $args['link'] ) {
$single_term_html .= '</a>';
} else {
$single_term_html .= '</span>';
}

if ( ! empty( $args['single_element'] ) ) {
$single_term_html .= '</' . $args['single_element'] . '>';
}

return $single_term_html;
}

/**
* 自動で単一のTermのhtmlを取得
*
* @param object $post .
* @param array $args .
* @return string .
*/
public static function get_auto_post_single_term_html( $post = '', $args = array() ) {
if ( ! $post ) {
global $post;
}

$args_default = array(
'single_element' => '',
'single_class' => '',
'single_inner_class' => 'btn btn-sm',
'link' => false,
'color' => true,
);
$args = wp_parse_args( $args, $args_default );

$taxonomies = get_the_taxonomies();
$exclusion = array( 'post_tag', 'product_type' );
// * vk_exclude_term_list is used in lightning too.
$exclusion = apply_filters( 'vk_get_display_taxonomies_exclusion', $exclusion );
if ( is_array( $exclusion ) ) {
foreach ( $exclusion as $key => $value ) {
unset( $taxonomies[ $value ] );
}
}

$single_term_with_color = '';
if ( $taxonomies ) {
// get $taxonomy name.
$taxonomy = key( $taxonomies );
$terms = get_the_terms( $post->ID, $taxonomy );
if ( ! empty( $terms[0] ) ) {
$single_term_with_color = self::get_post_single_term_html( $terms[0], $args );
}
}
return $single_term_with_color;
}

/**
* Termとカラーを投稿から自動で取得する取得
* 対象の taxonomy を指定したい場合はフックで指定。除外ができる
* 指定 : vk_term_color_taxonomy
* 除外 : vk_get_display_taxonomies_exclusion
*
* @param object $post : post object.
* @param array $args : setting parametor.
Expand All @@ -315,6 +425,7 @@ public static function get_single_term_with_color( $post = '', $args = array() )
$taxonomies = get_the_taxonomies( $post );
$exclusion = array( 'post_tag', 'product_type' );
// * vk_exclude_term_list is used in lightning too.
// 除外するタクソノミーがある場合はフックで指定
$exclusion = apply_filters( 'vk_get_display_taxonomies_exclusion', $exclusion );
if ( is_array( $exclusion ) ) {
foreach ( $exclusion as $key => $value ) {
Expand All @@ -325,6 +436,7 @@ public static function get_single_term_with_color( $post = '', $args = array() )
$single_term_with_color = '';
if ( $taxonomies ) {
// get $taxonomy name.
// 取得するタームのタクソノミーを指定したい場合はフックで指定
$taxonomy = apply_filters( 'vk_term_color_taxonomy', key( $taxonomies ) );
$terms = get_the_terms( $post->ID, $taxonomy );
if ( ! $terms ) {
Expand Down Expand Up @@ -352,10 +464,9 @@ public static function get_single_term_with_color( $post = '', $args = array() )
return apply_filters( 'vk_get_single_term_with_color', $single_term_with_color, $post, $args );
}



/**
* Get Post terms html
* 複数のタームを表示する場合に使用
*
* @param object $post : post object .
* @param array $args : see $args_default.
Expand Down Expand Up @@ -399,6 +510,9 @@ public static function get_post_terms_html( $post = '', $args = array() ) {
}

$terms = get_the_terms( $post->ID, $taxonomy );
if ( is_wp_error( $terms ) || ! $terms) {
return;
}

$outer_class = '';
if ( ! empty( $args['outer_class'] ) ) {
Expand Down Expand Up @@ -452,4 +566,4 @@ public static function get_term_color_taxonomies() {
return $taxonomies;
}

}
}

0 comments on commit 9a36815

Please sign in to comment.