WordPress.org

Make WordPress Core

Ticket #17807: 17807.5.patch

File 17807.5.patch, 19.6 KB (added by DrewAPicture, 5 years ago)

Refresh at r24075

  • wp-includes/link-template.php

     
    10871087 *
    10881088 * @since 1.5.0
    10891089 *
    1090  * @param bool $in_same_cat Optional. Whether post should be in a same category.
    1091  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1090 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
     1091 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1092 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true.
    10921093 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    10931094 */
    1094 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
    1095         return get_adjacent_post($in_same_cat, $excluded_categories);
     1095function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1096        return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
    10961097}
    10971098
    10981099/**
     
    11001101 *
    11011102 * @since 1.5.0
    11021103 *
    1103  * @param bool $in_same_cat Optional. Whether post should be in a same category.
    1104  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1104 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
     1105 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1106 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true.
    11051107 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    11061108 */
    1107 function get_next_post($in_same_cat = false, $excluded_categories = '') {
    1108         return get_adjacent_post($in_same_cat, $excluded_categories, false);
     1109function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1110        return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
    11091111}
    11101112
    11111113/**
     
    11151117 *
    11161118 * @since 2.5.0
    11171119 *
    1118  * @param bool $in_same_cat Optional. Whether post should be in a same category.
    1119  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1120 * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
     1121 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
    11201122 * @param bool $previous Optional. Whether to retrieve previous post.
     1123 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true.
    11211124 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    11221125 */
    1123 function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) {
     1126function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    11241127        global $wpdb;
    11251128
    1126         if ( ! $post = get_post() )
     1129        if ( empty( $post ) || ! taxonomy_exists( $taxonomy ) )
    11271130                return null;
    11281131
    11291132        $current_post_date = $post->post_date;
    11301133
    11311134        $join = '';
    1132         $posts_in_ex_cats_sql = '';
    1133         if ( $in_same_cat || ! empty( $excluded_categories ) ) {
     1135        $posts_in_ex_terms_sql = '';
     1136        if ( $in_same_term || ! empty( $excluded_terms ) ) {
    11341137                $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";
    11351138
    1136                 if ( $in_same_cat ) {
    1137                         if ( ! is_object_in_taxonomy( $post->post_type, 'category' ) )
     1139                if ( $in_same_term ) {
     1140                        if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
    11381141                                return '';
    1139                         $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
    1140                         if ( ! $cat_array || is_wp_error( $cat_array ) )
     1142                        $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
     1143                        if ( ! $term_array || is_wp_error( $term_array ) )
    11411144                                return '';
    1142                         $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
     1145                        $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
    11431146                }
    11441147
    1145                 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
    1146                 if ( ! empty( $excluded_categories ) ) {
    1147                         if ( ! is_array( $excluded_categories ) ) {
    1148                                 // back-compat, $excluded_categories used to be IDs separated by " and "
    1149                                 if ( strpos( $excluded_categories, ' and ' ) !== false ) {
    1150                                         _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );
    1151                                         $excluded_categories = explode( ' and ', $excluded_categories );
     1148                $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
     1149                if ( ! empty( $excluded_terms ) ) {
     1150                        if ( ! is_array( $excluded_terms ) ) {
     1151                                // back-compat, $excluded_terms used to be $excluded_categories with IDs separated by " and "
     1152                                if ( false !== strpos( $excluded_terms, ' and ' ) ) {
     1153                                        _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
     1154                                        $excluded_terms = explode( ' and ', $excluded_terms );
    11521155                                } else {
    1153                                         $excluded_categories = explode( ',', $excluded_categories );
     1156                                        $excluded_terms = explode( ',', $excluded_terms );
    11541157                                }
    11551158                        }
    11561159
    1157                         $excluded_categories = array_map( 'intval', $excluded_categories );
     1160                        $excluded_terms = array_map( 'intval', $excluded_terms );
    11581161
    1159                         if ( ! empty( $cat_array ) ) {
    1160                                 $excluded_categories = array_diff($excluded_categories, $cat_array);
    1161                                 $posts_in_ex_cats_sql = '';
     1162                        if ( ! empty( $term_array ) ) {
     1163                                $excluded_terms = array_diff( $excluded_terms, $term_array );
     1164                                $posts_in_ex_terms_sql = '';
    11621165                        }
    11631166
    1164                         if ( !empty($excluded_categories) ) {
    1165                                 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
     1167                        if ( ! empty( $excluded_terms ) ) {
     1168                                $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy );
    11661169                        }
    11671170                }
    11681171        }
     
    11711174        $op = $previous ? '<' : '>';
    11721175        $order = $previous ? 'DESC' : 'ASC';
    11731176
    1174         $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
    1175         $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 );
     1177        $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
     1178        $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 );
    11761179        $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
    11771180
    11781181        $query = "SELECT p.id FROM $wpdb->posts AS p $join $where $sort";
    1179         $query_key = 'adjacent_post_' . md5($query);
    1180         $result = wp_cache_get($query_key, 'counts');
     1182        $query_key = 'adjacent_post_' . md5( $query );
     1183        $result = wp_cache_get( $query_key, 'counts' );
    11811184        if ( false !== $result ) {
    11821185                if ( $result )
    11831186                        $result = get_post( $result );
    11841187                return $result;
    11851188        }
    11861189
    1187         $result = $wpdb->get_var( $query );
     1190        $result = $wpdb->get_row( "SELECT p.* FROM $wpdb->posts AS p $join $where $sort" );
    11881191        if ( null === $result )
    11891192                $result = '';
    11901193
     
    12041207 * @since 2.8.0
    12051208 *
    12061209 * @param string $title Optional. Link title format.
    1207  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1208  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1210 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
     1211 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
    12091212 * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
     1213 * @param string $taxonony Options, default is category. Taxonomy, if $in_same_term is true.
    12101214 * @return string
    12111215 */
    1212 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
     1216function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    12131217        if ( $previous && is_attachment() && $post = get_post() )
    12141218                $post = get_post( $post->post_parent );
    12151219        else
    1216                 $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
     1220                $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
    12171221
    12181222        if ( empty($post) )
    12191223                return;
    12201224
    1221         if ( empty($post->post_title) )
     1225        if ( empty( $post->post_title ) )
    12221226                $post_title = $previous ? __('Previous Post') : __('Next Post');
    12231227        else
    12241228                $post_title = $post->post_title;
    12251229
    1226         $date = mysql2date(get_option('date_format'), $post->post_date);
     1230        $date = mysql2date( get_option( 'date_format' ), $post->post_date );
    12271231
    1228         $title = str_replace('%title', $post_title, $title);
    1229         $title = str_replace('%date', $date, $title);
    1230         $title = apply_filters('the_title', $title, $post->ID);
     1232        $title = str_replace( '%title', $post->post_title, $title );
     1233        $title = str_replace( '%date', $date, $title );
     1234        $title = apply_filters( 'the_title', $title, $post->ID );
    12311235
    12321236        $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
    12331237        $link .= esc_attr( $title );
    1234         $link .= "' href='" . get_permalink($post) . "' />\n";
     1238        $link .= "' href='" . get_permalink( $post ) . "' />\n";
    12351239
    12361240        $adjacent = $previous ? 'previous' : 'next';
    12371241        return apply_filters( "{$adjacent}_post_rel_link", $link );
     
    12431247 * @since 2.8.0
    12441248 *
    12451249 * @param string $title Optional. Link title format.
    1246  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1247  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1250 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
     1251 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1252 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true.
    12481253 */
    1249 function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1250         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
    1251         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
     1254function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1255        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy );
     1256        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy );
    12521257}
    12531258
    12541259/**
     
    12701275 * @since 2.8.0
    12711276 *
    12721277 * @param string $title Optional. Link title format.
    1273  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1274  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1278 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
     1279 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1280 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true.
    12751281 */
    1276 function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1277         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
     1282function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1283        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy );
    12781284}
    12791285
    12801286/**
     
    12821288 *
    12831289 * @since 2.8.0
    12841290 *
    1285  * @param string $title Optional. Link title format.
    1286  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1287  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1291 * @param string $title (optional) Link title format.
     1292 * @param bool $in_same_term (optional) Whether link should be in a same taxonomy term.
     1293 * @param array|string $excluded_terms (optional) Array or comma-separated list of excluded term IDs.
     1294 * @param string $taxonomy (optional) Default is category. Taxonomy, if $in_same_term is true.
    12881295 */
    1289 function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1290         echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
     1296function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1297        echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy );
    12911298}
    12921299
    12931300/**
    12941301 * Retrieve boundary post.
    12951302 *
    12961303 * Boundary being either the first or last post by publish date within the constraints specified
    1297  * by $in_same_cat or $excluded_categories.
     1304 * by $in_same_term or $excluded_terms.
    12981305 *
    12991306 * @since 2.8.0
    13001307 *
    1301  * @param bool $in_same_cat Optional. Whether returned post should be in a same category.
    1302  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1303  * @param bool $start Optional. Whether to retrieve first or last post.
     1308 * @param bool $in_same_term (optional) Whether returned post should be in a same taxonomy term.
     1309 * @param array|string $excluded_terms (optional) Array or comma-separated list of excluded term IDs.
     1310 * @param bool $start (optional) Whether to retrieve first or last post.
     1311 * @param $taxonomy (optional) Default is category. Taxonomy, if $in_same_term is true.
    13041312 * @return object
    13051313 */
    1306 function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {
     1314function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
    13071315        $post = get_post();
    1308         if ( ! $post || ! is_single() || is_attachment() )
     1316        if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
    13091317                return null;
    13101318
    1311         $cat_array = array();
    1312         if( ! is_array( $excluded_categories ) )
    1313                 $excluded_categories = explode( ',', $excluded_categories );
     1319        $query_args = array(
     1320                'posts_per_page' => 1,
     1321                'order' => $start ? 'ASC' : 'DESC',
     1322                'update_post_term_cache' => false,
     1323                'update_post_meta_cache' => false
     1324        );
    13141325
    1315         if ( $in_same_cat || ! empty( $excluded_categories ) ) {
    1316                 if ( $in_same_cat )
    1317                         $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
     1326        $term_array = array();
     1327        if ( ! is_array( $excluded_terms ) )
     1328                if ( ! empty( $excluded_terms ) )
     1329                        $excluded_terms = explode( ',', $excluded_terms );
     1330                else
     1331                        $excluded_terms = array();
    13181332
    1319                 if ( ! empty( $excluded_categories ) ) {
    1320                         $excluded_categories = array_map( 'intval', $excluded_categories );
    1321                         $excluded_categories = array_diff( $excluded_categories, $cat_array );
     1333        if ( $in_same_term || ! empty( $excluded_terms ) ) {
     1334                if ( $in_same_term )
     1335                        $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    13221336
    1323                         $inverse_cats = array();
    1324                         foreach ( $excluded_categories as $excluded_category )
    1325                                 $inverse_cats[] = $excluded_category * -1;
    1326                         $excluded_categories = $inverse_cats;
     1337                if ( ! empty( $excluded_terms ) ) {
     1338                        $excluded_terms = array_map( 'intval', $excluded_terms );
     1339                        $excluded_terms = array_diff( $excluded_terms, $term_array );
     1340
     1341                        $inverse_terms = array();
     1342                        foreach ( $excluded_terms as $excluded_term )
     1343                                $inverse_terms[] = $excluded_term * -1;
     1344                        $excluded_terms = $inverse_terms;
    13271345                }
     1346
     1347                $query_args[ 'tax_query' ] = array( array(
     1348                        'taxonomy' => $taxonomy,
     1349                        'terms' => array_merge( $term_array, $excluded_terms )
     1350                ) );
    13281351        }
    13291352
    1330         $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) );
    1331 
    1332         $order = $start ? 'ASC' : 'DESC';
    1333 
    1334         return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) );
     1353        return get_posts( $query_args );
    13351354}
    13361355
    13371356/**
     
    13411360 *
    13421361 * @param string $format Optional. Link anchor format.
    13431362 * @param string $link Optional. Link permalink format.
    1344  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1345  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1363 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
     1364 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1365 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true.
    13461366 */
    1347 function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    1348         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
     1367function previous_post_link( $format='&laquo; %link', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1368        adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
    13491369}
    13501370
    13511371/**
     
    13551375 *
    13561376 * @param string $format Optional. Link anchor format.
    13571377 * @param string $link Optional. Link permalink format.
    1358  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1359  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1378 * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
     1379 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1380 * @param string $taxonomy Optional, default is category. Taxonomy, if $in_same_term is true.
    13601381 */
    1361 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    1362         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
     1382function next_post_link( $format='%link &raquo;', $link='%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1383        adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
    13631384}
    13641385
    13651386/**
     
    13711392 *
    13721393 * @param string $format Link anchor format.
    13731394 * @param string $link Link permalink format.
    1374  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1375  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
     1395 * @param bool $in_same_term (optional) Whether link should be in a same taxonomy term.
     1396 * @param array|string $excluded_terms (optional) Array or comma-separated list of excluded terms IDs.
    13761397 * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
     1398 * @param string $taxonomy (optional) Default is category. Taxonomy, if $in_same_term is true.
    13771399 */
    1378 function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
     1400function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    13791401        if ( $previous && is_attachment() )
    13801402                $post = get_post( get_post()->post_parent );
    13811403        else
    1382                 $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
     1404                $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
    13831405
    13841406        if ( ! $post ) {
    13851407                $output = '';