Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.18% covered (success)
91.18%
31 / 34
66.67% covered (warning)
66.67%
6 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Assets
91.18% covered (success)
91.18%
31 / 34
66.67% covered (warning)
66.67%
6 / 9
19.25
0.00% covered (danger)
0.00%
0 / 1
 name
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 init
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 enqueue_admin_styles
80.00% covered (success)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 enqueue_admin_scripts
83.33% covered (success)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
5.12
 register_login_styles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 enqueue_login_styles
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 register_script
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 register_style
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 get_file_version
75.00% covered (success)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
1<?php
2/**
3 * Assets class.
4 *
5 * This will manage the assets file (css/js)
6 * for adding style and JS functionality.
7 *
8 * @package RtCamp\GoogleLogin
9 * @since   1.0.0
10 */
11
12declare( strict_types=1 );
13
14namespace RtCamp\GoogleLogin\Modules;
15
16use RtCamp\GoogleLogin\Interfaces\Module as ModuleInterface;
17use function RtCamp\GoogleLogin\plugin;
18
19/**
20 * Class Assets
21 *
22 * @package RtCamp\GoogleLogin\Modules
23 */
24class Assets implements ModuleInterface {
25
26    /**
27     * Handle for login button style.
28     *
29     * @var string
30     */
31    const LOGIN_BUTTON_STYLE_HANDLE = 'login-with-google';
32
33    /**
34     * Handle for Settings Page style.
35     *
36     * @var string
37     */
38    const SETTINGS_PAGE_STYLE_HANDLE = 'login-with-google-settings';
39
40    /**
41     * Handle for Settings Page assets.
42     *
43     * @var string
44     */
45    const SETTINGS_PAGE_SCRIPT_HANDLE = 'login-with-google-settings-script';
46
47    /**
48     * Settings page hook suffix.
49     *
50     * @var string
51     */
52    const SETTINGS_PAGE_HOOK_SUFFIX = 'settings_page_login-with-google';
53
54    /**
55     * Module name.
56     *
57     * @return string
58     */
59    public function name(): string {
60        return 'assets';
61    }
62
63    /**
64     * Initialize the assets file
65     *
66     * @return void
67     */
68    public function init(): void {
69        add_action( 'login_enqueue_scripts', [ $this, 'enqueue_login_styles' ] );
70        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ]  );
71        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_styles' ]  );
72    }
73
74    /**
75     * Enqueue styles for admin.
76     *
77     * @param string $hook_suffix Current page hook.
78     *
79     * @return void
80     */
81    public function enqueue_admin_styles( string $hook_suffix ): void {
82        if ( 'settings_page_login-with-google' !== $hook_suffix ) {
83            return;
84        }
85
86        if ( ! wp_style_is( self::SETTINGS_PAGE_STYLE_HANDLE, 'registered' ) ) {
87            $this->register_style( self::SETTINGS_PAGE_STYLE_HANDLE, 'build/css/settings.css' );
88        }
89
90        wp_enqueue_style( self::SETTINGS_PAGE_STYLE_HANDLE );
91    }
92
93    /**
94     * Enqueue scripts and styles for admin.
95     *
96     * @param string $hook_suffix Current page hook.
97     *
98     * @return void
99     */
100    public function enqueue_admin_scripts( string $hook_suffix ): void {
101        if ( 'settings_page_login-with-google' !== $hook_suffix ) {
102            return;
103        }
104
105        $filename = ( defined( 'WP_SCRIPT_DEBUG' ) && true === WP_SCRIPT_DEBUG ) ? 'settings.min.js' : 'settings.js';
106
107        if ( ! wp_script_is( self::SETTINGS_PAGE_SCRIPT_HANDLE, 'registered' ) ) {
108            $this->register_script( self::SETTINGS_PAGE_SCRIPT_HANDLE, 'build/js/' . $filename );
109        }
110
111        wp_enqueue_script( self::SETTINGS_PAGE_SCRIPT_HANDLE );
112    }
113
114    /**
115     * Register style/script for Login Page.
116     *
117     * @action login_enqueue_scripts
118     *
119     * @return void
120     */
121    public function register_login_styles(): void {
122        $this->register_style( self::LOGIN_BUTTON_STYLE_HANDLE, 'build/css/login.css' );
123    }
124
125    /**
126     * Enqueue the login style.
127     *
128     * @return void
129     */
130    public function enqueue_login_styles(): void {
131        /**
132         * If style is not registered, register it.
133         */
134        if ( ! wp_style_is( self::LOGIN_BUTTON_STYLE_HANDLE, 'registered' ) ) {
135            $this->register_login_styles();
136        }
137
138        if ( ! wp_script_is( 'login-with-google-script', 'registered' ) ) {
139            $this->register_script( 'login-with-google-script', 'build/js/login.js' );
140        }
141
142        wp_enqueue_script( 'login-with-google-script' );
143        wp_enqueue_style( self::LOGIN_BUTTON_STYLE_HANDLE );
144    }
145
146    /**
147     * Register a new script.
148     *
149     * @param  string           $handle    Name of the script. Should be unique.
150     * @param  string|bool      $file      script file, path of the script relative to the assets/build/ directory.
151     * @param  array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
152     * @param  string|bool|null $ver       Optional. String specifying script version number, if not set, filetime will be used as version number.
153     * @param  bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
154     *                                     Default 'false'.
155     * @return bool Whether the script has been registered. True on success, false on failure.
156     */
157    public function register_script( $handle, $file, $deps = [], $ver = false, $in_footer = true ) {
158        $src     = sprintf( '%1$sassets/%2$s', plugin()->url, $file );
159        $version = $this->get_file_version( $file, $ver );
160
161        return wp_register_script( $handle, $src, $deps, $version, $in_footer );
162    }
163
164    /**
165     * Register a CSS stylesheet.
166     *
167     * @param string           $handle Name of the stylesheet. Should be unique.
168     * @param string|bool      $file   style file, path of the script relative to the assets/build/ directory.
169     * @param array            $deps   Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
170     * @param string|bool|null $ver    Optional. String specifying script version number, if not set, filetime will be used as version number.
171     * @param string           $media  Optional. The media for which this stylesheet has been defined.
172     *                                 Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
173     *                                 '(orientation: portrait)' and '(max-width: 640px)'.
174     *
175     * @return bool Whether the style has been registered. True on success, false on failure.
176     */
177    public function register_style( $handle, $file, $deps = [], $ver = false, $media = 'all' ) {
178        $src     = sprintf( '%1$sassets/%2$s', plugin()->url, $file );
179        $version = $this->get_file_version( $file, $ver );
180
181        return wp_register_style( $handle, $src, $deps, $version, $media );
182    }
183
184    /**
185     * Get file version.
186     *
187     * @param string             $file File path.
188     * @param int|string|boolean $ver  File version.
189     *
190     * @return bool|false|int
191     */
192    private function get_file_version( $file, $ver = false ) {
193        if ( ! empty( $ver ) ) {
194            return $ver;
195        }
196
197        $file_path = sprintf( '%s/%s', plugin()->assets_dir, $file );
198
199        return file_exists( $file_path ) ? filemtime( $file_path ) : false;
200    }
201}