| 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 |
| 16 |
if (!class_exists('TagGroups_Groups')) { |
| 17 |
class TagGroups_Groups |
| 18 |
{ |
| 19 |
/** |
| 20 |
* array of all term_group values, including 0 |
| 21 |
* |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
private $group_ids ; |
| 25 |
/** |
| 26 |
* array of positions[term_group] |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
private $positions ; |
| 31 |
/** |
| 32 |
* array of labels[term_group] |
| 33 |
* |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
private $labels ; |
| 37 |
/** |
| 38 |
* array of IDs that are parents |
| 39 |
* |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
private $parents ; |
| 43 |
/** |
| 44 |
* whether we loaded the data |
| 45 |
* |
| 46 |
* @var boolean |
| 47 |
*/ |
| 48 |
private $loaded ; |
| 49 |
/** |
| 50 |
* last error |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
public $error ; |
| 55 |
public const PERMISSION = 'permission denied' ; |
| 56 |
/** |
| 57 |
* Constructor |
| 58 |
* |
| 59 |
* |
| 60 |
* @param int $term_group optional term_group |
| 61 |
* @return return type |
| 62 |
*/ |
| 63 |
public function __construct() |
| 64 |
{ |
| 65 |
$this->loaded = false; |
| 66 |
$this->parents = array(); |
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Load data from database |
| 72 |
* |
| 73 |
* @param int $term_group optional term_group |
| 74 |
* @return return type |
| 75 |
*/ |
| 76 |
public function load() |
| 77 |
{ |
| 78 |
|
| 79 |
/** |
| 80 |
* set flag early to avoid infinite loops |
| 81 |
*/ |
| 82 |
$this->loaded = true; |
| 83 |
/* |
| 84 |
* For historical reasons, term_groups and labels have been defined dependent of the position. |
| 85 |
* In future the way how it is saved in the database should be dependent on term_group. |
| 86 |
*/ |
| 87 |
$this->group_ids = TagGroups_Options::get_option('term_groups', array()); |
| 88 |
|
| 89 |
if (empty($this->group_ids)) { |
| 90 |
$this->load_old_format(); |
| 91 |
} else { |
| 92 |
$this->positions = TagGroups_Options::get_option('term_group_positions', array()); |
| 93 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 94 |
$this->parents = array_values(array_intersect( |
| 95 |
TagGroups_Options::get_option('term_group_parents', array()), |
| 96 |
$this->group_ids |
| 97 |
)); |
| 98 |
} |
| 99 |
|
| 100 |
$this->labels = TagGroups_Options::get_option($this->get_tag_group_label_option_name(), array()); |
| 101 |
|
| 102 |
if (empty($this->labels)) { |
| 103 |
/** |
| 104 |
* This language has not yet been saved. We return the default language. |
| 105 |
*/ |
| 106 |
$this->labels = TagGroups_Options::get_option('term_group_labels', array()); |
| 107 |
} elseif ($this->is_wpml_translated_language()) { |
| 108 |
/** |
| 109 |
* Check for untranslated names |
| 110 |
*/ |
| 111 |
$default_language_labels = TagGroups_Options::get_option('term_group_labels', array()); |
| 112 |
foreach ($default_language_labels as $group_id => $default_language_label) { |
| 113 |
if (!isset($this->labels[$group_id])) { |
| 114 |
$this->labels[$group_id] = $default_language_label; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* sanity checks |
| 121 |
*/ |
| 122 |
/** |
| 123 |
* There should not be more elements for positions than IDs |
| 124 |
*/ |
| 125 |
if (count($this->group_ids) != count($this->positions)) { |
| 126 |
$this->recreate_ids_from_positions(); |
| 127 |
} |
| 128 |
/** |
| 129 |
* There should not be more elements for label that IDs |
| 130 |
*/ |
| 131 |
if (count($this->group_ids) < count($this->labels)) { |
| 132 |
$this->fix_labels(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Filters the group IDs after loading from the database |
| 138 |
* |
| 139 |
* @param int[] $this->group_ids |
| 140 |
* @return int[] |
| 141 |
*/ |
| 142 |
$this->group_ids = apply_filters('tag_groups_load_group_ids', $this->group_ids); |
| 143 |
/** |
| 144 |
* Filters the group labels after loading from the database |
| 145 |
* |
| 146 |
* @param string[] $this->labels keys are group IDs, values are the labels (names) |
| 147 |
* @return string[] |
| 148 |
*/ |
| 149 |
$this->labels = apply_filters('tag_groups_load_group_labels', $this->labels); |
| 150 |
/** |
| 151 |
* Filters the group positions after loading from the database |
| 152 |
* |
| 153 |
* @param int[] $this->positions keys are group IDs, values are the positions (determining the order) |
| 154 |
* @return int[] |
| 155 |
*/ |
| 156 |
$this->positions = apply_filters('tag_groups_load_group_positions', $this->positions); |
| 157 |
return $this; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Loads only on-demand |
| 162 |
* |
| 163 |
* @return object $this |
| 164 |
*/ |
| 165 |
public function conditionally_load() |
| 166 |
{ |
| 167 |
|
| 168 |
if (!$this->loaded) { |
| 169 |
$this->load(); |
| 170 |
|
| 171 |
if (count($this->group_ids) == 0) { |
| 172 |
$this->add_not_assigned(); |
| 173 |
$this->save(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return $this; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* checks and, if needed, initialize values for first use |
| 182 |
* |
| 183 |
* @param void |
| 184 |
* @return object $this |
| 185 |
*/ |
| 186 |
public function add_not_assigned() |
| 187 |
{ |
| 188 |
array_unshift($this->group_ids, 0); |
| 189 |
array_unshift($this->labels, __('not assigned', 'tag-groups')); |
| 190 |
array_unshift($this->positions, 0); |
| 191 |
return $this; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Saves tag group-relevant information to the database |
| 196 |
* |
| 197 |
* @param type var Description |
| 198 |
* @return return type |
| 199 |
*/ |
| 200 |
public function save() |
| 201 |
{ |
| 202 |
|
| 203 |
$tag_group_role_edit_groups = 'edit_pages'; |
| 204 |
|
| 205 |
if (!current_user_can($tag_group_role_edit_groups)) { |
| 206 |
$this->error = TagGroups_Groups::PERMISSION; |
| 207 |
return $this; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Filters the group IDs before saving to the database |
| 212 |
* |
| 213 |
* @param int[] $this->group_ids |
| 214 |
* @return int[] |
| 215 |
*/ |
| 216 |
$group_ids = apply_filters('tag_groups_save_group_ids', $this->group_ids); |
| 217 |
/** |
| 218 |
* Filters the group labels before saving to the database |
| 219 |
* |
| 220 |
* @param string[] $this->labels keys are group IDs, values are the labels (names) |
| 221 |
* @return string[] |
| 222 |
*/ |
| 223 |
$labels = apply_filters('tag_groups_save_group_labels', $this->labels); |
| 224 |
/** |
| 225 |
* Filters the group positions before saving to the database |
| 226 |
* |
| 227 |
* @param int[] $this->positions keys are group IDs, values are the positions (determining the order) |
| 228 |
* @return int[] |
| 229 |
*/ |
| 230 |
$positions = apply_filters('tag_groups_save_group_positions', $this->positions); |
| 231 |
TagGroups_Options::update_option('term_groups', $group_ids, true); |
| 232 |
TagGroups_Options::update_option('term_group_positions', $positions, true); |
| 233 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 234 |
$parents = apply_filters('tag_groups_save_group_parents', $this->parents); |
| 235 |
TagGroups_Options::update_option('term_group_parents', $parents, true); |
| 236 |
} |
| 237 |
|
| 238 |
TagGroups_Options::update_option($this->get_tag_group_label_option_name(), $labels, true); |
| 239 |
/** |
| 240 |
* If we save translated groups, make sure we have untranslated ones. If not, give them the translations. |
| 241 |
*/ |
| 242 |
|
| 243 |
if ($this->is_wpml_translated_language()) { |
| 244 |
$default_language_labels = TagGroups_Options::get_option('term_group_labels', array()); |
| 245 |
$changed = false; |
| 246 |
foreach ($this->labels as $group_id => $group_label) { |
| 247 |
if (!isset($default_language_labels[$group_id])) { |
| 248 |
$default_language_labels[$group_id] = $group_label; |
| 249 |
$changed = true; |
| 250 |
} |
| 251 |
} |
| 252 |
if ($changed) { |
| 253 |
TagGroups_Options::update_option('term_group_labels', $default_language_labels); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
do_action('term_group_saved'); |
| 258 |
return $this; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* returns the highest term_group in use |
| 263 |
* |
| 264 |
* @param void |
| 265 |
* @return int |
| 266 |
*/ |
| 267 |
public function get_max_term_group() |
| 268 |
{ |
| 269 |
$this->conditionally_load(); |
| 270 |
|
| 271 |
if (count($this->group_ids) == 0) { |
| 272 |
return 0; |
| 273 |
} else { |
| 274 |
return max($this->group_ids); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* returns the highest position in use |
| 280 |
* |
| 281 |
* @param void |
| 282 |
* @return int |
| 283 |
*/ |
| 284 |
public function get_max_position() |
| 285 |
{ |
| 286 |
$this->conditionally_load(); |
| 287 |
|
| 288 |
if (count($this->positions) == 0) { |
| 289 |
return 0; |
| 290 |
} else { |
| 291 |
return max($this->positions); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* returns the number of term groups_only |
| 297 |
* |
| 298 |
* @param void |
| 299 |
* @return int |
| 300 |
*/ |
| 301 |
public function get_number_of_term_groups() |
| 302 |
{ |
| 303 |
$this->conditionally_load(); |
| 304 |
$count = count($this->group_ids); |
| 305 |
if (isset($this->group_ids[0])) { |
| 306 |
$count--; |
| 307 |
} |
| 308 |
return $count; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* filter the list of tag groups by a substring |
| 313 |
* |
| 314 |
* @param string $substring |
| 315 |
* @return object $this |
| 316 |
*/ |
| 317 |
public function filter_by_substring($substring) |
| 318 |
{ |
| 319 |
if (empty($substring)) { |
| 320 |
return $this; |
| 321 |
} |
| 322 |
$this->conditionally_load(); |
| 323 |
foreach ($this->group_ids as $key => $group_id) { |
| 324 |
if (0 == $group_id) { |
| 325 |
continue; |
| 326 |
} |
| 327 |
if (strpos(strtoupper($this->labels[$group_id]), strtoupper($substring)) === false) { |
| 328 |
/** |
| 329 |
* Don't unset labels or positions since we might need them with parent IDs |
| 330 |
*/ |
| 331 |
unset($this->group_ids[$key]); |
| 332 |
} |
| 333 |
} |
| 334 |
$this->group_ids = array_values($this->group_ids); |
| 335 |
return $this; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* adds a new group |
| 340 |
* |
| 341 |
* @param object tag group |
| 342 |
* @return object $this |
| 343 |
*/ |
| 344 |
public function add_group($tg_group) |
| 345 |
{ |
| 346 |
$this->conditionally_load(); |
| 347 |
if (!is_numeric($tg_group->get_group_id()) || $tg_group->get_group_id() < 1 || in_array($tg_group->get_group_id(), $this->group_ids)) { |
| 348 |
return $this; |
| 349 |
} |
| 350 |
array_push($this->group_ids, $tg_group->get_group_id()); |
| 351 |
$this->labels[$tg_group->get_group_id()] = $tg_group->get_label(); |
| 352 |
$this->positions[$tg_group->get_group_id()] = $tg_group->get_position(); |
| 353 |
return $this; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* removes all terms from all groups |
| 358 |
* |
| 359 |
* @param void |
| 360 |
* @return object $this |
| 361 |
*/ |
| 362 |
public function unassign_all_terms() |
| 363 |
{ |
| 364 |
$this->conditionally_load(); |
| 365 |
$enabled_taxonomies = TagGroups_Taxonomy::get_enabled_taxonomies(); |
| 366 |
$terms = get_terms(array( |
| 367 |
'hide_empty' => false, |
| 368 |
'taxonomy' => $enabled_taxonomies, |
| 369 |
)); |
| 370 |
foreach ($terms as $term) { |
| 371 |
$term_o = new TagGroups_Term($term); |
| 372 |
$term_o->remove_all_groups()->save(); |
| 373 |
} |
| 374 |
return $this; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* getter for $group_ids |
| 379 |
* |
| 380 |
* @param void |
| 381 |
* @return array |
| 382 |
*/ |
| 383 |
public function get_group_ids() |
| 384 |
{ |
| 385 |
$this->conditionally_load(); |
| 386 |
return $this->group_ids; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* setter for $group_ids |
| 391 |
* |
| 392 |
* @param array $group_ids |
| 393 |
* @return object $this |
| 394 |
*/ |
| 395 |
public function set_group_ids($group_ids) |
| 396 |
{ |
| 397 |
$this->conditionally_load(); |
| 398 |
$this->group_ids = $group_ids; |
| 399 |
return $this; |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* returns the labels for an array of ids, sorted by position |
| 404 |
* |
| 405 |
* @param array $group_ids If we don't supply this parameter, we use all groups |
| 406 |
* @return array |
| 407 |
*/ |
| 408 |
public function get_labels_by_position($group_ids = array()) |
| 409 |
{ |
| 410 |
$this->conditionally_load(); |
| 411 |
$result = array(); |
| 412 |
if (empty($group_ids)) { |
| 413 |
$group_ids = $this->group_ids; |
| 414 |
} |
| 415 |
foreach ($group_ids as $group_id) { |
| 416 |
if (!empty($this->labels[$group_id]) && isset($this->positions[$group_id])) { |
| 417 |
$result[$this->positions[$group_id]] = $this->labels[$group_id]; |
| 418 |
} |
| 419 |
} |
| 420 |
ksort($result); |
| 421 |
return array_values($result); |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* returns an array of group properties as values |
| 426 |
* |
| 427 |
* @param void |
| 428 |
* @return array |
| 429 |
*/ |
| 430 |
public function get_info_of_all( |
| 431 |
$taxonomy = null, |
| 432 |
$hide_empty = false, |
| 433 |
$fields = null, |
| 434 |
$orderby = 'name', |
| 435 |
$order = 'ASC' |
| 436 |
) { |
| 437 |
$this->conditionally_load(); |
| 438 |
/** |
| 439 |
* dealing with NULL values |
| 440 |
*/ |
| 441 |
if (empty($fields)) { |
| 442 |
$fields = 'ids'; |
| 443 |
} |
| 444 |
if (empty($taxonomy)) { |
| 445 |
$taxonomy = TagGroups_Taxonomy::get_enabled_taxonomies(); |
| 446 |
} |
| 447 |
if (!isset($hide_empty) || empty($hide_empty)) { |
| 448 |
$hide_empty = false; |
| 449 |
} |
| 450 |
$result = array(); |
| 451 |
foreach ($this->group_ids as $term_group) { |
| 452 |
if (isset($this->positions[$term_group]) && isset($this->labels[$term_group])) { |
| 453 |
// allow unassigned |
| 454 |
$tg_group = new TagGroups_Group($term_group); |
| 455 |
$terms = $tg_group->get_group_terms( |
| 456 |
$taxonomy, |
| 457 |
$hide_empty, |
| 458 |
$fields, |
| 459 |
0, |
| 460 |
$orderby, |
| 461 |
$order |
| 462 |
); |
| 463 |
|
| 464 |
if (!is_array($terms)) { |
| 465 |
$terms = array(); |
| 466 |
TagGroups_Error::log('[Tag Groups] Error retrieving terms in get_info().'); |
| 467 |
} |
| 468 |
|
| 469 |
$result[$this->positions[$term_group]] = array( |
| 470 |
'term_group' => (int) $term_group, |
| 471 |
'label' => $this->labels[$term_group], |
| 472 |
'position' => (int) $this->positions[$term_group], |
| 473 |
'terms' => $terms, |
| 474 |
'is_parent' => $tg_group->is_parent, |
| 475 |
); |
| 476 |
if ($tg_group->is_parent) { |
| 477 |
$result[$this->positions[$term_group]]['children'] = $this->get_children($term_group); |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
/** |
| 482 |
* The position should determine the order. |
| 483 |
*/ |
| 484 |
ksort($result); |
| 485 |
return $result; |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* returns all tag groups with the position as keys and an array of group properties as values |
| 490 |
* including unassigned |
| 491 |
* |
| 492 |
* @param void |
| 493 |
* @return array |
| 494 |
*/ |
| 495 |
public function get_all_with_position_as_key($include_parents = false) |
| 496 |
{ |
| 497 |
|
| 498 |
$this->conditionally_load(); |
| 499 |
$result = array(); |
| 500 |
foreach ($this->group_ids as $group_id) { |
| 501 |
if (isset($this->positions[$group_id]) && isset($this->labels[$group_id])) { |
| 502 |
if (TagGroups_Utilities::is_premium_plan() && !$include_parents && in_array($group_id, $this->parents)) { |
| 503 |
continue; |
| 504 |
} |
| 505 |
|
| 506 |
$result[$this->positions[$group_id]] = array( |
| 507 |
'term_group' => (int) $group_id, |
| 508 |
'label' => $this->labels[$group_id], |
| 509 |
'position' => (int) $this->positions[$group_id], |
| 510 |
); |
| 511 |
} |
| 512 |
} |
| 513 |
/** |
| 514 |
* The position should determine the order. |
| 515 |
* Don't use ksort() because it reindexes the |
| 516 |
*/ |
| 517 |
uksort($result, function ($a, $b) { |
| 518 |
if ($a > $b) { |
| 519 |
return 1; |
| 520 |
} |
| 521 |
if ($a < $b) { |
| 522 |
return -1; |
| 523 |
} |
| 524 |
return 0; |
| 525 |
}); |
| 526 |
return $result; |
| 527 |
} |
| 528 |
|
| 529 |
/** |
| 530 |
* returns all tag groups with the term_group as keys and labels as values |
| 531 |
* sorted by position |
| 532 |
* |
| 533 |
* @param void |
| 534 |
* @return array |
| 535 |
*/ |
| 536 |
public function get_all_term_group_label() |
| 537 |
{ |
| 538 |
$this->conditionally_load(); |
| 539 |
$result = array(); |
| 540 |
$positions_flipped = array_flip($this->positions); |
| 541 |
ksort($positions_flipped); |
| 542 |
foreach ($positions_flipped as $term_group) { |
| 543 |
$result[$term_group] = $this->labels[$term_group]; |
| 544 |
} |
| 545 |
return $result; |
| 546 |
} |
| 547 |
|
| 548 |
/** |
| 549 |
* returns all tag group ids (including ID 0) |
| 550 |
* sorted by position |
| 551 |
* |
| 552 |
* @param void |
| 553 |
* @return array |
| 554 |
*/ |
| 555 |
public function get_group_ids_by_position($include_parents = false) |
| 556 |
{ |
| 557 |
$this->conditionally_load(); |
| 558 |
$result = array(); |
| 559 |
$position_flipped = array_flip($this->positions); |
| 560 |
ksort($position_flipped); |
| 561 |
foreach ($position_flipped as $group_id) { |
| 562 |
if (!$include_parents && in_array($group_id, $this->parents)) { |
| 563 |
continue; |
| 564 |
} |
| 565 |
$result[] = $group_id; |
| 566 |
} |
| 567 |
return $result; |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* returns all labels |
| 572 |
* sorted by position |
| 573 |
* |
| 574 |
* @param void |
| 575 |
* @return array |
| 576 |
*/ |
| 577 |
public function get_all_labels_by_position() |
| 578 |
{ |
| 579 |
$this->conditionally_load(); |
| 580 |
$result = array(); |
| 581 |
$positions = $this->positions; |
| 582 |
asort($positions); |
| 583 |
$positions_keys = array_keys($positions); |
| 584 |
foreach ($positions_keys as $term_group) { |
| 585 |
$result[] = $this->labels[$term_group]; |
| 586 |
} |
| 587 |
return $result; |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* getter for $labels |
| 592 |
* |
| 593 |
* @param void |
| 594 |
* @return string[] |
| 595 |
*/ |
| 596 |
public function get_labels() |
| 597 |
{ |
| 598 |
$this->conditionally_load(); |
| 599 |
return $this->labels; |
| 600 |
} |
| 601 |
|
| 602 |
/** |
| 603 |
* setter for $labels |
| 604 |
* |
| 605 |
* @param array $labels |
| 606 |
* @return object $this |
| 607 |
*/ |
| 608 |
public function set_labels($labels) |
| 609 |
{ |
| 610 |
$this->conditionally_load(); |
| 611 |
$this->labels = $labels; |
| 612 |
return $this; |
| 613 |
} |
| 614 |
|
| 615 |
/** |
| 616 |
* getter for $positions |
| 617 |
* |
| 618 |
* @param void |
| 619 |
* @return int[] |
| 620 |
*/ |
| 621 |
public function get_positions() |
| 622 |
{ |
| 623 |
$this->conditionally_load(); |
| 624 |
return $this->positions; |
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
* setter for $positions |
| 629 |
* |
| 630 |
* @param array $positions |
| 631 |
* @return object $this |
| 632 |
*/ |
| 633 |
public function set_positions($positions) |
| 634 |
{ |
| 635 |
$this->conditionally_load(); |
| 636 |
$this->positions = $positions; |
| 637 |
return $this; |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* getter for $parents |
| 642 |
* |
| 643 |
* @param void |
| 644 |
* @return int[] |
| 645 |
*/ |
| 646 |
public function get_parents() |
| 647 |
{ |
| 648 |
|
| 649 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 650 |
$this->conditionally_load(); |
| 651 |
return $this->parents; |
| 652 |
} |
| 653 |
|
| 654 |
return array(); |
| 655 |
} |
| 656 |
|
| 657 |
/** |
| 658 |
* setter for $parents |
| 659 |
* |
| 660 |
* @param array $parents |
| 661 |
* @return object $this |
| 662 |
*/ |
| 663 |
public function set_parents($parents) |
| 664 |
{ |
| 665 |
$this->conditionally_load(); |
| 666 |
$this->parents = $parents; |
| 667 |
return $this; |
| 668 |
} |
| 669 |
|
| 670 |
/** |
| 671 |
* Returns all IDs of groups that are children of a given parent group |
| 672 |
* |
| 673 |
* @param int $parent |
| 674 |
* @return array |
| 675 |
*/ |
| 676 |
public function get_children($parent) |
| 677 |
{ |
| 678 |
|
| 679 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 680 |
$this->conditionally_load(); |
| 681 |
if ( |
| 682 |
!in_array($parent, $this->group_ids) |
| 683 |
|| !in_array($parent, $this->parents) |
| 684 |
|| !isset($this->positions[$parent]) |
| 685 |
) { |
| 686 |
return array(); |
| 687 |
} |
| 688 |
|
| 689 |
$parent_position = $this->positions[$parent]; |
| 690 |
$positions_flipped = array_flip($this->positions); |
| 691 |
$children = array(); |
| 692 |
|
| 693 |
for ($i = $parent_position + 1; $i < count($positions_flipped); $i++) { |
| 694 |
if (in_array($positions_flipped[$i], $this->parents)) { |
| 695 |
break; |
| 696 |
} |
| 697 |
|
| 698 |
$children[] = $positions_flipped[$i]; |
| 699 |
} |
| 700 |
|
| 701 |
return $children; |
| 702 |
} |
| 703 |
|
| 704 |
/** |
| 705 |
* otherwise simply remove the parent IDs |
| 706 |
*/ |
| 707 |
return array_diff($this->group_ids, $this->parents); |
| 708 |
} |
| 709 |
|
| 710 |
/** |
| 711 |
* Deletes all groups |
| 712 |
* |
| 713 |
* @param void |
| 714 |
* @return void |
| 715 |
*/ |
| 716 |
public function reset_groups() |
| 717 |
{ |
| 718 |
$this->conditionally_load(); |
| 719 |
|
| 720 |
$tag_group_role_edit_groups = 'edit_pages'; |
| 721 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 722 |
$tag_group_role_edit_groups = TagGroups_Options::get_option('tag_group_role_edit_groups', 'edit_pages'); |
| 723 |
} |
| 724 |
if (!current_user_can($tag_group_role_edit_groups)) { |
| 725 |
return false; |
| 726 |
} |
| 727 |
$this->group_ids = array(); |
| 728 |
$this->positions = array(); |
| 729 |
$this->labels = array(); |
| 730 |
$this->delete_labels_languages(); |
| 731 |
$this->unassign_all_terms(); |
| 732 |
$this->add_not_assigned(); |
| 733 |
$this->save(); |
| 734 |
TagGroups_Cron::schedule_in_secs(5, 'tag_groups_clear_tag_groups_group_terms'); |
| 735 |
TagGroups_Transients::delete_all_transients_and_log(); |
| 736 |
return true; |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
* Deletes all labels for all languages |
| 741 |
* |
| 742 |
* @param void |
| 743 |
* @return void |
| 744 |
*/ |
| 745 |
public function delete_labels_languages() |
| 746 |
{ |
| 747 |
delete_option('term_group_labels'); |
| 748 |
$tag_group_group_languages = TagGroups_Options::get_option('tag_group_group_languages', array()); |
| 749 |
if (isset($tag_group_group_languages)) { |
| 750 |
foreach ($tag_group_group_languages as $language) { |
| 751 |
delete_option('term_group_labels_' . $language); |
| 752 |
} |
| 753 |
} |
| 754 |
delete_option('tag_group_group_languages'); |
| 755 |
} |
| 756 |
|
| 757 |
/** |
| 758 |
* Remove "holes" in position array |
| 759 |
* |
| 760 |
* This method assumes that element 0 for unassigned terms is set |
| 761 |
* |
| 762 |
* @param void |
| 763 |
* @return object |
| 764 |
*/ |
| 765 |
public function reindex_positions() |
| 766 |
{ |
| 767 |
$this->conditionally_load(); |
| 768 |
$positions_flipped = array_flip($this->positions); |
| 769 |
// result: position => id |
| 770 |
ksort($positions_flipped); |
| 771 |
// re-index |
| 772 |
$positions_flipped = array_values($positions_flipped); |
| 773 |
$this->positions = array_flip($positions_flipped); |
| 774 |
return $this; |
| 775 |
} |
| 776 |
|
| 777 |
/** |
| 778 |
* Sorts the groups (positions) by alphabetical order, while keeping the parent-child relationship |
| 779 |
* |
| 780 |
* @param void |
| 781 |
* @return void |
| 782 |
*/ |
| 783 |
public function sort($order = 'up') |
| 784 |
{ |
| 785 |
|
| 786 |
$this->conditionally_load(); |
| 787 |
|
| 788 |
if (empty($this->parents)) { |
| 789 |
$group_ids = $this->group_ids; |
| 790 |
// remove unassigned |
| 791 |
unset($group_ids[0]); |
| 792 |
usort($group_ids, array( $this, 'sort_by_label' )); |
| 793 |
if ('down' == $order) { |
| 794 |
$group_ids = array_reverse($group_ids); |
| 795 |
} |
| 796 |
// add back unassigned |
| 797 |
array_unshift($group_ids, 0); |
| 798 |
$this->positions = array_flip($group_ids); |
| 799 |
} else { |
| 800 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 801 |
/** |
| 802 |
* group by parents |
| 803 |
*/ |
| 804 |
$children_by_parents = $this->create_array_children_by_parents(); |
| 805 |
$sorted_ids = array(); |
| 806 |
foreach ($children_by_parents as $parent_id => $bunch) { |
| 807 |
if (!in_array($parent_id, $sorted_ids) && 0 != $parent_id) { |
| 808 |
$sorted_ids[] = $parent_id; |
| 809 |
} |
| 810 |
if (0 == $parent_id) { |
| 811 |
// remove unassigned |
| 812 |
unset($bunch[0]); |
| 813 |
} |
| 814 |
usort($bunch, array( $this, 'sort_by_label' )); |
| 815 |
if ('down' == $order) { |
| 816 |
$bunch = array_reverse($bunch); |
| 817 |
} |
| 818 |
if (0 == $parent_id) { |
| 819 |
// add back unassigned |
| 820 |
array_unshift($bunch, 0); |
| 821 |
} |
| 822 |
$sorted_ids = array_merge($sorted_ids, $bunch); |
| 823 |
} |
| 824 |
$this->positions = array_flip($sorted_ids); |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
return $this; |
| 829 |
} |
| 830 |
|
| 831 |
/** |
| 832 |
* return a 2-dimensional-array with children groups sorted as arrays under their parents as keys |
| 833 |
* |
| 834 |
* @return array |
| 835 |
*/ |
| 836 |
public function create_array_children_by_parents() |
| 837 |
{ |
| 838 |
|
| 839 |
$this->conditionally_load(); |
| 840 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 841 |
$groups_of_children = array(); |
| 842 |
$current_parent = 0; |
| 843 |
$groups_by_position = array_flip($this->positions); |
| 844 |
|
| 845 |
foreach ($groups_by_position as $group_id) { |
| 846 |
if (in_array($group_id, $this->parents)) { |
| 847 |
$current_parent = $group_id; |
| 848 |
$groups_of_children[$group_id] = array(); |
| 849 |
continue; |
| 850 |
} |
| 851 |
|
| 852 |
$groups_of_children[$current_parent][] = $group_id; |
| 853 |
} |
| 854 |
|
| 855 |
return $groups_of_children; |
| 856 |
} |
| 857 |
|
| 858 |
return array( |
| 859 |
0 => $this->group_ids, |
| 860 |
); |
| 861 |
} |
| 862 |
|
| 863 |
/** |
| 864 |
* Sorts by group label |
| 865 |
* |
| 866 |
* @param int $a |
| 867 |
* @param int $b |
| 868 |
* @return boolean |
| 869 |
*/ |
| 870 |
public function sort_by_label($a, $b) |
| 871 |
{ |
| 872 |
$labels = TagGroups_Options::get_option($this->get_tag_group_label_option_name(), array()); |
| 873 |
return strnatcmp($labels[$a], $labels[$b]); |
| 874 |
} |
| 875 |
|
| 876 |
/** |
| 877 |
* Check for WPML and use the correct option name |
| 878 |
* |
| 879 |
* @param void |
| 880 |
* @return string |
| 881 |
*/ |
| 882 |
public function get_tag_group_label_option_name() |
| 883 |
{ |
| 884 |
if (!TagGroups_WPML::get_current_language() || !$this->is_wpml_translated_language()) { |
| 885 |
return 'term_group_labels'; |
| 886 |
} |
| 887 |
|
| 888 |
if ('all' == TagGroups_WPML::get_current_language()) { |
| 889 |
$language = (string) apply_filters('wpml_default_language', null); |
| 890 |
} else { |
| 891 |
$language = (string) TagGroups_WPML::get_current_language(); |
| 892 |
} |
| 893 |
|
| 894 |
/** |
| 895 |
* Make sure we can delete this option during uninstallation |
| 896 |
*/ |
| 897 |
$tag_group_group_languages = TagGroups_Options::get_option('tag_group_group_languages', array()); |
| 898 |
if (!is_array($tag_group_group_languages)) { |
| 899 |
// preventing value being a string, see ticket #1707, maybe an isolated case |
| 900 |
$tag_group_group_languages = array(); |
| 901 |
} |
| 902 |
|
| 903 |
if (!in_array($language, $tag_group_group_languages)) { |
| 904 |
$tag_group_group_languages[] = $language; |
| 905 |
TagGroups_Options::update_option('tag_group_group_languages', $tag_group_group_languages); |
| 906 |
} |
| 907 |
|
| 908 |
return 'term_group_labels_' . $language; |
| 909 |
} |
| 910 |
|
| 911 |
/** |
| 912 |
* Returns true if WPML is installed and we are not using the default language. |
| 913 |
* |
| 914 |
* @param void |
| 915 |
* @return boolean |
| 916 |
*/ |
| 917 |
public function is_wpml_translated_language() |
| 918 |
{ |
| 919 |
$current_language = TagGroups_WPML::get_current_language(); |
| 920 |
if (!$current_language) { |
| 921 |
return false; |
| 922 |
} |
| 923 |
$default_language = apply_filters('wpml_default_language', null); |
| 924 |
/** |
| 925 |
* workaround for Polylang |
| 926 |
*/ |
| 927 |
if (empty($default_language) && function_exists('pll_default_language')) { |
| 928 |
$default_language = pll_default_language(); |
| 929 |
} |
| 930 |
if ($default_language === $current_language) { |
| 931 |
return false; |
| 932 |
} |
| 933 |
return true; |
| 934 |
} |
| 935 |
|
| 936 |
/** |
| 937 |
* Gets the current language, considers Polylang |
| 938 |
* |
| 939 |
* @deprecated |
| 940 |
* @param void |
| 941 |
* @return string|boolean |
| 942 |
*/ |
| 943 |
public static function get_current_language() |
| 944 |
{ |
| 945 |
TagGroups_Error::deprecated(); |
| 946 |
return TagGroups_WPML::get_current_language(); |
| 947 |
} |
| 948 |
|
| 949 |
/** |
| 950 |
* Add default groups to a new tag |
| 951 |
* |
| 952 |
* @since 1.26.0 |
| 953 |
* |
| 954 |
* @param int $term_id ID of the tag that has been created |
| 955 |
* @param int $taxonomy_id ID of the taxonomy of this tag |
| 956 |
* @return void |
| 957 |
*/ |
| 958 |
public function assign_default_groups($term_id) |
| 959 |
{ |
| 960 |
} |
| 961 |
|
| 962 |
/** |
| 963 |
* Sort the groups according to the positions |
| 964 |
* |
| 965 |
* @deprecated 1.40.2 |
| 966 |
* @return array |
| 967 |
*/ |
| 968 |
public function get_sorted_groups($groups) |
| 969 |
{ |
| 970 |
$groups_sorted = array(); |
| 971 |
foreach ($this->get_positions() as $group => $position) { |
| 972 |
if (array_search($group, $groups) !== false) { |
| 973 |
$groups_sorted[] = $group; |
| 974 |
} |
| 975 |
} |
| 976 |
return $groups_sorted; |
| 977 |
} |
| 978 |
|
| 979 |
/** |
| 980 |
* Load format from old structure |
| 981 |
* |
| 982 |
* @return void |
| 983 |
*/ |
| 984 |
public function load_old_format() |
| 985 |
{ |
| 986 |
$term_groups_position = TagGroups_Options::get_option('tag_group_ids', array()); |
| 987 |
// position -> id |
| 988 |
$labels_position = TagGroups_Options::get_option('tag_group_labels', array()); |
| 989 |
// position -> label |
| 990 |
$this->positions = array_flip($term_groups_position); |
| 991 |
$this->group_ids = array_keys($this->positions); |
| 992 |
/** |
| 993 |
* sort and use new keys |
| 994 |
*/ |
| 995 |
sort($this->group_ids); |
| 996 |
$this->labels = array(); |
| 997 |
foreach ($term_groups_position as $position => $id) { |
| 998 |
$this->labels[$id] = $labels_position[$position]; |
| 999 |
} |
| 1000 |
ksort($this->positions); |
| 1001 |
ksort($this->labels); |
| 1002 |
} |
| 1003 |
|
| 1004 |
/** |
| 1005 |
* Recreate group IDs from positions |
| 1006 |
* |
| 1007 |
* @return void |
| 1008 |
*/ |
| 1009 |
public function recreate_ids_from_positions() |
| 1010 |
{ |
| 1011 |
$this->reindex_positions(); |
| 1012 |
// recreate $this->group_ids from positions |
| 1013 |
$this->group_ids = array_keys($this->positions); |
| 1014 |
sort($this->group_ids); |
| 1015 |
TagGroups_Options::update_option('term_groups', $this->group_ids); |
| 1016 |
} |
| 1017 |
|
| 1018 |
/** |
| 1019 |
* Recreate the labels, considering translations |
| 1020 |
* |
| 1021 |
* @return void |
| 1022 |
*/ |
| 1023 |
public function fix_labels() |
| 1024 |
{ |
| 1025 |
foreach ($this->labels as $group_id => $label) { |
| 1026 |
if (!in_array($group_id, $this->group_ids)) { |
| 1027 |
unset($this->labels[$group_id]); |
| 1028 |
} |
| 1029 |
} |
| 1030 |
TagGroups_Options::update_option('term_group_labels', $this->labels); |
| 1031 |
$tag_group_group_languages = TagGroups_Options::get_option('tag_group_group_languages', array()); |
| 1032 |
if (!isset($tag_group_group_languages) || !is_array($tag_group_group_languages)) { |
| 1033 |
return; |
| 1034 |
} |
| 1035 |
foreach ($tag_group_group_languages as $language) { |
| 1036 |
$translated_labels = TagGroups_Options::get_option('term_group_labels_' . $language); |
| 1037 |
if (count($this->group_ids) >= count($translated_labels)) { |
| 1038 |
continue; |
| 1039 |
} |
| 1040 |
foreach ($translated_labels as $group_id => $label) { |
| 1041 |
if (!in_array($group_id, $this->group_ids)) { |
| 1042 |
unset($translated_labels[$group_id]); |
| 1043 |
} |
| 1044 |
} |
| 1045 |
TagGroups_Options::update_option('term_group_labels_' . $language, $translated_labels); |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
/** |
| 1050 |
* Replace IDs of parent groups by IDs of their children, keeping IDs in the resulting array unique |
| 1051 |
* |
| 1052 |
* @param int[] $group_ids |
| 1053 |
* @return int[] |
| 1054 |
*/ |
| 1055 |
public function expand_parents($group_ids) |
| 1056 |
{ |
| 1057 |
|
| 1058 |
$this->conditionally_load(); |
| 1059 |
if (empty($this->parents)) { |
| 1060 |
return $group_ids; |
| 1061 |
} |
| 1062 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 1063 |
$expanded_group_ids = array(); |
| 1064 |
|
| 1065 |
foreach ($group_ids as $group_id) { |
| 1066 |
if (in_array($group_id, $expanded_group_ids)) { |
| 1067 |
continue; |
| 1068 |
} |
| 1069 |
|
| 1070 |
if (in_array($group_id, $this->parents)) { |
| 1071 |
$children = $this->get_children($group_id); |
| 1072 |
$expanded_group_ids = array_merge($expanded_group_ids, $children); |
| 1073 |
} else { |
| 1074 |
$expanded_group_ids[] = $group_id; |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
return $expanded_group_ids; |
| 1079 |
} |
| 1080 |
|
| 1081 |
/** |
| 1082 |
* otherwise simply remove the parent IDs |
| 1083 |
*/ |
| 1084 |
return array_diff($group_ids, $this->parents); |
| 1085 |
} |
| 1086 |
|
| 1087 |
/** |
| 1088 |
* Tests whether all available groups are parents |
| 1089 |
* |
| 1090 |
* @return boolean |
| 1091 |
*/ |
| 1092 |
public function is_only_parents() |
| 1093 |
{ |
| 1094 |
|
| 1095 |
$this->conditionally_load(); |
| 1096 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 1097 |
$groups_diff = array_diff($this->group_ids, $this->parents); |
| 1098 |
return empty($groups_diff) || array( 0 ) == $groups_diff; |
| 1099 |
} |
| 1100 |
|
| 1101 |
return false; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|