PluginProbe
WP Ultimate Post Grid / 3.0.0
WP Ultimate Post Grid v3.0.0
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
← All changes | includes/public/class-wpupg-grid.php +81 -249 3.7.03.0.0 View file →
@@ -37,17 +37,8 @@
37 37 */
38 38 private $ids = false;
39 39
40 40 /**
41 - * Cached total IDs to display in this grid.
42 - *
43 - * @since 3.3.0
44 - * @access private
45 - * @var array $total_ids Total number of grid ids.
46 - */
47 - private $total_ids = false;
48 -
49 - /**
50 41 * Cached terms to display in this grid.
51 42 *
52 43 * @since 3.0.0
53 44 * @access private
@@ -98,10 +89,8 @@
98 89
99 90 // Grid Data Source.
100 91 $grid['type'] = $this->type();
101 92 $grid['post_types'] = $this->post_types();
102 - $grid['post_status'] = $this->post_status();
103 - $grid['post_status_require_permission'] = $this->post_status_require_permission();
104 93 $grid['taxonomies'] = $this->taxonomies();
105 94 $grid['order_by'] = $this->order_by();
106 95 $grid['order'] = $this->order();
107 96 $grid['order_custom_key'] = $this->order_custom_key();
@@ -123,9 +112,8 @@
123 112
124 113 // Grid Filters.
125 114 $grid['filters'] = $this->filters();
126 115 $grid['filters_enabled'] = $this->filters_enabled();
127 - $grid['filters_style'] = $this->filters_style();
128 116 $grid['filters_relation'] = $this->filters_relation();
129 117
130 118 // Grid Layout.
131 119 $grid['layout_mode'] = $this->layout_mode();
@@ -155,11 +143,8 @@
155 143 $grid['pagination_type'] = $this->pagination_type();
156 144 $grid['pagination'] = $this->pagination();
157 145
158 146 // Grid Other.
159 - $grid['metadata'] = $this->metadata();
160 - $grid['metadata_name'] = $this->metadata_name();
161 - $grid['metadata_description'] = $this->metadata_description();
162 147 $grid['deeplinking'] = $this->deeplinking();
163 148 $grid['empty_message'] = $this->empty_message();
164 149
165 150 return $grid;
@@ -341,9 +326,12 @@
341 326 return $slug;
342 327 }
343 328 public function date() {
344 329 return $this->post ? $this->post->post_date : $this->meta( 'date' );
345 - }
330 + }
331 + public function post_status() {
332 + return in_array( 'attachment', $this->post_types() ) ? array( 'publish', 'inherit' ) : 'publish';
333 + }
346 334 public function version() {
347 335 return $this->meta( 'version', '0.0.0' );
348 336 }
349 337
@@ -364,20 +352,8 @@
364 352 }
365 353 public function post_types() {
366 354 return $this->unserialize( $this->meta( 'post_types', array( 'post' ) ) );
367 355 }
368 - public function post_status() {
369 - $post_status = $this->unserialize( $this->meta( 'post_status', array( 'publish' ) ) );
370 -
371 - if ( in_array( 'attachment', $this->post_types() ) ) {
372 - $post_status[] = 'inherit';
373 - }
374 -
375 - return $post_status;
376 - }
377 - public function post_status_require_permission() {
378 - return $this->meta( 'post_status_require_permission', true );
379 - }
380 356 public function taxonomies() {
381 357 return $this->unserialize( $this->meta( 'taxonomies', array( 'category' ) ) );
382 358 }
383 359 public function order_by() {
@@ -476,26 +452,10 @@
476 452 if ( version_compare( $this->version(), '3.0.0', '<' ) ) {
477 453 return 0 < count( $this->filters() );
478 454 }
479 455
480 - // No filters for a term grid.
481 - if ( 'terms' === $this->type() ) {
482 - return false;
483 - }
484 -
485 456 return $this->meta( 'filters_enabled', false );
486 457 }
487 - public function filters_style( $field = false ) {
488 - $defaults = WPUPG_Filter::get_general_style_defaults();
489 - $filters_style = $this->unserialize( $this->meta( 'filters_style', array() ) );
490 - $filters_style_with_defaults = array_replace_recursive( $defaults, $filters_style );
491 -
492 - if ( $field === false ) {
493 - return $filters_style_with_defaults;
494 - } else {
495 - return isset( $filters_style_with_defaults[ $field ] ) ? $filters_style_with_defaults[ $field ] : false;
496 - }
497 - }
498 458 public function filters_relation() {
499 459 return $this->meta( 'filters_relation', 'AND' );
500 460 }
501 461
@@ -626,17 +586,8 @@
626 586 }
627 587 /**
628 588 * Grid Other Fields.
629 589 */
630 - public function metadata() {
631 - return $this->meta( 'metadata', false );
632 - }
633 - public function metadata_name() {
634 - return $this->meta( 'metadata_name', '' );
635 - }
636 - public function metadata_description() {
637 - return $this->meta( 'metadata_description', '' );
638 - }
639 590 public function deeplinking() {
640 591 return $this->meta( 'deeplinking', true );
641 592 }
642 593 public function empty_message() {
@@ -658,18 +609,8 @@
658 609 return array_filter( $ids, function( $id ) use ( $already_loaded_ids ) { return ! in_array( $id, $already_loaded_ids ); } );
659 610 }
660 611
661 612 /**
662 - * Get the total number of IDs for this grid.
663 - *
664 - * @since 3.4.0
665 - */
666 - public function total_ids() {
667 - $total_ids = $this->total_ids;
668 - return false !== $total_ids ? intval( $total_ids ) : false;
669 - }
670 -
671 - /**
672 613 * Get the query post arguments for getting all IDs.
673 614 *
674 615 * @since 3.0.0
675 616 * @param mixed $grid_args Optional arguments.
@@ -682,17 +623,11 @@
682 623 'post_status' => $this->post_status(),
683 624 'order' => $this->order(),
684 625 'orderby' => $this->order_by(),
685 626 'posts_per_page' => -1,
686 - // 'posts_per_page' => 2,
687 627 'fields' => 'ids',
688 628 );
689 629
690 - // Read permission for private posts.
691 - if ( in_array( 'private', $this->post_status() ) && $this->post_status_require_permission() ) {
692 - $args['perm'] = 'readable';
693 - }
694 -
695 630 // Images Only
696 631 if ( $this->images_only() ) {
697 632 if ( in_array( 'attachment', $post_types ) ) {
698 633 $args['post_mime_type'] = 'image/jpeg,image/gif,image/jpg,image/png';
@@ -698,20 +633,12 @@
698 633 $args['post_mime_type'] = 'image/jpeg,image/gif,image/jpg,image/png';
699 634 } else {
700 635 $args['meta_query'] = array(
701 636 array(
702 - 'relation' => 'OR',
703 - array(
704 - 'key' => '_thumbnail_id',
705 - 'value' => '0',
706 - 'compare' => '>'
707 - ),
708 - array(
709 - 'key' => 'wpupg_custom_image_id',
710 - 'value' => '0',
711 - 'compare' => '>'
712 - ),
713 - )
637 + 'key' => '_thumbnail_id',
638 + 'value' => '0',
639 + 'compare' => '>'
640 + ),
714 641 );
715 642 }
716 643 }
717 644
@@ -724,11 +651,8 @@
724 651 $tax_query = array(
725 652 'relation' => 'AND',
726 653 );
727 654
728 - $meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
729 - $meta_query['relation'] = 'AND';
730 -
731 655 $filters = isset( $grid_args['filters'] ) ? $grid_args['filters'] : array();
732 656 foreach ( $filters as $filter ) {
733 657 switch ( $filter['type'] ) {
734 658 case 'search':
@@ -749,43 +673,8 @@
749 673 if ( 'AND' !== $filter['terms_relation'] ) {
750 674 $tax_query['relation'] = $filter['terms_relation'];
751 675 }
752 676 break;
753 - case 'custom_field':
754 - foreach ( $filter['values'] as $value ) {
755 - if ( $value ) {
756 - switch ( $value['type'] ) {
757 - case 'string':
758 - $meta_query[] = array(
759 - 'key' => $filter['custom_field'],
760 - 'value' => $value['value'],
761 - 'compare' => $filter['values_inverse'] ? '!=' : '=',
762 - );
763 - break;
764 - case 'number':
765 - $meta_query[] = array(
766 - 'key' => $filter['custom_field'],
767 - 'value' => $value['value'],
768 - 'type' => 'NUMERIC',
769 - 'compare' => $filter['values_inverse'] ? '!=' : '=',
770 - );
771 - break;
772 - case 'range':
773 - $meta_query[] = array(
774 - 'key' => $filter['custom_field'],
775 - 'value' => $value['value'],
776 - 'type' => 'NUMERIC',
777 - 'compare' => $filter['values_inverse'] ? 'NOT BETWEEN' : 'BETWEEN',
778 - );
779 - break;
780 - }
781 - }
782 - }
783 -
784 - if ( 'AND' !== $filter['values_relation'] ) {
785 - $meta_query['relation'] = $filter['values_relation'];
786 - }
787 - break;
788 677 }
789 678 }
790 679
791 680 if ( 1 < count( $tax_query ) ) {
@@ -790,11 +679,8 @@
790 679
791 680 if ( 1 < count( $tax_query ) ) {
792 681 $args['tax_query'] = $tax_query;
793 682 }
794 - if ( 1 < count( $meta_query ) ) {
795 - $args['meta_query'] = $meta_query;
796 - }
797 683
798 684 return apply_filters( 'wpupg_query_post_args', $args, $this, $grid_args );
799 685 }
800 686
@@ -806,9 +692,8 @@
806 692 */
807 693 public function all_ids( $grid_args = array() ) {
808 694 if ( false === $this->ids ) {
809 695 $ids = array();
810 - $total_ids = false;
811 696
812 697 if ( 'posts' === $this->type() ) {
813 698 $args = $this->all_ids_query_post_args( $grid_args );
814 699
@@ -815,9 +700,8 @@
815 700 // Query IDs.
816 701 $query = new WP_Query( $args );
817 702 $posts = $query->have_posts() ? $query->posts : array();
818 703 $ids = array_map( 'intval', $posts );
819 - $total_ids = $query->found_posts;
820 704
821 705 // Offset posts
822 706 if ( $this->limit_posts_offset() ) {
823 707 $ids = array_slice( $ids, $this->limit_posts_offset() );
@@ -829,9 +713,8 @@
829 713 }
830 714 }
831 715
832 716 $this->ids = apply_filters( 'wpupg_grid_all_ids', $ids, $this, $grid_args );
833 - $this->total_ids = $total_ids;
834 717 }
835 718
836 719 return $this->ids;
837 720 }
@@ -848,29 +731,44 @@
848 731 'per_item' => array(),
849 732 'per_taxonomy' => array(),
850 733 );
851 734 $taxonomies = $this->filters_taxonomies();
735 +
736 + foreach ( $taxonomies as $taxonomy ) {
737 + $terms['per_taxonomy'][ $taxonomy ] = array();
738 + }
852 739
853 740 // Only need to get terms if there actually are taxonomies.
854 741 if ( count( $taxonomies ) ) {
855 742 $post_ids = $this->all_ids( $grid_args );
743 +
744 + foreach ( $post_ids as $post_id ) {
745 + $terms['per_item'][ $post_id ] = array();
746 +
747 + foreach ( $taxonomies as $taxonomy ) {
748 + $terms['per_item'][ $post_id ][ $taxonomy ] = array(
749 + 'terms' => array(),
750 + 'parent_terms' => array(),
751 + );
752 +
753 + $taxonomy_terms = get_terms( array(
754 + 'taxonomy' => $taxonomy,
755 + 'object_ids' => $post_id,
756 + ) );
757 + $taxonomy_terms = ! $taxonomy_terms || is_wp_error( $taxonomy_terms ) ? array() : $taxonomy_terms;
856 758
857 - // Build sanitized query.
858 - global $wpdb;
759 + foreach( $taxonomy_terms as $term ) {
760 + // Add term
761 + $terms = $this->include_term( $terms, $post_id, $taxonomy, $term, false );
859 762
860 - $where_taxonomies = implode( '","', array_map( 'sanitize_key', $taxonomies ) );
861 - $where_post_ids = implode( ',', array_map( 'intval', $post_ids ) );
862 -
863 - $query = 'SELECT tr.object_id, tt.taxonomy, tt.parent, t.term_id, t.slug, t.name FROM ' . $wpdb->prefix . 'term_relationships tr JOIN ' . $wpdb->prefix . 'term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id JOIN ' . $wpdb->prefix . 'terms t on tt.term_id = t.term_id WHERE tt.taxonomy IN ("' . $where_taxonomies . '")';
864 -
865 - if ( $where_post_ids ) {
866 - $query .= ' AND tr.object_id IN (' . $where_post_ids . ')';
763 + // Add optional parent terms.
764 + $parent_terms = $this->get_term_parents( $term );
765 + foreach ( $parent_terms as $parent_term ) {
766 + $terms = $this->include_term( $terms, $post_id, $taxonomy, $parent_term, true );
767 + }
768 + }
769 + }
867 770 }
868 -
869 - $results = $wpdb->get_results( $query, ARRAY_A );
870 -
871 - // Loop over all results.
872 - $terms = $this->handle_terms( $terms, $results );
873 771 }
874 772
875 773 $this->terms = $terms;
876 774 }
@@ -876,102 +774,61 @@
876 774 }
877 775
878 776 return $this->terms;
879 777 }
880 -
778 +
881 779 /**
882 - * Get terms from query reults.
780 + * Get parents of a specific term.
883 781 *
884 - * @since 3.5.0
782 + * @since 3.0.0
885 783 */
886 - public function handle_terms( $terms, $results, $depth = 0 ) {
887 - $parents = array();
784 + public function include_term( $terms, $post_id, $taxonomy, $term, $as_parent = false ) {
785 + $slug = urldecode( $term->slug );
888 786
889 - foreach ( $results as $result ) {
890 - $item_id = intval( $result['object_id'] );
891 - $taxonomy = $result['taxonomy'];
892 - $term_id = intval( $result['term_id'] );
893 - $parent = intval( $result['parent'] );
894 - $slug = rawurldecode( $result['slug'] );
895 - $name = apply_filters( 'wpupg_term_name', $result['name'], $term_id, $taxonomy );
896 -
897 - // Make sure arrays exist.
898 - if ( ! isset( $terms['per_taxonomy'][ $taxonomy ] ) ) { $terms['per_taxonomy'][ $taxonomy ] = array(); }
899 - if ( ! isset( $terms['per_item'][ $item_id ] ) ) { $terms['per_item'][ $item_id ] = array(); }
900 - if ( ! isset( $terms['per_item'][ $item_id ][ $taxonomy ] ) ) {
901 - $terms['per_item'][ $item_id ][ $taxonomy ] = array(
902 - 'terms' => array(),
903 - 'parent_terms' => array(),
904 - );
905 - }
906 - if ( !isset( $terms['per_taxonomy'][ $taxonomy ][ $slug ] ) ) {
907 - $terms['per_taxonomy'][ $taxonomy ][ $slug ] = array(
908 - 'id' => $term_id,
909 - 'parent' => $parent,
910 - 'name' => $name,
911 - 'posts' => array(),
912 - 'child_posts' => array(),
913 - );
914 - }
915 -
916 - // Add in correct category.
917 - if ( 0 === $depth ) {
918 - $terms['per_item'][ $item_id ][ $taxonomy ]['terms'][] = $slug;
919 - $terms['per_taxonomy'][ $taxonomy ][ $slug ]['posts'][] = $item_id;
920 - } else {
921 - $terms['per_item'][ $item_id ][ $taxonomy ]['parent_terms'][] = $slug;
922 - $terms['per_taxonomy'][ $taxonomy ][ $slug ]['child_posts'][] = $item_id;
923 - }
924 -
925 - // Check if there are further parents we need to get.
926 - if ( $parent ) {
927 - $parents[] = array(
928 - 'object_id' => $item_id,
929 - 'taxonomy' => $taxonomy,
930 - 'term_id' => $parent,
931 - );
932 - }
787 + // Terms per post.
788 + if ( $as_parent ) {
789 + $terms['per_item'][ $post_id ][ $taxonomy ]['parent_terms'][] = $slug;
790 + } else {
791 + $terms['per_item'][ $post_id ][ $taxonomy ]['terms'][] = $slug;
933 792 }
793 +
794 + // Terms per taxonomy.
795 + if ( !isset( $terms['per_taxonomy'][ $taxonomy ][ $slug ] ) ) {
796 + $terms['per_taxonomy'][ $taxonomy ][ $slug ] = array(
797 + 'id' => $term->term_id,
798 + 'parent' => $term->parent,
799 + 'name' => $term->name,
800 + 'posts' => array(),
801 + 'child_posts' => array(),
802 + );
803 + }
804 + if ( $as_parent ) {
805 + $terms['per_taxonomy'][ $taxonomy ][ $slug ]['child_posts'][] = $post_id;
806 + } else {
807 + $terms['per_taxonomy'][ $taxonomy ][ $slug ]['posts'][] = $post_id;
808 + }
934 809
935 - // Check if there are any parents we need to associate as well.
936 - if ( count( $parents ) ) {
937 - $parent_results = array();
810 + return $terms;
811 + }
938 812
939 - $parent_ids = wp_list_pluck( $parents, 'term_id' );
940 - $parent_terms = get_terms( array(
941 - 'include' => $parent_ids,
942 - ) );
813 + /**
814 + * Get parents of a specific term.
815 + *
816 + * @since 3.0.0
817 + * @param int $term Term to get the parents for.
818 + * @param array $parents Current list of known parents.
819 + */
820 + public function get_term_parents( $term, $parents = array() ) {
821 + if ( $term->parent ) {
822 + $parent = get_term( $term->parent );
943 823
944 - if ( $parent_terms && ! is_wp_error( $parent_terms ) ) {
945 - // Set term_id as key.
946 - $keyed_parent_terms = array();
947 - foreach ( $parent_terms as $parent_term ) {
948 - $keyed_parent_terms[ $parent_term->term_id ] = $parent_term;
949 - }
950 -
951 - foreach( $parents as $index => $parent ) {
952 - if ( isset( $keyed_parent_terms[ $parent['term_id'] ] ) ) {
953 - $parent_term = $keyed_parent_terms[ $parent['term_id'] ];
954 -
955 - $parent_results[] = array(
956 - 'object_id' => $parent['object_id'],
957 - 'taxonomy' => $parent['taxonomy'],
958 - 'term_id' => $parent['term_id'],
959 - 'parent' => $parent_term->parent,
960 - 'slug' => $parent_term->slug,
961 - 'name' => $parent_term->name,
962 - );
963 - }
964 - }
824 + if ( $parent && ! is_wp_error( $parent ) ) {
825 + $parents[] = $parent;
826 + return $this->get_term_parents( $parent, $parents );
965 827 }
966 -
967 - // Recursive loop.
968 - if ( count( $parent_results ) ) {
969 - $terms = $this->handle_terms( $terms, $parent_results, $depth + 1 );
970 - }
971 828 }
972 829
973 - return $terms;
830 + return $parents;
974 831 }
975 832
976 833 /**
977 834 * Get the terms for a specific item.
@@ -1008,38 +865,13 @@
1008 865 if ( $this->filters_enabled() ) {
1009 866 $filters = $this->filters();
1010 867
1011 868 foreach ( $filters as $filter ) {
1012 - if ( ! isset( $filter['options']['source'] ) || 'taxonomies' === $filter['options']['source'] ) {
1013 - if ( isset( $filter['options']['taxonomies'] ) ) {
1014 - $taxonomies = array_merge( $taxonomies, $filter['options']['taxonomies'] );
1015 - }
869 + if ( isset( $filter['options']['taxonomies'] ) ) {
870 + $taxonomies = array_merge( $taxonomies, $filter['options']['taxonomies'] );
1016 871 }
1017 872 }
1018 873 }
1019 874
1020 875 return array_unique( $taxonomies );
1021 - }
1022 -
1023 - /**
1024 - * Get the custom fields filtered by in this grid.
1025 - *
1026 - * @since 3.3.0
1027 - */
1028 - public function filters_custom_fields() {
1029 - $custom_fields = array();
1030 -
1031 - if ( $this->filters_enabled() ) {
1032 - $filters = $this->filters();
1033 -
1034 - foreach ( $filters as $filter ) {
1035 - if ( ! isset( $filter['options']['source'] ) || 'custom_field' === $filter['options']['source'] ) {
1036 - if ( isset( $filter['options']['custom_field'] ) ) {
1037 - $custom_fields[] = $filter['options']['custom_field'];
1038 - }
1039 - }
1040 - }
1041 - }
1042 -
1043 - return array_unique( $custom_fields );
1044 876 }
1045 877 }