WordPress.org

Make WordPress Core

Ticket #17673: 17673.2.diff

File 17673.2.diff, 9.7 KB (added by solarissmoke, 10 years ago)

Accept arrays as well as comma-separated strings

  • wp-includes/link-template.php

     
    10651065 * @since 1.5.0
    10661066 *
    10671067 * @param bool $in_same_cat Optional. Whether post should be in same category.
    1068  * @param string $excluded_categories Optional. Excluded categories IDs.
     1068 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    10691069 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    10701070 */
    10711071function get_previous_post($in_same_cat = false, $excluded_categories = '') {
     
    10781078 * @since 1.5.0
    10791079 *
    10801080 * @param bool $in_same_cat Optional. Whether post should be in same category.
    1081  * @param string $excluded_categories Optional. Excluded categories IDs.
     1081 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    10821082 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    10831083 */
    10841084function get_next_post($in_same_cat = false, $excluded_categories = '') {
     
    10931093 * @since 2.5.0
    10941094 *
    10951095 * @param bool $in_same_cat Optional. Whether post should be in same category.
    1096  * @param string $excluded_categories Optional. Excluded categories IDs.
     1096 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    10971097 * @param bool $previous Optional. Whether to retrieve previous post.
    10981098 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    10991099 */
     
    11071107
    11081108        $join = '';
    11091109        $posts_in_ex_cats_sql = '';
    1110         if ( $in_same_cat || !empty($excluded_categories) ) {
     1110        if ( $in_same_cat || ! empty( $excluded_categories ) ) {
    11111111                $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";
    11121112
    11131113                if ( $in_same_cat ) {
     
    11161116                }
    11171117
    11181118                $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
    1119                 if ( !empty($excluded_categories) ) {
    1120                         $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
    1121                         if ( !empty($cat_array) ) {
     1119                if ( ! empty( $excluded_categories ) ) {
     1120                        if( is_string( $excluded_categories ) ) {
     1121                                // back-compat, $excluded_categories used to be IDs separated by " and "
     1122                                if( strpos( $excluded_categories, ' and ' ) !== false )
     1123                                        $excluded_categories = explode( ' and ', $excluded_categories );
     1124                                else
     1125                                        $excluded_categories = explode( ',', $excluded_categories );
     1126                        }
     1127
     1128                        $excluded_categories = array_map( 'intval', $excluded_categories );
     1129                               
     1130                        if ( ! empty( $cat_array ) ) {
    11221131                                $excluded_categories = array_diff($excluded_categories, $cat_array);
    11231132                                $posts_in_ex_cats_sql = '';
    11241133                        }
     
    11601169 *
    11611170 * @param string $title Optional. Link title format.
    11621171 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1163  * @param string $excluded_categories Optional. Excluded categories IDs.
     1172 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    11641173 * @param bool $previous Optional, default is true. Whether display link to previous post.
    11651174 * @return string
    11661175 */
     
    11971206 *
    11981207 * @param string $title Optional. Link title format.
    11991208 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1200  * @param string $excluded_categories Optional. Excluded categories IDs.
     1209 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    12011210 */
    12021211function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    12031212        echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
     
    12241233 *
    12251234 * @param string $title Optional. Link title format.
    12261235 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1227  * @param string $excluded_categories Optional. Excluded categories IDs.
     1236 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    12281237 */
    12291238function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    12301239        echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
     
    12371246 *
    12381247 * @param string $title Optional. Link title format.
    12391248 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1240  * @param string $excluded_categories Optional. Excluded categories IDs.
     1249 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    12411250 */
    12421251function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    12431252        echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
     
    12521261 * @since 2.8.0
    12531262 *
    12541263 * @param bool $in_same_cat Optional. Whether returned post should be in same category.
    1255  * @param string $excluded_categories Optional. Excluded categories IDs.
     1264 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    12561265 * @param bool $start Optional. Whether to retrieve first or last post.
    12571266 * @return object
    12581267 */
    1259 function get_boundary_post($in_same_cat = false, $excluded_categories = '', $start = true) {
     1268function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {
    12601269        global $post;
    12611270
    12621271        if ( empty($post) || !is_single() || is_attachment() )
    12631272                return null;
    12641273
    12651274        $cat_array = array();
    1266         $excluded_categories = array();
    1267         if ( !empty($in_same_cat) || !empty($excluded_categories) ) {
    1268                 if ( !empty($in_same_cat) ) {
    1269                         $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
    1270                 }
     1275        if( is_string( $excluded_categories ) )
     1276                $excluded_categories = explode( ',', $excluded_categories );
     1277               
     1278        if ( $in_same_cat || ! empty( $excluded_categories ) ) {
     1279                if ( $in_same_cat )
     1280                        $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
    12711281
    1272                 if ( !empty($excluded_categories) ) {
    1273                         $excluded_categories = array_map('intval', explode(',', $excluded_categories));
     1282                if ( ! empty( $excluded_categories ) ) {
     1283                        $excluded_categories = array_map( 'intval', $excluded_categories );
     1284                        $excluded_categories = array_diff( $excluded_categories, $cat_array );
    12741285
    1275                         if ( !empty($cat_array) )
    1276                                 $excluded_categories = array_diff($excluded_categories, $cat_array);
    1277 
    12781286                        $inverse_cats = array();
    1279                         foreach ( $excluded_categories as $excluded_category)
     1287                        foreach ( $excluded_categories as $excluded_category )
    12801288                                $inverse_cats[] = $excluded_category * -1;
    12811289                        $excluded_categories = $inverse_cats;
    12821290                }
    12831291        }
    12841292
    1285         $categories = implode(',', array_merge($cat_array, $excluded_categories) );
     1293        $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) );
    12861294
    12871295        $order = $start ? 'ASC' : 'DESC';
    12881296
     
    12981306 *
    12991307 * @param string $title Optional. Link title format.
    13001308 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1301  * @param string $excluded_categories Optional. Excluded categories IDs.
     1309 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    13021310 * @param bool $start Optional, default is true. Whether display link to first or last post.
    13031311 * @return string
    13041312 */
     
    13351343 *
    13361344 * @param string $title Optional. Link title format.
    13371345 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1338  * @param string $excluded_categories Optional. Excluded categories IDs.
     1346 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    13391347 */
    13401348function start_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    13411349        echo get_boundary_post_rel_link($title, $in_same_cat, $excluded_categories, true);
     
    14071415 * @param string $format Optional. Link anchor format.
    14081416 * @param string $link Optional. Link permalink format.
    14091417 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1410  * @param string $excluded_categories Optional. Excluded categories IDs.
     1418 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    14111419 */
    14121420function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    14131421        adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
     
    14211429 * @param string $format Optional. Link anchor format.
    14221430 * @param string $link Optional. Link permalink format.
    14231431 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1424  * @param string $excluded_categories Optional. Excluded categories IDs.
     1432 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    14251433 */
    14261434function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
    14271435        adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
     
    14371445 * @param string $format Link anchor format.
    14381446 * @param string $link Link permalink format.
    14391447 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1440  * @param string $excluded_categories Optional. Excluded categories IDs.
     1448 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    14411449 * @param bool $previous Optional, default is true. Whether display link to previous post.
    14421450 */
    14431451function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {