class-easy-charts-loader.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Register all actions and filters for the plugin
  4. *
  5. * @link https://kiranpotphode.com/
  6. * @since 1.0.0
  7. *
  8. * @package Easy_Charts
  9. * @subpackage Easy_Charts/includes
  10. */
  11. /**
  12. * Register all actions and filters for the plugin.
  13. *
  14. * Maintain a list of all hooks that are registered throughout
  15. * the plugin, and register them with the WordPress API. Call the
  16. * run function to execute the list of actions and filters.
  17. *
  18. * @package Easy_Charts
  19. * @subpackage Easy_Charts/includes
  20. * @author Kiran Potphode <kiranpotphode15@gmail.com>
  21. */
  22. class Easy_Charts_Loader {
  23. /**
  24. * The array of actions registered with WordPress.
  25. *
  26. * @since 1.0.0
  27. * @access protected
  28. * @var array $actions The actions registered with WordPress to fire when the plugin loads.
  29. */
  30. protected $actions;
  31. /**
  32. * The array of filters registered with WordPress.
  33. *
  34. * @since 1.0.0
  35. * @access protected
  36. * @var array $filters The filters registered with WordPress to fire when the plugin loads.
  37. */
  38. protected $filters;
  39. /**
  40. * Initialize the collections used to maintain the actions and filters.
  41. *
  42. * @since 1.0.0
  43. */
  44. public function __construct() {
  45. $this->actions = array();
  46. $this->filters = array();
  47. }
  48. /**
  49. * Add a new action to the collection to be registered with WordPress.
  50. *
  51. * @since 1.0.0
  52. * @param string $hook The name of the WordPress action that is being registered.
  53. * @param object $component A reference to the instance of the object on which the action is defined.
  54. * @param string $callback The name of the function definition on the $component.
  55. * @param int $priority Optional. he priority at which the function should be fired. Default is 10.
  56. * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
  57. */
  58. public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
  59. $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
  60. }
  61. /**
  62. * Add a new filter to the collection to be registered with WordPress.
  63. *
  64. * @since 1.0.0
  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 Optional. he priority at which the function should be fired. Default is 10.
  69. * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1
  70. */
  71. public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
  72. $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
  73. }
  74. /**
  75. * A utility function that is used to register the actions and hooks into a single
  76. * collection.
  77. *
  78. * @since 1.0.0
  79. * @access private
  80. * @param array $hooks The collection of hooks that is being registered (that is, actions or filters).
  81. * @param string $hook The name of the WordPress filter that is being registered.
  82. * @param object $component A reference to the instance of the object on which the filter is defined.
  83. * @param string $callback The name of the function definition on the $component.
  84. * @param int $priority The priority at which the function should be fired.
  85. * @param int $accepted_args The number of arguments that should be passed to the $callback.
  86. * @return array The collection of actions and filters registered with WordPress.
  87. */
  88. private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
  89. $hooks[] = array(
  90. 'hook' => $hook,
  91. 'component' => $component,
  92. 'callback' => $callback,
  93. 'priority' => $priority,
  94. 'accepted_args' => $accepted_args,
  95. );
  96. return $hooks;
  97. }
  98. /**
  99. * Register the filters and actions with WordPress.
  100. *
  101. * @since 1.0.0
  102. */
  103. public function run() {
  104. foreach ( $this->filters as $hook ) {
  105. add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
  106. }
  107. foreach ( $this->actions as $hook ) {
  108. add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
  109. }
  110. }
  111. }