PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.6.4
Jetpack – WP Security, Backup, Speed, & Growth v12.6.4
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / json-endpoints / jetpack / class.jetpack-json-api-themes-endpoint.php

class.jetpack-json-api-themes-endpoint.php in Jetpack – WP Security, Backup, Speed, & Growth 12.6.4, at json-endpoints/jetpack/class.jetpack-json-api-themes-endpoint.php

218 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 use Automattic\Jetpack\Image_CDN\Image_CDN_Core;
4
5 /**
6 * Base class for working with themes, has useful helper functions.
7 */
8 abstract class Jetpack_JSON_API_Themes_Endpoint extends Jetpack_JSON_API_Endpoint {
9
10 /**
11 * The themes.
12 *
13 * @var array
14 */
15 protected $themes = array();
16
17 /**
18 * If we're working in bulk.
19 *
20 * @var boolean
21 */
22 protected $bulk = true;
23
24 /**
25 * The log.
26 *
27 * @var array
28 */
29 protected $log;
30
31 /**
32 * The current theme ID.
33 *
34 * @var int
35 */
36 protected $current_theme_id;
37
38 /**
39 * The response format.
40 *
41 * @var array
42 */
43 public static $_response_format = array( // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
44 'id' => '(string) The theme\'s ID.',
45 'screenshot' => '(string) A theme screenshot URL',
46 'name' => '(string) The name of the theme.',
47 'theme_uri' => '(string) The URI of the theme\'s webpage.',
48 'description' => '(string) A description of the theme.',
49 'author' => '(string) The author of the theme.',
50 'author_uri' => '(string) The website of the theme author.',
51 'tags' => '(array) Tags indicating styles and features of the theme.',
52 'log' => '(array) An array of log strings',
53 'update' => '(array|null) An object containing information about the available update if there is an update available, null otherwise.',
54 'autoupdate' => '(bool) Whether the theme is automatically updated',
55 'autoupdate_translation' => '(bool) Whether the theme is automatically updating translations',
56 );
57
58 /**
59 * The result.
60 */
61 protected function result() {
62
63 $themes = $this->get_themes();
64
65 if ( ! $this->bulk && ! empty( $themes ) ) {
66 return array_pop( $themes );
67 }
68
69 return array( 'themes' => $themes );
70 }
71
72 /**
73 * Walks through either the submitted theme or list of themes and creates the global array
74 *
75 * @param string $theme - the theme URL.
76 *
77 * @return bool|WP_Error
78 */
79 protected function validate_input( $theme ) {
80 $args = $this->input();
81 // lets set what themes were requested, and validate them
82 if ( ! isset( $theme ) || empty( $theme ) ) {
83
84 if ( ! $args['themes'] || empty( $args['themes'] ) ) {
85 return new WP_Error( 'missing_theme', __( 'You are required to specify a theme to update.', 'jetpack' ), 400 );
86 }
87 if ( is_array( $args['themes'] ) ) {
88 $this->themes = $args['themes'];
89 } else {
90 $this->themes[] = $args['themes'];
91 }
92 } else {
93 $this->themes[] = urldecode( $theme );
94 $this->bulk = false;
95 }
96
97 $error = $this->validate_themes();
98 if ( is_wp_error( $error ) ) {
99 return $error;
100 }
101
102 return parent::validate_input( $theme );
103 }
104
105 /**
106 * Walks through submitted themes to make sure they are valid
107 *
108 * @return bool|WP_Error
109 */
110 protected function validate_themes() {
111 foreach ( $this->themes as $theme ) {
112 $error = wp_get_theme( $theme )->errors();
113 if ( is_wp_error( $error ) ) {
114 return new WP_Error( 'unknown_theme', $error->get_error_messages(), 404 );
115 }
116 }
117 return true;
118 }
119
120 /**
121 * Format a theme for the public API
122 *
123 * @param object $theme WP_Theme object.
124 * @return array Named array of theme info used by the API
125 */
126 protected function format_theme( $theme ) {
127
128 if ( ! ( $theme instanceof WP_Theme ) ) {
129 $theme = wp_get_theme( $theme );
130 }
131
132 $fields = array(
133 'name' => 'Name',
134 'theme_uri' => 'ThemeURI',
135 'description' => 'Description',
136 'author' => 'Author',
137 'author_uri' => 'AuthorURI',
138 'tags' => 'Tags',
139 'version' => 'Version',
140 );
141
142 $id = $theme->get_stylesheet();
143 $formatted_theme = array(
144 'id' => $id,
145 'screenshot' => Image_CDN_Core::cdn_url( $theme->get_screenshot(), array(), 'network_path' ),
146 'active' => $id === $this->current_theme_id,
147 );
148
149 foreach ( $fields as $key => $field ) {
150 $formatted_theme[ $key ] = $theme->get( $field );
151 }
152
153 $update_themes = get_site_transient( 'update_themes' );
154 $formatted_theme['update'] = ( isset( $update_themes->response[ $id ] ) ) ? $update_themes->response[ $id ] : null;
155
156 $autoupdate = in_array( $id, Jetpack_Options::get_option( 'autoupdate_themes', array() ), true );
157 $formatted_theme['autoupdate'] = $autoupdate;
158
159 $autoupdate_translation = in_array( $id, Jetpack_Options::get_option( 'autoupdate_themes_translations', array() ), true );
160 $formatted_theme['autoupdate_translation'] = $autoupdate || $autoupdate_translation || Jetpack_Options::get_option( 'autoupdate_translations', false );
161
162 if ( isset( $this->log[ $id ] ) ) {
163 $formatted_theme['log'] = $this->log[ $id ];
164 }
165
166 /**
167 * Filter the array of theme information that will be returned per theme by the Jetpack theme APIs.
168 *
169 * @module json-api
170 *
171 * @since 4.7.0
172 *
173 * @param array $formatted_theme The theme info array.
174 */
175 return apply_filters( 'jetpack_format_theme_details', $formatted_theme );
176 }
177
178 /**
179 * Checks the query_args our collection endpoint was passed to ensure that it's in the proper bounds.
180 *
181 * @return bool|WP_Error a WP_Error object if the args are out of bounds, true if things are good.
182 */
183 protected function check_query_args() {
184 $args = $this->query_args();
185 if ( $args['offset'] < 0 ) {
186 return new WP_Error( 'invalid_offset', __( 'Offset must be greater than or equal to 0.', 'jetpack' ), 400 );
187 }
188 if ( $args['limit'] < 0 ) {
189 return new WP_Error( 'invalid_limit', __( 'Limit must be greater than or equal to 0.', 'jetpack' ), 400 );
190 }
191 return true;
192 }
193
194 /**
195 * Format a list of themes for public display, using the supplied offset and limit args
196 *
197 * @uses WPCOM_JSON_API_Endpoint::query_args()
198 * @return array Public API theme objects
199 */
200 protected function get_themes() {
201 // ditch keys
202 $themes = array_values( $this->themes );
203 // do offset & limit - we've already returned a 400 error if they're bad numbers
204 $args = $this->query_args();
205
206 if ( isset( $args['offset'] ) ) {
207 $themes = array_slice( $themes, (int) $args['offset'] );
208 }
209 if ( isset( $args['limit'] ) ) {
210 $themes = array_slice( $themes, 0, (int) $args['limit'] );
211 }
212
213 $this->current_theme_id = wp_get_theme()->get_stylesheet();
214
215 return array_map( array( $this, 'format_theme' ), $themes );
216 }
217 }
218