[ Index ]

PHP Cross Reference of PageLines Framework

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 Framework
  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 === true )
  42              delete_transient( 'pagelines_sections_cache' );
  43  
  44          /**
  45          * Load our main section folders
  46          * @filter pagelines_section_dirs
  47          */
  48          $section_dirs =  array(
  49  
  50              'child'        => PL_EXTEND_DIR,
  51              'parent'    => PL_SECTIONS
  52              );
  53  
  54          if ( is_child_theme() && is_dir( get_stylesheet_directory()  . '/sections' ) )
  55              $section_dirs = array_merge( array( 'custom' => get_stylesheet_directory()  . '/sections' ), $section_dirs );
  56  
  57          $section_dirs = apply_filters( 'pagelines_sections_dirs', $section_dirs );
  58  
  59          /**
  60          * If cache exists load into $sections array
  61          * If not populate array and prime cache
  62          */
  63          if ( ! $sections = get_transient( 'pagelines_sections_cache' ) ) {
  64  
  65              foreach ( $section_dirs as $type => $dir ) {
  66                  $sections[$type] = $this->pagelines_getsections( $dir, $type );
  67              }
  68  
  69              // check for deps within the main parent sections, load last if found.
  70              foreach ($sections['parent'] as $key => $section ) {
  71  
  72                  if ( !empty($section['depends']) ) {
  73                      unset($sections['parent'][$key]);
  74                      $sections['parent'][$key] = $section;
  75                  }
  76              }
  77              /**
  78              * TODO switch this to activation/deactivation interface
  79              * TODO better idea, clear cached vars on settings save.
  80              */
  81              set_transient( 'pagelines_sections_cache', $sections, 86400 );
  82          }
  83  
  84          if ( true === $echo )
  85              return $sections;
  86  
  87          // filter main array containing child and parent and any custom sections
  88          $sections = apply_filters( 'pagelines_section_admin', $sections );
  89          $disabled = get_option( 'pagelines_sections_disabled', array( 'child' => array(), 'parent' => array(), 'custom' => array() ) );
  90  
  91          foreach ( $sections as $type ) {
  92              if(is_array($type)){
  93  
  94                  foreach( $type as $section ) {
  95  
  96                      if ( ! isset( $section['loadme'] ) )
  97                          $section['loadme'] = false;
  98  
  99                      if ( 'parent' == $section['type'] || ! is_multisite() ) {
 100                          $section['loadme'] = true;
 101                      }
 102                      /**
 103                      * Checks to see if we are a child section, if so disable the parent
 104                      * Also if a parent section and disabled, skip.
 105                      */
 106                      if ( 'parent' != $section['type'] && isset( $sections['parent'][$section['class']]) )
 107                          $disabled['parent'][$section['class']] = true;
 108  
 109                      if (isset( $disabled[$section['type']][$section['class']] ) && ! $section['persistant'] )
 110                          continue;
 111  
 112                      // consolidate array vars
 113                      $dep = ( 'parent' != $section['type'] && $section['depends'] != '') ? $section['depends'] : null;
 114                      $parent_dep = (isset($sections['parent'][$section['depends']])) ? $sections['parent'][$section['depends']] : null;
 115  
 116                      $dep_data = array(
 117                          'base_dir'  => (isset($parent_dep['base_dir'])) ? $parent_dep['base_dir'] : null,
 118                          'base_url'  => (isset($parent_dep['base_url'])) ? $parent_dep['base_url'] : null,
 119                          'base_file' => (isset($parent_dep['base_file'])) ? $parent_dep['base_file'] : null,
 120                          'name'        => (isset($parent_dep['name'])) ? $parent_dep['name'] : null
 121                      );
 122  
 123                      $section_data = array(
 124                          'base_dir'  => $section['base_dir'],
 125                          'base_url'  => $section['base_url'],
 126                          'base_file' => $section['base_file'],
 127                          'name'        => $section['name']
 128                      );
 129                      if ( isset( $dep ) && $section['loadme'] ) { // do we have a dependency?
 130                          if ( !class_exists( $dep ) && is_file( $dep_data['base_file'] ) ) {
 131                              include( $dep_data['base_file'] );
 132                              $pl_section_factory->register( $dep, $dep_data );
 133                          }
 134                          // dep loaded...
 135                          if ( !class_exists( $section['class'] ) && is_file( $section['base_file'] ) ) {
 136                              include( $section['base_file'] );
 137                              $pl_section_factory->register( $section['class'], $section_data );
 138                          }
 139                      } else {
 140                              if ( !class_exists( $section['class'] ) && is_file( $section['base_file'] ) && ! isset( $disabled['parent'][$section['depends']] ) ) {
 141                                  include( $section['base_file'] );
 142                                  $pl_section_factory->register( $section['class'], $section_data );
 143                              }
 144                      }
 145                  }
 146              }
 147          }
 148          pagelines_register_hook('pagelines_register_sections'); // Hook
 149      }
 150      /**
 151       *
 152       * Helper function
 153       * Returns array of section files.
 154       * @return array of php files
 155       * @author Simon Prosser
 156       **/
 157  	function pagelines_getsections( $dir, $type ) {
 158  
 159          if ( 'parent' != $type && ! is_dir($dir) )
 160              return;
 161  
 162          if ( is_multisite() ) {
 163              $store_sections = $this->get_latest_cached( 'sections' );
 164          }
 165  
 166          $default_headers = array(
 167              'External'        => 'External',
 168              'Demo'            => 'Demo',
 169              'tags'            => 'Tags',
 170              'version'        => 'Version',
 171              'author'        => 'Author',
 172              'authoruri'        => 'Author URI',
 173              'section'        => 'Section',
 174              'description'    => 'Description',
 175              'classname'        => 'Class Name',
 176              'depends'        => 'Depends',
 177              'workswith'        => 'workswith',
 178              'edition'        => 'edition',
 179              'cloning'        => 'cloning',
 180              'failswith'        => 'failswith',
 181              'tax'            => 'tax',
 182              'persistant'    => 'Persistant',
 183              'format'        => 'Format',
 184              'classes'        => 'Classes'
 185              );
 186  
 187          $sections = array();
 188  
 189          // setup out directory iterator.
 190          // symlinks were only supported after 5.3.1
 191          // so we need to check first ;)
 192          $it = ( strnatcmp( phpversion(), '5.3.1' ) >= 0 ) ? new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, FilesystemIterator::FOLLOW_SYMLINKS) , RecursiveIteratorIterator::SELF_FIRST ) : new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, RecursiveIteratorIterator::CHILD_FIRST ) );
 193  
 194          foreach( $it as $fullFileName => $fileSPLObject ) {
 195  
 196              if ( basename( $fullFileName) == PL_EXTEND_SECTIONS_PLUGIN )
 197                  continue;
 198  
 199              if (pathinfo($fileSPLObject->getFilename(), PATHINFO_EXTENSION ) == 'php') {
 200  
 201                  $base_url = null;
 202                  $base_dir = null;
 203                  $load = true;
 204                  $price = '';
 205                  $uid = '';
 206                  $headers = get_file_data( $fullFileName, $default_headers );
 207  
 208                  // If no pagelines class headers ignore this file.
 209                  if ( !$headers['classname'] )
 210                      continue;
 211  
 212                  preg_match( '#[\/|\-]sections[\/|\\\]([^\/|\\\]+)#', $fullFileName, $out );
 213  
 214                   $version = ( '' != $headers['version'] ) ? $headers['version'] : PL_CORE_VERSION;
 215  
 216                  $folder = sprintf( '/%s', $out[1] );
 217  
 218                  $base_dir = get_template_directory()  . '/sections' . $folder;
 219  
 220                  if ( 'child' == $type ) {
 221  
 222                      $base_url =  PL_EXTEND_URL . $folder;
 223                      $base_dir =  PL_EXTEND_DIR . $folder;
 224  
 225                  }
 226  
 227                  if ( 'custom' == $type ) {
 228  
 229                      $base_url =  get_stylesheet_directory_uri()  . '/sections' . $folder;
 230                      $base_dir =  get_stylesheet_directory()  . '/sections' . $folder;
 231  
 232                  }
 233  
 234                  /*
 235                  * Look for custom dirs.
 236                  */
 237                  if ( 'custom' != $type && 'child' != $type && 'parent' != $type ) {
 238  
 239                      // prepare url
 240                      $file = basename( $dir );
 241                      $path = plugin_dir_path( $file );
 242                      $url = plugins_url( $file );
 243  
 244                      $base_url = sprintf( '%s/sections%s', $url, $folder );
 245                      $base_dir =  sprintf( '%ssections%s', $dir, $folder );;
 246  
 247                  }
 248                  $base_dir = ( isset( $base_dir ) ) ? $base_dir : PL_SECTIONS . $folder;
 249                  $base_url = ( isset( $base_url ) ) ? $base_url : PL_SECTION_ROOT . $folder;
 250  
 251                  // do we need to load this section?
 252                  if ( 'child' == $type && is_multisite() ) {
 253                      $load = false;
 254                      $slug = basename( $folder );
 255                      $purchased = ( isset( $store_sections->$slug->purchased ) ) ? $store_sections->$slug->purchased : '';
 256                      $plus = ( isset( $store_sections->$slug->plus_product ) ) ? $store_sections->$slug->plus_product : '';
 257                      $price = ( isset( $store_sections->$slug->price ) ) ? $store_sections->$slug->price : '';
 258                      $uid = ( isset( $store_sections->$slug->uid ) ) ? $store_sections->$slug->uid : '';
 259                      if ( 'purchased' === $purchased ) {
 260                          $load = true;
 261                      } elseif( $plus && pagelines_check_credentials( 'plus' ) ) {
 262                          $load = true;
 263                      } else {
 264  
 265                          $disabled = get_option( 'pagelines_sections_disabled', array( 'child' => array(), 'parent' => array() ) );
 266  
 267                          if ( ! isset( $disabled['child'][$headers['classname']] ) )
 268                              $load = true;
 269                      }
 270                  }
 271                  if ( $load )
 272                      $purchased = 'purchased';
 273                  $sections[$headers['classname']] = array(
 274                      'class'            => $headers['classname'],
 275                      'depends'        => $headers['depends'],
 276                      'type'            => $type,
 277                      'tags'            => $headers['tags'],
 278                      'author'        => $headers['author'],
 279                      'version'        => $version,
 280                      'authoruri'        => ( isset( $headers['authoruri'] ) ) ? $headers['authoruri'] : '',
 281                      'description'    => $headers['description'],
 282                      'name'            => $headers['section'],
 283                      'base_url'        => $base_url,
 284                      'base_dir'        => $base_dir,
 285                      'base_file'        => $fullFileName,
 286                      'workswith'        => ( $headers['workswith'] ) ? array_map( 'trim', explode( ',', $headers['workswith'] ) ) : '',
 287                      'edition'        => $headers['edition'],
 288                      'cloning'        => ( 'true' === $headers['cloning'] ) ? true : '',
 289                      'failswith'        => ( $headers['failswith'] ) ? array_map( 'trim', explode( ',', $headers['failswith'] ) ) : '',
 290                      'tax'            => $headers['tax'],
 291                      'demo'            => $headers['Demo'],
 292                      'external'        => $headers['External'],
 293                      'persistant'    => $headers['persistant'],
 294                      'format'        => $headers['format'],
 295                      'classes'        => $headers['classes'],
 296                      'screenshot'    => ( is_file( $base_dir . '/thumb.png' ) ) ? $base_url . '/thumb.png' : '',
 297                      'less'            => ( is_file( $base_dir . '/color.less' ) || is_file( $base_dir . '/style.less' ) ) ? true : false,
 298                      'loadme'        => $load,
 299                      'price'            => $price,
 300                      'purchased'        => $purchased,
 301                      'uid'            => $uid
 302                  );
 303              }
 304          }
 305          return $sections;
 306      }
 307  
 308  	function register_sidebars() {
 309  
 310          // This array contains the sidebars in the correct order.
 311          $sidebars = array(
 312  
 313              'sb_primary' => array(
 314                  'name'    =>    __( 'Primary Sidebar', 'pagelines' ),
 315                  'description'    =>    __( 'The main widgetized sidebar.', 'pagelines')
 316              ),
 317              'sb_secondary' => array(
 318                  'name'    =>    sprintf( '%s%s', __( 'Secondary Sidebar', 'pagelines' ), ( !VPRO ) ? ' (Pro Only)' : '' ),
 319                  'description'    =>    __( 'The secondary widgetized sidebar for the theme.', 'pagelines')
 320              ),
 321              'sb_tertiary' => array(
 322                  'name'    =>    __( 'Tertiary Sidebar', 'pagelines' ),
 323                  'description'    =>    __( 'A 3rd widgetized sidebar for the theme that can be used in standard sidebar templates.', 'pagelines')
 324              ),
 325              'sb_universal' => array(
 326                  'name'    =>    __( 'Universal Sidebar', 'pagelines' ),
 327                  'description'    =>    __( 'A universal widgetized sidebar', 'pagelines'),
 328                  'pro'    => true
 329              ),
 330              'sb_fullwidth' => array(
 331                  'name'    =>    __( 'Full Width Sidebar', 'pagelines' ),
 332                  'description'    =>    __( 'Shows full width widgetized sidebar.', 'pagelines')
 333              ),
 334              'sb_content' => array(
 335                  'name'    =>    __( 'Content Sidebar', 'pagelines' ),
 336                  'description'    =>    __( 'Displays a widgetized sidebar inside the main content area. Set it up in the widgets panel.', 'pagelines')
 337              ),
 338          );
 339          foreach( $sidebars as $key => $sidebar ) {
 340              if ( isset( $sidebar['pro'] ) && ! VPRO )
 341                  continue;
 342              pagelines_register_sidebar( pagelines_standard_sidebar( $sidebar['name'], $sidebar['description'] ) );
 343          }
 344      }
 345  
 346      /**
 347      * Simple cache.
 348      * @return object
 349      */
 350  	function get_latest_cached( $type, $flush = null ) {
 351  
 352          $url = trailingslashit( PL_API . $type );
 353          $options = array(
 354              'body' => array(
 355                  'username'    =>    ( $this->username != '' ) ? $this->username : false,
 356                  'password'    =>    ( $this->password != '' ) ? $this->password : false,
 357                  'flush'        =>    $flush
 358              )
 359          );
 360  
 361          if ( false === ( $api_check = get_transient( 'pagelines_extend_' . $type ) ) ) {
 362  
 363              // ok no transient, we need an update...
 364  
 365              $response = pagelines_try_api( $url, $options );
 366  
 367              if ( $response !== false ) {
 368  
 369                  // ok we have the data parse and store it
 370  
 371                  $api = wp_remote_retrieve_body( $response );
 372                  set_transient( 'pagelines_extend_' . $type, true, 86400 );
 373                  update_option( 'pagelines_extend_' . $type, $api );
 374              }
 375  
 376          }
 377          $api = get_option( 'pagelines_extend_' . $type, false );
 378  
 379          if( ! $api )
 380              return __( '<h2>Unable to fetch from API</h2>', 'pagelines' );
 381  
 382          return json_decode( $api );
 383      }
 384  
 385  } // end class


Generated: Sat Apr 6 23:00:27 2013 Cross-referenced by PHPXref 0.7.1