Make WordPress Core


Ignore:
Timestamp:
03/29/2014 10:52:17 AM (12 years ago)
Author:
nacin
Message:

Custom Headers: Simplify and consolidate the querying of custom headers for the customizer.

props mcsf.
see #21785.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-control.php

    r27816 r27849  
    756756    }
    757757
    758     public function get_default_header_images() {
    759         global $custom_image_header;
    760 
    761         // Get *the* default image if there is one
    762         $default = get_theme_support( 'custom-header', 'default-image' );
    763 
    764         if ( ! $default ) { // If not,
    765             return $custom_image_header->default_headers; // easy peasy.
    766         }
    767 
    768         $default = sprintf( $default,
    769             get_template_directory_uri(),
    770             get_stylesheet_directory_uri() );
    771 
    772         $header_images = array();
    773         $already_has_default = false;
    774 
    775         // Get the whole set of default images
    776         $default_header_images = $custom_image_header->default_headers;
    777         foreach ( $default_header_images as $k => $h ) {
    778             if ( $h['url'] == $default ) {
    779                 $already_has_default = true;
    780                 break;
    781             }
    782         }
    783 
    784         // If *the one true image* isn't included in the default set, add it in
    785         // first position
    786         if ( ! $already_has_default ) {
    787             $header_images['default'] = array(
    788                 'url' => $default,
    789                 'thumbnail_url' => $default,
    790                 'description' => 'Default'
    791             );
    792         }
    793 
    794         // The rest of the set comes after
    795         $header_images = array_merge( $header_images, $default_header_images );
    796 
    797         return $header_images;
    798     }
    799 
    800     public function get_uploaded_header_images() {
    801         $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
    802         $header_images = array();
    803 
    804         $headers_not_dated = get_posts( array(
    805             'post_type' => 'attachment',
    806             'meta_key' => '_wp_attachment_is_custom_header',
    807             'meta_value' => get_option('stylesheet'),
    808             'orderby' => 'none',
    809             'nopaging' => true,
    810             'meta_query' => array(
    811                 array(
    812                     'key' => '_wp_attachment_is_custom_header',
    813                     'value' => get_option( 'stylesheet' ),
    814                     'compare' => 'LIKE'
    815                 ),
    816                 array(
    817                     'key' => $key,
    818                     'value' => 'this string must not be empty',
    819                     'compare' => 'NOT EXISTS'
    820                 ),
    821             )
    822         ) );
    823 
    824         $headers_dated = get_posts( array(
    825             'post_type' => 'attachment',
    826             'meta_key' => $key,
    827             'orderby' => 'meta_value_num',
    828             'order' => 'DESC',
    829             'nopaging' => true,
    830             'meta_query' => array(
    831                 array(
    832                     'key' => '_wp_attachment_is_custom_header',
    833                     'value' => get_option( 'stylesheet' ),
    834                     'compare' => 'LIKE'
    835                 ),
    836             ),
    837         ) );
    838 
    839         $limit = apply_filters( 'custom_header_uploaded_limit', 15 );
    840         $headers = array_merge( $headers_dated, $headers_not_dated );
    841         $headers = array_slice( $headers, 0, $limit );
    842 
    843         foreach ( (array) $headers as $header ) {
    844             $url = esc_url_raw( $header->guid );
    845             $header_data = wp_get_attachment_metadata( $header->ID );
    846             $timestamp = get_post_meta( $header->ID,
    847                 '_wp_attachment_custom_header_last_used_' . get_stylesheet(),
    848                 true );
    849 
    850             $h = array(
    851                 'attachment_id' => $header->ID,
    852                 'url'           => $url,
    853                 'thumbnail_url' => $url,
    854                 'timestamp'     => $timestamp ? $timestamp : 0,
    855             );
    856 
    857             if ( isset( $header_data['width'] ) ) {
    858                 $h['width'] = $header_data['width'];
    859             }
    860             if ( isset( $header_data['height'] ) ) {
    861                 $h['height'] = $header_data['height'];
    862             }
    863 
    864             $header_images[] = $h;
    865         }
    866 
    867         return $header_images;
    868     }
    869 
    870758    public function prepare_control() {
    871759        global $custom_image_header;
     
    876764        // Process default headers and uploaded headers.
    877765        $custom_image_header->process_default_headers();
    878         $this->default_headers = $this->get_default_header_images();
    879         $this->uploaded_headers = $this->get_uploaded_header_images();
     766        $this->default_headers = $custom_image_header->get_default_header_images();
     767        $this->uploaded_headers = $custom_image_header->get_uploaded_header_images();
    880768    }
    881769
Note: See TracChangeset for help on using the changeset viewer.