/lava/_classes/lavaBase.php

https://github.com/volcanicpixels/Privacy-Plugin · PHP · 419 lines · 241 code · 38 blank · 140 comment · 40 complexity · 1396b76f8a4e244cc81cd80434f761b6 MD5 · raw file

  1. <?php
  2. /**
  3. * The lava base class
  4. *
  5. * This class is the base class for all lava classes - it adds chaining and config.
  6. *
  7. * @package Lava
  8. * @subpackage lavaBase
  9. *
  10. * @author Daniel Chatfield
  11. * @copyright 2011
  12. * @version 1.0.0
  13. */
  14. /**
  15. * lavaBase
  16. *
  17. * @package Lava
  18. * @subpackage LavaBase
  19. * @author Daniel Chatfield
  20. *
  21. * @since 1.0.0
  22. */
  23. class lavaBase
  24. {
  25. protected $pluginInstance;
  26. protected $chain = array();
  27. protected $memory = array();
  28. public $suffixes = array( "/pre", "", "/post" );
  29. public $allowMethodMiss = false;
  30. public $autoMethods = false;
  31. /**
  32. * __construct function.
  33. *
  34. * This method stores the plugin instance into a property so that chaining can be implemented.
  35. *
  36. * @magic
  37. * @param lavaPlugin $pluginInstance
  38. * @param array $arguments
  39. * @return void
  40. *
  41. * @since 1.0.0
  42. */
  43. function __construct( $pluginInstance, $arguments = array() )
  44. {
  45. $this->pluginInstance = $pluginInstance;
  46. if( method_exists( $this, "lavaConstruct" ) )//call the sub classes construct argument
  47. {
  48. $callback = array( $this, "lavaConstruct" );
  49. call_user_func_array( $callback, $arguments );
  50. }
  51. $this->addAutoMethods();
  52. }
  53. /**
  54. * __call function.
  55. *
  56. * This method implements chaining (allows lavaPlugin method calls to be called from any class)
  57. *
  58. * @magic
  59. * @param lavaPlugin $pluginInstance
  60. * @param array $arguments
  61. * @return void
  62. *
  63. * @since 1.0.0
  64. */
  65. function __call( $methodName, $arguments )
  66. {
  67. // lavaPlugin chainable methods start with "_" - so this is checking to see whether we should try a lavaPlugin method
  68. if( substr( $methodName, 0, 1 ) == "_" )
  69. {
  70. if( method_exists( $this->pluginInstance, $methodName ) )
  71. {
  72. $callback = array( $this->pluginInstance, $methodName );
  73. return call_user_func_array( $callback, $arguments );
  74. }
  75. }
  76. elseif( !is_null( $this->lavaContext() ) )
  77. {
  78. //lets see if the class that is the current context has this method
  79. if( method_exists( $this->lavaContext(), $methodName ) )
  80. {
  81. $callback = array( $this->lavaContext(), $methodName );
  82. return call_user_func_array( $callback, $arguments );
  83. }
  84. }
  85. $parent = $this->getContext( "parent" );
  86. if( !is_null( $parent ) )
  87. {
  88. $object = $this->lavaContext( null, "parent" );
  89. if( method_exists( $object, $methodName ) )
  90. {
  91. $callback = array( $object, $methodName );
  92. return call_user_func_array( $callback, $arguments );
  93. }
  94. }
  95. //some classes have methods with same name on parent and child. To get around this the parent method is prepended "parent_". Since no child exists with this method we should now check to see if parent has this method.
  96. if( method_exists( $this, "parent_$methodName" ) )
  97. {
  98. $callback = array( $this, "parent_$methodName" );
  99. return call_user_func_array( $callback, $arguments );
  100. }
  101. if( ! $this->allowMethodMiss )
  102. {
  103. echo "<h2>LavaError thrown on line 110 of lavaBase.php</h2> <br/>";
  104. echo "Could not find method '{$methodName}' on object of class '" . get_class( $this ) . "'. We also tried the current child which has class '" . get_class( $this->getContext() ) . "' and the parent which has class '" . get_class( $this->getContext() ) . "'.";
  105. exit;
  106. }
  107. //to prevent a dummy method call from returning a child parents set an "if lost return to" property on the children - we should check to see if it exists
  108. if( isset( $this->lavaCallReturn ) )
  109. {
  110. return $this->lavaCallReturn;
  111. }
  112. return $this;//couldn't find anything to call so return this object so chaining doesn't break
  113. }
  114. function addAutoMethods() {
  115. if( $this->autoMethods == true ) {
  116. $this->_misc()->_addAutoMethods( $this );
  117. }
  118. }
  119. //meant to be overridden - so a class can forward a request to something else
  120. function getThis() {
  121. return $this;
  122. }
  123. /**
  124. * lavaReset function.
  125. *
  126. * Resets the chain (prevents unexpected chaining to occur)
  127. *
  128. * @return $this
  129. *
  130. * @since 1.0.0
  131. */
  132. function lavaReset()
  133. {
  134. $this->chain = array();
  135. return $this;
  136. }
  137. /**
  138. * lavaContext function.
  139. *
  140. * adds/removes context
  141. *
  142. * @return $this
  143. *
  144. * @since 1.0.0
  145. */
  146. final function lavaContext( $context = null, $handle = "current" )
  147. {
  148. if( null != $context)
  149. {
  150. $this->chain[ $handle ] = $context;
  151. }
  152. if( array_key_exists($handle, $this->chain) ) {
  153. return $this->chain[ $handle ];
  154. } else {
  155. return $this;
  156. }
  157. }
  158. /**
  159. * lavaContext function.
  160. *
  161. * adds/removes context
  162. *
  163. * @return $this
  164. *
  165. * @since 1.0.0
  166. */
  167. final function setContext( $context = null, $handle = "current" )
  168. {
  169. $this->chain[ $handle ] = $context;
  170. }
  171. /**
  172. * lavaContext function.
  173. *
  174. * adds/removes context
  175. *
  176. * @return $this
  177. *
  178. * @since 1.0.0
  179. */
  180. final function getContext( $handle = "current" )
  181. {
  182. if( array_key_exists( $handle, $this->chain ) )
  183. {
  184. return $this->chain[ $handle ];
  185. }
  186. return null;
  187. }
  188. /**
  189. * withinContext function.
  190. *
  191. * Sets the parent handler (adds to chain for method lookups)
  192. *
  193. * @return $this
  194. *
  195. * @since 1.0.0
  196. */
  197. final function withinContext( $context )
  198. {
  199. $this->setContext( $context, "parent" );
  200. return $this;
  201. }
  202. /**
  203. * clearLavaContext function.
  204. *
  205. * adds/removes context
  206. *
  207. * @return $this
  208. *
  209. * @since 1.0.0
  210. */
  211. final function clearContext( $handle = "current" )
  212. {
  213. $this->chain[ $handle ] = null;
  214. }
  215. /**
  216. * lavaRemember function.
  217. *
  218. * The lavaRemember function stores data as a key>value pair as a protected property to a class
  219. *
  220. * @param string $key
  221. * @param $value (default: null)
  222. * @return $this || $value || false
  223. *
  224. * @since 1.0.0
  225. */
  226. function lavaRemember( $key, $value = null )
  227. {
  228. if( isset( $value ) )
  229. {//value has been given - so lets set it
  230. $this->memory[ $key ] = $value;
  231. return $this;
  232. }
  233. if( isset( $this->memory[ $key ] ) )
  234. {
  235. return $this->memory[ $key ];
  236. }
  237. return false;
  238. }
  239. function addWPAction( $hookTags, $methodNames = "", $priority = 10, $debug = false ) {
  240. if( !is_array( $hookTags ) ) {
  241. $hookTags = array( $hookTags );
  242. }
  243. if( !is_array( $methodNames ) ) {
  244. $methodNames = array( $methodNames );
  245. }
  246. foreach( $hookTags as $hookTag ) {
  247. foreach( $methodNames as $methodName ) {
  248. $_methodName = $methodName;
  249. if( empty( $_methodName) ) {
  250. $_methodName = $hookTag;
  251. }
  252. //if( $debug) { echo $hookTag; echo "<br>"; echo $_methodName;echo "<br>"; }
  253. add_action( $hookTag, array( $this, $_methodName ), $priority );
  254. }
  255. }
  256. //if( $debug ) exit;
  257. }
  258. function addWPFilter( $hookTags, $methodNames = "", $priority = 10, $args = 1 ) {
  259. if( !is_array( $hookTags ) ) {
  260. $hookTags = array( $hookTags );
  261. }
  262. if( !is_array( $methodNames ) ) {
  263. $methodNames = array( $methodNames );
  264. }
  265. foreach( $hookTags as $hookTag ) {
  266. foreach( $methodNames as $methodName ) {
  267. $_methodName = $methodName;
  268. if( empty( $_methodName) ) {
  269. $_methodName = $hookTag;
  270. }
  271. //if( $debug) { echo $hookTag; echo "<br>"; echo $_methodName;echo "<br>"; }
  272. add_filter( $hookTag, array( $this, $_methodName ), $priority, $args );
  273. }
  274. }
  275. //if( $debug ) exit;
  276. }
  277. function addAction( $hookTags, $methodNames = "", $priority = 10 ) {
  278. if( !is_array( $hookTags ) ) {
  279. $hookTags = array( $hookTags );
  280. }
  281. if( !is_array( $methodNames ) ) {
  282. $methodNames = array( $methodNames );
  283. }
  284. foreach( $hookTags as $hookTag ) {
  285. foreach( $methodNames as $methodName ) {
  286. $_methodName = $methodName;
  287. if( empty( $_methodName) ) {
  288. $_methodName = $hookTag;
  289. }
  290. add_action( $this->_slug( $hookTag ), array( $this, $_methodName ), $priority );
  291. }
  292. }
  293. }
  294. function addFilter( $hookTags, $methodNames = "", $priority = 10, $args = 1 ) {
  295. if( !is_array( $hookTags ) ) {
  296. $hookTags = array( $hookTags );
  297. }
  298. if( !is_array( $methodNames ) ) {
  299. $methodNames = array( $methodNames );
  300. }
  301. foreach( $hookTags as $hookTag ) {
  302. foreach( $methodNames as $methodName ) {
  303. $_methodName = $methodName;
  304. if( empty( $_methodName) ) {
  305. $_methodName = $hookTag;
  306. }
  307. add_filter( $this->_slug( $hookTag ), array( $this, $_methodName ), $priority, $args );
  308. }
  309. }
  310. }
  311. /**
  312. * runActions function.
  313. *
  314. * Runs the actions with all the parameters
  315. *
  316. * @param string $key
  317. * @param $value (default: null)
  318. *
  319. * @since 1.0.0
  320. */
  321. function runActions( $hookTag, $debug = false )
  322. {
  323. $hooks = array_unique( $this->hookTags() );
  324. $suffixes = array_unique( $this->suffixes );
  325. foreach( $suffixes as $suffix)
  326. {
  327. foreach( $hooks as $hook )
  328. {
  329. if( $hook == " " ) {
  330. $hook = "";
  331. } else {
  332. $hook = "-".$hook;
  333. }
  334. if( $debug )
  335. {
  336. echo $this->_slug( "{$hookTag}{$hook}{$suffix}" ) . "\n";
  337. }
  338. do_action( $this->_slug( "{$hookTag}{$hook}{$suffix}" ), $this );
  339. }
  340. }
  341. }
  342. /**
  343. * runActions function.
  344. *
  345. * Runs the filters with all the parameters
  346. *
  347. * @param string $hookTag
  348. * @param $args (default: null)
  349. *
  350. * @since 1.0.0
  351. */
  352. function runFilters( $hookTag, $argument = "", $args = null, $debug = false )
  353. {
  354. if( is_null( $args ) ) {
  355. $args = $this;
  356. }
  357. $hooks = array_unique( $this->hookTags() );
  358. $suffixes = array_unique( $this->suffixes );
  359. foreach( $suffixes as $suffix)
  360. {
  361. foreach( $hooks as $hook )
  362. {
  363. if( $hook == " " ) {
  364. $hook = "";
  365. } else {
  366. $hook = "-".$hook;
  367. }
  368. //echo( $this->_slug( "{$hookTag}{$hook}{$suffix}" ). "<br/>" );
  369. $theHook = $this->_slug( "{$hookTag}{$hook}{$suffix}" );
  370. if( $debug ){ echo( "$theHook<br>" ); }
  371. $argument = apply_filters( $theHook, $argument, $args );
  372. }
  373. }
  374. return $argument;
  375. }
  376. function hookTags()
  377. {
  378. return array( " " );
  379. }
  380. }
  381. ?>