Skip to content

Commit c026df2

Browse files
author
Hai Zheng
committed
Fixed frequent sync_conf() call when having WooCommerce installed.
1 parent 10f1644 commit c026df2

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

src/cdn/quic.cls.php

+19-33
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Quic extends Base
2323
const TYPE_REG = 'reg';
2424

2525
protected $_summary;
26+
private $_force = false;
2627
public function __construct()
2728
{
2829
$this->_summary = self::get_summary();
@@ -33,54 +34,35 @@ public function __construct()
3334
*
3435
* @access public
3536
*/
36-
public static function try_sync_config()
37-
{
38-
self::cls()->try_sync_conf();
39-
}
40-
4137
public function try_sync_conf($force = false)
4238
{
43-
$options = $this->get_options();
39+
if ($force) {
40+
$this->_force = $force;
41+
}
4442

45-
if (!$options[self::O_CDN_QUIC]) {
43+
if (!$this->conf(self::O_CDN_QUIC)) {
4644
if (!empty($this->_summary['conf_md5'])) {
45+
self::debug('❌ No QC CDN, clear conf md5!');
4746
self::save_summary(array('conf_md5' => ''));
4847
}
4948
return false;
5049
}
5150

52-
// Security: Remove cf key in report
53-
$secure_fields = array(self::O_CDN_CLOUDFLARE_KEY, self::O_OBJECT_PSWD);
54-
foreach ($secure_fields as $v) {
55-
if (!empty($options[$v])) {
56-
$options[$v] = str_repeat('*', strlen($options[$v]));
57-
}
51+
// Notice: Sync conf must be after `wp_loaded` hook, to get 3rd party vary injected (e.g. `woocommerce_cart_hash`).
52+
if (!did_action('wp_loaded')) {
53+
add_action('wp_loaded', array($this, 'try_sync_conf'), 999);
54+
self::debug('WP not loaded yet, delay sync to wp_loaded:999');
55+
return;
5856
}
59-
unset($options[self::O_MEDIA_LQIP_EXC]);
60-
unset($options[self::O_API_KEY]);
61-
unset($options[self::_VER]);
62-
63-
// Remove overflow multi lines fields
64-
foreach ($options as $k => $v) {
65-
if (is_array($v) && count($v) > 30) {
66-
$v = array_slice($v, 0, 30);
67-
$options[$k] = $v;
68-
}
69-
}
70-
71-
// Rest url
72-
$options['_rest'] = function_exists('rest_get_url_prefix') ? rest_get_url_prefix() : apply_filters('rest_url_prefix', 'wp-json');
73-
$options['_home_url'] = home_url('/');
7457

75-
// Append hooks
58+
$options = $this->get_options();
7659
$options['_tp_cookies'] = apply_filters('litespeed_vary_cookies', array());
7760

7861
// Build necessary options only
7962
$options_needed = array(
8063
self::O_CACHE_DROP_QS,
8164
self::O_CACHE_EXC_COOKIES,
8265
self::O_CACHE_EXC_USERAGENTS,
83-
self::O_CACHE_FAVICON,
8466
self::O_CACHE_LOGIN_COOKIE,
8567
self::O_CACHE_VARY_COOKIES,
8668
self::O_CACHE_MOBILE_RULES,
@@ -90,16 +72,20 @@ public function try_sync_conf($force = false)
9072
self::O_CACHE_TTL_BROWSER,
9173
self::O_IMG_OPTM_WEBP,
9274
self::O_GUEST,
93-
self::O_GUEST_OPTM,
9475
'_tp_cookies',
9576
);
9677
$consts_needed = array('WP_CONTENT_DIR', 'LSCWP_CONTENT_DIR', 'LSCWP_CONTENT_FOLDER', 'LSWCP_TAG_PREFIX');
9778
$options_for_md5 = array();
9879
foreach ($options_needed as $v) {
9980
if (isset($options[$v])) {
10081
$options_for_md5[$v] = $options[$v];
82+
// Remove overflow multi lines fields
83+
if (is_array($options_for_md5[$v]) && count($options_for_md5[$v]) > 30) {
84+
$options_for_md5[$v] = array_slice($options_for_md5[$v], 0, 30);
85+
}
10186
}
10287
}
88+
10389
$server_vars = $this->server_vars();
10490
foreach ($consts_needed as $v) {
10591
if (isset($server_vars[$v])) {
@@ -113,7 +99,7 @@ public function try_sync_conf($force = false)
11399
$conf_md5 = md5(\json_encode($options_for_md5));
114100
if (!empty($this->_summary['conf_md5'])) {
115101
if ($conf_md5 == $this->_summary['conf_md5']) {
116-
if (!$force) {
102+
if (!$this->_force) {
117103
self::debug('Bypass sync conf to QC due to same md5', $conf_md5);
118104
return;
119105
}
@@ -124,7 +110,7 @@ public function try_sync_conf($force = false)
124110
}
125111

126112
self::save_summary(array('conf_md5' => $conf_md5));
127-
self::debug('sync conf to QC', $options_for_md5);
113+
self::debug('sync conf to QC');
128114

129115
Cloud::post(Cloud::SVC_D_SYNC_CONF, $options_for_md5);
130116
}

0 commit comments

Comments
 (0)