class.term-color.php
252 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | このファイルの� |
| 5 | �ファイルは |
| 6 | https://github.com/vektor-inc/vektor-wp-libraries |
| 7 | にあります。修正の際は上記リポジトリのデータを修正してください。 |
| 8 | */ |
| 9 | |
| 10 | if ( ! class_exists( 'Vk_term_color' ) ) { |
| 11 | |
| 12 | class Vk_term_color { |
| 13 | |
| 14 | /*-------------------------------------------*/ |
| 15 | /* REGISTER TERM META |
| 16 | /*-------------------------------------------*/ |
| 17 | |
| 18 | function term_meta_color() { |
| 19 | |
| 20 | register_meta( 'term', 'term_color', array( $this, 'sanitize_hex' ) ); |
| 21 | } |
| 22 | |
| 23 | /*-------------------------------------------*/ |
| 24 | /* SANITIZE DATA |
| 25 | /*-------------------------------------------*/ |
| 26 | |
| 27 | public static function sanitize_hex( $color ) { |
| 28 | // sanitize_hex_color() は undefined function くらう |
| 29 | $color = ltrim( $color, '#' ); |
| 30 | return preg_match( '/([A-Fa-f0-9]{3}){1,2}$/', $color ) ? $color : ''; |
| 31 | } |
| 32 | |
| 33 | /*-------------------------------------------*/ |
| 34 | /* タクソノミー新規追加ページでの日本語� |
| 35 | �力フォーム |
| 36 | /*-------------------------------------------*/ |
| 37 | function taxonomy_add_new_meta_field_color() { |
| 38 | |
| 39 | // this will add the custom meta field to the add new term page |
| 40 | ?> |
| 41 | <div class="form-field"> |
| 42 | <?php wp_nonce_field( basename( __FILE__ ), 'term_color_nonce' ); ?> |
| 43 | <label for="term_color"><?php _e( 'Color', 'lightning-pro' ); ?></label> |
| 44 | <input type="text" name="term_color" id="term_color" class="term_color" value=""> |
| 45 | </div> |
| 46 | <?php |
| 47 | } |
| 48 | |
| 49 | /*-------------------------------------------*/ |
| 50 | /* タクソノミー編集ページでのフォーム |
| 51 | /*-------------------------------------------*/ |
| 52 | function taxonomy_add_edit_meta_field_color( $term ) { |
| 53 | |
| 54 | // put the term ID into a variable |
| 55 | $term_color = Vk_term_color::get_term_color( $term->term_id ); |
| 56 | ?> |
| 57 | <tr class="form-field"> |
| 58 | <th scope="row" valign="top"><label for="term_color"><?php _e( 'Color', 'lightning-pro' ); ?></label></th> |
| 59 | <td> |
| 60 | <?php wp_nonce_field( basename( __FILE__ ), 'term_color_nonce' ); ?> |
| 61 | <input type="text" name="term_color" id="term_color" class="term_color" value="<?php echo $term_color; ?>"> |
| 62 | </td> |
| 63 | </tr> |
| 64 | <?php |
| 65 | } |
| 66 | |
| 67 | /*-------------------------------------------*/ |
| 68 | /* カラーの保存処理 |
| 69 | /*-------------------------------------------*/ |
| 70 | // Save extra taxonomy fields callback function. |
| 71 | function save_term_meta_color( $term_id ) { |
| 72 | |
| 73 | // verify the nonce --- remove if you don't care |
| 74 | if ( ! isset( $_POST['term_color_nonce'] ) || ! wp_verify_nonce( $_POST['term_color_nonce'], basename( __FILE__ ) ) ) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | if ( isset( $_POST['term_color'] ) ) { |
| 79 | $now_value = get_term_meta( $term_id, 'term_color', true ); |
| 80 | $new_value = $_POST['term_color']; |
| 81 | if ( $now_value != $new_value ) { |
| 82 | update_term_meta( $term_id, 'term_color', $new_value ); |
| 83 | } else { |
| 84 | add_term_meta( $term_id, 'term_color', $new_value ); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /*-------------------------------------------*/ |
| 90 | /* 管理画面 _ カラーピッカーのスクリプトの読み込み |
| 91 | /*-------------------------------------------*/ |
| 92 | |
| 93 | function admin_enqueue_scripts( $hook_suffix ) { |
| 94 | |
| 95 | // if ( 'edit-tags.php' !== $hook_suffix || 'category' !== get_current_screen()->taxonomy ) |
| 96 | // return; |
| 97 | |
| 98 | wp_enqueue_style( 'wp-color-picker' ); |
| 99 | wp_enqueue_script( 'wp-color-picker' ); |
| 100 | |
| 101 | // add_action( 'admin_head', 'lmu_term_colors_print_styles' ); |
| 102 | add_action( 'admin_footer', array( $this, 'term_colors_print_scripts' ) ); |
| 103 | } |
| 104 | |
| 105 | function term_colors_print_styles() { |
| 106 | ?> |
| 107 | |
| 108 | <style type="text/css"> |
| 109 | .column-color { width: 50px; } |
| 110 | .column-color .color-block { display: inline-block; width: 28px; height: 28px; border: 1px solid #ddd; } |
| 111 | </style> |
| 112 | <?php |
| 113 | } |
| 114 | |
| 115 | function term_colors_print_scripts() { |
| 116 | ?> |
| 117 | |
| 118 | <script type="text/javascript"> |
| 119 | jQuery( document ).ready( function( $ ) { |
| 120 | $( '.term_color' ).wpColorPicker(); |
| 121 | } ); |
| 122 | </script> |
| 123 | <?php |
| 124 | } |
| 125 | |
| 126 | /*-------------------------------------------*/ |
| 127 | /* 管理画面 _ カテゴリー一覧でカラムの追加 |
| 128 | /*-------------------------------------------*/ |
| 129 | |
| 130 | function edit_term_columns( $columns ) { |
| 131 | |
| 132 | $columns['color'] = __( 'Color', 'lightning-pro' ); |
| 133 | |
| 134 | return $columns; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | function manage_term_custom_column( $out, $column, $term_id ) { |
| 139 | |
| 140 | if ( 'color' === $column ) { |
| 141 | |
| 142 | $color = Vk_term_color::get_term_color( $term_id ); |
| 143 | |
| 144 | if ( ! $color ) { |
| 145 | $color = '#ffffff'; |
| 146 | } |
| 147 | |
| 148 | $out = sprintf( '<span class="color-block" style="background:%s;"> </span>', esc_attr( $color ) ); |
| 149 | } |
| 150 | |
| 151 | return $out; |
| 152 | } |
| 153 | |
| 154 | /*-------------------------------------------*/ |
| 155 | /* termのカラーを取得 |
| 156 | /*-------------------------------------------*/ |
| 157 | public static function get_term_color( $term_id ) { |
| 158 | $term_color_default = '#999999'; |
| 159 | $term_color_default = apply_filters( 'term_color_default_custom', $term_color_default ); |
| 160 | if ( isset( $term_id ) ) { |
| 161 | $term_color = Vk_term_color::sanitize_hex( get_term_meta( $term_id, 'term_color', true ) ); |
| 162 | $term_color = ( $term_color ) ? '#' . $term_color : $term_color_default; |
| 163 | } else { |
| 164 | $term_color = $term_color_default; |
| 165 | } |
| 166 | return $term_color; |
| 167 | } |
| 168 | |
| 169 | /*-------------------------------------------*/ |
| 170 | /* termのカラーを取得 |
| 171 | /*-------------------------------------------*/ |
| 172 | public static function get_single_term_with_color( $post = '', $args = array() ) { |
| 173 | if ( ! $post ) { |
| 174 | global $post; |
| 175 | } |
| 176 | |
| 177 | $args_default = array( |
| 178 | 'class' => '', |
| 179 | ); |
| 180 | $args = wp_parse_args( $args, $args_default ); |
| 181 | |
| 182 | $outer_class = ''; |
| 183 | if ( ! empty( $args['class'] ) ) { |
| 184 | $outer_class = ' class="' . esc_attr( $args['class'] ) . '"'; |
| 185 | } |
| 186 | |
| 187 | $taxonomies = get_the_taxonomies(); |
| 188 | $single_term_with_color = ''; |
| 189 | if ( $taxonomies ) : |
| 190 | // get $taxonomy name |
| 191 | $taxonomy = key( $taxonomies ); |
| 192 | $terms = get_the_terms( $post->ID, $taxonomy ); |
| 193 | $term_name = esc_html( $terms[0]->name ); |
| 194 | $term_color = Vk_term_color::get_term_color( $terms[0]->term_id ); |
| 195 | $term_color = ( $term_color ) ? ' style="color:#fff;background-color:' . $term_color . '"' : ''; |
| 196 | $single_term_with_color .= '<span' . $outer_class . $term_color . '>' . $term_name . '</span>'; |
| 197 | endif; |
| 198 | return $single_term_with_color; |
| 199 | } |
| 200 | |
| 201 | /*-------------------------------------------*/ |
| 202 | /* term color を有効化する taxonomy |
| 203 | /*-------------------------------------------*/ |
| 204 | public function get_term_color_taxonomies() { |
| 205 | /* |
| 206 | 最初Global変数指定をしていたが、 Global変数では |
| 207 | 複数の term color が存在した場合に実行タイミングの都合上任意に指定が効かないため、 |
| 208 | フックでの指定を行う |
| 209 | */ |
| 210 | global $vk_term_color_taxonomies; |
| 211 | if ( $vk_term_color_taxonomies ) { |
| 212 | $taxonomies = $vk_term_color_taxonomies; |
| 213 | } else { |
| 214 | $taxonomies = array( 'category', 'post_tag' ); |
| 215 | } |
| 216 | $taxonomies = apply_filters( 'term_color_taxonomies_custom', $taxonomies ); |
| 217 | // 重複の値を削除 |
| 218 | $taxonomies = array_unique( $taxonomies ); |
| 219 | // 特に影響はないがキーを振り直す |
| 220 | $taxonomies = array_values( $taxonomies ); |
| 221 | return $taxonomies; |
| 222 | } |
| 223 | /*-------------------------------------------*/ |
| 224 | /* 実行 |
| 225 | /*-------------------------------------------*/ |
| 226 | public function __construct() { |
| 227 | add_action( 'init', array( $this, 'term_meta_color' ) ); |
| 228 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 229 | |
| 230 | /*-------------------------------------------*/ |
| 231 | /* 管理画面 _ 各種処理発火 |
| 232 | /*-------------------------------------------*/ |
| 233 | // カラーピッカーを追加するタクソノミー |
| 234 | |
| 235 | $taxonomies = Vk_term_color::get_term_color_taxonomies(); |
| 236 | |
| 237 | // 該当のタクソノミー分ループ処理する |
| 238 | foreach ( $taxonomies as $key => $value ) { |
| 239 | add_action( $value . '_add_form_fields', array( $this, 'taxonomy_add_new_meta_field_color' ), 10, 2 ); |
| 240 | add_action( $value . '_edit_form_fields', array( $this, 'taxonomy_add_edit_meta_field_color' ), 10, 2 ); |
| 241 | add_action( 'edited_' . $value, array( $this, 'save_term_meta_color' ), 10, 2 ); |
| 242 | add_action( 'create_' . $value, array( $this, 'save_term_meta_color' ), 10, 2 ); |
| 243 | add_filter( 'manage_edit-' . $value . '_columns', array( $this, 'edit_term_columns' ) ); |
| 244 | add_filter( 'manage_' . $value . '_custom_column', array( $this, 'manage_term_custom_column' ), 10, 3 ); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | $Vk_term_color = new Vk_term_color(); |
| 250 | |
| 251 | } |
| 252 |