PHP Class WPLib, wplib

Plugin Name: WPLib Plugin URI: http://wordpress.org/plugins/wplib/ Description: A WordPress Website Foundation Library Agency and Internal Corporate Developers Version: 0.13.2 Author: The WPLib Team Author URI: http://wplib.org Text Domain: wplib License: GPLv2 or later Copyright 2015 NewClarity Consulting LLC This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
See also: https://github.com/wplib/wplib/commit/8dc27c368e84f7ba6e1448753e1b1f082a60ac6d#commitcomment-11027141
ファイルを表示 Open project: wplib/wplib Class Usage Examples

Public Methods

Method Description
__callStatic ( string $method, array $args ) : mixed
_after_setup_theme ( ) Now load the theme's modules.
_after_setup_theme_11 ( ) Determine and then load the "mustload" classes They are the classes with an on_load() method.
_autoloader ( string $class_name )
_call_helper ( string $helped_class, string $helper_method, array $args, object $container = null ) : mixed | null Delegate calls to other classes.
_ensure_only_one_class ( $class_container ) Scans to ensure that only one PHP class is declared.
_find_files_autoloader ( $class_name ) Special autoloader to run only for conflicts.
_get_raw_meta_fieldname ( string $meta_name ) : string Returns the raw meta fieldname given a non-prefixed field name.
_init_9 ( ) : array Autoload all WPLib module classes to ensure they are available for 'init' hook.
_plugins_loaded_11 ( ) Load all necessary files. This finds autoloading files and loads modules.
_register_templates ( ) : array Register all templates for WPLib, an App or a module.
_set_mustload_classes ( array $autoload_files ) This will get called 4 times.
_shutdown ( ) Throw error if site failed to load because of a module failing to load.
_xmlrpc_call ( ) Capture status of DOING_XMLRPC
add_class_action ( string $action, integer $priority = 10 )
add_class_filter ( string $filter, integer $priority = 10 )
app_class ( ) : string | null Returns the one app class defined.
app_classes ( ) : array | null
assets_url ( ) : string Return the simple asset path
autoload_all_classes ( ) Force loading of all classes if needed to find all classes with a specific constant.
cache_delete ( string $key, string $group = '' )
cache_exists ( string $key, string $group = '' ) : boolean
cache_get ( string $key, string $group = '' ) : mixed
cache_set ( string $key, mixed $value, string $group = '', integer $expire )
can_call ( string $method_name, string | boolean $class_name = false ) : boolean Determines if a named method exists and is_callable a given class.
class_declares_method ( string $class_name, string $method_name ) : boolean Determines is a class actually declares a method instead of just inheriting it.
component_classes ( string $scope = 'all' ) : array Returns the list of "Component" classes. A Component is one of Lib, Site, App, Theme, Module.
current_helped_class ( ) : array Returns the class that is currently being "helped."
dashify ( string $string, boolean | true $lowercase = true ) : string
do_log_errors ( ) : boolean
do_the_methods ( string | object $view, string | object $model, string $method_name, array $args ) : mixed Do "the_" Methods - Allow classes to delegate the "the_" logic processing here.
doing_ajax ( ) : boolean
doing_autosave ( ) : boolean
doing_cron ( ) : boolean
doing_xmlrpc ( ) : boolean
emit_headers ( string | array $headers ) Emits one or more HTTP headers to the output stream
file_hash ( string $filepath ) : string Returns a file hash, but caches it in persistent cache
get_asset_url ( string $asset_path, boolean | string $class_name = false ) : string Return the asset path
get_callable ( string | object $object, string $method_name ) : callable | null Given an object/class name and method name return a callable or null if can't be called.
get_child_classes ( $base_class, $constant_name ) : string[] Returns array of class names $base_class children with positive values for $base_class::$contant_name.
get_constant ( string $constant_name, string | boolean | object $class_name = false, boolean $try_parent = true ) : mixed | null Return a class constant for the called class.
get_contents ( string $filepath ) : boolean
get_module_class ( string $module_name, string | boolean $app_class = false ) : string
get_module_classes ( string $app_class ) : string[]
get_module_dir ( WPLib_Item_Base | string | boolean $item_class = false ) : string | null
get_module_name ( string $class_name ) : mixed | null
get_real_url ( string $url ) : string Like realpath() but for URLs
get_root_dir ( string $filepath, boolean | string $class_name = false ) : string Return the root directory of the Lib/App/Site/Module/Theme class for a given class name.
get_root_url ( string $filepath, boolean | string $class_name = false ) : string Get the root URL for a given Lib/Site/App/Module/Theme.
get_template ( string $template_slug, array | string $_template_vars = [], WPLib_Item_Base | object $item = null ) : string
get_template_dir ( string $template ) : string Return the template filepath for the passed $template for the called class.
get_theme_file ( string $local_file ) : string Returns the filepath for a theme template file given its "local filename."
has_html_suffix ( string | false $suffix ) : boolean
initialize ( ) If used in a theme you have to first initialize it before WPLib_Theme_Base classes will be available to extend.
invoke_with_args ( callable $invokable, array $args ) : mixed
is_development ( ) : boolean
is_found ( string $filepath ) : boolean
is_production ( ) : boolean
is_script_debug ( ) : string If runmode is development or SCRIPT_DEBUG
is_staging ( ) : boolean
is_testing ( ) : boolean
is_wp_debug ( ) : boolean
make_new_item ( WPLib_Item_Base | WP_Post | WP_Term $item, array $args = [] ) : WPLib_Term_Base | WPLib_Post_Base
maybe_make_absolute_path ( string $filepath, boolean | string $dir = false ) : string Convert relative file paths to absolute file paths.
maybe_make_abspath_relative ( string $filepath ) : string Takes a filepath and potentially returns a relative path (prefixed with '~/'), if $filepath begins with ABSPATH.
module_classes ( ) : string[]
new_post_url ( ) : string
on_load ( )
put_contents ( string $filepath, string $contents ) : boolean Runs file_put_contents()
register_helper ( string $helper_class, string | boolean $helped_class = false ) Register a helper class to the specified class.
register_module ( string $module, integer $priority = 10 )
register_template ( string $template, string | boolean $called_class = false ) Register a template
remove_class_action ( string $action, integer $priority = 10 )
remove_class_filter ( string $filter, integer $priority = 10 )
root_dir ( ) : string Return the root directory of the Lib/App/Site/Module/Theme class.
root_url ( ) : string Return the root URL of the Lib/App/Site/Module/Theme class.
runmode ( ) : WPLib_Runmode
set_runmode ( integer | WPLib_Runmode $runmode )
set_stability ( integer | WPLib_Stability $stability )
set_theme ( WPLib_Theme_Base $theme )
short_prefix ( ) : boolean | string
site_classes ( ) : array Return the list of classes declared after WPLib first loads.
stability ( ) : WPLib_Stability
template_dir ( ) : string Return the templates directory path for the called class.
templates_subdir ( ) : string Return the subdir name for templates.
the_asset_url ( string $asset_path, boolean | string $class_name = false ) : string Echo the asset path
the_template ( string $template_slug, array | string $_template_vars = [], WPLib_Item_Base | object $item = null )
theme ( ) : WPLib_Theme_Base
trigger_error ( string $error_msg, integer $error_type = E_USER_NOTICE, boolean $echo = false ) Triggers error message unless doing AJAX, XMLRPC or Cron; then it logs the error but only if Development mode.
use_template_global_vars ( ) : boolean Return if WPLIB_TEMPLATE_GLOBAL_VARS was set to true

