Elastic
[ class tree: Elastic ] [ index: Elastic ] [ all elements ]

Source for file elastic.php

Documentation is available at elastic.php

  1. <?php
  2. /**
  3.  * Elastic API: Access to atomic hook functions, context, paths, and Elastic data (prefixes, theme data).
  4.  *
  5.  * @package Elastic
  6.  * @author Daryl Koopersmith
  7.  */
  8. /**
  9.  * Handles internal framework operation. Do not access directly. Instead, use {@link elastic_get()} and {@link elastic_set()}.
  10.  *
  11.  * @author Daryl Koopersmith
  12.  */
  13. class Elastic {
  14.     var $layout;
  15.     /**
  16.      * An array containing the context of a page.
  17.      * 
  18.      * Set through {}
  19.      *
  20.      * @var array 
  21.      */
  22.     var $context;
  23.     /**
  24.      * Contains the prefix used for most Elastic hooks and ids. Default 'elastic_'.
  25.      * Can be overridden through filter 'elastic_prefix'.
  26.      * 
  27.      * Warning: Override with care! The names other filters will change when Elastic::$prefix is overridden.
  28.      *
  29.      * @var string 
  30.      */
  31.     var $prefix;
  32.     /**
  33.      * Contains the prefix used for all Modules. Default 'module_'.
  34.      * Can be overridden through filter 'elastic_module_prefix'.
  35.      *
  36.      * @var string 
  37.      */
  38.     var $module_prefix;
  39.     /**
  40.      * An array of theme data obtained through {@link get_theme_data()}.
  41.      * Filtered hook: 'elastic_theme_data'.
  42.      *
  43.      * @var array 
  44.      */
  45.     var $theme_data;
  46.     /**
  47.      * An array of child theme data obtained through {@link get_theme_data()}.
  48.      * Filtered hook: 'elastic_child_data'.
  49.      *
  50.      * @var array 
  51.      */
  52.     var $child_data;
  53.     /**
  54.      * Are we running a child theme?
  55.      *
  56.      * @var boolean 
  57.      */
  58.     var $has_child;
  59.     /**
  60.      * An array of module names.
  61.      *
  62.      * @var array 
  63.      * @deprecated
  64.      * @todo Double check if Elastic::module_types is used anywhere.
  65.      */
  66.     var $module_types = array'header''content''sidebar' );
  67.     
  68.     /**
  69.      * Contains an array of relative paths. Access paths through {@link elastic_get_path()}.
  70.      *
  71.      * @var array 
  72.      */
  73.     var $path = array();
  74.     
  75.     /**
  76.      * Initializes and sets hooks for the Elastic framework
  77.      *
  78.      * @access private
  79.      * @return void 
  80.      * @author Daryl Koopersmith
  81.      */
  82.     function init({
  83.         $this->has_child = STYLESHEETPATH !== TEMPLATEPATH );
  84.         
  85.         // Set paths
  86.         $this->path['root''';
  87.         $this->path['library''library';
  88.         $this->path['classes'trailingslashit$this->path['library''classes';
  89.         $this->path['lib-css'trailingslashit$this->path['library''css';
  90.         $this->path['fallback-views'trailingslashit$this->path['library''fallback-views';
  91.         $this->path['custom''custom';
  92.                 
  93.         // Load classes
  94.         require_onceelastic_get_path('classes''/object.php');
  95.         require_onceelastic_get_path('classes''/module.php');
  96.         require_onceelastic_get_path('classes''/group.php');
  97.         require_onceelastic_get_path('classes''/selection.php');
  98.         require_onceelastic_get_path('classes''/sidebar.php');
  99.         require_onceelastic_get_path('classes''/header.php');
  100.         require_onceelastic_get_path('classes''/content.php');
  101.         
  102.         // Load user's functions.php
  103.         $functions TEMPLATEPATH '/custom/functions.php';
  104.         iffile_exists$functions ) )
  105.             include $functions;
  106.  
  107.         // Load child's functions.php            
  108.         $functions STYLESHEETPATH '/custom/functions.php';
  109.         if$this->has_child && file_exists$functions ) )
  110.             include $functions;
  111.         
  112.         // Set prefix for all hooks and ids.
  113.         $this->prefix = apply_filters('elastic_prefix','elastic_');
  114.         $this->module_prefix = apply_filters$this->prefix . 'module_prefix','module_');
  115.         
  116.         // Get theme and child theme data
  117.         $this->theme_data = apply_filters($this->prefix . 'theme_data'get_theme_data(TEMPLATEPATH '/style.css') );
  118.         $this->child_data = apply_filters($this->prefix . 'child_data'get_theme_data(STYLESHEETPATH '/style.css') );
  119.         
  120.         // Get layout
  121.         require_onceelastic_get_path('custom'(elastic_get('has_child')) 'child' 'theme' '/layout.php');
  122.         $this->layout = $layout;
  123.         
  124.         // Load styles
  125.         add_action('template_redirect'array(&$this'load_styles') );
  126.         
  127.         // Get context (once it's set)
  128.         add_action('template_redirect'array(&$this'get_context') );
  129.         // Get context now, for admin pages
  130.         $this->context = $this->get_context();
  131.         
  132.         // Register sidebars
  133.         ifis_admin() ) // Sidebars must be registered during admin_init, which is before template_redirect
  134.             add_action('init'array(&$this'register_sidebars') );
  135.         else // Sidebars are registered on template_redirect to ensure context has loaded
  136.             add_action('template_redirect'array(&$this'register_sidebars') );
  137.     }
  138.  
  139.     /**
  140.      * Loads user stylesheets (both parent and child theme) and base CSS reset.
  141.      * 
  142.      * CSS reset is a customized version of Tripoli.
  143.      *
  144.      * @access private
  145.      * @return void 
  146.      * @author Daryl Koopersmith
  147.      */
  148.     function load_styles({
  149.         global $wp_styles;
  150.         
  151.         wp_enqueue_style$this->prefix . 'tripoli'elastic_get_path('lib-css''uri''/tripoli.css'false'0.0.2.7');
  152.         wp_enqueue_style$this->prefix . 'tripoli-ie'elastic_get_path('lib-css''uri''/tripoli.ie.css'false'0.0.2.7');
  153.         $wp_styles->add_data$this->prefix . 'tripoli-ie''conditional''gte IE 5');
  154.         
  155.         wp_enqueue_style$this->prefix . 'style'elastic_get_path('custom''uri''/style.css'false'0.0.2.7');
  156.         ifelastic_get('has_child') )
  157.             wp_enqueue_style$this->prefix . 'style'elastic_get_path('custom''child''uri''/style.css'false'0.0.2.7');
  158.     }
  159.     
  160.     /**
  161.      * Registers all sidebar modules.
  162.      *
  163.      * @access private
  164.      * @return void 
  165.      * @author Daryl Koopersmith
  166.      */
  167.     function register_sidebars({
  168.         $sidebars elastic_get('layout');
  169.         $sidebars $sidebars->get_modules_by_type('sidebar');
  170.         
  171.         if$sidebars )
  172.             return;
  173.         
  174.         foreach$sidebars as $sidebar {
  175.             $settings $sidebar->apply_atomic'_register_sidebars'array(
  176.                 'name'          => $sidebar->id,
  177.                 'id'            => $sidebar->id,
  178.                 'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  179.                 'after_widget'  => "</li>",
  180.                 'before_title'  => '<h3 class="widget-title">',
  181.                 'after_title'   => '</h3>',
  182.                 )elastic_get('module_prefix') );
  183.             register_sidebar$settings );
  184.         }
  185.     }
  186.     
  187.     /**
  188.      * Retrieve the context of the current page.
  189.      *
  190.      * @access private
  191.      * @todo Potentially make this function public, and take in an optional $wp_query variable?
  192.      * @return array $context
  193.      * @author Chris Jean, Ptah Dunbar, Daryl Koopersmith
  194.      */
  195.     function get_context({
  196.         global $wp_query;
  197.  
  198.         $context array'global' => '''abstract' => null'general' => null'specific' => null );
  199.         
  200.         ifis_admin() ) {
  201.             $context['global''admin';
  202.             return $this->context = $context;
  203.         }
  204.         
  205.         $id $wp_query->get_queried_object_id();
  206.  
  207.         if is_front_page() )
  208.             $context['general''home';
  209.         else if is_singular() ) {
  210.             $context['abstract''singular';
  211.  
  212.             if is_attachment() ) {
  213.                 $context['general''attachment';
  214.                 $context['specific''attachment-'$id;
  215.             }
  216.             else if is_single() ) {
  217.                 $context['general''single';
  218.                 $context['specific''single-'$id;
  219.             }
  220.             else if is_page() ) {
  221.                 $context['general''page';
  222.                 $context['specific''page-'$id;
  223.             }
  224.         }
  225.         else if is_archive() ) {
  226.             $context['abstract''archive';
  227.  
  228.             if is_category() ) {
  229.                 $context['general''category';
  230.                 $context['specific''category-'$id;
  231.             }
  232.             else if is_tag() ) {
  233.                 $context['general''tag';
  234.                 $context['specific''tag-'$id;
  235.             }
  236.             else if is_date() ) {
  237.                 $context['general''date';
  238.  
  239.                 if is_month() )
  240.                     $context['specific''month-'$id;
  241.                 else if is_year() )
  242.                     $context['specific''year-'$id;
  243.                 else if is_day() )
  244.                     $context['specific''day-'$id;
  245.                 else if is_time() )
  246.                     $context['specific''time-' $id;
  247.             }
  248.             else if is_author() ) {
  249.                 $context['general''author';
  250.                 $context['specific''author-'$id;
  251.             }
  252.             else if is_tax() ) {
  253.                 $context['general''tax';
  254.                 $context['specific''tax-'$id;
  255.             }
  256.         }
  257.         else if is_search() )
  258.             $context['general''search';
  259.         else if is_404() )
  260.             $context['general''error404';
  261.  
  262.  
  263.         return $this->context = $context;
  264.     }
  265. }
  266.  
  267. // Make elastic object
  268. $elastic new Elastic();
  269. $elastic->init();
  270.  
  271. /**
  272.  * Gets data within the global  {@link Elastic} instance ($elastic).
  273.  *
  274.  * @param string $var 
  275.  * @global Elastic $elastic 
  276.  * @return mixed Requested variable ($elastic->var), or false.
  277.  * @author Daryl Koopersmith
  278.  */
  279. function elastic_get($var{
  280.     global $elastic;
  281.     
  282.     ifisset($elastic->$var) ) {
  283.         return $elastic->$var;
  284.     }
  285.     return false;
  286. }
  287.  
  288. /**
  289.  * Sets data within the global {@link Elastic} instance ($elastic).
  290.  *
  291.  * @param string $var Variable name to set.
  292.  * @param string $value Value to be set.
  293.  * @global Elastic $elastic 
  294.  * @return void 
  295.  * @author Daryl Koopersmith
  296.  */
  297. function elastic_set($var$value{
  298.     global $elastic;
  299.     
  300.     $elastic->$var $value;
  301. }
  302.  
  303. /**
  304.  * Returns a path within the Elastic framework.
  305.  * Arguments specify absolute or URI, and theme or child theme.
  306.  * 
  307.  * Takes a path $name, then up to two optional arguments: ['abs' or 'uri'] and ['theme' or 'child'].
  308.  * Defaults to 'abs' and 'theme'.
  309.  * 
  310.  * @param string $name The name of the path requested. A common value is 'custom'.
  311.  * @param string $arg1 Optional. Takes ['abs' or 'uri'] or ['theme' or 'child']
  312.  * @param string $arg2 Optional. Takes ['abs' or 'uri'] or ['theme' or 'child']
  313.  * @return string Requested path, or false.
  314.  * @author Daryl Koopersmith
  315.  */
  316. function elastic_get_path$name$arg1 'abs'$arg2 'theme' {
  317.     $path elastic_get('path');
  318.     
  319.     ifisset($path$name ]) )
  320.         return false;
  321.     
  322.     $uri 'uri' === $arg1 || 'uri' === $arg2 );
  323.     $child 'child' === $arg1 || 'child' === $arg2 );
  324.     
  325.     if$uri )
  326.         return trailingslashit( ($childget_stylesheet_directory_uri(get_template_directory_uri() ) $path$name ];
  327.     else
  328.         return trailingslashit( ($childSTYLESHEETPATH TEMPLATEPATH $path$name ];
  329. }
  330.  
  331. /**
  332.  * Calls {@link do_action()} at all context levels.
  333.  * Actions are run in the order: global, abstract, general, specific.
  334.  * Actions are not run for null contexts.
  335.  * 
  336.  * @param string $id The id of the hook.
  337.  * @todo Document how the hooks API can handle any number of variables.
  338.  * @return void 
  339.  * @author Daryl Koopersmith
  340.  */
  341. function elastic_do_atomic$id$prefix NULL {
  342.     foreach(elastic_get('context'as $view{
  343.         ifisset($view)) {
  344.             $args func_get_args();
  345.             array_splice$args02$view );
  346.             do_action_ref_arrayelastic_format_hook($id$view$prefix)$args );
  347.         }
  348.     }
  349. }
  350.  
  351. /**
  352.  * Calls {@link do_action()} at the most specific atomic level with a registered action.
  353.  * 
  354.  * For example, if actions were registered at the 'global' and 'general' levels,
  355.  * only the 'general' action would be called.
  356.  *
  357.  * @param string $id The id of the hook.
  358.  * @param string $prefix Optional. Defaults to {@link Elastic::$prefix}.
  359.  * @return void 
  360.  * @author Daryl Koopersmith
  361.  */
  362. function elastic_do_atomic_specific$id$prefix NULL {
  363.     foreacharray_reverseelastic_get('context') ) as $view {
  364.         ifisset($view)) {
  365.             $hook elastic_format_hook$id$view$prefix );
  366.             ifhas_action($hook) ) {
  367.                 $args func_get_args();
  368.                 array_splice$args02$view );
  369.                 do_action_ref_array$hook$args );
  370.                 break;
  371.             }
  372.         }
  373.     }
  374. }
  375.  
  376. /**
  377.  * Calls {@link apply_filters()} at all context levels.
  378.  * Filters are applied in the order: global, abstract, general, specific.
  379.  * Filters are not applied to null contexts.
  380.  * 
  381.  * $value is updated every time {@link apply_filters()} is run.
  382.  * (i.e. {@link apply_filters()} at the 'specific' level receives any changes made at the 'global' level).
  383.  * 
  384.  * @param string $id The id of the hook.
  385.  * @param mixed $value The value to be filtered.
  386.  * @param string $prefix Optional. Defaults to {@link Elastic::$prefix}.
  387.  * @return void 
  388.  * @author Daryl Koopersmith
  389.  */
  390. function elastic_apply_atomic$id$value$prefix NULL {
  391.     $preset_args 3;
  392.     
  393.     foreach(elastic_get('context'as $view{
  394.         ifisset($view)) {
  395.             $output_args arrayelastic_format_hook($id$view$prefix)$value );
  396.             iffunc_num_args($preset_args {
  397.                 $args func_get_args();
  398.                 array_splice$args0$preset_args$output_args );
  399.                 $value call_user_func_array('apply_filters'$args);
  400.             else {
  401.                 $value apply_filters$output_args[0]$output_args[1);
  402.             }
  403.         }
  404.     }
  405.     return $value;
  406. }
  407.  
  408. /**
  409.  * Calls {@link apply_filters()} at the most specific atomic level with a registered action.
  410.  *
  411.  * For example, if filters were registered at the 'global' and 'general' levels,
  412.  * only the 'general' filter would be called.
  413.  * 
  414.  * @param string $id The id of the hook.
  415.  * @param mixed $value The value to be filtered.
  416.  * @param string $prefix Optional. Defaults to {@link Elastic::$prefix}.
  417.  * @return void 
  418.  * @author Daryl Koopersmith
  419.  */
  420. function elastic_apply_atomic_specific$id$value$prefix NULL {
  421.     $preset_args 3;
  422.     
  423.     foreacharray_reverseelastic_get('context') ) as $view {
  424.         ifisset($view)) {
  425.             $hook elastic_format_hook$id$view$prefix );
  426.             ifhas_filter($hook) ) {
  427.                 $output_args array$hook$value );
  428.                 iffunc_num_args($preset_args {
  429.                     $args func_get_args();
  430.                     array_splice$args0$preset_args$output_args );
  431.                     return call_user_func_array('apply_filters'$args);
  432.                 else {
  433.                     return apply_filters$output_args[0]$output_args[1);
  434.                 }
  435.             }
  436.         }
  437.     }
  438. }
  439.  
  440. /**
  441.  * Returns a formatted hook title.
  442.  *
  443.  * @param string $id 
  444.  * @param string $view 
  445.  * @param string $prefix Optional. Defaults to {@link Elastic::$prefix}.
  446.  * @return string Formatted hook title
  447.  * @author Daryl Koopersmith
  448.  */
  449. function elastic_format_hook$id$view ''$prefix NULL {
  450.     ifisset($prefix) )
  451.         $prefix elastic_get('prefix');
  452.     // If $view is empty (i.e. context['global']), don't add an extra underscore
  453.     return $prefix (( empty($view) ) $view "_" '' $id;
  454. }
  455.  
  456. /**
  457.  * Returns a formatted hook title with the module prefix.
  458.  *
  459.  * @param string $id 
  460.  * @param string $view 
  461.  * @return string Formatted hook title
  462.  * @author Daryl Koopersmith
  463.  */
  464. function elastic_module_format_hook$id$view '' {
  465.     return elastic_format_hook$id$viewelastic_get('module_prefix') );
  466. }
  467.  
  468. ?>

Documentation generated on Sat, 24 Oct 2009 17:17:44 -0500 by phpDocumentor 1.4.3