PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 1.4.1
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v1.4.1
1.7.1 1.7.0 1.6.0 1.5.2 trunk 0.1 0.2.0 0.2.1 0.3 0.3.1 0.3.2 0.3.3 0.3.4 0.4 0.4.1 0.4.2 0.4.3 1.0 1.1 1.2 1.3.1 1.4.0 1.4.1 1.5.0 1.5.1 All 26 releases
flywp / includes / Api / UpdatesData.php

UpdatesData.php in FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel 1.4.1, at includes/Api/UpdatesData.php

238 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FlyWP\Api;
4
5 class UpdatesData {
6 public const CRON_HOOK = 'flywp_send_updates_data';
7 public const CRON_INTERVAL = 'twicedaily';
8
9 /**
10 * UpdatesData constructor.
11 */
12 public function __construct() {
13 $this->initialize_routes();
14 $this->initialize_cron_job();
15 }
16
17 /**
18 * Initialize API routes.
19 */
20 private function initialize_routes(): void {
21 flywp()->router->get( 'updates-data', [ $this, 'respond' ] );
22 }
23
24 /**
25 * Initialize cron job for sending updates data.
26 */
27 private function initialize_cron_job(): void {
28 add_action( self::CRON_HOOK, [ $this, 'send_updates_data_to_api' ] );
29
30 if ( ! wp_next_scheduled( self::CRON_HOOK ) ) {
31 wp_schedule_event( time(), self::CRON_INTERVAL, self::CRON_HOOK );
32 }
33 }
34
35 /**
36 * Send updates data to the remote API.
37 */
38 public function send_updates_data_to_api(): void {
39 $updates_data = $this->get_updates_data();
40 flywp()->flyapi->post( '/updates-data', $updates_data );
41 }
42
43 /**
44 * Handle the API request.
45 */
46 public function respond(): void {
47 wp_send_json( $this->get_updates_data() );
48 }
49
50 /**
51 * Get updates data.
52 *
53 * @return array
54 */
55 private function get_updates_data(): array {
56 return [
57 'wp_version' => get_bloginfo( 'version' ),
58 'updates' => $this->get_formatted_updates_data(),
59 ];
60 }
61
62 /**
63 * Get formatted updates data.
64 *
65 * @return array
66 */
67 private function get_formatted_updates_data(): array {
68 return [
69 'core' => $this->get_formatted_core_updates(),
70 'plugins' => $this->get_formatted_plugin_updates(),
71 'themes' => $this->get_formatted_theme_updates(),
72 ];
73 }
74
75 /**
76 * Get formatted core updates data.
77 *
78 * @return array
79 */
80 private function get_formatted_core_updates(): array {
81 $core_data = $this->get_core_updates();
82
83 if ( ! $core_data['update_available'] ) {
84 return [];
85 }
86
87 return [
88 'installed_version' => $core_data['version'],
89 'latest_version' => $core_data['new_version'],
90 ];
91 }
92
93 /**
94 * Get formatted plugin updates data.
95 *
96 * @return array
97 */
98 private function get_formatted_plugin_updates(): array {
99 $this->load_required_files();
100
101 $all_plugins = get_plugins();
102 $plugin_updates = get_plugin_updates();
103
104 $formatted_plugins = [];
105
106 foreach ( $plugin_updates as $plugin_file => $plugin_data ) {
107 $plugin_info = $all_plugins[ $plugin_file ];
108 $slug = dirname( $plugin_file );
109
110 $formatted_plugins[] = $this->format_plugin_data( $plugin_info, $plugin_data, $plugin_file, $slug );
111 }
112
113 return $formatted_plugins;
114 }
115
116 /**
117 * Get formatted theme updates data.
118 *
119 * @return array
120 */
121 private function get_formatted_theme_updates(): array {
122 $theme_updates = get_theme_updates();
123
124 $formatted_themes = [];
125
126 foreach ( $theme_updates as $theme_slug => $theme_data ) {
127 $theme = wp_get_theme( $theme_slug );
128 $formatted_themes[] = $this->format_theme_data( $theme, $theme_data, $theme_slug );
129 }
130
131 return $formatted_themes;
132 }
133
134 /**
135 * Check if WordPress core has an update available.
136 *
137 * @return array
138 */
139 private function get_core_updates(): array {
140 $current = get_bloginfo( 'version' );
141
142 $this->load_required_files();
143
144 $update = get_preferred_from_update_core();
145
146 $response = [
147 'version' => $current,
148 'update_available' => false,
149 'new_version' => null,
150 ];
151
152 if ( ! isset( $update->response ) || $update->response !== 'upgrade' ) {
153 return $response;
154 }
155
156 $response['update_available'] = true;
157 $response['new_version'] = $update->current;
158
159 return $response;
160 }
161
162 /**
163 * Format plugin data.
164 *
165 * @param array $plugin_info
166 * @param object $plugin_data
167 * @param string $plugin_file
168 * @param string $slug
169 *
170 * @return array
171 */
172 private function format_plugin_data( array $plugin_info, object $plugin_data, string $plugin_file, string $slug ): array {
173 $formatted_plugin = [
174 'slug' => $slug,
175 'name' => $plugin_info['Name'],
176 'installed_version' => $plugin_info['Version'],
177 'latest_version' => $plugin_data->update->new_version,
178 'is_active' => is_plugin_active( $plugin_file ),
179 ];
180
181 $extra = [
182 'url' => $plugin_info['PluginURI'] ?? '',
183 'author' => $plugin_info['Author'] ?? '',
184 'file' => $plugin_file,
185 'textdomain' => $plugin_info['TextDomain'] ?? '',
186 'description' => $plugin_info['Description'] ?? '',
187 'php' => $plugin_info['RequiresPHP'] ?? '',
188 ];
189
190 if ( ! empty( array_filter( $extra ) ) ) {
191 $formatted_plugin['extra'] = array_filter( $extra );
192 }
193
194 return $formatted_plugin;
195 }
196
197 /**
198 * Format theme data.
199 *
200 * @param \WP_Theme $theme
201 * @param object $theme_data
202 * @param string $theme_slug
203 *
204 * @return array
205 */
206 private function format_theme_data( \WP_Theme $theme, object $theme_data, string $theme_slug ): array {
207 $formatted_theme = [
208 'slug' => $theme_slug,
209 'name' => $theme->get( 'Name' ),
210 'installed_version' => $theme->get( 'Version' ),
211 'latest_version' => $theme_data->update['new_version'],
212 'is_active' => ( get_stylesheet() === $theme_slug ),
213 ];
214
215 $extra = [
216 'url' => $theme->get( 'ThemeURI' ),
217 ];
218
219 if ( ! empty( array_filter( $extra ) ) ) {
220 $formatted_theme['extra'] = array_filter( $extra );
221 }
222
223 return $formatted_theme;
224 }
225
226 /**
227 * Load required WordPress files.
228 */
229 private function load_required_files(): void {
230 if ( ! function_exists( 'get_plugins' ) ) {
231 require_once ABSPATH . 'wp-admin/includes/plugin.php';
232 }
233 if ( ! function_exists( 'get_plugin_updates' ) ) {
234 require_once ABSPATH . 'wp-admin/includes/update.php';
235 }
236 }
237 }
238