-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWcpUpdaterUtils.php
103 lines (93 loc) · 4.91 KB
/
WcpUpdaterUtils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace WebSharks\CometCache\Pro\Traits\Plugin;
use WebSharks\CometCache\Pro\Classes;
trait WcpUpdaterUtils
{
/**
* Automatically clears all cache files for current blog when WordPress core, or an active component, is upgraded.
*
* @since 150422 Rewrite.
*
* @attaches-to `upgrader_process_complete` hook.
*
* @param \WP_Upgrader $upgrader_instance An instance of \WP_Upgrader.
* Or, any class that extends \WP_Upgrader.
* @param array $data Array of bulk item update data.
*
* This array may include one or more of the following keys:
*
* - `string` `$action` Type of action. Default 'update'.
* - `string` `$type` Type of update process; e.g. 'plugin', 'theme', 'core'.
* - `boolean` `$bulk` Whether the update process is a bulk update. Default true.
* - `array` `$packages` Array of plugin, theme, or core packages to update.
*/
public function autoClearOnUpgraderProcessComplete(\WP_Upgrader $upgrader_instance, array $data)
{
$counter = 0; // Initialize.
switch (!empty($data['type']) ? $data['type'] : '') {
case 'plugin': // Plugin upgrade.
$multi_plugin_update = $single_plugin_update = false;
$upgrading_active_plugin = false; // Initialize.
if (!empty($data['bulk']) && !empty($data['plugins']) && is_array($data['plugins'])) {
$multi_plugin_update = true;
} elseif (!empty($data['plugin']) && is_string($data['plugin'])) {
$single_plugin_update = true;
}
if ($multi_plugin_update) {
foreach ($data['plugins'] as $_plugin) {
if ($_plugin && is_string($_plugin) && is_plugin_active($_plugin)) {
$upgrading_active_plugin = true;
break; // Got what we need here.
}
}
unset($_plugin); // Housekeeping.
} elseif ($single_plugin_update && is_plugin_active($data['plugin'])) {
$upgrading_active_plugin = true;
}
if ($upgrading_active_plugin) {
$counter += $this->autoClearCache();
}
break; // Break switch.
case 'theme': // Theme upgrade.
$current_active_theme = wp_get_theme();
$current_active_theme_parent = $current_active_theme->parent();
$multi_theme_update = $single_theme_update = false;
$upgrading_active_parent_theme = $upgrading_active_theme = false;
if (!empty($data['bulk']) && !empty($data['themes']) && is_array($data['themes'])) {
$multi_theme_update = true;
} elseif (!empty($data['theme']) && is_string($data['theme'])) {
$single_theme_update = true;
}
if ($multi_theme_update) {
foreach ($data['themes'] as $_theme) {
if (!$_theme || !is_string($_theme) || !($_theme_obj = wp_get_theme($_theme))) {
continue; // Unable to acquire theme object instance.
}
if ($current_active_theme_parent && $current_active_theme_parent->get_stylesheet() === $_theme_obj->get_stylesheet()) {
$upgrading_active_parent_theme = true;
break; // Got what we needed here.
} elseif ($current_active_theme->get_stylesheet() === $_theme_obj->get_stylesheet()) {
$upgrading_active_theme = true;
break; // Got what we needed here.
}
}
unset($_theme, $_theme_obj); // Housekeeping.
} elseif ($single_theme_update && ($_theme_obj = wp_get_theme($data['theme']))) {
if ($current_active_theme_parent && $current_active_theme_parent->get_stylesheet() === $_theme_obj->get_stylesheet()) {
$upgrading_active_parent_theme = true;
} elseif ($current_active_theme->get_stylesheet() === $_theme_obj->get_stylesheet()) {
$upgrading_active_theme = true;
}
}
unset($_theme_obj); // Housekeeping.
if ($upgrading_active_theme || $upgrading_active_parent_theme) {
$counter += $this->autoClearCache();
}
break; // Break switch.
case 'core': // Core upgrade.
default: // Or any other sort of upgrade.
$counter += $this->autoClearCache();
break; // Break switch.
}
}
}