PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.0.3
Jetpack – WP Security, Backup, Speed, & Growth v12.0.3
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.0.3, at json-endpoints/jetpack/class.jetpack-json-api-themes-endpoint.php

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