PluginProbe
Tag Groups is the Advanced Way to Display Your Taxonomy Terms / trunk
Tag Groups is the Advanced Way to Display Your Taxonomy Terms vtrunk
2.2.2 2.2.1 2.2.0 trunk 1.40.0 1.40.2 1.41.0 1.42.1 1.43.0 1.43.1 1.43.10 1.43.10.1 1.43.2 1.43.3 1.43.4 1.43.5 1.43.6 1.43.7 1.43.9 1.44.0 1.44.1 1.44.3 1.44.3.1 2.0.0 2.0.1 All 36 releases
tag-groups / include / helpers / class.terms.php

class.terms.php in Tag Groups is the Advanced Way to Display Your Taxonomy Terms trunk, at include/helpers/class.terms.php

176 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Tag Groups
5 *
6 * @package Tag Groups
7 *
8 * @author Christoph Amthor
9 * @copyright 2018 Christoph Amthor (@ Chatty Mango, chattymango.com)
10 * @license GPL-3.0+
11 */
12
13 // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, Squiz.Scope.MethodScope.Missing, PSR1.Methods.CamelCapsMethodName.NotCamelCaps
14 if (!class_exists('TagGroups_Terms')) {
15 class TagGroups_Terms
16 {
17 /**
18 * Deletes the transients that are relevant for terms and groups
19 *
20 * To be called only if general purging of cache is needed. Otherwise use hooks.
21 *
22 * @param int $rebuild_in_seconds Turned off if less than 0.
23 * @return void
24 */
25 public function clear_term_cache($rebuild_in_seconds = 10)
26 {
27 $this->clear_post_count_transient($rebuild_in_seconds);
28 $languages = apply_filters('wpml_active_languages', null, '');
29
30 if (!empty($languages)) {
31 foreach ($languages as $language_code => $language_info) {
32 TagGroups_Transients::delete_all_transients('tag_groups_group_terms-' . $language_code);
33 }
34 } else {
35 TagGroups_Transients::delete_all_transients('tag_groups_group_terms');
36 }
37
38 TagGroups_Transients::delete_transient('tag_groups_post_terms');
39 TagGroups_Transients::delete_transient('tag_groups_post_types');
40 TagGroups_Transients::delete_transient('tag_groups_post_ids_groups');
41 }
42
43 /**
44 * Clears the transient of post counts per group for terms
45 *
46 * @param integer $rebuild_in_seconds
47 * @return void
48 */
49 public function clear_post_count_transient($rebuild_in_seconds = 5)
50 {
51 if (!TagGroups_Utilities::is_premium_plan()) {
52 return;
53 }
54
55 $languages = apply_filters('wpml_active_languages', null, '');
56
57 if (!empty($languages)) {
58 foreach ($languages as $language_code => $language_info) {
59 TagGroups_Transients::delete_transient('tag_groups_post_counts-' . $language_code);
60 }
61 } else {
62 TagGroups_Transients::delete_transient('tag_groups_post_counts');
63 }
64
65 if ($rebuild_in_seconds < 0) {
66 return;
67 }
68
69 if (
70 (!defined('TAG_GROUPS_DISABLE_CACHE_REBUILD') || TAG_GROUPS_DISABLE_CACHE_REBUILD)
71 && class_exists('TagGroups_Premium_Cron')
72 ) {
73 TagGroups_Premium_Cron::schedule_in_secs($rebuild_in_seconds, 'tag_groups_rebuild_post_counts');
74 }
75 }
76
77 /**
78 * Helper for natural sorting of names
79 *
80 * Inspired by _wp_object_name_sort_cb
81 *
82 * @param array $terms
83 * @param string $order asc or desc
84 * @return array
85 */
86 public function natural_sorting($terms, $order)
87 {
88 $factor = ( 'desc' == strtolower($order) ? -1 : 1 );
89 // "use" requires PHP 5.3+
90 uasort($terms, function ($a, $b) use ($factor) {
91 return $factor * strnatcasecmp($a->name, $b->name);
92 });
93 return array_values($terms);
94 }
95
96 /**
97 * Modifies the query to show only terms that belong to particular tag group
98 *
99 * @param array $args
100 * @param array|int|string $group_ids Tag Group IDs (array of integers or comma-separated list of integers)
101 * @param string $relation Logic relation between the Tag Group IDs (and|or)
102 * @return array
103 */
104 public function modify_query_args($args, $group_ids = null, $relation = 'OR')
105 {
106 global $tag_group_groups ;
107 if (empty($group_ids)) {
108 return $args;
109 }
110 if (!is_array($group_ids)) {
111 $group_ids = explode(',', $group_ids);
112 }
113 $group_ids = array_map('intval', $group_ids);
114 // intval also trims spaces
115 if (strtoupper($relation) != 'OR') {
116 $relation = 'AND';
117 }
118 /**
119 * searching for not-assigned terms
120 */
121 if (in_array(0, $group_ids)) {
122 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
123 $args['meta_query'] = array(
124 'relation' => 'OR',
125 array(
126 'key' => '_cm_term_group_array',
127 'value' => ',0,',
128 'compare' => 'LIKE',
129 ),
130 array(
131 'key' => '_cm_term_group_array',
132 'compare' => 'NOT EXISTS',
133 ),
134 );
135 return $args;
136 }
137
138 $meta_query = array(
139 'relation' => $relation,
140 );
141 $group_ids = array_intersect($group_ids, $tag_group_groups->get_group_ids());
142
143 if (count($group_ids) == 0) {
144 // never matches -> create dummy condition that never is true
145 $meta_query[] = array(
146 'key' => '_cm_term_group_array_dummy',
147 'compare' => 'EXISTS',
148 );
149 } else {
150 foreach ($group_ids as $group_id) {
151 $meta_query[] = array(
152 'key' => '_cm_term_group_array',
153 'value' => ',' . $group_id . ',',
154 'compare' => 'LIKE',
155 );
156 }
157 }
158 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
159 $args['meta_query'] = $meta_query;
160 return $args;
161 }
162
163 /**
164 * Filter to enable term_order for orderby
165 *
166 * @param string $orderby
167 * @param array $query_vars
168 * @return array
169 */
170 public function enable_terms_order($orderby, $query_vars)
171 {
172 return ( 'term_order' == $query_vars['orderby'] ? 'term_order' : $orderby );
173 }
174 }
175 }
176