| 1 |
<?php |
| 2 |
/** |
| 3 |
* Docket Cache. |
| 4 |
* |
| 5 |
* @author Nawawi Jamili |
| 6 |
* @license MIT |
| 7 |
* |
| 8 |
* @see https://github.com/nawawi/docket-cache |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Nawawi\DocketCache; |
| 12 |
|
| 13 |
\defined('ABSPATH') || exit; |
| 14 |
|
| 15 |
final class Constans |
| 16 |
{ |
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
$this->register_default(); |
| 20 |
} |
| 21 |
|
| 22 |
public function px($name) |
| 23 |
{ |
| 24 |
return nwdcx_constfx($name); |
| 25 |
} |
| 26 |
|
| 27 |
public function is_false($name) |
| 28 |
{ |
| 29 |
return !\defined($name) || !\constant($name); |
| 30 |
} |
| 31 |
|
| 32 |
public function is_true($name) |
| 33 |
{ |
| 34 |
return \defined($name) && \constant($name); |
| 35 |
} |
| 36 |
|
| 37 |
public function value($name) |
| 38 |
{ |
| 39 |
$value = ''; |
| 40 |
if (\defined($name)) { |
| 41 |
$value = \constant($name); |
| 42 |
} |
| 43 |
|
| 44 |
return $value; |
| 45 |
} |
| 46 |
|
| 47 |
public function is_array($name) |
| 48 |
{ |
| 49 |
$value = $this->value($name); |
| 50 |
|
| 51 |
return !empty($value) && \is_array($value); |
| 52 |
} |
| 53 |
|
| 54 |
public function is_int($name) |
| 55 |
{ |
| 56 |
$value = $this->value($name); |
| 57 |
|
| 58 |
return !empty($value) && \is_int($value); |
| 59 |
} |
| 60 |
|
| 61 |
public function is($name, $value) |
| 62 |
{ |
| 63 |
return \defined($name) && $value === \constant($name); |
| 64 |
} |
| 65 |
|
| 66 |
public function is_dctrue($name, $reload = false) |
| 67 |
{ |
| 68 |
$key = $this->px($name); |
| 69 |
if ($reload) { |
| 70 |
$this->maybe_define($key, false, true); |
| 71 |
} |
| 72 |
|
| 73 |
return $this->is_true($key); |
| 74 |
} |
| 75 |
|
| 76 |
public function is_dcfalse($name, $reload = false) |
| 77 |
{ |
| 78 |
$key = $this->px($name); |
| 79 |
if ($reload) { |
| 80 |
$this->maybe_define($key, false, true); |
| 81 |
} |
| 82 |
|
| 83 |
return $this->is_false($key); |
| 84 |
} |
| 85 |
|
| 86 |
public function is_dcarray($name, &$value = '', $reload = false) |
| 87 |
{ |
| 88 |
$key = $this->px($name); |
| 89 |
if ($reload) { |
| 90 |
$this->maybe_define($key, false, true); |
| 91 |
} |
| 92 |
|
| 93 |
$value = ''; |
| 94 |
if ($this->is_array($key)) { |
| 95 |
$value = $this->value($key); |
| 96 |
|
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
public function is_dcint($name, &$value = '', $reload = false) |
| 104 |
{ |
| 105 |
$key = $this->px($name); |
| 106 |
if ($reload) { |
| 107 |
$this->maybe_define($key, false, true); |
| 108 |
} |
| 109 |
|
| 110 |
$value = ''; |
| 111 |
if ($this->is_int($key)) { |
| 112 |
$value = $this->value($key); |
| 113 |
|
| 114 |
return true; |
| 115 |
} |
| 116 |
|
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
public function dcvalue($name, $reload = false) |
| 121 |
{ |
| 122 |
$key = $this->px($name); |
| 123 |
if ($reload) { |
| 124 |
$this->maybe_define($key, '', true); |
| 125 |
} |
| 126 |
|
| 127 |
return $this->value($key); |
| 128 |
} |
| 129 |
|
| 130 |
public function maybe_define($name, $value, $user_config = true) |
| 131 |
{ |
| 132 |
if (!\defined($name)) { |
| 133 |
if ($user_config && class_exists('Nawawi\\DocketCache\\Canopt')) { |
| 134 |
$nv = Canopt::init()->get($name); |
| 135 |
if (!empty($nv) && 'default' !== $nv) { |
| 136 |
switch ($nv) { |
| 137 |
case 'enable': |
| 138 |
$nv = true; |
| 139 |
break; |
| 140 |
case 'disable': |
| 141 |
$nv = false; |
| 142 |
break; |
| 143 |
} |
| 144 |
|
| 145 |
$value = $nv; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return @\define($name, $value); |
| 150 |
} |
| 151 |
|
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
public function register_default() |
| 156 |
{ |
| 157 |
// compat |
| 158 |
$this->maybe_define('WP_CONTENT_DIR', nwdcx_normalizepath(ABSPATH).'wp-content', false); |
| 159 |
$this->maybe_define('WP_PLUGIN_DIR', nwdcx_normalizepath(WP_CONTENT_DIR).'/plugins', false); |
| 160 |
$this->maybe_define('DOCKET_CACHE_CONTENT_PATH', nwdcx_normalizepath(WP_CONTENT_DIR), false); |
| 161 |
|
| 162 |
// data dir |
| 163 |
$this->maybe_define($this->px('DATA_PATH'), DOCKET_CACHE_CONTENT_PATH.'/docket-cache-data/', false); |
| 164 |
|
| 165 |
// cache dir |
| 166 |
$this->maybe_define($this->px('PATH'), DOCKET_CACHE_CONTENT_PATH.'/cache/docket-cache/', false); |
| 167 |
|
| 168 |
// cache file max size: 3MB, 1MB = 1048576 bytes (binary) = 1000000 bytes (decimal) |
| 169 |
// Only numbers between 1000000 and 10485760 are accepted |
| 170 |
$this->maybe_define($this->px('MAXSIZE'), 3145728); |
| 171 |
|
| 172 |
// cache file max size total: 500MB, 1MB = 1048576 bytes (binary) = 1000000 bytes (decimal) |
| 173 |
// minimum 100MB |
| 174 |
$this->maybe_define($this->px('MAXSIZE_DISK'), 524288000); |
| 175 |
|
| 176 |
// cache file max accelerated files: Only numbers between 200 and 200000 are accepted |
| 177 |
$this->maybe_define($this->px('MAXFILE'), 50000); |
| 178 |
|
| 179 |
// cache maxttl: cache lifespan. Only seconds between 86400 and 2419200 are accepted |
| 180 |
$this->maybe_define($this->px('MAXTTL'), 345600); // 4d |
| 181 |
|
| 182 |
// log on/off |
| 183 |
$this->maybe_define($this->px('LOG'), false); |
| 184 |
|
| 185 |
// log all on/off |
| 186 |
$this->maybe_define($this->px('LOG_ALL'), \defined('WP_DEBUG') ? WP_DEBUG : false); |
| 187 |
|
| 188 |
// log file |
| 189 |
$this->maybe_define($this->px('LOG_FILE'), DOCKET_CACHE_CONTENT_PATH.'/.object-cache.log'); |
| 190 |
|
| 191 |
// empty file when cache flushed |
| 192 |
$this->maybe_define($this->px('LOG_FLUSH'), true); |
| 193 |
|
| 194 |
// log time format: utc, local |
| 195 |
$this->maybe_define($this->px('LOG_TIME'), 'utc'); |
| 196 |
|
| 197 |
// log file max size: 10MB, 1MB = 1048576 bytes (binary) = 1000000 bytes (decimal) |
| 198 |
$this->maybe_define($this->px('LOG_SIZE'), 10485760); |
| 199 |
|
| 200 |
// truncate or delete cache file |
| 201 |
$this->maybe_define($this->px('FLUSH_DELETE'), false); |
| 202 |
|
| 203 |
// flush cache when deactivate/uninstall |
| 204 |
$this->maybe_define($this->px('FLUSH_SHUTDOWN'), true); |
| 205 |
|
| 206 |
// flush wc_cache / advanced post cache stale cache |
| 207 |
$this->maybe_define($this->px('FLUSH_STALECACHE'), false); |
| 208 |
|
| 209 |
// split a cache file into smaller directory |
| 210 |
$this->maybe_define($this->px('CHUNKCACHEDIR'), false); |
| 211 |
|
| 212 |
// optimize db |
| 213 |
$this->maybe_define($this->px('CRONOPTMZDB'), 'never'); |
| 214 |
|
| 215 |
// option autoload |
| 216 |
$this->maybe_define($this->px('WPOPTALOAD'), false); |
| 217 |
|
| 218 |
// global cache group |
| 219 |
$this->maybe_define( |
| 220 |
$this->px('GLOBAL_GROUPS'), |
| 221 |
[ |
| 222 |
'blog-details', |
| 223 |
'blog-id-cache', |
| 224 |
'blog-lookup', |
| 225 |
'global-posts', |
| 226 |
'networks', |
| 227 |
'rss', |
| 228 |
'sites', |
| 229 |
'site-details', |
| 230 |
'site-lookup', |
| 231 |
'site-options', |
| 232 |
'site-transient', |
| 233 |
'users', |
| 234 |
'useremail', |
| 235 |
'userlogins', |
| 236 |
'usermeta', |
| 237 |
'user_meta', |
| 238 |
'userslugs', |
| 239 |
] |
| 240 |
); |
| 241 |
|
| 242 |
// cache ignored groups |
| 243 |
$this->maybe_define( |
| 244 |
$this->px('IGNORED_GROUPS'), |
| 245 |
[ |
| 246 |
'themes', |
| 247 |
'counts', |
| 248 |
'plugins', |
| 249 |
] |
| 250 |
); |
| 251 |
|
| 252 |
// @private |
| 253 |
// cache ignored keys |
| 254 |
// @note: dnh_dismissed_notices -> https://github.com/julien731/WP-Dismissible-Notices-Handler |
| 255 |
$this->maybe_define($this->px('IGNORED_KEYS'), ['dnh_dismissed_notices']); |
| 256 |
|
| 257 |
// @private |
| 258 |
// this option private for right now |
| 259 |
$this->maybe_define( |
| 260 |
$this->px('FILTERED_GROUPS'), |
| 261 |
[ |
| 262 |
'counts' => [ |
| 263 |
'posts-page', |
| 264 |
'posts-post', |
| 265 |
], |
| 266 |
] |
| 267 |
); |
| 268 |
|
| 269 |
// @private |
| 270 |
// cache ignored group:key |
| 271 |
$this->maybe_define($this->px('IGNORED_GROUPKEY'), []); |
| 272 |
|
| 273 |
// precache |
| 274 |
$this->maybe_define($this->px('PRECACHE'), false); |
| 275 |
|
| 276 |
// precache maxfile: < 1, false, null = unlimited |
| 277 |
$this->maybe_define($this->px('PRECACHE_MAXFILE'), 1000); |
| 278 |
|
| 279 |
// precache maxlist |
| 280 |
$this->maybe_define($this->px('PRECACHE_MAXLIST'), 1000); |
| 281 |
|
| 282 |
// @private |
| 283 |
// cache ignored precache |
| 284 |
$this->maybe_define( |
| 285 |
$this->px('IGNORED_PRECACHE'), |
| 286 |
[ |
| 287 |
'freemius:fs_accounts', |
| 288 |
'options:uninstall_plugins', |
| 289 |
'options:active_plugins', |
| 290 |
'options:cron', |
| 291 |
'options:litespeed_messages', |
| 292 |
'options:litespeed.admin_display.messages', |
| 293 |
] |
| 294 |
); |
| 295 |
|
| 296 |
// misc tweaks |
| 297 |
$this->maybe_define($this->px('MISC_TWEAKS'), true); |
| 298 |
|
| 299 |
// woocommerce tweaks |
| 300 |
$this->maybe_define($this->px('WOOTWEAKS'), true); |
| 301 |
|
| 302 |
// woocommerce admin |
| 303 |
$this->maybe_define($this->px('WOOADMINOFF'), false); |
| 304 |
|
| 305 |
// woocommerce dashboard status meta box |
| 306 |
$this->maybe_define($this->px('WOOWPDASHBOARDOFF'), false); |
| 307 |
|
| 308 |
// woocommerce widget |
| 309 |
$this->maybe_define($this->px('WOOWIDGETOFF'), false); |
| 310 |
|
| 311 |
// woocommerce cart fragments |
| 312 |
$this->maybe_define($this->px('WOOCARTFRAGSOFF'), false); |
| 313 |
|
| 314 |
// woocommerce robots crawling add-to-cart links |
| 315 |
$this->maybe_define($this->px('WOOADDTOCHARTCRAWLING'), true); |
| 316 |
|
| 317 |
// woocommerce marketplace / my subscription menu page |
| 318 |
$this->maybe_define($this->px('WOOEXTENSIONPAGEOFF'), false); |
| 319 |
|
| 320 |
// post missed schedule |
| 321 |
$this->maybe_define($this->px('POSTMISSEDSCHEDULE'), false); |
| 322 |
|
| 323 |
// advanced post cache |
| 324 |
$this->maybe_define($this->px('ADVCPOST'), false); |
| 325 |
|
| 326 |
// advanced post cache allow post type |
| 327 |
$this->maybe_define($this->px('ADVCPOST_POSTTYPE'), ['post', 'page', 'attachment']); |
| 328 |
|
| 329 |
// advanced post cache allow all post type |
| 330 |
$this->maybe_define($this->px('ADVCPOST_POSTTYPE_ALL'), false); |
| 331 |
|
| 332 |
// optimize term count |
| 333 |
$this->maybe_define($this->px('OPTERMCOUNT'), true); |
| 334 |
|
| 335 |
// translation mo file cache |
| 336 |
$this->maybe_define($this->px('MOCACHE'), false); |
| 337 |
|
| 338 |
// menu cache |
| 339 |
$this->maybe_define($this->px('MENUCACHE'), false); |
| 340 |
|
| 341 |
// menu cache ttl: 1209600 = 14 days |
| 342 |
$this->maybe_define($this->px('MENUCACHE_TTL'), 1209600); |
| 343 |
|
| 344 |
// @private |
| 345 |
// wp-cli |
| 346 |
$this->maybe_define($this->px('WPCLI'), \defined('WP_CLI') && WP_CLI); |
| 347 |
|
| 348 |
// banner |
| 349 |
$this->maybe_define($this->px('SIGNATURE'), true); |
| 350 |
|
| 351 |
// preload |
| 352 |
$this->maybe_define($this->px('PRELOAD'), false); |
| 353 |
|
| 354 |
// page loader |
| 355 |
$this->maybe_define($this->px('PAGELOADER'), true); |
| 356 |
|
| 357 |
// docket cronbot |
| 358 |
$this->maybe_define($this->px('CRONBOT'), true); |
| 359 |
|
| 360 |
// docket cronbot |
| 361 |
$this->maybe_define($this->px('CRONBOT_MAX'), 10); |
| 362 |
|
| 363 |
// opcviewer |
| 364 |
$this->maybe_define($this->px('OPCVIEWER'), false); |
| 365 |
|
| 366 |
// cache stats |
| 367 |
$this->maybe_define($this->px('STATS'), true); |
| 368 |
|
| 369 |
// gc action button |
| 370 |
$this->maybe_define($this->px('GCACTION'), false); |
| 371 |
|
| 372 |
// additional flush cache button |
| 373 |
$this->maybe_define($this->px('FLUSHACTION'), false); |
| 374 |
|
| 375 |
// check version |
| 376 |
$this->maybe_define($this->px('CHECKVERSION'), false); |
| 377 |
|
| 378 |
// auto update |
| 379 |
$this->maybe_define($this->px('AUTOUPDATE'), false); |
| 380 |
|
| 381 |
// flush opcache when deactivate |
| 382 |
$this->maybe_define($this->px('OPCSHUTDOWN'), false); |
| 383 |
|
| 384 |
// optimize post query |
| 385 |
$this->maybe_define($this->px('OPTWPQUERY'), true); |
| 386 |
|
| 387 |
// xmlrpc pingbacks |
| 388 |
$this->maybe_define($this->px('PINGBACK'), true); |
| 389 |
|
| 390 |
// header junk |
| 391 |
$this->maybe_define($this->px('HEADERJUNK'), true); |
| 392 |
|
| 393 |
// wp emoji |
| 394 |
$this->maybe_define($this->px('WPEMOJI'), false); |
| 395 |
|
| 396 |
// wp embed |
| 397 |
$this->maybe_define($this->px('WPEMBED'), false); |
| 398 |
|
| 399 |
// wp feed |
| 400 |
$this->maybe_define($this->px('WPFEED'), false); |
| 401 |
|
| 402 |
// wp lazyload |
| 403 |
$this->maybe_define($this->px('WPLAZYLOAD'), false); |
| 404 |
|
| 405 |
// wp sitemap |
| 406 |
$this->maybe_define($this->px('WPSITEMAP'), false); |
| 407 |
|
| 408 |
// wp sitemap |
| 409 |
$this->maybe_define($this->px('WPDASHBOARDNEWS'), false); |
| 410 |
|
| 411 |
// wp application password: wp >= 5.6 |
| 412 |
$this->maybe_define($this->px('WPAPPPASSWORD'), false); |
| 413 |
|
| 414 |
// limit http request from uncommon page. |
| 415 |
$this->maybe_define($this->px('LIMITHTTPREQUEST'), false); |
| 416 |
|
| 417 |
// whitelist host from limit http request. |
| 418 |
$this->maybe_define($this->px('LIMITHTTPREQUEST_WHITELIST'), []/* ['feeds.feedburner.com'] */); |
| 419 |
|
| 420 |
// curl "Expect" header performance tweak |
| 421 |
$this->maybe_define($this->px('HTTPHEADERSEXPECT'), false); |
| 422 |
|
| 423 |
// wp browse-happy |
| 424 |
$this->maybe_define($this->px('WPBROWSEHAPPY'), false); |
| 425 |
|
| 426 |
// wp serve-happy |
| 427 |
$this->maybe_define($this->px('WPSERVEHAPPY'), false); |
| 428 |
|
| 429 |
// cache http response from wp_remote_request. |
| 430 |
$this->maybe_define($this->px('CACHEHTTPRESPONSE'), false); |
| 431 |
|
| 432 |
// cache http response ttl: 300 = 5 minutes. |
| 433 |
$this->maybe_define($this->px('CACHEHTTPRESPONS_TTL'), 300); |
| 434 |
|
| 435 |
// cache http include list, if empty any url will include. |
| 436 |
$this->maybe_define($this->px('CACHEHTTPRESPONS_INCLUDE'), []); |
| 437 |
|
| 438 |
// cache http exclude list. |
| 439 |
$this->maybe_define($this->px('CACHEHTTPRESPONS_EXCLUDE'), []); |
| 440 |
|
| 441 |
// @private: auto save interval. |
| 442 |
$this->maybe_define($this->px('RTPOSTAUTOSAVE'), 1); |
| 443 |
|
| 444 |
// @private: post revision. |
| 445 |
$this->maybe_define($this->px('RTPOSTREVISION'), 'on'); |
| 446 |
|
| 447 |
// @private: empty trash. |
| 448 |
$this->maybe_define($this->px('RTPOSTEMPTYTRASH'), 30); |
| 449 |
|
| 450 |
// @private: plugin / theme editor. |
| 451 |
$this->maybe_define($this->px('RTPLUGINTHEMEEDITOR'), \defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ? 'on' : 'off'); |
| 452 |
|
| 453 |
// @private: plugin / theme install. |
| 454 |
$this->maybe_define($this->px('RTPLUGINTHEMEINSTALL'), \defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ? 'on' : 'off'); |
| 455 |
|
| 456 |
// @private: overwrite image after edit. |
| 457 |
$this->maybe_define($this->px('RTIMAGEOVERWRITE'), \defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ? 'on' : 'off'); |
| 458 |
|
| 459 |
// @private: wp debug. |
| 460 |
$this->maybe_define($this->px('RTWPDEBUG'), \defined('WP_DEBUG') && WP_DEBUG ? 'on' : 'off'); |
| 461 |
|
| 462 |
// @private: wp debug display. |
| 463 |
$this->maybe_define($this->px('RTWPDEBUGDISPLAY'), \defined('WP_DEBUG_DISPLAY') && WP_DEBUG_DISPLAY ? 'on' : 'off'); |
| 464 |
|
| 465 |
// @private: wp debug log. |
| 466 |
$this->maybe_define($this->px('RTWPDEBUGLOG'), \defined('WP_DEBUG_LOG') && WP_DEBUG_LOG ? 'on' : 'off'); |
| 467 |
|
| 468 |
// @private: deactivate wp auto update core. |
| 469 |
$this->maybe_define($this->px('RTWPCOREUPDATE'), \defined('WP_AUTO_UPDATE_CORE') && WP_AUTO_UPDATE_CORE ? 'off' : 'on'); |
| 470 |
|
| 471 |
// @private |
| 472 |
// capture fatal error rarely incase non-throwable |
| 473 |
// set true for debugging only |
| 474 |
$this->maybe_define($this->px('CAPTURE_FATALERROR'), false); |
| 475 |
} |
| 476 |
} |
| 477 |
|