@@ -1,33 +1,33 @@ |
||
1 | 1 | <?php |
2 | 2 | if ( !class_exists('Puc_v4p4_DebugBar_PluginExtension', false) ): |
3 | 3 | |
4 | - class Puc_v4p4_DebugBar_PluginExtension extends Puc_v4p4_DebugBar_Extension { |
|
5 | - /** @var Puc_v4p4_Plugin_UpdateChecker */ |
|
6 | - protected $updateChecker; |
|
4 | + class Puc_v4p4_DebugBar_PluginExtension extends Puc_v4p4_DebugBar_Extension { |
|
5 | + /** @var Puc_v4p4_Plugin_UpdateChecker */ |
|
6 | + protected $updateChecker; |
|
7 | 7 | |
8 | - public function __construct($updateChecker) { |
|
9 | - parent::__construct($updateChecker, 'Puc_v4p4_DebugBar_PluginPanel'); |
|
8 | + public function __construct($updateChecker) { |
|
9 | + parent::__construct($updateChecker, 'Puc_v4p4_DebugBar_PluginPanel'); |
|
10 | 10 | |
11 | - add_action('wp_ajax_puc_v4_debug_request_info', array($this, 'ajaxRequestInfo')); |
|
12 | - } |
|
11 | + add_action('wp_ajax_puc_v4_debug_request_info', array($this, 'ajaxRequestInfo')); |
|
12 | + } |
|
13 | 13 | |
14 | - /** |
|
15 | - * Request plugin info and output it. |
|
16 | - */ |
|
17 | - public function ajaxRequestInfo() { |
|
18 | - if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) { |
|
19 | - return; |
|
20 | - } |
|
21 | - $this->preAjaxRequest(); |
|
22 | - $info = $this->updateChecker->requestInfo(); |
|
23 | - if ( $info !== null ) { |
|
24 | - echo 'Successfully retrieved plugin info from the metadata URL:'; |
|
25 | - echo '<pre>', htmlentities(print_r($info, true)), '</pre>'; |
|
26 | - } else { |
|
27 | - echo 'Failed to retrieve plugin info from the metadata URL.'; |
|
28 | - } |
|
29 | - exit; |
|
30 | - } |
|
31 | - } |
|
14 | + /** |
|
15 | + * Request plugin info and output it. |
|
16 | + */ |
|
17 | + public function ajaxRequestInfo() { |
|
18 | + if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) { |
|
19 | + return; |
|
20 | + } |
|
21 | + $this->preAjaxRequest(); |
|
22 | + $info = $this->updateChecker->requestInfo(); |
|
23 | + if ( $info !== null ) { |
|
24 | + echo 'Successfully retrieved plugin info from the metadata URL:'; |
|
25 | + echo '<pre>', htmlentities(print_r($info, true)), '</pre>'; |
|
26 | + } else { |
|
27 | + echo 'Failed to retrieve plugin info from the metadata URL.'; |
|
28 | + } |
|
29 | + exit; |
|
30 | + } |
|
31 | + } |
|
32 | 32 | |
33 | 33 | endif; |
34 | 34 | \ No newline at end of file |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( !class_exists('Puc_v4p4_DebugBar_PluginExtension', false) ): |
|
2 | +if (!class_exists('Puc_v4p4_DebugBar_PluginExtension', false)): |
|
3 | 3 | |
4 | 4 | class Puc_v4p4_DebugBar_PluginExtension extends Puc_v4p4_DebugBar_Extension { |
5 | 5 | /** @var Puc_v4p4_Plugin_UpdateChecker */ |
@@ -15,12 +15,12 @@ discard block |
||
15 | 15 | * Request plugin info and output it. |
16 | 16 | */ |
17 | 17 | public function ajaxRequestInfo() { |
18 | - if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) { |
|
18 | + if ($_POST['uid'] !== $this->updateChecker->getUniqueName('uid')) { |
|
19 | 19 | return; |
20 | 20 | } |
21 | 21 | $this->preAjaxRequest(); |
22 | 22 | $info = $this->updateChecker->requestInfo(); |
23 | - if ( $info !== null ) { |
|
23 | + if ($info !== null) { |
|
24 | 24 | echo 'Successfully retrieved plugin info from the metadata URL:'; |
25 | 25 | echo '<pre>', htmlentities(print_r($info, true)), '</pre>'; |
26 | 26 | } else { |
@@ -2,37 +2,37 @@ |
||
2 | 2 | |
3 | 3 | if ( !class_exists('Puc_v4p4_DebugBar_PluginPanel', false) ): |
4 | 4 | |
5 | - class Puc_v4p4_DebugBar_PluginPanel extends Puc_v4p4_DebugBar_Panel { |
|
6 | - /** |
|
7 | - * @var Puc_v4p4_Plugin_UpdateChecker |
|
8 | - */ |
|
9 | - protected $updateChecker; |
|
5 | + class Puc_v4p4_DebugBar_PluginPanel extends Puc_v4p4_DebugBar_Panel { |
|
6 | + /** |
|
7 | + * @var Puc_v4p4_Plugin_UpdateChecker |
|
8 | + */ |
|
9 | + protected $updateChecker; |
|
10 | 10 | |
11 | - protected function displayConfigHeader() { |
|
12 | - $this->row('Plugin file', htmlentities($this->updateChecker->pluginFile)); |
|
13 | - parent::displayConfigHeader(); |
|
14 | - } |
|
11 | + protected function displayConfigHeader() { |
|
12 | + $this->row('Plugin file', htmlentities($this->updateChecker->pluginFile)); |
|
13 | + parent::displayConfigHeader(); |
|
14 | + } |
|
15 | 15 | |
16 | - protected function getMetadataButton() { |
|
17 | - $requestInfoButton = ''; |
|
18 | - if ( function_exists('get_submit_button') ) { |
|
19 | - $requestInfoButton = get_submit_button( |
|
20 | - 'Request Info', |
|
21 | - 'secondary', |
|
22 | - 'puc-request-info-button', |
|
23 | - false, |
|
24 | - array('id' => $this->updateChecker->getUniqueName('request-info-button')) |
|
25 | - ); |
|
26 | - } |
|
27 | - return $requestInfoButton; |
|
28 | - } |
|
16 | + protected function getMetadataButton() { |
|
17 | + $requestInfoButton = ''; |
|
18 | + if ( function_exists('get_submit_button') ) { |
|
19 | + $requestInfoButton = get_submit_button( |
|
20 | + 'Request Info', |
|
21 | + 'secondary', |
|
22 | + 'puc-request-info-button', |
|
23 | + false, |
|
24 | + array('id' => $this->updateChecker->getUniqueName('request-info-button')) |
|
25 | + ); |
|
26 | + } |
|
27 | + return $requestInfoButton; |
|
28 | + } |
|
29 | 29 | |
30 | - protected function getUpdateFields() { |
|
31 | - return array_merge( |
|
32 | - parent::getUpdateFields(), |
|
33 | - array('homepage', 'upgrade_notice', 'tested',) |
|
34 | - ); |
|
35 | - } |
|
36 | - } |
|
30 | + protected function getUpdateFields() { |
|
31 | + return array_merge( |
|
32 | + parent::getUpdateFields(), |
|
33 | + array('homepage', 'upgrade_notice', 'tested',) |
|
34 | + ); |
|
35 | + } |
|
36 | + } |
|
37 | 37 | |
38 | 38 | endif; |
39 | 39 | \ No newline at end of file |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if ( !class_exists('Puc_v4p4_DebugBar_PluginPanel', false) ): |
|
3 | +if (!class_exists('Puc_v4p4_DebugBar_PluginPanel', false)): |
|
4 | 4 | |
5 | 5 | class Puc_v4p4_DebugBar_PluginPanel extends Puc_v4p4_DebugBar_Panel { |
6 | 6 | /** |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | |
16 | 16 | protected function getMetadataButton() { |
17 | 17 | $requestInfoButton = ''; |
18 | - if ( function_exists('get_submit_button') ) { |
|
18 | + if (function_exists('get_submit_button')) { |
|
19 | 19 | $requestInfoButton = get_submit_button( |
20 | 20 | 'Request Info', |
21 | 21 | 'secondary', |
@@ -1,177 +1,177 @@ |
||
1 | 1 | <?php |
2 | 2 | if ( !class_exists('Puc_v4p4_Scheduler', false) ): |
3 | 3 | |
4 | - /** |
|
5 | - * The scheduler decides when and how often to check for updates. |
|
6 | - * It calls @see Puc_v4p4_UpdateChecker::checkForUpdates() to perform the actual checks. |
|
7 | - */ |
|
8 | - class Puc_v4p4_Scheduler { |
|
9 | - public $checkPeriod = 12; //How often to check for updates (in hours). |
|
10 | - public $throttleRedundantChecks = false; //Check less often if we already know that an update is available. |
|
11 | - public $throttledCheckPeriod = 72; |
|
12 | - |
|
13 | - protected $hourlyCheckHooks = array('load-update.php'); |
|
14 | - |
|
15 | - /** |
|
16 | - * @var Puc_v4p4_UpdateChecker |
|
17 | - */ |
|
18 | - protected $updateChecker; |
|
19 | - |
|
20 | - private $cronHook = null; |
|
21 | - |
|
22 | - /** |
|
23 | - * Scheduler constructor. |
|
24 | - * |
|
25 | - * @param Puc_v4p4_UpdateChecker $updateChecker |
|
26 | - * @param int $checkPeriod How often to check for updates (in hours). |
|
27 | - * @param array $hourlyHooks |
|
28 | - */ |
|
29 | - public function __construct($updateChecker, $checkPeriod, $hourlyHooks = array('load-plugins.php')) { |
|
30 | - $this->updateChecker = $updateChecker; |
|
31 | - $this->checkPeriod = $checkPeriod; |
|
32 | - |
|
33 | - //Set up the periodic update checks |
|
34 | - $this->cronHook = $this->updateChecker->getUniqueName('cron_check_updates'); |
|
35 | - if ( $this->checkPeriod > 0 ){ |
|
36 | - |
|
37 | - //Trigger the check via Cron. |
|
38 | - //Try to use one of the default schedules if possible as it's less likely to conflict |
|
39 | - //with other plugins and their custom schedules. |
|
40 | - $defaultSchedules = array( |
|
41 | - 1 => 'hourly', |
|
42 | - 12 => 'twicedaily', |
|
43 | - 24 => 'daily', |
|
44 | - ); |
|
45 | - if ( array_key_exists($this->checkPeriod, $defaultSchedules) ) { |
|
46 | - $scheduleName = $defaultSchedules[$this->checkPeriod]; |
|
47 | - } else { |
|
48 | - //Use a custom cron schedule. |
|
49 | - $scheduleName = 'every' . $this->checkPeriod . 'hours'; |
|
50 | - add_filter('cron_schedules', array($this, '_addCustomSchedule')); |
|
51 | - } |
|
52 | - |
|
53 | - if ( !wp_next_scheduled($this->cronHook) && !defined('WP_INSTALLING') ) { |
|
54 | - wp_schedule_event(time(), $scheduleName, $this->cronHook); |
|
55 | - } |
|
56 | - add_action($this->cronHook, array($this, 'maybeCheckForUpdates')); |
|
57 | - |
|
58 | - //In case Cron is disabled or unreliable, we also manually trigger |
|
59 | - //the periodic checks while the user is browsing the Dashboard. |
|
60 | - add_action( 'admin_init', array($this, 'maybeCheckForUpdates') ); |
|
61 | - |
|
62 | - //Like WordPress itself, we check more often on certain pages. |
|
63 | - /** @see wp_update_plugins */ |
|
64 | - add_action('load-update-core.php', array($this, 'maybeCheckForUpdates')); |
|
65 | - //"load-update.php" and "load-plugins.php" or "load-themes.php". |
|
66 | - $this->hourlyCheckHooks = array_merge($this->hourlyCheckHooks, $hourlyHooks); |
|
67 | - foreach($this->hourlyCheckHooks as $hook) { |
|
68 | - add_action($hook, array($this, 'maybeCheckForUpdates')); |
|
69 | - } |
|
70 | - //This hook fires after a bulk update is complete. |
|
71 | - add_action('upgrader_process_complete', array($this, 'maybeCheckForUpdates'), 11, 0); |
|
72 | - |
|
73 | - } else { |
|
74 | - //Periodic checks are disabled. |
|
75 | - wp_clear_scheduled_hook($this->cronHook); |
|
76 | - } |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Check for updates if the configured check interval has already elapsed. |
|
81 | - * Will use a shorter check interval on certain admin pages like "Dashboard -> Updates" or when doing cron. |
|
82 | - * |
|
83 | - * You can override the default behaviour by using the "puc_check_now-$slug" filter. |
|
84 | - * The filter callback will be passed three parameters: |
|
85 | - * - Current decision. TRUE = check updates now, FALSE = don't check now. |
|
86 | - * - Last check time as a Unix timestamp. |
|
87 | - * - Configured check period in hours. |
|
88 | - * Return TRUE to check for updates immediately, or FALSE to cancel. |
|
89 | - * |
|
90 | - * This method is declared public because it's a hook callback. Calling it directly is not recommended. |
|
91 | - */ |
|
92 | - public function maybeCheckForUpdates(){ |
|
93 | - if ( empty($this->checkPeriod) ){ |
|
94 | - return; |
|
95 | - } |
|
96 | - |
|
97 | - $state = $this->updateChecker->getUpdateState(); |
|
98 | - $shouldCheck = ($state->timeSinceLastCheck() >= $this->getEffectiveCheckPeriod()); |
|
99 | - |
|
100 | - //Let plugin authors substitute their own algorithm. |
|
101 | - $shouldCheck = apply_filters( |
|
102 | - $this->updateChecker->getUniqueName('check_now'), |
|
103 | - $shouldCheck, |
|
104 | - $state->getLastCheck(), |
|
105 | - $this->checkPeriod |
|
106 | - ); |
|
107 | - |
|
108 | - if ( $shouldCheck ) { |
|
109 | - $this->updateChecker->checkForUpdates(); |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Calculate the actual check period based on the current status and environment. |
|
115 | - * |
|
116 | - * @return int Check period in seconds. |
|
117 | - */ |
|
118 | - protected function getEffectiveCheckPeriod() { |
|
119 | - $currentFilter = current_filter(); |
|
120 | - if ( in_array($currentFilter, array('load-update-core.php', 'upgrader_process_complete')) ) { |
|
121 | - //Check more often when the user visits "Dashboard -> Updates" or does a bulk update. |
|
122 | - $period = 60; |
|
123 | - } else if ( in_array($currentFilter, $this->hourlyCheckHooks) ) { |
|
124 | - //Also check more often on /wp-admin/update.php and the "Plugins" or "Themes" page. |
|
125 | - $period = 3600; |
|
126 | - } else if ( $this->throttleRedundantChecks && ($this->updateChecker->getUpdate() !== null) ) { |
|
127 | - //Check less frequently if it's already known that an update is available. |
|
128 | - $period = $this->throttledCheckPeriod * 3600; |
|
129 | - } else if ( defined('DOING_CRON') && constant('DOING_CRON') ) { |
|
130 | - //WordPress cron schedules are not exact, so lets do an update check even |
|
131 | - //if slightly less than $checkPeriod hours have elapsed since the last check. |
|
132 | - $cronFuzziness = 20 * 60; |
|
133 | - $period = $this->checkPeriod * 3600 - $cronFuzziness; |
|
134 | - } else { |
|
135 | - $period = $this->checkPeriod * 3600; |
|
136 | - } |
|
137 | - |
|
138 | - return $period; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Add our custom schedule to the array of Cron schedules used by WP. |
|
143 | - * |
|
144 | - * @param array $schedules |
|
145 | - * @return array |
|
146 | - */ |
|
147 | - public function _addCustomSchedule($schedules){ |
|
148 | - if ( $this->checkPeriod && ($this->checkPeriod > 0) ){ |
|
149 | - $scheduleName = 'every' . $this->checkPeriod . 'hours'; |
|
150 | - $schedules[$scheduleName] = array( |
|
151 | - 'interval' => $this->checkPeriod * 3600, |
|
152 | - 'display' => sprintf('Every %d hours', $this->checkPeriod), |
|
153 | - ); |
|
154 | - } |
|
155 | - return $schedules; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Remove the scheduled cron event that the library uses to check for updates. |
|
160 | - * |
|
161 | - * @return void |
|
162 | - */ |
|
163 | - public function removeUpdaterCron(){ |
|
164 | - wp_clear_scheduled_hook($this->cronHook); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Get the name of the update checker's WP-cron hook. Mostly useful for debugging. |
|
169 | - * |
|
170 | - * @return string |
|
171 | - */ |
|
172 | - public function getCronHookName() { |
|
173 | - return $this->cronHook; |
|
174 | - } |
|
175 | - } |
|
4 | + /** |
|
5 | + * The scheduler decides when and how often to check for updates. |
|
6 | + * It calls @see Puc_v4p4_UpdateChecker::checkForUpdates() to perform the actual checks. |
|
7 | + */ |
|
8 | + class Puc_v4p4_Scheduler { |
|
9 | + public $checkPeriod = 12; //How often to check for updates (in hours). |
|
10 | + public $throttleRedundantChecks = false; //Check less often if we already know that an update is available. |
|
11 | + public $throttledCheckPeriod = 72; |
|
12 | + |
|
13 | + protected $hourlyCheckHooks = array('load-update.php'); |
|
14 | + |
|
15 | + /** |
|
16 | + * @var Puc_v4p4_UpdateChecker |
|
17 | + */ |
|
18 | + protected $updateChecker; |
|
19 | + |
|
20 | + private $cronHook = null; |
|
21 | + |
|
22 | + /** |
|
23 | + * Scheduler constructor. |
|
24 | + * |
|
25 | + * @param Puc_v4p4_UpdateChecker $updateChecker |
|
26 | + * @param int $checkPeriod How often to check for updates (in hours). |
|
27 | + * @param array $hourlyHooks |
|
28 | + */ |
|
29 | + public function __construct($updateChecker, $checkPeriod, $hourlyHooks = array('load-plugins.php')) { |
|
30 | + $this->updateChecker = $updateChecker; |
|
31 | + $this->checkPeriod = $checkPeriod; |
|
32 | + |
|
33 | + //Set up the periodic update checks |
|
34 | + $this->cronHook = $this->updateChecker->getUniqueName('cron_check_updates'); |
|
35 | + if ( $this->checkPeriod > 0 ){ |
|
36 | + |
|
37 | + //Trigger the check via Cron. |
|
38 | + //Try to use one of the default schedules if possible as it's less likely to conflict |
|
39 | + //with other plugins and their custom schedules. |
|
40 | + $defaultSchedules = array( |
|
41 | + 1 => 'hourly', |
|
42 | + 12 => 'twicedaily', |
|
43 | + 24 => 'daily', |
|
44 | + ); |
|
45 | + if ( array_key_exists($this->checkPeriod, $defaultSchedules) ) { |
|
46 | + $scheduleName = $defaultSchedules[$this->checkPeriod]; |
|
47 | + } else { |
|
48 | + //Use a custom cron schedule. |
|
49 | + $scheduleName = 'every' . $this->checkPeriod . 'hours'; |
|
50 | + add_filter('cron_schedules', array($this, '_addCustomSchedule')); |
|
51 | + } |
|
52 | + |
|
53 | + if ( !wp_next_scheduled($this->cronHook) && !defined('WP_INSTALLING') ) { |
|
54 | + wp_schedule_event(time(), $scheduleName, $this->cronHook); |
|
55 | + } |
|
56 | + add_action($this->cronHook, array($this, 'maybeCheckForUpdates')); |
|
57 | + |
|
58 | + //In case Cron is disabled or unreliable, we also manually trigger |
|
59 | + //the periodic checks while the user is browsing the Dashboard. |
|
60 | + add_action( 'admin_init', array($this, 'maybeCheckForUpdates') ); |
|
61 | + |
|
62 | + //Like WordPress itself, we check more often on certain pages. |
|
63 | + /** @see wp_update_plugins */ |
|
64 | + add_action('load-update-core.php', array($this, 'maybeCheckForUpdates')); |
|
65 | + //"load-update.php" and "load-plugins.php" or "load-themes.php". |
|
66 | + $this->hourlyCheckHooks = array_merge($this->hourlyCheckHooks, $hourlyHooks); |
|
67 | + foreach($this->hourlyCheckHooks as $hook) { |
|
68 | + add_action($hook, array($this, 'maybeCheckForUpdates')); |
|
69 | + } |
|
70 | + //This hook fires after a bulk update is complete. |
|
71 | + add_action('upgrader_process_complete', array($this, 'maybeCheckForUpdates'), 11, 0); |
|
72 | + |
|
73 | + } else { |
|
74 | + //Periodic checks are disabled. |
|
75 | + wp_clear_scheduled_hook($this->cronHook); |
|
76 | + } |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Check for updates if the configured check interval has already elapsed. |
|
81 | + * Will use a shorter check interval on certain admin pages like "Dashboard -> Updates" or when doing cron. |
|
82 | + * |
|
83 | + * You can override the default behaviour by using the "puc_check_now-$slug" filter. |
|
84 | + * The filter callback will be passed three parameters: |
|
85 | + * - Current decision. TRUE = check updates now, FALSE = don't check now. |
|
86 | + * - Last check time as a Unix timestamp. |
|
87 | + * - Configured check period in hours. |
|
88 | + * Return TRUE to check for updates immediately, or FALSE to cancel. |
|
89 | + * |
|
90 | + * This method is declared public because it's a hook callback. Calling it directly is not recommended. |
|
91 | + */ |
|
92 | + public function maybeCheckForUpdates(){ |
|
93 | + if ( empty($this->checkPeriod) ){ |
|
94 | + return; |
|
95 | + } |
|
96 | + |
|
97 | + $state = $this->updateChecker->getUpdateState(); |
|
98 | + $shouldCheck = ($state->timeSinceLastCheck() >= $this->getEffectiveCheckPeriod()); |
|
99 | + |
|
100 | + //Let plugin authors substitute their own algorithm. |
|
101 | + $shouldCheck = apply_filters( |
|
102 | + $this->updateChecker->getUniqueName('check_now'), |
|
103 | + $shouldCheck, |
|
104 | + $state->getLastCheck(), |
|
105 | + $this->checkPeriod |
|
106 | + ); |
|
107 | + |
|
108 | + if ( $shouldCheck ) { |
|
109 | + $this->updateChecker->checkForUpdates(); |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Calculate the actual check period based on the current status and environment. |
|
115 | + * |
|
116 | + * @return int Check period in seconds. |
|
117 | + */ |
|
118 | + protected function getEffectiveCheckPeriod() { |
|
119 | + $currentFilter = current_filter(); |
|
120 | + if ( in_array($currentFilter, array('load-update-core.php', 'upgrader_process_complete')) ) { |
|
121 | + //Check more often when the user visits "Dashboard -> Updates" or does a bulk update. |
|
122 | + $period = 60; |
|
123 | + } else if ( in_array($currentFilter, $this->hourlyCheckHooks) ) { |
|
124 | + //Also check more often on /wp-admin/update.php and the "Plugins" or "Themes" page. |
|
125 | + $period = 3600; |
|
126 | + } else if ( $this->throttleRedundantChecks && ($this->updateChecker->getUpdate() !== null) ) { |
|
127 | + //Check less frequently if it's already known that an update is available. |
|
128 | + $period = $this->throttledCheckPeriod * 3600; |
|
129 | + } else if ( defined('DOING_CRON') && constant('DOING_CRON') ) { |
|
130 | + //WordPress cron schedules are not exact, so lets do an update check even |
|
131 | + //if slightly less than $checkPeriod hours have elapsed since the last check. |
|
132 | + $cronFuzziness = 20 * 60; |
|
133 | + $period = $this->checkPeriod * 3600 - $cronFuzziness; |
|
134 | + } else { |
|
135 | + $period = $this->checkPeriod * 3600; |
|
136 | + } |
|
137 | + |
|
138 | + return $period; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Add our custom schedule to the array of Cron schedules used by WP. |
|
143 | + * |
|
144 | + * @param array $schedules |
|
145 | + * @return array |
|
146 | + */ |
|
147 | + public function _addCustomSchedule($schedules){ |
|
148 | + if ( $this->checkPeriod && ($this->checkPeriod > 0) ){ |
|
149 | + $scheduleName = 'every' . $this->checkPeriod . 'hours'; |
|
150 | + $schedules[$scheduleName] = array( |
|
151 | + 'interval' => $this->checkPeriod * 3600, |
|
152 | + 'display' => sprintf('Every %d hours', $this->checkPeriod), |
|
153 | + ); |
|
154 | + } |
|
155 | + return $schedules; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Remove the scheduled cron event that the library uses to check for updates. |
|
160 | + * |
|
161 | + * @return void |
|
162 | + */ |
|
163 | + public function removeUpdaterCron(){ |
|
164 | + wp_clear_scheduled_hook($this->cronHook); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Get the name of the update checker's WP-cron hook. Mostly useful for debugging. |
|
169 | + * |
|
170 | + * @return string |
|
171 | + */ |
|
172 | + public function getCronHookName() { |
|
173 | + return $this->cronHook; |
|
174 | + } |
|
175 | + } |
|
176 | 176 | |
177 | 177 | endif; |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( !class_exists('Puc_v4p4_Scheduler', false) ): |
|
2 | +if (!class_exists('Puc_v4p4_Scheduler', false)): |
|
3 | 3 | |
4 | 4 | /** |
5 | 5 | * The scheduler decides when and how often to check for updates. |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | |
33 | 33 | //Set up the periodic update checks |
34 | 34 | $this->cronHook = $this->updateChecker->getUniqueName('cron_check_updates'); |
35 | - if ( $this->checkPeriod > 0 ){ |
|
35 | + if ($this->checkPeriod > 0) { |
|
36 | 36 | |
37 | 37 | //Trigger the check via Cron. |
38 | 38 | //Try to use one of the default schedules if possible as it's less likely to conflict |
@@ -42,29 +42,29 @@ discard block |
||
42 | 42 | 12 => 'twicedaily', |
43 | 43 | 24 => 'daily', |
44 | 44 | ); |
45 | - if ( array_key_exists($this->checkPeriod, $defaultSchedules) ) { |
|
45 | + if (array_key_exists($this->checkPeriod, $defaultSchedules)) { |
|
46 | 46 | $scheduleName = $defaultSchedules[$this->checkPeriod]; |
47 | 47 | } else { |
48 | 48 | //Use a custom cron schedule. |
49 | - $scheduleName = 'every' . $this->checkPeriod . 'hours'; |
|
49 | + $scheduleName = 'every'.$this->checkPeriod.'hours'; |
|
50 | 50 | add_filter('cron_schedules', array($this, '_addCustomSchedule')); |
51 | 51 | } |
52 | 52 | |
53 | - if ( !wp_next_scheduled($this->cronHook) && !defined('WP_INSTALLING') ) { |
|
53 | + if (!wp_next_scheduled($this->cronHook) && !defined('WP_INSTALLING')) { |
|
54 | 54 | wp_schedule_event(time(), $scheduleName, $this->cronHook); |
55 | 55 | } |
56 | 56 | add_action($this->cronHook, array($this, 'maybeCheckForUpdates')); |
57 | 57 | |
58 | 58 | //In case Cron is disabled or unreliable, we also manually trigger |
59 | 59 | //the periodic checks while the user is browsing the Dashboard. |
60 | - add_action( 'admin_init', array($this, 'maybeCheckForUpdates') ); |
|
60 | + add_action('admin_init', array($this, 'maybeCheckForUpdates')); |
|
61 | 61 | |
62 | 62 | //Like WordPress itself, we check more often on certain pages. |
63 | 63 | /** @see wp_update_plugins */ |
64 | 64 | add_action('load-update-core.php', array($this, 'maybeCheckForUpdates')); |
65 | 65 | //"load-update.php" and "load-plugins.php" or "load-themes.php". |
66 | 66 | $this->hourlyCheckHooks = array_merge($this->hourlyCheckHooks, $hourlyHooks); |
67 | - foreach($this->hourlyCheckHooks as $hook) { |
|
67 | + foreach ($this->hourlyCheckHooks as $hook) { |
|
68 | 68 | add_action($hook, array($this, 'maybeCheckForUpdates')); |
69 | 69 | } |
70 | 70 | //This hook fires after a bulk update is complete. |
@@ -89,8 +89,8 @@ discard block |
||
89 | 89 | * |
90 | 90 | * This method is declared public because it's a hook callback. Calling it directly is not recommended. |
91 | 91 | */ |
92 | - public function maybeCheckForUpdates(){ |
|
93 | - if ( empty($this->checkPeriod) ){ |
|
92 | + public function maybeCheckForUpdates() { |
|
93 | + if (empty($this->checkPeriod)) { |
|
94 | 94 | return; |
95 | 95 | } |
96 | 96 | |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | $this->checkPeriod |
106 | 106 | ); |
107 | 107 | |
108 | - if ( $shouldCheck ) { |
|
108 | + if ($shouldCheck) { |
|
109 | 109 | $this->updateChecker->checkForUpdates(); |
110 | 110 | } |
111 | 111 | } |
@@ -117,22 +117,22 @@ discard block |
||
117 | 117 | */ |
118 | 118 | protected function getEffectiveCheckPeriod() { |
119 | 119 | $currentFilter = current_filter(); |
120 | - if ( in_array($currentFilter, array('load-update-core.php', 'upgrader_process_complete')) ) { |
|
120 | + if (in_array($currentFilter, array('load-update-core.php', 'upgrader_process_complete'))) { |
|
121 | 121 | //Check more often when the user visits "Dashboard -> Updates" or does a bulk update. |
122 | 122 | $period = 60; |
123 | - } else if ( in_array($currentFilter, $this->hourlyCheckHooks) ) { |
|
123 | + } else if (in_array($currentFilter, $this->hourlyCheckHooks)) { |
|
124 | 124 | //Also check more often on /wp-admin/update.php and the "Plugins" or "Themes" page. |
125 | 125 | $period = 3600; |
126 | - } else if ( $this->throttleRedundantChecks && ($this->updateChecker->getUpdate() !== null) ) { |
|
126 | + } else if ($this->throttleRedundantChecks && ($this->updateChecker->getUpdate() !== null)) { |
|
127 | 127 | //Check less frequently if it's already known that an update is available. |
128 | - $period = $this->throttledCheckPeriod * 3600; |
|
129 | - } else if ( defined('DOING_CRON') && constant('DOING_CRON') ) { |
|
128 | + $period = $this->throttledCheckPeriod*3600; |
|
129 | + } else if (defined('DOING_CRON') && constant('DOING_CRON')) { |
|
130 | 130 | //WordPress cron schedules are not exact, so lets do an update check even |
131 | 131 | //if slightly less than $checkPeriod hours have elapsed since the last check. |
132 | - $cronFuzziness = 20 * 60; |
|
133 | - $period = $this->checkPeriod * 3600 - $cronFuzziness; |
|
132 | + $cronFuzziness = 20*60; |
|
133 | + $period = $this->checkPeriod*3600 - $cronFuzziness; |
|
134 | 134 | } else { |
135 | - $period = $this->checkPeriod * 3600; |
|
135 | + $period = $this->checkPeriod*3600; |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | return $period; |
@@ -144,11 +144,11 @@ discard block |
||
144 | 144 | * @param array $schedules |
145 | 145 | * @return array |
146 | 146 | */ |
147 | - public function _addCustomSchedule($schedules){ |
|
148 | - if ( $this->checkPeriod && ($this->checkPeriod > 0) ){ |
|
149 | - $scheduleName = 'every' . $this->checkPeriod . 'hours'; |
|
147 | + public function _addCustomSchedule($schedules) { |
|
148 | + if ($this->checkPeriod && ($this->checkPeriod > 0)) { |
|
149 | + $scheduleName = 'every'.$this->checkPeriod.'hours'; |
|
150 | 150 | $schedules[$scheduleName] = array( |
151 | - 'interval' => $this->checkPeriod * 3600, |
|
151 | + 'interval' => $this->checkPeriod*3600, |
|
152 | 152 | 'display' => sprintf('Every %d hours', $this->checkPeriod), |
153 | 153 | ); |
154 | 154 | } |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | * |
161 | 161 | * @return void |
162 | 162 | */ |
163 | - public function removeUpdaterCron(){ |
|
163 | + public function removeUpdaterCron() { |
|
164 | 164 | wp_clear_scheduled_hook($this->cronHook); |
165 | 165 | } |
166 | 166 |
@@ -2,83 +2,83 @@ |
||
2 | 2 | |
3 | 3 | if ( !class_exists('Puc_v4p4_Theme_Update', false) ): |
4 | 4 | |
5 | - class Puc_v4p4_Theme_Update extends Puc_v4p4_Update { |
|
6 | - public $details_url = ''; |
|
5 | + class Puc_v4p4_Theme_Update extends Puc_v4p4_Update { |
|
6 | + public $details_url = ''; |
|
7 | 7 | |
8 | - protected static $extraFields = array('details_url'); |
|
8 | + protected static $extraFields = array('details_url'); |
|
9 | 9 | |
10 | - /** |
|
11 | - * Transform the metadata into the format used by WordPress core. |
|
12 | - * Note the inconsistency: WP stores plugin updates as objects and theme updates as arrays. |
|
13 | - * |
|
14 | - * @return array |
|
15 | - */ |
|
16 | - public function toWpFormat() { |
|
17 | - $update = array( |
|
18 | - 'theme' => $this->slug, |
|
19 | - 'new_version' => $this->version, |
|
20 | - 'url' => $this->details_url, |
|
21 | - ); |
|
10 | + /** |
|
11 | + * Transform the metadata into the format used by WordPress core. |
|
12 | + * Note the inconsistency: WP stores plugin updates as objects and theme updates as arrays. |
|
13 | + * |
|
14 | + * @return array |
|
15 | + */ |
|
16 | + public function toWpFormat() { |
|
17 | + $update = array( |
|
18 | + 'theme' => $this->slug, |
|
19 | + 'new_version' => $this->version, |
|
20 | + 'url' => $this->details_url, |
|
21 | + ); |
|
22 | 22 | |
23 | - if ( !empty($this->download_url) ) { |
|
24 | - $update['package'] = $this->download_url; |
|
25 | - } |
|
23 | + if ( !empty($this->download_url) ) { |
|
24 | + $update['package'] = $this->download_url; |
|
25 | + } |
|
26 | 26 | |
27 | - return $update; |
|
28 | - } |
|
27 | + return $update; |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Create a new instance of Theme_Update from its JSON-encoded representation. |
|
32 | - * |
|
33 | - * @param string $json Valid JSON string representing a theme information object. |
|
34 | - * @return self New instance of ThemeUpdate, or NULL on error. |
|
35 | - */ |
|
36 | - public static function fromJson($json) { |
|
37 | - $instance = new self(); |
|
38 | - if ( !parent::createFromJson($json, $instance) ) { |
|
39 | - return null; |
|
40 | - } |
|
41 | - return $instance; |
|
42 | - } |
|
30 | + /** |
|
31 | + * Create a new instance of Theme_Update from its JSON-encoded representation. |
|
32 | + * |
|
33 | + * @param string $json Valid JSON string representing a theme information object. |
|
34 | + * @return self New instance of ThemeUpdate, or NULL on error. |
|
35 | + */ |
|
36 | + public static function fromJson($json) { |
|
37 | + $instance = new self(); |
|
38 | + if ( !parent::createFromJson($json, $instance) ) { |
|
39 | + return null; |
|
40 | + } |
|
41 | + return $instance; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Create a new instance by copying the necessary fields from another object. |
|
46 | - * |
|
47 | - * @param StdClass|Puc_v4p4_Theme_Update $object The source object. |
|
48 | - * @return Puc_v4p4_Theme_Update The new copy. |
|
49 | - */ |
|
50 | - public static function fromObject($object) { |
|
51 | - $update = new self(); |
|
52 | - $update->copyFields($object, $update); |
|
53 | - return $update; |
|
54 | - } |
|
44 | + /** |
|
45 | + * Create a new instance by copying the necessary fields from another object. |
|
46 | + * |
|
47 | + * @param StdClass|Puc_v4p4_Theme_Update $object The source object. |
|
48 | + * @return Puc_v4p4_Theme_Update The new copy. |
|
49 | + */ |
|
50 | + public static function fromObject($object) { |
|
51 | + $update = new self(); |
|
52 | + $update->copyFields($object, $update); |
|
53 | + return $update; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Basic validation. |
|
58 | - * |
|
59 | - * @param StdClass $apiResponse |
|
60 | - * @return bool|WP_Error |
|
61 | - */ |
|
62 | - protected function validateMetadata($apiResponse) { |
|
63 | - $required = array('version', 'details_url'); |
|
64 | - foreach($required as $key) { |
|
65 | - if ( !isset($apiResponse->$key) || empty($apiResponse->$key) ) { |
|
66 | - return new WP_Error( |
|
67 | - 'tuc-invalid-metadata', |
|
68 | - sprintf('The theme metadata is missing the required "%s" key.', $key) |
|
69 | - ); |
|
70 | - } |
|
71 | - } |
|
72 | - return true; |
|
73 | - } |
|
56 | + /** |
|
57 | + * Basic validation. |
|
58 | + * |
|
59 | + * @param StdClass $apiResponse |
|
60 | + * @return bool|WP_Error |
|
61 | + */ |
|
62 | + protected function validateMetadata($apiResponse) { |
|
63 | + $required = array('version', 'details_url'); |
|
64 | + foreach($required as $key) { |
|
65 | + if ( !isset($apiResponse->$key) || empty($apiResponse->$key) ) { |
|
66 | + return new WP_Error( |
|
67 | + 'tuc-invalid-metadata', |
|
68 | + sprintf('The theme metadata is missing the required "%s" key.', $key) |
|
69 | + ); |
|
70 | + } |
|
71 | + } |
|
72 | + return true; |
|
73 | + } |
|
74 | 74 | |
75 | - protected function getFieldNames() { |
|
76 | - return array_merge(parent::getFieldNames(), self::$extraFields); |
|
77 | - } |
|
75 | + protected function getFieldNames() { |
|
76 | + return array_merge(parent::getFieldNames(), self::$extraFields); |
|
77 | + } |
|
78 | 78 | |
79 | - protected function getPrefixedFilter($tag) { |
|
80 | - return parent::getPrefixedFilter($tag) . '_theme'; |
|
81 | - } |
|
82 | - } |
|
79 | + protected function getPrefixedFilter($tag) { |
|
80 | + return parent::getPrefixedFilter($tag) . '_theme'; |
|
81 | + } |
|
82 | + } |
|
83 | 83 | |
84 | 84 | endif; |
85 | 85 | \ No newline at end of file |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if ( !class_exists('Puc_v4p4_Theme_Update', false) ): |
|
3 | +if (!class_exists('Puc_v4p4_Theme_Update', false)): |
|
4 | 4 | |
5 | 5 | class Puc_v4p4_Theme_Update extends Puc_v4p4_Update { |
6 | 6 | public $details_url = ''; |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | 'url' => $this->details_url, |
21 | 21 | ); |
22 | 22 | |
23 | - if ( !empty($this->download_url) ) { |
|
23 | + if (!empty($this->download_url)) { |
|
24 | 24 | $update['package'] = $this->download_url; |
25 | 25 | } |
26 | 26 | |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | */ |
36 | 36 | public static function fromJson($json) { |
37 | 37 | $instance = new self(); |
38 | - if ( !parent::createFromJson($json, $instance) ) { |
|
38 | + if (!parent::createFromJson($json, $instance)) { |
|
39 | 39 | return null; |
40 | 40 | } |
41 | 41 | return $instance; |
@@ -61,8 +61,8 @@ discard block |
||
61 | 61 | */ |
62 | 62 | protected function validateMetadata($apiResponse) { |
63 | 63 | $required = array('version', 'details_url'); |
64 | - foreach($required as $key) { |
|
65 | - if ( !isset($apiResponse->$key) || empty($apiResponse->$key) ) { |
|
64 | + foreach ($required as $key) { |
|
65 | + if (!isset($apiResponse->$key) || empty($apiResponse->$key)) { |
|
66 | 66 | return new WP_Error( |
67 | 67 | 'tuc-invalid-metadata', |
68 | 68 | sprintf('The theme metadata is missing the required "%s" key.', $key) |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | } |
78 | 78 | |
79 | 79 | protected function getPrefixedFilter($tag) { |
80 | - return parent::getPrefixedFilter($tag) . '_theme'; |
|
80 | + return parent::getPrefixedFilter($tag).'_theme'; |
|
81 | 81 | } |
82 | 82 | } |
83 | 83 |
@@ -2,176 +2,176 @@ |
||
2 | 2 | |
3 | 3 | if ( !class_exists('Puc_v4p4_Theme_UpdateChecker', false) ): |
4 | 4 | |
5 | - class Puc_v4p4_Theme_UpdateChecker extends Puc_v4p4_UpdateChecker { |
|
6 | - protected $filterSuffix = 'theme'; |
|
7 | - protected $updateTransient = 'update_themes'; |
|
8 | - protected $translationType = 'theme'; |
|
9 | - |
|
10 | - /** |
|
11 | - * @var string Theme directory name. |
|
12 | - */ |
|
13 | - protected $stylesheet; |
|
14 | - |
|
15 | - /** |
|
16 | - * @var WP_Theme Theme object. |
|
17 | - */ |
|
18 | - protected $theme; |
|
19 | - |
|
20 | - public function __construct($metadataUrl, $stylesheet = null, $customSlug = null, $checkPeriod = 12, $optionName = '') { |
|
21 | - if ( $stylesheet === null ) { |
|
22 | - $stylesheet = get_stylesheet(); |
|
23 | - } |
|
24 | - $this->stylesheet = $stylesheet; |
|
25 | - $this->theme = wp_get_theme($this->stylesheet); |
|
26 | - |
|
27 | - parent::__construct( |
|
28 | - $metadataUrl, |
|
29 | - $stylesheet, |
|
30 | - $customSlug ? $customSlug : $stylesheet, |
|
31 | - $checkPeriod, |
|
32 | - $optionName |
|
33 | - ); |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * For themes, the update array is indexed by theme directory name. |
|
38 | - * |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - protected function getUpdateListKey() { |
|
42 | - return $this->directoryName; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Retrieve the latest update (if any) from the configured API endpoint. |
|
47 | - * |
|
48 | - * @return Puc_v4p4_Update|null An instance of Update, or NULL when no updates are available. |
|
49 | - */ |
|
50 | - public function requestUpdate() { |
|
51 | - list($themeUpdate, $result) = $this->requestMetadata('Puc_v4p4_Theme_Update', 'request_update'); |
|
52 | - |
|
53 | - if ( $themeUpdate !== null ) { |
|
54 | - /** @var Puc_v4p4_Theme_Update $themeUpdate */ |
|
55 | - $themeUpdate->slug = $this->slug; |
|
56 | - } |
|
57 | - |
|
58 | - $themeUpdate = $this->filterUpdateResult($themeUpdate, $result); |
|
59 | - return $themeUpdate; |
|
60 | - } |
|
61 | - |
|
62 | - public function userCanInstallUpdates() { |
|
63 | - return current_user_can('update_themes'); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Get the currently installed version of the plugin or theme. |
|
68 | - * |
|
69 | - * @return string Version number. |
|
70 | - */ |
|
71 | - public function getInstalledVersion() { |
|
72 | - return $this->theme->get('Version'); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - public function getAbsoluteDirectoryPath() { |
|
79 | - if ( method_exists($this->theme, 'get_stylesheet_directory') ) { |
|
80 | - return $this->theme->get_stylesheet_directory(); //Available since WP 3.4. |
|
81 | - } |
|
82 | - return get_theme_root($this->stylesheet) . '/' . $this->stylesheet; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Create an instance of the scheduler. |
|
87 | - * |
|
88 | - * @param int $checkPeriod |
|
89 | - * @return Puc_v4p4_Scheduler |
|
90 | - */ |
|
91 | - protected function createScheduler($checkPeriod) { |
|
92 | - return new Puc_v4p4_Scheduler($this, $checkPeriod, array('load-themes.php')); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Is there an update being installed right now for this theme? |
|
97 | - * |
|
98 | - * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update. |
|
99 | - * @return bool |
|
100 | - */ |
|
101 | - public function isBeingUpgraded($upgrader = null) { |
|
102 | - return $this->upgraderStatus->isThemeBeingUpgraded($this->stylesheet, $upgrader); |
|
103 | - } |
|
104 | - |
|
105 | - protected function createDebugBarExtension() { |
|
106 | - return new Puc_v4p4_DebugBar_Extension($this, 'Puc_v4p4_DebugBar_ThemePanel'); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Register a callback for filtering query arguments. |
|
111 | - * |
|
112 | - * The callback function should take one argument - an associative array of query arguments. |
|
113 | - * It should return a modified array of query arguments. |
|
114 | - * |
|
115 | - * @param callable $callback |
|
116 | - * @return void |
|
117 | - */ |
|
118 | - public function addQueryArgFilter($callback){ |
|
119 | - $this->addFilter('request_update_query_args', $callback); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Register a callback for filtering arguments passed to wp_remote_get(). |
|
124 | - * |
|
125 | - * The callback function should take one argument - an associative array of arguments - |
|
126 | - * and return a modified array or arguments. See the WP documentation on wp_remote_get() |
|
127 | - * for details on what arguments are available and how they work. |
|
128 | - * |
|
129 | - * @uses add_filter() This method is a convenience wrapper for add_filter(). |
|
130 | - * |
|
131 | - * @param callable $callback |
|
132 | - * @return void |
|
133 | - */ |
|
134 | - public function addHttpRequestArgFilter($callback) { |
|
135 | - $this->addFilter('request_update_options', $callback); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Register a callback for filtering theme updates retrieved from the external API. |
|
140 | - * |
|
141 | - * The callback function should take two arguments. If the theme update was retrieved |
|
142 | - * successfully, the first argument passed will be an instance of Theme_Update. Otherwise, |
|
143 | - * it will be NULL. The second argument will be the corresponding return value of |
|
144 | - * wp_remote_get (see WP docs for details). |
|
145 | - * |
|
146 | - * The callback function should return a new or modified instance of Theme_Update or NULL. |
|
147 | - * |
|
148 | - * @uses add_filter() This method is a convenience wrapper for add_filter(). |
|
149 | - * |
|
150 | - * @param callable $callback |
|
151 | - * @return void |
|
152 | - */ |
|
153 | - public function addResultFilter($callback) { |
|
154 | - $this->addFilter('request_update_result', $callback, 10, 2); |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @return array |
|
159 | - */ |
|
160 | - protected function getHeaderNames() { |
|
161 | - return array( |
|
162 | - 'Name' => 'Theme Name', |
|
163 | - 'ThemeURI' => 'Theme URI', |
|
164 | - 'Description' => 'Description', |
|
165 | - 'Author' => 'Author', |
|
166 | - 'AuthorURI' => 'Author URI', |
|
167 | - 'Version' => 'Version', |
|
168 | - 'Template' => 'Template', |
|
169 | - 'Status' => 'Status', |
|
170 | - 'Tags' => 'Tags', |
|
171 | - 'TextDomain' => 'Text Domain', |
|
172 | - 'DomainPath' => 'Domain Path', |
|
173 | - ); |
|
174 | - } |
|
175 | - } |
|
5 | + class Puc_v4p4_Theme_UpdateChecker extends Puc_v4p4_UpdateChecker { |
|
6 | + protected $filterSuffix = 'theme'; |
|
7 | + protected $updateTransient = 'update_themes'; |
|
8 | + protected $translationType = 'theme'; |
|
9 | + |
|
10 | + /** |
|
11 | + * @var string Theme directory name. |
|
12 | + */ |
|
13 | + protected $stylesheet; |
|
14 | + |
|
15 | + /** |
|
16 | + * @var WP_Theme Theme object. |
|
17 | + */ |
|
18 | + protected $theme; |
|
19 | + |
|
20 | + public function __construct($metadataUrl, $stylesheet = null, $customSlug = null, $checkPeriod = 12, $optionName = '') { |
|
21 | + if ( $stylesheet === null ) { |
|
22 | + $stylesheet = get_stylesheet(); |
|
23 | + } |
|
24 | + $this->stylesheet = $stylesheet; |
|
25 | + $this->theme = wp_get_theme($this->stylesheet); |
|
26 | + |
|
27 | + parent::__construct( |
|
28 | + $metadataUrl, |
|
29 | + $stylesheet, |
|
30 | + $customSlug ? $customSlug : $stylesheet, |
|
31 | + $checkPeriod, |
|
32 | + $optionName |
|
33 | + ); |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * For themes, the update array is indexed by theme directory name. |
|
38 | + * |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + protected function getUpdateListKey() { |
|
42 | + return $this->directoryName; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Retrieve the latest update (if any) from the configured API endpoint. |
|
47 | + * |
|
48 | + * @return Puc_v4p4_Update|null An instance of Update, or NULL when no updates are available. |
|
49 | + */ |
|
50 | + public function requestUpdate() { |
|
51 | + list($themeUpdate, $result) = $this->requestMetadata('Puc_v4p4_Theme_Update', 'request_update'); |
|
52 | + |
|
53 | + if ( $themeUpdate !== null ) { |
|
54 | + /** @var Puc_v4p4_Theme_Update $themeUpdate */ |
|
55 | + $themeUpdate->slug = $this->slug; |
|
56 | + } |
|
57 | + |
|
58 | + $themeUpdate = $this->filterUpdateResult($themeUpdate, $result); |
|
59 | + return $themeUpdate; |
|
60 | + } |
|
61 | + |
|
62 | + public function userCanInstallUpdates() { |
|
63 | + return current_user_can('update_themes'); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Get the currently installed version of the plugin or theme. |
|
68 | + * |
|
69 | + * @return string Version number. |
|
70 | + */ |
|
71 | + public function getInstalledVersion() { |
|
72 | + return $this->theme->get('Version'); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + public function getAbsoluteDirectoryPath() { |
|
79 | + if ( method_exists($this->theme, 'get_stylesheet_directory') ) { |
|
80 | + return $this->theme->get_stylesheet_directory(); //Available since WP 3.4. |
|
81 | + } |
|
82 | + return get_theme_root($this->stylesheet) . '/' . $this->stylesheet; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Create an instance of the scheduler. |
|
87 | + * |
|
88 | + * @param int $checkPeriod |
|
89 | + * @return Puc_v4p4_Scheduler |
|
90 | + */ |
|
91 | + protected function createScheduler($checkPeriod) { |
|
92 | + return new Puc_v4p4_Scheduler($this, $checkPeriod, array('load-themes.php')); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Is there an update being installed right now for this theme? |
|
97 | + * |
|
98 | + * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update. |
|
99 | + * @return bool |
|
100 | + */ |
|
101 | + public function isBeingUpgraded($upgrader = null) { |
|
102 | + return $this->upgraderStatus->isThemeBeingUpgraded($this->stylesheet, $upgrader); |
|
103 | + } |
|
104 | + |
|
105 | + protected function createDebugBarExtension() { |
|
106 | + return new Puc_v4p4_DebugBar_Extension($this, 'Puc_v4p4_DebugBar_ThemePanel'); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Register a callback for filtering query arguments. |
|
111 | + * |
|
112 | + * The callback function should take one argument - an associative array of query arguments. |
|
113 | + * It should return a modified array of query arguments. |
|
114 | + * |
|
115 | + * @param callable $callback |
|
116 | + * @return void |
|
117 | + */ |
|
118 | + public function addQueryArgFilter($callback){ |
|
119 | + $this->addFilter('request_update_query_args', $callback); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Register a callback for filtering arguments passed to wp_remote_get(). |
|
124 | + * |
|
125 | + * The callback function should take one argument - an associative array of arguments - |
|
126 | + * and return a modified array or arguments. See the WP documentation on wp_remote_get() |
|
127 | + * for details on what arguments are available and how they work. |
|
128 | + * |
|
129 | + * @uses add_filter() This method is a convenience wrapper for add_filter(). |
|
130 | + * |
|
131 | + * @param callable $callback |
|
132 | + * @return void |
|
133 | + */ |
|
134 | + public function addHttpRequestArgFilter($callback) { |
|
135 | + $this->addFilter('request_update_options', $callback); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Register a callback for filtering theme updates retrieved from the external API. |
|
140 | + * |
|
141 | + * The callback function should take two arguments. If the theme update was retrieved |
|
142 | + * successfully, the first argument passed will be an instance of Theme_Update. Otherwise, |
|
143 | + * it will be NULL. The second argument will be the corresponding return value of |
|
144 | + * wp_remote_get (see WP docs for details). |
|
145 | + * |
|
146 | + * The callback function should return a new or modified instance of Theme_Update or NULL. |
|
147 | + * |
|
148 | + * @uses add_filter() This method is a convenience wrapper for add_filter(). |
|
149 | + * |
|
150 | + * @param callable $callback |
|
151 | + * @return void |
|
152 | + */ |
|
153 | + public function addResultFilter($callback) { |
|
154 | + $this->addFilter('request_update_result', $callback, 10, 2); |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @return array |
|
159 | + */ |
|
160 | + protected function getHeaderNames() { |
|
161 | + return array( |
|
162 | + 'Name' => 'Theme Name', |
|
163 | + 'ThemeURI' => 'Theme URI', |
|
164 | + 'Description' => 'Description', |
|
165 | + 'Author' => 'Author', |
|
166 | + 'AuthorURI' => 'Author URI', |
|
167 | + 'Version' => 'Version', |
|
168 | + 'Template' => 'Template', |
|
169 | + 'Status' => 'Status', |
|
170 | + 'Tags' => 'Tags', |
|
171 | + 'TextDomain' => 'Text Domain', |
|
172 | + 'DomainPath' => 'Domain Path', |
|
173 | + ); |
|
174 | + } |
|
175 | + } |
|
176 | 176 | |
177 | 177 | endif; |
178 | 178 | \ No newline at end of file |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if ( !class_exists('Puc_v4p4_Theme_UpdateChecker', false) ): |
|
3 | +if (!class_exists('Puc_v4p4_Theme_UpdateChecker', false)): |
|
4 | 4 | |
5 | 5 | class Puc_v4p4_Theme_UpdateChecker extends Puc_v4p4_UpdateChecker { |
6 | 6 | protected $filterSuffix = 'theme'; |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | protected $theme; |
19 | 19 | |
20 | 20 | public function __construct($metadataUrl, $stylesheet = null, $customSlug = null, $checkPeriod = 12, $optionName = '') { |
21 | - if ( $stylesheet === null ) { |
|
21 | + if ($stylesheet === null) { |
|
22 | 22 | $stylesheet = get_stylesheet(); |
23 | 23 | } |
24 | 24 | $this->stylesheet = $stylesheet; |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | public function requestUpdate() { |
51 | 51 | list($themeUpdate, $result) = $this->requestMetadata('Puc_v4p4_Theme_Update', 'request_update'); |
52 | 52 | |
53 | - if ( $themeUpdate !== null ) { |
|
53 | + if ($themeUpdate !== null) { |
|
54 | 54 | /** @var Puc_v4p4_Theme_Update $themeUpdate */ |
55 | 55 | $themeUpdate->slug = $this->slug; |
56 | 56 | } |
@@ -76,10 +76,10 @@ discard block |
||
76 | 76 | * @return string |
77 | 77 | */ |
78 | 78 | public function getAbsoluteDirectoryPath() { |
79 | - if ( method_exists($this->theme, 'get_stylesheet_directory') ) { |
|
79 | + if (method_exists($this->theme, 'get_stylesheet_directory')) { |
|
80 | 80 | return $this->theme->get_stylesheet_directory(); //Available since WP 3.4. |
81 | 81 | } |
82 | - return get_theme_root($this->stylesheet) . '/' . $this->stylesheet; |
|
82 | + return get_theme_root($this->stylesheet).'/'.$this->stylesheet; |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | * @param callable $callback |
116 | 116 | * @return void |
117 | 117 | */ |
118 | - public function addQueryArgFilter($callback){ |
|
118 | + public function addQueryArgFilter($callback) { |
|
119 | 119 | $this->addFilter('request_update_query_args', $callback); |
120 | 120 | } |
121 | 121 |
@@ -1,110 +1,110 @@ |
||
1 | 1 | <?php |
2 | 2 | if ( !class_exists('Puc_v4p4_Plugin_Update', false) ): |
3 | 3 | |
4 | - /** |
|
5 | - * A simple container class for holding information about an available update. |
|
6 | - * |
|
7 | - * @author Janis Elsts |
|
8 | - * @copyright 2016 |
|
9 | - * @access public |
|
10 | - */ |
|
11 | - class Puc_v4p4_Plugin_Update extends Puc_v4p4_Update { |
|
12 | - public $id = 0; |
|
13 | - public $homepage; |
|
14 | - public $upgrade_notice; |
|
15 | - public $tested; |
|
16 | - public $icons = array(); |
|
17 | - public $filename; //Plugin filename relative to the plugins directory. |
|
4 | + /** |
|
5 | + * A simple container class for holding information about an available update. |
|
6 | + * |
|
7 | + * @author Janis Elsts |
|
8 | + * @copyright 2016 |
|
9 | + * @access public |
|
10 | + */ |
|
11 | + class Puc_v4p4_Plugin_Update extends Puc_v4p4_Update { |
|
12 | + public $id = 0; |
|
13 | + public $homepage; |
|
14 | + public $upgrade_notice; |
|
15 | + public $tested; |
|
16 | + public $icons = array(); |
|
17 | + public $filename; //Plugin filename relative to the plugins directory. |
|
18 | 18 | |
19 | - protected static $extraFields = array( |
|
20 | - 'id', 'homepage', 'tested', 'upgrade_notice', 'icons', 'filename', |
|
21 | - ); |
|
19 | + protected static $extraFields = array( |
|
20 | + 'id', 'homepage', 'tested', 'upgrade_notice', 'icons', 'filename', |
|
21 | + ); |
|
22 | 22 | |
23 | - /** |
|
24 | - * Create a new instance of PluginUpdate from its JSON-encoded representation. |
|
25 | - * |
|
26 | - * @param string $json |
|
27 | - * @return Puc_v4p4_Plugin_Update|null |
|
28 | - */ |
|
29 | - public static function fromJson($json){ |
|
30 | - //Since update-related information is simply a subset of the full plugin info, |
|
31 | - //we can parse the update JSON as if it was a plugin info string, then copy over |
|
32 | - //the parts that we care about. |
|
33 | - $pluginInfo = Puc_v4p4_Plugin_Info::fromJson($json); |
|
34 | - if ( $pluginInfo !== null ) { |
|
35 | - return self::fromPluginInfo($pluginInfo); |
|
36 | - } else { |
|
37 | - return null; |
|
38 | - } |
|
39 | - } |
|
23 | + /** |
|
24 | + * Create a new instance of PluginUpdate from its JSON-encoded representation. |
|
25 | + * |
|
26 | + * @param string $json |
|
27 | + * @return Puc_v4p4_Plugin_Update|null |
|
28 | + */ |
|
29 | + public static function fromJson($json){ |
|
30 | + //Since update-related information is simply a subset of the full plugin info, |
|
31 | + //we can parse the update JSON as if it was a plugin info string, then copy over |
|
32 | + //the parts that we care about. |
|
33 | + $pluginInfo = Puc_v4p4_Plugin_Info::fromJson($json); |
|
34 | + if ( $pluginInfo !== null ) { |
|
35 | + return self::fromPluginInfo($pluginInfo); |
|
36 | + } else { |
|
37 | + return null; |
|
38 | + } |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Create a new instance of PluginUpdate based on an instance of PluginInfo. |
|
43 | - * Basically, this just copies a subset of fields from one object to another. |
|
44 | - * |
|
45 | - * @param Puc_v4p4_Plugin_Info $info |
|
46 | - * @return Puc_v4p4_Plugin_Update |
|
47 | - */ |
|
48 | - public static function fromPluginInfo($info){ |
|
49 | - return self::fromObject($info); |
|
50 | - } |
|
41 | + /** |
|
42 | + * Create a new instance of PluginUpdate based on an instance of PluginInfo. |
|
43 | + * Basically, this just copies a subset of fields from one object to another. |
|
44 | + * |
|
45 | + * @param Puc_v4p4_Plugin_Info $info |
|
46 | + * @return Puc_v4p4_Plugin_Update |
|
47 | + */ |
|
48 | + public static function fromPluginInfo($info){ |
|
49 | + return self::fromObject($info); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Create a new instance by copying the necessary fields from another object. |
|
54 | - * |
|
55 | - * @param StdClass|Puc_v4p4_Plugin_Info|Puc_v4p4_Plugin_Update $object The source object. |
|
56 | - * @return Puc_v4p4_Plugin_Update The new copy. |
|
57 | - */ |
|
58 | - public static function fromObject($object) { |
|
59 | - $update = new self(); |
|
60 | - $update->copyFields($object, $update); |
|
61 | - return $update; |
|
62 | - } |
|
52 | + /** |
|
53 | + * Create a new instance by copying the necessary fields from another object. |
|
54 | + * |
|
55 | + * @param StdClass|Puc_v4p4_Plugin_Info|Puc_v4p4_Plugin_Update $object The source object. |
|
56 | + * @return Puc_v4p4_Plugin_Update The new copy. |
|
57 | + */ |
|
58 | + public static function fromObject($object) { |
|
59 | + $update = new self(); |
|
60 | + $update->copyFields($object, $update); |
|
61 | + return $update; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return string[] |
|
66 | - */ |
|
67 | - protected function getFieldNames() { |
|
68 | - return array_merge(parent::getFieldNames(), self::$extraFields); |
|
69 | - } |
|
64 | + /** |
|
65 | + * @return string[] |
|
66 | + */ |
|
67 | + protected function getFieldNames() { |
|
68 | + return array_merge(parent::getFieldNames(), self::$extraFields); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Transform the update into the format used by WordPress native plugin API. |
|
73 | - * |
|
74 | - * @return object |
|
75 | - */ |
|
76 | - public function toWpFormat() { |
|
77 | - $update = parent::toWpFormat(); |
|
71 | + /** |
|
72 | + * Transform the update into the format used by WordPress native plugin API. |
|
73 | + * |
|
74 | + * @return object |
|
75 | + */ |
|
76 | + public function toWpFormat() { |
|
77 | + $update = parent::toWpFormat(); |
|
78 | 78 | |
79 | - $update->id = $this->id; |
|
80 | - $update->url = $this->homepage; |
|
81 | - $update->tested = $this->tested; |
|
82 | - $update->plugin = $this->filename; |
|
79 | + $update->id = $this->id; |
|
80 | + $update->url = $this->homepage; |
|
81 | + $update->tested = $this->tested; |
|
82 | + $update->plugin = $this->filename; |
|
83 | 83 | |
84 | - if ( !empty($this->upgrade_notice) ) { |
|
85 | - $update->upgrade_notice = $this->upgrade_notice; |
|
86 | - } |
|
84 | + if ( !empty($this->upgrade_notice) ) { |
|
85 | + $update->upgrade_notice = $this->upgrade_notice; |
|
86 | + } |
|
87 | 87 | |
88 | - if ( !empty($this->icons) && is_array($this->icons) ) { |
|
89 | - //This should be an array with up to 4 keys: 'svg', '1x', '2x' and 'default'. |
|
90 | - //Docs: https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons |
|
91 | - $icons = array_intersect_key( |
|
92 | - $this->icons, |
|
93 | - array('svg' => true, '1x' => true, '2x' => true, 'default' => true) |
|
94 | - ); |
|
95 | - if ( !empty($icons) ) { |
|
96 | - $update->icons = $icons; |
|
88 | + if ( !empty($this->icons) && is_array($this->icons) ) { |
|
89 | + //This should be an array with up to 4 keys: 'svg', '1x', '2x' and 'default'. |
|
90 | + //Docs: https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons |
|
91 | + $icons = array_intersect_key( |
|
92 | + $this->icons, |
|
93 | + array('svg' => true, '1x' => true, '2x' => true, 'default' => true) |
|
94 | + ); |
|
95 | + if ( !empty($icons) ) { |
|
96 | + $update->icons = $icons; |
|
97 | 97 | |
98 | - //It appears that the 'default' icon isn't used anywhere in WordPress 4.9, |
|
99 | - //but lets set it just in case a future release needs it. |
|
100 | - if ( !isset($update->icons['default']) ) { |
|
101 | - $update->icons['default'] = current($update->icons); |
|
102 | - } |
|
103 | - } |
|
104 | - } |
|
98 | + //It appears that the 'default' icon isn't used anywhere in WordPress 4.9, |
|
99 | + //but lets set it just in case a future release needs it. |
|
100 | + if ( !isset($update->icons['default']) ) { |
|
101 | + $update->icons['default'] = current($update->icons); |
|
102 | + } |
|
103 | + } |
|
104 | + } |
|
105 | 105 | |
106 | - return $update; |
|
107 | - } |
|
108 | - } |
|
106 | + return $update; |
|
107 | + } |
|
108 | + } |
|
109 | 109 | |
110 | 110 | endif; |
111 | 111 | \ No newline at end of file |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( !class_exists('Puc_v4p4_Plugin_Update', false) ): |
|
2 | +if (!class_exists('Puc_v4p4_Plugin_Update', false)): |
|
3 | 3 | |
4 | 4 | /** |
5 | 5 | * A simple container class for holding information about an available update. |
@@ -26,12 +26,12 @@ discard block |
||
26 | 26 | * @param string $json |
27 | 27 | * @return Puc_v4p4_Plugin_Update|null |
28 | 28 | */ |
29 | - public static function fromJson($json){ |
|
29 | + public static function fromJson($json) { |
|
30 | 30 | //Since update-related information is simply a subset of the full plugin info, |
31 | 31 | //we can parse the update JSON as if it was a plugin info string, then copy over |
32 | 32 | //the parts that we care about. |
33 | 33 | $pluginInfo = Puc_v4p4_Plugin_Info::fromJson($json); |
34 | - if ( $pluginInfo !== null ) { |
|
34 | + if ($pluginInfo !== null) { |
|
35 | 35 | return self::fromPluginInfo($pluginInfo); |
36 | 36 | } else { |
37 | 37 | return null; |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | * @param Puc_v4p4_Plugin_Info $info |
46 | 46 | * @return Puc_v4p4_Plugin_Update |
47 | 47 | */ |
48 | - public static function fromPluginInfo($info){ |
|
48 | + public static function fromPluginInfo($info) { |
|
49 | 49 | return self::fromObject($info); |
50 | 50 | } |
51 | 51 | |
@@ -81,23 +81,23 @@ discard block |
||
81 | 81 | $update->tested = $this->tested; |
82 | 82 | $update->plugin = $this->filename; |
83 | 83 | |
84 | - if ( !empty($this->upgrade_notice) ) { |
|
84 | + if (!empty($this->upgrade_notice)) { |
|
85 | 85 | $update->upgrade_notice = $this->upgrade_notice; |
86 | 86 | } |
87 | 87 | |
88 | - if ( !empty($this->icons) && is_array($this->icons) ) { |
|
88 | + if (!empty($this->icons) && is_array($this->icons)) { |
|
89 | 89 | //This should be an array with up to 4 keys: 'svg', '1x', '2x' and 'default'. |
90 | 90 | //Docs: https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons |
91 | 91 | $icons = array_intersect_key( |
92 | 92 | $this->icons, |
93 | 93 | array('svg' => true, '1x' => true, '2x' => true, 'default' => true) |
94 | 94 | ); |
95 | - if ( !empty($icons) ) { |
|
95 | + if (!empty($icons)) { |
|
96 | 96 | $update->icons = $icons; |
97 | 97 | |
98 | 98 | //It appears that the 'default' icon isn't used anywhere in WordPress 4.9, |
99 | 99 | //but lets set it just in case a future release needs it. |
100 | - if ( !isset($update->icons['default']) ) { |
|
100 | + if (!isset($update->icons['default'])) { |
|
101 | 101 | $update->icons['default'] = current($update->icons); |
102 | 102 | } |
103 | 103 | } |
@@ -2,895 +2,895 @@ |
||
2 | 2 | |
3 | 3 | if ( !class_exists('Puc_v4p4_UpdateChecker', false) ): |
4 | 4 | |
5 | - abstract class Puc_v4p4_UpdateChecker { |
|
6 | - protected $filterSuffix = ''; |
|
7 | - protected $updateTransient = ''; |
|
8 | - protected $translationType = ''; //"plugin" or "theme". |
|
9 | - |
|
10 | - /** |
|
11 | - * Set to TRUE to enable error reporting. Errors are raised using trigger_error() |
|
12 | - * and should be logged to the standard PHP error log. |
|
13 | - * @var bool |
|
14 | - */ |
|
15 | - public $debugMode = false; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var string Where to store the update info. |
|
19 | - */ |
|
20 | - public $optionName = ''; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var string The URL of the metadata file. |
|
24 | - */ |
|
25 | - public $metadataUrl = ''; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var string Plugin or theme directory name. |
|
29 | - */ |
|
30 | - public $directoryName = ''; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var string The slug that will be used in update checker hooks and remote API requests. |
|
34 | - * Usually matches the directory name unless the plugin/theme directory has been renamed. |
|
35 | - */ |
|
36 | - public $slug = ''; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var Puc_v4p4_Scheduler |
|
40 | - */ |
|
41 | - public $scheduler; |
|
42 | - |
|
43 | - /** |
|
44 | - * @var Puc_v4p4_UpgraderStatus |
|
45 | - */ |
|
46 | - protected $upgraderStatus; |
|
47 | - |
|
48 | - /** |
|
49 | - * @var Puc_v4p4_StateStore |
|
50 | - */ |
|
51 | - protected $updateState; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var array List of API errors triggered during the last checkForUpdates() call. |
|
55 | - */ |
|
56 | - protected $lastRequestApiErrors = array(); |
|
57 | - |
|
58 | - public function __construct($metadataUrl, $directoryName, $slug = null, $checkPeriod = 12, $optionName = '') { |
|
59 | - $this->debugMode = (bool)(constant('WP_DEBUG')); |
|
60 | - $this->metadataUrl = $metadataUrl; |
|
61 | - $this->directoryName = $directoryName; |
|
62 | - $this->slug = !empty($slug) ? $slug : $this->directoryName; |
|
63 | - |
|
64 | - $this->optionName = $optionName; |
|
65 | - if ( empty($this->optionName) ) { |
|
66 | - //BC: Initially the library only supported plugin updates and didn't use type prefixes |
|
67 | - //in the option name. Lets use the same prefix-less name when possible. |
|
68 | - if ( $this->filterSuffix === '' ) { |
|
69 | - $this->optionName = 'external_updates-' . $this->slug; |
|
70 | - } else { |
|
71 | - $this->optionName = $this->getUniqueName('external_updates'); |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - $this->scheduler = $this->createScheduler($checkPeriod); |
|
76 | - $this->upgraderStatus = new Puc_v4p4_UpgraderStatus(); |
|
77 | - $this->updateState = new Puc_v4p4_StateStore($this->optionName); |
|
78 | - |
|
79 | - if ( did_action('init') ) { |
|
80 | - $this->loadTextDomain(); |
|
81 | - } else { |
|
82 | - add_action('init', array($this, 'loadTextDomain')); |
|
83 | - } |
|
84 | - |
|
85 | - $this->installHooks(); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @internal |
|
90 | - */ |
|
91 | - public function loadTextDomain() { |
|
92 | - //We're not using load_plugin_textdomain() or its siblings because figuring out where |
|
93 | - //the library is located (plugin, mu-plugin, theme, custom wp-content paths) is messy. |
|
94 | - $domain = 'plugin-update-checker'; |
|
95 | - $locale = apply_filters( |
|
96 | - 'plugin_locale', |
|
97 | - (is_admin() && function_exists('get_user_locale')) ? get_user_locale() : get_locale(), |
|
98 | - $domain |
|
99 | - ); |
|
100 | - |
|
101 | - $moFile = $domain . '-' . $locale . '.mo'; |
|
102 | - $path = realpath(dirname(__FILE__) . '/../../languages'); |
|
103 | - |
|
104 | - if ($path && file_exists($path)) { |
|
105 | - load_textdomain($domain, $path . '/' . $moFile); |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - protected function installHooks() { |
|
110 | - //Insert our update info into the update array maintained by WP. |
|
111 | - add_filter('site_transient_' . $this->updateTransient, array($this,'injectUpdate')); |
|
112 | - |
|
113 | - //Insert translation updates into the update list. |
|
114 | - add_filter('site_transient_' . $this->updateTransient, array($this, 'injectTranslationUpdates')); |
|
115 | - |
|
116 | - //Clear translation updates when WP clears the update cache. |
|
117 | - //This needs to be done directly because the library doesn't actually remove obsolete plugin updates, |
|
118 | - //it just hides them (see getUpdate()). We can't do that with translations - too much disk I/O. |
|
119 | - add_action( |
|
120 | - 'delete_site_transient_' . $this->updateTransient, |
|
121 | - array($this, 'clearCachedTranslationUpdates') |
|
122 | - ); |
|
123 | - |
|
124 | - //Rename the update directory to be the same as the existing directory. |
|
125 | - if ( $this->directoryName !== '.' ) { |
|
126 | - add_filter('upgrader_source_selection', array($this, 'fixDirectoryName'), 10, 3); |
|
127 | - } |
|
128 | - |
|
129 | - //Allow HTTP requests to the metadata URL even if it's on a local host. |
|
130 | - add_filter('http_request_host_is_external', array($this, 'allowMetadataHost'), 10, 2); |
|
131 | - |
|
132 | - //DebugBar integration. |
|
133 | - if ( did_action('plugins_loaded') ) { |
|
134 | - $this->maybeInitDebugBar(); |
|
135 | - } else { |
|
136 | - add_action('plugins_loaded', array($this, 'maybeInitDebugBar')); |
|
137 | - } |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Remove hooks that were added by this update checker instance. |
|
142 | - */ |
|
143 | - protected function removeHooks() { |
|
144 | - remove_filter('site_transient_' . $this->updateTransient, array($this,'injectUpdate')); |
|
145 | - remove_filter('site_transient_' . $this->updateTransient, array($this, 'injectTranslationUpdates')); |
|
146 | - remove_action( |
|
147 | - 'delete_site_transient_' . $this->updateTransient, |
|
148 | - array($this, 'clearCachedTranslationUpdates') |
|
149 | - ); |
|
150 | - |
|
151 | - remove_filter('upgrader_source_selection', array($this, 'fixDirectoryName'), 10); |
|
152 | - remove_filter('http_request_host_is_external', array($this, 'allowMetadataHost'), 10); |
|
153 | - remove_action('plugins_loaded', array($this, 'maybeInitDebugBar')); |
|
154 | - |
|
155 | - remove_action('init', array($this, 'loadTextDomain')); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Check if the current user has the required permissions to install updates. |
|
160 | - * |
|
161 | - * @return bool |
|
162 | - */ |
|
163 | - abstract public function userCanInstallUpdates(); |
|
164 | - |
|
165 | - /** |
|
166 | - * Explicitly allow HTTP requests to the metadata URL. |
|
167 | - * |
|
168 | - * WordPress has a security feature where the HTTP API will reject all requests that are sent to |
|
169 | - * another site hosted on the same server as the current site (IP match), a local host, or a local |
|
170 | - * IP, unless the host exactly matches the current site. |
|
171 | - * |
|
172 | - * This feature is opt-in (at least in WP 4.4). Apparently some people enable it. |
|
173 | - * |
|
174 | - * That can be a problem when you're developing your plugin and you decide to host the update information |
|
175 | - * on the same server as your test site. Update requests will mysteriously fail. |
|
176 | - * |
|
177 | - * We fix that by adding an exception for the metadata host. |
|
178 | - * |
|
179 | - * @param bool $allow |
|
180 | - * @param string $host |
|
181 | - * @return bool |
|
182 | - */ |
|
183 | - public function allowMetadataHost($allow, $host) { |
|
184 | - static $metadataHost = 0; //Using 0 instead of NULL because parse_url can return NULL. |
|
185 | - if ( $metadataHost === 0 ) { |
|
186 | - $metadataHost = @parse_url($this->metadataUrl, PHP_URL_HOST); |
|
187 | - } |
|
188 | - |
|
189 | - if ( is_string($metadataHost) && (strtolower($host) === strtolower($metadataHost)) ) { |
|
190 | - return true; |
|
191 | - } |
|
192 | - return $allow; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Create an instance of the scheduler. |
|
197 | - * |
|
198 | - * This is implemented as a method to make it possible for plugins to subclass the update checker |
|
199 | - * and substitute their own scheduler. |
|
200 | - * |
|
201 | - * @param int $checkPeriod |
|
202 | - * @return Puc_v4p4_Scheduler |
|
203 | - */ |
|
204 | - abstract protected function createScheduler($checkPeriod); |
|
205 | - |
|
206 | - /** |
|
207 | - * Check for updates. The results are stored in the DB option specified in $optionName. |
|
208 | - * |
|
209 | - * @return Puc_v4p4_Update|null |
|
210 | - */ |
|
211 | - public function checkForUpdates() { |
|
212 | - $installedVersion = $this->getInstalledVersion(); |
|
213 | - //Fail silently if we can't find the plugin/theme or read its header. |
|
214 | - if ( $installedVersion === null ) { |
|
215 | - $this->triggerError( |
|
216 | - sprintf('Skipping update check for %s - installed version unknown.', $this->slug), |
|
217 | - E_USER_WARNING |
|
218 | - ); |
|
219 | - return null; |
|
220 | - } |
|
221 | - |
|
222 | - //Start collecting API errors. |
|
223 | - $this->lastRequestApiErrors = array(); |
|
224 | - add_action('puc_api_error', array($this, 'collectApiErrors'), 10, 4); |
|
225 | - |
|
226 | - $state = $this->updateState; |
|
227 | - $state->setLastCheckToNow() |
|
228 | - ->setCheckedVersion($installedVersion) |
|
229 | - ->save(); //Save before checking in case something goes wrong |
|
230 | - |
|
231 | - $state->setUpdate($this->requestUpdate()); |
|
232 | - $state->save(); |
|
233 | - |
|
234 | - //Stop collecting API errors. |
|
235 | - remove_action('puc_api_error', array($this, 'collectApiErrors'), 10); |
|
236 | - |
|
237 | - return $this->getUpdate(); |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Load the update checker state from the DB. |
|
242 | - * |
|
243 | - * @return Puc_v4p4_StateStore |
|
244 | - */ |
|
245 | - public function getUpdateState() { |
|
246 | - return $this->updateState->lazyLoad(); |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * Reset update checker state - i.e. last check time, cached update data and so on. |
|
251 | - * |
|
252 | - * Call this when your plugin is being uninstalled, or if you want to |
|
253 | - * clear the update cache. |
|
254 | - */ |
|
255 | - public function resetUpdateState() { |
|
256 | - $this->updateState->delete(); |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * Get the details of the currently available update, if any. |
|
261 | - * |
|
262 | - * If no updates are available, or if the last known update version is below or equal |
|
263 | - * to the currently installed version, this method will return NULL. |
|
264 | - * |
|
265 | - * Uses cached update data. To retrieve update information straight from |
|
266 | - * the metadata URL, call requestUpdate() instead. |
|
267 | - * |
|
268 | - * @return Puc_v4p4_Update|null |
|
269 | - */ |
|
270 | - public function getUpdate() { |
|
271 | - $update = $this->updateState->getUpdate(); |
|
272 | - |
|
273 | - //Is there an update available? |
|
274 | - if ( isset($update) ) { |
|
275 | - //Check if the update is actually newer than the currently installed version. |
|
276 | - $installedVersion = $this->getInstalledVersion(); |
|
277 | - if ( ($installedVersion !== null) && version_compare($update->version, $installedVersion, '>') ){ |
|
278 | - return $update; |
|
279 | - } |
|
280 | - } |
|
281 | - return null; |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * Retrieve the latest update (if any) from the configured API endpoint. |
|
286 | - * |
|
287 | - * Subclasses should run the update through filterUpdateResult before returning it. |
|
288 | - * |
|
289 | - * @return Puc_v4p4_Update An instance of Update, or NULL when no updates are available. |
|
290 | - */ |
|
291 | - abstract public function requestUpdate(); |
|
292 | - |
|
293 | - /** |
|
294 | - * Filter the result of a requestUpdate() call. |
|
295 | - * |
|
296 | - * @param Puc_v4p4_Update|null $update |
|
297 | - * @param array|WP_Error|null $httpResult The value returned by wp_remote_get(), if any. |
|
298 | - * @return Puc_v4p4_Update |
|
299 | - */ |
|
300 | - protected function filterUpdateResult($update, $httpResult = null) { |
|
301 | - //Let plugins/themes modify the update. |
|
302 | - $update = apply_filters($this->getUniqueName('request_update_result'), $update, $httpResult); |
|
303 | - |
|
304 | - if ( isset($update, $update->translations) ) { |
|
305 | - //Keep only those translation updates that apply to this site. |
|
306 | - $update->translations = $this->filterApplicableTranslations($update->translations); |
|
307 | - } |
|
308 | - |
|
309 | - return $update; |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * Get the currently installed version of the plugin or theme. |
|
314 | - * |
|
315 | - * @return string|null Version number. |
|
316 | - */ |
|
317 | - abstract public function getInstalledVersion(); |
|
318 | - |
|
319 | - /** |
|
320 | - * Get the full path of the plugin or theme directory. |
|
321 | - * |
|
322 | - * @return string |
|
323 | - */ |
|
324 | - abstract public function getAbsoluteDirectoryPath(); |
|
325 | - |
|
326 | - /** |
|
327 | - * Trigger a PHP error, but only when $debugMode is enabled. |
|
328 | - * |
|
329 | - * @param string $message |
|
330 | - * @param int $errorType |
|
331 | - */ |
|
332 | - protected function triggerError($message, $errorType) { |
|
333 | - if ($this->debugMode) { |
|
334 | - trigger_error($message, $errorType); |
|
335 | - } |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * Get the full name of an update checker filter, action or DB entry. |
|
340 | - * |
|
341 | - * This method adds the "puc_" prefix and the "-$slug" suffix to the filter name. |
|
342 | - * For example, "pre_inject_update" becomes "puc_pre_inject_update-plugin-slug". |
|
343 | - * |
|
344 | - * @param string $baseTag |
|
345 | - * @return string |
|
346 | - */ |
|
347 | - public function getUniqueName($baseTag) { |
|
348 | - $name = 'puc_' . $baseTag; |
|
349 | - if ($this->filterSuffix !== '') { |
|
350 | - $name .= '_' . $this->filterSuffix; |
|
351 | - } |
|
352 | - return $name . '-' . $this->slug; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Store API errors that are generated when checking for updates. |
|
357 | - * |
|
358 | - * @internal |
|
359 | - * @param WP_Error $error |
|
360 | - * @param array|null $httpResponse |
|
361 | - * @param string|null $url |
|
362 | - * @param string|null $slug |
|
363 | - */ |
|
364 | - public function collectApiErrors($error, $httpResponse = null, $url = null, $slug = null) { |
|
365 | - if ( isset($slug) && ($slug !== $this->slug) ) { |
|
366 | - return; |
|
367 | - } |
|
368 | - |
|
369 | - $this->lastRequestApiErrors[] = array( |
|
370 | - 'error' => $error, |
|
371 | - 'httpResponse' => $httpResponse, |
|
372 | - 'url' => $url, |
|
373 | - ); |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * @return array |
|
378 | - */ |
|
379 | - public function getLastRequestApiErrors() { |
|
380 | - return $this->lastRequestApiErrors; |
|
381 | - } |
|
382 | - |
|
383 | - /* ------------------------------------------------------------------- |
|
5 | + abstract class Puc_v4p4_UpdateChecker { |
|
6 | + protected $filterSuffix = ''; |
|
7 | + protected $updateTransient = ''; |
|
8 | + protected $translationType = ''; //"plugin" or "theme". |
|
9 | + |
|
10 | + /** |
|
11 | + * Set to TRUE to enable error reporting. Errors are raised using trigger_error() |
|
12 | + * and should be logged to the standard PHP error log. |
|
13 | + * @var bool |
|
14 | + */ |
|
15 | + public $debugMode = false; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var string Where to store the update info. |
|
19 | + */ |
|
20 | + public $optionName = ''; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var string The URL of the metadata file. |
|
24 | + */ |
|
25 | + public $metadataUrl = ''; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var string Plugin or theme directory name. |
|
29 | + */ |
|
30 | + public $directoryName = ''; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var string The slug that will be used in update checker hooks and remote API requests. |
|
34 | + * Usually matches the directory name unless the plugin/theme directory has been renamed. |
|
35 | + */ |
|
36 | + public $slug = ''; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var Puc_v4p4_Scheduler |
|
40 | + */ |
|
41 | + public $scheduler; |
|
42 | + |
|
43 | + /** |
|
44 | + * @var Puc_v4p4_UpgraderStatus |
|
45 | + */ |
|
46 | + protected $upgraderStatus; |
|
47 | + |
|
48 | + /** |
|
49 | + * @var Puc_v4p4_StateStore |
|
50 | + */ |
|
51 | + protected $updateState; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var array List of API errors triggered during the last checkForUpdates() call. |
|
55 | + */ |
|
56 | + protected $lastRequestApiErrors = array(); |
|
57 | + |
|
58 | + public function __construct($metadataUrl, $directoryName, $slug = null, $checkPeriod = 12, $optionName = '') { |
|
59 | + $this->debugMode = (bool)(constant('WP_DEBUG')); |
|
60 | + $this->metadataUrl = $metadataUrl; |
|
61 | + $this->directoryName = $directoryName; |
|
62 | + $this->slug = !empty($slug) ? $slug : $this->directoryName; |
|
63 | + |
|
64 | + $this->optionName = $optionName; |
|
65 | + if ( empty($this->optionName) ) { |
|
66 | + //BC: Initially the library only supported plugin updates and didn't use type prefixes |
|
67 | + //in the option name. Lets use the same prefix-less name when possible. |
|
68 | + if ( $this->filterSuffix === '' ) { |
|
69 | + $this->optionName = 'external_updates-' . $this->slug; |
|
70 | + } else { |
|
71 | + $this->optionName = $this->getUniqueName('external_updates'); |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + $this->scheduler = $this->createScheduler($checkPeriod); |
|
76 | + $this->upgraderStatus = new Puc_v4p4_UpgraderStatus(); |
|
77 | + $this->updateState = new Puc_v4p4_StateStore($this->optionName); |
|
78 | + |
|
79 | + if ( did_action('init') ) { |
|
80 | + $this->loadTextDomain(); |
|
81 | + } else { |
|
82 | + add_action('init', array($this, 'loadTextDomain')); |
|
83 | + } |
|
84 | + |
|
85 | + $this->installHooks(); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @internal |
|
90 | + */ |
|
91 | + public function loadTextDomain() { |
|
92 | + //We're not using load_plugin_textdomain() or its siblings because figuring out where |
|
93 | + //the library is located (plugin, mu-plugin, theme, custom wp-content paths) is messy. |
|
94 | + $domain = 'plugin-update-checker'; |
|
95 | + $locale = apply_filters( |
|
96 | + 'plugin_locale', |
|
97 | + (is_admin() && function_exists('get_user_locale')) ? get_user_locale() : get_locale(), |
|
98 | + $domain |
|
99 | + ); |
|
100 | + |
|
101 | + $moFile = $domain . '-' . $locale . '.mo'; |
|
102 | + $path = realpath(dirname(__FILE__) . '/../../languages'); |
|
103 | + |
|
104 | + if ($path && file_exists($path)) { |
|
105 | + load_textdomain($domain, $path . '/' . $moFile); |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + protected function installHooks() { |
|
110 | + //Insert our update info into the update array maintained by WP. |
|
111 | + add_filter('site_transient_' . $this->updateTransient, array($this,'injectUpdate')); |
|
112 | + |
|
113 | + //Insert translation updates into the update list. |
|
114 | + add_filter('site_transient_' . $this->updateTransient, array($this, 'injectTranslationUpdates')); |
|
115 | + |
|
116 | + //Clear translation updates when WP clears the update cache. |
|
117 | + //This needs to be done directly because the library doesn't actually remove obsolete plugin updates, |
|
118 | + //it just hides them (see getUpdate()). We can't do that with translations - too much disk I/O. |
|
119 | + add_action( |
|
120 | + 'delete_site_transient_' . $this->updateTransient, |
|
121 | + array($this, 'clearCachedTranslationUpdates') |
|
122 | + ); |
|
123 | + |
|
124 | + //Rename the update directory to be the same as the existing directory. |
|
125 | + if ( $this->directoryName !== '.' ) { |
|
126 | + add_filter('upgrader_source_selection', array($this, 'fixDirectoryName'), 10, 3); |
|
127 | + } |
|
128 | + |
|
129 | + //Allow HTTP requests to the metadata URL even if it's on a local host. |
|
130 | + add_filter('http_request_host_is_external', array($this, 'allowMetadataHost'), 10, 2); |
|
131 | + |
|
132 | + //DebugBar integration. |
|
133 | + if ( did_action('plugins_loaded') ) { |
|
134 | + $this->maybeInitDebugBar(); |
|
135 | + } else { |
|
136 | + add_action('plugins_loaded', array($this, 'maybeInitDebugBar')); |
|
137 | + } |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Remove hooks that were added by this update checker instance. |
|
142 | + */ |
|
143 | + protected function removeHooks() { |
|
144 | + remove_filter('site_transient_' . $this->updateTransient, array($this,'injectUpdate')); |
|
145 | + remove_filter('site_transient_' . $this->updateTransient, array($this, 'injectTranslationUpdates')); |
|
146 | + remove_action( |
|
147 | + 'delete_site_transient_' . $this->updateTransient, |
|
148 | + array($this, 'clearCachedTranslationUpdates') |
|
149 | + ); |
|
150 | + |
|
151 | + remove_filter('upgrader_source_selection', array($this, 'fixDirectoryName'), 10); |
|
152 | + remove_filter('http_request_host_is_external', array($this, 'allowMetadataHost'), 10); |
|
153 | + remove_action('plugins_loaded', array($this, 'maybeInitDebugBar')); |
|
154 | + |
|
155 | + remove_action('init', array($this, 'loadTextDomain')); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Check if the current user has the required permissions to install updates. |
|
160 | + * |
|
161 | + * @return bool |
|
162 | + */ |
|
163 | + abstract public function userCanInstallUpdates(); |
|
164 | + |
|
165 | + /** |
|
166 | + * Explicitly allow HTTP requests to the metadata URL. |
|
167 | + * |
|
168 | + * WordPress has a security feature where the HTTP API will reject all requests that are sent to |
|
169 | + * another site hosted on the same server as the current site (IP match), a local host, or a local |
|
170 | + * IP, unless the host exactly matches the current site. |
|
171 | + * |
|
172 | + * This feature is opt-in (at least in WP 4.4). Apparently some people enable it. |
|
173 | + * |
|
174 | + * That can be a problem when you're developing your plugin and you decide to host the update information |
|
175 | + * on the same server as your test site. Update requests will mysteriously fail. |
|
176 | + * |
|
177 | + * We fix that by adding an exception for the metadata host. |
|
178 | + * |
|
179 | + * @param bool $allow |
|
180 | + * @param string $host |
|
181 | + * @return bool |
|
182 | + */ |
|
183 | + public function allowMetadataHost($allow, $host) { |
|
184 | + static $metadataHost = 0; //Using 0 instead of NULL because parse_url can return NULL. |
|
185 | + if ( $metadataHost === 0 ) { |
|
186 | + $metadataHost = @parse_url($this->metadataUrl, PHP_URL_HOST); |
|
187 | + } |
|
188 | + |
|
189 | + if ( is_string($metadataHost) && (strtolower($host) === strtolower($metadataHost)) ) { |
|
190 | + return true; |
|
191 | + } |
|
192 | + return $allow; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Create an instance of the scheduler. |
|
197 | + * |
|
198 | + * This is implemented as a method to make it possible for plugins to subclass the update checker |
|
199 | + * and substitute their own scheduler. |
|
200 | + * |
|
201 | + * @param int $checkPeriod |
|
202 | + * @return Puc_v4p4_Scheduler |
|
203 | + */ |
|
204 | + abstract protected function createScheduler($checkPeriod); |
|
205 | + |
|
206 | + /** |
|
207 | + * Check for updates. The results are stored in the DB option specified in $optionName. |
|
208 | + * |
|
209 | + * @return Puc_v4p4_Update|null |
|
210 | + */ |
|
211 | + public function checkForUpdates() { |
|
212 | + $installedVersion = $this->getInstalledVersion(); |
|
213 | + //Fail silently if we can't find the plugin/theme or read its header. |
|
214 | + if ( $installedVersion === null ) { |
|
215 | + $this->triggerError( |
|
216 | + sprintf('Skipping update check for %s - installed version unknown.', $this->slug), |
|
217 | + E_USER_WARNING |
|
218 | + ); |
|
219 | + return null; |
|
220 | + } |
|
221 | + |
|
222 | + //Start collecting API errors. |
|
223 | + $this->lastRequestApiErrors = array(); |
|
224 | + add_action('puc_api_error', array($this, 'collectApiErrors'), 10, 4); |
|
225 | + |
|
226 | + $state = $this->updateState; |
|
227 | + $state->setLastCheckToNow() |
|
228 | + ->setCheckedVersion($installedVersion) |
|
229 | + ->save(); //Save before checking in case something goes wrong |
|
230 | + |
|
231 | + $state->setUpdate($this->requestUpdate()); |
|
232 | + $state->save(); |
|
233 | + |
|
234 | + //Stop collecting API errors. |
|
235 | + remove_action('puc_api_error', array($this, 'collectApiErrors'), 10); |
|
236 | + |
|
237 | + return $this->getUpdate(); |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Load the update checker state from the DB. |
|
242 | + * |
|
243 | + * @return Puc_v4p4_StateStore |
|
244 | + */ |
|
245 | + public function getUpdateState() { |
|
246 | + return $this->updateState->lazyLoad(); |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * Reset update checker state - i.e. last check time, cached update data and so on. |
|
251 | + * |
|
252 | + * Call this when your plugin is being uninstalled, or if you want to |
|
253 | + * clear the update cache. |
|
254 | + */ |
|
255 | + public function resetUpdateState() { |
|
256 | + $this->updateState->delete(); |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * Get the details of the currently available update, if any. |
|
261 | + * |
|
262 | + * If no updates are available, or if the last known update version is below or equal |
|
263 | + * to the currently installed version, this method will return NULL. |
|
264 | + * |
|
265 | + * Uses cached update data. To retrieve update information straight from |
|
266 | + * the metadata URL, call requestUpdate() instead. |
|
267 | + * |
|
268 | + * @return Puc_v4p4_Update|null |
|
269 | + */ |
|
270 | + public function getUpdate() { |
|
271 | + $update = $this->updateState->getUpdate(); |
|
272 | + |
|
273 | + //Is there an update available? |
|
274 | + if ( isset($update) ) { |
|
275 | + //Check if the update is actually newer than the currently installed version. |
|
276 | + $installedVersion = $this->getInstalledVersion(); |
|
277 | + if ( ($installedVersion !== null) && version_compare($update->version, $installedVersion, '>') ){ |
|
278 | + return $update; |
|
279 | + } |
|
280 | + } |
|
281 | + return null; |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * Retrieve the latest update (if any) from the configured API endpoint. |
|
286 | + * |
|
287 | + * Subclasses should run the update through filterUpdateResult before returning it. |
|
288 | + * |
|
289 | + * @return Puc_v4p4_Update An instance of Update, or NULL when no updates are available. |
|
290 | + */ |
|
291 | + abstract public function requestUpdate(); |
|
292 | + |
|
293 | + /** |
|
294 | + * Filter the result of a requestUpdate() call. |
|
295 | + * |
|
296 | + * @param Puc_v4p4_Update|null $update |
|
297 | + * @param array|WP_Error|null $httpResult The value returned by wp_remote_get(), if any. |
|
298 | + * @return Puc_v4p4_Update |
|
299 | + */ |
|
300 | + protected function filterUpdateResult($update, $httpResult = null) { |
|
301 | + //Let plugins/themes modify the update. |
|
302 | + $update = apply_filters($this->getUniqueName('request_update_result'), $update, $httpResult); |
|
303 | + |
|
304 | + if ( isset($update, $update->translations) ) { |
|
305 | + //Keep only those translation updates that apply to this site. |
|
306 | + $update->translations = $this->filterApplicableTranslations($update->translations); |
|
307 | + } |
|
308 | + |
|
309 | + return $update; |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * Get the currently installed version of the plugin or theme. |
|
314 | + * |
|
315 | + * @return string|null Version number. |
|
316 | + */ |
|
317 | + abstract public function getInstalledVersion(); |
|
318 | + |
|
319 | + /** |
|
320 | + * Get the full path of the plugin or theme directory. |
|
321 | + * |
|
322 | + * @return string |
|
323 | + */ |
|
324 | + abstract public function getAbsoluteDirectoryPath(); |
|
325 | + |
|
326 | + /** |
|
327 | + * Trigger a PHP error, but only when $debugMode is enabled. |
|
328 | + * |
|
329 | + * @param string $message |
|
330 | + * @param int $errorType |
|
331 | + */ |
|
332 | + protected function triggerError($message, $errorType) { |
|
333 | + if ($this->debugMode) { |
|
334 | + trigger_error($message, $errorType); |
|
335 | + } |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * Get the full name of an update checker filter, action or DB entry. |
|
340 | + * |
|
341 | + * This method adds the "puc_" prefix and the "-$slug" suffix to the filter name. |
|
342 | + * For example, "pre_inject_update" becomes "puc_pre_inject_update-plugin-slug". |
|
343 | + * |
|
344 | + * @param string $baseTag |
|
345 | + * @return string |
|
346 | + */ |
|
347 | + public function getUniqueName($baseTag) { |
|
348 | + $name = 'puc_' . $baseTag; |
|
349 | + if ($this->filterSuffix !== '') { |
|
350 | + $name .= '_' . $this->filterSuffix; |
|
351 | + } |
|
352 | + return $name . '-' . $this->slug; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Store API errors that are generated when checking for updates. |
|
357 | + * |
|
358 | + * @internal |
|
359 | + * @param WP_Error $error |
|
360 | + * @param array|null $httpResponse |
|
361 | + * @param string|null $url |
|
362 | + * @param string|null $slug |
|
363 | + */ |
|
364 | + public function collectApiErrors($error, $httpResponse = null, $url = null, $slug = null) { |
|
365 | + if ( isset($slug) && ($slug !== $this->slug) ) { |
|
366 | + return; |
|
367 | + } |
|
368 | + |
|
369 | + $this->lastRequestApiErrors[] = array( |
|
370 | + 'error' => $error, |
|
371 | + 'httpResponse' => $httpResponse, |
|
372 | + 'url' => $url, |
|
373 | + ); |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * @return array |
|
378 | + */ |
|
379 | + public function getLastRequestApiErrors() { |
|
380 | + return $this->lastRequestApiErrors; |
|
381 | + } |
|
382 | + |
|
383 | + /* ------------------------------------------------------------------- |
|
384 | 384 | * PUC filters and filter utilities |
385 | 385 | * ------------------------------------------------------------------- |
386 | 386 | */ |
387 | 387 | |
388 | - /** |
|
389 | - * Register a callback for one of the update checker filters. |
|
390 | - * |
|
391 | - * Identical to add_filter(), except it automatically adds the "puc_" prefix |
|
392 | - * and the "-$slug" suffix to the filter name. For example, "request_info_result" |
|
393 | - * becomes "puc_request_info_result-your_plugin_slug". |
|
394 | - * |
|
395 | - * @param string $tag |
|
396 | - * @param callable $callback |
|
397 | - * @param int $priority |
|
398 | - * @param int $acceptedArgs |
|
399 | - */ |
|
400 | - public function addFilter($tag, $callback, $priority = 10, $acceptedArgs = 1) { |
|
401 | - add_filter($this->getUniqueName($tag), $callback, $priority, $acceptedArgs); |
|
402 | - } |
|
403 | - |
|
404 | - /* ------------------------------------------------------------------- |
|
388 | + /** |
|
389 | + * Register a callback for one of the update checker filters. |
|
390 | + * |
|
391 | + * Identical to add_filter(), except it automatically adds the "puc_" prefix |
|
392 | + * and the "-$slug" suffix to the filter name. For example, "request_info_result" |
|
393 | + * becomes "puc_request_info_result-your_plugin_slug". |
|
394 | + * |
|
395 | + * @param string $tag |
|
396 | + * @param callable $callback |
|
397 | + * @param int $priority |
|
398 | + * @param int $acceptedArgs |
|
399 | + */ |
|
400 | + public function addFilter($tag, $callback, $priority = 10, $acceptedArgs = 1) { |
|
401 | + add_filter($this->getUniqueName($tag), $callback, $priority, $acceptedArgs); |
|
402 | + } |
|
403 | + |
|
404 | + /* ------------------------------------------------------------------- |
|
405 | 405 | * Inject updates |
406 | 406 | * ------------------------------------------------------------------- |
407 | 407 | */ |
408 | 408 | |
409 | - /** |
|
410 | - * Insert the latest update (if any) into the update list maintained by WP. |
|
411 | - * |
|
412 | - * @param stdClass $updates Update list. |
|
413 | - * @return stdClass Modified update list. |
|
414 | - */ |
|
415 | - public function injectUpdate($updates) { |
|
416 | - //Is there an update to insert? |
|
417 | - $update = $this->getUpdate(); |
|
418 | - |
|
419 | - if ( !$this->shouldShowUpdates() ) { |
|
420 | - $update = null; |
|
421 | - } |
|
422 | - |
|
423 | - if ( !empty($update) ) { |
|
424 | - //Let plugins filter the update info before it's passed on to WordPress. |
|
425 | - $update = apply_filters($this->getUniqueName('pre_inject_update'), $update); |
|
426 | - $updates = $this->addUpdateToList($updates, $update->toWpFormat()); |
|
427 | - } else { |
|
428 | - //Clean up any stale update info. |
|
429 | - $updates = $this->removeUpdateFromList($updates); |
|
430 | - } |
|
431 | - |
|
432 | - return $updates; |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * @param stdClass|null $updates |
|
437 | - * @param stdClass|array $updateToAdd |
|
438 | - * @return stdClass |
|
439 | - */ |
|
440 | - protected function addUpdateToList($updates, $updateToAdd) { |
|
441 | - if ( !is_object($updates) ) { |
|
442 | - $updates = new stdClass(); |
|
443 | - $updates->response = array(); |
|
444 | - } |
|
445 | - |
|
446 | - $updates->response[$this->getUpdateListKey()] = $updateToAdd; |
|
447 | - return $updates; |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * @param stdClass|null $updates |
|
452 | - * @return stdClass|null |
|
453 | - */ |
|
454 | - protected function removeUpdateFromList($updates) { |
|
455 | - if ( isset($updates, $updates->response) ) { |
|
456 | - unset($updates->response[$this->getUpdateListKey()]); |
|
457 | - } |
|
458 | - return $updates; |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Get the key that will be used when adding updates to the update list that's maintained |
|
463 | - * by the WordPress core. The list is always an associative array, but the key is different |
|
464 | - * for plugins and themes. |
|
465 | - * |
|
466 | - * @return string |
|
467 | - */ |
|
468 | - abstract protected function getUpdateListKey(); |
|
469 | - |
|
470 | - /** |
|
471 | - * Should we show available updates? |
|
472 | - * |
|
473 | - * Usually the answer is "yes", but there are exceptions. For example, WordPress doesn't |
|
474 | - * support automatic updates installation for mu-plugins, so PUC usually won't show update |
|
475 | - * notifications in that case. See the plugin-specific subclass for details. |
|
476 | - * |
|
477 | - * Note: This method only applies to updates that are displayed (or not) in the WordPress |
|
478 | - * admin. It doesn't affect APIs like requestUpdate and getUpdate. |
|
479 | - * |
|
480 | - * @return bool |
|
481 | - */ |
|
482 | - protected function shouldShowUpdates() { |
|
483 | - return true; |
|
484 | - } |
|
485 | - |
|
486 | - /* ------------------------------------------------------------------- |
|
409 | + /** |
|
410 | + * Insert the latest update (if any) into the update list maintained by WP. |
|
411 | + * |
|
412 | + * @param stdClass $updates Update list. |
|
413 | + * @return stdClass Modified update list. |
|
414 | + */ |
|
415 | + public function injectUpdate($updates) { |
|
416 | + //Is there an update to insert? |
|
417 | + $update = $this->getUpdate(); |
|
418 | + |
|
419 | + if ( !$this->shouldShowUpdates() ) { |
|
420 | + $update = null; |
|
421 | + } |
|
422 | + |
|
423 | + if ( !empty($update) ) { |
|
424 | + //Let plugins filter the update info before it's passed on to WordPress. |
|
425 | + $update = apply_filters($this->getUniqueName('pre_inject_update'), $update); |
|
426 | + $updates = $this->addUpdateToList($updates, $update->toWpFormat()); |
|
427 | + } else { |
|
428 | + //Clean up any stale update info. |
|
429 | + $updates = $this->removeUpdateFromList($updates); |
|
430 | + } |
|
431 | + |
|
432 | + return $updates; |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * @param stdClass|null $updates |
|
437 | + * @param stdClass|array $updateToAdd |
|
438 | + * @return stdClass |
|
439 | + */ |
|
440 | + protected function addUpdateToList($updates, $updateToAdd) { |
|
441 | + if ( !is_object($updates) ) { |
|
442 | + $updates = new stdClass(); |
|
443 | + $updates->response = array(); |
|
444 | + } |
|
445 | + |
|
446 | + $updates->response[$this->getUpdateListKey()] = $updateToAdd; |
|
447 | + return $updates; |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * @param stdClass|null $updates |
|
452 | + * @return stdClass|null |
|
453 | + */ |
|
454 | + protected function removeUpdateFromList($updates) { |
|
455 | + if ( isset($updates, $updates->response) ) { |
|
456 | + unset($updates->response[$this->getUpdateListKey()]); |
|
457 | + } |
|
458 | + return $updates; |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Get the key that will be used when adding updates to the update list that's maintained |
|
463 | + * by the WordPress core. The list is always an associative array, but the key is different |
|
464 | + * for plugins and themes. |
|
465 | + * |
|
466 | + * @return string |
|
467 | + */ |
|
468 | + abstract protected function getUpdateListKey(); |
|
469 | + |
|
470 | + /** |
|
471 | + * Should we show available updates? |
|
472 | + * |
|
473 | + * Usually the answer is "yes", but there are exceptions. For example, WordPress doesn't |
|
474 | + * support automatic updates installation for mu-plugins, so PUC usually won't show update |
|
475 | + * notifications in that case. See the plugin-specific subclass for details. |
|
476 | + * |
|
477 | + * Note: This method only applies to updates that are displayed (or not) in the WordPress |
|
478 | + * admin. It doesn't affect APIs like requestUpdate and getUpdate. |
|
479 | + * |
|
480 | + * @return bool |
|
481 | + */ |
|
482 | + protected function shouldShowUpdates() { |
|
483 | + return true; |
|
484 | + } |
|
485 | + |
|
486 | + /* ------------------------------------------------------------------- |
|
487 | 487 | * JSON-based update API |
488 | 488 | * ------------------------------------------------------------------- |
489 | 489 | */ |
490 | 490 | |
491 | - /** |
|
492 | - * Retrieve plugin or theme metadata from the JSON document at $this->metadataUrl. |
|
493 | - * |
|
494 | - * @param string $metaClass Parse the JSON as an instance of this class. It must have a static fromJson method. |
|
495 | - * @param string $filterRoot |
|
496 | - * @param array $queryArgs Additional query arguments. |
|
497 | - * @return array [Puc_v4p4_Metadata|null, array|WP_Error] A metadata instance and the value returned by wp_remote_get(). |
|
498 | - */ |
|
499 | - protected function requestMetadata($metaClass, $filterRoot, $queryArgs = array()) { |
|
500 | - //Query args to append to the URL. Plugins can add their own by using a filter callback (see addQueryArgFilter()). |
|
501 | - $queryArgs = array_merge( |
|
502 | - array( |
|
503 | - 'installed_version' => strval($this->getInstalledVersion()), |
|
504 | - 'php' => phpversion(), |
|
505 | - 'locale' => get_locale(), |
|
506 | - ), |
|
507 | - $queryArgs |
|
508 | - ); |
|
509 | - $queryArgs = apply_filters($this->getUniqueName($filterRoot . '_query_args'), $queryArgs); |
|
510 | - |
|
511 | - //Various options for the wp_remote_get() call. Plugins can filter these, too. |
|
512 | - $options = array( |
|
513 | - 'timeout' => 10, //seconds |
|
514 | - 'headers' => array( |
|
515 | - 'Accept' => 'application/json', |
|
516 | - ), |
|
517 | - ); |
|
518 | - $options = apply_filters($this->getUniqueName($filterRoot . '_options'), $options); |
|
519 | - |
|
520 | - //The metadata file should be at 'http://your-api.com/url/here/$slug/info.json' |
|
521 | - $url = $this->metadataUrl; |
|
522 | - if ( !empty($queryArgs) ){ |
|
523 | - $url = add_query_arg($queryArgs, $url); |
|
524 | - } |
|
525 | - |
|
526 | - $result = wp_remote_get($url, $options); |
|
527 | - |
|
528 | - $result = apply_filters($this->getUniqueName('request_metadata_http_result'), $result, $url, $options); |
|
491 | + /** |
|
492 | + * Retrieve plugin or theme metadata from the JSON document at $this->metadataUrl. |
|
493 | + * |
|
494 | + * @param string $metaClass Parse the JSON as an instance of this class. It must have a static fromJson method. |
|
495 | + * @param string $filterRoot |
|
496 | + * @param array $queryArgs Additional query arguments. |
|
497 | + * @return array [Puc_v4p4_Metadata|null, array|WP_Error] A metadata instance and the value returned by wp_remote_get(). |
|
498 | + */ |
|
499 | + protected function requestMetadata($metaClass, $filterRoot, $queryArgs = array()) { |
|
500 | + //Query args to append to the URL. Plugins can add their own by using a filter callback (see addQueryArgFilter()). |
|
501 | + $queryArgs = array_merge( |
|
502 | + array( |
|
503 | + 'installed_version' => strval($this->getInstalledVersion()), |
|
504 | + 'php' => phpversion(), |
|
505 | + 'locale' => get_locale(), |
|
506 | + ), |
|
507 | + $queryArgs |
|
508 | + ); |
|
509 | + $queryArgs = apply_filters($this->getUniqueName($filterRoot . '_query_args'), $queryArgs); |
|
510 | + |
|
511 | + //Various options for the wp_remote_get() call. Plugins can filter these, too. |
|
512 | + $options = array( |
|
513 | + 'timeout' => 10, //seconds |
|
514 | + 'headers' => array( |
|
515 | + 'Accept' => 'application/json', |
|
516 | + ), |
|
517 | + ); |
|
518 | + $options = apply_filters($this->getUniqueName($filterRoot . '_options'), $options); |
|
519 | + |
|
520 | + //The metadata file should be at 'http://your-api.com/url/here/$slug/info.json' |
|
521 | + $url = $this->metadataUrl; |
|
522 | + if ( !empty($queryArgs) ){ |
|
523 | + $url = add_query_arg($queryArgs, $url); |
|
524 | + } |
|
525 | + |
|
526 | + $result = wp_remote_get($url, $options); |
|
527 | + |
|
528 | + $result = apply_filters($this->getUniqueName('request_metadata_http_result'), $result, $url, $options); |
|
529 | 529 | |
530 | - //Try to parse the response |
|
531 | - $status = $this->validateApiResponse($result); |
|
532 | - $metadata = null; |
|
533 | - if ( !is_wp_error($status) ){ |
|
534 | - $metadata = call_user_func(array($metaClass, 'fromJson'), $result['body']); |
|
535 | - } else { |
|
536 | - do_action('puc_api_error', $status, $result, $url, $this->slug); |
|
537 | - $this->triggerError( |
|
538 | - sprintf('The URL %s does not point to a valid metadata file. ', $url) |
|
539 | - . $status->get_error_message(), |
|
540 | - E_USER_WARNING |
|
541 | - ); |
|
542 | - } |
|
543 | - |
|
544 | - return array($metadata, $result); |
|
545 | - } |
|
546 | - |
|
547 | - /** |
|
548 | - * Check if $result is a successful update API response. |
|
549 | - * |
|
550 | - * @param array|WP_Error $result |
|
551 | - * @return true|WP_Error |
|
552 | - */ |
|
553 | - protected function validateApiResponse($result) { |
|
554 | - if ( is_wp_error($result) ) { /** @var WP_Error $result */ |
|
555 | - return new WP_Error($result->get_error_code(), 'WP HTTP Error: ' . $result->get_error_message()); |
|
556 | - } |
|
557 | - |
|
558 | - if ( !isset($result['response']['code']) ) { |
|
559 | - return new WP_Error( |
|
560 | - 'puc_no_response_code', |
|
561 | - 'wp_remote_get() returned an unexpected result.' |
|
562 | - ); |
|
563 | - } |
|
564 | - |
|
565 | - if ( $result['response']['code'] !== 200 ) { |
|
566 | - return new WP_Error( |
|
567 | - 'puc_unexpected_response_code', |
|
568 | - 'HTTP response code is ' . $result['response']['code'] . ' (expected: 200)' |
|
569 | - ); |
|
570 | - } |
|
571 | - |
|
572 | - if ( empty($result['body']) ) { |
|
573 | - return new WP_Error('puc_empty_response', 'The metadata file appears to be empty.'); |
|
574 | - } |
|
575 | - |
|
576 | - return true; |
|
577 | - } |
|
578 | - |
|
579 | - /* ------------------------------------------------------------------- |
|
530 | + //Try to parse the response |
|
531 | + $status = $this->validateApiResponse($result); |
|
532 | + $metadata = null; |
|
533 | + if ( !is_wp_error($status) ){ |
|
534 | + $metadata = call_user_func(array($metaClass, 'fromJson'), $result['body']); |
|
535 | + } else { |
|
536 | + do_action('puc_api_error', $status, $result, $url, $this->slug); |
|
537 | + $this->triggerError( |
|
538 | + sprintf('The URL %s does not point to a valid metadata file. ', $url) |
|
539 | + . $status->get_error_message(), |
|
540 | + E_USER_WARNING |
|
541 | + ); |
|
542 | + } |
|
543 | + |
|
544 | + return array($metadata, $result); |
|
545 | + } |
|
546 | + |
|
547 | + /** |
|
548 | + * Check if $result is a successful update API response. |
|
549 | + * |
|
550 | + * @param array|WP_Error $result |
|
551 | + * @return true|WP_Error |
|
552 | + */ |
|
553 | + protected function validateApiResponse($result) { |
|
554 | + if ( is_wp_error($result) ) { /** @var WP_Error $result */ |
|
555 | + return new WP_Error($result->get_error_code(), 'WP HTTP Error: ' . $result->get_error_message()); |
|
556 | + } |
|
557 | + |
|
558 | + if ( !isset($result['response']['code']) ) { |
|
559 | + return new WP_Error( |
|
560 | + 'puc_no_response_code', |
|
561 | + 'wp_remote_get() returned an unexpected result.' |
|
562 | + ); |
|
563 | + } |
|
564 | + |
|
565 | + if ( $result['response']['code'] !== 200 ) { |
|
566 | + return new WP_Error( |
|
567 | + 'puc_unexpected_response_code', |
|
568 | + 'HTTP response code is ' . $result['response']['code'] . ' (expected: 200)' |
|
569 | + ); |
|
570 | + } |
|
571 | + |
|
572 | + if ( empty($result['body']) ) { |
|
573 | + return new WP_Error('puc_empty_response', 'The metadata file appears to be empty.'); |
|
574 | + } |
|
575 | + |
|
576 | + return true; |
|
577 | + } |
|
578 | + |
|
579 | + /* ------------------------------------------------------------------- |
|
580 | 580 | * Language packs / Translation updates |
581 | 581 | * ------------------------------------------------------------------- |
582 | 582 | */ |
583 | 583 | |
584 | - /** |
|
585 | - * Filter a list of translation updates and return a new list that contains only updates |
|
586 | - * that apply to the current site. |
|
587 | - * |
|
588 | - * @param array $translations |
|
589 | - * @return array |
|
590 | - */ |
|
591 | - protected function filterApplicableTranslations($translations) { |
|
592 | - $languages = array_flip(array_values(get_available_languages())); |
|
593 | - $installedTranslations = $this->getInstalledTranslations(); |
|
594 | - |
|
595 | - $applicableTranslations = array(); |
|
596 | - foreach($translations as $translation) { |
|
597 | - //Does it match one of the available core languages? |
|
598 | - $isApplicable = array_key_exists($translation->language, $languages); |
|
599 | - //Is it more recent than an already-installed translation? |
|
600 | - if ( isset($installedTranslations[$translation->language]) ) { |
|
601 | - $updateTimestamp = strtotime($translation->updated); |
|
602 | - $installedTimestamp = strtotime($installedTranslations[$translation->language]['PO-Revision-Date']); |
|
603 | - $isApplicable = $updateTimestamp > $installedTimestamp; |
|
604 | - } |
|
605 | - |
|
606 | - if ( $isApplicable ) { |
|
607 | - $applicableTranslations[] = $translation; |
|
608 | - } |
|
609 | - } |
|
610 | - |
|
611 | - return $applicableTranslations; |
|
612 | - } |
|
613 | - |
|
614 | - /** |
|
615 | - * Get a list of installed translations for this plugin or theme. |
|
616 | - * |
|
617 | - * @return array |
|
618 | - */ |
|
619 | - protected function getInstalledTranslations() { |
|
620 | - if ( !function_exists('wp_get_installed_translations') ) { |
|
621 | - return array(); |
|
622 | - } |
|
623 | - $installedTranslations = wp_get_installed_translations($this->translationType . 's'); |
|
624 | - if ( isset($installedTranslations[$this->directoryName]) ) { |
|
625 | - $installedTranslations = $installedTranslations[$this->directoryName]; |
|
626 | - } else { |
|
627 | - $installedTranslations = array(); |
|
628 | - } |
|
629 | - return $installedTranslations; |
|
630 | - } |
|
631 | - |
|
632 | - /** |
|
633 | - * Insert translation updates into the list maintained by WordPress. |
|
634 | - * |
|
635 | - * @param stdClass $updates |
|
636 | - * @return stdClass |
|
637 | - */ |
|
638 | - public function injectTranslationUpdates($updates) { |
|
639 | - $translationUpdates = $this->getTranslationUpdates(); |
|
640 | - if ( empty($translationUpdates) ) { |
|
641 | - return $updates; |
|
642 | - } |
|
643 | - |
|
644 | - //Being defensive. |
|
645 | - if ( !is_object($updates) ) { |
|
646 | - $updates = new stdClass(); |
|
647 | - } |
|
648 | - if ( !isset($updates->translations) ) { |
|
649 | - $updates->translations = array(); |
|
650 | - } |
|
651 | - |
|
652 | - //In case there's a name collision with a plugin or theme hosted on wordpress.org, |
|
653 | - //remove any preexisting updates that match our thing. |
|
654 | - $updates->translations = array_values(array_filter( |
|
655 | - $updates->translations, |
|
656 | - array($this, 'isNotMyTranslation') |
|
657 | - )); |
|
658 | - |
|
659 | - //Add our updates to the list. |
|
660 | - foreach($translationUpdates as $update) { |
|
661 | - $convertedUpdate = array_merge( |
|
662 | - array( |
|
663 | - 'type' => $this->translationType, |
|
664 | - 'slug' => $this->directoryName, |
|
665 | - 'autoupdate' => 0, |
|
666 | - //AFAICT, WordPress doesn't actually use the "version" field for anything. |
|
667 | - //But lets make sure it's there, just in case. |
|
668 | - 'version' => isset($update->version) ? $update->version : ('1.' . strtotime($update->updated)), |
|
669 | - ), |
|
670 | - (array)$update |
|
671 | - ); |
|
672 | - |
|
673 | - $updates->translations[] = $convertedUpdate; |
|
674 | - } |
|
675 | - |
|
676 | - return $updates; |
|
677 | - } |
|
678 | - |
|
679 | - /** |
|
680 | - * Get a list of available translation updates. |
|
681 | - * |
|
682 | - * This method will return an empty array if there are no updates. |
|
683 | - * Uses cached update data. |
|
684 | - * |
|
685 | - * @return array |
|
686 | - */ |
|
687 | - public function getTranslationUpdates() { |
|
688 | - return $this->updateState->getTranslations(); |
|
689 | - } |
|
690 | - |
|
691 | - /** |
|
692 | - * Remove all cached translation updates. |
|
693 | - * |
|
694 | - * @see wp_clean_update_cache |
|
695 | - */ |
|
696 | - public function clearCachedTranslationUpdates() { |
|
697 | - $this->updateState->setTranslations(array()); |
|
698 | - } |
|
699 | - |
|
700 | - /** |
|
701 | - * Filter callback. Keeps only translations that *don't* match this plugin or theme. |
|
702 | - * |
|
703 | - * @param array $translation |
|
704 | - * @return bool |
|
705 | - */ |
|
706 | - protected function isNotMyTranslation($translation) { |
|
707 | - $isMatch = isset($translation['type'], $translation['slug']) |
|
708 | - && ($translation['type'] === $this->translationType) |
|
709 | - && ($translation['slug'] === $this->directoryName); |
|
710 | - |
|
711 | - return !$isMatch; |
|
712 | - } |
|
713 | - |
|
714 | - /* ------------------------------------------------------------------- |
|
584 | + /** |
|
585 | + * Filter a list of translation updates and return a new list that contains only updates |
|
586 | + * that apply to the current site. |
|
587 | + * |
|
588 | + * @param array $translations |
|
589 | + * @return array |
|
590 | + */ |
|
591 | + protected function filterApplicableTranslations($translations) { |
|
592 | + $languages = array_flip(array_values(get_available_languages())); |
|
593 | + $installedTranslations = $this->getInstalledTranslations(); |
|
594 | + |
|
595 | + $applicableTranslations = array(); |
|
596 | + foreach($translations as $translation) { |
|
597 | + //Does it match one of the available core languages? |
|
598 | + $isApplicable = array_key_exists($translation->language, $languages); |
|
599 | + //Is it more recent than an already-installed translation? |
|
600 | + if ( isset($installedTranslations[$translation->language]) ) { |
|
601 | + $updateTimestamp = strtotime($translation->updated); |
|
602 | + $installedTimestamp = strtotime($installedTranslations[$translation->language]['PO-Revision-Date']); |
|
603 | + $isApplicable = $updateTimestamp > $installedTimestamp; |
|
604 | + } |
|
605 | + |
|
606 | + if ( $isApplicable ) { |
|
607 | + $applicableTranslations[] = $translation; |
|
608 | + } |
|
609 | + } |
|
610 | + |
|
611 | + return $applicableTranslations; |
|
612 | + } |
|
613 | + |
|
614 | + /** |
|
615 | + * Get a list of installed translations for this plugin or theme. |
|
616 | + * |
|
617 | + * @return array |
|
618 | + */ |
|
619 | + protected function getInstalledTranslations() { |
|
620 | + if ( !function_exists('wp_get_installed_translations') ) { |
|
621 | + return array(); |
|
622 | + } |
|
623 | + $installedTranslations = wp_get_installed_translations($this->translationType . 's'); |
|
624 | + if ( isset($installedTranslations[$this->directoryName]) ) { |
|
625 | + $installedTranslations = $installedTranslations[$this->directoryName]; |
|
626 | + } else { |
|
627 | + $installedTranslations = array(); |
|
628 | + } |
|
629 | + return $installedTranslations; |
|
630 | + } |
|
631 | + |
|
632 | + /** |
|
633 | + * Insert translation updates into the list maintained by WordPress. |
|
634 | + * |
|
635 | + * @param stdClass $updates |
|
636 | + * @return stdClass |
|
637 | + */ |
|
638 | + public function injectTranslationUpdates($updates) { |
|
639 | + $translationUpdates = $this->getTranslationUpdates(); |
|
640 | + if ( empty($translationUpdates) ) { |
|
641 | + return $updates; |
|
642 | + } |
|
643 | + |
|
644 | + //Being defensive. |
|
645 | + if ( !is_object($updates) ) { |
|
646 | + $updates = new stdClass(); |
|
647 | + } |
|
648 | + if ( !isset($updates->translations) ) { |
|
649 | + $updates->translations = array(); |
|
650 | + } |
|
651 | + |
|
652 | + //In case there's a name collision with a plugin or theme hosted on wordpress.org, |
|
653 | + //remove any preexisting updates that match our thing. |
|
654 | + $updates->translations = array_values(array_filter( |
|
655 | + $updates->translations, |
|
656 | + array($this, 'isNotMyTranslation') |
|
657 | + )); |
|
658 | + |
|
659 | + //Add our updates to the list. |
|
660 | + foreach($translationUpdates as $update) { |
|
661 | + $convertedUpdate = array_merge( |
|
662 | + array( |
|
663 | + 'type' => $this->translationType, |
|
664 | + 'slug' => $this->directoryName, |
|
665 | + 'autoupdate' => 0, |
|
666 | + //AFAICT, WordPress doesn't actually use the "version" field for anything. |
|
667 | + //But lets make sure it's there, just in case. |
|
668 | + 'version' => isset($update->version) ? $update->version : ('1.' . strtotime($update->updated)), |
|
669 | + ), |
|
670 | + (array)$update |
|
671 | + ); |
|
672 | + |
|
673 | + $updates->translations[] = $convertedUpdate; |
|
674 | + } |
|
675 | + |
|
676 | + return $updates; |
|
677 | + } |
|
678 | + |
|
679 | + /** |
|
680 | + * Get a list of available translation updates. |
|
681 | + * |
|
682 | + * This method will return an empty array if there are no updates. |
|
683 | + * Uses cached update data. |
|
684 | + * |
|
685 | + * @return array |
|
686 | + */ |
|
687 | + public function getTranslationUpdates() { |
|
688 | + return $this->updateState->getTranslations(); |
|
689 | + } |
|
690 | + |
|
691 | + /** |
|
692 | + * Remove all cached translation updates. |
|
693 | + * |
|
694 | + * @see wp_clean_update_cache |
|
695 | + */ |
|
696 | + public function clearCachedTranslationUpdates() { |
|
697 | + $this->updateState->setTranslations(array()); |
|
698 | + } |
|
699 | + |
|
700 | + /** |
|
701 | + * Filter callback. Keeps only translations that *don't* match this plugin or theme. |
|
702 | + * |
|
703 | + * @param array $translation |
|
704 | + * @return bool |
|
705 | + */ |
|
706 | + protected function isNotMyTranslation($translation) { |
|
707 | + $isMatch = isset($translation['type'], $translation['slug']) |
|
708 | + && ($translation['type'] === $this->translationType) |
|
709 | + && ($translation['slug'] === $this->directoryName); |
|
710 | + |
|
711 | + return !$isMatch; |
|
712 | + } |
|
713 | + |
|
714 | + /* ------------------------------------------------------------------- |
|
715 | 715 | * Fix directory name when installing updates |
716 | 716 | * ------------------------------------------------------------------- |
717 | 717 | */ |
718 | 718 | |
719 | - /** |
|
720 | - * Rename the update directory to match the existing plugin/theme directory. |
|
721 | - * |
|
722 | - * When WordPress installs a plugin or theme update, it assumes that the ZIP file will contain |
|
723 | - * exactly one directory, and that the directory name will be the same as the directory where |
|
724 | - * the plugin or theme is currently installed. |
|
725 | - * |
|
726 | - * GitHub and other repositories provide ZIP downloads, but they often use directory names like |
|
727 | - * "project-branch" or "project-tag-hash". We need to change the name to the actual plugin folder. |
|
728 | - * |
|
729 | - * This is a hook callback. Don't call it from a plugin. |
|
730 | - * |
|
731 | - * @access protected |
|
732 | - * |
|
733 | - * @param string $source The directory to copy to /wp-content/plugins or /wp-content/themes. Usually a subdirectory of $remoteSource. |
|
734 | - * @param string $remoteSource WordPress has extracted the update to this directory. |
|
735 | - * @param WP_Upgrader $upgrader |
|
736 | - * @return string|WP_Error |
|
737 | - */ |
|
738 | - public function fixDirectoryName($source, $remoteSource, $upgrader) { |
|
739 | - global $wp_filesystem; |
|
740 | - /** @var WP_Filesystem_Base $wp_filesystem */ |
|
741 | - |
|
742 | - //Basic sanity checks. |
|
743 | - if ( !isset($source, $remoteSource, $upgrader, $upgrader->skin, $wp_filesystem) ) { |
|
744 | - return $source; |
|
745 | - } |
|
746 | - |
|
747 | - //If WordPress is upgrading anything other than our plugin/theme, leave the directory name unchanged. |
|
748 | - if ( !$this->isBeingUpgraded($upgrader) ) { |
|
749 | - return $source; |
|
750 | - } |
|
751 | - |
|
752 | - //Rename the source to match the existing directory. |
|
753 | - $correctedSource = trailingslashit($remoteSource) . $this->directoryName . '/'; |
|
754 | - if ( $source !== $correctedSource ) { |
|
755 | - //The update archive should contain a single directory that contains the rest of plugin/theme files. |
|
756 | - //Otherwise, WordPress will try to copy the entire working directory ($source == $remoteSource). |
|
757 | - //We can't rename $remoteSource because that would break WordPress code that cleans up temporary files |
|
758 | - //after update. |
|
759 | - if ( $this->isBadDirectoryStructure($remoteSource) ) { |
|
760 | - return new WP_Error( |
|
761 | - 'puc-incorrect-directory-structure', |
|
762 | - sprintf( |
|
763 | - 'The directory structure of the update is incorrect. All files should be inside ' . |
|
764 | - 'a directory named <span class="code">%s</span>, not at the root of the ZIP archive.', |
|
765 | - htmlentities($this->slug) |
|
766 | - ) |
|
767 | - ); |
|
768 | - } |
|
769 | - |
|
770 | - /** @var WP_Upgrader_Skin $upgrader ->skin */ |
|
771 | - $upgrader->skin->feedback(sprintf( |
|
772 | - 'Renaming %s to %s…', |
|
773 | - '<span class="code">' . basename($source) . '</span>', |
|
774 | - '<span class="code">' . $this->directoryName . '</span>' |
|
775 | - )); |
|
776 | - |
|
777 | - if ( $wp_filesystem->move($source, $correctedSource, true) ) { |
|
778 | - $upgrader->skin->feedback('Directory successfully renamed.'); |
|
779 | - return $correctedSource; |
|
780 | - } else { |
|
781 | - return new WP_Error( |
|
782 | - 'puc-rename-failed', |
|
783 | - 'Unable to rename the update to match the existing directory.' |
|
784 | - ); |
|
785 | - } |
|
786 | - } |
|
787 | - |
|
788 | - return $source; |
|
789 | - } |
|
790 | - |
|
791 | - /** |
|
792 | - * Is there an update being installed right now, for this plugin or theme? |
|
793 | - * |
|
794 | - * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update. |
|
795 | - * @return bool |
|
796 | - */ |
|
797 | - abstract public function isBeingUpgraded($upgrader = null); |
|
798 | - |
|
799 | - /** |
|
800 | - * Check for incorrect update directory structure. An update must contain a single directory, |
|
801 | - * all other files should be inside that directory. |
|
802 | - * |
|
803 | - * @param string $remoteSource Directory path. |
|
804 | - * @return bool |
|
805 | - */ |
|
806 | - protected function isBadDirectoryStructure($remoteSource) { |
|
807 | - global $wp_filesystem; |
|
808 | - /** @var WP_Filesystem_Base $wp_filesystem */ |
|
809 | - |
|
810 | - $sourceFiles = $wp_filesystem->dirlist($remoteSource); |
|
811 | - if ( is_array($sourceFiles) ) { |
|
812 | - $sourceFiles = array_keys($sourceFiles); |
|
813 | - $firstFilePath = trailingslashit($remoteSource) . $sourceFiles[0]; |
|
814 | - return (count($sourceFiles) > 1) || (!$wp_filesystem->is_dir($firstFilePath)); |
|
815 | - } |
|
816 | - |
|
817 | - //Assume it's fine. |
|
818 | - return false; |
|
819 | - } |
|
820 | - |
|
821 | - /* ------------------------------------------------------------------- |
|
719 | + /** |
|
720 | + * Rename the update directory to match the existing plugin/theme directory. |
|
721 | + * |
|
722 | + * When WordPress installs a plugin or theme update, it assumes that the ZIP file will contain |
|
723 | + * exactly one directory, and that the directory name will be the same as the directory where |
|
724 | + * the plugin or theme is currently installed. |
|
725 | + * |
|
726 | + * GitHub and other repositories provide ZIP downloads, but they often use directory names like |
|
727 | + * "project-branch" or "project-tag-hash". We need to change the name to the actual plugin folder. |
|
728 | + * |
|
729 | + * This is a hook callback. Don't call it from a plugin. |
|
730 | + * |
|
731 | + * @access protected |
|
732 | + * |
|
733 | + * @param string $source The directory to copy to /wp-content/plugins or /wp-content/themes. Usually a subdirectory of $remoteSource. |
|
734 | + * @param string $remoteSource WordPress has extracted the update to this directory. |
|
735 | + * @param WP_Upgrader $upgrader |
|
736 | + * @return string|WP_Error |
|
737 | + */ |
|
738 | + public function fixDirectoryName($source, $remoteSource, $upgrader) { |
|
739 | + global $wp_filesystem; |
|
740 | + /** @var WP_Filesystem_Base $wp_filesystem */ |
|
741 | + |
|
742 | + //Basic sanity checks. |
|
743 | + if ( !isset($source, $remoteSource, $upgrader, $upgrader->skin, $wp_filesystem) ) { |
|
744 | + return $source; |
|
745 | + } |
|
746 | + |
|
747 | + //If WordPress is upgrading anything other than our plugin/theme, leave the directory name unchanged. |
|
748 | + if ( !$this->isBeingUpgraded($upgrader) ) { |
|
749 | + return $source; |
|
750 | + } |
|
751 | + |
|
752 | + //Rename the source to match the existing directory. |
|
753 | + $correctedSource = trailingslashit($remoteSource) . $this->directoryName . '/'; |
|
754 | + if ( $source !== $correctedSource ) { |
|
755 | + //The update archive should contain a single directory that contains the rest of plugin/theme files. |
|
756 | + //Otherwise, WordPress will try to copy the entire working directory ($source == $remoteSource). |
|
757 | + //We can't rename $remoteSource because that would break WordPress code that cleans up temporary files |
|
758 | + //after update. |
|
759 | + if ( $this->isBadDirectoryStructure($remoteSource) ) { |
|
760 | + return new WP_Error( |
|
761 | + 'puc-incorrect-directory-structure', |
|
762 | + sprintf( |
|
763 | + 'The directory structure of the update is incorrect. All files should be inside ' . |
|
764 | + 'a directory named <span class="code">%s</span>, not at the root of the ZIP archive.', |
|
765 | + htmlentities($this->slug) |
|
766 | + ) |
|
767 | + ); |
|
768 | + } |
|
769 | + |
|
770 | + /** @var WP_Upgrader_Skin $upgrader ->skin */ |
|
771 | + $upgrader->skin->feedback(sprintf( |
|
772 | + 'Renaming %s to %s…', |
|
773 | + '<span class="code">' . basename($source) . '</span>', |
|
774 | + '<span class="code">' . $this->directoryName . '</span>' |
|
775 | + )); |
|
776 | + |
|
777 | + if ( $wp_filesystem->move($source, $correctedSource, true) ) { |
|
778 | + $upgrader->skin->feedback('Directory successfully renamed.'); |
|
779 | + return $correctedSource; |
|
780 | + } else { |
|
781 | + return new WP_Error( |
|
782 | + 'puc-rename-failed', |
|
783 | + 'Unable to rename the update to match the existing directory.' |
|
784 | + ); |
|
785 | + } |
|
786 | + } |
|
787 | + |
|
788 | + return $source; |
|
789 | + } |
|
790 | + |
|
791 | + /** |
|
792 | + * Is there an update being installed right now, for this plugin or theme? |
|
793 | + * |
|
794 | + * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update. |
|
795 | + * @return bool |
|
796 | + */ |
|
797 | + abstract public function isBeingUpgraded($upgrader = null); |
|
798 | + |
|
799 | + /** |
|
800 | + * Check for incorrect update directory structure. An update must contain a single directory, |
|
801 | + * all other files should be inside that directory. |
|
802 | + * |
|
803 | + * @param string $remoteSource Directory path. |
|
804 | + * @return bool |
|
805 | + */ |
|
806 | + protected function isBadDirectoryStructure($remoteSource) { |
|
807 | + global $wp_filesystem; |
|
808 | + /** @var WP_Filesystem_Base $wp_filesystem */ |
|
809 | + |
|
810 | + $sourceFiles = $wp_filesystem->dirlist($remoteSource); |
|
811 | + if ( is_array($sourceFiles) ) { |
|
812 | + $sourceFiles = array_keys($sourceFiles); |
|
813 | + $firstFilePath = trailingslashit($remoteSource) . $sourceFiles[0]; |
|
814 | + return (count($sourceFiles) > 1) || (!$wp_filesystem->is_dir($firstFilePath)); |
|
815 | + } |
|
816 | + |
|
817 | + //Assume it's fine. |
|
818 | + return false; |
|
819 | + } |
|
820 | + |
|
821 | + /* ------------------------------------------------------------------- |
|
822 | 822 | * File header parsing |
823 | 823 | * ------------------------------------------------------------------- |
824 | 824 | */ |
825 | 825 | |
826 | - /** |
|
827 | - * Parse plugin or theme metadata from the header comment. |
|
828 | - * |
|
829 | - * This is basically a simplified version of the get_file_data() function from /wp-includes/functions.php. |
|
830 | - * It's intended as a utility for subclasses that detect updates by parsing files in a VCS. |
|
831 | - * |
|
832 | - * @param string|null $content File contents. |
|
833 | - * @return string[] |
|
834 | - */ |
|
835 | - public function getFileHeader($content) { |
|
836 | - $content = (string) $content; |
|
837 | - |
|
838 | - //WordPress only looks at the first 8 KiB of the file, so we do the same. |
|
839 | - $content = substr($content, 0, 8192); |
|
840 | - //Normalize line endings. |
|
841 | - $content = str_replace("\r", "\n", $content); |
|
842 | - |
|
843 | - $headers = $this->getHeaderNames(); |
|
844 | - $results = array(); |
|
845 | - foreach ($headers as $field => $name) { |
|
846 | - $success = preg_match('/^[ \t\/*#@]*' . preg_quote($name, '/') . ':(.*)$/mi', $content, $matches); |
|
847 | - |
|
848 | - if ( ($success === 1) && $matches[1] ) { |
|
849 | - $value = $matches[1]; |
|
850 | - if ( function_exists('_cleanup_header_comment') ) { |
|
851 | - $value = _cleanup_header_comment($value); |
|
852 | - } |
|
853 | - $results[$field] = $value; |
|
854 | - } else { |
|
855 | - $results[$field] = ''; |
|
856 | - } |
|
857 | - } |
|
858 | - |
|
859 | - return $results; |
|
860 | - } |
|
861 | - |
|
862 | - /** |
|
863 | - * @return array Format: ['HeaderKey' => 'Header Name'] |
|
864 | - */ |
|
865 | - abstract protected function getHeaderNames(); |
|
866 | - |
|
867 | - /* ------------------------------------------------------------------- |
|
826 | + /** |
|
827 | + * Parse plugin or theme metadata from the header comment. |
|
828 | + * |
|
829 | + * This is basically a simplified version of the get_file_data() function from /wp-includes/functions.php. |
|
830 | + * It's intended as a utility for subclasses that detect updates by parsing files in a VCS. |
|
831 | + * |
|
832 | + * @param string|null $content File contents. |
|
833 | + * @return string[] |
|
834 | + */ |
|
835 | + public function getFileHeader($content) { |
|
836 | + $content = (string) $content; |
|
837 | + |
|
838 | + //WordPress only looks at the first 8 KiB of the file, so we do the same. |
|
839 | + $content = substr($content, 0, 8192); |
|
840 | + //Normalize line endings. |
|
841 | + $content = str_replace("\r", "\n", $content); |
|
842 | + |
|
843 | + $headers = $this->getHeaderNames(); |
|
844 | + $results = array(); |
|
845 | + foreach ($headers as $field => $name) { |
|
846 | + $success = preg_match('/^[ \t\/*#@]*' . preg_quote($name, '/') . ':(.*)$/mi', $content, $matches); |
|
847 | + |
|
848 | + if ( ($success === 1) && $matches[1] ) { |
|
849 | + $value = $matches[1]; |
|
850 | + if ( function_exists('_cleanup_header_comment') ) { |
|
851 | + $value = _cleanup_header_comment($value); |
|
852 | + } |
|
853 | + $results[$field] = $value; |
|
854 | + } else { |
|
855 | + $results[$field] = ''; |
|
856 | + } |
|
857 | + } |
|
858 | + |
|
859 | + return $results; |
|
860 | + } |
|
861 | + |
|
862 | + /** |
|
863 | + * @return array Format: ['HeaderKey' => 'Header Name'] |
|
864 | + */ |
|
865 | + abstract protected function getHeaderNames(); |
|
866 | + |
|
867 | + /* ------------------------------------------------------------------- |
|
868 | 868 | * DebugBar integration |
869 | 869 | * ------------------------------------------------------------------- |
870 | 870 | */ |
871 | 871 | |
872 | - /** |
|
873 | - * Initialize the update checker Debug Bar plugin/add-on thingy. |
|
874 | - */ |
|
875 | - public function maybeInitDebugBar() { |
|
876 | - if ( class_exists('Debug_Bar', false) && file_exists(dirname(__FILE__ . '/DebugBar')) ) { |
|
877 | - $this->createDebugBarExtension(); |
|
878 | - } |
|
879 | - } |
|
880 | - |
|
881 | - protected function createDebugBarExtension() { |
|
882 | - return new Puc_v4p4_DebugBar_Extension($this); |
|
883 | - } |
|
884 | - |
|
885 | - /** |
|
886 | - * Display additional configuration details in the Debug Bar panel. |
|
887 | - * |
|
888 | - * @param Puc_v4p4_DebugBar_Panel $panel |
|
889 | - */ |
|
890 | - public function onDisplayConfiguration($panel) { |
|
891 | - //Do nothing. Subclasses can use this to add additional info to the panel. |
|
892 | - } |
|
893 | - |
|
894 | - } |
|
872 | + /** |
|
873 | + * Initialize the update checker Debug Bar plugin/add-on thingy. |
|
874 | + */ |
|
875 | + public function maybeInitDebugBar() { |
|
876 | + if ( class_exists('Debug_Bar', false) && file_exists(dirname(__FILE__ . '/DebugBar')) ) { |
|
877 | + $this->createDebugBarExtension(); |
|
878 | + } |
|
879 | + } |
|
880 | + |
|
881 | + protected function createDebugBarExtension() { |
|
882 | + return new Puc_v4p4_DebugBar_Extension($this); |
|
883 | + } |
|
884 | + |
|
885 | + /** |
|
886 | + * Display additional configuration details in the Debug Bar panel. |
|
887 | + * |
|
888 | + * @param Puc_v4p4_DebugBar_Panel $panel |
|
889 | + */ |
|
890 | + public function onDisplayConfiguration($panel) { |
|
891 | + //Do nothing. Subclasses can use this to add additional info to the panel. |
|
892 | + } |
|
893 | + |
|
894 | + } |
|
895 | 895 | |
896 | 896 | endif; |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if ( !class_exists('Puc_v4p4_UpdateChecker', false) ): |
|
3 | +if (!class_exists('Puc_v4p4_UpdateChecker', false)): |
|
4 | 4 | |
5 | 5 | abstract class Puc_v4p4_UpdateChecker { |
6 | 6 | protected $filterSuffix = ''; |
@@ -56,17 +56,17 @@ discard block |
||
56 | 56 | protected $lastRequestApiErrors = array(); |
57 | 57 | |
58 | 58 | public function __construct($metadataUrl, $directoryName, $slug = null, $checkPeriod = 12, $optionName = '') { |
59 | - $this->debugMode = (bool)(constant('WP_DEBUG')); |
|
59 | + $this->debugMode = (bool) (constant('WP_DEBUG')); |
|
60 | 60 | $this->metadataUrl = $metadataUrl; |
61 | 61 | $this->directoryName = $directoryName; |
62 | 62 | $this->slug = !empty($slug) ? $slug : $this->directoryName; |
63 | 63 | |
64 | 64 | $this->optionName = $optionName; |
65 | - if ( empty($this->optionName) ) { |
|
65 | + if (empty($this->optionName)) { |
|
66 | 66 | //BC: Initially the library only supported plugin updates and didn't use type prefixes |
67 | 67 | //in the option name. Lets use the same prefix-less name when possible. |
68 | - if ( $this->filterSuffix === '' ) { |
|
69 | - $this->optionName = 'external_updates-' . $this->slug; |
|
68 | + if ($this->filterSuffix === '') { |
|
69 | + $this->optionName = 'external_updates-'.$this->slug; |
|
70 | 70 | } else { |
71 | 71 | $this->optionName = $this->getUniqueName('external_updates'); |
72 | 72 | } |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | $this->upgraderStatus = new Puc_v4p4_UpgraderStatus(); |
77 | 77 | $this->updateState = new Puc_v4p4_StateStore($this->optionName); |
78 | 78 | |
79 | - if ( did_action('init') ) { |
|
79 | + if (did_action('init')) { |
|
80 | 80 | $this->loadTextDomain(); |
81 | 81 | } else { |
82 | 82 | add_action('init', array($this, 'loadTextDomain')); |
@@ -98,31 +98,31 @@ discard block |
||
98 | 98 | $domain |
99 | 99 | ); |
100 | 100 | |
101 | - $moFile = $domain . '-' . $locale . '.mo'; |
|
102 | - $path = realpath(dirname(__FILE__) . '/../../languages'); |
|
101 | + $moFile = $domain.'-'.$locale.'.mo'; |
|
102 | + $path = realpath(dirname(__FILE__).'/../../languages'); |
|
103 | 103 | |
104 | 104 | if ($path && file_exists($path)) { |
105 | - load_textdomain($domain, $path . '/' . $moFile); |
|
105 | + load_textdomain($domain, $path.'/'.$moFile); |
|
106 | 106 | } |
107 | 107 | } |
108 | 108 | |
109 | 109 | protected function installHooks() { |
110 | 110 | //Insert our update info into the update array maintained by WP. |
111 | - add_filter('site_transient_' . $this->updateTransient, array($this,'injectUpdate')); |
|
111 | + add_filter('site_transient_'.$this->updateTransient, array($this, 'injectUpdate')); |
|
112 | 112 | |
113 | 113 | //Insert translation updates into the update list. |
114 | - add_filter('site_transient_' . $this->updateTransient, array($this, 'injectTranslationUpdates')); |
|
114 | + add_filter('site_transient_'.$this->updateTransient, array($this, 'injectTranslationUpdates')); |
|
115 | 115 | |
116 | 116 | //Clear translation updates when WP clears the update cache. |
117 | 117 | //This needs to be done directly because the library doesn't actually remove obsolete plugin updates, |
118 | 118 | //it just hides them (see getUpdate()). We can't do that with translations - too much disk I/O. |
119 | 119 | add_action( |
120 | - 'delete_site_transient_' . $this->updateTransient, |
|
120 | + 'delete_site_transient_'.$this->updateTransient, |
|
121 | 121 | array($this, 'clearCachedTranslationUpdates') |
122 | 122 | ); |
123 | 123 | |
124 | 124 | //Rename the update directory to be the same as the existing directory. |
125 | - if ( $this->directoryName !== '.' ) { |
|
125 | + if ($this->directoryName !== '.') { |
|
126 | 126 | add_filter('upgrader_source_selection', array($this, 'fixDirectoryName'), 10, 3); |
127 | 127 | } |
128 | 128 | |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | add_filter('http_request_host_is_external', array($this, 'allowMetadataHost'), 10, 2); |
131 | 131 | |
132 | 132 | //DebugBar integration. |
133 | - if ( did_action('plugins_loaded') ) { |
|
133 | + if (did_action('plugins_loaded')) { |
|
134 | 134 | $this->maybeInitDebugBar(); |
135 | 135 | } else { |
136 | 136 | add_action('plugins_loaded', array($this, 'maybeInitDebugBar')); |
@@ -141,10 +141,10 @@ discard block |
||
141 | 141 | * Remove hooks that were added by this update checker instance. |
142 | 142 | */ |
143 | 143 | protected function removeHooks() { |
144 | - remove_filter('site_transient_' . $this->updateTransient, array($this,'injectUpdate')); |
|
145 | - remove_filter('site_transient_' . $this->updateTransient, array($this, 'injectTranslationUpdates')); |
|
144 | + remove_filter('site_transient_'.$this->updateTransient, array($this, 'injectUpdate')); |
|
145 | + remove_filter('site_transient_'.$this->updateTransient, array($this, 'injectTranslationUpdates')); |
|
146 | 146 | remove_action( |
147 | - 'delete_site_transient_' . $this->updateTransient, |
|
147 | + 'delete_site_transient_'.$this->updateTransient, |
|
148 | 148 | array($this, 'clearCachedTranslationUpdates') |
149 | 149 | ); |
150 | 150 | |
@@ -182,11 +182,11 @@ discard block |
||
182 | 182 | */ |
183 | 183 | public function allowMetadataHost($allow, $host) { |
184 | 184 | static $metadataHost = 0; //Using 0 instead of NULL because parse_url can return NULL. |
185 | - if ( $metadataHost === 0 ) { |
|
185 | + if ($metadataHost === 0) { |
|
186 | 186 | $metadataHost = @parse_url($this->metadataUrl, PHP_URL_HOST); |
187 | 187 | } |
188 | 188 | |
189 | - if ( is_string($metadataHost) && (strtolower($host) === strtolower($metadataHost)) ) { |
|
189 | + if (is_string($metadataHost) && (strtolower($host) === strtolower($metadataHost))) { |
|
190 | 190 | return true; |
191 | 191 | } |
192 | 192 | return $allow; |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | public function checkForUpdates() { |
212 | 212 | $installedVersion = $this->getInstalledVersion(); |
213 | 213 | //Fail silently if we can't find the plugin/theme or read its header. |
214 | - if ( $installedVersion === null ) { |
|
214 | + if ($installedVersion === null) { |
|
215 | 215 | $this->triggerError( |
216 | 216 | sprintf('Skipping update check for %s - installed version unknown.', $this->slug), |
217 | 217 | E_USER_WARNING |
@@ -271,10 +271,10 @@ discard block |
||
271 | 271 | $update = $this->updateState->getUpdate(); |
272 | 272 | |
273 | 273 | //Is there an update available? |
274 | - if ( isset($update) ) { |
|
274 | + if (isset($update)) { |
|
275 | 275 | //Check if the update is actually newer than the currently installed version. |
276 | 276 | $installedVersion = $this->getInstalledVersion(); |
277 | - if ( ($installedVersion !== null) && version_compare($update->version, $installedVersion, '>') ){ |
|
277 | + if (($installedVersion !== null) && version_compare($update->version, $installedVersion, '>')) { |
|
278 | 278 | return $update; |
279 | 279 | } |
280 | 280 | } |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | //Let plugins/themes modify the update. |
302 | 302 | $update = apply_filters($this->getUniqueName('request_update_result'), $update, $httpResult); |
303 | 303 | |
304 | - if ( isset($update, $update->translations) ) { |
|
304 | + if (isset($update, $update->translations)) { |
|
305 | 305 | //Keep only those translation updates that apply to this site. |
306 | 306 | $update->translations = $this->filterApplicableTranslations($update->translations); |
307 | 307 | } |
@@ -345,11 +345,11 @@ discard block |
||
345 | 345 | * @return string |
346 | 346 | */ |
347 | 347 | public function getUniqueName($baseTag) { |
348 | - $name = 'puc_' . $baseTag; |
|
348 | + $name = 'puc_'.$baseTag; |
|
349 | 349 | if ($this->filterSuffix !== '') { |
350 | - $name .= '_' . $this->filterSuffix; |
|
350 | + $name .= '_'.$this->filterSuffix; |
|
351 | 351 | } |
352 | - return $name . '-' . $this->slug; |
|
352 | + return $name.'-'.$this->slug; |
|
353 | 353 | } |
354 | 354 | |
355 | 355 | /** |
@@ -362,7 +362,7 @@ discard block |
||
362 | 362 | * @param string|null $slug |
363 | 363 | */ |
364 | 364 | public function collectApiErrors($error, $httpResponse = null, $url = null, $slug = null) { |
365 | - if ( isset($slug) && ($slug !== $this->slug) ) { |
|
365 | + if (isset($slug) && ($slug !== $this->slug)) { |
|
366 | 366 | return; |
367 | 367 | } |
368 | 368 | |
@@ -416,11 +416,11 @@ discard block |
||
416 | 416 | //Is there an update to insert? |
417 | 417 | $update = $this->getUpdate(); |
418 | 418 | |
419 | - if ( !$this->shouldShowUpdates() ) { |
|
419 | + if (!$this->shouldShowUpdates()) { |
|
420 | 420 | $update = null; |
421 | 421 | } |
422 | 422 | |
423 | - if ( !empty($update) ) { |
|
423 | + if (!empty($update)) { |
|
424 | 424 | //Let plugins filter the update info before it's passed on to WordPress. |
425 | 425 | $update = apply_filters($this->getUniqueName('pre_inject_update'), $update); |
426 | 426 | $updates = $this->addUpdateToList($updates, $update->toWpFormat()); |
@@ -438,7 +438,7 @@ discard block |
||
438 | 438 | * @return stdClass |
439 | 439 | */ |
440 | 440 | protected function addUpdateToList($updates, $updateToAdd) { |
441 | - if ( !is_object($updates) ) { |
|
441 | + if (!is_object($updates)) { |
|
442 | 442 | $updates = new stdClass(); |
443 | 443 | $updates->response = array(); |
444 | 444 | } |
@@ -452,7 +452,7 @@ discard block |
||
452 | 452 | * @return stdClass|null |
453 | 453 | */ |
454 | 454 | protected function removeUpdateFromList($updates) { |
455 | - if ( isset($updates, $updates->response) ) { |
|
455 | + if (isset($updates, $updates->response)) { |
|
456 | 456 | unset($updates->response[$this->getUpdateListKey()]); |
457 | 457 | } |
458 | 458 | return $updates; |
@@ -506,7 +506,7 @@ discard block |
||
506 | 506 | ), |
507 | 507 | $queryArgs |
508 | 508 | ); |
509 | - $queryArgs = apply_filters($this->getUniqueName($filterRoot . '_query_args'), $queryArgs); |
|
509 | + $queryArgs = apply_filters($this->getUniqueName($filterRoot.'_query_args'), $queryArgs); |
|
510 | 510 | |
511 | 511 | //Various options for the wp_remote_get() call. Plugins can filter these, too. |
512 | 512 | $options = array( |
@@ -515,11 +515,11 @@ discard block |
||
515 | 515 | 'Accept' => 'application/json', |
516 | 516 | ), |
517 | 517 | ); |
518 | - $options = apply_filters($this->getUniqueName($filterRoot . '_options'), $options); |
|
518 | + $options = apply_filters($this->getUniqueName($filterRoot.'_options'), $options); |
|
519 | 519 | |
520 | 520 | //The metadata file should be at 'http://your-api.com/url/here/$slug/info.json' |
521 | 521 | $url = $this->metadataUrl; |
522 | - if ( !empty($queryArgs) ){ |
|
522 | + if (!empty($queryArgs)) { |
|
523 | 523 | $url = add_query_arg($queryArgs, $url); |
524 | 524 | } |
525 | 525 | |
@@ -530,7 +530,7 @@ discard block |
||
530 | 530 | //Try to parse the response |
531 | 531 | $status = $this->validateApiResponse($result); |
532 | 532 | $metadata = null; |
533 | - if ( !is_wp_error($status) ){ |
|
533 | + if (!is_wp_error($status)) { |
|
534 | 534 | $metadata = call_user_func(array($metaClass, 'fromJson'), $result['body']); |
535 | 535 | } else { |
536 | 536 | do_action('puc_api_error', $status, $result, $url, $this->slug); |
@@ -551,25 +551,25 @@ discard block |
||
551 | 551 | * @return true|WP_Error |
552 | 552 | */ |
553 | 553 | protected function validateApiResponse($result) { |
554 | - if ( is_wp_error($result) ) { /** @var WP_Error $result */ |
|
555 | - return new WP_Error($result->get_error_code(), 'WP HTTP Error: ' . $result->get_error_message()); |
|
554 | + if (is_wp_error($result)) { /** @var WP_Error $result */ |
|
555 | + return new WP_Error($result->get_error_code(), 'WP HTTP Error: '.$result->get_error_message()); |
|
556 | 556 | } |
557 | 557 | |
558 | - if ( !isset($result['response']['code']) ) { |
|
558 | + if (!isset($result['response']['code'])) { |
|
559 | 559 | return new WP_Error( |
560 | 560 | 'puc_no_response_code', |
561 | 561 | 'wp_remote_get() returned an unexpected result.' |
562 | 562 | ); |
563 | 563 | } |
564 | 564 | |
565 | - if ( $result['response']['code'] !== 200 ) { |
|
565 | + if ($result['response']['code'] !== 200) { |
|
566 | 566 | return new WP_Error( |
567 | 567 | 'puc_unexpected_response_code', |
568 | - 'HTTP response code is ' . $result['response']['code'] . ' (expected: 200)' |
|
568 | + 'HTTP response code is '.$result['response']['code'].' (expected: 200)' |
|
569 | 569 | ); |
570 | 570 | } |
571 | 571 | |
572 | - if ( empty($result['body']) ) { |
|
572 | + if (empty($result['body'])) { |
|
573 | 573 | return new WP_Error('puc_empty_response', 'The metadata file appears to be empty.'); |
574 | 574 | } |
575 | 575 | |
@@ -593,17 +593,17 @@ discard block |
||
593 | 593 | $installedTranslations = $this->getInstalledTranslations(); |
594 | 594 | |
595 | 595 | $applicableTranslations = array(); |
596 | - foreach($translations as $translation) { |
|
596 | + foreach ($translations as $translation) { |
|
597 | 597 | //Does it match one of the available core languages? |
598 | 598 | $isApplicable = array_key_exists($translation->language, $languages); |
599 | 599 | //Is it more recent than an already-installed translation? |
600 | - if ( isset($installedTranslations[$translation->language]) ) { |
|
600 | + if (isset($installedTranslations[$translation->language])) { |
|
601 | 601 | $updateTimestamp = strtotime($translation->updated); |
602 | 602 | $installedTimestamp = strtotime($installedTranslations[$translation->language]['PO-Revision-Date']); |
603 | 603 | $isApplicable = $updateTimestamp > $installedTimestamp; |
604 | 604 | } |
605 | 605 | |
606 | - if ( $isApplicable ) { |
|
606 | + if ($isApplicable) { |
|
607 | 607 | $applicableTranslations[] = $translation; |
608 | 608 | } |
609 | 609 | } |
@@ -617,11 +617,11 @@ discard block |
||
617 | 617 | * @return array |
618 | 618 | */ |
619 | 619 | protected function getInstalledTranslations() { |
620 | - if ( !function_exists('wp_get_installed_translations') ) { |
|
620 | + if (!function_exists('wp_get_installed_translations')) { |
|
621 | 621 | return array(); |
622 | 622 | } |
623 | - $installedTranslations = wp_get_installed_translations($this->translationType . 's'); |
|
624 | - if ( isset($installedTranslations[$this->directoryName]) ) { |
|
623 | + $installedTranslations = wp_get_installed_translations($this->translationType.'s'); |
|
624 | + if (isset($installedTranslations[$this->directoryName])) { |
|
625 | 625 | $installedTranslations = $installedTranslations[$this->directoryName]; |
626 | 626 | } else { |
627 | 627 | $installedTranslations = array(); |
@@ -637,15 +637,15 @@ discard block |
||
637 | 637 | */ |
638 | 638 | public function injectTranslationUpdates($updates) { |
639 | 639 | $translationUpdates = $this->getTranslationUpdates(); |
640 | - if ( empty($translationUpdates) ) { |
|
640 | + if (empty($translationUpdates)) { |
|
641 | 641 | return $updates; |
642 | 642 | } |
643 | 643 | |
644 | 644 | //Being defensive. |
645 | - if ( !is_object($updates) ) { |
|
645 | + if (!is_object($updates)) { |
|
646 | 646 | $updates = new stdClass(); |
647 | 647 | } |
648 | - if ( !isset($updates->translations) ) { |
|
648 | + if (!isset($updates->translations)) { |
|
649 | 649 | $updates->translations = array(); |
650 | 650 | } |
651 | 651 | |
@@ -657,7 +657,7 @@ discard block |
||
657 | 657 | )); |
658 | 658 | |
659 | 659 | //Add our updates to the list. |
660 | - foreach($translationUpdates as $update) { |
|
660 | + foreach ($translationUpdates as $update) { |
|
661 | 661 | $convertedUpdate = array_merge( |
662 | 662 | array( |
663 | 663 | 'type' => $this->translationType, |
@@ -665,9 +665,9 @@ discard block |
||
665 | 665 | 'autoupdate' => 0, |
666 | 666 | //AFAICT, WordPress doesn't actually use the "version" field for anything. |
667 | 667 | //But lets make sure it's there, just in case. |
668 | - 'version' => isset($update->version) ? $update->version : ('1.' . strtotime($update->updated)), |
|
668 | + 'version' => isset($update->version) ? $update->version : ('1.'.strtotime($update->updated)), |
|
669 | 669 | ), |
670 | - (array)$update |
|
670 | + (array) $update |
|
671 | 671 | ); |
672 | 672 | |
673 | 673 | $updates->translations[] = $convertedUpdate; |
@@ -740,27 +740,27 @@ discard block |
||
740 | 740 | /** @var WP_Filesystem_Base $wp_filesystem */ |
741 | 741 | |
742 | 742 | //Basic sanity checks. |
743 | - if ( !isset($source, $remoteSource, $upgrader, $upgrader->skin, $wp_filesystem) ) { |
|
743 | + if (!isset($source, $remoteSource, $upgrader, $upgrader->skin, $wp_filesystem)) { |
|
744 | 744 | return $source; |
745 | 745 | } |
746 | 746 | |
747 | 747 | //If WordPress is upgrading anything other than our plugin/theme, leave the directory name unchanged. |
748 | - if ( !$this->isBeingUpgraded($upgrader) ) { |
|
748 | + if (!$this->isBeingUpgraded($upgrader)) { |
|
749 | 749 | return $source; |
750 | 750 | } |
751 | 751 | |
752 | 752 | //Rename the source to match the existing directory. |
753 | - $correctedSource = trailingslashit($remoteSource) . $this->directoryName . '/'; |
|
754 | - if ( $source !== $correctedSource ) { |
|
753 | + $correctedSource = trailingslashit($remoteSource).$this->directoryName.'/'; |
|
754 | + if ($source !== $correctedSource) { |
|
755 | 755 | //The update archive should contain a single directory that contains the rest of plugin/theme files. |
756 | 756 | //Otherwise, WordPress will try to copy the entire working directory ($source == $remoteSource). |
757 | 757 | //We can't rename $remoteSource because that would break WordPress code that cleans up temporary files |
758 | 758 | //after update. |
759 | - if ( $this->isBadDirectoryStructure($remoteSource) ) { |
|
759 | + if ($this->isBadDirectoryStructure($remoteSource)) { |
|
760 | 760 | return new WP_Error( |
761 | 761 | 'puc-incorrect-directory-structure', |
762 | 762 | sprintf( |
763 | - 'The directory structure of the update is incorrect. All files should be inside ' . |
|
763 | + 'The directory structure of the update is incorrect. All files should be inside '. |
|
764 | 764 | 'a directory named <span class="code">%s</span>, not at the root of the ZIP archive.', |
765 | 765 | htmlentities($this->slug) |
766 | 766 | ) |
@@ -770,11 +770,11 @@ discard block |
||
770 | 770 | /** @var WP_Upgrader_Skin $upgrader ->skin */ |
771 | 771 | $upgrader->skin->feedback(sprintf( |
772 | 772 | 'Renaming %s to %s…', |
773 | - '<span class="code">' . basename($source) . '</span>', |
|
774 | - '<span class="code">' . $this->directoryName . '</span>' |
|
773 | + '<span class="code">'.basename($source).'</span>', |
|
774 | + '<span class="code">'.$this->directoryName.'</span>' |
|
775 | 775 | )); |
776 | 776 | |
777 | - if ( $wp_filesystem->move($source, $correctedSource, true) ) { |
|
777 | + if ($wp_filesystem->move($source, $correctedSource, true)) { |
|
778 | 778 | $upgrader->skin->feedback('Directory successfully renamed.'); |
779 | 779 | return $correctedSource; |
780 | 780 | } else { |
@@ -808,9 +808,9 @@ discard block |
||
808 | 808 | /** @var WP_Filesystem_Base $wp_filesystem */ |
809 | 809 | |
810 | 810 | $sourceFiles = $wp_filesystem->dirlist($remoteSource); |
811 | - if ( is_array($sourceFiles) ) { |
|
811 | + if (is_array($sourceFiles)) { |
|
812 | 812 | $sourceFiles = array_keys($sourceFiles); |
813 | - $firstFilePath = trailingslashit($remoteSource) . $sourceFiles[0]; |
|
813 | + $firstFilePath = trailingslashit($remoteSource).$sourceFiles[0]; |
|
814 | 814 | return (count($sourceFiles) > 1) || (!$wp_filesystem->is_dir($firstFilePath)); |
815 | 815 | } |
816 | 816 | |
@@ -843,11 +843,11 @@ discard block |
||
843 | 843 | $headers = $this->getHeaderNames(); |
844 | 844 | $results = array(); |
845 | 845 | foreach ($headers as $field => $name) { |
846 | - $success = preg_match('/^[ \t\/*#@]*' . preg_quote($name, '/') . ':(.*)$/mi', $content, $matches); |
|
846 | + $success = preg_match('/^[ \t\/*#@]*'.preg_quote($name, '/').':(.*)$/mi', $content, $matches); |
|
847 | 847 | |
848 | - if ( ($success === 1) && $matches[1] ) { |
|
848 | + if (($success === 1) && $matches[1]) { |
|
849 | 849 | $value = $matches[1]; |
850 | - if ( function_exists('_cleanup_header_comment') ) { |
|
850 | + if (function_exists('_cleanup_header_comment')) { |
|
851 | 851 | $value = _cleanup_header_comment($value); |
852 | 852 | } |
853 | 853 | $results[$field] = $value; |
@@ -873,7 +873,7 @@ discard block |
||
873 | 873 | * Initialize the update checker Debug Bar plugin/add-on thingy. |
874 | 874 | */ |
875 | 875 | public function maybeInitDebugBar() { |
876 | - if ( class_exists('Debug_Bar', false) && file_exists(dirname(__FILE__ . '/DebugBar')) ) { |
|
876 | + if (class_exists('Debug_Bar', false) && file_exists(dirname(__FILE__.'/DebugBar'))) { |
|
877 | 877 | $this->createDebugBarExtension(); |
878 | 878 | } |
879 | 879 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | if ( !class_exists('Puc_v4_Factory', false) ): |
3 | 3 | |
4 | - class Puc_v4_Factory extends Puc_v4p4_Factory { } |
|
4 | + class Puc_v4_Factory extends Puc_v4p4_Factory { } |
|
5 | 5 | |
6 | 6 | endif; |
7 | 7 | \ No newline at end of file |
@@ -1,5 +1,5 @@ |
||
1 | 1 | <?php |
2 | -if ( !class_exists('Puc_v4_Factory', false) ): |
|
2 | +if (!class_exists('Puc_v4_Factory', false)): |
|
3 | 3 | |
4 | 4 | class Puc_v4_Factory extends Puc_v4p4_Factory { } |
5 | 5 |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | * Checks if cachesize is > 0.5GB (size is filterable), if so, an option is set which controls showing an admin notice. |
7 | 7 | */ |
8 | 8 | |
9 | -if ( ! defined( 'ABSPATH' ) ) { |
|
9 | +if (!defined('ABSPATH')) { |
|
10 | 10 | exit; |
11 | 11 | } |
12 | 12 | |
@@ -25,51 +25,51 @@ discard block |
||
25 | 25 | |
26 | 26 | public function add_hooks() |
27 | 27 | { |
28 | - if ( is_admin() ) { |
|
29 | - add_action( 'plugins_loaded', array( $this, 'setup' ) ); |
|
28 | + if (is_admin()) { |
|
29 | + add_action('plugins_loaded', array($this, 'setup')); |
|
30 | 30 | } |
31 | - add_action( self::SCHEDULE_HOOK, array( $this, 'cronjob' ) ); |
|
32 | - add_action( 'admin_notices', array( $this, 'show_admin_notice' ) ); |
|
31 | + add_action(self::SCHEDULE_HOOK, array($this, 'cronjob')); |
|
32 | + add_action('admin_notices', array($this, 'show_admin_notice')); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | public function setup() |
36 | 36 | { |
37 | - $do_cache_check = (bool) apply_filters( 'autoptimize_filter_cachecheck_do', true ); |
|
38 | - $schedule = wp_get_schedule( self::SCHEDULE_HOOK ); |
|
39 | - $frequency = apply_filters( 'autoptimize_filter_cachecheck_frequency', 'twicedaily' ); |
|
40 | - if ( ! in_array( $frequency, array( 'hourly', 'twicedaily', 'daily', 'weekly', 'monthly' ) ) ) { |
|
37 | + $do_cache_check = (bool) apply_filters('autoptimize_filter_cachecheck_do', true); |
|
38 | + $schedule = wp_get_schedule(self::SCHEDULE_HOOK); |
|
39 | + $frequency = apply_filters('autoptimize_filter_cachecheck_frequency', 'twicedaily'); |
|
40 | + if (!in_array($frequency, array('hourly', 'twicedaily', 'daily', 'weekly', 'monthly'))) { |
|
41 | 41 | $frequency = 'twicedaily'; |
42 | 42 | } |
43 | - if ( $do_cache_check && ( ! $schedule || $schedule !== $frequency ) ) { |
|
44 | - if ( $schedule ) { |
|
45 | - wp_clear_scheduled_hook( self::SCHEDULE_HOOK ); |
|
43 | + if ($do_cache_check && (!$schedule || $schedule !== $frequency)) { |
|
44 | + if ($schedule) { |
|
45 | + wp_clear_scheduled_hook(self::SCHEDULE_HOOK); |
|
46 | 46 | } |
47 | - wp_schedule_event( time(), $frequency, self::SCHEDULE_HOOK ); |
|
48 | - } elseif ( $schedule && ! $do_cache_check ) { |
|
49 | - wp_clear_scheduled_hook( self::SCHEDULE_HOOK ); |
|
47 | + wp_schedule_event(time(), $frequency, self::SCHEDULE_HOOK); |
|
48 | + } elseif ($schedule && !$do_cache_check) { |
|
49 | + wp_clear_scheduled_hook(self::SCHEDULE_HOOK); |
|
50 | 50 | } |
51 | 51 | } |
52 | 52 | |
53 | 53 | public function cronjob() |
54 | 54 | { |
55 | 55 | // Check cachesize and act accordingly. |
56 | - $max_size = (int) apply_filters( 'autoptimize_filter_cachecheck_maxsize', 536870912 ); |
|
57 | - $do_cache_check = (bool) apply_filters( 'autoptimize_filter_cachecheck_do', true ); |
|
56 | + $max_size = (int) apply_filters('autoptimize_filter_cachecheck_maxsize', 536870912); |
|
57 | + $do_cache_check = (bool) apply_filters('autoptimize_filter_cachecheck_do', true); |
|
58 | 58 | $stat_array = autoptimizeCache::stats(); |
59 | - $cache_size = round( $stat_array[1] ); |
|
60 | - if ( ( $cache_size > $max_size ) && ( $do_cache_check ) ) { |
|
61 | - update_option( 'autoptimize_cachesize_notice', true ); |
|
62 | - if ( apply_filters( 'autoptimize_filter_cachecheck_sendmail', true ) ) { |
|
63 | - $site_url = esc_url( site_url() ); |
|
64 | - $ao_mailto = apply_filters( 'autoptimize_filter_cachecheck_mailto', get_option( 'admin_email', '' ) ); |
|
65 | - |
|
66 | - $ao_mailsubject = __( 'Autoptimize cache size warning', 'autoptimize' ) . ' (' . $site_url . ')'; |
|
67 | - $ao_mailbody = __( 'Autoptimize\'s cache size is getting big, consider purging the cache. Have a look at https://wordpress.org/plugins/autoptimize/faq/ to see how you can keep the cache size under control.', 'autoptimize' ) . ' (site: ' . $site_url . ')'; |
|
68 | - |
|
69 | - if ( ! empty( $ao_mailto ) ) { |
|
70 | - $ao_mailresult = wp_mail( $ao_mailto, $ao_mailsubject, $ao_mailbody ); |
|
71 | - if ( ! $ao_mailresult ) { |
|
72 | - error_log( 'Autoptimize could not send cache size warning mail.' ); |
|
59 | + $cache_size = round($stat_array[1]); |
|
60 | + if (($cache_size > $max_size) && ($do_cache_check)) { |
|
61 | + update_option('autoptimize_cachesize_notice', true); |
|
62 | + if (apply_filters('autoptimize_filter_cachecheck_sendmail', true)) { |
|
63 | + $site_url = esc_url(site_url()); |
|
64 | + $ao_mailto = apply_filters('autoptimize_filter_cachecheck_mailto', get_option('admin_email', '')); |
|
65 | + |
|
66 | + $ao_mailsubject = __('Autoptimize cache size warning', 'autoptimize').' ('.$site_url.')'; |
|
67 | + $ao_mailbody = __('Autoptimize\'s cache size is getting big, consider purging the cache. Have a look at https://wordpress.org/plugins/autoptimize/faq/ to see how you can keep the cache size under control.', 'autoptimize').' (site: '.$site_url.')'; |
|
68 | + |
|
69 | + if (!empty($ao_mailto)) { |
|
70 | + $ao_mailresult = wp_mail($ao_mailto, $ao_mailsubject, $ao_mailbody); |
|
71 | + if (!$ao_mailresult) { |
|
72 | + error_log('Autoptimize could not send cache size warning mail.'); |
|
73 | 73 | } |
74 | 74 | } |
75 | 75 | } |
@@ -87,16 +87,16 @@ discard block |
||
87 | 87 | |
88 | 88 | public function show_admin_notice() |
89 | 89 | { |
90 | - if ( (bool) get_option( 'autoptimize_cachesize_notice', false ) && current_user_can( 'manage_options' ) ) { |
|
90 | + if ((bool) get_option('autoptimize_cachesize_notice', false) && current_user_can('manage_options')) { |
|
91 | 91 | echo '<div class="notice notice-warning"><p>'; |
92 | - _e( '<strong>Autoptimize\'s cache size is getting big</strong>, consider purging the cache. Have a look at <a href="https://wordpress.org/plugins/autoptimize/faq/" target="_blank" rel="noopener noreferrer">the Autoptimize FAQ</a> to see how you can keep the cache size under control.', 'autoptimize' ); |
|
92 | + _e('<strong>Autoptimize\'s cache size is getting big</strong>, consider purging the cache. Have a look at <a href="https://wordpress.org/plugins/autoptimize/faq/" target="_blank" rel="noopener noreferrer">the Autoptimize FAQ</a> to see how you can keep the cache size under control.', 'autoptimize'); |
|
93 | 93 | echo '</p></div>'; |
94 | - update_option( 'autoptimize_cachesize_notice', false ); |
|
94 | + update_option('autoptimize_cachesize_notice', false); |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | // Notice for image proxy usage. |
98 | 98 | $_imgopt_notice = autoptimizeImages::instance()->get_status_notice(); |
99 | - if ( current_user_can( 'manage_options' ) && is_array( $_imgopt_notice ) && array_key_exists( 'status', $_imgopt_notice ) && in_array( $_imgopt_notice['status'], array( 1, -1, -2 ) ) ) { |
|
99 | + if (current_user_can('manage_options') && is_array($_imgopt_notice) && array_key_exists('status', $_imgopt_notice) && in_array($_imgopt_notice['status'], array(1, -1, -2))) { |
|
100 | 100 | $_dismissible = 'ao-img-opt-notice-'; |
101 | 101 | $_hide_notice = '7'; |
102 | 102 | |
@@ -104,10 +104,10 @@ discard block |
||
104 | 104 | $_hide_notice = '1'; |
105 | 105 | } |
106 | 106 | |
107 | - $_imgopt_notice_dismissible = apply_filters( 'autoptimize_filter_imgopt_notice_dismissable', $_dismissible . $_hide_notice ); |
|
107 | + $_imgopt_notice_dismissible = apply_filters('autoptimize_filter_imgopt_notice_dismissable', $_dismissible.$_hide_notice); |
|
108 | 108 | |
109 | - if ( $_imgopt_notice && PAnD::is_admin_notice_active( $_imgopt_notice_dismissible ) ) { |
|
110 | - echo '<div class="notice notice-warning is-dismissible" data-dismissible="' . $_imgopt_notice_dismissible . '"><p>' . $_imgopt_notice['notice'] . '</p></div>'; |
|
109 | + if ($_imgopt_notice && PAnD::is_admin_notice_active($_imgopt_notice_dismissible)) { |
|
110 | + echo '<div class="notice notice-warning is-dismissible" data-dismissible="'.$_imgopt_notice_dismissible.'"><p>'.$_imgopt_notice['notice'].'</p></div>'; |
|
111 | 111 | } |
112 | 112 | } |
113 | 113 | } |