Changeset 55569
- Timestamp:
- 03/21/2023 12:47:20 PM (17 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/post.php
r55526 r55569 5887 5887 * Retrieves an array of pages (or hierarchical post type items). 5888 5888 * 5889 * @global wpdb $wpdb WordPress database abstraction object.5890 *5891 5889 * @since 1.5.0 5890 5892 5891 * 5893 5892 * @param array|string $args { … … 5929 5928 */ 5930 5929 function get_pages( $args = array() ) { 5931 global $wpdb;5932 5933 5930 $defaults = array( 5934 5931 'child_of' => 0, … … 5979 5976 } 5980 5977 5981 // $args can be whatever, only use the args defined in defaults to compute the key. 5982 $key = md5( serialize( wp_array_slice_assoc( $parsed_args, array_keys( $defaults ) ) ) ); 5983 $last_changed = wp_cache_get_last_changed( 'posts' ); 5984 5985 $cache_key = "get_pages:$key:$last_changed"; 5986 $cache = wp_cache_get( $cache_key, 'post-queries' ); 5987 if ( false !== $cache ) { 5988 _prime_post_caches( $cache, false, false ); 5989 5990 // Convert to WP_Post instances. 5991 $pages = array_map( 'get_post', $cache ); 5992 /** This filter is documented in wp-includes/post.php */ 5993 $pages = apply_filters( 'get_pages', $pages, $parsed_args ); 5994 5995 return $pages; 5996 } 5997 5998 $inclusions = ''; 5978 $query_args = array( 5979 'orderby' => 'post_title', 5980 'order' => 'ASC', 5981 'post__not_in' => wp_parse_id_list( $exclude ), 5982 'meta_key' => $meta_key, 5983 'meta_value' => $meta_value, 5984 'posts_per_page' => -1, 5985 'offset' => $offset, 5986 'post_type' => $parsed_args['post_type'], 5987 'post_status' => $post_status, 5988 'update_post_term_cache' => false, 5989 'update_post_meta_cache' => false, 5990 'ignore_sticky_posts' => true, 5991 'no_found_rows' => true, 5992 ); 5993 5999 5994 if ( ! empty( $parsed_args['include'] ) ) { 6000 $child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include. 6001 $parent = -1; 6002 $exclude = ''; 6003 $meta_key = ''; 6004 $meta_value = ''; 6005 $hierarchical = false; 6006 $incpages = wp_parse_id_list( $parsed_args['include'] ); 6007 if ( ! empty( $incpages ) ) { 6008 $inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')'; 6009 } 6010 } 6011 6012 $exclusions = ''; 6013 if ( ! empty( $exclude ) ) { 6014 $expages = wp_parse_id_list( $exclude ); 6015 if ( ! empty( $expages ) ) { 6016 $exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')'; 6017 } 6018 } 6019 6020 $author_query = ''; 5995 $child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include. 5996 $parent = -1; 5997 unset( $query_args['post__not_in'], $query_args['meta_key'], $query_args['meta_value'] ); 5998 $hierarchical = false; 5999 $query_args['post__in'] = wp_parse_id_list( $parsed_args['include'] ); 6000 } 6001 6021 6002 if ( ! empty( $parsed_args['authors'] ) ) { 6022 6003 $post_authors = wp_parse_list( $parsed_args['authors'] ); 6023 6004 6024 6005 if ( ! empty( $post_authors ) ) { 6006 6025 6007 foreach ( $post_authors as $post_author ) { 6026 6008 // Do we have an author id or an author login? … … 6035 6017 $post_author = $post_author->ID; 6036 6018 } 6037 6038 if ( '' === $author_query ) { 6039 $author_query = $wpdb->prepare( ' post_author = %d ', $post_author ); 6040 } else { 6041 $author_query .= $wpdb->prepare( ' OR post_author = %d ', $post_author ); 6042 } 6019 $query_args['author__in'][] = $post_author; 6043 6020 } 6044 if ( '' !== $author_query ) {6045 $author_query = " AND ($author_query)";6046 }6047 }6048 }6049 6050 $join = '';6051 $where = "$exclusions $inclusions ";6052 if ( '' !== $meta_key || '' !== $meta_value ) {6053 $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";6054 6055 // meta_key and meta_value might be slashed.6056 $meta_key = wp_unslash( $meta_key );6057 $meta_value = wp_unslash( $meta_value );6058 if ( '' !== $meta_key ) {6059 $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_key = %s", $meta_key );6060 }6061 if ( '' !== $meta_value ) {6062 $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_value = %s", $meta_value );6063 6021 } 6064 6022 } 6065 6023 6066 6024 if ( is_array( $parent ) ) { 6067 $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ));6025 $post_parent__in = ); 6068 6026 if ( ! empty( $post_parent__in ) ) { 6069 $ where .= " AND post_parent IN ($post_parent__in)";6027 $; 6070 6028 } 6071 6029 } elseif ( $parent >= 0 ) { 6072 $where .= $wpdb->prepare( ' AND post_parent = %d ', $parent ); 6073 } 6074 6075 if ( 1 === count( $post_status ) ) { 6076 $where_post_type = $wpdb->prepare( 'post_type = %s AND post_status = %s', $parsed_args['post_type'], reset( $post_status ) ); 6077 } else { 6078 $post_status = implode( "', '", $post_status ); 6079 $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $parsed_args['post_type'] ); 6080 } 6081 6082 $orderby_array = array(); 6083 $allowed_keys = array( 6084 'author', 6085 'post_author', 6086 'date', 6087 'post_date', 6088 'title', 6089 'post_title', 6090 'name', 6091 'post_name', 6092 'modified', 6093 'post_modified', 6094 'modified_gmt', 6095 'post_modified_gmt', 6096 'menu_order', 6097 'parent', 6098 'post_parent', 6099 'ID', 6100 'rand', 6101 'comment_count', 6102 ); 6103 6104 foreach ( explode( ',', $parsed_args['sort_column'] ) as $orderby ) { 6105 $orderby = trim( $orderby ); 6106 if ( ! in_array( $orderby, $allowed_keys, true ) ) { 6107 continue; 6108 } 6109 6110 switch ( $orderby ) { 6111 case 'menu_order': 6112 break; 6113 case 'ID': 6114 $orderby = "$wpdb->posts.ID"; 6115 break; 6116 case 'rand': 6117 $orderby = 'RAND()'; 6118 break; 6119 case 'comment_count': 6120 $orderby = "$wpdb->posts.comment_count"; 6121 break; 6122 default: 6123 if ( 0 === strpos( $orderby, 'post_' ) ) { 6124 $orderby = "$wpdb->posts." . $orderby; 6125 } else { 6126 $orderby = "$wpdb->posts.post_" . $orderby; 6127 } 6128 } 6129 6130 $orderby_array[] = $orderby; 6131 6132 } 6133 $sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title"; 6134 6135 $sort_order = strtoupper( $parsed_args['sort_order'] ); 6136 if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ), true ) ) { 6137 $sort_order = 'ASC'; 6138 } 6139 6140 $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where "; 6141 $query .= $author_query; 6142 $query .= ' ORDER BY ' . $sort_column . ' ' . $sort_order; 6030 $query_args['post_parent'] = $parent; 6031 } 6032 6033 $orderby = wp_parse_list( $parsed_args['sort_column'] ); 6034 $orderby = array_map( 'trim', $orderby ); 6035 if ( $orderby ) { 6036 $query_args['orderby'] = array_fill_keys( $orderby, $parsed_args['sort_order'] ); 6037 } 6038 6039 $order = $parsed_args['sort_order']; 6040 if ( $order ) { 6041 $query_args['order'] = $order; 6042 } 6143 6043 6144 6044 if ( ! empty( $number ) ) { 6145 $query .= ' LIMIT ' . $offset . ',' . $number; 6146 } 6147 6148 $pages = $wpdb->get_results( $query ); 6149 6150 if ( empty( $pages ) ) { 6151 wp_cache_set( $cache_key, array(), 'post-queries' ); 6152 6153 /** This filter is documented in wp-includes/post.php */ 6154 $pages = apply_filters( 'get_pages', array(), $parsed_args ); 6155 6156 return $pages; 6157 } 6158 6159 // Sanitize before caching so it'll only get done once. 6160 $num_pages = count( $pages ); 6161 for ( $i = 0; $i < $num_pages; $i++ ) { 6162 $pages[ $i ] = sanitize_post( $pages[ $i ], 'raw' ); 6163 } 6164 6165 // Update cache. 6166 update_post_cache( $pages ); 6045 $query_args['posts_per_page'] = $number; 6046 } 6047 6048 $query = new WP_Query( $query_args ); 6049 $pages = $query->get_posts(); 6167 6050 6168 6051 if ( $child_of || $hierarchical ) { … … 6186 6069 } 6187 6070 } 6188 6189 $page_structure = array();6190 foreach ( $pages as $page ) {6191 $page_structure[] = $page->ID;6192 }6193 6194 wp_cache_set( $cache_key, $page_structure, 'post-queries' );6195 6196 // Convert to WP_Post instances.6197 $pages = array_map( 'get_post', $pages );6198 6071 6199 6072 /** -
trunk/tests/phpunit/tests/post/getPages.php
r54402 r55569 67 67 clean_post_cache( $pages[0]->ID ); 68 68 $this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'posts' ) ); 69 69 get_post( $pages[0]->ID ); 70 70 $num_queries = $wpdb->num_queries; 71 71 … … 171 171 $found_ids = wp_list_pluck( $found, 'ID' ); 172 172 $this->assertSameSets( array( $posts[0] ), $found_ids ); 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 173 227 } 174 228 … … 720 774 $this->assertSame( $num_queries, $wpdb->num_queries ); 721 775 } 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 �� 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 722 1025 }
Note: See TracChangeset
for help on using the changeset viewer.