PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 1.8.0
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v1.8.0
1.8.0 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 All 27 releases
← All changes | includes/Api/Updates.php +143 -34 0.2.1 → 1.8.0 View file →
@@ -1,9 +1,13 @@
1 1 <?php
2 2
3 3 namespace FlyWP\Api;
4 4
5 +use Core_Upgrader;
6 +use Exception;
5 7 use Plugin_Upgrader;
8 +use Theme_Upgrader;
9 +use Throwable;
6 10 use WP_Ajax_Upgrader_Skin;
7 11
8 12 class Updates {
9 13
@@ -11,9 +15,11 @@
11 15 * Updates constructor.
12 16 */
13 17 public function __construct() {
14 18 flywp()->router->get( 'updates', [ $this, 'respond' ] );
15 - flywp()->router->post( 'update-plugin', [ $this, 'update_plugin' ] );
19 + flywp()->router->post( 'update-plugins', [ $this, 'update_plugins' ] );
20 + flywp()->router->post( 'update-themes', [ $this, 'update_themes' ] );
21 + flywp()->router->post( 'update-core', [ $this, 'update_core' ] );
16 22 }
17 23
18 24 /**
19 25 * Handle the request.
@@ -36,55 +42,158 @@
36 42 * @param array $args
37 43 *
38 44 * @return void
39 45 */
40 - public function update_plugin( $args ) {
41 - if ( ! isset( $args['plugin'] ) ) {
42 - wp_send_json_error( 'Missing plugin name' );
46 + public function update_plugins( $args ) {
47 + if ( ! isset( $args['plugins'] ) || ! is_array( $args['plugins'] ) ) {
48 + wp_send_json_error( 'Missing plugin name(s)' );
43 49 }
44 50
45 - $plugin_file = sanitize_text_field( wp_unslash( $args['plugin'] ) );
46 - $plugin = plugin_basename( $plugin_file );
51 + $plugins = array_map( 'sanitize_text_field', wp_unslash( $args['plugins'] ) );
47 52
53 + try {
54 + $this->update_item( $plugins, 'plugin' );
55 + } catch ( Throwable $th ) {
56 + wp_send_json_error( [
57 + 'message' => $th->getMessage(),
58 + ], 500 );
59 + }
60 +
61 + wp_send_json_success( [
62 + 'message' => 'Plugins updated successfully.',
63 + ] );
64 + }
65 +
66 + /**
67 + * Update a theme.
68 + *
69 + * @param array $args
70 + *
71 + * @return void
72 + */
73 + public function update_themes( $args ) {
74 + if ( ! isset( $args['themes'] ) || ! is_array( $args['themes'] ) ) {
75 + wp_send_json_error( 'Missing theme name(s)' );
76 + }
77 +
78 + $themes = array_map( 'sanitize_text_field', wp_unslash( $args['themes'] ) );
79 +
80 + try {
81 + $this->update_item( $themes, 'theme' );
82 + } catch ( Throwable $th ) {
83 + wp_send_json_error( [
84 + 'message' => $th->getMessage(),
85 + ], 500 );
86 + }
87 +
88 + wp_send_json_success( [
89 + 'message' => 'Themes updated successfully.',
90 + ] );
91 + }
92 +
93 + /**
94 + * Update WordPress core.
95 + *
96 + * @param array $args
97 + *
98 + * @return void
99 + */
100 + public function update_core( $args ) {
101 + $version = isset( $args['version'] ) ? sanitize_text_field( $args['version'] ) : false;
102 + $locale = isset( $args['locale'] ) ? sanitize_text_field( $args['locale'] ) : get_locale();
103 +
104 + if ( ! function_exists( 'find_core_update' ) ) {
105 + require_once ABSPATH . 'wp-admin/includes/update.php';
106 + }
107 +
108 + if ( ! $version ) {
109 + $updates = get_core_updates();
110 + $version = $updates[0]->current;
111 + }
112 +
113 + $update = find_core_update( $version, $locale );
114 +
115 + if ( ! $update ) {
116 + wp_send_json_error( [
117 + 'message' => 'Update not found.',
118 + ] );
119 + }
120 +
121 + $this->include_files();
122 + $this->extend_time_limit();
123 +
124 + $upgrader = new Core_Upgrader( new WP_Ajax_Upgrader_Skin() );
125 + $result = $upgrader->upgrade( $update );
126 +
127 + if ( is_wp_error( $result ) ) {
128 + wp_send_json_error( [
129 + 'message' => $result->get_error_message(),
130 + ], 500 );
131 + }
132 +
133 + wp_send_json_success( [
134 + 'message' => 'WordPress core updated successfully.',
135 + ] );
136 + }
137 +
138 + /**
139 + * Include required files.
140 + *
141 + * @return void
142 + */
143 + private function include_files() {
48 144 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
49 145 require_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php';
50 146 require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
51 147 require_once ABSPATH . 'wp-admin/includes/update.php';
52 148 require_once ABSPATH . 'wp-admin/includes/file.php';
53 - require_once ABSPATH . 'wp-admin/includes/plugin.php';
149 + }
54 150
55 - $plugin_info = get_plugin_updates();
151 + /**
152 + * Increase execution time limit.
153 + *
154 + * @return void
155 + */
156 + public function extend_time_limit() {
157 + set_time_limit( 300 );
158 + }
56 159
57 - if ( isset( $plugin_info[$plugin_file] ) && isset( $plugin_info[$plugin_file]->update ) ) {
58 - $skin = new WP_Ajax_Upgrader_Skin();
59 - $upgrader = new Plugin_Upgrader( $skin );
60 - $result = $upgrader->bulk_upgrade( [$plugin_file] );
160 + /**
161 + * Update plugins or themes.
162 + *
163 + * @param string $type the type of update ('plugin' or 'theme')
164 + * @param array $args the update arguments
165 + *
166 + * @return void
167 + */
168 + private function update_item( $items, $type = 'plugin' ) {
169 + $this->include_files();
61 170
62 - if ( is_wp_error( $skin->result ) ) {
63 - wp_send_json_error( [
64 - 'code' => $skin->result->get_error_code(),
65 - 'message' => $skin->result->get_error_message(),
66 - ] );
67 - } elseif ( $skin->get_errors()->has_errors() ) {
68 - wp_send_json_error( [
69 - 'message' => $skin->result->get_error_message(),
70 - ] );
71 - } elseif ( is_array( $result ) && !empty( $result[ $plugin ] ) ) {
72 - if ( true === $result[ $plugin ] ) {
73 - wp_send_json_error( [
74 - 'message' => $upgrader->strings['up_to_date'],
75 - ] );
76 - }
171 + $skin = new WP_Ajax_Upgrader_Skin();
77 172
78 - wp_send_json_success( [
79 - 'message' => 'Plugin updated successfully.',
80 - ] );
81 - }
173 + if ( $type === 'plugin' ) {
174 + $upgrader = new Plugin_Upgrader( $skin );
175 + } elseif ( $type === 'theme' ) {
176 + $upgrader = new Theme_Upgrader( $skin );
177 + } else {
178 + throw new Exception( 'Invalid update type' );
82 179 }
83 180
84 - wp_send_json_error( [
85 - 'message' => 'Plugin update failed.',
86 - ] );
181 + $this->extend_time_limit();
182 + $result = $upgrader->bulk_upgrade( $items );
183 +
184 + if ( false === $result ) {
185 + throw new Exception( 'Update failed' );
186 + }
187 +
188 + // refresh the update cache
189 + if ( $type === 'plugin' ) {
190 + wp_update_plugins();
191 + } elseif ( $type === 'theme' ) {
192 + wp_update_themes();
193 + }
194 +
195 + return true;
87 196 }
88 197
89 198 /**
90 199 * Check if WordPress core has an update available.