1: <?php
2: /**
3: * TAXONOMY Contact Tag
4: *
5: * @package EPL
6: * @subpackage Taxonomy/ContactTag
7: * @copyright Copyright (c) 2019, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * Registers and sets up the epl_contact_tag taxonomy
19: *
20: * @since 3.0
21: * @return void
22: */
23: function epl_register_taxonomy_contact_tag() {
24: $labels = array(
25: 'name' => _x( 'Contact Tags', 'Taxonomy General Name', 'easy-property-listings' ),
26: 'singular_name' => _x( 'Contact Tag', 'Taxonomy Singular Name', 'easy-property-listings' ),
27: 'menu_name' => __( 'Contact Tags', 'easy-property-listings' ),
28: 'all_items' => __( 'All Contact Tags', 'easy-property-listings' ),
29: 'parent_item' => __( 'Parent Contact Tag', 'easy-property-listings' ),
30: 'parent_item_colon' => __( 'Parent Contact Tag:', 'easy-property-listings' ),
31: 'new_item_name' => __( 'New Contact Tag Name', 'easy-property-listings' ),
32: 'add_new_item' => __( 'Add New Contact Tag', 'easy-property-listings' ),
33: 'edit_item' => __( 'Edit Contact Tag', 'easy-property-listings' ),
34: 'update_item' => __( 'Update Contact Tag', 'easy-property-listings' ),
35: 'separate_items_with_commas' => __( 'Separate Contact Tag with commas', 'easy-property-listings' ),
36: 'search_items' => __( 'Search Contact Tag', 'easy-property-listings' ),
37: 'add_or_remove_items' => __( 'Add or remove Contact Tag', 'easy-property-listings' ),
38: 'choose_from_most_used' => __( 'Choose from the most used Contact Tag', 'easy-property-listings' ),
39: 'not_found' => __( 'Contact Tag Not Found', 'easy-property-listings' ),
40: );
41: $rewrite = array(
42: 'slug' => 'epl_contact_tag',
43: 'with_front' => true,
44: 'hierarchical' => false,
45: );
46: $args = array(
47: 'labels' => $labels,
48: 'hierarchical' => false,
49: 'public' => false,
50: 'show_ui' => false,
51: 'show_admin_column' => true,
52: 'show_in_nav_menus' => false,
53: 'show_tagcloud' => true,
54: 'rewrite' => $rewrite,
55: );
56: register_taxonomy( 'epl_contact_tag', array( 'epl_contact' ), $args );
57: }
58: add_action( 'init', 'epl_register_taxonomy_contact_tag', 0 );
59:
60: /**
61: * Add background color on add contact tag screen
62: *
63: * @since 3.0
64: */
65: function epl_add_contact_tag_bgcolor_field() {
66:
67: echo '<div class="form-field epl-contact-tag-bgwrap">
68: <label for="epl_contact_tag_bgcolor">' . esc_html__( 'Background Color', 'easy-property-listings' ) . '</label>
69: <input type="color" class="epl_contact_tag_bgcolor" name="epl_contact_tag_bgcolor" id="epl_contact_tag_bgcolor" value="" />
70: </div>';
71: }
72: add_action( 'contact_tag_add_form_fields', 'epl_add_contact_tag_bgcolor_field' );
73:
74: /**
75: * Add background color on edit contact tag screen
76: *
77: * @since 3.0
78: * @param array $taxonomy term object.
79: */
80: function epl_edit_contact_tag_bgcolor_field( $taxonomy ) {
81:
82: $bg_color = epl_get_contact_tag_bgcolor( $taxonomy->term_id );
83:
84: wp_nonce_field( 'epl_contact_tag_bg_nonce', 'epl_contact_tag_bg_nonce' );
85:
86: echo '
87: <tr class="form-field epl-contact-tag-bgwrap">
88: <th scope="row" valign="top">
89: <label for="epl_contact_tag_bgcolor">' . esc_html__( 'Background Color', 'easy-property-listings' ) . '</label>
90: </th>
91: <td class="eti-image-wrap">
92: <input type="color" class="epl_contact_tag_bgcolor" name="epl_contact_tag_bgcolor" id="epl_contact_tag_bgcolor" value="' . esc_html( $bg_color ) . '" /><br />
93: </td>
94: </tr>';
95: }
96: add_action( 'contact_tag_edit_form_fields', 'epl_edit_contact_tag_bgcolor_field' );
97:
98: /**
99: * Save term background color while saving / editing the term
100: *
101: * @since 3.0
102: * @param string $term_id Term ID.
103: */
104: function epl_save_contact_tag_bgcolor( $term_id ) {
105:
106: if ( ! isset( $_POST['epl_contact_tag_bg_nonce'] )
107: || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['epl_contact_tag_bg_nonce'] ) ), 'epl_contact_tag_bg_nonce' )
108: ) {
109: return;
110: }
111:
112: if ( isset( $_POST['epl_contact_tag_bgcolor'] ) ) {
113: epl_update_contact_tag_bgcolor( $term_id, sanitize_text_field( wp_unslash( $_POST['epl_contact_tag_bgcolor'] ) ) );
114: }
115: }
116: add_action( 'create_term', 'epl_save_contact_tag_bgcolor' );
117: add_action( 'edit_term', 'epl_save_contact_tag_bgcolor' );
118:
119: /**
120: * Returns the background of contact Tag
121: *
122: * @since 3.0
123: * @param string $term_id Term ID.
124: * @return string background color of the contact tag
125: */
126: function epl_get_contact_tag_bgcolor( $term_id ) {
127:
128: // Support for wp version who doesnt support term meta.
129: $all_bgs = get_option( 'epl_contact_tag_bg' );
130:
131: $term_bg = isset( $all_bgs[ $term_id ] ) ? $all_bgs[ $term_id ] : '';
132:
133: if ( empty( $term_bg ) && function_exists( 'get_term_meta' ) ) {
134:
135: $term_bg = get_term_meta( $term_id, 'term_bg', true );
136: }
137:
138: return empty( $term_bg ) ? '#43ac6d' : $term_bg;
139: }
140:
141: /**
142: * Updates the background of contact Tag
143: *
144: * @since 3.0
145: * @param string $term_id Term ID.
146: * @param string $bg_color HTML color code.
147: * @return true if bgcolor updated
148: */
149: function epl_update_contact_tag_bgcolor( $term_id, $bg_color = '#43ac6d' ) {
150:
151: if ( function_exists( 'get_term_meta' ) ) {
152:
153: return update_term_meta( $term_id, 'term_bg', esc_attr( $bg_color ) );
154: }
155:
156: // Support for wp version who doesnt support term meta.
157: $all_bgs = get_option( 'epl_contact_tag_bg' );
158:
159: $all_bgs[ $term_id ] = esc_attr( $bg_color );
160:
161: return update_option( 'epl_contact_tag_bg', $all_bgs );
162: }
163: