[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_Themes.class.php (source)

   1  <?php
   2  class MP_Themes
   3  {
   4      const default_theme_folder     = 'twentyten';
   5      const default_theme_name     = 'MailPress Twenty Ten';
   6  
   7      const option_current_theme     = 'MailPress_current_theme';
   8      const option_stylesheet        = 'MailPress_stylesheet';
   9      const option_template        = 'MailPress_template';
  10  
  11  	function __construct()
  12      {
  13          global $mp_theme_directories;
  14  
  15          $mp_theme_directories         = $this->get_theme_directories();
  16  
  17          $this->themes            = $this->get_themes();
  18          $this->current_theme         = $this->get_current_theme();
  19          $this->path_current_theme     = ABSPATH . $this->themes[$this->current_theme] ['Template Dir'];
  20      }
  21  
  22  	function get_folder($folder) 
  23      {
  24          return apply_filters("MailPress_{$folder}", get_option(constant("self::option_{$folder}")));
  25      }
  26  
  27  	function get_folder_directory($folder) 
  28      {
  29          $func        = "get_{$folder}";
  30          $folder    = $this->$func();
  31          $theme_root = $this->get_theme_root( $folder );
  32  
  33          return apply_filters("MailPress_{$folder}_directory", "$theme_root/$folder", $folder, $theme_root );
  34      }
  35  
  36  	function get_theme_by_folder($name, $type = 'Stylesheet') 
  37      {
  38          foreach ($this->themes as $theme) if ( $theme[$type] == $name) return $theme;
  39          return null;
  40      }
  41  
  42  	function get_theme_by_stylesheet($stylesheet) { return $this->get_theme_by_folder($stylesheet, 'Stylesheet'); }
  43  
  44  	function get_theme_by_template($template)     { return $this->get_theme_by_folder($template,   'Template'); }
  45  
  46  	function get_template_files($path, $t_files = array()) 
  47      {
  48          if (is_dir($path)) {
  49              $dir = @ dir($path);
  50              if ( $dir ) {
  51                  while ( ($file = $dir->read()) !== false ) {
  52                      if ( preg_match('|^\.+$|', $file) )
  53                          continue;
  54                      if ( preg_match('|\.php$|', $file) ) 
  55                          $t_files[] = str_replace(ABSPATH, '', "$path/$file");
  56                  }
  57                  @ $dir->close();
  58              }
  59          }
  60          return array_unique($t_files);
  61      }
  62  
  63  /* /wp-admin/includes/theme.php */
  64  
  65  	function current_theme_info() 
  66      {
  67          $themes = $this->themes;
  68          $current_theme = $this->current_theme;
  69  
  70          if (! $themes) {
  71              $ct = new stdClass;
  72              $ct->name = $this->current_theme;
  73              return $ct;
  74          }
  75  
  76          if ( ! isset( $themes[$current_theme] ) ) {
  77              delete_option(self::option_current_theme);
  78              $current_theme = $this->get_current_theme();
  79          }
  80  
  81          $ct                 = new stdClass();
  82          $ct->name             = $current_theme;
  83          $ct->title             = $themes[$current_theme]['Title'];
  84          $ct->version         = $themes[$current_theme]['Version'];
  85          $ct->parent_theme     = $themes[$current_theme]['Parent Theme'];
  86          $ct->template_dir     = $themes[$current_theme]['Template Dir'];
  87          $ct->stylesheet_dir     = $themes[$current_theme]['Stylesheet Dir'];
  88          $ct->template         = $themes[$current_theme]['Template'];
  89          $ct->stylesheet         = $themes[$current_theme]['Stylesheet'];
  90          $ct->screenshot         = $themes[$current_theme]['Screenshot'];
  91          $ct->description         = $themes[$current_theme]['Description'];
  92          $ct->author         = $themes[$current_theme]['Author'];
  93          $ct->tags             = $themes[$current_theme]['Tags'];
  94          $ct->theme_root         = $themes[$current_theme]['Theme Root'];
  95          $ct->theme_root_uri     = $themes[$current_theme]['Theme Root URI'];
  96          return $ct;
  97      }
  98  
  99  	function get_broken_themes() 
 100      {
 101          global $mp_broken_themes;
 102          return $mp_broken_themes;
 103      }
 104  
 105  	function get_page_templates($t = false, $plaintext = false) 
 106      {
 107          $s_dir    = ($plaintext) ? 'Plaintext Stylesheet Dir' : 'Stylesheet Dir';
 108          $t_dir     = ($plaintext) ? 'Plaintext Template Dir'   : 'Template Dir';
 109          $t_files     = ($plaintext) ? 'Plaintext Template Files' : 'Template Files';
 110  
 111          $themes     = $this->themes;
 112          $theme     = ($t) ? $this->get_theme_by_stylesheet($t) : $themes[$this->current_theme];
 113          $templates     = $theme[$t_files];
 114          $pt         = array ();
 115  
 116          $stylesheet_directory     = $theme[$s_dir];
 117          $template_directory     = $theme[$t_dir];
 118          $plaintext_directory     = ($plaintext && isset($themes['plaintext']['Stylesheet Dir'])) ? $themes['plaintext']['Stylesheet Dir'] : null;
 119  
 120          if ( is_array( $templates ) ) {
 121  
 122              $base[] = $stylesheet_directory;
 123              $base[] = $template_directory;
 124              if (isset($plaintext_directory)) $base[] = $plaintext_directory;
 125              $base = array_map('trailingslashit', array_unique(array_diff($base, array('/'))));
 126  
 127              foreach ( $templates as $template ) {
 128                  $basename = str_replace($base, '', $template);
 129  
 130                  // don't allow template files in subdirectories
 131                  if ( false !== strpos($basename, '/') )
 132                      continue;
 133  
 134                  if ( 'functions.php' == $basename )
 135                      continue;
 136  
 137                  $template_data = implode( '', file( ABSPATH . $template ));
 138  
 139                  $name = '';
 140                  preg_match( '|Template Name:(.*)$|mi', $template_data, $name );
 141                  if (empty($name)) continue;
 142                  if (!isset($pt[trim( $name[1] )]['file']))     $pt[trim( $name[1] )]['file'] = $basename;
 143  
 144                  $subject = '';
 145                  preg_match( '|Subject:(.*)$|mi', $template_data, $subject );
 146                  if (empty($subject)) continue;
 147                  if (!isset($pt[trim( $name[1] )]['subject']))     $pt[trim( $name[1] )]['subject'] = $subject[1];
 148              }
 149          }
 150          return $pt;
 151      }
 152  
 153  /* /wp-includes/theme.php */
 154  
 155  	function is_child_theme()
 156      {
 157          return ( $this->get_template_directory() !== $this->get_stylesheet_directory() );
 158      }
 159  
 160  	function get_stylesheet() { return $this->get_folder('stylesheet'); }
 161  
 162  	function get_stylesheet_directory() { return $this->get_folder_directory('stylesheet'); }
 163  
 164  	function get_template() { return $this->get_folder('template'); }
 165  
 166  	function get_template_directory() { return $this->get_folder_directory('template'); }
 167  
 168  	function get_themes() 
 169      {
 170          global $mp_themes, $mp_broken_themes;
 171  
 172          if ( isset($mp_themes) ) 
 173              return $mp_themes;
 174  
 175          if ( !$theme_files = $this->search_theme_directories() )
 176              return false;
 177  
 178          asort($theme_files);
 179  
 180          $mp_themes = array();
 181  
 182          foreach ( (array) $theme_files as $stylesheet => $theme_file ) {
 183              $theme_root = $theme_file['theme_root'];
 184              $theme_file = $theme_file['theme_file'];
 185  
 186              if ( !is_readable("$theme_root/$theme_file") ) {
 187                  $mp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'), 'Folder' => $theme_file);
 188                  continue;
 189              }
 190  
 191              //$theme_data = get_theme_data("$theme_root/$theme_file");
 192              $theme_data = wp_get_theme($stylesheet, $theme_root);
 193  
 194              $name        = $theme_data['Name'];
 195              $title    = $theme_data['Title'];
 196              $description= wptexturize($theme_data['Description']);
 197              $version    = $theme_data['Version'];
 198              $author    = $theme_data['Author'];
 199              $template    = $theme_data['Template'];
 200              $stylesheet = dirname($theme_file);
 201  
 202              $screenshot = false;
 203              foreach ( MP_::ext_image() as $ext ) {
 204                  if (is_file("$theme_root/$stylesheet/screenshot.$ext")) {
 205                      $screenshot = "screenshot.$ext";
 206                      break;
 207                  }
 208              }
 209  
 210              if ( empty($name) ) {
 211                  $name = dirname($theme_file);
 212                  $title = $name;
 213              }
 214  
 215              $parent_template = $template;
 216  
 217              if ( empty($template) ) {
 218                  if ( is_file("$theme_root/$stylesheet/index.php") )
 219                      $template = $stylesheet;
 220                  else
 221                      continue;
 222              }
 223  
 224              $template = trim($template);
 225  
 226              if ( !is_file("$theme_root/$template/index.php") ) {
 227                  $parent_dir = dirname(dirname($theme_file));
 228                  if ( is_file("$theme_root/$parent_dir/$template/index.php") ) {
 229                      $template = "$parent_dir/$template";
 230                      $template_directory = "$theme_root/$template";
 231                  } else {
 232                      /**
 233                      * The parent theme doesn't exist in the current theme's folder or sub folder
 234                      * so lets use the theme root for the parent template.
 235                      */
 236                      if ( isset($theme_files[$template]) && is_file( $theme_files[$template]['theme_root'] . "/$template/index.php" ) ) {
 237                          $template_directory = $theme_files[$template]['theme_root'] . "/$template";
 238                      } else {
 239                          if ( empty( $parent_template) )
 240                              $mp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'), 'error' => 'no_template', 'Folder' => $stylesheet);
 241                          else
 242                              $mp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => sprintf( __('The parent theme is missing. Please install the "%s" parent theme.'), $parent_template ), 'error' => 'no_parent', 'parent' => $parent_template, 'Folder' => $stylesheet );
 243                          continue;
 244                      }
 245                  }
 246              } else {
 247                  $template_directory = trim( $theme_root . '/' . $template );
 248              }
 249  
 250              $stylesheet_files = $template_files = array();
 251  
 252              $stylesheet_dir = @ dir("$theme_root/$stylesheet");
 253              if ( $stylesheet_dir ) {
 254                  while ( ($file = $stylesheet_dir->read()) !== false ) {
 255                      if ( !preg_match('|^\.+$|', $file) ) {
 256                          if ( preg_match('|\.css$|', $file) )
 257                              $stylesheet_files[] = str_replace(ABSPATH, '', "$theme_root/$stylesheet/$file");
 258                          elseif ( preg_match('|\.php$|', $file) )
 259                              $template_files[] = str_replace(ABSPATH, '', "$theme_root/$stylesheet/$file");
 260                      }
 261                  }
 262                  @ $stylesheet_dir->close();
 263              }
 264              $stylesheet_files = array_unique($stylesheet_files);
 265              $stylesheet_dir   = (empty($stylesheet_files )) ? '/' : $theme_root . '/' . $stylesheet;
 266  
 267              $plaintext_stylesheet_files = $this->get_template_files("$theme_root/$stylesheet/plaintext");
 268              $plaintext_stylesheet_dir   = (empty($plaintext_stylesheet_files)) ? '/' : "$theme_root/$stylesheet/plaintext";
 269  
 270              $template_files = $this->get_template_files($template_directory, $template_files);
 271              $template_dir   = (empty($template_files)) ? '/' : $template_directory;
 272  
 273              $plaintext_template_files = $this->get_template_files("$template_directory/plaintext");
 274              $plaintext_template_dir   = (empty($plaintext_template_files)) ? '/' : "$template_directory/plaintext";
 275  
 276              $plaintext_template_files = $this->get_template_files(dirname($template_directory) . '/plaintext', array_merge($plaintext_stylesheet_files, $plaintext_template_files));
 277  
 278              // Check for theme name collision.
 279              if ( isset($mp_themes[$name]) ) {
 280                  $trump_cards = array(
 281                      'MailPress'    => 'MailPress Theme',
 282                      'nogent94'    => 'Nogent94',
 283                      self::default_theme_folder    => self::default_theme_name,
 284                      'nohtml'    => 'nohtml',
 285                  );
 286                  if ( isset( $trump_cards[ $stylesheet ] ) && $name == $trump_cards[ $stylesheet ] ) {
 287                      // If another theme has claimed to be one of our default themes, move
 288                      // them aside.
 289                      $suffix = $mp_themes[$name]['Stylesheet'];
 290                      $new_name = "$name/$suffix";
 291                      $mp_themes[$new_name] = $mp_themes[$name];
 292                      $mp_themes[$new_name]['Name'] = $new_name;
 293                  } else {
 294                      $name = "$name/$stylesheet";
 295                  }
 296              }
 297  
 298              $this->theme_roots[$stylesheet] = str_replace( MP_CONTENT_DIR, '', $theme_root );
 299              $mp_themes[$name] = array(
 300                  'Name'                 => $name,
 301                  'Title'                 => $title,
 302                  'Description'             => $description,
 303                  'Author'                 => $author,
 304                  'Author Name'             => $theme_data['AuthorName'],
 305                  'Author URI'             => $theme_data['AuthorURI'],
 306                  'Version'                 => $version,
 307                  'Template'                 => $template,
 308                  'Stylesheet'             => $stylesheet,
 309                  'Template Files'             => $template_files,
 310                  'Plaintext Template Files'     => $plaintext_template_files,
 311                  'Stylesheet Files'         => $stylesheet_files,
 312                  'Template Dir'             => str_replace(ABSPATH, '', $template_dir),
 313                  'Plaintext Template Dir'     => str_replace(ABSPATH, '', $plaintext_template_dir),
 314                  'Stylesheet Dir'             => str_replace(ABSPATH, '', $stylesheet_dir),
 315                  'Plaintext Stylesheet Dir'     => str_replace(ABSPATH, '', $plaintext_stylesheet_dir),
 316                  'Status'                 => $theme_data['Status'],
 317                  'Screenshot'             => $screenshot,
 318                  'Tags'                 => $theme_data['Tags'],
 319                  'Theme Root'             => $theme_root,
 320                  'Theme Root URI'             => str_replace(ABSPATH, site_url() . '/' , $theme_root),
 321              );
 322          }
 323  
 324          unset($theme_files);
 325  
 326          /* Resolve theme dependencies. */
 327          $theme_names = array_keys( $mp_themes );
 328          foreach ( (array) $theme_names as $theme_name ) 
 329          {
 330              $mp_themes[$theme_name]['Parent Theme'] = '';
 331              if ( $mp_themes[$theme_name]['Stylesheet'] != $mp_themes[$theme_name]['Template'] ) 
 332              {
 333                  foreach ( (array) $theme_names as $parent_theme_name ) 
 334                  {
 335                      if ( ($mp_themes[$parent_theme_name]['Stylesheet'] == $mp_themes[$parent_theme_name]['Template']) && ($mp_themes[$parent_theme_name]['Template'] == $mp_themes[$theme_name]['Template']) ) 
 336                      {
 337                          $mp_themes[$theme_name]['Parent Theme'] = $mp_themes[$parent_theme_name]['Name'];
 338                          break;
 339                      }
 340                  }
 341              }
 342          }
 343          return $mp_themes;
 344      }
 345  
 346  	function get_theme($theme) 
 347      {
 348          $themes = $this->themes;
 349      
 350          if ( array_key_exists($theme, $themes) )    
 351              return $themes[$theme];
 352  
 353          return null;
 354      }
 355  
 356  	function get_current_theme() 
 357      {
 358          if ( $theme = get_option(self::option_current_theme) )
 359              return $theme;
 360  
 361          $themes = $this->themes;
 362          $current_theme = self::default_theme_name;
 363  
 364          if ( $themes ) {
 365              $theme_names = array_keys($themes);
 366              $current_template = get_option(self::option_template);
 367              $current_stylesheet = get_option(self::option_stylesheet);
 368  
 369              foreach ( (array) $theme_names as $theme_name ) {
 370                  if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
 371                          $themes[$theme_name]['Template'] == $current_template ) {
 372                      $current_theme = $themes[$theme_name]['Name'];
 373                      break;
 374                  }
 375              }
 376          }
 377  
 378          update_option(self::option_current_theme, $current_theme);
 379  
 380          return $current_theme;
 381      }
 382  
 383  	function get_theme_directories()
 384      {
 385          $theme_directories[] = $this->get_theme_root();
 386  
 387          $theme_directories = apply_filters('MailPress_theme_directories', $theme_directories);
 388          foreach($theme_directories as $key => $theme_directory)
 389          {
 390              $theme_directory = ( !file_exists( $theme_directory ) ) ? MP_CONTENT_DIR . '/' . $theme_directory : $theme_directory;
 391              if ( !file_exists( $theme_directory ) || (strpos($theme_directory, ABSPATH) !== 0) ) unset($theme_directories[$key]);
 392          }
 393  
 394          return $theme_directories;
 395      }
 396  
 397  	function search_theme_directories() 
 398      {
 399          global $mp_theme_directories, $mp_broken_themes;
 400  
 401          if ( empty( $mp_theme_directories ) ) return false;
 402  
 403          $theme_files = array();
 404          $mp_broken_themes = array();
 405  
 406          foreach ( (array) $mp_theme_directories as $theme_root ) {
 407              /* Files in the root of the current theme directory and one subdir down */
 408              $themes_dir = @ opendir($theme_root);
 409  
 410              if ( !$themes_dir )
 411                  return false;
 412  
 413              while ( ($theme_dir = readdir($themes_dir)) !== false ) {
 414                  if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
 415                      if ( $theme_dir{0} == '.' || $theme_dir == 'CVS' )
 416                          continue;
 417  
 418                      $stylish_dir = @opendir($theme_root . '/' . $theme_dir);
 419                      $found_stylesheet = false;
 420  
 421                      while ( ($theme_file = readdir($stylish_dir)) !== false ) {
 422                          if ( $theme_file == 'style.css' ) {
 423                              $theme_files[$theme_dir] = array( 'theme_file' => $theme_dir . '/' . $theme_file, 'theme_root' => $theme_root );
 424                              $found_stylesheet = true;
 425                              break;
 426                          }
 427                      }
 428                      @closedir($stylish_dir);
 429  
 430                      if ( !$found_stylesheet ) { // look for themes in that dir
 431                          $subdir = "$theme_root/$theme_dir";
 432                          $subdir_name = $theme_dir;
 433                          $theme_subdirs = @ opendir( $subdir );
 434  
 435                          $found_subdir_themes = false;
 436                          while ( ($theme_subdir = readdir($theme_subdirs)) !== false ) {
 437                              if ( is_dir( $subdir . '/' . $theme_dir) && is_readable($subdir . '/' . $theme_dir) ) {
 438                                  if ( $theme_dir{0} == '.' || $theme_dir == 'CVS' )
 439                                      continue;
 440  
 441                                  $stylish_dir = @ opendir($subdir . '/' . $theme_subdir);
 442                                  $found_stylesheet = false;
 443  
 444                                  while ( ($theme_file = readdir($stylish_dir)) !== false ) {
 445                                      if ( $theme_file == 'style.css' ) {
 446                                          $theme_files["$theme_dir/$theme_subdir"] = array( 'theme_file' => $subdir_name . '/' . $theme_subdir . '/' . $theme_file, 'theme_root' => $theme_root );
 447                                          $found_stylesheet = true;
 448                                          $found_subdir_themes = true;
 449                                          break;
 450                                      }
 451                                  }
 452                                  @closedir($stylish_dir);
 453                              }
 454                          }
 455                          @closedir($theme_subdirs);
 456                          if ( !$found_subdir_themes )
 457                              $mp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'), 'Folder' => basename($subdir));
 458                      }
 459                  }
 460              }
 461              @closedir( $themes_dir );
 462          }
 463          return $theme_files;
 464      }
 465  
 466  	function get_theme_root($folder = false) 
 467      {
 468          if ( $folder && ( $theme_root = $this->get_raw_theme_root($folder) ) ) $theme_root = ABSPATH . MP_PATH_CONTENT . $theme_root;
 469          else $theme_root = ABSPATH . MP_PATH_CONTENT . 'themes';
 470  
 471          return apply_filters('MailPress_theme_root', $theme_root, $folder);
 472      }
 473  
 474  	function get_raw_theme_root($folder) 
 475      {
 476          global $mp_theme_directories;
 477   
 478          if ( count($mp_theme_directories) <= 1 ) return 'themes';
 479  
 480          return ( empty($this->theme_roots[$folder]) ) ? false : $theme_root = $this->theme_roots[$folder];
 481      }
 482  
 483  	function switch_theme($template, $stylesheet) 
 484      {
 485          update_option(self::option_template, $template);
 486          update_option(self::option_stylesheet, $stylesheet);
 487  
 488          delete_option(self::option_current_theme);
 489          $theme = $this->get_current_theme();
 490  
 491          do_action('MailPress_switch_theme', $theme);
 492      }
 493  
 494  	function validate_current_theme() 
 495      {
 496          if ( $this->get_template() != self::default_theme_folder && !is_file($this->get_template_directory() . '/index.php') ) {
 497              $this->switch_theme(self::default_theme_folder, self::default_theme_folder);
 498              return false;
 499          }
 500  
 501          if ( $this->get_stylesheet() != self::default_theme_folder && !is_file($this->get_template_directory() . '/style.css') ) {
 502              $this->switch_theme(self::default_theme_folder, self::default_theme_folder);
 503              return false;
 504          }
 505  
 506          if ( $this->is_child_theme() && !is_file($this->get_stylesheet_directory() . '/style.css' ) ) {
 507              $this->switch_theme(self::default_theme_folder, self::default_theme_folder);
 508              return false;
 509          }
 510          return true;
 511      }
 512  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1