Private Methods

Method Description
_current_helped_classes ( ) : array
_filter_group ( $group ) : string
_find_autoload_files ( ) Scan registered autoload files, by priority
_flatten_array_dependency_order ( string $parent_class, string $child_class, array &$mustload_classes, string[] &$ordered_classes ) : array Flatten an array containing parent class names with array.
_load_modules ( ) Load all registered modules, by priority
_load_mustload_classes ( string[] $mustload_classes ) Loads the "mustload" classes on every page load.
_load_necessary_files ( ) Load all necessary files, i.e. modules and finds all autoloading files.
_ordered_mustload_classes ( ) : array Orders the Mustload classes in order of least dependency.

Method Details

__callStatic() static public method

static public __callStatic ( string $method, array $args ) : mixed
$method string
$args array
return mixed

_after_setup_theme() static public method

Now load the theme's modules.
static public _after_setup_theme ( )

_after_setup_theme_11() static public method

Determine and then load the "mustload" classes They are the classes with an on_load() method.
static public _after_setup_theme_11 ( )

_autoloader() static public method

static public _autoloader ( string $class_name )
$class_name string

_call_helper() static public method

This allows us to document a single "API" for WPLib yet structure the code more conveniently in multiple class files.
static public _call_helper ( string $helped_class, string $helper_method, array $args, object $container = null ) : mixed | null
$helped_class string Name of class that is calling the helper
$helper_method string Name of the helper method
$args array Arguments to pass to the helper method
$container object An object containing a 'callable' property.
return mixed | null

