PluginProbe
Event Post / 6.1.2
Event Post v6.1.2
6.1.2 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 All 47 releases
event-post / inc / class-taxonomies.php

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

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