Ticket #12726: post.patch

File post.patch, 4.6 KB (added by sorich87, 3 years ago)
  • wp-includes/post.php

     
    401401} 
    402402 
    403403/** 
     404 * Retrieves post data by a given field. 
     405 * 
     406 * See {@link &get_post()} for optional filter and output arguments values. 
     407 * 
     408 * @since 3.1.0 
     409 * 
     410 * @param string $field The field to retrieve the post with.  id | title | path 
     411 * @param int|string $value A value for $field.  A post ID, title, or path. 
     412 * @param string|array $args List of arguments to overwrite the defaults 
     413 *        string $post_type Optional. Post type. Default page. 
     414 *        string $output Optional. Output type. 
     415 *        string $filter How the return value should be filtered. 
     416 * @return mixed Null when complete. 
     417 */ 
     418function get_post_by($field, $value, $args = '') { 
     419        global $wpdb; 
     420 
     421        $defaults = array( 
     422                'post_type' => 'post', 'output' => 'OBJECT', 
     423                'filter' => 'raw' 
     424        ); 
     425 
     426        $r = wp_parse_args( $args, $defaults ); 
     427        extract( $r, EXTR_SKIP ); 
     428 
     429        switch ($field) { 
     430                case 'id': 
     431                        $p = get_post($value, $output, $filter); 
     432                        if ( $p->post_type == $post_type ) 
     433                                return $p; 
     434                        else 
     435                                return null; 
     436                        break; 
     437                         
     438                case 'title': 
     439                        $page_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $value, $post_type ) ); 
     440                        if ( $page_id ) 
     441                                return get_post($page_id, $output, $filter); 
     442                        else 
     443                                return null; 
     444                        break; 
     445                         
     446                case 'path': 
     447                        $page_path = rawurlencode(urldecode($value)); 
     448                        $page_path = str_replace('%2F', '/', $page_path); 
     449                        $page_path = str_replace('%20', ' ', $page_path); 
     450                        $page_paths = '/' . trim($page_path, '/'); 
     451                        $leaf_path  = sanitize_title(basename($page_paths)); 
     452                        $page_paths = explode('/', $page_paths); 
     453                        $full_path = ''; 
     454                        foreach ( (array) $page_paths as $pathdir ) 
     455                                $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir); 
     456 
     457                        $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = %s OR post_type = 'attachment')", $leaf_path, $post_type )); 
     458 
     459                        if ( empty($pages) ) 
     460                                return null; 
     461 
     462                        foreach ( $pages as $page ) { 
     463                                $path = '/' . $leaf_path; 
     464                                $curpage = $page; 
     465                                while ( $curpage->post_parent != 0 ) { 
     466                                        $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $curpage->post_parent, $post_type )); 
     467                                        $path = '/' . $curpage->post_name . $path; 
     468                                } 
     469 
     470                                if ( $path == $full_path ) 
     471                                        return get_post($page->ID, $output, $filter); 
     472                        } 
     473                        break; 
     474                         
     475                default: 
     476                        return false; 
     477        } 
     478         
     479} 
     480 
     481/** 
    404482 * Retrieve ancestors of a post. 
    405483 * 
    406484 * @since 2.5.0 
     
    28882966 * @return mixed Null when complete. 
    28892967 */ 
    28902968function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') { 
    2891         global $wpdb; 
    2892         $page_path = rawurlencode(urldecode($page_path)); 
    2893         $page_path = str_replace('%2F', '/', $page_path); 
    2894         $page_path = str_replace('%20', ' ', $page_path); 
    2895         $page_paths = '/' . trim($page_path, '/'); 
    2896         $leaf_path  = sanitize_title(basename($page_paths)); 
    2897         $page_paths = explode('/', $page_paths); 
    2898         $full_path = ''; 
    2899         foreach ( (array) $page_paths as $pathdir ) 
    2900                 $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir); 
    2901  
    2902         $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = %s OR post_type = 'attachment')", $leaf_path, $post_type )); 
    2903  
    2904         if ( empty($pages) ) 
    2905                 return null; 
    2906  
    2907         foreach ( $pages as $page ) { 
    2908                 $path = '/' . $leaf_path; 
    2909                 $curpage = $page; 
    2910                 while ( $curpage->post_parent != 0 ) { 
    2911                         $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $curpage->post_parent, $post_type )); 
    2912                         $path = '/' . $curpage->post_name . $path; 
    2913                 } 
    2914  
    2915                 if ( $path == $full_path ) 
    2916                         return get_page($page->ID, $output, $post_type); 
    2917         } 
    2918  
    2919         return null; 
     2969        $args = array( 
     2970                'output' => $output, 'post_type' => $post_type 
     2971        ); 
     2972        return get_post_by('path', $page_path, $args); 
    29202973} 
    29212974 
    29222975/** 
     
    29312984 * @return mixed 
    29322985 */ 
    29332986function get_page_by_title($page_title, $output = OBJECT, $post_type = 'page' ) { 
    2934         global $wpdb; 
    2935         $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type ) ); 
    2936         if ( $page ) 
    2937                 return get_page($page, $output); 
    2938  
    2939         return null; 
     2987        $args = array( 
     2988                'output' => $output, 'post_type' => $post_type 
     2989        ); 
     2990        return get_post_by('title', $page_title, $args); 
    29402991} 
    29412992 
    29422993/**