_ensure_only_one_class() static public method

This is important because we assume only one class for the autoloader.
static public _ensure_only_one_class ( $class_container )
$class_container

_find_files_autoloader() static public method

Special autoloader to run only for conflicts.
static public _find_files_autoloader ( $class_name )
$class_name

_get_raw_meta_fieldname() static public method

Adds both a leading underscore and a short prefix to the meta name.
static public _get_raw_meta_fieldname ( string $meta_name ) : string
$meta_name string
return string

_init_9() static public method

Autoload all WPLib module classes to ensure they are available for 'init' hook.
static public _init_9 ( ) : array
return array

_plugins_loaded_11() static public method

Load all necessary files. This finds autoloading files and loads modules.
static public _plugins_loaded_11 ( )

_register_templates() static public method

Register all templates for WPLib, an App or a module.
static public _register_templates ( ) : array
return array

_set_mustload_classes() static public method

1 & 2: Finding all autoloading files from components that have been loaded by (1) plugins or (2) the theme. 3 & 4: Finding all autoloading files defined by modules specified by (1) plugins or (2) the theme. Each time it is called it will have values added to self::$_mustload_classes.
static public _set_mustload_classes ( array $autoload_files )
$autoload_files array

_shutdown() static public method

Throw error if site failed to load because of a module failing to load.
static public _shutdown ( )

_xmlrpc_call() static public method

Capture status of DOING_XMLRPC
static public _xmlrpc_call ( )

add_class_action() static public method

static public add_class_action ( string $action, integer $priority = 10 )
$action string
$priority integer

add_class_filter() static public method

static public add_class_filter ( string $filter, integer $priority = 10 )
$filter string
$priority integer

app_class() static public method

Returns the one app class defined.
static public app_class ( ) : string | null
return string | null

app_classes() static public method

static public app_classes ( ) : array | null
return array | null

assets_url() static public method

Return the simple asset path
static public assets_url ( ) : string
return string

autoload_all_classes() static public method

Force loading of all classes if needed to find all classes with a specific constant.
static public autoload_all_classes ( )

cache_delete() static public method

static public cache_delete ( string $key, string $group = '' )
$key string
$group string

cache_exists() static public method

static public cache_exists ( string $key, string $group = '' ) : boolean
$key string
$group string
return boolean

cache_get() static public method

static public cache_get ( string $key, string $group = '' ) : mixed
$key string
$group string
return mixed

cache_set() static public method

static public cache_set ( string $key, mixed $value, string $group = '', integer $expire )
$key string
$value mixed
$group string
$expire integer

can_call() static public method

Determines if a named method exists and is_callable a given class.
static public can_call ( string $method_name, string | boolean $class_name = false ) : boolean
$method_name string
$class_name string | boolean
return boolean

class_declares_method() static public method

Determines is a class actually declares a method instead of just inheriting it.
static public class_declares_method ( string $class_name, string $method_name ) : boolean
$class_name string
$method_name string
return boolean

component_classes() static public method

Returns the 'latest' or 'all' (default). This follows 'principle of least surprise'
static public component_classes ( string $scope = 'all' ) : array
$scope string 'all' or 'latest'
return array

current_helped_class() static public method

The Helped class is the one to the left of '::' when the method is actually in a "helper" class: {$current_helped_class}::register_helper( $helper_class ); Which is equivalent to: WPLib::register_helper( $helper_class, $current_helped_class );
static public current_helped_class ( ) : array
return array

dashify() static public method

static public dashify ( string $string, boolean | true $lowercase = true ) : string
$string string
$lowercase boolean | true
return string

do_log_errors() static public method

static public do_log_errors ( ) : boolean
return boolean

do_the_methods() static public method

The "the_" method can call virtual methods and/or delegate to a view or a model. The view and model can both be the same object if needed.
static public do_the_methods ( string | object $view, string | object $model, string $method_name, array $args ) : mixed
$view string | object
$model string | object
$method_name string
$args array
return mixed

doing_ajax() static public method

static public doing_ajax ( ) : boolean
return boolean

