[ Index ]

PHP Cross Reference of PageLines DMS

title

Body

[close]

/includes/ -> class.register.php (source)

   1  <?php
   2  /**
   3   * Controls and Manages PageLines Extension
   4   *
   5   *
   6   *
   7   * @author        PageLines
   8   * @copyright    2011 PageLines
   9   */
  10  
  11  class PageLinesRegister {
  12  
  13  	function __construct() {
  14          $this->username = get_pagelines_credentials( 'user' );
  15          $this->password = get_pagelines_credentials( 'pass' );
  16      }
  17      /**
  18       *  Scans THEMEDIR/sections recursively for section files and auto loads them.
  19       *  Child section folder also scanned if found and dependencies resolved.
  20       *
  21       *  Section files MUST include a class header and optional depends header.
  22       *
  23       *  Example section header:
  24       *
  25       *    Section: BrandNav Section
  26       *    Author: PageLines
  27       *    Description: Branding and Nav Inline
  28       *    Version: 1.0.0
  29       *    Class Name: BrandNav
  30       *    Depends: PageLinesNav
  31       *
  32       *  @package PageLines DMS
  33       *  @subpackage Config
  34       *  @since 2.0
  35       *
  36       */
  37  	function pagelines_register_sections( $reset = null, $echo = null ){
  38  
  39          global $pl_section_factory;
  40          
  41          if ( $reset )
  42              delete_transient( 'pagelines_sections_cache' );
  43  
  44          $section_dirs = pl_get_section_dirs();
  45  
  46          /**
  47          * If cache exists load into $sections array
  48          * If not populate array and prime cache
  49          */
  50          if ( ! $sections = get_transient( 'pagelines_sections_cache' ) ) {
  51  
  52              foreach ( $section_dirs as $type => $dir )
  53                  $sections[ $type ] = $this->get_sections( $dir, $type );
  54  
  55              /**
  56              * 
  57              * Load special plugin/sections
  58              */
  59              $editorsections = new PLSectionsRegister;
  60              $sections['editor'] = $editorsections->get_all_plugins();
  61  
  62              // check for deps within the main parent sections, load last if found.
  63              foreach ( $sections['parent'] as $key => $section ) {
  64  
  65                  if ( !empty( $section['depends'] ) ) {
  66                      unset( $sections['parent'][ $key ] );
  67                      $sections['parent'][ $key ] = $section;
  68                  }
  69              }
  70              /**
  71              * TODO switch this to activation/deactivation interface
  72              * TODO better idea, clear cached vars on settings save.
  73              */
  74              set_transient( 'pagelines_sections_cache', $sections, 86400 );
  75          }
  76  
  77          if ( $echo )
  78              return $sections;
  79  
  80          // filter main array containing child and parent and any custom sections
  81          $sections = apply_filters( 'pagelines_section_admin', $sections );
  82          $disabled = pl_get_disabled_sections();
  83  
  84          foreach ( $sections as $type ) {
  85  
  86              if ( !is_array( $type ) )
  87                  continue;
  88  
  89              foreach ( $type as $section ) {
  90  
  91                  if ( ! isset( $section['loadme'] ) )
  92                      $section['loadme'] = false;
  93  
  94                  if ( 'parent' == $section['type'] || ! is_multisite() )
  95                      $section['loadme'] = true;
  96                  
  97                  /**
  98                  * Checks to see if we are a child section, if so disable the parent
  99                  * Also if a parent section and disabled, skip.
 100                  */
 101                  if ( 'parent' != $section['type'] && isset( $sections['parent'][ $section['class'] ] ) )
 102                      $disabled['parent'][ $section['class'] ] = true;
 103  
 104                  if ( isset( $disabled[ $section['type'] ][ $section['class'] ] ) && ! $section['persistant'] )
 105                      continue;
 106  
 107                  // consolidate array vars
 108                  $dep = ( 'parent' != $section['type'] && $section['depends'] ) ? $section['depends'] : null;
 109                  $parent_dep = isset( $sections['parent'][ $section['depends'] ] ) ? $sections['parent'][ $section['depends'] ] : null;
 110  
 111                  $dep_data = array(
 112                      'base_dir'  => isset( $parent_dep['base_dir'] )        ? $parent_dep['base_dir']    : null,
 113                      'base_url'  => isset( $parent_dep['base_url'] )        ? $parent_dep['base_url']    : null,
 114                      'base_file' => isset( $parent_dep['base_file'] )    ? $parent_dep['base_file']    : null,
 115                      'name'        => isset( $parent_dep['name'] )            ? $parent_dep['name']        : null
 116                  );
 117  
 118                  $section_data = array(
 119                      'base_dir'  => $section['base_dir'],
 120                      'base_url'  => $section['base_url'],
 121                      'base_file' => $section['base_file'],
 122                      'name'        => $section['name']
 123                  );
 124  
 125                  if ( isset( $dep ) && $section['loadme'] ) { // do we have a dependency?
 126                      if ( !class_exists( $dep ) && is_file( $dep_data['base_file'] ) ) {
 127                          include( $dep_data['base_file'] );
 128                          $pl_section_factory->register( $dep, $dep_data );
 129                      }
 130                      // dep loaded...
 131                      if ( !class_exists( $section['class'] ) && class_exists( $dep ) && is_file( $section['base_file'] ) ) {
 132                          include( $section['base_file'] );
 133                          $pl_section_factory->register( $section['class'], $section_data );
 134                      }
 135                  } else {
 136                      if ( !class_exists( $section['class'] ) && is_file( $section['base_file'] ) && ! isset( $disabled['parent'][$section['depends']] ) ) {
 137                          include( $section['base_file'] );
 138                          $pl_section_factory->register( $section['class'], $section_data );
 139                      }
 140                  }
 141              }
 142          }
 143          pagelines_register_hook('pagelines_register_sections'); // Hook
 144      }
 145      /**
 146       *
 147       * Helper function
 148       * Returns array of section files.
 149       * @return array of php files
 150       * @author Simon Prosser
 151       **/
 152  	function get_sections( $dir, $type ) {
 153  
 154          if ( 'parent' != $type && ! is_dir( $dir ) )
 155              return;
 156  
 157          if ( is_multisite() )
 158              $store_sections = $this->get_latest_cached( 'sections' );
 159  
 160          $default_headers = array(
 161              'External'        => 'External',
 162              'Demo'            => 'Demo',
 163              'tags'            => 'Tags',
 164              'version'        => 'Version',
 165              'author'        => 'Author',
 166              'authoruri'        => 'Author URI',
 167              'section'        => 'Section',
 168              'description'    => 'Description',
 169              'classname'        => 'Class Name',
 170              'depends'        => 'Depends',
 171              'workswith'        => 'workswith',
 172              'isolate'        => 'isolate',
 173              'edition'        => 'edition',
 174              'cloning'        => 'cloning',
 175              'failswith'        => 'failswith',
 176              'tax'            => 'tax',
 177              'persistant'    => 'Persistant',
 178              'format'        => 'Format',
 179              'classes'        => 'Classes',
 180              'filter'        => 'Filter',
 181              'loading'        => 'Loading'
 182          );
 183  
 184          $sections = array();
 185  
 186          // setup out directory iterator.
 187          // symlinks were only supported after 5.3.1
 188          // so we need to check first ;)
 189          $it = ( strnatcmp( phpversion(), '5.3.1' ) >= 0 )
 190              ? new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, FilesystemIterator::FOLLOW_SYMLINKS        ) , RecursiveIteratorIterator::SELF_FIRST )
 191              : new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, RecursiveIteratorIterator::CHILD_FIRST    )
 192          );
 193  
 194          foreach ( $it as $fullFileName => $fileSPLObject ) {
 195  
 196              if ( basename( $fullFileName ) == PL_EXTEND_SECTIONS_PLUGIN )
 197                  continue;
 198  
 199              if ( 'php' != pathinfo( $fileSPLObject->getFilename(), PATHINFO_EXTENSION ) )
 200                  continue;
 201  
 202              $base_url = null;
 203              $base_dir = null;
 204              $load     = true;
 205              $price    = '';
 206              $uid      = '';
 207              $headers  = get_file_data( $fullFileName, $default_headers );
 208  
 209              // If no pagelines class headers ignore this file.
 210              // beyond this point $fullFileName should refer to a section.php
 211              if ( !$headers['classname'] )
 212                  continue;
 213              
 214              preg_match( '#[\/|\-]sections[\/|\\\]([^\/|\\\]+)#', $fullFileName, $out );
 215              $folder = sprintf( '/%s', $out[1] );
 216              
 217              // base values
 218              $version  = $headers['version'] ? $headers['version'] : PL_CORE_VERSION;
 219              $base_dir = PL_SECTIONS . $folder;
 220              $base_url = PL_SECTION_ROOT . $folder;
 221  
 222              if ( 'child' == $type ) {
 223                  $base_url =  PL_EXTEND_URL . $folder;
 224                  $base_dir =  PL_EXTEND_DIR . $folder;
 225              }
 226              if ( 'custom' == $type ) {
 227                  $base_url =  get_stylesheet_directory_uri()  . '/sections' . $folder;
 228                  $base_dir =  get_stylesheet_directory()  . '/sections' . $folder;
 229              }
 230  
 231              /*
 232              * Look for custom dirs.
 233              */
 234              if ( !in_array( $type, array( 'custom', 'child', 'parent', 'editor' ) ) ) {
 235  
 236                  // Ok so we're a plugin then.. if not active then bypass.
 237                  $plugin_slug = $type;
 238  
 239                  // base plugin path
 240                  $plugin = sprintf( '%s/%s.php', $plugin_slug, $plugin_slug );
 241  
 242                  $check = str_replace('\\', '/', $fullFileName); // must convert backslashes before preg_match
 243                  preg_match( '#\/sections\/([^\/]+)#', $check, $out );
 244  
 245                  // check for active container plugin and existing individual section directory
 246                  if ( ! is_plugin_active( $plugin ) || ! isset( $out[1] ) )
 247                      continue;
 248  
 249                  $section_slug = $out[1];
 250  
 251                  $base_url = sprintf( '%s/sections/%s',
 252                      untrailingslashit( plugins_url( $plugin_slug ) ),
 253                      $section_slug
 254                  );
 255                  $base_dir = dirname( $fullFileName );
 256              }
 257  
 258  
 259              // do we need to load this section?
 260              if ( 'child' == $type && is_multisite() ) {
 261                  $load      = false;
 262                  $slug      = basename( $folder );
 263                  $purchased = ( isset( $store_sections->$slug->purchased ) ) ? $store_sections->$slug->purchased : '';
 264                  $plus      = ( isset( $store_sections->$slug->plus_product ) ) ? $store_sections->$slug->plus_product : '';
 265                  $price     = ( isset( $store_sections->$slug->price ) ) ? $store_sections->$slug->price : '';
 266                  $uid       = ( isset( $store_sections->$slug->uid ) ) ? $store_sections->$slug->uid : '';
 267  
 268                  if ( 'purchased' === $purchased )
 269                      $load = true;
 270                  elseif ( $plus && pagelines_check_credentials( 'plus' ) )
 271                      $load = true;
 272                  else {
 273                      $disabled = pl_get_disabled_sections();
 274  
 275                      if ( ! isset( $disabled['child'][ $headers['classname'] ] ) )
 276                          $load = true;
 277                  }
 278              }
 279  
 280              if ( $load )
 281                  $purchased = 'purchased';
 282  
 283  
 284              $sections[ $headers['classname'] ] = array(
 285                  'class'            => $headers['classname'],
 286                  'depends'        => $headers['depends'],
 287                  'type'            => $type,
 288                  'tags'            => $headers['tags'],
 289                  'author'        => $headers['author'],
 290                  'version'        => $version,
 291                  'authoruri'        => ( isset( $headers['authoruri'] ) ) ? $headers['authoruri'] : '',
 292                  'description'    => $headers['description'],
 293                  'name'            => $headers['section'],
 294                  'base_url'        => $base_url,
 295                  'base_dir'        => $base_dir,
 296                  'base_file'        => $fullFileName,
 297                  'workswith'        => ( $headers['workswith'] ) ? array_map( 'trim', explode( ',', $headers['workswith'] ) ) : '',
 298                  'isolate'        => ( $headers['isolate'] ) ? array_map( 'trim', explode( ',', $headers['isolate'] ) ) : '',
 299                  'edition'        => $headers['edition'],
 300                  'cloning'        => ( 'true' === $headers['cloning'] ) ? true : '',
 301                  'failswith'        => ( $headers['failswith'] ) ? array_map( 'trim', explode( ',', $headers['failswith'] ) ) : '',
 302                  'tax'            => $headers['tax'],
 303                  'demo'            => $headers['Demo'],
 304                  'external'        => $headers['External'],
 305                  'persistant'    => $headers['persistant'],
 306                  'format'        => $headers['format'],
 307                  'classes'        => $headers['classes'],
 308                  'screenshot'    => ( is_file( $base_dir . '/thumb.png' ) ) ? $base_url . '/thumb.png' : '',
 309                  'less'            => ( is_file( $base_dir . '/color.less' ) || is_file( $base_dir . '/style.less' ) ) ? true : false,
 310                  'loadme'        => $load,
 311                  'price'            => $price,
 312                  'purchased'        => $purchased,
 313                  'uid'            => $uid,
 314                  'filter'        => $headers['filter'],
 315                  'loading'        => $headers['loading']
 316              );
 317          }
 318  
 319          return $sections;
 320      }
 321  
 322  
 323  
 324      /**
 325      * Simple cache.
 326      * @return object
 327      */
 328  	function get_latest_cached( $type, $flush = null ) {
 329  
 330          $url = trailingslashit( PL_API . $type );
 331          $options = array(
 332              'body' => array(
 333                  'username'    =>    ( $this->username != '' ) ? $this->username : false,
 334                  'password'    =>    ( $this->password != '' ) ? $this->password : false,
 335                  'flush'        =>    $flush
 336              )
 337          );
 338  
 339          if ( false === ( $api_check = get_transient( 'pagelines_extend_' . $type ) ) ) {
 340  
 341              // ok no transient, we need an update...
 342  
 343              $response = pagelines_try_api( $url, $options );
 344  
 345              if ( $response !== false ) {
 346  
 347                  // ok we have the data parse and store it
 348  
 349                  $api = wp_remote_retrieve_body( $response );
 350                  set_transient( 'pagelines_extend_' . $type, true, 86400 );
 351                  update_option( 'pagelines_extend_' . $type, $api );
 352              }
 353  
 354          }
 355          $api = get_option( 'pagelines_extend_' . $type, false );
 356  
 357          if( ! $api )
 358              return __( '<h2>Unable to fetch from API</h2>', 'pagelines' );
 359  
 360          return json_decode( $api );
 361      }
 362  
 363  } // PageLinesRegister
 364  
 365  function pl_get_section_dirs() {
 366  
 367      $section_dirs = array();
 368  
 369      $theme_sections_dir = PL_CHILD_DIR . '/sections';
 370  
 371      if ( is_child_theme() && is_dir( $theme_sections_dir ) )
 372          $section_dirs['custom'] = $theme_sections_dir;
 373  
 374      // load v3 section/plugins...
 375  
 376      include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
 377  
 378      foreach ( get_plugins() as $plugin => $data ) {
 379  
 380          $slug = dirname( $plugin );
 381          $path = path_join( WP_PLUGIN_DIR, "$slug/sections" );
 382  
 383          if ( is_dir( $path ) && is_plugin_active( $plugin ) )
 384              $section_dirs[ $slug ] = $path;
 385      }
 386      if ( ! pl_deprecate_v2() ) {
 387          $section_dirs['child'] = PL_EXTEND_DIR;
 388      }
 389  
 390      $section_dirs['parent'] = PL_SECTIONS;
 391  
 392      return apply_filters( 'pagelines_sections_dirs', $section_dirs );
 393  }
 394  
 395  function pl_get_disabled_sections() {
 396  
 397      // get all section types - including any added/removed by filters
 398      $types = array_keys( (array) pl_get_section_dirs() );
 399      // make sure the base type keys are all there even if they were filtered out
 400      $types = array_merge( array('child','parent','custom'), $types );
 401  
 402      $d = array();
 403      foreach ( $types as $type )
 404          $d[ $type ] = array();
 405  
 406      $saved = get_option( 'pagelines_sections_disabled', array() );
 407  
 408      return wp_parse_args( $saved, $d );
 409  }


Generated: Sun Dec 1 19:43:16 2013 Cross-referenced by PHPXref 0.7.1