| 1 |
<?php |
| 2 |
/** |
| 3 |
* Category color functions |
| 4 |
* |
| 5 |
* @package Flex Posts |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Register category color meta |
| 14 |
*/ |
| 15 |
function flex_posts_register_color_meta() { |
| 16 |
register_term_meta( |
| 17 |
'category', |
| 18 |
'fp_color', |
| 19 |
array( |
| 20 |
'type' => 'string', |
| 21 |
'description' => __( 'Category color', 'flex-posts' ), |
| 22 |
'single' => true, |
| 23 |
'sanitize_callback' => 'sanitize_hex_color', |
| 24 |
'show_in_rest' => true, |
| 25 |
) |
| 26 |
); |
| 27 |
} |
| 28 |
add_action( 'init', 'flex_posts_register_color_meta' ); |
| 29 |
|
| 30 |
/** |
| 31 |
* Add color field to category edit form |
| 32 |
* |
| 33 |
* @param obj $term Term object. |
| 34 |
*/ |
| 35 |
function flex_posts_edit_color_field( $term ) { |
| 36 |
$color = get_term_meta( $term->term_id, 'fp_color', true ); |
| 37 |
?> |
| 38 |
<tr class="form-field"> |
| 39 |
<th scope="row"><label for="fp_color"><?php esc_html_e( 'Color', 'flex-posts' ); ?></label></th> |
| 40 |
<td> |
| 41 |
<input name="fp_color" id="fp_color" type="text" value="<?php echo esc_attr( $color ); ?>" class="wp-color-picker-field"> |
| 42 |
</td> |
| 43 |
</tr> |
| 44 |
<?php |
| 45 |
} |
| 46 |
add_action( 'category_edit_form_fields', 'flex_posts_edit_color_field' ); |
| 47 |
|
| 48 |
/** |
| 49 |
* Add color field to category add form |
| 50 |
*/ |
| 51 |
function flex_posts_add_color_field() { |
| 52 |
?> |
| 53 |
<div class="form-field"> |
| 54 |
<label for="fp_color"><?php esc_html_e( 'Color', 'flex-posts' ); ?></label> |
| 55 |
<input name="fp_color" id="fp_color" type="text" value="" class="wp-color-picker-field"> |
| 56 |
</div> |
| 57 |
<?php |
| 58 |
} |
| 59 |
add_action( 'category_add_form_fields', 'flex_posts_add_color_field' ); |
| 60 |
|
| 61 |
/** |
| 62 |
* Save category color meta |
| 63 |
* |
| 64 |
* @param int $term_id Term ID. |
| 65 |
*/ |
| 66 |
function flex_posts_save_color_field( $term_id ) { |
| 67 |
if ( ! isset( $_POST['fp_color'] ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
// Re-verify the nonce that core set for whichever term form was submitted. |
| 72 |
$verified = false; |
| 73 |
if ( isset( $_POST['_wpnonce'] ) ) { |
| 74 |
$verified = wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'update-tag_' . $term_id ); |
| 75 |
} elseif ( isset( $_POST['_wpnonce_add-tag'] ) ) { |
| 76 |
$verified = wp_verify_nonce( sanitize_key( $_POST['_wpnonce_add-tag'] ), 'add-tag' ); |
| 77 |
} elseif ( isset( $_POST['_ajax_nonce-add-tag-category'] ) ) { |
| 78 |
$verified = wp_verify_nonce( sanitize_key( $_POST['_ajax_nonce-add-tag-category'] ), 'add-tag' ); |
| 79 |
} |
| 80 |
|
| 81 |
if ( ! $verified ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$color = sanitize_hex_color( wp_unslash( $_POST['fp_color'] ) ); |
| 86 |
update_term_meta( |
| 87 |
$term_id, |
| 88 |
'fp_color', |
| 89 |
$color |
| 90 |
); |
| 91 |
} |
| 92 |
add_action( 'edited_category', 'flex_posts_save_color_field' ); |
| 93 |
add_action( 'create_category', 'flex_posts_save_color_field' ); |
| 94 |
|
| 95 |
/** |
| 96 |
* Enqueue color picker and initialize |
| 97 |
* |
| 98 |
* @param string $hook Hook. |
| 99 |
*/ |
| 100 |
function flex_posts_enqueue_admin_color_picker( $hook ) { |
| 101 |
if ( 'edit-tags.php' !== $hook && 'term.php' !== $hook ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
$screen = get_current_screen(); |
| 106 |
if ( ! $screen || 'category' !== $screen->taxonomy ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
wp_enqueue_style( 'wp-color-picker' ); |
| 111 |
wp_enqueue_script( 'wp-color-picker' ); |
| 112 |
|
| 113 |
add_action( |
| 114 |
'admin_footer', |
| 115 |
function() { |
| 116 |
/** |
| 117 |
* Filter: flex_posts_color_palettes |
| 118 |
* |
| 119 |
* If a theme or plugin wants to supply custom palettes, return a |
| 120 |
* non-empty indexed array of hex color strings. If the filter |
| 121 |
* returns null or an empty value, the picker will use WordPress's |
| 122 |
* built-in default palettes. |
| 123 |
* |
| 124 |
* @param array|null $palettes Array of hex colors or null to use WP defaults. |
| 125 |
*/ |
| 126 |
$palettes = apply_filters( 'flex_posts_color_palettes', null ); |
| 127 |
|
| 128 |
$fp_palettes = array(); |
| 129 |
if ( is_array( $palettes ) && ! empty( $palettes ) ) { |
| 130 |
foreach ( $palettes as $color ) { |
| 131 |
$color = sanitize_hex_color( $color ); |
| 132 |
if ( ! empty( $color ) ) { |
| 133 |
$fp_palettes[] = $color; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
?> |
| 138 |
<script type="text/javascript"> |
| 139 |
jQuery(document).ready(function($){ |
| 140 |
<?php if ( ! empty( $fp_palettes ) ) : ?> |
| 141 |
var fpPalettes = <?php echo wp_json_encode( $fp_palettes ); ?>; |
| 142 |
|
| 143 |
$('.wp-color-picker-field').wpColorPicker({ |
| 144 |
palettes: fpPalettes |
| 145 |
}); |
| 146 |
<?php else : ?> |
| 147 |
$('.wp-color-picker-field').wpColorPicker(); |
| 148 |
<?php endif; ?> |
| 149 |
}); |
| 150 |
</script> |
| 151 |
<?php |
| 152 |
} |
| 153 |
); |
| 154 |
} |
| 155 |
add_action( 'admin_enqueue_scripts', 'flex_posts_enqueue_admin_color_picker' ); |
| 156 |
|
| 157 |
/** |
| 158 |
* Add a "Color" column to the Categories list table in WP Admin. |
| 159 |
* |
| 160 |
* @param array $columns Existing columns. |
| 161 |
* @return array Modified columns with fp_color added. |
| 162 |
*/ |
| 163 |
function flex_posts_add_category_columns( $columns ) { |
| 164 |
$columns['fp_color'] = __( 'Color', 'flex-posts' ); |
| 165 |
return $columns; |
| 166 |
} |
| 167 |
add_filter( 'manage_edit-category_columns', 'flex_posts_add_category_columns' ); |
| 168 |
|
| 169 |
/** |
| 170 |
* Render the content for the custom "Color" category column. |
| 171 |
* |
| 172 |
* @param string $deprecated Deprecated (unused). |
| 173 |
* @param string $column Column name. |
| 174 |
* @param int $term_id Term ID. |
| 175 |
*/ |
| 176 |
function flex_posts_render_category_color_column( $deprecated, $column, $term_id ) { |
| 177 |
if ( 'fp_color' !== $column ) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
$color = get_term_meta( $term_id, 'fp_color', true ); |
| 182 |
if ( empty( $color ) ) { |
| 183 |
echo ''; // no color set. |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
echo '<span style="display:inline-block;width:18px;height:18px;vertical-align:middle;margin-right:8px;border:1px solid #ddd;background:' . esc_attr( $color ) . ';"></span><span>' . esc_html( $color ) . '</span>'; |
| 188 |
} |
| 189 |
add_action( 'manage_category_custom_column', 'flex_posts_render_category_color_column', 10, 3 ); |
| 190 |
|