doing_autosave() static public method

static public doing_autosave ( ) : boolean
return boolean

doing_cron() static public method

static public doing_cron ( ) : boolean
return boolean

doing_xmlrpc() static public method

static public doing_xmlrpc ( ) : boolean
return boolean

emit_headers() static public method

Emits one or more HTTP headers to the output stream
static public emit_headers ( string | array $headers )
$headers string | array

file_hash() static public method

Returns a file hash, but caches it in persistent cache
static public file_hash ( string $filepath ) : string
$filepath string
return string

get_asset_url() static public method

Return the asset path
static public get_asset_url ( string $asset_path, boolean | string $class_name = false ) : string
$asset_path string
$class_name boolean | string Name of class to return the root dir.
return string

get_callable() static public method

Given an object/class name and method name return a callable or null if can't be called.
static public get_callable ( string | object $object, string $method_name ) : callable | null
$object string | object
$method_name string
return callable | null

get_child_classes() static public method

Returns array of class names $base_class children with positive values for $base_class::$contant_name.
static public get_child_classes ( $base_class, $constant_name ) : string[]
$base_class
$constant_name
return string[]

get_constant() static public method

Return a class constant for the called class.
static public get_constant ( string $constant_name, string | boolean | object $class_name = false, boolean $try_parent = true ) : mixed | null
$constant_name string
$class_name string | boolean | object
$try_parent boolean
return mixed | null

get_contents() static public method

static public get_contents ( string $filepath ) : boolean
$filepath string
return boolean

get_module_class() static public method

static public get_module_class ( string $module_name, string | boolean $app_class = false ) : string
$module_name string
$app_class string | boolean
return string

get_module_classes() static public method

static public get_module_classes ( string $app_class ) : string[]
$app_class string
return string[]

get_module_dir() static public method

static public get_module_dir ( WPLib_Item_Base | string | boolean $item_class = false ) : string | null
$item_class WPLib_Item_Base | string | boolean
return string | null

get_module_name() static public method

static public get_module_name ( string $class_name ) : mixed | null
$class_name string
return mixed | null

get_real_url() static public method

Like realpath() but for URLs
static public get_real_url ( string $url ) : string
$url string
return string

get_root_dir() static public method

Return the root directory of the Lib/App/Site/Module/Theme class for a given class name.
static public get_root_dir ( string $filepath, boolean | string $class_name = false ) : string
$filepath string Name of path to append to root dir.
$class_name boolean | string Name of class to return the source dir.
return string

get_root_url() static public method

Get the root URL for a given Lib/Site/App/Module/Theme.
static public get_root_url ( string $filepath, boolean | string $class_name = false ) : string
$filepath string Name of path to append to root URL.
$class_name boolean | string Name of class to return the root dir.
return string

get_template() static public method

See also: self::the_template()
static public get_template ( string $template_slug, array | string $_template_vars = [], WPLib_Item_Base | object $item = null ) : string
$template_slug string
$_template_vars array | string
$item WPLib_Item_Base | object
return string

get_template_dir() static public method

Return the template filepath for the passed $template for the called class.
static public get_template_dir ( string $template ) : string
$template string
return string

get_theme_file() static public method

Local filename means based at the root of the theme w/o leading slash.
static public get_theme_file ( string $local_file ) : string
$local_file string
return string

has_html_suffix() static public method

static public has_html_suffix ( string | false $suffix ) : boolean
$suffix string | false
return boolean

initialize() static public method

If used in a theme you have to first initialize it before WPLib_Theme_Base classes will be available to extend.
static public initialize ( )

invoke_with_args() static public method

static public invoke_with_args ( callable $invokable, array $args ) : mixed
$invokable callable
$args array
return mixed

is_development() static public method

static public is_development ( ) : boolean
return boolean

is_found() static public method

static public is_found ( string $filepath ) : boolean
$filepath string
return boolean

is_production() static public method

static public is_production ( ) : boolean
return boolean

is_script_debug() static public method

If runmode is development or SCRIPT_DEBUG
See also: https://github.com/wplib/wplib/commit/8dc27c368e84f7ba6e1448753e1b1f082a60ac6d#commitcomment-11026829
static public is_script_debug ( ) : string
return string

is_staging() static public method

static public is_staging ( ) : boolean
return boolean

is_testing() static public method

static public is_testing ( ) : boolean
return boolean

is_wp_debug() static public method

