WordPressを読む 29-2 /blog/wp-includes/theme.php 2
2014/12/12
目次
- 1 /blog/wp-includes/theme.php 2
- 2 関数 get_theme_root()
- 3 関数 get_theme_root_uri()
- 4 関数 get_raw_theme_root()
- 5 関数 locale_stylesheet()
- 6 関数 preview_theme()
- 7 関数 _preview_theme_template_filter()
- 8 関数 _preview_theme_stylesheet_filter()
- 9 関数 preview_theme_ob_filter()
- 10 関数 preview_theme_ob_filter_callback()
- 11 関数 switch_theme()
- 12 関数 validate_current_theme()
- 13 関数 get_theme_mods()
- 14 関数 get_theme_mod()
- 15 関数 set_theme_mod()
- 16 関数 remove_theme_mod()
- 17 関数 remove_theme_mods()
- 18 関数 get_header_textcolor()
- 19 関数 header_textcolor()
/blog/wp-includes/theme.php 2
関数 get_theme_root()
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 | /** * Retrieve path to themes directory. * * Does not have trailing slash. * * @since 1.5.0 * * @param string $stylesheet_or_template The stylesheet or template name of the theme * @return string Theme path. */ function get_theme_root( $stylesheet_or_template = false ) { global $wp_theme_directories; if ( $stylesheet_or_template && $theme_root = get_raw_theme_root( $stylesheet_or_template ) ) { // Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory. // This gives relative theme roots the benefit of the doubt when things go haywire. if ( ! in_array( $theme_root, (array) $wp_theme_directories ) ) $theme_root = WP_CONTENT_DIR . $theme_root; } else { $theme_root = WP_CONTENT_DIR . '/themes'; } /** * Filter the absolute path to the themes directory. * * @since 1.5.0 * * @param string $theme_root Absolute path to themes directory. */ return apply_filters( 'theme_root', $theme_root ); } |
関数 get_theme_root_uri()
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 | /** * Retrieve URI for themes directory. * * Does not have trailing slash. * * @since 1.5.0 * * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme. * Default is to leverage the main theme root. * @param string $theme_root Optional. The theme root for which calculations will be based, preventing * the need for a get_raw_theme_root() call. * @return string Themes URI. */ function get_theme_root_uri( $stylesheet_or_template = false, $theme_root = false ) { global $wp_theme_directories; if ( $stylesheet_or_template && ! $theme_root ) $theme_root = get_raw_theme_root( $stylesheet_or_template ); if ( $stylesheet_or_template && $theme_root ) { if ( in_array( $theme_root, (array) $wp_theme_directories ) ) { // Absolute path. Make an educated guess. YMMV -- but note the filter below. if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) ) $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) ); elseif ( 0 === strpos( $theme_root, ABSPATH ) ) $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) ); elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) ) $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root ); else $theme_root_uri = $theme_root; } else { $theme_root_uri = content_url( $theme_root ); } } else { $theme_root_uri = content_url( 'themes' ); } /** * Filter the URI for themes directory. * * @since 1.5.0 * * @param string $theme_root_uri The URI for themes directory. * @param string $siteurl WordPress web address which is set in General Options. * @param string $stylesheet_or_template Stylesheet or template name of the theme. */ return apply_filters( 'theme_root_uri', $theme_root_uri, get_option( 'siteurl' ), $stylesheet_or_template ); } |
関数 get_raw_theme_root()
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 | /** * Get the raw theme root relative to the content directory with no filters applied. * * @since 3.1.0 * * @param string $stylesheet_or_template The stylesheet or template name of the theme * @param bool $skip_cache Optional. Whether to skip the cache. Defaults to false, meaning the cache is used. * @return string Theme root */ function get_raw_theme_root( $stylesheet_or_template, $skip_cache = false ) { global $wp_theme_directories; if ( count($wp_theme_directories) <= 1 ) return '/themes'; $theme_root = false; // If requesting the root for the current theme, consult options to avoid calling get_theme_roots() if ( ! $skip_cache ) { if ( get_option('stylesheet') == $stylesheet_or_template ) $theme_root = get_option('stylesheet_root'); elseif ( get_option('template') == $stylesheet_or_template ) $theme_root = get_option('template_root'); } if ( empty($theme_root) ) { $theme_roots = get_theme_roots(); if ( !empty($theme_roots[$stylesheet_or_template]) ) $theme_root = $theme_roots[$stylesheet_or_template]; } return $theme_root; } |
関数 locale_stylesheet()
1 2 3 4 5 6 7 8 9 10 11 | /** * Display localized stylesheet link element. * * @since 2.1.0 */ function locale_stylesheet() { $stylesheet = get_locale_stylesheet_uri(); if ( empty($stylesheet) ) return; echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />'; } |
関数 preview_theme()
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 | /** * Start preview theme output buffer. * * Will only perform task if the user has permissions and template and preview * query variables exist. * * @since 2.6.0 */ function preview_theme() { if ( ! (isset($_GET['template']) && isset($_GET['preview'])) ) return; if ( !current_user_can( 'switch_themes' ) ) return; // Admin Thickbox requests if ( isset( $_GET['preview_iframe'] ) ) show_admin_bar( false ); $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']); if ( validate_file($_GET['template']) ) return; add_filter( 'template', '_preview_theme_template_filter' ); if ( isset($_GET['stylesheet']) ) { $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']); if ( validate_file($_GET['stylesheet']) ) return; add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' ); } // Prevent theme mods to current theme being used on theme being previewed add_filter( 'pre_option_theme_mods_' . get_option( 'stylesheet' ), '__return_empty_array' ); ob_start( 'preview_theme_ob_filter' ); } add_action('setup_theme', 'preview_theme'); |
関数 _preview_theme_template_filter()
1 2 3 4 5 6 7 8 9 10 11 | /** * Private function to modify the current template when previewing a theme * * @since 2.9.0 * @access private * * @return string */ function _preview_theme_template_filter() { return isset($_GET['template']) ? $_GET['template'] : ''; } |
関数 _preview_theme_stylesheet_filter()
1 2 3 4 5 6 7 8 9 10 11 | /** * Private function to modify the current stylesheet when previewing a theme * * @since 2.9.0 * @access private * * @return string */ function _preview_theme_stylesheet_filter() { return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : ''; } |
関数 preview_theme_ob_filter()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Callback function for ob_start() to capture all links in the theme. * * @since 2.6.0 * @access private * * @param string $content * @return string */ function preview_theme_ob_filter( $content ) { return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content ); } |
関数 preview_theme_ob_filter_callback()
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 | /** * Manipulates preview theme links in order to control and maintain location. * * Callback function for preg_replace_callback() to accept and filter matches. * * @since 2.6.0 * @access private * * @param array $matches * @return string */ function preview_theme_ob_filter_callback( $matches ) { if ( strpos($matches[4], 'onclick') !== false ) $matches[4] = preg_replace('#onclick=([\'"]).*?(?<!\\\)\\1#i', '', $matches[4]); //Strip out any onclicks from rest of <a>. (?<!\\\) means to ignore the '" if it's escaped by \ to prevent breaking mid-attribute. if ( ( false !== strpos($matches[3], '/wp-admin/') ) || ( false !== strpos( $matches[3], '://' ) && 0 !== strpos( $matches[3], home_url() ) ) || ( false !== strpos($matches[3], '/feed/') ) || ( false !== strpos($matches[3], '/trackback/') ) ) return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4]; $stylesheet = isset( $_GET['stylesheet'] ) ? $_GET['stylesheet'] : ''; $template = isset( $_GET['template'] ) ? $_GET['template'] : ''; $link = add_query_arg( array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => 1 ), $matches[3] ); if ( 0 === strpos($link, 'preview=1') ) $link = "?$link"; return $matches[1] . esc_attr( $link ) . $matches[4]; } |
関数 switch_theme()
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 | /** * Switches the theme. * * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature * of two arguments: $template then $stylesheet. This is for backwards compatibility. * * @since 2.5.0 * * @param string $stylesheet Stylesheet name */ function switch_theme( $stylesheet ) { global $wp_theme_directories, $wp_customize, $sidebars_widgets; $_sidebars_widgets = null; if ( 'wp_ajax_customize_save' === current_action() ) { $_sidebars_widgets = $wp_customize->post_value( $wp_customize->get_setting( 'old_sidebars_widgets_data' ) ); } elseif ( is_array( $sidebars_widgets ) ) { $_sidebars_widgets = $sidebars_widgets; } if ( is_array( $_sidebars_widgets ) ) { set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $_sidebars_widgets ) ); } $old_theme = wp_get_theme(); $new_theme = wp_get_theme( $stylesheet ); if ( func_num_args() > 1 ) { $template = $stylesheet; $stylesheet = func_get_arg( 1 ); } else { $template = $new_theme->get_template(); } update_option( 'template', $template ); update_option( 'stylesheet', $stylesheet ); if ( count( $wp_theme_directories ) > 1 ) { update_option( 'template_root', get_raw_theme_root( $template, true ) ); update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) ); } else { delete_option( 'template_root' ); delete_option( 'stylesheet_root' ); } $new_name = $new_theme->get('Name'); update_option( 'current_theme', $new_name ); // Migrate from the old mods_{name} option to theme_mods_{slug}. if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) { $default_theme_mods = (array) get_option( 'mods_' . $new_name ); add_option( "theme_mods_$stylesheet", $default_theme_mods ); } else { /* * Since retrieve_widgets() is called when initializing the customizer theme, * we need to to remove the theme mods to avoid overwriting changes made via * the widget customizer when accessing wp-admin/widgets.php. */ if ( 'wp_ajax_customize_save' === current_action() ) { remove_theme_mod( 'sidebars_widgets' ); } } update_option( 'theme_switched', $old_theme->get_stylesheet() ); /** * Fires after the theme is switched. * * @since 1.5.0 * * @param string $new_name Name of the new theme. * @param WP_Theme $new_theme WP_Theme instance of the new theme. */ do_action( 'switch_theme', $new_name, $new_theme ); } |
関数 validate_current_theme()
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 | /** * Checks that current theme files 'index.php' and 'style.css' exists. * * Does not check the default theme, which is the fallback and should always exist. * Will switch theme to the fallback theme if current theme does not validate. * You can use the 'validate_current_theme' filter to return false to * disable this functionality. * * @since 1.5.0 * @see WP_DEFAULT_THEME * * @return bool */ function validate_current_theme() { /** * Filter whether to validate the current theme. * * @since 2.7.0 * * @param bool true Validation flag to check the current theme. */ if ( defined('WP_INSTALLING') || ! apply_filters( 'validate_current_theme', true ) ) return true; if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) { switch_theme( WP_DEFAULT_THEME ); return false; } if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) { switch_theme( WP_DEFAULT_THEME ); return false; } if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) { switch_theme( WP_DEFAULT_THEME ); return false; } return true; } |
関数 get_theme_mods()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Retrieve all theme modifications. * * @since 3.1.0 * * @return array Theme modifications. */ function get_theme_mods() { $theme_slug = get_option( 'stylesheet' ); if ( false === ( $mods = get_option( "theme_mods_$theme_slug" ) ) ) { $theme_name = get_option( 'current_theme' ); if ( false === $theme_name ) $theme_name = wp_get_theme()->get('Name'); $mods = get_option( "mods_$theme_name" ); // Deprecated location. if ( is_admin() && false !== $mods ) { update_option( "theme_mods_$theme_slug", $mods ); delete_option( "mods_$theme_name" ); } } return $mods; } |
関数 get_theme_mod()
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 | /** * Retrieve theme modification value for the current theme. * * If the modification name does not exist, then the $default will be passed * through {@link http://php.net/sprintf sprintf()} PHP function with the first * string the template directory URI and the second string the stylesheet * directory URI. * * @since 2.1.0 * * @param string $name Theme modification name. * @param bool|string $default * @return string */ function get_theme_mod( $name, $default = false ) { $mods = get_theme_mods(); if ( isset( $mods[$name] ) ) { /** * Filter the theme modification, or 'theme_mod', value. * * The dynamic portion of the hook name, $name, refers to * the key name of the modification array. For example, * 'header_textcolor', 'header_image', and so on depending * on the theme options. * * @since 2.2.0 * * @param string $current_mod The value of the current theme modification. */ return apply_filters( "theme_mod_{$name}", $mods[$name] ); } if ( is_string( $default ) ) $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); /** This filter is documented in wp-includes/theme.php */ return apply_filters( "theme_mod_{$name}", $default ); } |
関数 set_theme_mod()
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 | /** * Update theme modification value for the current theme. * * @since 2.1.0 * * @param string $name Theme modification name. * @param string $value theme modification value. */ function set_theme_mod( $name, $value ) { $mods = get_theme_mods(); $old_value = isset( $mods[ $name ] ) ? $mods[ $name ] : false; /** * Filter the theme mod value on save. * * The dynamic portion of the hook name, $name, refers to the key name of * the modification array. For example, 'header_textcolor', 'header_image', * and so on depending on the theme options. * * @since 3.9.0 * * @param string $value The new value of the theme mod. * @param string $old_value The current value of the theme mod. */ $mods[ $name ] = apply_filters( "pre_set_theme_mod_$name", $value, $old_value ); $theme = get_option( 'stylesheet' ); update_option( "theme_mods_$theme", $mods ); } |
関数 remove_theme_mod()
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 | /** * Remove theme modification name from current theme list. * * If removing the name also removes all elements, then the entire option will * be removed. * * @since 2.1.0 * * @param string $name Theme modification name. * @return null */ function remove_theme_mod( $name ) { $mods = get_theme_mods(); if ( ! isset( $mods[ $name ] ) ) return; unset( $mods[ $name ] ); if ( empty( $mods ) ) return remove_theme_mods(); $theme = get_option( 'stylesheet' ); update_option( "theme_mods_$theme", $mods ); } |
関数 remove_theme_mods()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Remove theme modifications option for current theme. * * @since 2.1.0 */ function remove_theme_mods() { delete_option( 'theme_mods_' . get_option( 'stylesheet' ) ); // Old style. $theme_name = get_option( 'current_theme' ); if ( false === $theme_name ) $theme_name = wp_get_theme()->get('Name'); delete_option( 'mods_' . $theme_name ); } |
関数 get_header_textcolor()
1 2 3 4 5 6 7 8 9 10 | /** * Retrieve text color for custom header. * * @since 2.1.0 * * @return string */ function get_header_textcolor() { return get_theme_mod('header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) ); } |
関数 header_textcolor()
1 2 3 4 5 6 7 8 | /** * Display text color for custom header. * * @since 2.1.0 */ function header_textcolor() { echo get_header_textcolor(); } |