Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Theme
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 register_hooks
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 theme_changed
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 mojo_preview
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 browse_wporg_themes
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace NewfoldLabs\WP\Module\Data\Listeners;
4
5/**
6 * Monitors generic theme events
7 */
8class Theme extends Listener {
9
10    /**
11     * Register the hooks for the listener
12     *
13     * @return void
14     */
15    public function register_hooks() {
16        // Theme changed
17        add_filter( 'pre_update_option_stylesheet', array( $this, 'theme_changed' ), 10, 2 );
18
19        // Mojo theme preview
20        add_action( 'admin_footer', array( $this, 'mojo_preview' ) );
21
22        // Browse theme category .org
23        // @todo Check when switching between categories - may need to use wp.org api filter
24        add_action( 'admin_footer-theme-install.php', array( $this, 'browse_wporg_themes' ) );
25
26        // @todo Need ajax event for Mojo themes
27    }
28
29    /**
30     * Theme changed
31     *
32     * @param string $new_option New theme
33     * @param string $old_option Old theme
34     * @return string The new theme
35     */
36    public function theme_changed( $new_option, $old_option ) {
37        if ( $new_option !== $old_option ) {
38            $data = array(
39                'label_key' => 'new_theme',
40                'old_theme' => $old_option,
41                'new_theme' => $new_option,
42            );
43            $this->push( 'theme_changed', $data );
44        }
45
46        return $new_option;
47    }
48
49    /**
50     * Preview of Mojo Marketplace theme
51     *
52     * @return void
53     */
54    public function mojo_preview() {
55        global $theme;
56        if ( isset( $_GET['page'] ) && 'mojo-theme-preview' === $_GET['page'] && ! is_wp_error( $theme ) ) {
57            $this->push(
58                'mojo_theme_preview',
59                array(
60                    'label_key' => 'theme',
61                    'theme'     => $theme,
62                )
63            );
64        }
65    }
66
67    /**
68     * Browse free wordpress.org themes
69     *
70     * @return void
71     */
72    public function browse_wporg_themes() {
73        $category = ( isset( $_GET['browse'] ) ) ? esc_attr( $_GET['browse'] ) : 'featured';
74        $this->push(
75            'browse_wporg_themes',
76            array(
77                'label_key' => 'category',
78                'category'  => $category,
79            )
80        );
81    }
82}