| 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 |
* @since 1.8.0 |
| 13 |
*/ |
| 14 |
|
| 15 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, PSR1.Methods.CamelCapsMethodName.NotCamelCaps, WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize, WordPress.DB.SlowDBQuery |
| 16 |
if (!class_exists('TagGroups_Group')) { |
| 17 |
class TagGroups_Group |
| 18 |
{ |
| 19 |
/** |
| 20 |
* term_group id |
| 21 |
* |
| 22 |
* @var int |
| 23 |
*/ |
| 24 |
private $group_id ; |
| 25 |
/** |
| 26 |
* object of groups |
| 27 |
* |
| 28 |
* @var object |
| 29 |
*/ |
| 30 |
private $tg_groups ; |
| 31 |
/** |
| 32 |
* @var int |
| 33 |
*/ |
| 34 |
private $position ; |
| 35 |
/** |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
private $label ; |
| 40 |
/** |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
private $parent ; |
| 45 |
/** |
| 46 |
* |
| 47 |
* @var boolean |
| 48 |
*/ |
| 49 |
public $is_parent ; |
| 50 |
/** |
| 51 |
* instance of the premium part |
| 52 |
* |
| 53 |
* @var object |
| 54 |
*/ |
| 55 |
private $tg_group_premium ; |
| 56 |
/** |
| 57 |
* last error |
| 58 |
* |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
public $error ; |
| 62 |
public const WRONG_ID = 'wrong ID' ; |
| 63 |
public const WRONG_POSITION = 'wrong position' ; |
| 64 |
/** |
| 65 |
* Constructor |
| 66 |
* |
| 67 |
* @param int $group_id optional term_group |
| 68 |
* @return return type |
| 69 |
*/ |
| 70 |
public function __construct($group_id = null) |
| 71 |
{ |
| 72 |
global $tag_group_groups ; |
| 73 |
$this->tg_groups = $tag_group_groups; |
| 74 |
$this->is_parent = false; |
| 75 |
if (isset($group_id)) { |
| 76 |
$this->set_group_id($group_id); |
| 77 |
} |
| 78 |
$this->load(); |
| 79 |
if (TagGroups_Utilities::is_premium_plan() && class_exists('TagGroups_Premium_Group')) { |
| 80 |
$this->tg_group_premium = new TagGroups_Premium_Group($this->group_id); |
| 81 |
} |
| 82 |
|
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Load data from database |
| 88 |
* |
| 89 |
* @param int $group_id optional term_group |
| 90 |
* @return return type |
| 91 |
*/ |
| 92 |
public function load() |
| 93 |
{ |
| 94 |
|
| 95 |
$labels = $this->tg_groups->get_labels(); |
| 96 |
|
| 97 |
if (isset($labels[$this->group_id])) { |
| 98 |
$this->label = $labels[$this->group_id]; |
| 99 |
} else { |
| 100 |
$this->label = ''; |
| 101 |
} |
| 102 |
|
| 103 |
$positions = $this->tg_groups->get_positions(); |
| 104 |
|
| 105 |
if (isset($positions[$this->group_id])) { |
| 106 |
$this->position = $positions[$this->group_id]; |
| 107 |
} else { |
| 108 |
$this->position = 1; |
| 109 |
} |
| 110 |
|
| 111 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 112 |
$parents = $this->tg_groups->get_parents(); |
| 113 |
$this->is_parent = is_array($parents) && in_array($this->group_id, $parents); |
| 114 |
} |
| 115 |
|
| 116 |
return $this; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* checks whether this group exists, identified by its ID |
| 121 |
* |
| 122 |
* |
| 123 |
* @param void |
| 124 |
* @return boolean |
| 125 |
*/ |
| 126 |
public function exists() |
| 127 |
{ |
| 128 |
if (0 == $this->group_id) { |
| 129 |
return true; |
| 130 |
} |
| 131 |
return isset($this->group_id) && in_array($this->group_id, $this->tg_groups->get_group_ids()); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Saves this group to the database |
| 136 |
* |
| 137 |
* |
| 138 |
* @param void |
| 139 |
* @return object $this |
| 140 |
*/ |
| 141 |
public function save() |
| 142 |
{ |
| 143 |
|
| 144 |
|
| 145 |
if (empty($this->group_id)) { |
| 146 |
$this->error = TagGroups_Group::WRONG_ID; |
| 147 |
return $this; |
| 148 |
} |
| 149 |
|
| 150 |
$labels = $this->tg_groups->get_labels(); |
| 151 |
$positions = $this->tg_groups->get_positions(); |
| 152 |
$labels[$this->group_id] = $this->label; |
| 153 |
$positions[$this->group_id] = $this->position; |
| 154 |
$this->tg_groups->set_labels($labels); |
| 155 |
$this->tg_groups->set_positions($positions); |
| 156 |
if (TagGroups_Utilities::is_premium_plan() && $this->is_parent) { |
| 157 |
$parents = $this->tg_groups->get_parents(); |
| 158 |
if (!in_array($this->group_id, $parents)) { |
| 159 |
$parents[] = $this->group_id; |
| 160 |
$this->tg_groups->set_parents($parents); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
$this->tg_groups->save(); |
| 165 |
return $this; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* getter for the term_group value |
| 170 |
* |
| 171 |
* |
| 172 |
* @param void |
| 173 |
* @return int term_group |
| 174 |
*/ |
| 175 |
public function get_group_id() |
| 176 |
{ |
| 177 |
if (!isset($this->group_id)) { |
| 178 |
return null; |
| 179 |
} |
| 180 |
return $this->group_id; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* setter for the term_group value |
| 185 |
* |
| 186 |
* @param int $group_id |
| 187 |
* @return object $this |
| 188 |
*/ |
| 189 |
public function set_group_id($group_id) |
| 190 |
{ |
| 191 |
|
| 192 |
$this->group_id = (int) $group_id; |
| 193 |
if (TagGroups_Utilities::is_premium_plan() && isset($this->tg_group_premium)) { |
| 194 |
$this->tg_group_premium->set_group_id((int) $group_id); |
| 195 |
} |
| 196 |
|
| 197 |
return $this; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* adds a new group and saves it |
| 202 |
* |
| 203 |
* @param string $label label of the new group |
| 204 |
* @param int $position position of the new group; null means after last |
| 205 |
* @return object |
| 206 |
*/ |
| 207 |
public function create($label, $position = null, $is_parent = false) |
| 208 |
{ |
| 209 |
$this->set_group_id($this->tg_groups->get_max_term_group() + 1); |
| 210 |
$this->label = $label; |
| 211 |
$this->set_position($this->tg_groups->get_max_position() + 1); |
| 212 |
$this->tg_groups->reindex_positions()->add_group($this); |
| 213 |
if (!empty($position) && $position != $this->position) { |
| 214 |
$this->move_to_position($position); |
| 215 |
} |
| 216 |
$this->is_parent = $is_parent; |
| 217 |
$this->save(); |
| 218 |
if (!empty($this->tg_groups->error)) { |
| 219 |
$this->error = $this->tg_groups->error; |
| 220 |
} |
| 221 |
return $this; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* returns all terms that are associated with this term group |
| 226 |
* |
| 227 |
* @param string|array $taxonomy See get_terms |
| 228 |
* @param string $hide_empty See get_terms |
| 229 |
* @param string $fields See get_terms |
| 230 |
* @param string $post_id Possibly required to determine the language |
| 231 |
* @param string $orderby See get_terms |
| 232 |
* @param string $order See get_terms |
| 233 |
* @return array|integer |
| 234 |
*/ |
| 235 |
public function get_group_terms( |
| 236 |
$taxonomy = 'post_tag', |
| 237 |
$hide_empty = false, |
| 238 |
$fields = 'all', |
| 239 |
$post_id = 0, |
| 240 |
$orderby = 'name', |
| 241 |
$order = 'ASC' |
| 242 |
) { |
| 243 |
global $tag_group_terms ; |
| 244 |
if (!isset($this->group_id)) { |
| 245 |
return array(); |
| 246 |
} |
| 247 |
$orderby = is_string($orderby) ? strtolower($orderby) : 'name'; |
| 248 |
/** |
| 249 |
* Remove invalid taxonomies |
| 250 |
*/ |
| 251 |
$taxonomy = TagGroups_Taxonomy::remove_invalid($taxonomy); |
| 252 |
/** |
| 253 |
* In case we use the Polylang plugin: get the terms for the language of that post. |
| 254 |
* Polylang needs extra query parameter |
| 255 |
*/ |
| 256 |
|
| 257 |
if (function_exists('pll_get_post_language') && $post_id) { |
| 258 |
/** |
| 259 |
* Better sanitize what we get from other plugins |
| 260 |
*/ |
| 261 |
$pll_post_language = sanitize_text_field(pll_get_post_language($post_id, 'locale')); |
| 262 |
} else { |
| 263 |
$pll_post_language = ''; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* In case we use the WPML plugin: consider the language |
| 268 |
*/ |
| 269 |
$current_language = TagGroups_WPML::get_current_language(); |
| 270 |
|
| 271 |
if ($current_language) { |
| 272 |
$wpml_language = (string) ICL_LANGUAGE_CODE; |
| 273 |
} else { |
| 274 |
$wpml_language = ''; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* try to get cached version |
| 279 |
* |
| 280 |
* We need to supply the language parameters |
| 281 |
*/ |
| 282 |
|
| 283 |
if ("count" == $fields) { |
| 284 |
$cache_key = md5('count-' . serialize($taxonomy) . '-' . serialize($hide_empty) . '-ids-' . $pll_post_language . '-' . $wpml_language); |
| 285 |
$count_transient_name = TagGroups_WPML::get_tag_groups_group_terms_transient_name() . '-' . $cache_key; |
| 286 |
$count_transient_value = TagGroups_Transients::get_transient($count_transient_name); |
| 287 |
if (false !== $count_transient_value && isset($count_transient_value[$this->group_id])) { |
| 288 |
return $count_transient_value[$this->group_id]; |
| 289 |
} |
| 290 |
} else { |
| 291 |
$cache_key = md5($this->group_id . '-' . serialize($taxonomy) . '-' . serialize($hide_empty) . '-' . (is_string($fields) ? strtolower($fields) : 'all') . '-' . $orderby . '-' . (is_string($order) ? strtolower($order) : 'asc') . '-' . $pll_post_language . '-' . $wpml_language); |
| 292 |
$transient_name = TagGroups_WPML::get_tag_groups_group_terms_transient_name() . '-' . $cache_key; |
| 293 |
$transient_value = TagGroups_Transients::get_transient($transient_name); |
| 294 |
if (false !== $transient_value) { |
| 295 |
return $transient_value; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
$orderby_query = $orderby; |
| 300 |
if ('natural' == $orderby) { |
| 301 |
$orderby_query = 'name'; |
| 302 |
} |
| 303 |
$default_orderby = array( |
| 304 |
'name', |
| 305 |
'slug', |
| 306 |
'term_group', |
| 307 |
'term_id', |
| 308 |
'id', |
| 309 |
'description', |
| 310 |
'parent', |
| 311 |
'term_order', |
| 312 |
'count' |
| 313 |
); |
| 314 |
|
| 315 |
if (!in_array($orderby_query, $default_orderby)) { |
| 316 |
// order by a custom meta field |
| 317 |
$additional_args = array( |
| 318 |
'meta_key' => $orderby_query, |
| 319 |
'meta_compare' => 'NUMERIC', |
| 320 |
); |
| 321 |
$orderby_query = 'meta_value_num'; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
if (0 == $this->group_id) { |
| 326 |
$args = array( |
| 327 |
'taxonomy' => $taxonomy, |
| 328 |
'hide_empty' => $hide_empty, |
| 329 |
'fields' => $fields, |
| 330 |
'orderby' => $orderby_query, |
| 331 |
'order' => $order, |
| 332 |
'meta_query' => array( |
| 333 |
'relation' => 'OR', |
| 334 |
array( |
| 335 |
'key' => '_cm_term_group_array', |
| 336 |
'value' => ',0,', |
| 337 |
'compare' => 'LIKE', |
| 338 |
), |
| 339 |
array( |
| 340 |
'key' => '_cm_term_group_array', |
| 341 |
'compare' => 'NOT EXISTS', |
| 342 |
), |
| 343 |
), |
| 344 |
); |
| 345 |
} else { |
| 346 |
$args = array( |
| 347 |
'taxonomy' => $taxonomy, |
| 348 |
'hide_empty' => $hide_empty, |
| 349 |
'fields' => $fields, |
| 350 |
'orderby' => $orderby_query, |
| 351 |
'order' => $order, |
| 352 |
'meta_query' => array( array( |
| 353 |
'key' => '_cm_term_group_array', |
| 354 |
'value' => ',' . $this->group_id . ',', |
| 355 |
'compare' => 'LIKE', |
| 356 |
) ), |
| 357 |
); |
| 358 |
} |
| 359 |
|
| 360 |
if (isset($additional_args)) { |
| 361 |
$args = array_merge($args, $additional_args); |
| 362 |
} |
| 363 |
/** |
| 364 |
* Add Polylang query parameter |
| 365 |
*/ |
| 366 |
if (!empty($pll_post_language)) { |
| 367 |
$args['lang'] = $pll_post_language; |
| 368 |
} |
| 369 |
/** |
| 370 |
* term_order requires special treatment |
| 371 |
*/ |
| 372 |
if ('term_order' == $orderby_query) { |
| 373 |
add_filter( |
| 374 |
'get_terms_orderby', |
| 375 |
array( $tag_group_terms, 'enable_terms_order' ), |
| 376 |
10, |
| 377 |
2 |
| 378 |
); |
| 379 |
} |
| 380 |
$terms = get_terms($args); |
| 381 |
if (!empty($terms)) { |
| 382 |
if ('natural' == $orderby) { |
| 383 |
$terms = $tag_group_terms->natural_sorting($terms, $order); |
| 384 |
} |
| 385 |
} |
| 386 |
/** |
| 387 |
* Filters the terms of a group before it's returned or saved to the transient |
| 388 |
* |
| 389 |
* @param WP_Term[]|int[]|string[]|string|WP_Error $terms Return type depends on $fields |
| 390 |
* @param int $this->group_id ID of this group |
| 391 |
* @param string|string[] $taxonomy |
| 392 |
* @param bool|int $hide_empty Whether to hide terms with post count zero |
| 393 |
* @param string $fields What to return. See WP's get_terms() |
| 394 |
* @param int $post_id This parameter is only relevant if the tags depend on the language of a post |
| 395 |
* @param string $orderby |
| 396 |
* @param string $order |
| 397 |
* @param string include |
| 398 |
* @param string exclude |
| 399 |
* @param int threshold |
| 400 |
* |
| 401 |
* @return mixed $terms Must be the same type as the input $terms |
| 402 |
*/ |
| 403 |
$terms = apply_filters( |
| 404 |
'tag_groups_get_terms', |
| 405 |
$terms, |
| 406 |
$this->group_id, |
| 407 |
$taxonomy, |
| 408 |
$hide_empty, |
| 409 |
$fields, |
| 410 |
$post_id, |
| 411 |
$orderby, |
| 412 |
$order, |
| 413 |
'', |
| 414 |
'', |
| 415 |
0 |
| 416 |
); |
| 417 |
|
| 418 |
if ("count" == $fields) { |
| 419 |
if (!is_array($count_transient_value)) { |
| 420 |
$count_transient_value = array(); |
| 421 |
} |
| 422 |
$count_transient_value[$this->group_id] = $terms; |
| 423 |
TagGroups_Transients::set_transient($count_transient_name, $count_transient_value, 1 * HOUR_IN_SECONDS); |
| 424 |
} else { |
| 425 |
TagGroups_Transients::set_transient($transient_name, $terms, 1 * HOUR_IN_SECONDS); |
| 426 |
} |
| 427 |
|
| 428 |
return $terms; |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* adds terms to this group |
| 433 |
* |
| 434 |
* @param array $term_ids one-dimensional array of term IDs or WP terms |
| 435 |
* @return object $this |
| 436 |
*/ |
| 437 |
public function add_terms($term_ids) |
| 438 |
{ |
| 439 |
if (!is_int($this->group_id)) { |
| 440 |
return $this; |
| 441 |
} |
| 442 |
foreach ($term_ids as $term_id) { |
| 443 |
$tg_term = new TagGroups_Term($term_id); |
| 444 |
$tg_term->add_group($this->group_id); |
| 445 |
$tg_term->save(); |
| 446 |
} |
| 447 |
return $this; |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* removes terms from this group |
| 452 |
* |
| 453 |
* @param array $term_ids one-dimensional array of term IDs |
| 454 |
* @return object $this |
| 455 |
*/ |
| 456 |
public function remove_terms($term_ids = array()) |
| 457 |
{ |
| 458 |
$enabled_taxonomies = TagGroups_Taxonomy::get_enabled_taxonomies(); |
| 459 |
if (empty($term_ids)) { |
| 460 |
$term_ids = $this->get_group_terms($enabled_taxonomies, false, 'ids'); |
| 461 |
} |
| 462 |
foreach ($term_ids as $term_id) { |
| 463 |
$tg_term = new TagGroups_Term($term_id); |
| 464 |
$tg_term->remove_group($this)->save(); |
| 465 |
} |
| 466 |
return $this; |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* deletes this group |
| 471 |
* |
| 472 |
* @param int $group_id ID of this group |
| 473 |
* @return object $this |
| 474 |
*/ |
| 475 |
public function delete() |
| 476 |
{ |
| 477 |
|
| 478 |
$group_ids = $this->tg_groups->get_group_ids(); |
| 479 |
|
| 480 |
if (($key = array_search($this->group_id, $group_ids)) === false) { |
| 481 |
$this->error = TagGroups_Group::WRONG_ID; |
| 482 |
return $this; |
| 483 |
} |
| 484 |
|
| 485 |
$labels = $this->tg_groups->get_labels(); |
| 486 |
$positions = $this->tg_groups->get_positions(); |
| 487 |
unset($group_ids[$key]); |
| 488 |
unset($labels[$this->group_id]); |
| 489 |
// remove labels of translations |
| 490 |
$tag_group_group_languages = TagGroups_Options::get_option('tag_group_group_languages', array()); |
| 491 |
if (!empty($tag_group_group_languages)) { |
| 492 |
foreach ($tag_group_group_languages as $language) { |
| 493 |
$translated_labels = TagGroups_Options::get_option('term_group_labels_' . $language, array()); |
| 494 |
|
| 495 |
if (is_array($translated_labels)) { |
| 496 |
unset($translated_labels[$this->group_id]); |
| 497 |
TagGroups_Options::update_option('term_group_labels_' . $language, $translated_labels); |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
unset($positions[$this->group_id]); |
| 502 |
$this->tg_groups->set_labels($labels); |
| 503 |
$this->tg_groups->set_positions($positions); |
| 504 |
$this->tg_groups->set_group_ids($group_ids); |
| 505 |
$this->tg_groups->reindex_positions(); |
| 506 |
$this->tg_groups->save(); |
| 507 |
if (!empty($this->tg_groups->error)) { |
| 508 |
$this->error = $this->tg_groups->error; |
| 509 |
} |
| 510 |
$this->remove_terms(); |
| 511 |
do_action('tag_groups_term_group_deleted', $this->group_id); |
| 512 |
unset($this->group_id); |
| 513 |
return $this; |
| 514 |
} |
| 515 |
|
| 516 |
/** |
| 517 |
* returns the position of this group |
| 518 |
* |
| 519 |
* @param void |
| 520 |
* @return int|boolean |
| 521 |
*/ |
| 522 |
public function get_position() |
| 523 |
{ |
| 524 |
return $this->position; |
| 525 |
} |
| 526 |
|
| 527 |
/** |
| 528 |
* sets the position of this group |
| 529 |
* |
| 530 |
* @param int $position position of this group |
| 531 |
* @return object $this |
| 532 |
*/ |
| 533 |
public function set_position($position) |
| 534 |
{ |
| 535 |
|
| 536 |
if ($position < 1 || $position > $this->tg_groups->get_max_position() + 1) { |
| 537 |
$this->error = TagGroups_Group::WRONG_POSITION; |
| 538 |
return $this; |
| 539 |
} |
| 540 |
|
| 541 |
$this->position = $position; |
| 542 |
return $this; |
| 543 |
} |
| 544 |
|
| 545 |
/** |
| 546 |
* sets the position of this group |
| 547 |
* |
| 548 |
* @param int $position position of this group |
| 549 |
* @return object $this |
| 550 |
*/ |
| 551 |
public function move_to_position($new_position) |
| 552 |
{ |
| 553 |
|
| 554 |
if (empty($this->group_id)) { |
| 555 |
$this->error = TagGroups_Group::WRONG_ID; |
| 556 |
return $this; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
if ($new_position < 1 || $new_position > $this->tg_groups->get_max_position() + 1) { |
| 561 |
$this->error = TagGroups_Group::WRONG_POSITION; |
| 562 |
return $this; |
| 563 |
} |
| 564 |
|
| 565 |
$old_position = $this->get_position(); |
| 566 |
$positions = $this->tg_groups->get_positions(); |
| 567 |
/** |
| 568 |
* 1. move down on old position |
| 569 |
*/ |
| 570 |
foreach ($positions as $key => $value) { |
| 571 |
if ($value > $old_position) { |
| 572 |
$positions[$key] = $value - 1; |
| 573 |
} |
| 574 |
} |
| 575 |
/** |
| 576 |
* 2. make space at new position |
| 577 |
*/ |
| 578 |
foreach ($positions as $key => $value) { |
| 579 |
if ($value >= $new_position) { |
| 580 |
$positions[$key] = $value + 1; |
| 581 |
} |
| 582 |
} |
| 583 |
/** |
| 584 |
* 3. Insert |
| 585 |
*/ |
| 586 |
$positions[$this->group_id] = $new_position; |
| 587 |
$this->tg_groups->set_positions($positions); |
| 588 |
$this->position = $new_position; |
| 589 |
$this->tg_groups->reindex_positions(); |
| 590 |
return $this; |
| 591 |
} |
| 592 |
|
| 593 |
/** |
| 594 |
* returns the label of this group |
| 595 |
* |
| 596 |
* @param void |
| 597 |
* @return string|boolean |
| 598 |
*/ |
| 599 |
public function get_label() |
| 600 |
{ |
| 601 |
if (!isset($this->group_id)) { |
| 602 |
// allow also "not assigned" |
| 603 |
return false; |
| 604 |
} |
| 605 |
return $this->label; |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* sets the label of this group |
| 610 |
* |
| 611 |
* @param string $label label of this group |
| 612 |
* @return object $this |
| 613 |
*/ |
| 614 |
public function set_label($label) |
| 615 |
{ |
| 616 |
|
| 617 |
if (empty($this->group_id)) { |
| 618 |
$this->error = TagGroups_Group::WRONG_ID; |
| 619 |
return $this; |
| 620 |
} |
| 621 |
|
| 622 |
$this->label = $label; |
| 623 |
return $this; |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* returns the number of terms associated with this group |
| 628 |
* |
| 629 |
* @param void |
| 630 |
* @return int |
| 631 |
*/ |
| 632 |
public function get_number_of_terms($taxonomies) |
| 633 |
{ |
| 634 |
if (!isset($this->group_id)) { |
| 635 |
return false; |
| 636 |
} |
| 637 |
if (!is_array($taxonomies)) { |
| 638 |
$taxonomies = array( $taxonomies ); |
| 639 |
} |
| 640 |
/** |
| 641 |
* Consider only taxonomies that |
| 642 |
* 1. are among $tag_group_taxonomies |
| 643 |
* 2. actually exist |
| 644 |
*/ |
| 645 |
$taxonomies = TagGroups_Taxonomy::remove_invalid($taxonomies); |
| 646 |
return $this->get_group_terms($taxonomies, false, 'count'); |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
* sets $this->group_id by label |
| 651 |
* |
| 652 |
* @param string $label |
| 653 |
* @return boolean|int |
| 654 |
*/ |
| 655 |
public function find_by_label($label) |
| 656 |
{ |
| 657 |
$labels = $this->tg_groups->get_labels(); |
| 658 |
|
| 659 |
if (in_array($label, $labels)) { |
| 660 |
$this->set_group_id(array_search($label, $labels)); |
| 661 |
$this->load(); |
| 662 |
return $this; |
| 663 |
} else { |
| 664 |
return false; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
/** |
| 669 |
* sets $this->group_id by position |
| 670 |
* |
| 671 |
* |
| 672 |
* @param int $position |
| 673 |
* @return boolean|int |
| 674 |
*/ |
| 675 |
public function find_by_position($position) |
| 676 |
{ |
| 677 |
$positions = $this->tg_groups->get_positions(); |
| 678 |
|
| 679 |
if (in_array($position, $positions)) { |
| 680 |
$this->set_group_id(array_search($position, $positions)); |
| 681 |
$this->load(); |
| 682 |
return $this; |
| 683 |
} else { |
| 684 |
$this->set_group_id(0); |
| 685 |
return false; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* returns an array of group properties as values |
| 691 |
* |
| 692 |
* @param void |
| 693 |
* @return array |
| 694 |
*/ |
| 695 |
public function get_info( |
| 696 |
$taxonomy = null, |
| 697 |
$hide_empty = false, |
| 698 |
$fields = null, |
| 699 |
$orderby = 'name', |
| 700 |
$order = 'ASC' |
| 701 |
) { |
| 702 |
// dealing with NULL values |
| 703 |
if (empty($fields)) { |
| 704 |
$fields = 'ids'; |
| 705 |
} |
| 706 |
if (empty($taxonomy)) { |
| 707 |
$taxonomy = TagGroups_Taxonomy::get_enabled_taxonomies(); |
| 708 |
} |
| 709 |
if (!isset($hide_empty) || empty($hide_empty)) { |
| 710 |
$hide_empty = false; |
| 711 |
} |
| 712 |
$terms = $this->get_group_terms( |
| 713 |
$taxonomy, |
| 714 |
$hide_empty, |
| 715 |
$fields, |
| 716 |
0, |
| 717 |
$orderby, |
| 718 |
$order |
| 719 |
); |
| 720 |
|
| 721 |
if (!is_array($terms)) { |
| 722 |
$terms = array(); |
| 723 |
TagGroups_Error::log('[Tag Groups] Error retrieving terms in get_info().'); |
| 724 |
} |
| 725 |
|
| 726 |
return array( |
| 727 |
'term_group' => (int) $this->group_id, |
| 728 |
'label' => $this->label, |
| 729 |
'position' => (int) $this->position, |
| 730 |
'terms' => $terms, |
| 731 |
); |
| 732 |
} |
| 733 |
|
| 734 |
/** |
| 735 |
* Returns pieces for for terms_clauses(), enabled for multiple groups |
| 736 |
* |
| 737 |
* @param int $group_id |
| 738 |
* @return array |
| 739 |
*/ |
| 740 |
public function terms_clauses() |
| 741 |
{ |
| 742 |
global $tag_group_groups ; |
| 743 |
|
| 744 |
if (0 == $this->group_id) { |
| 745 |
/** |
| 746 |
* We are searching for unassigned terms. |
| 747 |
*/ |
| 748 |
$meta_query_args = array( |
| 749 |
'relation' => 'OR', |
| 750 |
array( |
| 751 |
'key' => '_cm_term_group_array', |
| 752 |
'value' => ',0,', |
| 753 |
'compare' => '=', |
| 754 |
), |
| 755 |
array( |
| 756 |
'key' => '_cm_term_group_array', |
| 757 |
'compare' => 'NOT EXISTS', |
| 758 |
), |
| 759 |
); |
| 760 |
} else { |
| 761 |
/** |
| 762 |
* We are searching for terms assigned to a group with $this->group_id. |
| 763 |
*/ |
| 764 |
$group_ids = array( $this->group_id ); |
| 765 |
|
| 766 |
if (count($group_ids) > 1) { |
| 767 |
$meta_query_args = array( |
| 768 |
'relation' => 'OR', |
| 769 |
); |
| 770 |
foreach ($group_ids as $group_id) { |
| 771 |
$meta_query_args[] = array( |
| 772 |
'key' => '_cm_term_group_array', |
| 773 |
'value' => ',' . $group_id . ',', |
| 774 |
'compare' => 'LIKE', |
| 775 |
); |
| 776 |
} |
| 777 |
} else { |
| 778 |
$meta_query_args = array( |
| 779 |
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found |
| 780 |
// 'relation' => 'OR', |
| 781 |
array( |
| 782 |
'key' => '_cm_term_group_array', |
| 783 |
'value' => ',' . $group_ids[0] . ',', |
| 784 |
'compare' => 'LIKE', |
| 785 |
), |
| 786 |
); |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
/** |
| 791 |
* Convert the arguments to SQL pieces |
| 792 |
*/ |
| 793 |
$meta_query = new WP_Meta_Query($meta_query_args); |
| 794 |
return $meta_query->get_sql('term', 't', 'term_id'); |
| 795 |
} |
| 796 |
|
| 797 |
/** |
| 798 |
* removes terms that have post count == 0 |
| 799 |
* |
| 800 |
* @param array $terms |
| 801 |
* @param array|string $taxonomy |
| 802 |
* @param string $fields |
| 803 |
* @return array |
| 804 |
*/ |
| 805 |
public function remove_empty_terms($terms, $taxonomy, $fields) |
| 806 |
{ |
| 807 |
$fields = strtolower($fields); |
| 808 |
foreach ($terms as $key => $term) { |
| 809 |
if (is_array($taxonomy)) { |
| 810 |
$taxonomy = TagGroups_Utilities::get_first_element($taxonomy); |
| 811 |
} |
| 812 |
switch ($fields) { |
| 813 |
case 'all': |
| 814 |
$wp_term = $term; |
| 815 |
break; |
| 816 |
case 'ids': |
| 817 |
$wp_term = get_term_by('id', $term, $taxonomy); |
| 818 |
break; |
| 819 |
case 'slugs': |
| 820 |
$wp_term = get_term_by('slug', $term, $taxonomy); |
| 821 |
break; |
| 822 |
case 'names': |
| 823 |
$wp_term = get_term_by('name', $term, $taxonomy); |
| 824 |
break; |
| 825 |
default: |
| 826 |
TagGroups_Error::log('[Tag Groups] Wrong parameter in remove_empty_terms'); |
| 827 |
return $terms; |
| 828 |
break; |
| 829 |
} |
| 830 |
|
| 831 |
if (false === $wp_term || is_wp_error($wp_term)) { |
| 832 |
TagGroups_Error::log('[Tag Groups] Cannot remove empty terms in remove_empty_terms'); |
| 833 |
return $terms; |
| 834 |
} |
| 835 |
|
| 836 |
$tg_term = new TagGroups_Term($wp_term); |
| 837 |
if (!$tg_term->get_post_count($this->group_id)) { |
| 838 |
unset($terms[$key]); |
| 839 |
} |
| 840 |
} |
| 841 |
return $terms; |
| 842 |
} |
| 843 |
|
| 844 |
/** |
| 845 |
* Returns the ID of the parent group or 0 if no parent |
| 846 |
* |
| 847 |
* @return int |
| 848 |
*/ |
| 849 |
public function get_parent() |
| 850 |
{ |
| 851 |
|
| 852 |
if ($this->is_parent) { |
| 853 |
return 0; |
| 854 |
} |
| 855 |
/** |
| 856 |
* This group's parent is the closest parent that is above |
| 857 |
*/ |
| 858 |
if ($this->position < 2) { |
| 859 |
return 0; |
| 860 |
} |
| 861 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 862 |
$positions = $this->tg_groups->get_positions(); |
| 863 |
$positions_flipped = array_flip($positions); |
| 864 |
$parents = $this->tg_groups->get_parents(); |
| 865 |
|
| 866 |
for ($i = $this->position; $i > 0; $i--) { |
| 867 |
if (isset($positions_flipped[$i]) && in_array($positions_flipped[$i], $parents)) { |
| 868 |
return $positions_flipped[$i]; |
| 869 |
} |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
return 0; |
| 874 |
} |
| 875 |
|
| 876 |
/** |
| 877 |
* Returns the label of the parent, if it exists, or a default string |
| 878 |
* |
| 879 |
* @return string |
| 880 |
*/ |
| 881 |
public function get_parent_label() |
| 882 |
{ |
| 883 |
|
| 884 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 885 |
$parent_id = $this->get_parent(); |
| 886 |
|
| 887 |
if (0 == $parent_id) { |
| 888 |
return '- none -'; |
| 889 |
} |
| 890 |
|
| 891 |
/* |
| 892 |
* Use the labels array because group IDs may already be filtered. |
| 893 |
*/ |
| 894 |
$labels = $this->tg_groups->get_labels(); |
| 895 |
|
| 896 |
if (isset($labels[$parent_id])) { |
| 897 |
return $labels[$parent_id]; |
| 898 |
} |
| 899 |
|
| 900 |
return '- none -'; |
| 901 |
} |
| 902 |
|
| 903 |
return ''; |
| 904 |
} |
| 905 |
} |
| 906 |
} |
| 907 |
|