class-wordpress-popular-posts-activator.php
8 years ago
class-wordpress-popular-posts-deactivator.php
8 years ago
class-wordpress-popular-posts-helper.php
8 years ago
class-wordpress-popular-posts-i18n.php
8 years ago
class-wordpress-popular-posts-image.php
8 years ago
class-wordpress-popular-posts-loader.php
8 years ago
class-wordpress-popular-posts-output.php
8 years ago
class-wordpress-popular-posts-query.php
8 years ago
class-wordpress-popular-posts-rest-controller.php
8 years ago
class-wordpress-popular-posts-settings.php
8 years ago
class-wordpress-popular-posts-template.php
8 years ago
class-wordpress-popular-posts-translate.php
8 years ago
class-wordpress-popular-posts-widget.php
8 years ago
class-wordpress-popular-posts.php
8 years ago
widget-form.php
8 years ago
class-wordpress-popular-posts-loader.php
156 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Register all actions, filters and shortcodes for the plugin |
| 5 | * |
| 6 | * @link http://cabrerahector.com |
| 7 | * @since 4.0.0 |
| 8 | * |
| 9 | * @package WordPressPopularPosts |
| 10 | * @subpackage WordPressPopularPosts/includes |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * Register all actions, filters and shortcodes for the plugin. |
| 15 | * |
| 16 | * Maintain a list of all hooks that are registered throughout |
| 17 | * the plugin, and register them with the WordPress API. Call the |
| 18 | * run function to execute the list of actions, filters and shortcodes. |
| 19 | * |
| 20 | * @package WordPressPopularPosts |
| 21 | * @subpackage WordPressPopularPosts/includes |
| 22 | * @author Hector Cabrera <me@cabrerahector.com> |
| 23 | */ |
| 24 | class WPP_Loader { |
| 25 | |
| 26 | /** |
| 27 | * The array of actions registered with WordPress. |
| 28 | * |
| 29 | * @since 4.0.0 |
| 30 | * @access protected |
| 31 | * @var array $actions The actions registered with WordPress to fire when the plugin loads. |
| 32 | */ |
| 33 | protected $actions; |
| 34 | |
| 35 | /** |
| 36 | * The array of filters registered with WordPress. |
| 37 | * |
| 38 | * @since 4.0.0 |
| 39 | * @access protected |
| 40 | * @var array $filters The filters registered with WordPress to fire when the plugin loads. |
| 41 | */ |
| 42 | protected $filters; |
| 43 | |
| 44 | /** |
| 45 | * The array of shortcodes registered with WordPress. |
| 46 | * |
| 47 | * @since 4.0.0 |
| 48 | * @access protected |
| 49 | * @var array $shortcodes The shortcodes registered with WordPress to fire when the plugin loads. |
| 50 | */ |
| 51 | protected $shortcodes; |
| 52 | |
| 53 | /** |
| 54 | * Initialize the collections used to maintain the actions and filters. |
| 55 | * |
| 56 | * @since 4.0.0 |
| 57 | */ |
| 58 | public function __construct() { |
| 59 | |
| 60 | $this->actions = array(); |
| 61 | $this->filters = array(); |
| 62 | $this->shortcodes = array(); |
| 63 | |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Add a new action to the collection to be registered with WordPress. |
| 68 | * |
| 69 | * @since 4.0.0 |
| 70 | * @param string $hook The name of the WordPress action that is being registered. |
| 71 | * @param object $component A reference to the instance of the object on which the action is defined. |
| 72 | * @param string $callback The name of the function definition on the $component. |
| 73 | * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
| 74 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. |
| 75 | */ |
| 76 | public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
| 77 | $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Add a new filter to the collection to be registered with WordPress. |
| 82 | * |
| 83 | * @since 4.0.0 |
| 84 | * @param string $hook The name of the WordPress filter that is being registered. |
| 85 | * @param object $component A reference to the instance of the object on which the filter is defined. |
| 86 | * @param string $callback The name of the function definition on the $component. |
| 87 | * @param int $priority Optional. he priority at which the function should be fired. Default is 10. |
| 88 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 |
| 89 | */ |
| 90 | public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
| 91 | $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Add a new shortcode to the collection to be registered with WordPress |
| 96 | * |
| 97 | * @since 4.0.0 |
| 98 | * @param string $tag The name of the new shortcode. |
| 99 | * @param object $component A reference to the instance of the object on which the shortcode is defined. |
| 100 | * @param string $callback The name of the function that defines the shortcode. |
| 101 | */ |
| 102 | public function add_shortcode( $tag, $component, $callback) { |
| 103 | $this->shortcodes = $this->add( $this->shortcodes, $tag, $component, $callback, 10, 2 ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * A utility function that is used to register the actions and hooks into a single |
| 108 | * collection. |
| 109 | * |
| 110 | * @since 4.0.0 |
| 111 | * @access private |
| 112 | * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). |
| 113 | * @param string $hook The name of the WordPress filter that is being registered. |
| 114 | * @param object $component A reference to the instance of the object on which the filter is defined. |
| 115 | * @param string $callback The name of the function definition on the $component. |
| 116 | * @param int $priority The priority at which the function should be fired. |
| 117 | * @param int $accepted_args The number of arguments that should be passed to the $callback. |
| 118 | * @return array The collection of actions and filters registered with WordPress. |
| 119 | */ |
| 120 | private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
| 121 | |
| 122 | $hooks[] = array( |
| 123 | 'hook' => $hook, |
| 124 | 'component' => $component, |
| 125 | 'callback' => $callback, |
| 126 | 'priority' => $priority, |
| 127 | 'accepted_args' => $accepted_args |
| 128 | ); |
| 129 | |
| 130 | return $hooks; |
| 131 | |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Register the filters and actions with WordPress. |
| 136 | * |
| 137 | * @since 4.0.0 |
| 138 | */ |
| 139 | public function run() { |
| 140 | |
| 141 | foreach ( $this->filters as $hook ) { |
| 142 | add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
| 143 | } |
| 144 | |
| 145 | foreach ( $this->actions as $hook ) { |
| 146 | add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); |
| 147 | } |
| 148 | |
| 149 | foreach ( $this->shortcodes as $hook ) { |
| 150 | add_shortcode( $hook['hook'], array( $hook['component'], $hook['callback'] ) ); |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | } |
| 156 |