WordPress.org

Make WordPress Core

Ticket #17807: 17807.7.patch

File 17807.7.patch, 19.6 KB (added by ethitter, 4 years ago)

Refreshed at r24829

  • wp-includes/link-template.php

     
    10891089 * 
    10901090 * @since 1.5.0 
    10911091 * 
    1092  * @param bool $in_same_cat Optional. Whether post should be in a same category. 
    1093  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1092 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 
     1093 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1094 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    10941095 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 
    10951096 */ 
    1096 function get_previous_post($in_same_cat = false, $excluded_categories = '') { 
    1097         return get_adjacent_post($in_same_cat, $excluded_categories); 
     1097function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1098        return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy ); 
    10981099} 
    10991100 
    11001101/** 
     
    11021103 * 
    11031104 * @since 1.5.0 
    11041105 * 
    1105  * @param bool $in_same_cat Optional. Whether post should be in a same category. 
    1106  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1106 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 
     1107 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1108 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    11071109 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 
    11081110 */ 
    1109 function get_next_post($in_same_cat = false, $excluded_categories = '') { 
    1110         return get_adjacent_post($in_same_cat, $excluded_categories, false); 
     1111function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1112        return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy ); 
    11111113} 
    11121114 
    11131115/** 
     
    11171119 * 
    11181120 * @since 2.5.0 
    11191121 * 
    1120  * @param bool $in_same_cat Optional. Whether post should be in a same category. 
    1121  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1122 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. 
     1123 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
    11221124 * @param bool $previous Optional. Whether to retrieve previous post. 
     1125 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    11231126 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. 
    11241127 */ 
    1125 function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) { 
     1128function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
    11261129        global $wpdb; 
    11271130 
    1128         if ( ! $post = get_post() ) 
     1131        if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) ) 
    11291132                return null; 
    11301133 
    11311134        $current_post_date = $post->post_date; 
    11321135 
    11331136        $join = ''; 
    1134         $posts_in_ex_cats_sql = ''; 
    1135         if ( $in_same_cat || ! empty( $excluded_categories ) ) { 
     1137        $posts_in_ex_terms_sql = ''; 
     1138        if ( $in_same_term || ! empty( $excluded_terms ) ) { 
    11361139                $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"; 
    11371140 
    1138                 if ( $in_same_cat ) { 
    1139                         if ( ! is_object_in_taxonomy( $post->post_type, 'category' ) ) 
     1141                if ( $in_same_term ) { 
     1142                        if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) 
    11401143                                return ''; 
    1141                         $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids')); 
    1142                         if ( ! $cat_array || is_wp_error( $cat_array ) ) 
     1144                        $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 
     1145                        if ( ! $term_array || is_wp_error( $term_array ) ) 
    11431146                                return ''; 
    1144                         $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")"; 
     1147                        $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy ); 
    11451148                } 
    11461149 
    1147                 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'"; 
    1148                 if ( ! empty( $excluded_categories ) ) { 
    1149                         if ( ! is_array( $excluded_categories ) ) { 
    1150                                 // back-compat, $excluded_categories used to be IDs separated by " and " 
    1151                                 if ( strpos( $excluded_categories, ' and ' ) !== false ) { 
    1152                                         _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) ); 
    1153                                         $excluded_categories = explode( ' and ', $excluded_categories ); 
     1150                $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); 
     1151                if ( ! empty( $excluded_terms ) ) { 
     1152                        if ( ! is_array( $excluded_terms ) ) { 
     1153                                // back-compat, $excluded_terms used to be $excluded_categories with IDs separated by " and " 
     1154                                if ( false !== strpos( $excluded_terms, ' and ' ) ) { 
     1155                                        _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) ); 
     1156                                        $excluded_terms = explode( ' and ', $excluded_terms ); 
    11541157                                } else { 
    1155                                         $excluded_categories = explode( ',', $excluded_categories ); 
     1158                                        $excluded_terms = explode( ',', $excluded_terms ); 
    11561159                                } 
    11571160                        } 
    11581161 
    1159                         $excluded_categories = array_map( 'intval', $excluded_categories ); 
     1162                        $excluded_terms = array_map( 'intval', $excluded_terms ); 
    11601163 
    1161                         if ( ! empty( $cat_array ) ) { 
    1162                                 $excluded_categories = array_diff($excluded_categories, $cat_array); 
    1163                                 $posts_in_ex_cats_sql = ''; 
     1164                        if ( ! empty( $term_array ) ) { 
     1165                                $excluded_terms = array_diff( $excluded_terms, $term_array ); 
     1166                                $posts_in_ex_terms_sql = ''; 
    11641167                        } 
    11651168 
    1166                         if ( !empty($excluded_categories) ) { 
    1167                                 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; 
     1169                        if ( ! empty( $excluded_terms ) ) { 
     1170                                $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy ); 
    11681171                        } 
    11691172                } 
    11701173        } 
     
    11731176        $op = $previous ? '<' : '>'; 
    11741177        $order = $previous ? 'DESC' : 'ASC'; 
    11751178 
    1176         $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories ); 
    1177         $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 ); 
     1179        $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms ); 
     1180        $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 ); 
    11781181        $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); 
    11791182 
    11801183        $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort"; 
    1181         $query_key = 'adjacent_post_' . md5($query); 
    1182         $result = wp_cache_get($query_key, 'counts'); 
     1184        $query_key = 'adjacent_post_' . md5( $query ); 
     1185        $result = wp_cache_get( $query_key, 'counts' ); 
    11831186        if ( false !== $result ) { 
    11841187                if ( $result ) 
    11851188                        $result = get_post( $result ); 
     
    11901193        if ( null === $result ) 
    11911194                $result = ''; 
    11921195 
    1193         wp_cache_set($query_key, $result, 'counts'); 
     1196        wp_cache_set( $query_key, $result, 'counts' ); 
    11941197 
    11951198        if ( $result ) 
    11961199                $result = get_post( $result ); 
     
    12061209 * @since 2.8.0 
    12071210 * 
    12081211 * @param string $title Optional. Link title format. 
    1209  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1210  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1212 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1213 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
    12111214 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. 
     1215 * @param string $taxonony Options, default is category. Taxonomy, if $in_same_term is true. 
    12121216 * @return string 
    12131217 */ 
    1214 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) { 
     1218function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
    12151219        if ( $previous && is_attachment() && $post = get_post() ) 
    12161220                $post = get_post( $post->post_parent ); 
    12171221        else 
    1218                 $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous ); 
     1222                $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
    12191223 
    1220         if ( empty($post) ) 
     1224        if ( empty( $post ) ) 
    12211225                return; 
    12221226 
    12231227        $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) ); 
    12241228 
    12251229        if ( empty( $post_title ) ) 
    1226                 $post_title = $previous ? __('Previous Post') : __('Next Post'); 
     1230                $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); 
    12271231 
    1228         $date = mysql2date(get_option('date_format'), $post->post_date); 
     1232        $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 
    12291233 
    1230         $title = str_replace('%title', $post_title, $title); 
    1231         $title = str_replace('%date', $date, $title); 
     1234        $title = str_replace( '%title', $post_title, $title ); 
     1235        $title = str_replace( '%date', $date, $title ); 
    12321236 
    12331237        $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; 
    12341238        $link .= esc_attr( $title ); 
    1235         $link .= "' href='" . get_permalink($post) . "' />\n"; 
     1239        $link .= "' href='" . get_permalink( $post ) . "' />\n"; 
    12361240 
    12371241        $adjacent = $previous ? 'previous' : 'next'; 
    12381242        return apply_filters( "{$adjacent}_post_rel_link", $link ); 
     
    12441248 * @since 2.8.0 
    12451249 * 
    12461250 * @param string $title Optional. Link title format. 
    1247  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1248  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1251 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1252 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1253 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    12491254 */ 
    1250 function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { 
    1251         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true); 
    1252         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false); 
     1255function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1256        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy ); 
     1257        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy ); 
    12531258} 
    12541259 
    12551260/** 
     
    12711276 * @since 2.8.0 
    12721277 * 
    12731278 * @param string $title Optional. Link title format. 
    1274  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1275  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1279 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1280 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1281 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    12761282 */ 
    1277 function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { 
    1278         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false); 
     1283function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1284        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy ); 
    12791285} 
    12801286 
    12811287/** 
     
    12831289 * 
    12841290 * @since 2.8.0 
    12851291 * 
    1286  * @param string $title Optional. Link title format. 
    1287  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1288  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1292 * @param string $title (optional) Link title format. 
     1293 * @param bool $in_same_term (optional) Whether link should be in a same taxonomy term. 
     1294 * @param array|string $excluded_terms (optional) Array or comma-separated list of excluded term IDs. 
     1295 * @param string $taxonomy (optional) Default is category. Taxonomy, if $in_same_term is true. 
    12891296 */ 
    1290 function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') { 
    1291         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true); 
     1297function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1298        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy ); 
    12921299} 
    12931300 
    12941301/** 
    12951302 * Retrieve boundary post. 
    12961303 * 
    12971304 * Boundary being either the first or last post by publish date within the constraints specified 
    1298  * by $in_same_cat or $excluded_categories. 
     1305 * by $in_same_term or $excluded_terms. 
    12991306 * 
    13001307 * @since 2.8.0 
    13011308 * 
    1302  * @param bool $in_same_cat Optional. Whether returned post should be in a same category. 
    1303  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
    1304  * @param bool $start Optional. Whether to retrieve first or last post. 
     1309 * @param bool $in_same_term (optional) Whether returned post should be in a same taxonomy term. 
     1310 * @param array|string $excluded_terms (optional) Array or comma-separated list of excluded term IDs. 
     1311 * @param bool $start (optional) Whether to retrieve first or last post. 
     1312 * @param $taxonomy (optional) Default is category. Taxonomy, if $in_same_term is true. 
    13051313 * @return object 
    13061314 */ 
    1307 function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) { 
     1315function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) { 
    13081316        $post = get_post(); 
    1309         if ( ! $post || ! is_single() || is_attachment() ) 
     1317        if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) 
    13101318                return null; 
    13111319 
    1312         $cat_array = array(); 
    1313         if( ! is_array( $excluded_categories ) ) 
    1314                 $excluded_categories = explode( ',', $excluded_categories ); 
     1320        $query_args = array( 
     1321                'posts_per_page' => 1, 
     1322                'order' => $start ? 'ASC' : 'DESC', 
     1323                'update_post_term_cache' => false, 
     1324                'update_post_meta_cache' => false 
     1325        ); 
    13151326 
    1316         if ( $in_same_cat || ! empty( $excluded_categories ) ) { 
    1317                 if ( $in_same_cat ) 
    1318                         $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) ); 
     1327        $term_array = array(); 
     1328        if ( ! is_array( $excluded_terms ) ) 
     1329                if ( ! empty( $excluded_terms ) ) 
     1330                        $excluded_terms = explode( ',', $excluded_terms ); 
     1331                else 
     1332                        $excluded_terms = array(); 
    13191333 
    1320                 if ( ! empty( $excluded_categories ) ) { 
    1321                         $excluded_categories = array_map( 'intval', $excluded_categories ); 
    1322                         $excluded_categories = array_diff( $excluded_categories, $cat_array ); 
     1334        if ( $in_same_term || ! empty( $excluded_terms ) ) { 
     1335                if ( $in_same_term ) 
     1336                        $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 
    13231337 
    1324                         $inverse_cats = array(); 
    1325                         foreach ( $excluded_categories as $excluded_category ) 
    1326                                 $inverse_cats[] = $excluded_category * -1; 
    1327                         $excluded_categories = $inverse_cats; 
     1338                if ( ! empty( $excluded_terms ) ) { 
     1339                        $excluded_terms = array_map( 'intval', $excluded_terms ); 
     1340                        $excluded_terms = array_diff( $excluded_terms, $term_array ); 
     1341 
     1342                        $inverse_terms = array(); 
     1343                        foreach ( $excluded_terms as $excluded_term ) 
     1344                                $inverse_terms[] = $excluded_term * -1; 
     1345                        $excluded_terms = $inverse_terms; 
    13281346                } 
     1347 
     1348                $query_args[ 'tax_query' ] = array( array( 
     1349                        'taxonomy' => $taxonomy, 
     1350                        'terms' => array_merge( $term_array, $excluded_terms ) 
     1351                ) ); 
    13291352        } 
    13301353 
    1331         $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) ); 
    1332  
    1333         $order = $start ? 'ASC' : 'DESC'; 
    1334  
    1335         return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) ); 
     1354        return get_posts( $query_args ); 
    13361355} 
    13371356 
    13381357/** 
     
    13421361 * 
    13431362 * @param string $format Optional. Link anchor format. 
    13441363 * @param string $link Optional. Link permalink format. 
    1345  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1346  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1364 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1365 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1366 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    13471367 */ 
    1348 function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') { 
    1349         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true); 
     1368function previous_post_link( $format='&laquo; %link', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1369        adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy ); 
    13501370} 
    13511371 
    13521372/** 
     
    13561376 * 
    13571377 * @param string $format Optional. Link anchor format. 
    13581378 * @param string $link Optional. Link permalink format. 
    1359  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1360  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1379 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. 
     1380 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
     1381 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true. 
    13611382 */ 
    1362 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') { 
    1363         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false); 
     1383function next_post_link( $format='%link &raquo;', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
     1384        adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy ); 
    13641385} 
    13651386 
    13661387/** 
     
    13721393 * 
    13731394 * @param string $format Link anchor format. 
    13741395 * @param string $link Link permalink format. 
    1375  * @param bool $in_same_cat Optional. Whether link should be in a same category. 
    1376  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. 
     1396 * @param bool $in_same_term (optional) Whether link should be in a same taxonomy term. 
     1397 * @param array|string $excluded_terms (optional) Array or comma-separated list of excluded terms IDs. 
    13771398 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. 
     1399 * @param string $taxonomy (optional) Default is category. Taxonomy, if $in_same_term is true. 
    13781400 */ 
    1379 function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) { 
     1401function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
    13801402        if ( $previous && is_attachment() ) 
    13811403                $post = get_post( get_post()->post_parent ); 
    13821404        else 
    1383                 $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous ); 
     1405                $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
    13841406 
    13851407        if ( ! $post ) { 
    13861408                $output = '';