PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 0.3.1
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v0.3.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 / Updates.php

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

158 lines 4.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 use Plugin_Upgrader;
6 use WP_Ajax_Upgrader_Skin;
7
8 class Updates {
9
10 /**
11 * Updates constructor.
12 */
13 public function __construct() {
14 flywp()->router->get( 'updates', [ $this, 'respond' ] );
15 flywp()->router->post( 'update-plugin', [ $this, 'update_plugin' ] );
16 }
17
18 /**
19 * Handle the request.
20 *
21 * @return void
22 */
23 public function respond() {
24 $response = [
25 'core' => $this->core_updates(),
26 'plugins' => $this->has_plugin_update(),
27 'themes' => $this->has_theme_update(),
28 ];
29
30 wp_send_json( $response );
31 }
32
33 /**
34 * Update a plugin.
35 *
36 * @param array $args
37 *
38 * @return void
39 */
40 public function update_plugin( $args ) {
41 if ( ! isset( $args['plugin'] ) ) {
42 wp_send_json_error( 'Missing plugin name' );
43 }
44
45 $plugin_file = sanitize_text_field( wp_unslash( $args['plugin'] ) );
46 $plugin = plugin_basename( $plugin_file );
47
48 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
49 require_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php';
50 require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
51 require_once ABSPATH . 'wp-admin/includes/update.php';
52 require_once ABSPATH . 'wp-admin/includes/file.php';
53 require_once ABSPATH . 'wp-admin/includes/plugin.php';
54
55 $plugin_info = get_plugin_updates();
56
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] );
61
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 }
77
78 wp_send_json_success( [
79 'message' => 'Plugin updated successfully.',
80 ] );
81 }
82 }
83
84 wp_send_json_error( [
85 'message' => 'Plugin update failed.',
86 ] );
87 }
88
89 /**
90 * Check if WordPress core has an update available.
91 *
92 * @return array
93 */
94 private function core_updates() {
95 $update = get_site_transient( 'update_core' );
96 $current = get_bloginfo( 'version' );
97
98 if ( ! function_exists( 'get_preferred_from_update_core' ) ) {
99 require_once ABSPATH . 'wp-admin/includes/update.php';
100 }
101
102 $update = get_preferred_from_update_core();
103
104 $response = [
105 'version' => $current,
106 'update_available' => false,
107 'new_version' => null,
108 ];
109
110 if ( ! isset( $update->response ) || 'upgrade' !== $update->response ) {
111 return $response;
112 }
113
114 $response['update_available'] = true;
115 $response['new_version'] = $update->current;
116
117 return $response;
118 }
119
120 /**
121 * Check if a plugin has an update available.
122 *
123 * @return bool
124 */
125 private function has_plugin_update() {
126 if ( ! function_exists( 'get_plugins' ) ) {
127 require_once ABSPATH . 'wp-admin/includes/plugin.php';
128 }
129
130 if ( ! function_exists( 'get_plugin_updates' ) ) {
131 require_once ABSPATH . 'wp-admin/includes/update.php';
132 }
133
134 $updates = get_plugin_updates();
135
136 if ( $updates && is_array( $updates ) && count( $updates ) > 0 ) {
137 return count( $updates );
138 }
139
140 return false;
141 }
142
143 /**
144 * Check if a theme has an update available.
145 *
146 * @return bool
147 */
148 private function has_theme_update() {
149 $updates = get_site_transient( 'update_themes' );
150
151 if ( $updates && is_object( $updates ) && count( $updates->response ) > 0 ) {
152 return count( $updates->response );
153 }
154
155 return false;
156 }
157 }
158