WordPress.org

Make WordPress Core

Changeset 32994


Ignore:
Timestamp:
06/29/2015 12:57:35 PM (3 years ago)
Author:
obenland
Message:

Introducing Site Icon, favicon management for WordPress.

This v1 marries Jetpack's Site Icon module with the Media Modal, reusing code
from the Custom Header admin. For now, the core-provided icons will be limited
to a favicon, an iOS app icon, and a Windows tile icon, leaving .ico support
and additional icons to plugins to add.

Props obenland, tyxla, flixos90, jancbeck, markjaquith, scruffian.
See #16434.

Location:
trunk/src
Files:
5 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/css/wp-admin.css

    r31534 r32994  
    1111@import url(nav-menus.css);
    1212@import url(widgets.css);
     13@import url(site-icon.css);
    1314@import url(l10n.css);
  • trunk/src/wp-admin/includes/admin.php

    r32653 r32994  
    6565require_once(ABSPATH . 'wp-admin/includes/user.php');
    6666
     67/** WordPress Site Icon API */
     68require_once(ABSPATH . 'wp-admin/includes/site-icon.php');
     69
    6770/** WordPress Update Administration API */
    6871require_once(ABSPATH . 'wp-admin/includes/update.php');
  • trunk/src/wp-admin/includes/template.php

    r32964 r32994  
    17791779    }
    17801780
     1781    if ( $post->ID == get_option( 'site_icon' ) ) {
     1782        $media_states[] = __( 'Site Icon' );
     1783    }
     1784
    17811785    /**
    17821786     * Filter the default media display states for items in the Media list table.
     
    17851789     *
    17861790     * @param array $media_states An array of media states. Default 'Header Image',
    1787      *                            'Background Image'.
     1791     *                            'Background Image', 'Site Icon'.
    17881792     */
    17891793    $media_states = apply_filters( 'display_media_states', $media_states );
  • trunk/src/wp-admin/options-general.php

    r32974 r32994  
    124124<td><input name="blogdescription" type="text" id="blogdescription" aria-describedby="tagline-description" value="<?php form_option('blogdescription'); ?>" class="regular-text" />
    125125<p class="description" id="tagline-description"><?php _e( 'In a few words, explain what this site is about.' ) ?></p></td>
     126</tr>
     127<tr>
     128<th scope="row"><label for="site_icon"><?php _e( 'Site Icon' ); ?></label></th>
     129<td>
     130    <?php
     131    $upload_url = admin_url( 'options-general.php?page=site-icon' );
     132    $update_url = esc_url( add_query_arg( array(
     133        'page'   => 'site-icon',
     134        'action' => 'crop_site_icon',
     135    ), wp_nonce_url( admin_url( 'options-general.php' ), 'crop-site-icon' ) ) );
     136
     137    wp_enqueue_media();
     138    wp_enqueue_script( 'site-icon' );
     139
     140    if ( has_site_icon() ) :
     141        $remove_url = add_query_arg( array(
     142            'action' => 'remove_site_icon',
     143        ), wp_nonce_url( admin_url( 'options-general.php' ), 'remove-site-icon' ) );
     144    ?>
     145
     146    <img class="avatar avatar-150" src="<?php site_icon_url( null, 150 ); ?>" height="150" width="150" />
     147    <p class="hide-if-no-js">
     148        <label class="screen-reader-text" for="choose-from-library-link"><?php _e( 'Choose an image from your media library:' ); ?></label>
     149        <button type="button" id="choose-from-library-link" class="button" data-update-link="<?php echo esc_attr( $update_url ); ?>" data-choose="<?php esc_attr_e( 'Choose a Site Icon' ); ?>" data-update="<?php esc_attr_e( 'Set as Site Icon' ); ?>"><?php _e( 'Update Site Icon' ); ?></button>
     150        <a href="<?php echo esc_url( $remove_url ); ?>"><?php _e( 'Remove Site Icon' ); ?></a>
     151    </p>
     152    <p class="hide-if-js">
     153        <a href="<?php echo esc_url( $upload_url ); ?>" class="button"><?php _e( 'Update Site Icon' ); ?></a>
     154        <a href="<?php echo esc_url( $remove_url ); ?>"><?php _e( 'Remove Site Icon' ); ?></a>
     155    </p>
     156
     157    <?php else : ?>
     158
     159    <p class="hide-if-no-js">
     160        <label class="screen-reader-text" for="choose-from-library-link"><?php _e( 'Choose an image from your media library:' ); ?></label>
     161        <button type="button" id="choose-from-library-link" class="button" data-update-link="<?php echo esc_attr( $update_url ); ?>" data-choose="<?php esc_attr_e( 'Choose a Site Icon' ); ?>" data-update="<?php esc_attr_e( 'Set as Site Icon' ); ?>"><?php _e( 'Choose Image' ); ?></button>
     162    </p>
     163    <a class="button hide-if-js" href="<?php echo esc_url( $upload_url ); ?>"><?php _e( 'Add a Site Icon' ); ?></a>
     164
     165    <?php endif; ?>
     166    <p class="description"><?php _e( 'Site Icon creates a favicon and app icons for your site.' ) ?></p>
     167</td>
    126168</tr>
    127169<?php if ( !is_multisite() ) { ?>
  • trunk/src/wp-admin/options.php

    r32974 r32994  
    7373
    7474$whitelist_options = array(
    75     'general' => array( 'blogname', 'blogdescription', 'gmt_offset', 'date_format', 'time_format', 'start_of_week', 'timezone_string', 'WPLANG' ),
     75    'general' => array( 'blogname', 'blogdescription', 'site_icon', 'gmt_offset', 'date_format', 'time_format', 'start_of_week', 'timezone_string', 'WPLANG' ),
    7676    'discussion' => array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'avatar_default', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page', 'default_comments_page', 'comment_order', 'comment_registration' ),
    7777    'media' => array( 'thumbnail_size_w', 'thumbnail_size_h', 'thumbnail_crop', 'medium_size_w', 'medium_size_h', 'large_size_w', 'large_size_h', 'image_default_size', 'image_default_align', 'image_default_link_type' ),
  • trunk/src/wp-includes/default-filters.php

    r32899 r32994  
    223223add_action( 'wp_head',             'wp_generator'                           );
    224224add_action( 'wp_head',             'rel_canonical'                          );
     225add_action( 'wp_head',             'wp_shortlink_wp_head',            10, 0 );
     226add_action( 'wp_head',             'wp_site_icon',                    99    );
    225227add_action( 'wp_footer',           'wp_print_footer_scripts',         20    );
    226 add_action( 'wp_head',             'wp_shortlink_wp_head',            10, 0 );
    227228add_action( 'template_redirect',   'wp_shortlink_header',             11, 0 );
    228229add_action( 'wp_print_footer_scripts', '_wp_footer_scripts'                 );
     
    243244    add_action( $action, 'the_generator' );
    244245}
     246
     247// Feed Site Icon
     248add_action( 'atom_head', 'atom_site_icon' );
     249add_action( 'rss2_head', 'rss2_site_icon' );
     250
    245251
    246252// WP Cron
  • trunk/src/wp-includes/feed.php

    r32940 r32994  
    589589
    590590/**
     591 * Display Site Icon in atom feeds.
     592 *
     593 * @since 4.3.0
     594 */
     595function atom_site_icon() {
     596    $url = get_site_icon_url( null, 32 );
     597    if ( $url ) {
     598        echo "<icon>$url</icon>\n";
     599    }
     600}
     601
     602/**
     603 * Display Site Icon in RSS2.
     604 *
     605 * @since 4.3.0
     606 */
     607function rss2_site_icon() {
     608    $rss_title = get_wp_title_rss();
     609    if ( empty( $rss_title ) ) {
     610        $rss_title = get_bloginfo_rss( 'name' );
     611    }
     612
     613    $url = get_site_icon_url( null, 32 );
     614    if ( $url ) {
     615        echo '
     616<image>
     617    <url>' . convert_chars( $url ) . '</url>
     618    <title>' . $rss_title . '</title>
     619    <link>' . get_bloginfo_rss( 'url' ) . '</link>
     620    <width>32</width>
     621    <height>32</height>
     622</image> ' . "\n";
     623    }
     624}
     625
     626/**
    591627 * Display the link for the currently displayed feed in a XSS safe way.
    592628 *
  • trunk/src/wp-includes/formatting.php

    r32935 r32994  
    34223422        case 'users_can_register':
    34233423        case 'start_of_week':
     3424        case 'site_icon':
    34243425            $value = absint( $value );
    34253426            break;
  • trunk/src/wp-includes/general-template.php

    r32935 r32994  
    720720
    721721    return $output;
     722}
     723
     724/**
     725 * Returns the Site Icon URL.
     726 *
     727 * @param  null|int $blog_id Id of the blog to get the site icon for.
     728 * @param  int      $size    Size of the site icon.
     729 * @param  string   $url     Fallback url if no site icon is found.
     730 * @return string            Site Icon URL.
     731 */
     732function get_site_icon_url( $blog_id = null, $size = 512, $url = '' ) {
     733    if ( function_exists( 'get_blog_option' ) ) {
     734        if ( ! is_int( $blog_id ) ) {
     735            $blog_id = get_current_blog_id();
     736        }
     737        $site_icon_id = get_blog_option( $blog_id, 'site_icon' );
     738    } else {
     739        $site_icon_id = get_option( 'site_icon' );
     740    }
     741
     742    if ( $site_icon_id  ) {
     743        if ( $size >= 512 ) {
     744            $size_data = 'full';
     745        } else {
     746            $size_data = array( $size, $size );
     747        }
     748        $url_data = wp_get_attachment_image_src( $site_icon_id, $size_data );
     749        $url = $url_data[0];
     750    }
     751
     752    return $url;
     753}
     754
     755/**
     756 * Displays the Site Icon URL.
     757 *
     758 * @param null|int $blog_id Id of the blog to get the site icon for.
     759 * @param int      $size    Size of the site icon.
     760 * @param string   $url     Fallback url if no site icon is found.
     761 */
     762function site_icon_url( $blog_id = null, $size = 512, $url = '' ) {
     763    echo esc_url( get_site_icon_url( $blog_id, $size, $url ) );
     764}
     765
     766/**
     767 * Whether the site has a Site Icon.
     768 *
     769 * @param int|null $blog_id Optional. Blog ID. Default: Current blog.
     770 * @return bool
     771 */
     772function has_site_icon( $blog_id = null ) {
     773    return !! get_site_icon_url( $blog_id, 512 );
    722774}
    723775
     
    23872439
    23882440/**
     2441 * Display site icon meta tags.
     2442 *
     2443 * @since 4.3.0
     2444 *
     2445 * @link http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#rel-icon HTML5 specification link icon.
     2446 */
     2447function wp_site_icon() {
     2448    if ( ! has_site_icon() ) {
     2449        return;
     2450    }
     2451
     2452    $meta_tags = array(
     2453        sprintf( '<link rel="icon" href="%s" sizes="32x32" />', esc_url( get_site_icon_url( null, 32 ) ) ),
     2454        sprintf( '<link rel="apple-touch-icon-precomposed" href="%s">', esc_url( get_site_icon_url( null, 180 ) ) ),
     2455        sprintf( '<meta name="msapplication-TileImage" content="%s">', esc_url( get_site_icon_url( null, 270 ) ) ),
     2456    );
     2457
     2458    /**
     2459     * Filters the site icon meta tags, so Plugins can add their own.
     2460     *
     2461     * @since 4.3.0
     2462     *
     2463     * @param array $meta_tags Site Icon meta elements.
     2464     */
     2465    $meta_tags = apply_filters( 'site_icon_meta_tags', $meta_tags );
     2466    $meta_tags = array_filter( $meta_tags );
     2467
     2468    foreach ( $meta_tags as $meta_tag ) {
     2469        echo "$meta_tag\n";
     2470    }
     2471}
     2472
     2473/**
    23892474 * Whether the user should have a WYSIWIG editor.
    23902475 *
  • trunk/src/wp-includes/script-loader.php

    r32978 r32994  
    613613        ) );
    614614
     615        $scripts->add( 'site-icon', '/wp-admin/js/site-icon.js', array( 'jquery' ), false, 1 );
     616        $scripts->add( 'site-icon-crop', '/wp-admin/js/site-icon-crop.js', array( 'jcrop' ), false, 1 );
     617
    615618        $scripts->add( 'custom-header', "/wp-admin/js/custom-header.js", array( 'jquery-masonry' ), false, 1 );
    616619        $scripts->add( 'custom-background', "/wp-admin/js/custom-background$suffix.js", array( 'wp-color-picker', 'media-views' ), false, 1 );
Note: See TracChangeset for help on using the changeset viewer.