| Conditions | 96 |
| Paths | 0 |
| Total Lines | 447 |
| Code Lines | 244 |
| Lines | 18 |
| Ratio | 4.03 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 307 | public function get_terms() { |
||
| 308 | $this->parse_query( $this->query_vars ); |
||
| 309 | $args = $this->query_vars; |
||
| 310 | |||
| 311 | // Set up meta_query so it's available to 'pre_get_terms'. |
||
| 312 | $this->meta_query = new WP_Meta_Query(); |
||
| 313 | $this->meta_query->parse_query_vars( $args ); |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Fires before terms are retrieved. |
||
| 317 | * |
||
| 318 | * @since 4.6.0 |
||
| 319 | * |
||
| 320 | * @param WP_Term_Query $this Current instance of WP_Term_Query. |
||
| 321 | */ |
||
| 322 | do_action( 'pre_get_terms', $this ); |
||
| 323 | |||
| 324 | $taxonomies = $args['taxonomy']; |
||
| 325 | |||
| 326 | // Save queries by not crawling the tree in the case of multiple taxes or a flat tax. |
||
| 327 | $has_hierarchical_tax = false; |
||
| 328 | if ( $taxonomies ) { |
||
| 329 | foreach ( $taxonomies as $_tax ) { |
||
| 330 | if ( is_taxonomy_hierarchical( $_tax ) ) { |
||
| 331 | $has_hierarchical_tax = true; |
||
| 332 | } |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | if ( ! $has_hierarchical_tax ) { |
||
| 337 | $args['hierarchical'] = false; |
||
| 338 | $args['pad_counts'] = false; |
||
| 339 | } |
||
| 340 | |||
| 341 | // 'parent' overrides 'child_of'. |
||
| 342 | if ( 0 < intval( $args['parent'] ) ) { |
||
| 343 | $args['child_of'] = false; |
||
| 344 | } |
||
| 345 | |||
| 346 | View Code Duplication | if ( 'all' == $args['get'] ) { |
|
| 347 | $args['childless'] = false; |
||
| 348 | $args['child_of'] = 0; |
||
| 349 | $args['hide_empty'] = 0; |
||
| 350 | $args['hierarchical'] = false; |
||
| 351 | $args['pad_counts'] = false; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Filters the terms query arguments. |
||
| 356 | * |
||
| 357 | * @since 3.1.0 |
||
| 358 | * |
||
| 359 | * @param array $args An array of get_terms() arguments. |
||
| 360 | * @param array $taxonomies An array of taxonomies. |
||
| 361 | */ |
||
| 362 | $args = apply_filters( 'get_terms_args', $args, $taxonomies ); |
||
| 363 | |||
| 364 | // Avoid the query if the queried parent/child_of term has no descendants. |
||
| 365 | $child_of = $args['child_of']; |
||
| 366 | $parent = $args['parent']; |
||
| 367 | |||
| 368 | if ( $child_of ) { |
||
| 369 | $_parent = $child_of; |
||
| 370 | } elseif ( $parent ) { |
||
| 371 | $_parent = $parent; |
||
| 372 | } else { |
||
| 373 | $_parent = false; |
||
| 374 | } |
||
| 375 | |||
| 376 | if ( $_parent ) { |
||
| 377 | $in_hierarchy = false; |
||
| 378 | foreach ( $taxonomies as $_tax ) { |
||
| 379 | $hierarchy = _get_term_hierarchy( $_tax ); |
||
| 380 | |||
| 381 | if ( isset( $hierarchy[ $_parent ] ) ) { |
||
| 382 | $in_hierarchy = true; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | if ( ! $in_hierarchy ) { |
||
| 387 | return array(); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | $orderby = $this->parse_orderby( $this->query_vars['orderby'] ); |
||
| 392 | if ( $orderby ) { |
||
| 393 | $orderby = "ORDER BY $orderby"; |
||
| 394 | } |
||
| 395 | |||
| 396 | $order = $this->parse_order( $this->query_vars['order'] ); |
||
| 397 | |||
| 398 | if ( $taxonomies ) { |
||
| 399 | $this->sql_clauses['where']['taxonomy'] = "tt.taxonomy IN ('" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "')"; |
||
| 400 | } |
||
| 401 | |||
| 402 | $exclude = $args['exclude']; |
||
| 403 | $exclude_tree = $args['exclude_tree']; |
||
| 404 | $include = $args['include']; |
||
| 405 | |||
| 406 | $inclusions = ''; |
||
| 407 | if ( ! empty( $include ) ) { |
||
| 408 | $exclude = ''; |
||
| 409 | $exclude_tree = ''; |
||
| 410 | $inclusions = implode( ',', wp_parse_id_list( $include ) ); |
||
| 411 | } |
||
| 412 | |||
| 413 | if ( ! empty( $inclusions ) ) { |
||
| 414 | $this->sql_clauses['where']['inclusions'] = 't.term_id IN ( ' . $inclusions . ' )'; |
||
| 415 | } |
||
| 416 | |||
| 417 | $exclusions = array(); |
||
| 418 | if ( ! empty( $exclude_tree ) ) { |
||
| 419 | $exclude_tree = wp_parse_id_list( $exclude_tree ); |
||
| 420 | $excluded_children = $exclude_tree; |
||
| 421 | foreach ( $exclude_tree as $extrunk ) { |
||
| 422 | $excluded_children = array_merge( |
||
| 423 | $excluded_children, |
||
| 424 | (array) get_terms( $taxonomies[0], array( |
||
| 425 | 'child_of' => intval( $extrunk ), |
||
| 426 | 'fields' => 'ids', |
||
| 427 | 'hide_empty' => 0 |
||
| 428 | ) ) |
||
| 429 | ); |
||
| 430 | } |
||
| 431 | $exclusions = array_merge( $excluded_children, $exclusions ); |
||
| 432 | } |
||
| 433 | |||
| 434 | if ( ! empty( $exclude ) ) { |
||
| 435 | $exclusions = array_merge( wp_parse_id_list( $exclude ), $exclusions ); |
||
| 436 | } |
||
| 437 | |||
| 438 | // 'childless' terms are those without an entry in the flattened term hierarchy. |
||
| 439 | $childless = (bool) $args['childless']; |
||
| 440 | if ( $childless ) { |
||
| 441 | foreach ( $taxonomies as $_tax ) { |
||
| 442 | $term_hierarchy = _get_term_hierarchy( $_tax ); |
||
| 443 | $exclusions = array_merge( array_keys( $term_hierarchy ), $exclusions ); |
||
| 444 | } |
||
| 445 | } |
||
| 446 | |||
| 447 | if ( ! empty( $exclusions ) ) { |
||
| 448 | $exclusions = 't.term_id NOT IN (' . implode( ',', array_map( 'intval', $exclusions ) ) . ')'; |
||
| 449 | } else { |
||
| 450 | $exclusions = ''; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Filters the terms to exclude from the terms query. |
||
| 455 | * |
||
| 456 | * @since 2.3.0 |
||
| 457 | * |
||
| 458 | * @param string $exclusions `NOT IN` clause of the terms query. |
||
| 459 | * @param array $args An array of terms query arguments. |
||
| 460 | * @param array $taxonomies An array of taxonomies. |
||
| 461 | */ |
||
| 462 | $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies ); |
||
| 463 | |||
| 464 | if ( ! empty( $exclusions ) ) { |
||
| 465 | // Must do string manipulation here for backward compatibility with filter. |
||
| 466 | $this->sql_clauses['where']['exclusions'] = preg_replace( '/^\s*AND\s*/', '', $exclusions ); |
||
| 467 | } |
||
| 468 | |||
| 469 | if ( ! empty( $args['name'] ) ) { |
||
| 470 | $names = (array) $args['name']; |
||
| 471 | foreach ( $names as &$_name ) { |
||
| 472 | // `sanitize_term_field()` returns slashed data. |
||
| 473 | $_name = stripslashes( sanitize_term_field( 'name', $_name, 0, reset( $taxonomies ), 'db' ) ); |
||
| 474 | } |
||
| 475 | |||
| 476 | $this->sql_clauses['where']['name'] = "t.name IN ('" . implode( "', '", array_map( 'esc_sql', $names ) ) . "')"; |
||
| 477 | } |
||
| 478 | |||
| 479 | if ( ! empty( $args['slug'] ) ) { |
||
| 480 | if ( is_array( $args['slug'] ) ) { |
||
| 481 | $slug = array_map( 'sanitize_title', $args['slug'] ); |
||
| 482 | $this->sql_clauses['where']['slug'] = "t.slug IN ('" . implode( "', '", $slug ) . "')"; |
||
| 483 | } else { |
||
| 484 | $slug = sanitize_title( $args['slug'] ); |
||
| 485 | $this->sql_clauses['where']['slug'] = "t.slug = '$slug'"; |
||
| 486 | } |
||
| 487 | } |
||
| 488 | |||
| 489 | if ( ! empty( $args['term_taxonomy_id'] ) ) { |
||
| 490 | if ( is_array( $args['term_taxonomy_id'] ) ) { |
||
| 491 | $tt_ids = implode( ',', array_map( 'intval', $args['term_taxonomy_id'] ) ); |
||
| 492 | $this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})"; |
||
| 493 | } else { |
||
| 494 | $this->sql_clauses['where']['term_taxonomy_id'] = $this->db->prepare( "tt.term_taxonomy_id = %d", $args['term_taxonomy_id'] ); |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | View Code Duplication | if ( ! empty( $args['name__like'] ) ) { |
|
| 499 | $this->sql_clauses['where']['name__like'] = $this->db->prepare( "t.name LIKE %s", '%' . $this->db->esc_like( $args['name__like'] ) . '%' ); |
||
| 500 | } |
||
| 501 | |||
| 502 | View Code Duplication | if ( ! empty( $args['description__like'] ) ) { |
|
| 503 | $this->sql_clauses['where']['description__like'] = $this->db->prepare( "tt.description LIKE %s", '%' . $this->db->esc_like( $args['description__like'] ) . '%' ); |
||
| 504 | } |
||
| 505 | |||
| 506 | if ( '' !== $parent ) { |
||
| 507 | $parent = (int) $parent; |
||
| 508 | $this->sql_clauses['where']['parent'] = "tt.parent = '$parent'"; |
||
| 509 | } |
||
| 510 | |||
| 511 | $hierarchical = $args['hierarchical']; |
||
| 512 | if ( 'count' == $args['fields'] ) { |
||
| 513 | $hierarchical = false; |
||
| 514 | } |
||
| 515 | if ( $args['hide_empty'] && !$hierarchical ) { |
||
| 516 | $this->sql_clauses['where']['count'] = 'tt.count > 0'; |
||
| 517 | } |
||
| 518 | |||
| 519 | $number = $args['number']; |
||
| 520 | $offset = $args['offset']; |
||
| 521 | |||
| 522 | // Don't limit the query results when we have to descend the family tree. |
||
| 523 | if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) { |
||
| 524 | View Code Duplication | if ( $offset ) { |
|
| 525 | $limits = 'LIMIT ' . $offset . ',' . $number; |
||
| 526 | } else { |
||
| 527 | $limits = 'LIMIT ' . $number; |
||
| 528 | } |
||
| 529 | } else { |
||
| 530 | $limits = ''; |
||
| 531 | } |
||
| 532 | |||
| 533 | |||
| 534 | if ( ! empty( $args['search'] ) ) { |
||
| 535 | $this->sql_clauses['where']['search'] = $this->get_search_sql( $args['search'] ); |
||
| 536 | } |
||
| 537 | |||
| 538 | // Meta query support. |
||
| 539 | $join = ''; |
||
| 540 | $distinct = ''; |
||
| 541 | |||
| 542 | // Reparse meta_query query_vars, in case they were modified in a 'pre_get_terms' callback. |
||
| 543 | $this->meta_query->parse_query_vars( $this->query_vars ); |
||
| 544 | $mq_sql = $this->meta_query->get_sql( 'term', 't', 'term_id' ); |
||
| 545 | $meta_clauses = $this->meta_query->get_clauses(); |
||
| 546 | |||
| 547 | if ( ! empty( $meta_clauses ) ) { |
||
| 548 | $join .= $mq_sql['join']; |
||
| 549 | $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $mq_sql['where'] ); |
||
| 550 | $distinct .= "DISTINCT"; |
||
| 551 | |||
| 552 | } |
||
| 553 | |||
| 554 | $selects = array(); |
||
| 555 | switch ( $args['fields'] ) { |
||
| 556 | case 'all': |
||
| 557 | $selects = array( 't.*', 'tt.*' ); |
||
| 558 | break; |
||
| 559 | case 'ids': |
||
| 560 | case 'id=>parent': |
||
| 561 | $selects = array( 't.term_id', 'tt.parent', 'tt.count', 'tt.taxonomy' ); |
||
| 562 | break; |
||
| 563 | case 'names': |
||
| 564 | $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy' ); |
||
| 565 | break; |
||
| 566 | case 'count': |
||
| 567 | $orderby = ''; |
||
| 568 | $order = ''; |
||
| 569 | $selects = array( 'COUNT(*)' ); |
||
| 570 | break; |
||
| 571 | case 'id=>name': |
||
| 572 | $selects = array( 't.term_id', 't.name', 'tt.count', 'tt.taxonomy' ); |
||
| 573 | break; |
||
| 574 | case 'id=>slug': |
||
| 575 | $selects = array( 't.term_id', 't.slug', 'tt.count', 'tt.taxonomy' ); |
||
| 576 | break; |
||
| 577 | } |
||
| 578 | |||
| 579 | $_fields = $args['fields']; |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Filters the fields to select in the terms query. |
||
| 583 | * |
||
| 584 | * Field lists modified using this filter will only modify the term fields returned |
||
| 585 | * by the function when the `$fields` parameter set to 'count' or 'all'. In all other |
||
| 586 | * cases, the term fields in the results array will be determined by the `$fields` |
||
| 587 | * parameter alone. |
||
| 588 | * |
||
| 589 | * Use of this filter can result in unpredictable behavior, and is not recommended. |
||
| 590 | * |
||
| 591 | * @since 2.8.0 |
||
| 592 | * |
||
| 593 | * @param array $selects An array of fields to select for the terms query. |
||
| 594 | * @param array $args An array of term query arguments. |
||
| 595 | * @param array $taxonomies An array of taxonomies. |
||
| 596 | */ |
||
| 597 | $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) ); |
||
| 598 | |||
| 599 | $join .= " INNER JOIN {$this->db->term_taxonomy} AS tt ON t.term_id = tt.term_id"; |
||
| 600 | |||
| 601 | $where = implode( ' AND ', $this->sql_clauses['where'] ); |
||
| 602 | |||
| 603 | $pieces = array( 'fields', 'join', 'where', 'distinct', 'orderby', 'order', 'limits' ); |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Filters the terms query SQL clauses. |
||
| 607 | * |
||
| 608 | * @since 3.1.0 |
||
| 609 | * |
||
| 610 | * @param array $pieces Terms query SQL clauses. |
||
| 611 | * @param array $taxonomies An array of taxonomies. |
||
| 612 | * @param array $args An array of terms query arguments. |
||
| 613 | */ |
||
| 614 | $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args ); |
||
| 615 | |||
| 616 | $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; |
||
| 617 | $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; |
||
| 618 | $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; |
||
| 619 | $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : ''; |
||
| 620 | $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; |
||
| 621 | $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : ''; |
||
| 622 | $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; |
||
| 623 | |||
| 624 | if ( $where ) { |
||
| 625 | $where = "WHERE $where"; |
||
| 626 | } |
||
| 627 | |||
| 628 | $this->sql_clauses['select'] = "SELECT $distinct $fields"; |
||
| 629 | $this->sql_clauses['from'] = "FROM {$this->db->terms} AS t $join"; |
||
| 630 | $this->sql_clauses['orderby'] = $orderby ? "$orderby $order" : ''; |
||
| 631 | $this->sql_clauses['limits'] = $limits; |
||
| 632 | |||
| 633 | $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}"; |
||
| 634 | |||
| 635 | // $args can be anything. Only use the args defined in defaults to compute the key. |
||
| 636 | $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request ); |
||
| 637 | $last_changed = wp_cache_get( 'last_changed', 'terms' ); |
||
| 638 | if ( ! $last_changed ) { |
||
| 639 | $last_changed = microtime(); |
||
| 640 | wp_cache_set( 'last_changed', $last_changed, 'terms' ); |
||
| 641 | } |
||
| 642 | $cache_key = "get_terms:$key:$last_changed"; |
||
| 643 | $cache = wp_cache_get( $cache_key, 'terms' ); |
||
| 644 | if ( false !== $cache ) { |
||
| 645 | if ( 'all' === $_fields ) { |
||
| 646 | $cache = array_map( 'get_term', $cache ); |
||
| 647 | } |
||
| 648 | |||
| 649 | $this->terms = $cache; |
||
| 650 | return $this->terms; |
||
| 651 | } |
||
| 652 | |||
| 653 | if ( 'count' == $_fields ) { |
||
| 654 | return $this->db->get_var( $this->request ); |
||
| 655 | } |
||
| 656 | |||
| 657 | $terms = $this->db->get_results( $this->request ); |
||
| 658 | if ( 'all' == $_fields ) { |
||
| 659 | update_term_cache( $terms ); |
||
| 660 | } |
||
| 661 | |||
| 662 | // Prime termmeta cache. |
||
| 663 | if ( $args['update_term_meta_cache'] ) { |
||
| 664 | $term_ids = wp_list_pluck( $terms, 'term_id' ); |
||
| 665 | update_termmeta_cache( $term_ids ); |
||
| 666 | } |
||
| 667 | |||
| 668 | if ( empty( $terms ) ) { |
||
| 669 | wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS ); |
||
| 670 | return array(); |
||
| 671 | } |
||
| 672 | |||
| 673 | if ( $child_of ) { |
||
| 674 | foreach ( $taxonomies as $_tax ) { |
||
| 675 | $children = _get_term_hierarchy( $_tax ); |
||
| 676 | if ( ! empty( $children ) ) { |
||
| 677 | $terms = _get_term_children( $child_of, $terms, $_tax ); |
||
| 678 | } |
||
| 679 | } |
||
| 680 | } |
||
| 681 | |||
| 682 | // Update term counts to include children. |
||
| 683 | if ( $args['pad_counts'] && 'all' == $_fields ) { |
||
| 684 | foreach ( $taxonomies as $_tax ) { |
||
| 685 | _pad_term_counts( $terms, $_tax ); |
||
| 686 | } |
||
| 687 | } |
||
| 688 | |||
| 689 | // Make sure we show empty categories that have children. |
||
| 690 | if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) { |
||
| 691 | foreach ( $terms as $k => $term ) { |
||
| 692 | if ( ! $term->count ) { |
||
| 693 | $children = get_term_children( $term->term_id, $term->taxonomy ); |
||
| 694 | if ( is_array( $children ) ) { |
||
| 695 | foreach ( $children as $child_id ) { |
||
| 696 | $child = get_term( $child_id, $term->taxonomy ); |
||
| 697 | if ( $child->count ) { |
||
| 698 | continue 2; |
||
| 699 | } |
||
| 700 | } |
||
| 701 | } |
||
| 702 | |||
| 703 | // It really is empty. |
||
| 704 | unset( $terms[ $k ] ); |
||
| 705 | } |
||
| 706 | } |
||
| 707 | } |
||
| 708 | |||
| 709 | $_terms = array(); |
||
| 710 | if ( 'id=>parent' == $_fields ) { |
||
| 711 | foreach ( $terms as $term ) { |
||
| 712 | $_terms[ $term->term_id ] = $term->parent; |
||
| 713 | } |
||
| 714 | } elseif ( 'ids' == $_fields ) { |
||
| 715 | foreach ( $terms as $term ) { |
||
| 716 | $_terms[] = $term->term_id; |
||
| 717 | } |
||
| 718 | } elseif ( 'names' == $_fields ) { |
||
| 719 | foreach ( $terms as $term ) { |
||
| 720 | $_terms[] = $term->name; |
||
| 721 | } |
||
| 722 | } elseif ( 'id=>name' == $_fields ) { |
||
| 723 | foreach ( $terms as $term ) { |
||
| 724 | $_terms[ $term->term_id ] = $term->name; |
||
| 725 | } |
||
| 726 | } elseif ( 'id=>slug' == $_fields ) { |
||
| 727 | foreach ( $terms as $term ) { |
||
| 728 | $_terms[ $term->term_id ] = $term->slug; |
||
| 729 | } |
||
| 730 | } |
||
| 731 | |||
| 732 | if ( ! empty( $_terms ) ) { |
||
| 733 | $terms = $_terms; |
||
| 734 | } |
||
| 735 | |||
| 736 | // Hierarchical queries are not limited, so 'offset' and 'number' must be handled now. |
||
| 737 | if ( $hierarchical && $number && is_array( $terms ) ) { |
||
| 738 | if ( $offset >= count( $terms ) ) { |
||
| 739 | $terms = array(); |
||
| 740 | } else { |
||
| 741 | $terms = array_slice( $terms, $offset, $number, true ); |
||
| 742 | } |
||
| 743 | } |
||
| 744 | |||
| 745 | wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); |
||
| 746 | |||
| 747 | if ( 'all' === $_fields ) { |
||
| 748 | $terms = array_map( 'get_term', $terms ); |
||
| 749 | } |
||
| 750 | |||
| 751 | $this->terms = $terms; |
||
| 752 | return $this->terms; |
||
| 753 | } |
||
| 754 | |||
| 909 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..