Plugin Directory

source: event-post/trunk/inc/class-taxonomies.php @ 3060317

Last change on this file since 3060317 was 3060317, checked in by bastho, 4 months ago

Bump version to 5.9.3

File size: 5.8 KB
Line 
1<?php
2/**
3 * Taxonomies
4 *
5 * @package event-post
6 * @version 5.9.3
7 * @since   5.6
8 */
9
10namespace EventPost;
11
12class 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 $field ?>-wrap">
45        <?php echo $components['label']?>
46        <?php echo $components['field']?>
47        <p><?php echo $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 $field ?>-wrap'>
86        <th scope='row' valign='top'>
87          <?php echo $components['label']?>
88        </th>
89        <td>
90          <?php echo $components['field']?>
91          <p class='description'><?php echo $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-'.$icon.'" style="color : #'.$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}
Note: See TracBrowser for help on using the repository browser.