PluginProbe
Assistant – Every Day Productivity Apps / 0.2.1
Assistant – Every Day Productivity Apps v0.2.1
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 / classes / class-fl-assistant-rest-updates.php

class-fl-assistant-rest-updates.php in Assistant – Every Day Productivity Apps 0.2.1, at classes/class-fl-assistant-rest-updates.php

193 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * REST API logic for updates.
5 */
6 final class FL_Assistant_REST_Updates {
7
8 /**
9 * Register routes.
10 */
11 static public function register_routes() {
12 register_rest_route(
13 FL_Assistant_REST::$namespace, '/updates', array(
14 array(
15 'methods' => WP_REST_Server::READABLE,
16 'callback' => __CLASS__ . '::updates',
17 'args' => array(
18 'type' => array(
19 'required' => false,
20 'type' => 'string',
21 ),
22 ),
23 'permission_callback' => function() {
24 return current_user_can( 'update_plugins' ) && current_user_can( 'update_themes' );
25 },
26 ),
27 )
28 );
29
30 register_rest_route(
31 FL_Assistant_REST::$namespace, '/updates/count', array(
32 array(
33 'methods' => WP_REST_Server::READABLE,
34 'callback' => __CLASS__ . '::updates_count',
35 'permission_callback' => function() {
36 return current_user_can( 'update_plugins' ) && current_user_can( 'update_themes' );
37 },
38 ),
39 )
40 );
41 }
42
43 /**
44 * Returns an array of response data for a single plugin.
45 */
46 static public function get_plugin_response_data( $update, $plugin ) {
47 $thumbnail = null;
48 $banner = null;
49
50 if ( isset( $update->icons ) ) {
51 if ( isset( $update->icons['2x'] ) ) {
52 $thumbnail = $update->icons['2x'];
53 } elseif ( isset( $update->icons['1x'] ) ) {
54 $thumbnail = $update->icons['1x'];
55 }
56 }
57
58 if ( isset( $update->banners ) ) {
59 if ( isset( $update->banners['2x'] ) ) {
60 $banner = $update->banners['2x'];
61 } elseif ( isset( $update->banners['1x'] ) ) {
62 $banner = $update->banners['1x'];
63 }
64 }
65
66 return array(
67 'author' => $plugin['AuthorName'],
68 'banner' => $banner,
69 'content' => $plugin['Description'],
70 'key' => $update->plugin,
71 'meta' => $plugin['Version'] . ' by ' . $plugin['AuthorName'],
72 'metaUpdated' => $update->new_version . ' by ' . $plugin['AuthorName'],
73 'plugin' => $update->plugin,
74 'thumbnail' => $thumbnail,
75 'title' => $plugin['Name'],
76 'type' => 'plugin',
77 'version' => $plugin['Version'],
78 );
79 }
80
81 /**
82 * Returns an array of response data for a single theme.
83 */
84 static public function get_theme_response_data( $update, $theme ) {
85 $thumbnail = null;
86
87 if ( isset( $update->icons ) ) {
88 if ( isset( $update->icons['2x'] ) ) {
89 $thumbnail = $update->icons['2x'];
90 } elseif ( isset( $update->icons['1x'] ) ) {
91 $thumbnail = $update->icons['1x'];
92 }
93 }
94
95 return array(
96 'author' => strip_tags( $theme->Author ),
97 'banner' => $theme->get_screenshot(),
98 'content' => $theme->Description,
99 'key' => $update['theme'],
100 'meta' => $theme->Version . ' by ' . strip_tags( $theme->Author ),
101 'meta_updated' => $update['new_version'] . ' by ' . strip_tags( $theme->Author ),
102 'theme' => $update['theme'],
103 'thumbnail' => $theme->get_screenshot(),
104 'title' => $theme->Name,
105 'type' => 'theme',
106 'version' => $theme->Version,
107 );
108 }
109
110 /**
111 * Returns an array of updates and related data.
112 */
113 static public function updates( $request ) {
114 require_once ABSPATH . 'wp-admin/includes/plugin.php';
115
116 wp_update_plugins();
117 wp_update_themes();
118
119 $response = array();
120 $update_plugins = get_site_transient( 'update_plugins' );
121 $update_themes = get_site_transient( 'update_themes' );
122 $type = $request->get_param( 'type' );
123
124 if ( ! $type || 'all' === $type || 'plugins' === $type ) {
125 if ( current_user_can( 'update_plugins' ) && ! empty( $update_plugins->response ) ) {
126 $plugins = array(
127 'label' => __( 'Plugins', 'fl-assistant' ),
128 'items' => [],
129 );
130 foreach ( $update_plugins->response as $key => $update ) {
131 $plugin = get_plugin_data( trailingslashit( WP_PLUGIN_DIR ) . $key );
132 if ( version_compare( $update->new_version, $plugin['Version'], '>' ) ) {
133 $plugins['items'][] = self::get_plugin_response_data( $update, $plugin );
134 }
135 }
136 $response[] = $plugins;
137 }
138 }
139
140 if ( ! $type || 'all' === $type || 'themes' === $type ) {
141 if ( current_user_can( 'update_themes' ) && ! empty( $update_themes->response ) ) {
142 $themes = array(
143 'label' => __( 'Themes', 'fl-assistant' ),
144 'items' => [],
145 );
146 foreach ( $update_themes->response as $key => $update ) {
147 $theme = wp_get_theme( $key );
148 if ( version_compare( $update['new_version'], $theme->Version, '>' ) ) {
149 $themes['items'][] = self::get_theme_response_data( $update, $theme );
150 }
151 }
152 $response[] = $themes;
153 }
154 }
155
156 return rest_ensure_response( $response );
157 }
158
159 /**
160 * Returns the number of updates found.
161 */
162 static public function updates_count( $request ) {
163 wp_update_plugins();
164 wp_update_themes();
165
166 $count = 0;
167 $plugins = 0;
168 $themes = 0;
169 $update_plugins = get_site_transient( 'update_plugins' );
170 $update_themes = get_site_transient( 'update_themes' );
171
172 if ( current_user_can( 'update_plugins' ) && ! empty( $update_plugins->response ) ) {
173 $plugins = count( $update_plugins->response );
174 $count += $plugins;
175 }
176
177 if ( current_user_can( 'update_themes' ) && ! empty( $update_themes->response ) ) {
178 $themes = count( $update_themes->response );
179 $count += $themes;
180 }
181
182 return rest_ensure_response(
183 array(
184 'plugins' => $plugins,
185 'themes' => $themes,
186 'total' => $count,
187 )
188 );
189 }
190 }
191
192 FL_Assistant_REST_Updates::register_routes();
193