PluginProbe
GutSlider – All in One Slider and Carousel Blocks for Gutenberg / 3.2.0
GutSlider – All in One Slider and Carousel Blocks for Gutenberg v3.2.0
3.2.0 3.1.0 3.0.0 2.13.2 2.13.1 2.13.0 trunk 1.0.0 2.1.0 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.1 2.5.3 2.5.4 2.5.5 All 57 releases
slider-blocks / includes / Blocks / TermColors.php

TermColors.php in GutSlider – All in One Slider and Carousel Blocks for Gutenberg 3.2.0, at includes/Blocks/TermColors.php

128 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare( strict_types=1 );
3
4 namespace GutSlider\Blocks;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 /**
11 * A "Badge colour" for categories, tags and other public taxonomies.
12 *
13 * Used by the Blog Post block's category badges (terms without one get a colour
14 * from a fixed palette). Stored as term meta `gutslider_color`.
15 *
16 * @package GutSlider\Blocks
17 * @since 3.1.1
18 */
19 final class TermColors {
20
21 const META_KEY = 'gutslider_color';
22
23 /**
24 * Constructor.
25 */
26 public function __construct() {
27 add_action( 'init', array( $this, 'register' ), 20 );
28 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
29 }
30
31 /**
32 * Register the meta and the form fields on every public taxonomy with an admin screen.
33 */
34 public function register() {
35 register_meta(
36 'term',
37 self::META_KEY,
38 array(
39 'type' => 'string',
40 'single' => true,
41 'show_in_rest' => true,
42 'sanitize_callback' => 'sanitize_hex_color',
43 'auth_callback' => static function () {
44 return current_user_can( 'manage_categories' );
45 },
46 )
47 );
48
49 foreach ( get_taxonomies( array( 'public' => true, 'show_ui' => true ) ) as $taxonomy ) {
50 if ( 'post_format' === $taxonomy ) {
51 continue;
52 }
53 add_action( "{$taxonomy}_add_form_fields", array( $this, 'add_field' ) );
54 add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_field' ) );
55 add_action( "created_{$taxonomy}", array( $this, 'save' ) );
56 add_action( "edited_{$taxonomy}", array( $this, 'save' ) );
57 }
58 }
59
60 /**
61 * Colour picker on the term screens.
62 *
63 * @param string $hook Admin page.
64 */
65 public function enqueue( $hook ) {
66 if ( 'edit-tags.php' !== $hook && 'term.php' !== $hook ) {
67 return;
68 }
69 wp_enqueue_style( 'wp-color-picker' );
70 wp_enqueue_script( 'wp-color-picker' );
71 wp_add_inline_script( 'wp-color-picker', 'jQuery(function($){$(".gutslider-term-color").wpColorPicker();});' );
72 }
73
74 /**
75 * Field on "Add new".
76 */
77 public function add_field() {
78 ?>
79 <div class="form-field">
80 <label for="gutslider-term-color"><?php esc_html_e( 'Badge colour', 'slider-blocks' ); ?></label>
81 <?php wp_nonce_field( 'gutslider_term_color', 'gutslider_term_color_nonce' ); ?>
82 <input type="text" id="gutslider-term-color" name="gutslider_term_color" class="gutslider-term-color" value="" />
83 <p><?php esc_html_e( 'Used by GutSlider category badges. Leave empty for an automatic colour.', 'slider-blocks' ); ?></p>
84 </div>
85 <?php
86 }
87
88 /**
89 * Field on "Edit".
90 *
91 * @param \WP_Term $term Term.
92 */
93 public function edit_field( $term ) {
94 $color = (string) get_term_meta( $term->term_id, self::META_KEY, true );
95 ?>
96 <tr class="form-field">
97 <th scope="row"><label for="gutslider-term-color"><?php esc_html_e( 'Badge colour', 'slider-blocks' ); ?></label></th>
98 <td>
99 <?php wp_nonce_field( 'gutslider_term_color', 'gutslider_term_color_nonce' ); ?>
100 <input type="text" id="gutslider-term-color" name="gutslider_term_color" class="gutslider-term-color" value="<?php echo esc_attr( $color ); ?>" />
101 <p class="description"><?php esc_html_e( 'Used by GutSlider category badges. Leave empty for an automatic colour.', 'slider-blocks' ); ?></p>
102 </td>
103 </tr>
104 <?php
105 }
106
107 /**
108 * Save the colour.
109 *
110 * @param int $term_id Term id.
111 */
112 public function save( $term_id ) {
113 if ( ! isset( $_POST['gutslider_term_color_nonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['gutslider_term_color_nonce'] ) ), 'gutslider_term_color' ) ) {
114 return;
115 }
116 if ( ! current_user_can( 'manage_categories' ) ) {
117 return;
118 }
119 $color = isset( $_POST['gutslider_term_color'] ) ? sanitize_hex_color( wp_unslash( $_POST['gutslider_term_color'] ) ) : '';
120 if ( $color ) {
121 update_term_meta( $term_id, self::META_KEY, $color );
122 } else {
123 delete_term_meta( $term_id, self::META_KEY );
124 }
125 PostSlider::flush_cache();
126 }
127 }
128