WordPress.org

Make WordPress Core

Ticket #17807: 17807.5.diff

File 17807.5.diff, 19.2 KB (added by ethitter, 2 years ago)
  • link-template.php

     
    10641064 * 
    10651065 * @since 1.5.0 
    10661066 * 
    1067  * @param bool $in_same_cat Optional. Whether post should be in a same category. 
    1068  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1067 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 
     1068 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1069 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    10691070 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 
    10701071 */ 
    1071 function get_previous_post($in_same_cat = false, $excluded_categories = '') { 
    1072         return get_adjacent_post($in_same_cat, $excluded_categories); 
     1072function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1073        return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy ); 
    10731074} 
    10741075 
    10751076/** 
     
    10771078 * 
    10781079 * @since 1.5.0 
    10791080 * 
    1080  * @param bool $in_same_cat Optional. Whether post should be in a same category. 
    1081  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1081 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 
     1082 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1083 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    10821084 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 
    10831085 */ 
    1084 function get_next_post($in_same_cat = false, $excluded_categories = '') { 
    1085         return get_adjacent_post($in_same_cat, $excluded_categories, false); 
     1086function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1087        return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy ); 
    10861088} 
    10871089 
    10881090/** 
     
    10921094 * 
    10931095 * @since 2.5.0 
    10941096 * 
    1095  * @param bool $in_same_cat Optional. Whether post should be in a same category. 
    1096  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1097 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 
     1098 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
    10971099 * @param bool $previous Optional. Whether to retrieve previous post. 
     1100 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    10981101 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 
    10991102 */ 
    1100 function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) { 
     1103function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
    11011104        global $post, $wpdb; 
    11021105 
    1103         if ( empty( $post ) ) 
     1106        if ( empty( $post ) || ! taxonomy_exists( $taxonomy ) ) 
    11041107                return null; 
    11051108 
    11061109        $current_post_date = $post->post_date; 
    11071110 
    11081111        $join = ''; 
    1109         $posts_in_ex_cats_sql = ''; 
    1110         if ( $in_same_cat || ! empty( $excluded_categories ) ) { 
     1112        $posts_in_ex_terms_sql = ''; 
     1113        if ( $in_same_term || ! empty( $excluded_terms ) ) { 
    11111114                $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id"; 
    11121115 
    1113                 if ( $in_same_cat ) { 
    1114                         $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids')); 
    1115                         $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")"; 
     1116                if ( $in_same_term ) { 
     1117                        $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 
     1118                        $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy ); 
    11161119                } 
    11171120 
    1118                 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'"; 
    1119                 if ( ! empty( $excluded_categories ) ) { 
    1120                         if ( ! is_array( $excluded_categories ) ) { 
    1121                                 // back-compat, $excluded_categories used to be IDs separated by " and " 
    1122                                 if ( strpos( $excluded_categories, ' and ' ) !== false ) { 
    1123                                         _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) ); 
    1124                                         $excluded_categories = explode( ' and ', $excluded_categories ); 
     1121                $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); 
     1122                if ( ! empty( $excluded_terms ) ) { 
     1123                        if ( ! is_array( $excluded_terms ) ) { 
     1124                                // back-compat, $excluded_terms used to be IDs separated by " and " 
     1125                                if ( strpos( $excluded_terms, ' and ' ) !== false ) { 
     1126                                        _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) ); 
     1127                                        $excluded_terms = explode( ' and ', $excluded_terms ); 
    11251128                                } else { 
    1126                                         $excluded_categories = explode( ',', $excluded_categories ); 
     1129                                        $excluded_terms = explode( ',', $excluded_terms ); 
    11271130                                } 
    11281131                        } 
    11291132 
    1130                         $excluded_categories = array_map( 'intval', $excluded_categories ); 
    1131  
    1132                         if ( ! empty( $cat_array ) ) { 
    1133                                 $excluded_categories = array_diff($excluded_categories, $cat_array); 
    1134                                 $posts_in_ex_cats_sql = ''; 
     1133                        $excluded_terms = array_map( 'intval', $excluded_terms ); 
     1134                         
     1135                        if ( ! empty( $term_array ) ) { 
     1136                                $excluded_terms = array_diff( $excluded_terms, $term_array ); 
     1137                                $posts_in_ex_terms_sql = ''; 
    11351138                        } 
    11361139 
    1137                         if ( !empty($excluded_categories) ) { 
    1138                                 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; 
    1139                         } 
     1140                        if ( !empty( $excluded_terms ) ) 
     1141                                $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy ); 
    11401142                } 
    11411143        } 
    11421144 
     
    11441146        $op = $previous ? '<' : '>'; 
    11451147        $order = $previous ? 'DESC' : 'ASC'; 
    11461148 
    1147         $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories ); 
    1148         $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories ); 
     1149        $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms ); 
     1150        $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_terms_sql", $current_post_date, $post->post_type), $in_same_term, $excluded_terms ); 
    11491151        $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); 
    11501152 
    11511153        $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort"; 
    1152         $query_key = 'adjacent_post_' . md5($query); 
    1153         $result = wp_cache_get($query_key, 'counts'); 
     1154        $query_key = 'adjacent_post_' . md5( $query ); 
     1155        $result = wp_cache_get( $query_key, 'counts' ); 
    11541156        if ( false !== $result ) 
    11551157                return $result; 
    11561158 
    1157         $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort"); 
     1159        $result = $wpdb->get_row( "SELECT p.* FROM $wpdb->posts AS p $join $where $sort" ); 
    11581160        if ( null === $result ) 
    11591161                $result = ''; 
    11601162 
    1161         wp_cache_set($query_key, $result, 'counts'); 
     1163        wp_cache_set( $query_key, $result, 'counts' ); 
    11621164        return $result; 
    11631165} 
    11641166 
     
    11701172 * @since 2.8.0 
    11711173 * 
    11721174 * @param string $title Optional. Link title format. 
    1173  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1174  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1175 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1176 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
    11751177 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. 
     1178 * @param string $taxonony Options, default is category. Taxonomy, if $in_same_term is true. 
    11761179 * @return string 
    11771180 */ 
    1178 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) { 
     1181function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
    11791182        if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) ) 
    11801183                $post = & get_post($GLOBALS['post']->post_parent); 
    11811184        else 
    1182                 $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous); 
     1185                $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
    11831186 
    11841187        if ( empty($post) ) 
    11851188                return; 
    11861189 
    1187         if ( empty($post->post_title) ) 
     1190        if ( empty( $post->post_title ) ) 
    11881191                $post->post_title = $previous ? __('Previous Post') : __('Next Post'); 
    11891192 
    1190         $date = mysql2date(get_option('date_format'), $post->post_date); 
     1193        $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 
    11911194 
    1192         $title = str_replace('%title', $post->post_title, $title); 
    1193         $title = str_replace('%date', $date, $title); 
    1194         $title = apply_filters('the_title', $title, $post->ID); 
     1195        $title = str_replace( '%title', $post->post_title, $title ); 
     1196        $title = str_replace( '%date', $date, $title ); 
     1197        $title = apply_filters( 'the_title', $title, $post->ID ); 
    11951198 
    11961199        $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; 
    11971200        $link .= esc_attr( $title ); 
    1198         $link .= "' href='" . get_permalink($post) . "' />\n"; 
     1201        $link .= "' href='" . get_permalink( $post ) . "' />\n"; 
    11991202 
    12001203        $adjacent = $previous ? 'previous' : 'next'; 
    12011204        return apply_filters( "{$adjacent}_post_rel_link", $link ); 
     
    12071210 * @since 2.8.0 
    12081211 * 
    12091212 * @param string $title Optional. Link title format. 
    1210  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1211  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1213 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1214 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1215 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    12121216 */ 
    1213 function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { 
    1214         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true); 
    1215         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false); 
     1217function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1218        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy ); 
     1219        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy ); 
    12161220} 
    12171221 
    12181222/** 
     
    12341238 * @since 2.8.0 
    12351239 * 
    12361240 * @param string $title Optional. Link title format. 
    1237  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1238  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1241 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1242 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1243 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    12391244 */ 
    1240 function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { 
    1241         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false); 
     1245function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1246        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy ); 
    12421247} 
    12431248 
    12441249/** 
     
    12471252 * @since 2.8.0 
    12481253 * 
    12491254 * @param string $title Optional. Link title format. 
    1250  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1251  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1255 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1256 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1257 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    12521258 */ 
    1253 function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { 
    1254         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true); 
     1259function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1260        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy ); 
    12551261} 
    12561262 
    12571263/** 
    12581264 * Retrieve boundary post. 
    12591265 * 
    12601266 * Boundary being either the first or last post by publish date within the constraints specified 
    1261  * by $in_same_cat or $excluded_categories. 
     1267 * by $in_same_term or $excluded_terms. 
    12621268 * 
    12631269 * @since 2.8.0 
    12641270 * 
    1265  * @param bool $in_same_cat Optional. Whether returned post should be in a same category. 
    1266  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1271 * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term. 
     1272 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
    12671273 * @param bool $start Optional. Whether to retrieve first or last post. 
     1274 * @param $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    12681275 * @return object 
    12691276 */ 
    1270 function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) { 
     1277function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) { 
    12711278        global $post; 
    12721279 
    1273         if ( empty($post) || ! is_single() || is_attachment() ) 
     1280        if ( empty( $post ) || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) 
    12741281                return null; 
    12751282 
    1276         $cat_array = array(); 
    1277         if( ! is_array( $excluded_categories ) ) 
    1278                 $excluded_categories = explode( ',', $excluded_categories ); 
     1283        $query_args = array( 
     1284                'posts_per_page' => 1, 
     1285                'order' => $start ? 'ASC' : 'DESC', 
     1286                'update_post_term_cache' => false, 
     1287                'update_post_meta_cache' => false 
     1288        ); 
    12791289 
    1280         if ( $in_same_cat || ! empty( $excluded_categories ) ) { 
    1281                 if ( $in_same_cat ) 
    1282                         $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) ); 
     1290        $term_array = array(); 
     1291         
     1292        if( ! is_array( $excluded_terms ) ) { 
     1293                if( ! empty( $excluded_terms ) ) 
     1294                        $excluded_terms = explode( ',', $excluded_terms ); 
     1295                else 
     1296                        $excluded_terms = array(); 
     1297        } 
    12831298 
    1284                 if ( ! empty( $excluded_categories ) ) { 
    1285                         $excluded_categories = array_map( 'intval', $excluded_categories ); 
    1286                         $excluded_categories = array_diff( $excluded_categories, $cat_array ); 
     1299        if ( $in_same_term || ! empty( $excluded_terms ) ) { 
     1300                if ( $in_same_term ) 
     1301                        $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 
    12871302 
    1288                         $inverse_cats = array(); 
    1289                         foreach ( $excluded_categories as $excluded_category ) 
    1290                                 $inverse_cats[] = $excluded_category * -1; 
    1291                         $excluded_categories = $inverse_cats; 
     1303                if ( ! empty( $excluded_terms ) ) { 
     1304                        $excluded_terms = array_map( 'intval', $excluded_terms ); 
     1305                        $excluded_terms = array_diff( $excluded_terms, $term_array ); 
     1306 
     1307                        $inverse_terms = array(); 
     1308                        foreach ( $excluded_terms as $excluded_term ) { 
     1309                                $inverse_terms[] = $excluded_term * -1; 
     1310                        } 
     1311                        $excluded_terms = $inverse_terms; 
    12921312                } 
     1313                 
     1314                $query_args[ 'tax_query' ] = array( array( 
     1315                        'taxonomy' => $taxonomy, 
     1316                        'terms' => array_merge( $term_array, $excluded_terms ) 
     1317                ) ); 
    12931318        } 
    12941319 
    1295         $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) ); 
    1296  
    1297         $order = $start ? 'ASC' : 'DESC'; 
    1298  
    1299         return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) ); 
     1320        return get_posts( $query_args ); 
    13001321} 
    13011322 
    13021323/** 
     
    13061327 * 
    13071328 * @param string $format Optional. Link anchor format. 
    13081329 * @param string $link Optional. Link permalink format. 
    1309  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1310  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1330 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1331 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1332 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    13111333 */ 
    1312 function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') { 
    1313         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true); 
     1334function previous_post_link( $format='&laquo; %link', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1335        adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy ); 
    13141336} 
    13151337 
    13161338/** 
     
    13201342 * 
    13211343 * @param string $format Optional. Link anchor format. 
    13221344 * @param string $link Optional. Link permalink format. 
    1323  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1324  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1345 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1346 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1347 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    13251348 */ 
    1326 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') { 
    1327         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false); 
     1349function next_post_link( $format='%link &raquo;', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1350        adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy ); 
    13281351} 
    13291352 
    13301353/** 
     
    13361359 * 
    13371360 * @param string $format Link anchor format. 
    13381361 * @param string $link Link permalink format. 
    1339  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1340  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1362 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1363 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs. 
    13411364 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. 
     1365 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    13421366 */ 
    1343 function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) { 
     1367function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
    13441368        if ( $previous && is_attachment() ) 
    13451369                $post = & get_post($GLOBALS['post']->post_parent); 
    13461370        else 
    1347                 $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous); 
     1371                $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
    13481372 
    13491373        if ( !$post ) 
    13501374                return;