PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 1.7.0
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v1.7.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 1.5.1 All 26 releases
flywp / includes / Api / Plugins.php

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

99 lines 3.1 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 Plugins {
6
7 /**
8 * API constructor.
9 */
10 public function __construct() {
11 flywp()->router->get( 'plugins', [ $this, 'respond' ] );
12 }
13
14 /**
15 * Handle request.
16 *
17 * @return void
18 */
19 public function respond( $args ) {
20 $valid_statuses = [ 'all', 'active', 'inactive', 'upgrade' ];
21 $status = isset( $args['status'] ) && in_array( $args['status'], $valid_statuses, true ) ? $args['status'] : 'all';
22
23 if ( ! function_exists( 'get_plugins' ) ) {
24 require_once ABSPATH . 'wp-admin/includes/plugin.php';
25 }
26
27 if ( ! function_exists( 'get_plugin_updates' ) ) {
28 require_once ABSPATH . 'wp-admin/includes/update.php';
29 }
30
31 // Update the update cache if requested.
32 if ( isset( $args['force'] ) && $args['force'] === 'true' ) {
33 wp_update_plugins();
34 }
35
36 $response = [];
37 $plugins = get_plugins();
38 $updates = get_plugin_updates();
39 $auto_updates = get_site_option( 'auto_update_plugins', [] );
40
41 foreach ( $plugins as $file => $details ) {
42 $plugin_status = $this->get_status( $file );
43 $update = $this->get_update( $file, $updates );
44
45 // skip if 'all', status mismatch, or needs the 'upgrade' status, which matches truthy $update.
46 if ( 'all' !== $status && $status !== $plugin_status && ( 'upgrade' !== $status || ! $update ) ) {
47 continue;
48 }
49
50 $response[] = [
51 'name' => $details['Name'],
52 'version' => $details['Version'],
53 'url' => $details['PluginURI'],
54 'update_available' => $update ? true : false,
55 'new_version' => $update ? $update['new_version'] : null,
56 'author' => $details['Author'],
57 'file' => $file,
58 'status' => $plugin_status,
59 'textdomain' => $details['TextDomain'],
60 'description' => $details['Description'],
61 'php' => $details['RequiresPHP'],
62 'auto_update' => in_array( $file, $auto_updates, true ),
63 ];
64 }
65
66 wp_send_json( $response );
67 }
68
69 /**
70 * Get plugin active status.
71 *
72 * @param string $file
73 *
74 * @return string
75 */
76 private function get_status( $file ) {
77 return is_plugin_active( $file ) ? 'active' : 'inactive';
78 }
79
80 /**
81 * Check if a plugin has an update available.
82 *
83 * @param string $plugin_file
84 * @param array $updates
85 *
86 * @return array|bool
87 */
88 private function get_update( $plugin_file, $updates ) {
89 if ( isset( $updates[ $plugin_file ] ) && isset( $updates[ $plugin_file ]->update ) ) {
90 return [
91 'new_version' => $updates[ $plugin_file ]->update->new_version,
92 'package' => $updates[ $plugin_file ]->update->package,
93 ];
94 }
95
96 return false;
97 }
98 }
99