WordPressを読む 29-4 /blog/wp-includes/theme.php 4
2014/12/12
目次
- 1 /blog/wp-includes/theme.php 4
- 2 関数 add_theme_support()
- 3 関数 _custom_header_background_just_in_time()
- 4 関数 get_theme_support()
- 5 関数 remove_theme_support()
- 6 関数 _remove_theme_support()
- 7 関数 current_theme_supports()
- 8 関数 require_if_theme_supports()
- 9 関数 _delete_attachment_theme_mod()
- 10 関数 check_theme_switched()
- 11 関数 _wp_customize_include()
- 12 関数 _wp_customize_loader_settings()
- 13 関数 wp_customize_url()
- 14 関数 wp_customize_support_script()
- 15 関数 is_customize_preview()
/blog/wp-includes/theme.php 4
関数 add_theme_support()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | /** * Allows a theme to register its support of a certain feature * * Must be called in the theme's functions.php file to work. * If attached to a hook, it must be after_setup_theme. * The init hook may be too late for some features. * * @since 2.9.0 * * @param string $feature The feature being added. * @return void|bool False on failure, void otherwise. */ function add_theme_support( $feature ) { global $_wp_theme_features; if ( func_num_args() == 1 ) $args = true; else $args = array_slice( func_get_args(), 1 ); switch ( $feature ) { case 'post-formats' : if ( is_array( $args[0] ) ) $args[0] = array_intersect( $args[0], array_keys( get_post_format_slugs() ) ); break; case 'html5' : // You can't just pass 'html5', you need to pass an array of types. if ( empty( $args[0] ) ) { // Build an array of types for back-compat. $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) ); } elseif ( ! is_array( $args[0] ) ) { _doing_it_wrong( "add_theme_support( 'html5' )", 'You need to pass an array of types.', '3.6.1' ); return false; } // Calling 'html5' again merges, rather than overwrites. if ( isset( $_wp_theme_features['html5'] ) ) $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] ); break; case 'custom-header-uploads' : return add_theme_support( 'custom-header', array( 'uploads' => true ) ); case 'custom-header' : if ( ! is_array( $args ) ) $args = array( 0 => array() ); $defaults = array( 'default-image' => '', 'random-default' => false, 'width' => 0, 'height' => 0, 'flex-height' => false, 'flex-width' => false, 'default-text-color' => '', 'header-text' => true, 'uploads' => true, 'wp-head-callback' => '', 'admin-head-callback' => '', 'admin-preview-callback' => '', ); $jit = isset( $args[0]['__jit'] ); unset( $args[0]['__jit'] ); // Merge in data from previous add_theme_support() calls. // The first value registered wins. (A child theme is set up first.) if ( isset( $_wp_theme_features['custom-header'] ) ) $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] ); // Load in the defaults at the end, as we need to insure first one wins. // This will cause all constants to be defined, as each arg will then be set to the default. if ( $jit ) $args[0] = wp_parse_args( $args[0], $defaults ); // If a constant was defined, use that value. Otherwise, define the constant to ensure // the constant is always accurate (and is not defined later, overriding our value). // As stated above, the first value wins. // Once we get to wp_loaded (just-in-time), define any constants we haven't already. // Constants are lame. Don't reference them. This is just for backwards compatibility. if ( defined( 'NO_HEADER_TEXT' ) ) $args[0]['header-text'] = ! NO_HEADER_TEXT; elseif ( isset( $args[0]['header-text'] ) ) define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) ); if ( defined( 'HEADER_IMAGE_WIDTH' ) ) $args[0]['width'] = (int) HEADER_IMAGE_WIDTH; elseif ( isset( $args[0]['width'] ) ) define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] ); if ( defined( 'HEADER_IMAGE_HEIGHT' ) ) $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT; elseif ( isset( $args[0]['height'] ) ) define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] ); if ( defined( 'HEADER_TEXTCOLOR' ) ) $args[0]['default-text-color'] = HEADER_TEXTCOLOR; elseif ( isset( $args[0]['default-text-color'] ) ) define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] ); if ( defined( 'HEADER_IMAGE' ) ) $args[0]['default-image'] = HEADER_IMAGE; elseif ( isset( $args[0]['default-image'] ) ) define( 'HEADER_IMAGE', $args[0]['default-image'] ); if ( $jit && ! empty( $args[0]['default-image'] ) ) $args[0]['random-default'] = false; // If headers are supported, and we still don't have a defined width or height, // we have implicit flex sizes. if ( $jit ) { if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) ) $args[0]['flex-width'] = true; if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) ) $args[0]['flex-height'] = true; } break; case 'custom-background' : if ( ! is_array( $args ) ) $args = array( 0 => array() ); $defaults = array( 'default-image' => '', 'default-repeat' => 'repeat', 'default-position-x' => 'left', 'default-attachment' => 'scroll', 'default-color' => '', 'wp-head-callback' => '_custom_background_cb', 'admin-head-callback' => '', 'admin-preview-callback' => '', ); $jit = isset( $args[0]['__jit'] ); unset( $args[0]['__jit'] ); // Merge in data from previous add_theme_support() calls. The first value registered wins. if ( isset( $_wp_theme_features['custom-background'] ) ) $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] ); if ( $jit ) $args[0] = wp_parse_args( $args[0], $defaults ); if ( defined( 'BACKGROUND_COLOR' ) ) $args[0]['default-color'] = BACKGROUND_COLOR; elseif ( isset( $args[0]['default-color'] ) || $jit ) define( 'BACKGROUND_COLOR', $args[0]['default-color'] ); if ( defined( 'BACKGROUND_IMAGE' ) ) $args[0]['default-image'] = BACKGROUND_IMAGE; elseif ( isset( $args[0]['default-image'] ) || $jit ) define( 'BACKGROUND_IMAGE', $args[0]['default-image'] ); break; } $_wp_theme_features[ $feature ] = $args; } |
関数 _custom_header_background_just_in_time()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /** * Registers the internal custom header and background routines. * * @since 3.4.0 * @access private */ function _custom_header_background_just_in_time() { global $custom_image_header, $custom_background; if ( current_theme_supports( 'custom-header' ) ) { // In case any constants were defined after an add_custom_image_header() call, re-run. add_theme_support( 'custom-header', array( '__jit' => true ) ); $args = get_theme_support( 'custom-header' ); if ( $args[0]['wp-head-callback'] ) add_action( 'wp_head', $args[0]['wp-head-callback'] ); if ( is_admin() ) { require_once( ABSPATH . 'wp-admin/custom-header.php' ); $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] ); } } if ( current_theme_supports( 'custom-background' ) ) { // In case any constants were defined after an add_custom_background() call, re-run. add_theme_support( 'custom-background', array( '__jit' => true ) ); $args = get_theme_support( 'custom-background' ); add_action( 'wp_head', $args[0]['wp-head-callback'] ); if ( is_admin() ) { require_once( ABSPATH . 'wp-admin/custom-background.php' ); $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] ); } } } |
1 | add_action( 'wp_loaded', '_custom_header_background_just_in_time' ); |
関数 get_theme_support()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /** * Gets the theme support arguments passed when registering that support * * @since 3.1.0 * * @param string $feature the feature to check * @return array The array of extra arguments */ function get_theme_support( $feature ) { global $_wp_theme_features; if ( ! isset( $_wp_theme_features[ $feature ] ) ) return false; if ( func_num_args() <= 1 ) return $_wp_theme_features[ $feature ]; $args = array_slice( func_get_args(), 1 ); switch ( $feature ) { case 'custom-header' : case 'custom-background' : if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) ) return $_wp_theme_features[ $feature ][0][ $args[0] ]; return false; default : return $_wp_theme_features[ $feature ]; } } |
関数 remove_theme_support()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Allows a theme to de-register its support of a certain feature * * Should be called in the theme's functions.php file. Generally would * be used for child themes to override support from the parent theme. * * @since 3.0.0 * @see add_theme_support() * @param string $feature the feature being added * @return bool Whether feature was removed. */ function remove_theme_support( $feature ) { // Blacklist: for internal registrations not used directly by themes. if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ) ) ) return false; return _remove_theme_support( $feature ); } |
関数 _remove_theme_support()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /** * Do not use. Removes theme support internally, ignorant of the blacklist. * * @access private * @since 3.1.0 */ function _remove_theme_support( $feature ) { global $_wp_theme_features; switch ( $feature ) { case 'custom-header-uploads' : if ( ! isset( $_wp_theme_features['custom-header'] ) ) return false; add_theme_support( 'custom-header', array( 'uploads' => false ) ); return; // Do not continue - custom-header-uploads no longer exists. } if ( ! isset( $_wp_theme_features[ $feature ] ) ) return false; switch ( $feature ) { case 'custom-header' : if ( ! did_action( 'wp_loaded' ) ) break; $support = get_theme_support( 'custom-header' ); if ( $support[0]['wp-head-callback'] ) remove_action( 'wp_head', $support[0]['wp-head-callback'] ); remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) ); unset( $GLOBALS['custom_image_header'] ); break; case 'custom-background' : if ( ! did_action( 'wp_loaded' ) ) break; $support = get_theme_support( 'custom-background' ); remove_action( 'wp_head', $support[0]['wp-head-callback'] ); remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) ); unset( $GLOBALS['custom_background'] ); break; } unset( $_wp_theme_features[ $feature ] ); return true; } |
関数 current_theme_supports()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | /** * Checks a theme's support for a given feature * * @since 2.9.0 * @param string $feature the feature being checked * @return boolean */ function current_theme_supports( $feature ) { global $_wp_theme_features; if ( 'custom-header-uploads' == $feature ) return current_theme_supports( 'custom-header', 'uploads' ); if ( !isset( $_wp_theme_features[$feature] ) ) return false; // If no args passed then no extra checks need be performed if ( func_num_args() <= 1 ) return true; $args = array_slice( func_get_args(), 1 ); switch ( $feature ) { case 'post-thumbnails': // post-thumbnails can be registered for only certain content/post types by passing // an array of types to add_theme_support(). If no array was passed, then // any type is accepted if ( true === $_wp_theme_features[$feature] ) // Registered for all types return true; $content_type = $args[0]; return in_array( $content_type, $_wp_theme_features[$feature][0] ); case 'html5': case 'post-formats': // specific post formats can be registered by passing an array of types to // add_theme_support() // Specific areas of HTML5 support *must* be passed via an array to add_theme_support() $type = $args[0]; return in_array( $type, $_wp_theme_features[$feature][0] ); case 'custom-header': case 'custom-background' : // specific custom header and background capabilities can be registered by passing // an array to add_theme_support() $header_support = $args[0]; return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] ); } /** * Filter whether the current theme supports a specific feature. * * The dynamic portion of the hook name, $feature, refers to * the specific theme feature. Possible values include 'post-formats', * 'post-thumbnails', 'custom-background', 'custom-header', 'menus', * 'automatic-feed-links', and 'html5'. * * @since 3.4.0 * * @param bool true Whether the current theme supports the given feature. Default true. * @param array $args Array of arguments for the feature. * @param string $feature The theme feature. */ return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[$feature] ); } |
関数 require_if_theme_supports()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Checks a theme's support for a given feature before loading the functions which implement it. * * @since 2.9.0 * * @param string $feature The feature being checked. * @param string $include Path to the file. * @return bool True if the current theme supports the supplied feature, false otherwise. */ function require_if_theme_supports( $feature, $include ) { if ( current_theme_supports( $feature ) ) { require ( $include ); return true; } return false; } |
関数 _delete_attachment_theme_mod()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Checks an attachment being deleted to see if it's a header or background image. * * If true it removes the theme modification which would be pointing at the deleted * attachment * * @access private * @since 3.0.0 * @param int $id the attachment id */ function _delete_attachment_theme_mod( $id ) { $attachment_image = wp_get_attachment_url( $id ); $header_image = get_header_image(); $background_image = get_background_image(); if ( $header_image && $header_image == $attachment_image ) remove_theme_mod( 'header_image' ); if ( $background_image && $background_image == $attachment_image ) remove_theme_mod( 'background_image' ); } |
1 | add_action( 'delete_attachment', '_delete_attachment_theme_mod' ); |
関数 check_theme_switched()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load * * @since 3.3.0 */ function check_theme_switched() { if ( $stylesheet = get_option( 'theme_switched' ) ) { $old_theme = wp_get_theme( $stylesheet ); // Prevent retrieve_widgets() from running since Customizer already called it up front if ( get_option( 'theme_switched_via_customizer' ) ) { remove_action( 'after_switch_theme', '_wp_sidebars_changed' ); update_option( 'theme_switched_via_customizer', false ); } if ( $old_theme->exists() ) { /** * Fires on the first WP load after a theme switch if the old theme still exists. * * This action fires multiple times and the parameters differs * according to the context, if the old theme exists or not. * If the old theme is missing, the parameter will be the slug * of the old theme. * * @since 3.3.0 * * @param string $old_name Old theme name. * @param WP_Theme $old_theme WP_Theme instance of the old theme. */ do_action( 'after_switch_theme', $old_theme->get( 'Name' ), $old_theme ); } else { /** This action is documented in wp-includes/theme.php */ do_action( 'after_switch_theme', $stylesheet ); } update_option( 'theme_switched', false ); } } |
関数 _wp_customize_include()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * Includes and instantiates the WP_Customize_Manager class. * * Fires when ?wp_customize=on or on wp-admin/customize.php. * * @since 3.4.0 */ function _wp_customize_include() { if ( ! ( ( isset( $_REQUEST['wp_customize'] ) && 'on' == $_REQUEST['wp_customize'] ) || ( is_admin() && 'customize.php' == basename( $_SERVER['PHP_SELF'] ) ) ) ) return; require( ABSPATH . WPINC . '/class-wp-customize-manager.php' ); // Init Customize class $GLOBALS['wp_customize'] = new WP_Customize_Manager; } |
1 | add_action( 'plugins_loaded', '_wp_customize_include' ); |
関数 _wp_customize_loader_settings()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * Adds settings for the customize-loader script. * * @since 3.4.0 */ function _wp_customize_loader_settings() { global $wp_scripts; $admin_origin = parse_url( admin_url() ); $home_origin = parse_url( home_url() ); $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) ); $browser = array( 'mobile' => wp_is_mobile(), 'ios' => wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] ), ); $settings = array( 'url' => esc_url( admin_url( 'customize.php' ) ), 'isCrossDomain' => $cross_domain, 'browser' => $browser, 'l10n' => array( 'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ), ), ); $script = 'var _wpCustomizeLoaderSettings = ' . json_encode( $settings ) . ';'; $data = $wp_scripts->get_data( 'customize-loader', 'data' ); if ( $data ) $script = "$data\n$script"; $wp_scripts->add_data( 'customize-loader', 'data', $script ); } |
1 | add_action( 'admin_enqueue_scripts', '_wp_customize_loader_settings' ); |
関数 wp_customize_url()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Returns a URL to load the theme customizer. * * @since 3.4.0 * * @param string $stylesheet Optional. Theme to customize. Defaults to current theme. * The theme's stylesheet will be urlencoded if necessary. */ function wp_customize_url( $stylesheet = null ) { $url = admin_url( 'customize.php' ); if ( $stylesheet ) $url .= '?theme=' . urlencode( $stylesheet ); return esc_url( $url ); } |
関数 wp_customize_support_script()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * Prints a script to check whether or not the customizer is supported, * and apply either the no-customize-support or customize-support class * to the body. * * This function MUST be called inside the body tag. * * Ideally, call this function immediately after the body tag is opened. * This prevents a flash of unstyled content. * * It is also recommended that you add the "no-customize-support" class * to the body tag by default. * * @since 3.4.0 */ function wp_customize_support_script() { $admin_origin = parse_url( admin_url() ); $home_origin = parse_url( home_url() ); $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) ); ?> <script type="text/javascript"> (function() { var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)'); <?php if ( $cross_domain ): ?> request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })(); <?php else: ?> request = true; <?php endif; ?> b[c] = b[c].replace( rcs, ' ' ); b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs; }()); </script> <?php } |
関数 is_customize_preview()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Whether the site is being previewed in the Customizer. * * @since 4.0.0 * * @global WP_Customize_Manager $wp_customize Customizer instance. * * @return bool True if the site is being previewed in the Customizer, false otherwise. */ function is_customize_preview() { global $wp_customize; return is_a( $wp_customize, 'WP_Customize_Manager' ) && $wp_customize->is_preview(); } |