PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 0.4.2
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v0.4.2
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 0.4.2, at includes/Api/Plugins.php

97 lines 2.9 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
40 foreach ( $plugins as $file => $details ) {
41 $plugin_status = $this->get_status( $file );
42 $update = $this->get_update( $file, $updates );
43
44 // skip if 'all', status mismatch, or needs the 'upgrade' status, which matches truthy $update.
45 if ( 'all' !== $status && $status !== $plugin_status && ( 'upgrade' !== $status || ! $update ) ) {
46 continue;
47 }
48
49 $response[] = [
50 'name' => $details['Name'],
51 'version' => $details['Version'],
52 'url' => $details['PluginURI'],
53 'update_available' => $update ? true : false,
54 'new_version' => $update ? $update['new_version'] : null,
55 'author' => $details['Author'],
56 'file' => $file,
57 'status' => $plugin_status,
58 'textdomain' => $details['TextDomain'],
59 'description' => $details['Description'],
60 'php' => $details['RequiresPHP'],
61 ];
62 }
63
64 wp_send_json( $response );
65 }
66
67 /**
68 * Get plugin active status.
69 *
70 * @param string $file
71 *
72 * @return string
73 */
74 private function get_status( $file ) {
75 return is_plugin_active( $file ) ? 'active' : 'inactive';
76 }
77
78 /**
79 * Check if a plugin has an update available.
80 *
81 * @param string $plugin_file
82 * @param array $updates
83 *
84 * @return array|bool
85 */
86 private function get_update( $plugin_file, $updates ) {
87 if ( isset( $updates[ $plugin_file ] ) && isset( $updates[ $plugin_file ]->update ) ) {
88 return [
89 'new_version' => $updates[ $plugin_file ]->update->new_version,
90 'package' => $updates[ $plugin_file ]->update->package,
91 ];
92 }
93
94 return false;
95 }
96 }
97