PluginProbe
Essential Widgets / 1.5
Essential Widgets v1.5
3.2.1 3.3 3.2 trunk 1.0.0 1.0.1 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.4.1 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.8 1.9 2.0 2.1 2.2 All 31 releases
essential-widgets / includes / CatchThemesThemePlugin.php

CatchThemesThemePlugin.php in Essential Widgets 1.5, at includes/CatchThemesThemePlugin.php

448 lines 15.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 Class CatchThemesThemePlugin {
4 public function __construct(){
5 remove_action( 'wp_ajax_query-themes', array( $this, 'wp_ajax_query_themes' ), 1 );
6 add_action( 'wp_ajax_query-themes', array( $this, 'wp_ajax_custom_query_themes' ), 1 );
7
8 add_action( 'admin_enqueue_scripts', array( $this, 'our_themes_script' ) );
9
10 if ( ! is_multisite() ) {
11 add_action( 'customize_register', array( $this, 'customize_register' ) );
12 }
13
14 global $wp_customize;
15 remove_action( 'wp_ajax_customize_load_themes', array( $wp_customize, 'handle_load_themes_request' ) );
16 add_action( 'wp_ajax_customize_load_themes', array( $this, 'handle_load_themes_request' ) );
17
18 add_filter( 'install_plugins_tabs', array( $this, 'add_our_plugins_tab' ), 1 );
19 add_filter( "install_plugins_table_api_args_catchplugins", array( $this, 'catchplugins' ), 1 );
20 add_action( 'install_plugins_catchplugins', array( $this, 'plugins_table' ) );
21 }
22
23 /* Adds Catch Themes tab in Add Theme page to show all themes by Catch Themes in wordpress.org */
24 public function wp_ajax_custom_query_themes() {
25 global $themes_allowedtags, $theme_field_defaults;
26
27 if ( ! current_user_can( 'install_themes' ) ) {
28 wp_send_json_error();
29 }
30
31 $args = wp_parse_args( wp_unslash( $_REQUEST['request'] ), array(
32 'per_page' => 20,
33 'fields' => $theme_field_defaults
34 ) );
35
36 if ( isset( $args['browse'] ) && 'catchthemes' === $args['browse'] && ! isset( $args['user'] ) ) {
37 $args['author'] = 'catchthemes';
38 }
39
40 if ( isset( $args['browse'] ) && 'favorites' === $args['browse'] && ! isset( $args['user'] ) ) {
41 $user = get_user_option( 'wporg_favorites' );
42 if ( $user ) {
43 $args['user'] = $user;
44 }
45 }
46
47 $old_filter = isset( $args['browse'] ) ? $args['browse'] : 'search';
48
49 /** This filter is documented in wp-admin/includes/class-wp-theme-install-list-table.php */
50 $args = apply_filters( 'install_themes_table_api_args_' . $old_filter, $args );
51
52 $api = themes_api( 'query_themes', $args );
53
54 if ( is_wp_error( $api ) ) {
55 wp_send_json_error();
56 }
57
58 $update_php = network_admin_url( 'update.php?action=install-theme' );
59 foreach ( $api->themes as &$theme ) {
60 $theme->install_url = add_query_arg( array(
61 'theme' => $theme->slug,
62 '_wpnonce' => wp_create_nonce( 'install-theme_' . $theme->slug )
63 ), $update_php );
64
65 if ( current_user_can( 'switch_themes' ) ) {
66 if ( is_multisite() ) {
67 $theme->activate_url = add_query_arg( array(
68 'action' => 'enable',
69 '_wpnonce' => wp_create_nonce( 'enable-theme_' . $theme->slug ),
70 'theme' => $theme->slug,
71 ), network_admin_url( 'themes.php' ) );
72 } else {
73 $theme->activate_url = add_query_arg( array(
74 'action' => 'activate',
75 '_wpnonce' => wp_create_nonce( 'switch-theme_' . $theme->slug ),
76 'stylesheet' => $theme->slug,
77 ), admin_url( 'themes.php' ) );
78 }
79 }
80
81 if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
82 $theme->customize_url = add_query_arg( array(
83 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ),
84 ), wp_customize_url( $theme->slug ) );
85 }
86
87 $theme->name = wp_kses( $theme->name, $themes_allowedtags );
88 $theme->author = wp_kses( $theme->author, $themes_allowedtags );
89 $theme->version = wp_kses( $theme->version, $themes_allowedtags );
90 $theme->description = wp_kses( $theme->description, $themes_allowedtags );
91 $theme->stars = wp_star_rating( array( 'rating' => $theme->rating, 'type' => 'percent', 'number' => $theme->num_ratings, 'echo' => false ) );
92 $theme->num_ratings = number_format_i18n( $theme->num_ratings );
93 $theme->preview_url = set_url_scheme( $theme->preview_url );
94 }
95
96 wp_send_json_success( $api );
97 }
98
99 public function our_themes_script( $hook_suffix ) {
100
101 if( 'theme-install.php' == $hook_suffix ) {
102 wp_enqueue_script( 'our-themes-script', plugin_dir_url( __FILE__ ) . '../admin/js/our-themes.js', array( 'jquery' ), '2018-05-16' );
103 }
104 }
105
106 /* Add Catch Themes Section in Theme in Customizer */
107 public function customize_register($wp_customize) {
108 $wp_customize->add_section( new WP_Customize_Themes_Section( $wp_customize, 'catchthemes', array(
109 'title' => __( 'Themes by CatchThemes', 'catch-themes-demo-import' ),
110 'action' => 'catchthemes',
111 'capability' => 'install_themes',
112 'panel' => 'themes',
113 'priority' => 6,
114 ) ) );
115 }
116
117
118
119
120 /**
121 * Load themes into the theme browsing/installation UI.
122 * taken from wp-includes/cllass-wp-customize-manager.php
123 * @since 4.9.0
124 */
125 public function handle_load_themes_request() {
126 check_ajax_referer( 'switch_themes', 'nonce' );
127 if ( ! current_user_can( 'switch_themes' ) ) {
128 wp_die( -1 );
129 }
130
131 if ( empty( $_POST['theme_action'] ) ) {
132 wp_send_json_error( 'missing_theme_action' );
133 }
134 $theme_action = sanitize_key( $_POST['theme_action'] );
135 $themes = array();
136 $args = array();
137
138 // Define query filters based on user input.
139 if ( ! array_key_exists( 'search', $_POST ) ) {
140 $args['search'] = '';
141 } else {
142 $args['search'] = sanitize_text_field( wp_unslash( $_POST['search'] ) );
143 }
144
145 if ( ! array_key_exists( 'tags', $_POST ) ) {
146 $args['tag'] = '';
147 } else {
148 $args['tag'] = array_map( 'sanitize_text_field', wp_unslash( (array) $_POST['tags'] ) );
149 }
150
151 if ( ! array_key_exists( 'page', $_POST ) ) {
152 $args['page'] = 1;
153 } else {
154 $args['page'] = absint( $_POST['page'] );
155 }
156
157 require_once ABSPATH . 'wp-admin/includes/theme.php';
158
159 if ( 'installed' === $theme_action ) {
160
161 // Load all installed themes from wp_prepare_themes_for_js().
162 $themes = array( 'themes' => wp_prepare_themes_for_js() );
163 foreach ( $themes['themes'] as &$theme ) {
164 $theme['type'] = 'installed';
165 $theme['active'] = ( isset( $_POST['customized_theme'] ) && $_POST['customized_theme'] === $theme['id'] );
166 }
167
168 } elseif ( 'catchthemes' === $theme_action ) {
169
170 // Load WordPress.org themes from the .org API and normalize data to match installed theme objects.
171 if ( ! current_user_can( 'install_themes' ) ) {
172 wp_die( -1 );
173 }
174
175 // Arguments for all queries.
176 $wporg_args = array(
177 'per_page' => -1,
178 'fields' => array(
179 'screenshot_url' => true,
180 'description' => true,
181 'rating' => true,
182 'downloaded' => true,
183 'downloadlink' => true,
184 'last_updated' => true,
185 'homepage' => true,
186 'num_ratings' => true,
187 'tags' => true,
188 'parent' => true,
189 // 'extended_author' => true, @todo: WordPress.org throws a 500 server error when this is here.
190 ),
191 );
192
193 $args = array_merge( $wporg_args, $args );
194
195 if ( '' === $args['search'] && '' === $args['tag'] ) {
196 $args['browse'] = 'new'; // Sort by latest themes by default.
197 }
198
199 $args['author'] = 'catchthemes';
200
201 // Load themes from the .org API.
202 $themes = themes_api( 'query_themes', $args );
203 if ( is_wp_error( $themes ) ) {
204 wp_send_json_error();
205 }
206
207 // This list matches the allowed tags in wp-admin/includes/theme-install.php.
208 $themes_allowedtags = array_fill_keys(
209 array( 'a', 'abbr', 'acronym', 'code', 'pre', 'em', 'strong', 'div', 'p', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'img' ),
210 array()
211 );
212 $themes_allowedtags['a'] = array_fill_keys( array( 'href', 'title', 'target' ), true );
213 $themes_allowedtags['acronym']['title'] = true;
214 $themes_allowedtags['abbr']['title'] = true;
215 $themes_allowedtags['img'] = array_fill_keys( array( 'src', 'class', 'alt' ), true );
216
217 // Prepare a list of installed themes to check against before the loop.
218 $installed_themes = array();
219 $wp_themes = wp_get_themes();
220 foreach ( $wp_themes as $theme ) {
221 $installed_themes[] = $theme->get_stylesheet();
222 }
223 $update_php = network_admin_url( 'update.php?action=install-theme' );
224
225 // Set up properties for themes available on WordPress.org.
226 foreach ( $themes->themes as &$theme ) {
227 $theme->install_url = add_query_arg( array(
228 'theme' => $theme->slug,
229 '_wpnonce' => wp_create_nonce( 'install-theme_' . $theme->slug ),
230 ), $update_php );
231
232 $theme->name = wp_kses( $theme->name, $themes_allowedtags );
233 $theme->author = wp_kses( $theme->author, $themes_allowedtags );
234 $theme->version = wp_kses( $theme->version, $themes_allowedtags );
235 $theme->description = wp_kses( $theme->description, $themes_allowedtags );
236 $theme->tags = implode( ', ', $theme->tags );
237 $theme->stars = wp_star_rating( array(
238 'rating' => $theme->rating,
239 'type' => 'percent',
240 'number' => $theme->num_ratings,
241 'echo' => false,
242 ) );
243 $theme->num_ratings = number_format_i18n( $theme->num_ratings );
244 $theme->preview_url = set_url_scheme( $theme->preview_url );
245
246 // Handle themes that are already installed as installed themes.
247 if ( in_array( $theme->slug, $installed_themes, true ) ) {
248 $theme->type = 'installed';
249 } else {
250 $theme->type = $theme_action;
251 }
252
253 // Set active based on customized theme.
254 $theme->active = ( isset( $_POST['customized_theme'] ) && $_POST['customized_theme'] === $theme->slug );
255
256 // Map available theme properties to installed theme properties.
257 $theme->id = $theme->slug;
258 $theme->screenshot = array( $theme->screenshot_url );
259 $theme->authorAndUri = $theme->author;
260 // The .org API can return the full parent theme details if passed the 'parent' arg, or if passed the 'template' option it'll return that in the event it's a child theme.
261 if ( isset( $theme->parent ) ) {
262 $theme->parent = $theme->parent['slug'];
263 } else {
264 $theme->parent = false;
265 }
266 unset( $theme->slug );
267 unset( $theme->screenshot_url );
268 unset( $theme->author );
269 } // End foreach().
270 } elseif ( 'wporg' === $theme_action ) {
271
272 // Load WordPress.org themes from the .org API and normalize data to match installed theme objects.
273 if ( ! current_user_can( 'install_themes' ) ) {
274 wp_die( -1 );
275 }
276
277 // Arguments for all queries.
278 $wporg_args = array(
279 'per_page' => 100,
280 'fields' => array(
281 'screenshot_url' => true,
282 'description' => true,
283 'rating' => true,
284 'downloaded' => true,
285 'downloadlink' => true,
286 'last_updated' => true,
287 'homepage' => true,
288 'num_ratings' => true,
289 'tags' => true,
290 'parent' => true,
291 // 'extended_author' => true, @todo: WordPress.org throws a 500 server error when this is here.
292 ),
293 );
294
295 $args = array_merge( $wporg_args, $args );
296
297 if ( '' === $args['search'] && '' === $args['tag'] ) {
298 $args['browse'] = 'new'; // Sort by latest themes by default.
299 }
300
301 // Load themes from the .org API.
302 $themes = themes_api( 'query_themes', $args );
303 if ( is_wp_error( $themes ) ) {
304 wp_send_json_error();
305 }
306
307 // This list matches the allowed tags in wp-admin/includes/theme-install.php.
308 $themes_allowedtags = array_fill_keys(
309 array( 'a', 'abbr', 'acronym', 'code', 'pre', 'em', 'strong', 'div', 'p', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'img' ),
310 array()
311 );
312 $themes_allowedtags['a'] = array_fill_keys( array( 'href', 'title', 'target' ), true );
313 $themes_allowedtags['acronym']['title'] = true;
314 $themes_allowedtags['abbr']['title'] = true;
315 $themes_allowedtags['img'] = array_fill_keys( array( 'src', 'class', 'alt' ), true );
316
317 // Prepare a list of installed themes to check against before the loop.
318 $installed_themes = array();
319 $wp_themes = wp_get_themes();
320 foreach ( $wp_themes as $theme ) {
321 $installed_themes[] = $theme->get_stylesheet();
322 }
323 $update_php = network_admin_url( 'update.php?action=install-theme' );
324
325 // Set up properties for themes available on WordPress.org.
326 foreach ( $themes->themes as &$theme ) {
327 $theme->install_url = add_query_arg( array(
328 'theme' => $theme->slug,
329 '_wpnonce' => wp_create_nonce( 'install-theme_' . $theme->slug ),
330 ), $update_php );
331
332 $theme->name = wp_kses( $theme->name, $themes_allowedtags );
333 $theme->author = wp_kses( $theme->author, $themes_allowedtags );
334 $theme->version = wp_kses( $theme->version, $themes_allowedtags );
335 $theme->description = wp_kses( $theme->description, $themes_allowedtags );
336 $theme->tags = implode( ', ', $theme->tags );
337 $theme->stars = wp_star_rating( array(
338 'rating' => $theme->rating,
339 'type' => 'percent',
340 'number' => $theme->num_ratings,
341 'echo' => false,
342 ) );
343 $theme->num_ratings = number_format_i18n( $theme->num_ratings );
344 $theme->preview_url = set_url_scheme( $theme->preview_url );
345
346 // Handle themes that are already installed as installed themes.
347 if ( in_array( $theme->slug, $installed_themes, true ) ) {
348 $theme->type = 'installed';
349 } else {
350 $theme->type = $theme_action;
351 }
352
353 // Set active based on customized theme.
354 $theme->active = ( isset( $_POST['customized_theme'] ) && $_POST['customized_theme'] === $theme->slug );
355
356 // Map available theme properties to installed theme properties.
357 $theme->id = $theme->slug;
358 $theme->screenshot = array( $theme->screenshot_url );
359 $theme->authorAndUri = $theme->author;
360 // The .org API can return the full parent theme details if passed the 'parent' arg, or if passed the 'template' option it'll return that in the event it's a child theme.
361 if ( isset( $theme->parent ) ) {
362 $theme->parent = $theme->parent['slug'];
363 } else {
364 $theme->parent = false;
365 }
366 unset( $theme->slug );
367 unset( $theme->screenshot_url );
368 unset( $theme->author );
369 } // End foreach().
370 } // End if().
371
372 /**
373 * Filters the theme data loaded in the customizer.
374 *
375 * This allows theme data to be loading from an external source,
376 * or modification of data loaded from `wp_prepare_themes_for_js()`
377 * or WordPress.org via `themes_api()`.
378 *
379 * @since 4.9.0
380 *
381 * @see wp_prepare_themes_for_js()
382 * @see themes_api()
383 * @see WP_Customize_Manager::__construct()
384 *
385 * @param array $themes Nested array of theme data.
386 * @param array $args List of arguments, such as page, search term, and tags to query for.
387 * @param WP_Customize_Manager $manager Instance of Customize manager.
388 */
389 $themes = apply_filters( 'customize_load_themes', $themes, $args, $wp_customize );
390
391 wp_send_json_success( $themes );
392 }
393
394 /* Plugins */
395 /* Adds Catch Plugins tab in Add Plugin page to show all plugins by Catch Plugins in wordpress.org */
396 public function add_our_plugins_tab($tabs) {
397 // Add our filter here
398 $tabs['catchplugins'] = _x( 'Catch Plugins', 'Plugin Installer' );
399
400 return $tabs;
401 }
402
403 public function catchplugins() {
404 /* From CORE Start */
405 global $paged, $tab;
406 wp_reset_vars( array( 'tab' ) );
407
408 $defined_class = new WP_Plugin_Install_List_Table();
409 $paged = $defined_class->get_pagenum();
410
411 $per_page = 30;
412 //$installed_plugins = catch_get_installed_plugins();
413
414 $args = array(
415 'page' => $paged,
416 'per_page' => $per_page,
417 'fields' => array(
418 'last_updated' => true,
419 'icons' => true,
420 'active_installs' => true
421 ),
422 // Send the locale and installed plugin slugs to the API so it can provide context-sensitive results.
423 'locale' => get_user_locale(),
424 //'installed_plugins' => array_keys( $installed_plugins ),
425 );
426 /* From CORE End */
427
428 // Add author filter for our plugins
429 $args['author'] = 'catchplugins';
430
431 return $args;
432 }
433
434 public function plugins_table() {
435 global $wp_list_table;
436 printf(
437 '<p class="catch-plugins-list">' . __( 'You can use any of our free plugins or premium plugins from <a href="%s" target="_blank">Catch Plugins</a>' ) . '.</p>',
438 'https://catchplugins.com/'
439 );
440 ?>
441 <form id="plugin-filter" method="post">
442 <?php $wp_list_table->display(); ?>
443 </form>
444 <?php
445 }
446 }
447
448 $catchthemes_theme_plugin = new CatchThemesThemePlugin();