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 / entities / class.term.php

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

510 lines 15.4 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, PSR1.Methods.CamelCapsMethodName.NotCamelCaps
14 if (!class_exists('TagGroups_Term')) {
15 class TagGroups_Term
16 {
17 /**
18 * identificator
19 *
20 * @var int
21 */
22 private $term_id ;
23 /**
24 * taxonomy, needed for updating
25 *
26 * @var int
27 */
28 private $taxonomy ;
29 /**
30 * name, needed for metabox and dynamic post filter
31 *
32 * @var string
33 */
34 private $name ;
35 /**
36 * array of groups that this term is a member of
37 *
38 * @var array
39 */
40 private $groups ;
41 /**
42 * slug of the term
43 *
44 * @var array
45 */
46 private $slug ;
47 /**
48 * count of the term
49 *
50 * @var array
51 */
52 private $count ;
53 /**
54 * last error
55 *
56 * @var string
57 */
58 public $error ;
59 /**
60 * Constructor
61 *
62 * @param int|object $term term
63 * @return object $this|boolean false if error occured during loading
64 */
65 public function __construct($term = null, $tg_terms = null)
66 {
67
68 if (isset($term)) {
69 if (is_object($term)) {
70 /**
71 * We can fill the properties directly from the WP term object.
72 */
73 $this->term_id = $term->term_id;
74 $this->taxonomy = $term->taxonomy;
75 $this->name = $term->name;
76 $this->slug = $term->slug;
77 $this->count = $term->count;
78 } else {
79 $this->term_id = $term;
80 }
81
82 $this->groups = array();
83 return $this->load();
84 }
85
86 return $this;
87 }
88
89 /**
90 * Loads relevant data from the database
91 *
92 * @param void
93 * @return object|boolean $this or false on error
94 */
95 public function load()
96 {
97 if (empty($this->term_id)) {
98 return $this;
99 }
100
101 if (empty($this->groups) || empty($this->taxonomy) || empty($this->name) || empty($this->slug)) {
102 /**
103 * We need to fill the properties from the WP term object.
104 */
105 $tag_groups_hooks = new TagGroups_Hooks();
106 /**
107 * Some plugins hook into get_term but forget to forward term_group
108 */
109
110 if (!empty($this->taxonomy)) {
111 $tag_groups_hooks->remove_all_filters(array( 'get_term', 'get_' . $this->taxonomy ));
112 } else {
113 $tag_groups_hooks->remove_all_filters(array( 'get_term' ));
114 }
115
116 $term = get_term($this->term_id);
117 $tag_groups_hooks->restore_hooks();
118 /**
119 * Check if term exists.
120 */
121
122 if (is_object($term) && !is_wp_error($term)) {
123 $this->taxonomy = $term->taxonomy;
124 $this->name = $term->name;
125 $this->slug = $term->slug;
126 $this->count = $term->count;
127 // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
128 // $this->groups = array( $term->term_group );
129 } else {
130 TagGroups_Error::verbose_log('[Tag Groups] Error loading term (ID %d).', $this->term_id);
131 }
132 }
133
134 $groups = get_term_meta($this->term_id, '_cm_term_group_array', true);
135
136 if (false === $groups || '' === $groups) {
137 // not found
138 $this->groups = array( 0 );
139 } else {
140 $groups_a = explode(',', $groups);
141 // remove empty values
142 $groups_a = array_filter($groups_a, function ($v) {
143 return '' != $v;
144 });
145 // must be ints and no funny keys
146 $groups_a = array_values(array_map('intval', $groups_a));
147 // We return full array even for free plugin, because user might have downgraded after creating multiple groups
148 $this->groups = $groups_a;
149 }
150
151 return $this;
152 }
153
154 /**
155 * Save group-relevant data to the database (We are not saving the name)
156 *
157 * @param boolean $override_permission_check Option to override the permission check if we are saving default groups
158 * @return object $this|boolean false in case of error
159 */
160 public function save($override_permission_check = false)
161 {
162
163 if (empty($this->term_id)) {
164 return $this;
165 }
166
167 if (!$override_permission_check) {
168 /**
169 * Check permissions
170 */
171 $tag_group_role_edit_tags = 'edit_pages';
172 if (TagGroups_Utilities::is_premium_plan()) {
173 $tag_group_role_edit_tags = TagGroups_Options::get_option('tag_group_role_edit_tags', 'edit_pages');
174 }
175
176 if (!current_user_can($tag_group_role_edit_tags)) {
177 TagGroups_Error::verbose_log('[Tag Groups] Insufficient permission to save terms');
178 return $this;
179 }
180 }
181
182 /**
183 * Remove the "not assigned" element - usually with term_group 0
184 * Use a copy of $this->groups.
185 * Bring groups in correct order
186 */
187 $term_groups = $this->get_sorted_groups();
188
189 if (count($term_groups) > 1) {
190 $index_not_assigned = array_search(0, $term_groups);
191 if (false !== $index_not_assigned) {
192 unset($term_groups[$index_not_assigned]);
193 }
194 }
195
196 if (TagGroups_Utilities::is_premium_plan()) {
197 $result = update_term_meta($this->term_id, '_cm_term_group_array', ',' . implode(',', $term_groups) . ',');
198 } else {
199 $first_group = TagGroups_Utilities::get_first_element($term_groups);
200 $result = update_term_meta($this->term_id, '_cm_term_group_array', ',' . $first_group . ',');
201 }
202
203 if ($result) {
204 do_action('tag_groups_groups_of_term_saved', $term_groups, $this->term_id);
205 }
206 return $this;
207 }
208
209 /**
210 * Checks if this term is assigned to at least one of these groups
211 *
212 * @param int|object|array $group (int, object) or groups (array of int)
213 * @return boolean
214 */
215 public function has_group($group)
216 {
217
218 if (0 === $group) {
219 if (empty($this->groups) || array_values($this->groups) == array( 0 )) {
220 return true;
221 } else {
222 return false;
223 }
224 } else {
225 $term_groups = $this->make_array($group);
226
227 if (count(array_intersect($this->groups, $term_groups))) {
228 return true;
229 } else {
230 return false;
231 }
232 }
233 }
234
235 /**
236 * Checks if this term is assigned to all of these groups
237 *
238 * @param int|object|array $group (int, object) or groups (array of int)
239 * @return boolean
240 */
241 public function has_all_groups($group)
242 {
243
244 if (0 === $group) {
245 if (empty($this->groups) || array_values($this->groups) == array( 0 )) {
246 return true;
247 } else {
248 return false;
249 }
250 } else {
251 $term_groups = $this->make_array($group);
252 /**
253 * find out which of the submitted groups are not among this term's groups
254 */
255
256 if (count(array_diff($term_groups, $this->groups))) {
257 return false;
258 } else {
259 return true;
260 }
261 }
262 }
263
264 /**
265 * Checks if this term is assigned to exactly these groups
266 *
267 * @param int|object|array $group (int, object) or groups (array of int)
268 * @return boolean
269 */
270 public function has_exactly_groups($group)
271 {
272
273 if (0 === $group) {
274 if (empty($this->groups) || array_values($this->groups) == array( 0 )) {
275 return true;
276 } else {
277 return false;
278 }
279 } else {
280 $term_groups = $this->make_array($group);
281 /**
282 * find out which of the submitted groups are not among this term's groups
283 */
284
285 if (count(array_diff($term_groups, $this->groups)) || count(array_diff($this->groups, $term_groups))) {
286 return false;
287 } else {
288 return true;
289 }
290 }
291 }
292
293 /**
294 * Getter for $this->groups (values cast to integer)
295 *
296 * @param
297 * @return
298 */
299 public function get_groups()
300 {
301
302 if (is_array($this->groups)) {
303 return array_values(array_map('intval', $this->groups));
304 } else {
305 return (int) $this->groups;
306 }
307 }
308
309 /**
310 * Setter for $this->groups
311 *
312 * @param int|object|array $group (int, object) or groups (array of int)
313 * @return object $this
314 */
315 public function set_group($group)
316 {
317 $this->groups = $this->make_array($group);
318 return $this;
319 }
320
321 /**
322 * Adds one or more groups to $this->groups
323 *
324 * @param int|object|array $group (int, object) or groups (array of int)
325 * @return object|boolean $this|false
326 */
327 public function add_group($group)
328 {
329 if (!is_array($this->groups)) {
330 return $this;
331 }
332 $group = $this->make_array($group);
333
334 if (in_array(0, $group)) {
335 $this->groups = array( 0 );
336 return $this;
337 }
338
339 /**
340 * Important: New group(s) must come first so that it will be saved in base plugin
341 */
342 $this->groups = array_merge($group, $this->groups);
343 return $this;
344 }
345
346 /**
347 * Remove a group from $this->groups
348 *
349 * @param int|object|array $group (int, object) or groups (array of int)
350 * @return object|boolean $this|false
351 */
352 public function remove_group($group)
353 {
354 $this->groups = array_diff($this->groups, $this->make_array($group));
355 if (count($this->groups) == 0) {
356 $this->groups = array( 0 );
357 }
358 return $this;
359 }
360
361 /**
362 * Remove all groups from $this->groups
363 *
364 * @param
365 * @return
366 */
367 public function remove_all_groups()
368 {
369 $this->groups = array( 0 );
370 return $this;
371 }
372
373 /**
374 * Setter for $term_id
375 *
376 * @param int $term_id
377 */
378 public function set_term_id($term_id)
379 {
380 $this->term_id = (int) $term_id;
381 }
382
383 /**
384 * Getter for $term_id
385 *
386 * @return int
387 */
388 public function get_term_id()
389 {
390 return $this->term_id;
391 }
392
393 /**
394 * returns the term's name
395 *
396 * @param void
397 * @return string
398 */
399 public function get_name()
400 {
401 return $this->name;
402 }
403
404 /**
405 * returns the term's taxonomy
406 *
407 * @param void
408 * @return string
409 */
410 public function get_taxonomy()
411 {
412 return $this->taxonomy;
413 }
414
415 /**
416 * returns the term's slug
417 *
418 * @param void
419 * @return string
420 */
421 public function get_slug()
422 {
423 return $this->slug;
424 }
425
426 /**
427 * Makes an array of group ids from an object, an integer or an array
428 * includes sanitation
429 *
430 * @param object|array|integer
431 * @return array one-dimensional array of integers (term_group values)
432 */
433 public function make_array($group)
434 {
435
436 if (is_object($group)) {
437 return array( (int) $group->get_group_id() );
438 } elseif (is_array($group)) {
439 return array_map('intval', $group);
440 } else {
441 return array( (int) $group );
442 }
443 }
444
445 /**
446 * Returns the post count for the term, considering the group
447 *
448 * @param integer $group_id
449 * @return integer
450 */
451 public function get_post_count($group_id = 0)
452 {
453
454 if (0 == $group_id) {
455 return $this->count;
456 }
457
458 if (TagGroups_Utilities::is_premium_plan()) {
459 global $tag_group_premium_terms;
460
461 if (empty($tag_group_premium_terms)) {
462 return $this->count;
463 }
464
465 $post_counts = $tag_group_premium_terms->get_post_counts();
466
467 if (!isset($post_counts[$this->term_id])) {
468 return $this->count;
469 }
470
471 if (!isset($post_counts[$this->term_id][$group_id])) {
472 return 0;
473 }
474
475 return $post_counts[$this->term_id][$group_id];
476 }
477 }
478
479 /**
480 * Checks whether the term exists as WP term
481 *
482 * @return boolean
483 */
484 public function exists()
485 {
486 if (empty($this->term_id)) {
487 return false;
488 }
489 return (bool) term_exists($this->term_id);
490 }
491
492 /**
493 * Sort the groups according to the positions
494 *
495 * @return array
496 */
497 public function get_sorted_groups()
498 {
499 global $tag_group_groups ;
500 $groups_sorted = array();
501 foreach ($tag_group_groups->get_positions() as $group => $position) {
502 if (in_array($group, $this->groups)) {
503 $groups_sorted[] = $group;
504 }
505 }
506 return $groups_sorted;
507 }
508 }
509 }
510