PluginProbe
Assistant – Every Day Productivity Apps / 1.4.3
Assistant – Every Day Productivity Apps v1.4.3
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
assistant / backend / src / Controllers / UpdatesController.php

UpdatesController.php in Assistant – Every Day Productivity Apps 1.4.3, at backend/src/Controllers/UpdatesController.php

135 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FL\Assistant\Controllers;
4
5 require_once ABSPATH . 'wp-admin/includes/update.php';
6 require_once ABSPATH . 'wp-admin/includes/plugin.php';
7
8 use FL\Assistant\Data\Transformers\PluginUpdatesTransformer;
9 use FL\Assistant\Data\Transformers\ThemeUpdatesTransformer;
10
11 use FL\Assistant\System\Contracts\ControllerAbstract;
12 use WP_REST_Server;
13
14 /**
15 * REST API logic for updates.
16 */
17 class UpdatesController extends ControllerAbstract {
18
19 /**
20 * @var ThemeUpdatesTransformer
21 */
22 protected $theme_updates_transformer;
23 /**
24 * @var PluginUpdatesTransformer
25 */
26 protected $plugin_updates_transformer;
27
28 /**
29 * UpdatesController constructor.
30 * @param PluginUpdatesTransformer $plugin_updates_transformer
31 * @param ThemeUpdatesTransformer $theme_updates_transformer
32 */
33 public function __construct( PluginUpdatesTransformer $plugin_updates_transformer, ThemeUpdatesTransformer $theme_updates_transformer ) {
34 $this->plugin_updates_transformer = $plugin_updates_transformer;
35 $this->theme_updates_transformer = $theme_updates_transformer;
36 }
37
38 /**
39 * Register routes.
40 */
41 public function register_routes() {
42 $this->route(
43 '/updates', [
44 [
45 'methods' => WP_REST_Server::READABLE,
46 'callback' => [ $this, 'updates' ],
47 'args' => [
48 'type' => [
49 'required' => false,
50 'type' => 'string',
51 ],
52 ],
53 'permission_callback' => function () {
54 return current_user_can( 'update_plugins' ) && current_user_can( 'update_themes' );
55 },
56 ],
57 ]
58 );
59
60 $this->route(
61 '/updates/count', [
62 [
63 'methods' => WP_REST_Server::READABLE,
64 'callback' => [ $this, 'updates_count' ],
65 'permission_callback' => function () {
66 return current_user_can( 'update_plugins' ) && current_user_can( 'update_themes' );
67 },
68 ],
69 ]
70 );
71 }
72
73 /**
74 * Returns an array of updates and related data.
75 * @param \WP_REST_Request $request
76 * @return mixed|\WP_REST_Response
77 */
78 public function updates( \WP_REST_Request $request ) {
79 wp_update_plugins();
80 wp_update_themes();
81
82 $plugins = get_plugin_updates();
83 $themes = get_theme_updates();
84 $type = $request->get_param( 'type' );
85
86 $response = [];
87
88 if ( ! $type || 'all' === $type || 'plugins' === $type ) {
89 foreach ( $plugins as $key => $plugin ) {
90 $response[] = call_user_func( $this->plugin_updates_transformer, $plugin );
91 }
92 }
93
94 if ( ! $type || 'all' === $type || 'themes' === $type ) {
95 foreach ( $themes as $key => $theme ) {
96 $response[] = call_user_func( $this->theme_updates_transformer, $theme );
97 }
98 }
99
100 $p = $this->paginate_array( $response, count( $response ), 0 );
101
102 return rest_ensure_response( $p->to_array() );
103 }
104
105 /**
106 * Returns the number of updates found.
107 */
108 public function updates_count( $request ) {
109 wp_update_plugins();
110 wp_update_themes();
111
112 $count = 0;
113 $plugins = 0;
114 $themes = 0;
115
116 if ( current_user_can( 'update_plugins' ) ) {
117 $plugins = count( get_plugin_updates() );
118 $count += $plugins;
119 }
120
121 if ( current_user_can( 'update_themes' ) ) {
122 $themes = count( get_theme_updates() );
123 $count += $themes;
124 }
125
126 return rest_ensure_response(
127 [
128 'plugins' => $plugins,
129 'themes' => $themes,
130 'total' => $count,
131 ]
132 );
133 }
134 }
135