wp/wp-includes/class-wp-theme.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    41 		'default'        => 'WordPress Default',
    41 		'default'        => 'WordPress Default',
    42 		'twentyten'      => 'Twenty Ten',
    42 		'twentyten'      => 'Twenty Ten',
    43 		'twentyeleven'   => 'Twenty Eleven',
    43 		'twentyeleven'   => 'Twenty Eleven',
    44 		'twentytwelve'   => 'Twenty Twelve',
    44 		'twentytwelve'   => 'Twenty Twelve',
    45 		'twentythirteen' => 'Twenty Thirteen',
    45 		'twentythirteen' => 'Twenty Thirteen',
       
    46 		'twentyfourteen' => 'Twenty Fourteen',
       
    47 		'twentyfifteen'  => 'Twenty Fifteen',
       
    48 	);
       
    49 
       
    50 	/**
       
    51 	 * Renamed theme tags.
       
    52 	 */
       
    53 	private static $tag_map = array(
       
    54 		'fixed-width'    => 'fixed-layout',
       
    55 		'flexible-width' => 'fluid-layout',
    46 	);
    56 	);
    47 
    57 
    48 	/**
    58 	/**
    49 	 * Absolute path to the theme root, usually wp-content/themes
    59 	 * Absolute path to the theme root, usually wp-content/themes
    50 	 *
    60 	 *
   168 	public function __construct( $theme_dir, $theme_root, $_child = null ) {
   178 	public function __construct( $theme_dir, $theme_root, $_child = null ) {
   169 		global $wp_theme_directories;
   179 		global $wp_theme_directories;
   170 
   180 
   171 		// Initialize caching on first run.
   181 		// Initialize caching on first run.
   172 		if ( ! isset( self::$persistently_cache ) ) {
   182 		if ( ! isset( self::$persistently_cache ) ) {
       
   183 			/** This action is documented in wp-includes/theme.php */
   173 			self::$persistently_cache = apply_filters( 'wp_cache_themes_persistently', false, 'WP_Theme' );
   184 			self::$persistently_cache = apply_filters( 'wp_cache_themes_persistently', false, 'WP_Theme' );
   174 			if ( self::$persistently_cache ) {
   185 			if ( self::$persistently_cache ) {
   175 				wp_cache_add_global_groups( 'themes' );
   186 				wp_cache_add_global_groups( 'themes' );
   176 				if ( is_int( self::$persistently_cache ) )
   187 				if ( is_int( self::$persistently_cache ) )
   177 					self::$cache_expiration = self::$persistently_cache;
   188 					self::$cache_expiration = self::$persistently_cache;
   261 		}
   272 		}
   262 
   273 
   263 		// Set the parent, if we're a child theme.
   274 		// Set the parent, if we're a child theme.
   264 		if ( $this->template != $this->stylesheet ) {
   275 		if ( $this->template != $this->stylesheet ) {
   265 			// If we are a parent, then there is a problem. Only two generations allowed! Cancel things out.
   276 			// If we are a parent, then there is a problem. Only two generations allowed! Cancel things out.
   266 			if ( is_a( $_child, 'WP_Theme' ) && $_child->template == $this->stylesheet ) {
   277 			if ( $_child instanceof WP_Theme && $_child->template == $this->stylesheet ) {
   267 				$_child->parent = null;
   278 				$_child->parent = null;
   268 				$_child->errors = new WP_Error( 'theme_parent_invalid', sprintf( __( 'The "%s" theme is not a valid parent theme.' ), $_child->template ) );
   279 				$_child->errors = new WP_Error( 'theme_parent_invalid', sprintf( __( 'The "%s" theme is not a valid parent theme.' ), $_child->template ) );
   269 				$_child->cache_add( 'theme', array( 'headers' => $_child->headers, 'errors' => $_child->errors, 'stylesheet' => $_child->stylesheet, 'template' => $_child->template ) );
   280 				$_child->cache_add( 'theme', array( 'headers' => $_child->headers, 'errors' => $_child->errors, 'stylesheet' => $_child->stylesheet, 'template' => $_child->template ) );
   270 				// The two themes actually reference each other with the Template header.
   281 				// The two themes actually reference each other with the Template header.
   271 				if ( $_child->stylesheet == $this->template ) {
   282 				if ( $_child->stylesheet == $this->template ) {
   523 	 *
   534 	 *
   524 	 * @access public
   535 	 * @access public
   525 	 * @since 3.4.0
   536 	 * @since 3.4.0
   526 	 *
   537 	 *
   527 	 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
   538 	 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
   528 	 * @return string String on success, false on failure.
   539 	 * @return string|bool String on success, false on failure.
   529 	 */
   540 	 */
   530 	public function get( $header ) {
   541 	public function get( $header ) {
   531 		if ( ! isset( $this->headers[ $header ] ) )
   542 		if ( ! isset( $this->headers[ $header ] ) )
   532 			return false;
   543 			return false;
   533 
   544 
   559 	 * @since 3.4.0
   570 	 * @since 3.4.0
   560 	 *
   571 	 *
   561 	 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
   572 	 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
   562 	 * @param bool $markup Optional. Whether to mark up the header. Defaults to true.
   573 	 * @param bool $markup Optional. Whether to mark up the header. Defaults to true.
   563 	 * @param bool $translate Optional. Whether to translate the header. Defaults to true.
   574 	 * @param bool $translate Optional. Whether to translate the header. Defaults to true.
   564 	 * @return string Processed header, false on failure.
   575 	 * @return string|bool Processed header, false on failure.
   565 	 */
   576 	 */
   566 	public function display( $header, $markup = true, $translate = true ) {
   577 	public function display( $header, $markup = true, $translate = true ) {
   567 		$value = $this->get( $header );
   578 		$value = $this->get( $header );
       
   579 		if ( false === $value ) {
       
   580 			return false;
       
   581 		}
   568 
   582 
   569 		if ( $translate && ( empty( $value ) || ! $this->load_textdomain() ) )
   583 		if ( $translate && ( empty( $value ) || ! $this->load_textdomain() ) )
   570 			$translate = false;
   584 			$translate = false;
   571 
   585 
   572 		if ( $translate )
   586 		if ( $translate )
   620 				$value = esc_url_raw( $value );
   634 				$value = esc_url_raw( $value );
   621 				break;
   635 				break;
   622 			case 'Tags' :
   636 			case 'Tags' :
   623 				$value = array_filter( array_map( 'trim', explode( ',', strip_tags( $value ) ) ) );
   637 				$value = array_filter( array_map( 'trim', explode( ',', strip_tags( $value ) ) ) );
   624 				break;
   638 				break;
       
   639 			case 'Version' :
       
   640 				$value = strip_tags( $value );
       
   641 				break;
   625 		}
   642 		}
   626 
   643 
   627 		return $value;
   644 		return $value;
   628 	}
   645 	}
   629 
   646 
   647 			case 'Description' :
   664 			case 'Description' :
   648 				$value = wptexturize( $value );
   665 				$value = wptexturize( $value );
   649 				break;
   666 				break;
   650 			case 'Author' :
   667 			case 'Author' :
   651 				if ( $this->get('AuthorURI') ) {
   668 				if ( $this->get('AuthorURI') ) {
   652 					static $attr = null;
   669 					$value = sprintf( '<a href="%1$s">%2$s</a>', $this->display( 'AuthorURI', true, $translate ), $value );
   653 					if ( ! isset( $attr ) )
       
   654 						$attr = esc_attr__( 'Visit author homepage' );
       
   655 					$value = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $this->display( 'AuthorURI', true, $translate ), $attr, $value );
       
   656 				} elseif ( ! $value ) {
   670 				} elseif ( ! $value ) {
   657 					$value = __( 'Anonymous' );
   671 					$value = __( 'Anonymous' );
   658 				}
   672 				}
   659 				break;
   673 				break;
   660 			case 'Tags' :
   674 			case 'Tags' :
   703 					foreach ( $feature_list as $tags )
   717 					foreach ( $feature_list as $tags )
   704 						$tags_list += $tags;
   718 						$tags_list += $tags;
   705 				}
   719 				}
   706 
   720 
   707 				foreach ( $value as &$tag ) {
   721 				foreach ( $value as &$tag ) {
   708 					if ( isset( $tags_list[ $tag ] ) )
   722 					if ( isset( $tags_list[ $tag ] ) ) {
   709 						$tag = $tags_list[ $tag ];
   723 						$tag = $tags_list[ $tag ];
       
   724 					} elseif ( isset( self::$tag_map[ $tag ] ) ) {
       
   725 						$tag = $tags_list[ self::$tag_map[ $tag ] ];
       
   726 					}
   710 				}
   727 				}
   711 
   728 
   712 				return $value;
   729 				return $value;
   713 				break;
   730 
   714 			default :
   731 			default :
   715 				$value = translate( $value, $this->get('TextDomain') );
   732 				$value = translate( $value, $this->get('TextDomain') );
   716 		}
   733 		}
   717 		return $value;
   734 		return $value;
   718 	}
   735 	}
   838 	 * Returns the URL to the directory of the theme root.
   855 	 * Returns the URL to the directory of the theme root.
   839 	 *
   856 	 *
   840 	 * This is typically the absolute URL to wp-content/themes. This forms the basis
   857 	 * This is typically the absolute URL to wp-content/themes. This forms the basis
   841 	 * for all other URLs returned by WP_Theme, so we pass it to the public function
   858 	 * for all other URLs returned by WP_Theme, so we pass it to the public function
   842 	 * get_theme_root_uri() and allow it to run the theme_root_uri filter.
   859 	 * get_theme_root_uri() and allow it to run the theme_root_uri filter.
   843 	 *
       
   844 	 * @uses get_theme_root_uri()
       
   845 	 *
   860 	 *
   846 	 * @since 3.4.0
   861 	 * @since 3.4.0
   847 	 * @access public
   862 	 * @access public
   848 	 *
   863 	 *
   849 	 * @return string Theme root URI.
   864 	 * @return string Theme root URI.
   916 	 * Returns the theme's page templates.
   931 	 * Returns the theme's page templates.
   917 	 *
   932 	 *
   918 	 * @since 3.4.0
   933 	 * @since 3.4.0
   919 	 * @access public
   934 	 * @access public
   920 	 *
   935 	 *
       
   936 	 * @param WP_Post|null $post Optional. The post being edited, provided for context.
   921 	 * @return array Array of page templates, keyed by filename, with the value of the translated header name.
   937 	 * @return array Array of page templates, keyed by filename, with the value of the translated header name.
   922 	 */
   938 	 */
   923 	public function get_page_templates() {
   939 	public function get_page_templates( $post = null ) {
   924 		// If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
   940 		// If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
   925 		if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) )
   941 		if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) )
   926 			return array();
   942 			return array();
   927 
   943 
   928 		$page_templates = $this->cache_get( 'page_templates' );
   944 		$page_templates = $this->cache_get( 'page_templates' );
   946 				$page_template = $this->translate_header( 'Template Name', $page_template );
   962 				$page_template = $this->translate_header( 'Template Name', $page_template );
   947 			}
   963 			}
   948 		}
   964 		}
   949 
   965 
   950 		if ( $this->parent() )
   966 		if ( $this->parent() )
   951 			$page_templates += $this->parent()->get_page_templates();
   967 			$page_templates += $this->parent()->get_page_templates( $post );
   952 
   968 
   953 		return $page_templates;
   969 		/**
       
   970 		 * Filter list of page templates for a theme.
       
   971 		 *
       
   972 		 * This filter does not currently allow for page templates to be added.
       
   973 		 *
       
   974 		 * @since 3.9.0
       
   975 		 *
       
   976 		 * @param array        $page_templates Array of page templates. Keys are filenames,
       
   977 		 *                                     values are translated names.
       
   978 		 * @param WP_Theme     $this           The theme object.
       
   979 		 * @param WP_Post|null $post           The post being edited, provided for context, or null.
       
   980 		 */
       
   981 		$return = apply_filters( 'theme_page_templates', $page_templates, $this, $post );
       
   982 
       
   983 		return array_intersect_assoc( $return, $page_templates );
   954 	}
   984 	}
   955 
   985 
   956 	/**
   986 	/**
   957 	 * Scans a directory for files of a certain extension.
   987 	 * Scans a directory for files of a certain extension.
   958 	 *
   988 	 *
  1004 	 * child theme, it should probably try to load the parent theme's translations.
  1034 	 * child theme, it should probably try to load the parent theme's translations.
  1005 	 *
  1035 	 *
  1006 	 * @since 3.4.0
  1036 	 * @since 3.4.0
  1007 	 * @access public
  1037 	 * @access public
  1008 	 *
  1038 	 *
  1009 	 * @return True if the textdomain was successfully loaded or has already been loaded. False if
  1039 	 * @return bool True if the textdomain was successfully loaded or has already been loaded.
  1010 	 * 	no textdomain was specified in the file headers, or if the domain could not be loaded.
  1040 	 * 	False if no textdomain was specified in the file headers, or if the domain could not be loaded.
  1011 	 */
  1041 	 */
  1012 	public function load_textdomain() {
  1042 	public function load_textdomain() {
  1013 		if ( isset( $this->textdomain_loaded ) )
  1043 		if ( isset( $this->textdomain_loaded ) )
  1014 			return $this->textdomain_loaded;
  1044 			return $this->textdomain_loaded;
  1015 
  1045 
  1072 	 *
  1102 	 *
  1073 	 * @param int $blog_id Optional. Defaults to current blog.
  1103 	 * @param int $blog_id Optional. Defaults to current blog.
  1074 	 * @return array Array of stylesheet names.
  1104 	 * @return array Array of stylesheet names.
  1075 	 */
  1105 	 */
  1076 	public static function get_allowed( $blog_id = null ) {
  1106 	public static function get_allowed( $blog_id = null ) {
       
  1107 		/**
       
  1108 		 * Filter the array of themes allowed on the site or network.
       
  1109 		 *
       
  1110 		 * @since MU
       
  1111 		 *
       
  1112 		 * @param array $allowed_themes An array of theme stylesheet names.
       
  1113 		 */
  1077 		$network = (array) apply_filters( 'allowed_themes', self::get_allowed_on_network() );
  1114 		$network = (array) apply_filters( 'allowed_themes', self::get_allowed_on_network() );
  1078 		return $network + self::get_allowed_on_site( $blog_id );
  1115 		return $network + self::get_allowed_on_site( $blog_id );
  1079 	}
  1116 	}
  1080 
  1117 
  1081 	/**
  1118 	/**