wp/wp-includes/class-wp-theme.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * WP_Theme Class
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Theme
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
final class WP_Theme implements ArrayAccess {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
	 * Headers for style.css files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
	private static $file_headers = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
		'Name'        => 'Theme Name',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
		'ThemeURI'    => 'Theme URI',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
		'Description' => 'Description',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
		'Author'      => 'Author',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
		'AuthorURI'   => 'Author URI',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
		'Version'     => 'Version',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
		'Template'    => 'Template',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
		'Status'      => 'Status',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
		'Tags'        => 'Tags',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
		'TextDomain'  => 'Text Domain',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
		'DomainPath'  => 'Domain Path',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
	 * Default themes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
	private static $default_themes = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
		'classic'        => 'WordPress Classic',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
		'default'        => 'WordPress Default',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
		'twentyten'      => 'Twenty Ten',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
		'twentyeleven'   => 'Twenty Eleven',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
		'twentytwelve'   => 'Twenty Twelve',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		'twentythirteen' => 'Twenty Thirteen',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    46
		'twentyfourteen' => 'Twenty Fourteen',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    47
		'twentyfifteen'  => 'Twenty Fifteen',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    48
	);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    49
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    50
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    51
	 * Renamed theme tags.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    52
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
	private static $tag_map = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
		'fixed-width'    => 'fixed-layout',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
		'flexible-width' => 'fluid-layout',
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	 * Absolute path to the theme root, usually wp-content/themes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
	private $theme_root;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
	 * Header data from the theme's style.css file.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	private $headers = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
	 * Header data from the theme's style.css file after being sanitized.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
	private $headers_sanitized;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
	 * Header name from the theme's style.css after being translated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
	 * Cached due to sorting functions running over the translated name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
	private $name_translated;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
	 * Errors encountered when initializing the theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
	 * @var WP_Error
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
	private $errors;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
	 * The directory name of the theme's files, inside the theme root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
	 * In the case of a child theme, this is directory name of the child theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
	 * Otherwise, 'stylesheet' is the same as 'template'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
	private $stylesheet;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
	 * The directory name of the theme's files, inside the theme root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
	 * In the case of a child theme, this is the directory name of the parent theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
	 * Otherwise, 'template' is the same as 'stylesheet'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
	private $template;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
	 * A reference to the parent theme, in the case of a child theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	 * @var WP_Theme
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
	private $parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
	 * URL to the theme root, usually an absolute URL to wp-content/themes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	 * var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
	private $theme_root_uri;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
	 * Flag for whether the theme's textdomain is loaded.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
	 * @var bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
	private $textdomain_loaded;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
	 * Stores an md5 hash of the theme root, to function as the cache key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
	private $cache_hash;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
	 * Flag for whether the themes cache bucket should be persistently cached.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
	 * Default is false. Can be set with the wp_cache_themes_persistently filter.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
	 * @var bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
	private static $persistently_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
	 * Expiration time for the themes cache bucket.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
	 * By default the bucket is not cached, so this value is useless.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
	 * @var bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
	private static $cache_expiration = 1800;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
	 * Constructor for WP_Theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
	 * @param string $theme_dir Directory of the theme within the theme_root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
	 * @param string $theme_root Theme root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
	 * @param WP_Error|null $_child If this theme is a parent theme, the child may be passed for validation purposes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
	public function __construct( $theme_dir, $theme_root, $_child = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
		global $wp_theme_directories;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
		// Initialize caching on first run.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
		if ( ! isset( self::$persistently_cache ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   183
			/** This action is documented in wp-includes/theme.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
			self::$persistently_cache = apply_filters( 'wp_cache_themes_persistently', false, 'WP_Theme' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
			if ( self::$persistently_cache ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
				wp_cache_add_global_groups( 'themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
				if ( is_int( self::$persistently_cache ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
					self::$cache_expiration = self::$persistently_cache;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
				wp_cache_add_non_persistent_groups( 'themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
		$this->theme_root = $theme_root;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
		$this->stylesheet = $theme_dir;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
		// Correct a situation where the theme is 'some-directory/some-theme' but 'some-directory' was passed in as part of the theme root instead.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
		if ( ! in_array( $theme_root, (array) $wp_theme_directories ) && in_array( dirname( $theme_root ), (array) $wp_theme_directories ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
			$this->stylesheet = basename( $this->theme_root ) . '/' . $this->stylesheet;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
			$this->theme_root = dirname( $theme_root );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
		$this->cache_hash = md5( $this->theme_root . '/' . $this->stylesheet );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
		$theme_file = $this->stylesheet . '/style.css';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
		$cache = $this->cache_get( 'theme' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
		if ( is_array( $cache ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
			foreach ( array( 'errors', 'headers', 'template' ) as $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
				if ( isset( $cache[ $key ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
					$this->$key = $cache[ $key ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
			if ( $this->errors )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
			if ( isset( $cache['theme_root_template'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
				$theme_root_template = $cache['theme_root_template'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
		} elseif ( ! file_exists( $this->theme_root . '/' . $theme_file ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
			$this->headers['Name'] = $this->stylesheet;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
			if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
				$this->errors = new WP_Error( 'theme_not_found', sprintf( __( 'The theme directory "%s" does not exist.' ), $this->stylesheet ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
				$this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
			$this->template = $this->stylesheet;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
			$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
			if ( ! file_exists( $this->theme_root ) ) // Don't cache this one.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
				$this->errors->add( 'theme_root_missing', __( 'ERROR: The themes directory is either empty or doesn&#8217;t exist. Please check your installation.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
		} elseif ( ! is_readable( $this->theme_root . '/' . $theme_file ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
			$this->headers['Name'] = $this->stylesheet;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
			$this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
			$this->template = $this->stylesheet;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
			$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
			$this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
			// Default themes always trump their pretenders.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
			// Properly identify default themes that are inside a directory within wp-content/themes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
			if ( $default_theme_slug = array_search( $this->headers['Name'], self::$default_themes ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
				if ( basename( $this->stylesheet ) != $default_theme_slug )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
					$this->headers['Name'] .= '/' . $this->stylesheet;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
		// (If template is set from cache [and there are no errors], we know it's good.)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
		if ( ! $this->template && ! ( $this->template = $this->headers['Template'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
			$this->template = $this->stylesheet;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
			if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet . '/index.php' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
				$this->errors = new WP_Error( 'theme_no_index', __( 'Template is missing.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
				$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
		// If we got our data from cache, we can assume that 'template' is pointing to the right place.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
		if ( ! is_array( $cache ) && $this->template != $this->stylesheet && ! file_exists( $this->theme_root . '/' . $this->template . '/index.php' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
			// If we're in a directory of themes inside /themes, look for the parent nearby.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
			// wp-content/themes/directory-of-themes/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
			$parent_dir = dirname( $this->stylesheet );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
			if ( '.' != $parent_dir && file_exists( $this->theme_root . '/' . $parent_dir . '/' . $this->template . '/index.php' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
				$this->template = $parent_dir . '/' . $this->template;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
			} elseif ( ( $directories = search_theme_directories() ) && isset( $directories[ $this->template ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
				// Look for the template in the search_theme_directories() results, in case it is in another theme root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
				// We don't look into directories of themes, just the theme root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
				$theme_root_template = $directories[ $this->template ]['theme_root'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
				// Parent theme is missing.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
				$this->errors = new WP_Error( 'theme_no_parent', sprintf( __( 'The parent theme is missing. Please install the "%s" parent theme.' ), $this->template ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
				$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
				$this->parent = new WP_Theme( $this->template, $this->theme_root, $this );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
		// Set the parent, if we're a child theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
		if ( $this->template != $this->stylesheet ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
			// If we are a parent, then there is a problem. Only two generations allowed! Cancel things out.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
			if ( $_child instanceof WP_Theme && $_child->template == $this->stylesheet ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
				$_child->parent = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
				$_child->errors = new WP_Error( 'theme_parent_invalid', sprintf( __( 'The "%s" theme is not a valid parent theme.' ), $_child->template ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
				$_child->cache_add( 'theme', array( 'headers' => $_child->headers, 'errors' => $_child->errors, 'stylesheet' => $_child->stylesheet, 'template' => $_child->template ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
				// The two themes actually reference each other with the Template header.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
				if ( $_child->stylesheet == $this->template ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
					$this->errors = new WP_Error( 'theme_parent_invalid', sprintf( __( 'The "%s" theme is not a valid parent theme.' ), $this->template ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
					$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
			// Set the parent. Pass the current instance so we can do the crazy checks above and assess errors.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
			$this->parent = new WP_Theme( $this->template, isset( $theme_root_template ) ? $theme_root_template : $this->theme_root, $this );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
		// We're good. If we didn't retrieve from cache, set it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
		if ( ! is_array( $cache ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
			$cache = array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
			// If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
			if ( isset( $theme_root_template ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
				$cache['theme_root_template'] = $theme_root_template;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
			$this->cache_add( 'theme', $cache );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
	 * When converting the object to a string, the theme name is returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
	 * @return string Theme name, ready for display (translated)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
	public function __toString() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
		return (string) $this->display('Name');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
	 * __isset() magic method for properties formerly returned by current_theme_info()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
	public function __isset( $offset ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
		static $properties = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
			'name', 'title', 'version', 'parent_theme', 'template_dir', 'stylesheet_dir', 'template', 'stylesheet',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
			'screenshot', 'description', 'author', 'tags', 'theme_root', 'theme_root_uri',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
		return in_array( $offset, $properties );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
	 * __get() magic method for properties formerly returned by current_theme_info()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
	public function __get( $offset ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
		switch ( $offset ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
			case 'name' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
			case 'title' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
				return $this->get('Name');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
			case 'version' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
				return $this->get('Version');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
			case 'parent_theme' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
				return $this->parent() ? $this->parent()->get('Name') : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
			case 'template_dir' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
				return $this->get_template_directory();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
			case 'stylesheet_dir' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
				return $this->get_stylesheet_directory();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
			case 'template' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
				return $this->get_template();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
			case 'stylesheet' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
				return $this->get_stylesheet();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
			case 'screenshot' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
				return $this->get_screenshot( 'relative' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
			// 'author' and 'description' did not previously return translated data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
			case 'description' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
				return $this->display('Description');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
			case 'author' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
				return $this->display('Author');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
			case 'tags' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
				return $this->get( 'Tags' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
			case 'theme_root' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
				return $this->get_theme_root();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
			case 'theme_root_uri' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
				return $this->get_theme_root_uri();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
			// For cases where the array was converted to an object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
			default :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
				return $this->offsetGet( $offset );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
	 * Method to implement ArrayAccess for keys formerly returned by get_themes()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
	public function offsetSet( $offset, $value ) {}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
	 * Method to implement ArrayAccess for keys formerly returned by get_themes()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
	public function offsetUnset( $offset ) {}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
	 * Method to implement ArrayAccess for keys formerly returned by get_themes()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
	public function offsetExists( $offset ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
		static $keys = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
			'Name', 'Version', 'Status', 'Title', 'Author', 'Author Name', 'Author URI', 'Description',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
			'Template', 'Stylesheet', 'Template Files', 'Stylesheet Files', 'Template Dir', 'Stylesheet Dir',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
			 'Screenshot', 'Tags', 'Theme Root', 'Theme Root URI', 'Parent Theme',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
		return in_array( $offset, $keys );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
	 * Method to implement ArrayAccess for keys formerly returned by get_themes().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
	 * Author, Author Name, Author URI, and Description did not previously return
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
	 * translated data. We are doing so now as it is safe to do. However, as
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
	 * Name and Title could have been used as the key for get_themes(), both remain
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
	 * untranslated for back compatibility. This means that ['Name'] is not ideal,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
	 * and care should be taken to use $theme->display('Name') to get a properly
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
	 * translated header.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
	public function offsetGet( $offset ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
		switch ( $offset ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
			case 'Name' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
			case 'Title' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
				// See note above about using translated data. get() is not ideal.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
				// It is only for backwards compatibility. Use display().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
				return $this->get('Name');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
			case 'Author' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
				return $this->display( 'Author');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
			case 'Author Name' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
				return $this->display( 'Author', false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
			case 'Author URI' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
				return $this->display('AuthorURI');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
			case 'Description' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
				return $this->display( 'Description');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
			case 'Version' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
			case 'Status' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
				return $this->get( $offset );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
			case 'Template' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
				return $this->get_template();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
			case 'Stylesheet' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
				return $this->get_stylesheet();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
			case 'Template Files' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
				return $this->get_files( 'php', 1, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
			case 'Stylesheet Files' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
				return $this->get_files( 'css', 0, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
			case 'Template Dir' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
				return $this->get_template_directory();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
			case 'Stylesheet Dir' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
				return $this->get_stylesheet_directory();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
			case 'Screenshot' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
				return $this->get_screenshot( 'relative' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
			case 'Tags' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
				return $this->get('Tags');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
			case 'Theme Root' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
				return $this->get_theme_root();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
			case 'Theme Root URI' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
				return $this->get_theme_root_uri();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
			case 'Parent Theme' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
				return $this->parent() ? $this->parent()->get('Name') : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
			default :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
				return null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
	 * Returns errors property.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
	 * @return WP_Error|bool WP_Error if there are errors, or false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
	public function errors() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
		return is_wp_error( $this->errors ) ? $this->errors : false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
	 * Whether the theme exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
	 * A theme with errors exists. A theme with the error of 'theme_not_found',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
	 * meaning that the theme's directory was not found, does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
	 * @return bool Whether the theme exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
	public function exists() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
		return ! ( $this->errors() && in_array( 'theme_not_found', $this->errors()->get_error_codes() ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
	 * Returns reference to the parent theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
	 * @return WP_Theme|bool Parent theme, or false if the current theme is not a child theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
	public function parent() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
		return isset( $this->parent ) ? $this->parent : false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	 * Adds theme data to cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
	 * Cache entries keyed by the theme and the type of data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
	 * @param string $key Type of data to store (theme, screenshot, headers, page_templates)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
	 * @param string $data Data to store
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
	 * @return bool Return value from wp_cache_add()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
	private function cache_add( $key, $data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
		return wp_cache_add( $key . '-' . $this->cache_hash, $data, 'themes', self::$cache_expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
	 * Gets theme data from cache.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
	 * Cache entries are keyed by the theme and the type of data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
	 * @param string $key Type of data to retrieve (theme, screenshot, headers, page_templates)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	 * @return mixed Retrieved data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
	private function cache_get( $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
		return wp_cache_get( $key . '-' . $this->cache_hash, 'themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
	 * Clears the cache for the theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
	public function cache_delete() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
		foreach ( array( 'theme', 'screenshot', 'headers', 'page_templates' ) as $key )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
			wp_cache_delete( $key . '-' . $this->cache_hash, 'themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
		$this->template = $this->textdomain_loaded = $this->theme_root_uri = $this->parent = $this->errors = $this->headers_sanitized = $this->name_translated = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
		$this->headers = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
		$this->__construct( $this->stylesheet, $this->theme_root );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
	 * Get a raw, unformatted theme header.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
	 * The header is sanitized, but is not translated, and is not marked up for display.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
	 * To get a theme header for display, use the display() method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
	 * Use the get_template() method, not the 'Template' header, for finding the template.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
	 * The 'Template' header is only good for what was written in the style.css, while
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
	 * get_template() takes into account where WordPress actually located the theme and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
	 * whether it is actually valid.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
	 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   539
	 * @return string|bool String on success, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
	public function get( $header ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
		if ( ! isset( $this->headers[ $header ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
		if ( ! isset( $this->headers_sanitized ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
			$this->headers_sanitized = $this->cache_get( 'headers' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
			if ( ! is_array( $this->headers_sanitized ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
				$this->headers_sanitized = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
		if ( isset( $this->headers_sanitized[ $header ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
			return $this->headers_sanitized[ $header ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
		// If themes are a persistent group, sanitize everything and cache it. One cache add is better than many cache sets.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
		if ( self::$persistently_cache ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
			foreach ( array_keys( $this->headers ) as $_header )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
				$this->headers_sanitized[ $_header ] = $this->sanitize_header( $_header, $this->headers[ $_header ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
			$this->cache_add( 'headers', $this->headers_sanitized );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
			$this->headers_sanitized[ $header ] = $this->sanitize_header( $header, $this->headers[ $header ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
		return $this->headers_sanitized[ $header ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
	 * Gets a theme header, formatted and translated for display.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
	 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
	 * @param bool $markup Optional. Whether to mark up the header. Defaults to true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
	 * @param bool $translate Optional. Whether to translate the header. Defaults to true.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   575
	 * @return string|bool Processed header, false on failure.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
	public function display( $header, $markup = true, $translate = true ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
		$value = $this->get( $header );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   579
		if ( false === $value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   580
			return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   581
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
		if ( $translate && ( empty( $value ) || ! $this->load_textdomain() ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
			$translate = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
		if ( $translate )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
			$value = $this->translate_header( $header, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
		if ( $markup )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
			$value = $this->markup_header( $header, $value, $translate );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
	 * Sanitize a theme header.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
	 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
	 * @param string $value Value to sanitize.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
	private function sanitize_header( $header, $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
		switch ( $header ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
			case 'Status' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
				if ( ! $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
					$value = 'publish';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
				// Fall through otherwise.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
			case 'Name' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
				static $header_tags = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
					'abbr'    => array( 'title' => true ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
					'acronym' => array( 'title' => true ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
					'code'    => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
					'em'      => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
					'strong'  => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
				);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
				$value = wp_kses( $value, $header_tags );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
			case 'Author' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
				// There shouldn't be anchor tags in Author, but some themes like to be challenging.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
			case 'Description' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
				static $header_tags_with_a = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
					'a'       => array( 'href' => true, 'title' => true ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
					'abbr'    => array( 'title' => true ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
					'acronym' => array( 'title' => true ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
					'code'    => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
					'em'      => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
					'strong'  => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
				);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
				$value = wp_kses( $value, $header_tags_with_a );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
			case 'ThemeURI' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
			case 'AuthorURI' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
				$value = esc_url_raw( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
			case 'Tags' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
				$value = array_filter( array_map( 'trim', explode( ',', strip_tags( $value ) ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
				break;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   639
			case 'Version' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   640
				$value = strip_tags( $value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   641
				break;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
	 * Mark up a theme header.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
	 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
	 * @param string $value Value to mark up.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
	 * @param string $translate Whether the header has been translated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
	 * @return string Value, marked up.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
	private function markup_header( $header, $value, $translate ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
		switch ( $header ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
			case 'Name' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
				if ( empty( $value ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
					$value = $this->get_stylesheet();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
			case 'Description' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
				$value = wptexturize( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
			case 'Author' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
				if ( $this->get('AuthorURI') ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   669
					$value = sprintf( '<a href="%1$s">%2$s</a>', $this->display( 'AuthorURI', true, $translate ), $value );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
				} elseif ( ! $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
					$value = __( 'Anonymous' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
			case 'Tags' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
				static $comma = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
				if ( ! isset( $comma ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
					/* translators: used between list items, there is a space after the comma */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
					$comma = __( ', ' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
				$value = implode( $comma, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
			case 'ThemeURI' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
			case 'AuthorURI' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
				$value = esc_url( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
	 * Translate a theme header.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
	 * @param string $header Theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
	 * @param string $value Value to translate.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
	 * @return string Translated value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
	private function translate_header( $header, $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
		switch ( $header ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
			case 'Name' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
				// Cached for sorting reasons.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
				if ( isset( $this->name_translated ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
					return $this->name_translated;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
				$this->name_translated = translate( $value, $this->get('TextDomain' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
				return $this->name_translated;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
			case 'Tags' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
				if ( empty( $value ) || ! function_exists( 'get_theme_feature_list' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
					return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
				static $tags_list;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
				if ( ! isset( $tags_list ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
					$tags_list = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
					$feature_list = get_theme_feature_list( false ); // No API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
					foreach ( $feature_list as $tags )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
						$tags_list += $tags;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
				foreach ( $value as &$tag ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   722
					if ( isset( $tags_list[ $tag ] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
						$tag = $tags_list[ $tag ];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   724
					} elseif ( isset( self::$tag_map[ $tag ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   725
						$tag = $tags_list[ self::$tag_map[ $tag ] ];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   726
					}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
				return $value;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   730
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
			default :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
				$value = translate( $value, $this->get('TextDomain') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
	 * The directory name of the theme's "stylesheet" files, inside the theme root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
	 * In the case of a child theme, this is directory name of the child theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
	 * Otherwise, get_stylesheet() is the same as get_template().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
	 * @return string Stylesheet
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
	public function get_stylesheet() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
		return $this->stylesheet;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
	 * The directory name of the theme's "template" files, inside the theme root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
	 * In the case of a child theme, this is the directory name of the parent theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
	 * Otherwise, the get_template() is the same as get_stylesheet().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
	 * @return string Template
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
	public function get_template() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
		return $this->template;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
	 * Returns the absolute path to the directory of a theme's "stylesheet" files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
	 * In the case of a child theme, this is the absolute path to the directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
	 * of the child theme's files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
	 * @return string Absolute path of the stylesheet directory.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
	public function get_stylesheet_directory() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
		if ( $this->errors() && in_array( 'theme_root_missing', $this->errors()->get_error_codes() ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
			return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
		return $this->theme_root . '/' . $this->stylesheet;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
	 * Returns the absolute path to the directory of a theme's "template" files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
	 * In the case of a child theme, this is the absolute path to the directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
	 * of the parent theme's files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
	 * @return string Absolute path of the template directory.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
	public function get_template_directory() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
		if ( $this->parent() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
			$theme_root = $this->parent()->theme_root;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
			$theme_root = $this->theme_root;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
		return $theme_root . '/' . $this->template;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
	 * Returns the URL to the directory of a theme's "stylesheet" files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
	 * In the case of a child theme, this is the URL to the directory of the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
	 * child theme's files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
	 * @return string URL to the stylesheet directory.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
	public function get_stylesheet_directory_uri() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
		return $this->get_theme_root_uri() . '/' . str_replace( '%2F', '/', rawurlencode( $this->stylesheet ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
	 * Returns the URL to the directory of a theme's "template" files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
	 * In the case of a child theme, this is the URL to the directory of the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
	 * parent theme's files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
	 * @return string URL to the template directory.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
	public function get_template_directory_uri() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
		if ( $this->parent() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
			$theme_root_uri = $this->parent()->get_theme_root_uri();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
			$theme_root_uri = $this->get_theme_root_uri();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
		return $theme_root_uri . '/' . str_replace( '%2F', '/', rawurlencode( $this->template ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
	 * The absolute path to the directory of the theme root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
	 * This is typically the absolute path to wp-content/themes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
	 * @return string Theme root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
	public function get_theme_root() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
		return $this->theme_root;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
	 * Returns the URL to the directory of the theme root.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
	 * This is typically the absolute URL to wp-content/themes. This forms the basis
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
	 * for all other URLs returned by WP_Theme, so we pass it to the public function
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
	 * get_theme_root_uri() and allow it to run the theme_root_uri filter.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
	 * @return string Theme root URI.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
	public function get_theme_root_uri() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
		if ( ! isset( $this->theme_root_uri ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
			$this->theme_root_uri = get_theme_root_uri( $this->stylesheet, $this->theme_root );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
		return $this->theme_root_uri;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
	 * Returns the main screenshot file for the theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
	 * The main screenshot is called screenshot.png. gif and jpg extensions are also allowed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
	 * Screenshots for a theme must be in the stylesheet directory. (In the case of child
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
	 * themes, parent theme screenshots are not inherited.)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
	 * @param string $uri Type of URL to return, either 'relative' or an absolute URI. Defaults to absolute URI.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
	 * @return mixed Screenshot file. False if the theme does not have a screenshot.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
	public function get_screenshot( $uri = 'uri' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
		$screenshot = $this->cache_get( 'screenshot' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
		if ( $screenshot ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
			if ( 'relative' == $uri )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
				return $screenshot;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
			return $this->get_stylesheet_directory_uri() . '/' . $screenshot;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
		} elseif ( 0 === $screenshot ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
		foreach ( array( 'png', 'gif', 'jpg', 'jpeg' ) as $ext ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
			if ( file_exists( $this->get_stylesheet_directory() . "/screenshot.$ext" ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
				$this->cache_add( 'screenshot', 'screenshot.' . $ext );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
				if ( 'relative' == $uri )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
					return 'screenshot.' . $ext;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
				return $this->get_stylesheet_directory_uri() . '/' . 'screenshot.' . $ext;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
		$this->cache_add( 'screenshot', 0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
	 * Return files in the theme's directory.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
	 * @param mixed $type Optional. Array of extensions to return. Defaults to all files (null).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
	 * @param int $depth Optional. How deep to search for files. Defaults to a flat scan (0 depth). -1 depth is infinite.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   917
	 * @param bool $search_parent Optional. Whether to return parent files. Defaults to false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
	 * @return array Array of files, keyed by the path to the file relative to the theme's directory, with the values
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
	 * 	being absolute paths.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
	public function get_files( $type = null, $depth = 0, $search_parent = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
		$files = (array) self::scandir( $this->get_stylesheet_directory(), $type, $depth );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
		if ( $search_parent && $this->parent() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
			$files += (array) self::scandir( $this->get_template_directory(), $type, $depth );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
		return $files;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   931
	 * Returns the theme's page templates.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   936
	 * @param WP_Post|null $post Optional. The post being edited, provided for context.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
	 * @return array Array of page templates, keyed by filename, with the value of the translated header name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   938
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   939
	public function get_page_templates( $post = null ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
		// If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
		if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
			return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
		$page_templates = $this->cache_get( 'page_templates' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
		if ( ! is_array( $page_templates ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
			$page_templates = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
			$files = (array) $this->get_files( 'php', 1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
			foreach ( $files as $file => $full_path ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
				if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
				$page_templates[ $file ] = _cleanup_header_comment( $header[1] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   957
			$this->cache_add( 'page_templates', $page_templates );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   960
		if ( $this->load_textdomain() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   961
			foreach ( $page_templates as &$page_template ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   962
				$page_template = $this->translate_header( 'Template Name', $page_template );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   963
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
		if ( $this->parent() )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   967
			$page_templates += $this->parent()->get_page_templates( $post );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   969
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   970
		 * Filter list of page templates for a theme.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   971
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   972
		 * This filter does not currently allow for page templates to be added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   973
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   974
		 * @since 3.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   975
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   976
		 * @param array        $page_templates Array of page templates. Keys are filenames,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   977
		 *                                     values are translated names.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   978
		 * @param WP_Theme     $this           The theme object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   979
		 * @param WP_Post|null $post           The post being edited, provided for context, or null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   980
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   981
		$return = apply_filters( 'theme_page_templates', $page_templates, $this, $post );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   982
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   983
		return array_intersect_assoc( $return, $page_templates );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
	 * Scans a directory for files of a certain extension.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
	 * @param string $path Absolute path to search.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
	 * @param mixed  Array of extensions to find, string of a single extension, or null for all extensions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
	 * @param int $depth How deep to search for files. Optional, defaults to a flat scan (0 depth). -1 depth is infinite.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
	 * @param string $relative_path The basename of the absolute path. Used to control the returned path
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
	 * 	for the found files, particularly when this function recurses to lower depths.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
	private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
		if ( ! is_dir( $path ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
		if ( $extensions ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
			$extensions = (array) $extensions;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
			$_extensions = implode( '|', $extensions );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
		$relative_path = trailingslashit( $relative_path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
		if ( '/' == $relative_path )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
			$relative_path = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
		$results = scandir( $path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
		$files = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
		foreach ( $results as $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
			if ( '.' == $result[0] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
			if ( is_dir( $path . '/' . $result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
				if ( ! $depth || 'CVS' == $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
				$found = self::scandir( $path . '/' . $result, $extensions, $depth - 1 , $relative_path . $result );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
				$files = array_merge_recursive( $files, $found );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
			} elseif ( ! $extensions || preg_match( '~\.(' . $_extensions . ')$~', $result ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
				$files[ $relative_path . $result ] = $path . '/' . $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
		return $files;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
	 * Loads the theme's textdomain.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
	 * Translation files are not inherited from the parent theme. Todo: if this fails for the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1034
	 * child theme, it should probably try to load the parent theme's translations.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1039
	 * @return bool True if the textdomain was successfully loaded or has already been loaded.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1040
	 * 	False if no textdomain was specified in the file headers, or if the domain could not be loaded.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
	public function load_textdomain() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
		if ( isset( $this->textdomain_loaded ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
			return $this->textdomain_loaded;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
		$textdomain = $this->get('TextDomain');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
		if ( ! $textdomain ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
			$this->textdomain_loaded = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
		if ( is_textdomain_loaded( $textdomain ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
			$this->textdomain_loaded = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
		$path = $this->get_stylesheet_directory();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
		if ( $domainpath = $this->get('DomainPath') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
			$path .= $domainpath;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
			$path .= '/languages';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
		$this->textdomain_loaded = load_theme_textdomain( $textdomain, $path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
		return $this->textdomain_loaded;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
	 * Whether the theme is allowed (multisite only).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
	 * @param string $check Optional. Whether to check only the 'network'-wide settings, the 'site'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
	 * 	settings, or 'both'. Defaults to 'both'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
	 * @param int $blog_id Optional. Ignored if only network-wide settings are checked. Defaults to current blog.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
	 * @return bool Whether the theme is allowed for the network. Returns true in single-site.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
	public function is_allowed( $check = 'both', $blog_id = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
		if ( ! is_multisite() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
		if ( 'both' == $check || 'network' == $check ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
			$allowed = self::get_allowed_on_network();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
			if ( ! empty( $allowed[ $this->get_stylesheet() ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
				return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
		if ( 'both' == $check || 'site' == $check ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
			$allowed = self::get_allowed_on_site( $blog_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
			if ( ! empty( $allowed[ $this->get_stylesheet() ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
				return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
	 * Returns array of stylesheet names of themes allowed on the site or network.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
	 * @param int $blog_id Optional. Defaults to current blog.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1104
	 * @return array Array of stylesheet names.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
	public static function get_allowed( $blog_id = null ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1107
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1108
		 * Filter the array of themes allowed on the site or network.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1109
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1110
		 * @since MU
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1111
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1112
		 * @param array $allowed_themes An array of theme stylesheet names.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1113
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
		$network = (array) apply_filters( 'allowed_themes', self::get_allowed_on_network() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
		return $network + self::get_allowed_on_site( $blog_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
	 * Returns array of stylesheet names of themes allowed on the network.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
	 * @return array Array of stylesheet names.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
	public static function get_allowed_on_network() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
		static $allowed_themes;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
		if ( ! isset( $allowed_themes ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
			$allowed_themes = (array) get_site_option( 'allowedthemes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
		return $allowed_themes;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
	 * Returns array of stylesheet names of themes allowed on the site.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
	 * @param int $blog_id Optional. Defaults to current blog.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
	 * @return array Array of stylesheet names.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
	public static function get_allowed_on_site( $blog_id = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
		static $allowed_themes = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
		if ( ! $blog_id || ! is_multisite() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
			$blog_id = get_current_blog_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
		if ( isset( $allowed_themes[ $blog_id ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
			return $allowed_themes[ $blog_id ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
		$current = $blog_id == get_current_blog_id();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1152
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1153
		if ( $current ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1154
			$allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
			switch_to_blog( $blog_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
			$allowed_themes[ $blog_id ] = get_option( 'allowedthemes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
			restore_current_blog();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
		// This is all super old MU back compat joy.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
		// 'allowedthemes' keys things by stylesheet. 'allowed_themes' keyed things by name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
		if ( false === $allowed_themes[ $blog_id ] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
			if ( $current ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
				$allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
				switch_to_blog( $blog_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
				$allowed_themes[ $blog_id ] = get_option( 'allowed_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
				restore_current_blog();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
			if ( ! is_array( $allowed_themes[ $blog_id ] ) || empty( $allowed_themes[ $blog_id ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
				$allowed_themes[ $blog_id ] = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
				$converted = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
				$themes = wp_get_themes();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
				foreach ( $themes as $stylesheet => $theme_data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
					if ( isset( $allowed_themes[ $blog_id ][ $theme_data->get('Name') ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
						$converted[ $stylesheet ] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
				$allowed_themes[ $blog_id ] = $converted;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
			// Set the option so we never have to go through this pain again.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
			if ( is_admin() && $allowed_themes[ $blog_id ] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
				if ( $current ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
					update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
					delete_option( 'allowed_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
					switch_to_blog( $blog_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
					update_option( 'allowedthemes', $allowed_themes[ $blog_id ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
					delete_option( 'allowed_themes' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
					restore_current_blog();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
		return (array) $allowed_themes[ $blog_id ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
	 * Sort themes by name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
	public static function sort_by_name( &$themes ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
		if ( 0 === strpos( get_locale(), 'en_' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
			uasort( $themes, array( 'WP_Theme', '_name_sort' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
			uasort( $themes, array( 'WP_Theme', '_name_sort_i18n' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1211
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1212
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1213
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1214
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1215
	 * Callback function for usort() to naturally sort themes by name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1216
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1217
	 * Accesses the Name header directly from the class for maximum speed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1218
	 * Would choke on HTML but we don't care enough to slow it down with strip_tags().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1219
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
	private static function _name_sort( $a, $b ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
		return strnatcasecmp( $a->headers['Name'], $b->headers['Name'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1226
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1227
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
	 * Name sort (with translation).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
	private static function _name_sort_i18n( $a, $b ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1234
		// Don't mark up; Do translate.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1235
		return strnatcasecmp( $a->display( 'Name', false, true ), $b->display( 'Name', false, true ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1236
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1237
}