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