class-simple-job-board-loader.php 5.0 KB

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