static public is_wp_debug ( ) : boolean
return boolean

make_new_item() static public method

static public make_new_item ( WPLib_Item_Base | WP_Post | WP_Term $item, array $args = [] ) : WPLib_Term_Base | WPLib_Post_Base
$item WPLib_Item_Base | WP_Post | WP_Term
$args array
return WPLib_Term_Base | WPLib_Post_Base

maybe_make_absolute_path() static public method

Recognize a path with a leading slash as an absolute, a no leading slash or starting with '~/' as relative.
static public maybe_make_absolute_path ( string $filepath, boolean | string $dir = false ) : string
$filepath string
$dir boolean | string
return string

maybe_make_abspath_relative() static public method

Takes a filepath and potentially returns a relative path (prefixed with '~/'), if $filepath begins with ABSPATH.
static public maybe_make_abspath_relative ( string $filepath ) : string
$filepath string
return string

module_classes() static public method

static public module_classes ( ) : string[]
return string[]

new_post_url() static public method

static public new_post_url ( ) : string
return string

on_load() static public method

static public on_load ( )

put_contents() static public method

Runs file_put_contents()
static public put_contents ( string $filepath, string $contents ) : boolean
$filepath string
$contents string
return boolean

register_helper() static public method

Register a helper class to the specified class.
static public register_helper ( string $helper_class, string | boolean $helped_class = false )
$helper_class string The name of the helper class.
$helped_class string | boolean Name of the class adding the helper. Defaults to called class.

register_module() static public method

static public register_module ( string $module, integer $priority = 10 )
$module string
$priority integer

register_template() static public method

Register a template
static public register_template ( string $template, string | boolean $called_class = false )
$template string
$called_class string | boolean

remove_class_action() static public method

static public remove_class_action ( string $action, integer $priority = 10 )
$action string
$priority integer

remove_class_filter() static public method

static public remove_class_filter ( string $filter, integer $priority = 10 )
$filter string
$priority integer

root_dir() static public method

Return the root directory of the Lib/App/Site/Module/Theme class.
static public root_dir ( ) : string
return string

root_url() static public method

Return the root URL of the Lib/App/Site/Module/Theme class.
static public root_url ( ) : string
return string

runmode() static public method

static public runmode ( ) : WPLib_Runmode
return WPLib_Runmode

set_runmode() static public method

static public set_runmode ( integer | WPLib_Runmode $runmode )
$runmode integer | WPLib_Runmode

set_stability() static public method

static public set_stability ( integer | WPLib_Stability $stability )
$stability integer | WPLib_Stability

set_theme() static public method

static public set_theme ( WPLib_Theme_Base $theme )
$theme WPLib_Theme_Base

short_prefix() static public method

static public short_prefix ( ) : boolean | string
return boolean | string

site_classes() static public method

Return the list of classes declared after WPLib first loads.
static public site_classes ( ) : array
return array

stability() static public method

static public stability ( ) : WPLib_Stability
return WPLib_Stability

template_dir() static public method

Return the templates directory path for the called class.
static public template_dir ( ) : string
return string

templates_subdir() static public method

Return the subdir name for templates.
static public templates_subdir ( ) : string
return string

the_asset_url() static public method

Echo the asset path
static public the_asset_url ( string $asset_path, boolean | string $class_name = false ) : string
$asset_path string
$class_name boolean | string Name of class to return the root dir.
return string

the_template() static public method

See also: http://stackoverflow.com/a/7983863/102699
static public the_template ( string $template_slug, array | string $_template_vars = [], WPLib_Item_Base | object $item = null )
$template_slug string
$_template_vars array | string
$item WPLib_Item_Base | object

theme() static public method

static public theme ( ) : WPLib_Theme_Base
return WPLib_Theme_Base

trigger_error() static public method

Triggers error message unless doing AJAX, XMLRPC or Cron; then it logs the error but only if Development mode.
static public trigger_error ( string $error_msg, integer $error_type = E_USER_NOTICE, boolean $echo = false )
$error_msg string
$error_type integer
$echo boolean If true use 'echo', if false use trigger_error().

use_template_global_vars() static public method

Setting WPLIB_TEMPLATE_GLOBAL_VARS to false will cause WPLib to extract $GLOBALS before loading the WP template which normally happens in wp-include/template-loader.php but WPLib hijacks that.
static public use_template_global_vars ( ) : boolean
return boolean