PluginProbe
MultiSafepay plugin for WooCommerce / trunk
MultiSafepay plugin for WooCommerce vtrunk
6.11.1 6.12.0 6.13.0 6.2.0 6.2.1 6.3.0 6.3.1 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.6.0 6.6.1 6.6.2 6.7.0 6.7.1 6.7.2 6.7.3 6.8.0 6.8.1 6.8.2 6.8.3 6.9.0 All 84 releases
multisafepay / src / Utils / Loader.php

Loader.php in MultiSafepay plugin for WooCommerce trunk, at src/Utils/Loader.php

97 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2
3 namespace MultiSafepay\WooCommerce\Utils;
4
5 /**
6 * Register all the actions and filters for the plugin.
7 */
8 class Loader {
9
10 /**
11 * The array of actions registered with WordPress.
12 *
13 * @var array The actions registered with WordPress to fire when the plugin loads.
14 */
15 public $actions;
16
17 /**
18 * The array of filters registered with WordPress.
19 *
20 * @var array The filters registered with WordPress to fire when the plugin loads.
21 */
22 public $filters;
23
24 /**
25 * Initialize the collections used to maintain the actions and filters.
26 */
27 public function __construct() {
28 $this->actions = array();
29 $this->filters = array();
30 }
31
32 /**
33 * Add a new action to the collection to be registered with WordPress.
34 *
35 * @param string $hook The name of the WordPress action that is being registered.
36 * @param object $component A reference to the instance of the object on which the action is defined.
37 * @param string $callback The name of the function defined on the $component.
38 * @param int $priority The priority at which the function should be fired.
39 * @param int $accepted_args The number of arguments that should be passed to the $callback.
40 * @return void
41 */
42 public function add_action( string $hook, $component, string $callback, int $priority = 10, int $accepted_args = 1 ): void {
43 $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
44 }
45
46 /**
47 * Add a new filter to the collection to be registered with WordPress.
48 *
49 * @param string $hook The name of the WordPress filter that is being registered.
50 * @param object $component A reference to the instance of the object on which the filter is defined.
51 * @param string $callback The name of the function defined on the $component.
52 * @param int $priority The priority at which the function should be fired.
53 * @param int $accepted_args The number of arguments that should be passed to the $callback.
54 * @return void
55 */
56 public function add_filter( string $hook, $component, string $callback, int $priority = 10, int $accepted_args = 1 ) {
57 $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
58 }
59
60 /**
61 * A utility function that is used to register the actions and hooks into a single
62 * collection.
63 *
64 * @param array $hooks The collection of hooks that is being registered (that is, actions or filters).
65 * @param string $hook The name of the WordPress filter that is being registered.
66 * @param object $component A reference to the instance of the object on which the filter is defined.
67 * @param string $callback The name of the function definition on the $component.
68 * @param int $priority The priority at which the function should be fired.
69 * @param int $accepted_args The number of arguments that should be passed to the $callback.
70 * @return array The collection of actions and filters registered with WordPress.
71 */
72 private function add( array $hooks, string $hook, $component, string $callback, int $priority, int $accepted_args ) {
73 $hooks[] = array(
74 'hook' => $hook,
75 'component' => $component,
76 'callback' => $callback,
77 'priority' => $priority,
78 'accepted_args' => $accepted_args,
79 );
80 return $hooks;
81 }
82
83 /**
84 * Register the filters and actions with WordPress.
85 *
86 * @return void
87 */
88 public function init() {
89 foreach ( $this->filters as $hook ) {
90 add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
91 }
92 foreach ( $this->actions as $hook ) {
93 add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
94 }
95 }
96 }
97