class-qdr-tss-loader.php 4.8 KB

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