TheLib

Implement uniform data storage and sharing among all child classes.

Defined (1)

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

/lib/wpmu-lib/inc/class-thelib.php  
  1. abstract class TheLib { 
  2.  
  3. /** 
  4. * Internal data collection used to pass arguments to callback functions. 
  5. * Only used for 5.2 version as alternative to closures. 
  6. * @var array 
  7. * @internal 
  8. */ 
  9. static protected $data = array(); 
  10.  
  11. /** 
  12. * Back-reference to the main component: TheLib_Core 
  13. * @var TheLib_Core 
  14. * @internal 
  15. */ 
  16. static protected $core = null; 
  17.  
  18.  
  19. /** 
  20. * Checks if a key exists in the request-cache. 
  21. * @since 1.1.0 
  22. * @internal 
  23. * @param string $key The key to check 
  24. * @return bool 
  25. */ 
  26. protected function _have( $key ) { 
  27. return isset( self::$data[ $key ] ); 
  28.  
  29. /** 
  30. * Request cache 
  31. * @since 1.1.0 
  32. * @internal 
  33. * @param string $key 
  34. * @param mixed $value 
  35. */ 
  36. protected function _add( $key, $value ) { 
  37. if ( ! isset( self::$data[ $key ] ) 
  38. || ! is_array( self::$data[ $key ] ) 
  39. ) { 
  40. self::$data[ $key ] = array(); 
  41.  
  42. self::$data[ $key ][] = $value; 
  43.  
  44. /** 
  45. * Request cache 
  46. * @since 1.1.0 
  47. * @internal 
  48. * @param string $key 
  49. * @return mixed 
  50. */ 
  51. protected function _get( $key ) { 
  52. if ( ! isset( self::$data[ $key ] ) 
  53. || ! is_array( self::$data[ $key ] ) 
  54. ) { 
  55. self::$data[ $key ] = array(); 
  56.  
  57. return self::$data[ $key ]; 
  58.  
  59. /** 
  60. * Request cache 
  61. * @since 1.1.0 
  62. * @internal 
  63. * @param string $key 
  64. */ 
  65. protected function _clear( $key ) { 
  66. self::$data[ $key ] = array(); 
  67.  
  68.  
  69. // --- Start of Session access 
  70.  
  71. /** 
  72. * Flag if we can use the $_SESSION variable 
  73. * @var bool 
  74. * @internal 
  75. */ 
  76. static protected $_have_session = null; 
  77.  
  78. /** 
  79. * Session storage 
  80. * @since 1.1.0 
  81. * @internal 
  82. */ 
  83. static private function _sess_init() { 
  84. if ( null !== self::$_have_session ) { return; } 
  85.  
  86. self::$_have_session = false; 
  87.  
  88. if ( ! session_id() ) { 
  89. if ( ! headers_sent() ) { 
  90. /** 
  91. * Fix for IE: This is a privacy policy which states, that we do 
  92. * not collect personal contact information without consent. 
  93. * Without this declaraion IE might not save our session! 
  94. * Note that other plugins that output this header later will 
  95. * overwrite it! So this is a default value if no other file 
  96. * sends the P3P header. 
  97. * @since 3.0.0 
  98. */ 
  99. if ( WDEV_SEND_P3P ) { 
  100. $p3p_done = false; 
  101. foreach ( headers_list() as $header ) { 
  102. if ( false !== stripos( $header, 'P3P:' ) ) { 
  103. $p3p_done = true; 
  104. break; 
  105. if ( ! $p3p_done ) { header( 'P3P:' . WDEV_SEND_P3P ); } 
  106.  
  107. session_start(); 
  108. self::$_have_session = true; 
  109. } else { 
  110. self::$_have_session = true; 
  111.  
  112. /** 
  113. * Session storage 
  114. * @since 1.1.0 
  115. * @internal 
  116. * @param string $key 
  117. * @return bool 
  118. */ 
  119. static protected function _sess_have( $key ) { 
  120. if ( null === self::$_have_session ) { self::_sess_init(); } 
  121. if ( ! self::$_have_session ) { return false; } 
  122.  
  123. return isset( $_SESSION[ '_lib_persist_' . $key ] ); 
  124.  
  125. /** 
  126. * Session storage 
  127. * @since 1.1.0 
  128. * @internal 
  129. * @param string $key 
  130. * @param mixed $value 
  131. */ 
  132. static protected function _sess_add( $key, $value ) { 
  133. if ( null === self::$_have_session ) { self::_sess_init(); } 
  134. if ( ! self::$_have_session ) { return; } 
  135.  
  136. if ( ! isset( $_SESSION[ '_lib_persist_' . $key ] ) 
  137. || ! is_array( $_SESSION[ '_lib_persist_' . $key ] ) 
  138. ) { 
  139. $_SESSION[ '_lib_persist_' . $key ] = array(); 
  140.  
  141. $_SESSION[ '_lib_persist_' . $key ][] = $value; 
  142.  
  143. /** 
  144. * Session storage 
  145. * @since 1.1.0 
  146. * @internal 
  147. * @param string $key 
  148. * @return array 
  149. */ 
  150. static protected function _sess_get( $key ) { 
  151. if ( null === self::$_have_session ) { self::_sess_init(); } 
  152. if ( ! self::$_have_session ) { return array(); } 
  153.  
  154. if ( ! isset( $_SESSION[ '_lib_persist_' . $key ] ) 
  155. || ! is_array( $_SESSION[ '_lib_persist_' . $key ] ) 
  156. ) { 
  157. $_SESSION[ '_lib_persist_' . $key ] = array(); 
  158.  
  159. return $_SESSION[ '_lib_persist_' . $key ]; 
  160.  
  161. /** 
  162. * Session storage 
  163. * @since 1.1.0 
  164. * @internal 
  165. * @param string $key 
  166. */ 
  167. static protected function _sess_clear( $key ) { 
  168. if ( null === self::$_have_session ) { self::_sess_init(); } 
  169. if ( ! self::$_have_session ) { return; } 
  170.  
  171. unset( $_SESSION[ '_lib_persist_' . $key ] ); 
  172.  
  173. // --- End of Session access 
  174.  
  175. /** 
  176. * Base constructor. Initialize the session if not already done. 
  177. * @since 1.1.0 
  178. * @internal 
  179. */ 
  180. public function __construct() { 
  181. self::_init_const(); 
  182. self::_sess_init(); 
  183.  
  184. /** 
  185. * Initializes missing module-flags with default values. 
  186. * @since 3.0.0 
  187. * @internal 
  188. */ 
  189. static protected function _init_const() { 
  190. if ( ! defined( 'WDEV_UNMINIFIED' ) ) { 
  191. define( 'WDEV_UNMINIFIED', false ); 
  192. if ( ! defined( 'WDEV_DEBUG' ) ) { 
  193. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  194. define( 'WDEV_DEBUG', true ); 
  195. } else { 
  196. define( 'WDEV_DEBUG', false ); 
  197. if ( ! defined( 'WDEV_AJAX_DEBUG' ) ) { 
  198. define( 'WDEV_AJAX_DEBUG', WDEV_DEBUG ); 
  199. if ( ! defined( 'WDEV_SEND_P3P' ) ) { 
  200. define( 'WDEV_SEND_P3P', 'CP="NOI"' ); 
  201.  
  202. /** 
  203. * Returns the full URL to an internal CSS file of the code library. 
  204. * @since 1.0.0 
  205. * @internal 
  206. * @param string $file The filename, relative to this plugins folder. 
  207. * @return string 
  208. */ 
  209. protected function _css_url( $file ) { 
  210. static $Url = null; 
  211.  
  212. if ( WDEV_UNMINIFIED ) { 
  213. $file = str_replace( '.min.css', '.css', $file ); 
  214. if ( null === $Url ) { 
  215. $Url = plugins_url( 'css/', dirname( __FILE__ ) ); 
  216.  
  217. return $Url . $file; 
  218.  
  219. /** 
  220. * Returns the full URL to an internal JS file of the code library. 
  221. * @since 1.0.0 
  222. * @internal 
  223. * @param string $file The filename, relative to this plugins folder. 
  224. * @return string 
  225. */ 
  226. protected function _js_url( $file ) { 
  227. static $Url = null; 
  228.  
  229. if ( WDEV_UNMINIFIED ) { 
  230. $file = str_replace( '.min.js', '.js', $file ); 
  231. if ( null === $Url ) { 
  232. $Url = plugins_url( 'js/', dirname( __FILE__ ) ); 
  233.  
  234. return $Url . $file; 
  235.  
  236. /** 
  237. * Returns the full path to an internal php partial of the code library. 
  238. * @since 1.0.0 
  239. * @internal 
  240. * @param string $file The filename, relative to this plugins folder. 
  241. * @return string 
  242. */ 
  243. protected function _view_path( $file ) { 
  244. static $Path = null; 
  245.  
  246. if ( null === $Path ) { 
  247. $basedir = dirname( dirname( __FILE__ ) ) . '/'; 
  248. $Path = $basedir . 'view/'; 
  249.  
  250. return $Path . $file; 
  251.  
  252. /** 
  253. * Adds or executes an action. 
  254. * @since 1.1.3 
  255. * @api 
  256. * @param string $tag The action-name. 
  257. * @param string $function Function name (must be a class function) 
  258. * @param int $priority Execution priority. Lower is earlier. 
  259. */ 
  260. protected function add_action( $tag, $function, $priority = 10 ) { 
  261. $hooked = $this->_have( '_hooked_action-' . $tag ); 
  262.  
  263. if ( did_action( $tag ) ) { 
  264. $this->$function(); 
  265. } else { 
  266. $this->_add( '_hooked_action-' . $tag, true ); 
  267. add_action( $tag, array( $this, $function ), $priority ); 
  268.