CB_Component
in package
CB Component
Helps create components that have similar properties and methods. Gives us radical global values across the app that lets us dynamically call and/or create functions and the like.
Tags
Table of Contents
Properties
- $admin_menu : array<string|int, mixed>
- WordPress Toolbar links.
- $current_id : string
- The current ID of the queried object.
- $global_tables : array<string|int, mixed>
- Global tables for the component (if applicable).
- $has_directory : bool
- Does the component need a top-level directory?
- $id : string
- Unique ID for the component.
- $meta_tables : array<string|int, mixed>
- Metadata tables for the component (if applicable).
- $name : string
- Translatable name for the component.
- $notification_callback : callable
- Callback for formatting notifications.
- $path : string
- The path to the component's files.
- $query : WP_Query
- The WP_Query loop for this component.
- $root_slug : string
- Root slug for the component.
- $search_query_arg : string
- Query argument for component search URLs.
- $search_string : string
- Placeholder text for component directory search box.
- $slug : string
- Unique slug for the component, for use in query strings and URLs.
Methods
- add_permastructs() : mixed
- Add any permalink structures.
- add_rewrite_rules() : mixed
- Add any additional rewrite rules.
- add_rewrite_tags() : mixed
- Add any additional rewrite tags.
- enqueue_scripts() : mixed
- Enqueues all of our scripts in a clean fashion.
- generate_rewrite_rules() : mixed
- Generate any additional rewrite rules.
- includes() : mixed
- Include required files.
- late_includes() : mixed
- Late includes method.
- parse_query() : mixed
- Allow components to parse the main query.
- register_api_endpoints() : mixed
- Sets up REST endpoints for the component.
- register_global_tables() : mixed
- Register global tables for the component, so that it may use WordPress's database API.
- register_meta_tables() : mixed
- Register component metadata tables.
- register_post_types() : mixed
- Set up the component post types.
- register_scripts() : mixed
- register_taxonomies() : mixed
- Register component-specific taxonomies.
- setup_actions() : mixed
- Set up the actions.
- setup_admin_bar() : mixed
- Set up the component entries in the WordPress Admin Bar.
- setup_cache_groups() : mixed
- Setup component-specific cache groups.
- setup_canonical_stack() : mixed
- Set up the canonical URL stack for this component.
- setup_filters() : mixed
- Sets up filters for redirect shenanigans.
- setup_globals() : mixed
- Set up component global variables.
- setup_nav() : mixed
- Set up component navigation.
- setup_title() : mixed
- Set up the component title.
- start() : mixed
- Component loader.
Properties
$admin_menu
WordPress Toolbar links.
public
array<string|int, mixed>
$admin_menu
= ''
Tags
$current_id
The current ID of the queried object.
public
string
$current_id
= ''
Tags
$global_tables
Global tables for the component (if applicable).
public
array<string|int, mixed>
$global_tables
= array()
Tags
$has_directory
Does the component need a top-level directory?
public
bool
$has_directory
= \false
Tags
$id
Unique ID for the component.
public
string
$id
= ''
Tags
$meta_tables
Metadata tables for the component (if applicable).
public
array<string|int, mixed>
$meta_tables
= array()
Tags
$name
Translatable name for the component.
public
string
$name
= ''
$notification_callback
Callback for formatting notifications.
public
callable
$notification_callback
= ''
Tags
$path
The path to the component's files.
public
string
$path
= ''
Tags
$query
The WP_Query loop for this component.
public
WP_Query
$query
= \false
Tags
$root_slug
Root slug for the component.
public
string
$root_slug
= ''
Tags
$search_query_arg
Query argument for component search URLs.
public
string
$search_query_arg
= 's'
Tags
$search_string
Placeholder text for component directory search box.
public
string
$search_string
= ''
Tags
$slug
Unique slug for the component, for use in query strings and URLs.
public
string
$slug
= ''
Tags
Methods
add_permastructs()
Add any permalink structures.
public
add_permastructs() : mixed
Tags
add_rewrite_rules()
Add any additional rewrite rules.
public
add_rewrite_rules() : mixed
Tags
add_rewrite_tags()
Add any additional rewrite tags.
public
add_rewrite_tags() : mixed
Tags
enqueue_scripts()
Enqueues all of our scripts in a clean fashion.
public
enqueue_scripts([mixed $components = [] ]) : mixed
From each according to their capability, to each according to their need. Scripts are enqueued and localized using the following nested array structure: [ $unique_name_for_script => [ $name_of_api_component_to_use => [ $http_method1, $http_method2, ... ], 'dependencies' => [ $dependency1, $dependency2, ... ] ] ] This structure is then picked apart to dynamically enqueue and localize scripts. This gives us granular control over who gets access to what API endpoints, and can perform which actions, based on capability.
The scripts will be enqueued as: "cb_{$unique_name_for_script}", and will load a corresponding file, with the underscores replaced with dashes, like so: "cb-{$unique-name-for-script}.js". This will also load any dependencies found in the 'dependencies' array.
Scripts will then be localized using the same "cb_{$unique_name_for_script}" identifier, which will become the global name that is usable within the file. All API endpoints are localized as: "{$endpoint}_{$name_of_api_component}".
So, for example: - "cb_participation.get_participation" will return the API endpoint for getting participation entries ONLY when used within the cb-participation.js file. - "cb_core_admin.new_transactions" will return the API endpoint for creating a new transaction ONLY when used within the cb-core-admin.js file.
To access the API key, use "{$unique_name_for_script}.api_key" in the {$unique-name-for-script}.js file.
Parameters
- $components : mixed = []
Tags
generate_rewrite_rules()
Generate any additional rewrite rules.
public
generate_rewrite_rules() : mixed
Tags
includes()
Include required files.
public
includes([array<string|int, mixed> $includes = array() ]) : mixed
Please note that, by default, this method is fired on the cb_include hook, with priority 8. This is necessary so that core components are loaded in time to be available to third-party plugins. However, this load order means that third-party plugins whose main files are loaded at cb_include with priority 10 (as recommended), will not be loaded in time for their includes() method to fire automatically.
For this reason, it is recommended that your plugin has its own method or function for requiring necessary files. If you must use this method, you will have to call it manually in your constructor class, ie $this->includes();
Note that when you pass an array value like 'actions' to includes, it looks for the following three files (assuming your component is called 'my_component'):
- ./actions
- ./cb-my_component/actions
- ./cb-my_component/cb-my_component-actions.php
Parameters
- $includes : array<string|int, mixed> = array()
-
An array of file names, or file name chunks, to be parsed and then included.
Tags
late_includes()
Late includes method.
public
late_includes() : mixed
Components should include files here only on specific pages using conditionals such as . Intentionally left empty.
Tags
parse_query()
Allow components to parse the main query.
public
parse_query(object $query) : mixed
Parameters
- $query : object
-
The main WP_Query.
Tags
register_api_endpoints()
Sets up REST endpoints for the component.
public
register_api_endpoints([mixed $components = [] ]) : mixed
When passed a collection of strings, uses cb_ajax_register_api_endpoints() to register endpoints based on the component.
Parameters
- $components : mixed = []
Tags
register_global_tables()
Register global tables for the component, so that it may use WordPress's database API.
public
register_global_tables([array<string|int, mixed> $tables = array() ]) : mixed
Parameters
- $tables : array<string|int, mixed> = array()
-
Table names to register.
Tags
register_meta_tables()
Register component metadata tables.
public
register_meta_tables([array<string|int, mixed> $tables = array() ]) : mixed
Metadata tables are registered in the $wpdb global, for compatibility with the WordPress metadata API.
Parameters
- $tables : array<string|int, mixed> = array()
-
Table names to register.
Tags
register_post_types()
Set up the component post types.
public
register_post_types() : mixed
Tags
register_scripts()
public
register_scripts([mixed $components = [] ]) : mixed
Parameters
- $components : mixed = []
register_taxonomies()
Register component-specific taxonomies.
public
register_taxonomies() : mixed
Tags
setup_actions()
Set up the actions.
public
setup_actions() : mixed
Tags
setup_admin_bar()
Set up the component entries in the WordPress Admin Bar.
public
setup_admin_bar([array<string|int, mixed> $wp_admin_nav = array() ]) : mixed
Parameters
- $wp_admin_nav : array<string|int, mixed> = array()
-
An array of nav item arguments. Each item in this parameter array is passed to . See that method for a description of the required syntax for each item.
Tags
setup_cache_groups()
Setup component-specific cache groups.
public
setup_cache_groups() : mixed
Tags
setup_canonical_stack()
Set up the canonical URL stack for this component.
public
setup_canonical_stack() : mixed
Tags
setup_filters()
Sets up filters for redirect shenanigans.
public
setup_filters() : mixed
Tags
setup_globals()
Set up component global variables.
public
setup_globals([array<string|int, mixed> $args = array() ]) : mixed
Parameters
- $args : array<string|int, mixed> = array()
-
{ All values are optional. @type string $slug The component slug. Used to construct certain URLs, such as 'friends' in http://example.com/members/joe/friends/. Default: the value of $this->id. @type string $root_slug The component root slug. Note that this value is generally unused if the component has a root directory (the slug will be overridden by the post_name of the directory page). Default: the slug of the directory page if one is found, otherwise an empty string. @type bool $has_directory Set to true if the component requires an associated WordPress page. @type callable $notification_callback Optional. The callable function that formats the component's notifications. @type string $search_term Optional. The placeholder text in the component directory search box. Eg, 'Search Groups...'. @type array $global_tables Optional. An array of database table names. @type array $meta_tables Optional. An array of metadata table names. }
Tags
setup_nav()
Set up component navigation.
public
setup_nav([array<string|int, mixed> $main_nav = array() ][, array<string|int, mixed> $sub_nav = array() ]) : mixed
Maybe we'll use this someday.
Parameters
- $main_nav : array<string|int, mixed> = array()
-
Optional. Passed directly to cb_core_new_nav_item(). See that function for a description.
- $sub_nav : array<string|int, mixed> = array()
-
Optional. Multidimensional array, each item in which is passed to cb_core_new_subnav_item(). See that function for a description.
Tags
setup_title()
Set up the component title.
public
setup_title() : mixed
Tags
start()
Component loader.
public
start([string $id = '' ][, string $name = '' ][, string $path = '' ][, array<string|int, mixed> $params = array() ]) : mixed
Parameters
- $id : string = ''
-
Unique ID. Letters, numbers, and underscores only.
- $name : string = ''
-
Unique name. This should be a translatable name, eg. __( 'Groups', 'confetti_bits' ).
- $path : string = ''
-
- $params : array<string|int, mixed> = array()
-
{ Additional parameters used by the component. @type int $adminbar_myaccount_order Set the position for our menu under the WP Toolbar's "My Account menu". @type array $features An array of feature names. This is used to load additional files from your component directory and for feature active checks. eg. array( 'awesome' ) would look for a file called "cb-{$this->id}-awesome.php" and you could use cb_is_active( $this->id, 'awesome' ) to determine if the feature is active. @type string $search_query_arg String to be used as the query argument in component search URLs. }