MS_Hooker

Call when you really need a hooker.

Defined (1)

The class is defined in the following location(s).

/app/class-ms-hooker.php  
  1. class MS_Hooker { 
  2.  
  3. /** 
  4. * A unique object ID which is only used for debugging and tracing data. 
  5. * @since 1.0.0 
  6. * @var null 
  7. */ 
  8. protected $_factory_id = null; 
  9.  
  10. /** 
  11. * Internal list of messages which is passed between functions. 
  12. * The message list is shared between all controller objects! 
  13. * @since 1.0.0 
  14. * @see _message() 
  15. * @var array 
  16. */ 
  17. static private $_messages = array(); 
  18.  
  19. /** 
  20. * Called before loading the model. 
  21. * @since 1.0.0 
  22. */ 
  23. public function before_load() { 
  24. do_action( 'ms_hooker_before_load', $this ); 
  25.  
  26. /** 
  27. * Load the model data. 
  28. * @since 1.0.0 
  29. */ 
  30. public function load( $model_id = false ) { 
  31. throw new Exception( 'Method to be implemented in child class' ); 
  32.  
  33. /** 
  34. * Called after loading model data. 
  35. * @since 1.0.0 
  36. */ 
  37. public function after_load() { 
  38. do_action( 'ms_hooker_after_load', $this ); 
  39.  
  40. /** 
  41. * Called my MS_Factory after an object was either created or loaded. 
  42. * @since 1.0.0 
  43. */ 
  44. public function prepare_obj() { 
  45. do_action( 'ms_hooker_prepare_obj', $this ); 
  46.  
  47. /** 
  48. * Called by MS_Factory when the first object of the class is created/loaded 
  49. * @since 1.0.0 
  50. */ 
  51. static public function prepare_class() { 
  52. do_action( 'ms_hooker_prepare_class' ); 
  53.  
  54. /** 
  55. * Returns the callback array for the specified method 
  56. * @since 1.0.0 
  57. * @param string $tag The tag that is addressed by the callback. 
  58. * @param string|array $method The callback method. 
  59. * @return array A working callback. 
  60. */ 
  61. private function get_callback( $tag, $method ) { 
  62. if ( is_array( $method ) ) { 
  63. $callback = $method; 
  64. } else { 
  65. $callback = array( $this, ! empty( $method ) ? $method : $tag ); 
  66.  
  67. return $callback; 
  68.  
  69. /** 
  70. * Registers an action hook. 
  71. * @since 1.0.0 
  72. * @uses add_action() To register action hook. 
  73. * @param string $tag The name of the action to which the $method is hooked. 
  74. * @param string $method The name of the method to be called. 
  75. * @param int $priority optional. Used to specify the order in which the 
  76. * functions associated with a particular action are executed 
  77. * (default: 10). Lower numbers correspond with earlier execution,  
  78. * and functions with the same priority are executed in the order in 
  79. * which they were added to the action. 
  80. * @param int $accepted_args optional. The number of arguments the function 
  81. * accept (default 1). 
  82. * @return MS_Hooker The Object. 
  83. */ 
  84. protected function add_action( $tag, $method = '', $priority = 10, $accepted_args = 1 ) { 
  85. add_action( 
  86. $tag,  
  87. $this->get_callback( $tag, $method ),  
  88. $priority,  
  89. $accepted_args 
  90. ); 
  91.  
  92. return $this; 
  93.  
  94. /** 
  95. * Executes the callback function instantly if the specified action was 
  96. * already fired. If the action was not fired yet then the action handler 
  97. * is registered via add_action(). 
  98. * Important note: 
  99. * If the callback is executed instantly, then the functionr receives NO 
  100. * parameters! 
  101. * @since 1.0.0 
  102. * @uses add_action() To register action hook. 
  103. * @param string $tag 
  104. * @param string $method 
  105. * @param int $priority 
  106. * @param int $accepted_args 
  107. * @return MS_Hooker 
  108. */ 
  109. protected function run_action( $tag, $method = '', $priority = 10, $accepted_args = 1 ) { 
  110. $callback = $this->get_callback( $tag, $method ); 
  111.  
  112. if ( did_action( $tag ) ) { 
  113. // Note: No argument is passed to the callback! 
  114. call_user_func( $callback ); 
  115. } else { 
  116. add_action( 
  117. $tag,  
  118. $callback,  
  119. $priority,  
  120. $accepted_args 
  121. ); 
  122.  
  123. return $this; 
  124.  
  125. /** 
  126. * Removes an action hook. 
  127. * @since 1.0.0 
  128. * @uses remove_action() To remove action hook. 
  129. * @param string $tag The name of the action to which the $method is hooked. 
  130. * @param string $method The name of the method to be called. 
  131. * @param int $priority optional. Used to specify the order in which the 
  132. * functions associated with a particular action are executed 
  133. * (default: 10). Lower numbers correspond with earlier execution,  
  134. * and functions with the same priority are executed in the order in 
  135. * which they were added to the action. 
  136. * @return MS_Hooker 
  137. */ 
  138. protected function remove_action( $tag, $method = null, $priority = 10 ) { 
  139. if ( null === $method ) { 
  140. remove_all_actions( $tag ); 
  141. } else { 
  142. remove_action( 
  143. $tag,  
  144. $this->get_callback( $tag, $method ),  
  145. $priority 
  146. ); 
  147.  
  148. return $this; 
  149.  
  150. /** 
  151. * Registers AJAX action hook. 
  152. * @since 1.0.0 
  153. * @param string $tag The name of the AJAX action to which the $method is 
  154. * hooked. 
  155. * @param string $method Optional. The name of the method to be called. 
  156. * If the name of the method is not provided, tag name will be used 
  157. * as method name. 
  158. * @param boolean $private Optional. Determines if we should register hook 
  159. * for logged in users. 
  160. * @param boolean $public Optional. Determines if we should register hook 
  161. * for not logged in users. 
  162. * @return MS_Hooker 
  163. */ 
  164. protected function add_ajax_action( $tag, $method = '', $private = true, $public = false ) { 
  165. if ( $private ) { 
  166. $this->run_action( 'wp_ajax_' . $tag, $method ); 
  167.  
  168. if ( $public ) { 
  169. $this->run_action( 'wp_ajax_nopriv_' . $tag, $method ); 
  170.  
  171. return $this; 
  172.  
  173. /** 
  174. * Removes AJAX action hook. 
  175. * @since 1.0.0 
  176. * @param string $tag The name of the AJAX action to which the $method is 
  177. * hooked. 
  178. * @param string $method Optional. The name of the method to be called. If 
  179. * the name of the method is not provided, tag name will be used as 
  180. * method name. 
  181. * @param boolean $private Optional. Determines if we should register hook 
  182. * for logged in users. 
  183. * @param boolean $public Optional. Determines if we should register hook 
  184. * for not logged in users. 
  185. * @return MS_Hooker 
  186. */ 
  187. protected function remove_ajax_action( $tag, $method = null, $private = true, $public = false ) { 
  188. if ( $private ) { 
  189. $this->remove_action( 'wp_ajax_' . $tag, $method ); 
  190.  
  191. if ( $public ) { 
  192. $this->remove_action( 'wp_ajax_nopriv_' . $tag, $method ); 
  193.  
  194. return $this; 
  195.  
  196. /** 
  197. * Registers a filter hook. 
  198. * @since 1.0.0 
  199. * @uses add_filter() To register filter hook. 
  200. * @param string $tag The name of the filter to hook the $method to. 
  201. * @param string $method The name of the method to be called when the 
  202. * filter is applied. 
  203. * @param int $priority optional. Used to specify the order in which the 
  204. * functions associated with a particular action are executed 
  205. * (default: 10). Lower numbers correspond with earlier execution,  
  206. * and functions with the same priority are executed in the order in 
  207. * which they were added to the action. 
  208. * @param int $accepted_args optional. The number of arguments the function 
  209. * accept (default 1). 
  210. * @return MS_Hooker 
  211. */ 
  212. protected function add_filter( $tag, $method = '', $priority = 10, $accepted_args = 1 ) { 
  213. $args = func_get_args(); 
  214.  
  215. add_filter( 
  216. $tag,  
  217. $this->get_callback( $tag, $method ),  
  218. $priority,  
  219. $accepted_args 
  220. ); 
  221. return $this; 
  222.  
  223. /** 
  224. * Removes a filter hook. 
  225. * @since 1.0.0 
  226. * @uses remove_filter() To remove filter hook. 
  227. * @param string $tag The name of the filter to remove the $method to. 
  228. * @param string $method The name of the method to remove. 
  229. * @param int $priority optional. The priority of the function (default: 10). 
  230. * @return MS_Hooker 
  231. */ 
  232. protected function remove_filter( $tag, $method = null, $priority = 10 ) { 
  233. if ( null === $method ) { 
  234. remove_all_filters( $tag ); 
  235. } else { 
  236. remove_filter( 
  237. $tag,  
  238. $this->get_callback( $tag, $method ),  
  239. $priority 
  240. ); 
  241.  
  242. return $this; 
  243.  
  244. /** 
  245. * Unbinds all hooks previously registered for actions and/or filters. 
  246. * @since 1.0.0 
  247. * @param boolean $actions Optional. TRUE to unbind all actions hooks. 
  248. * @param boolean $filters Optional. TRUE to unbind all filters hooks. 
  249. */ 
  250. public function unbind( $actions = true, $filters = true ) { 
  251. $types = array(); 
  252.  
  253. if ( $actions ) { 
  254. $types['actions'] = 'remove_action'; 
  255.  
  256. if ( $filters ) { 
  257. $types['filters'] = 'remove_filter'; 
  258.  
  259. foreach ( $types as $hooks => $method ) { 
  260. foreach ( $this->$hooks as $hook ) { 
  261. call_user_func_array( $method, $hook ); 
  262.  
  263. /** 
  264. * Returns property associated with the render. 
  265. * @since 1.0.0 
  266. * @param string $property The name of a property. 
  267. * @return mixed Returns mixed value of a property or NULL if a property 
  268. * doesn't exist. 
  269. */ 
  270. public function __get( $property ) { 
  271. if ( property_exists( $this, $property ) ) { 
  272. return $this->$property; 
  273.  
  274. /** 
  275. * Associates the render with specific property. 
  276. * @since 1.0.0 
  277. * @param string $property The name of a property to associate. 
  278. * @param mixed $value The value of a property. 
  279. */ 
  280. public function __set( $property, $value ) { 
  281. if ( property_exists( $this, $property ) ) { 
  282. $this->$property = $value; 
  283.  
  284. /** 
  285. * Reset the message array. 
  286. * The _message_ functions are used to set and pass data between functions; 
  287. * the data is meant to be displayed and is not stored in the database. 
  288. * @since 1.0.0 
  289. */ 
  290. static protected function _message_reset() { 
  291. MS_Hooker::$_messages = array(); 
  292.  
  293. /** 
  294. * Get or set a message. 
  295. * _message() .. return the array with all messages 
  296. * _message( 'key' ) .. return the message 'key' 
  297. * _message( 'key', 'value' ) .. set 'value' as message 'key' 
  298. * @since 1.0.0 
  299. */ 
  300. static protected function _message( $key = null, $value = null ) { 
  301. if ( ! is_array( MS_Hooker::$_messages ) ) { 
  302. $this->_message_reset(); 
  303.  
  304. if ( null === $key ) { 
  305. return MS_Hooker::$_messages; 
  306. } else if ( null !== $key && null === $value ) { 
  307. if ( isset( MS_Hooker::$_messages[$key] ) ) { 
  308. return MS_Hooker::$_messages[$key]; 
  309. } else { 
  310. return ''; 
  311. } else if ( null !== $key && null !== $value ) { 
  312. MS_Hooker::$_messages[$key] = $value; 
  313.  
  314. /** 
  315. * Debug function for developers. 
  316. * This will dump the current object to the output stream. 
  317. * @since 1.0.0 
  318. */ 
  319. public function dump() { 
  320. lib3()->debug->enable(); 
  321. // Intended debug output, leave it here. 
  322. lib3()->debug->dump( $this );