Source for file module.php
Documentation is available at module.php
* The basic unit of a theme.
* <li>{@link Module::format_hook()}</li>
* <li>{@link Module::do_atomic()}</li>
* <li>{@link Module::do_atomic_specific()}</li>
* <li>{@link Module::apply_atomic()}</li>
* <li>{@link Module::apply_atomic_specific()}</li>
* <br><b>Classes API:</b>
* <li>{@link Module::add_class()}</li>
* <li>{@link Module::has_class()}</li>
* <li>{@link Module::remove_class()}</li>
* <li>{@link Module::set_view()}</li>
* <li>{@link Module::has_view()}</li>
* <li>{@link Module::remove_view()}</li>
* <li>{@link Module::do_view()}</li>
* <li>{@link Module::load_views_folder()}</li>
* <li>{@link Module::load_default_views()}</li>
* <li>{@link Module::get_module()}</li>
* <li>{@link Module::get_modules()}</li>
* <li>{@link Module::get_modules_by_type()}</li>
* @package Elastic Framework
* @author Daryl Koopersmith
* Constructs a new Module.
* @param string $id The id of the module. Must be a unique string.
* @param string $type Optional. Default, the module's class (lowercase). The type of the module.
* @author Daryl Koopersmith
add_filter( $this->format_hook( '_html_before', 'admin' ), array(&$this, '_blank') );
add_filter( $this->format_hook( '_html_after', 'admin' ), array(&$this, '_blank') );
* Calls the hooks and generates the output.
* @author Daryl Koopersmith
// If view is empty, do not show module.
if ( ! empty( $view ) ) {
echo $this->apply_atomic( '_html_before', $this->_html_before(), $prefix );
echo $this->apply_atomic( '_html_after', $this->_html_after(), $prefix );
* Formats a hook for this module.
* @author Daryl Koopersmith
* An interface to {@link elastic_do_atomic()} for an instance of a {@link Module}.
* @param string $suffix A string appended to the end of the {@link Module::$id}.
* @see elastic_do_atomic()
* @author Daryl Koopersmith
$output_args = array( $this->id . $suffix, elastic_get('module_prefix') );
* An interface to {@link elastic_do_atomic_specific()} for an instance of a {@link Module}.
* @param string $suffix A string appended to the end of the {@link Module::$id}.
* @see elastic_do_atomic_specific()
* @author Daryl Koopersmith
$output_args = array( $this->id . $suffix, elastic_get('module_prefix') );
* An interface to {@link elastic_apply_atomic()} for an instance of a {@link Module}.
* @param string $suffix A string appended to the end of the {@link Module::$id}.
* @param mixed $value The value to be filtered.
* @see elastic_apply_atomic()
* @author Daryl Koopersmith
$output_args = array( $this->id . $suffix, $value, elastic_get('module_prefix') );
* An interface to {@link elastic_apply_atomic_specific()} for an instance of a {@link Module}.
* @param string $suffix A string appended to the end of the {@link Module::$id}.
* @param mixed $value The value to be filtered.
* @see elastic_apply_atomic_specific()
* @author Daryl Koopersmith
$output_args = array( $this->id . $suffix, $value, elastic_get('module_prefix') );
return isset ( $this->classes[$class] );
* Binds a callback to a view. At any time, only one callback will be bound to a view.
* If the callback is false, the module will not be rendered for the given view.
* @param string $callback
* @param boolean $file Optional. Default false. If true, $callback is a file path, and will be included.
* @author Daryl Koopersmith
function set_view( $view, $callback, $file = false ) {
$hook = $this->_format_view_hook( $view );
if( $callback === false ) {
$callback = array(&$this, '_blank');
$this->_views[$view] = $callback;
$callback = array(&$this, '_load_file_view');
add_action( $hook, $callback, 10, 2 );
return has_action( $this->_format_view_hook( $view ) );
* Removes any view associated with a provided context.
* @author Daryl Koopersmith
remove_all_actions( $this->_format_view_hook( $view ) );
* Returns the output of the most contextually-specific set view.
* @author Daryl Koopersmith
* Loads and sets views from a folder.
* File for global view is named index.php
* @param string $folder Absolute path to folder
* @author Daryl Koopersmith
$path = trailingslashit( $folder ) . $this->type;
$files = glob( $path . '/*.php');
foreach( $files as $file ) {
$view = ( 'index' === $view ) ? '' : $view; // Global view is named index.php. Can't have a file named '.php'
* Loads and sets default views
* @author Daryl Koopersmith
* Formats the internal view hook.
* @param string $view Optional. If set, returns a formatted hook. If null, returns the formatted id.
* @author Daryl Koopersmith
function _format_view_hook( $view = NULL ) {
* Used in {@link set_view()} to load files.
* @author Daryl Koopersmith
function _load_file_view( $view, $module ) {
include $this->_views[$view];
* Returns the html prepended to the module.
* @todo Add a hook to modify the html.
* @return string HTML prepended to module.
* @author Daryl Koopersmith
function _html_before() {
return "<div id='{$this->id}' class=' " . join( ' ', array_keys( $this->classes ) ) . "'>";
* Returns the html appended to the module.
* @todo Add a hook to modify the html.
* @return string HTML appended to module.
* @author Daryl Koopersmith
* Blank function to be used in conjunction with both filters and actions.
* @return string Returns the empty string only if $arg is set.
* @author Daryl Koopersmith
function _blank( $arg = NULL ) {
* Function that provides a framework for searching all modules based on a slug and a callback.
* @param string $slug The value to retrieve.
* @param callback $condition A function that compares the $slug and each Module.
* @param string $return Optional. Default 'array'. If 'single', returns the first matched module. If 'selection', returns the matches in a new Selection.
* @return mixed Return type based on $return value. An array of matched modules. If no matches found, false.
* @author Daryl Koopersmith
function get_modules($slug, $condition, $return = 'array') {
while( ! empty($stack) ) {
$ptr = array_shift( $stack );
if( call_user_func_array( $condition, array( $slug, $ptr ) ) ) {
if( 'single' === $return )
if ( isset($ptr->children) )
$stack = array_merge($stack, $ptr->children);
if( ! empty( $matches ) ) {
if ( 'single' === $return )
else if ( 'selection' === $return )
return new Selection( $matches );
* @author Daryl Koopersmith
function get_module($id) {
return $this->get_modules($id, array($this, '_get_module'), 'single');
* Callback for get_module (modules by id).
* @author Daryl Koopersmith
function _get_module( $id, $ptr ) {
return ( $id === $ptr->id );
* @param boolean $selection Optional. Default false. If true, return matches in a new Selection.
* @author Daryl Koopersmith
function get_modules_by_type( $type, $selection = false ) {
return $this->get_modules( $type, array($this, '_get_modules_by_type'), ( $selection ) ? 'selection' : 'array' );
* Callback for get_module_by_type
* @author Daryl Koopersmith
function _get_modules_by_type( $type, $ptr ) {
return ( $type === $ptr->type );
|