PluginProbe
Event Post / 5.11.1
Event Post v5.11.1
6.1.1 6.1.0 6.0.1 6.0.0 5.12.0 trunk 3.9 4.0 4.2 4.3 4.4 4.5 5.0 5.1 5.10.0 5.10.1 5.10.2 5.10.3 5.10.4 5.11.0 5.11.1 5.2 5.5 5.6 5.6.1 All 46 releases
event-post / inc / class-taxonomies.php

class-taxonomies.php in Event Post 5.11.1, at inc/class-taxonomies.php

156 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Taxonomies
4 *
5 * @package event-post
6 * @version 5.11.1
7 * @since 5.6
8 */
9
10 namespace EventPost;
11
12 class Taxonomies{
13 private $META_COLOR = 'taxonomy_color';
14 private $META_ICON = 'taxonomy_icon';
15 private $dashicons = 'taxonomy_icon';
16
17
18 function __construct($dashicons){
19 $this->dashicons = $dashicons;
20 }
21
22
23 function add_fields_to_taxonomies($posttypes){
24 $taxonomies_done = [];
25 foreach($posttypes as $posttype){
26 $taxonomies = get_object_taxonomies( $posttype, 'names' );
27 foreach ($taxonomies as $taxonomy) {
28 if(!isset($taxonomies_done[$taxonomy])){
29 add_action( 'created_'.$taxonomy, array($this, 'save_taxonomy_fields') );
30 add_action( 'edited_'.$taxonomy, array($this, 'save_taxonomy_fields') );
31 add_action( $taxonomy.'_add_form_fields', array($this, 'taxonomy_fields_new'), 10 );
32 add_action( $taxonomy.'_edit_form_fields', array($this, 'taxonomy_fields_edit'), 10, 2 );
33 add_filter( "manage_edit-".$taxonomy."_columns", array($this, 'taxonomy_custom_column_header'), 10);
34 add_action( "manage_".$taxonomy."_custom_column", array($this, 'taxonomy_custom_column_content'), 10, 3);
35 $taxonomies_done[$taxonomy] = $taxonomy;
36 }
37 }
38 }
39 }
40 function taxonomy_fields_new( $taxonomy ) { // Function has one field to pass – Taxonomy
41 $fields = $this->get_fields(false);
42 foreach($fields as $field => $components){
43 ?>
44 <div class="form-field event-color-section <?php echo esc_attr($field) ?>-wrap">
45 <?php echo wp_kses($components['label'], EventPost()->kses_tags)?>
46 <?php echo wp_kses($components['field'], EventPost()->kses_tags)?>
47 <p><?php echo esc_html($components['desc']) ?></p>
48 </div>
49 <?php
50 }
51 }
52 function get_fields($term){
53 $color = "";
54 $icon = "";
55 if($term){
56 $color = get_term_meta( $term->term_id, $this->META_COLOR, true );
57 $icon = get_term_meta( $term->term_id, $this->META_ICON, true );
58 }
59 $options = '<option value="">'.__('None', 'event-post').'</option>';
60 foreach($this->dashicons->icons as $class => $unicode){
61 $options .= '<option value="'.$class.'" '.selected($icon, $class, false).'>&#x'.$unicode.'; '.$class.'</option>';
62 }
63 $fields = [
64 $this->META_COLOR => [
65 "label" => '<label for="taxonomy_'.$this->META_COLOR.'">' .__('Event Color:', 'event-post').'</label>',
66 "field" => '<input class="color-field-taxo eventpost-colorpicker" type="text" name="'.$this->META_COLOR.'" value="'.$color.'" id="taxonomy_'.$this->META_COLOR.'"/>',
67 "desc" => __('This color will be applied to all events of this category on the map, if they do not have their own color, and it will add the color to the term where it is shown on events.', 'event-post')
68 ],
69 $this->META_ICON => [
70 "label" => '<label for="taxonomy_'.$this->META_ICON.'">' .__('Map Icon:', 'event-post').'</label>',
71 "field" => '<select style="font-family : dashicons;" class="eventpost-iconpicker" name="'.$this->META_ICON.'">'.$options.'</select>
72 <div class="custom-marker-container">
73 <p>'.__('An image was found in your custom folder for this color', 'event-post').'<span class="color-hex"></span> </p>
74 <img src="" class="image-marker">
75 </div>',
76 "desc" => __('This color will be applied to all events of this category on the map, if they do not have their own color, and it will add the color to the term where it is shown on events.', 'event-post')
77 ],
78 ];
79 return $fields;
80 }
81 function taxonomy_fields_edit( $term, $taxonomy ) {
82 $fields = $this->get_fields($term);
83 foreach($fields as $field => $components){
84 ?>
85 <tr class='form-field <?php echo esc_attr($field) ?>-wrap'>
86 <th scope='row' valign='top'>
87 <?php echo wp_kses($components['label'], EventPost()->kses_tags)?>
88 </th>
89 <td>
90 <?php echo wp_kses($components['field'], EventPost()->kses_tags); ?>
91 <p class='description'><?php echo esc_html($components['desc']) ?></p>
92 </td>
93 </tr>
94 <?php
95 }
96 }
97
98 function save_taxonomy_fields( $term_id ) {
99 if( ! empty( $_POST[$this->META_COLOR] ) ) {
100 update_term_meta( $term_id, $this->META_COLOR, sanitize_hex_color( $_POST[$this->META_COLOR] ) );
101 }
102 if( ! empty( $_POST[$this->META_ICON] ) ) {
103 update_term_meta( $term_id, $this->META_ICON, sanitize_html_class( $_POST[$this->META_ICON] ) );
104 }
105
106 }
107
108 function taxonomy_custom_column_header( $columns ){
109 $columns['event_post_style'] = __('Event Post Style', 'event-post');
110 return $columns;
111 }
112 function taxonomy_custom_column_content( $content,$column_name,$term_id ){
113 if ($column_name === 'event_post_style') {
114 $icon = $this->get_taxonomy_icon($term_id, "location");
115 $color = $this->get_taxonomy_color($term_id, "#000000");
116 echo '<span class="dashicons dashicons-'.esc_attr($icon).'" style="color : #'.esc_attr($color).'"></span>';
117 }
118 return $content;
119 }
120
121 function get_taxonomy_icon($term_id, $default = false){
122 $icon = get_term_meta( $term_id, $this->META_ICON, true );
123 if($icon && !empty($icon)){
124 return $icon;
125 }else{
126 $ancestors = get_ancestors($term_id, '', 'taxonomy' );
127 if($ancestors){
128 foreach($ancestors as $ancestor){
129 $icon = get_term_meta( $ancestor, $this->META_ICON, true );
130 if($icon && !empty($icon)){
131 return $icon;
132 }
133 }
134 }
135 }
136 return $default;
137 }
138 function get_taxonomy_color($term_id, $default = false){
139 $color = get_term_meta( $term_id, $this->META_COLOR, true );
140 if($color && !empty($color)){
141 return event_post_format_color($color);
142 }else{
143 $ancestors = get_ancestors($term_id, '', 'taxonomy' );
144 if($ancestors){
145 foreach($ancestors as $ancestor){
146 $color = get_term_meta( $ancestor, $this->META_COLOR, true );
147 if($color && !empty($color)){
148 return event_post_format_color($color);
149 }
150 }
151 }
152 }
153 return $default;
154 }
155 }
156