Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.24% |
20 / 21 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Assets | |
95.24% |
20 / 21 |
|
85.71% |
6 / 7 |
11 | |
0.00% |
0 / 1 |
| name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| init | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register_login_styles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| enqueue_login_styles | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| register_script | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| register_style | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| get_file_version | |
75.00% |
3 / 4 |
|
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 | |
| 12 | declare( strict_types=1 ); |
| 13 | |
| 14 | namespace RtCamp\GoogleLogin\Modules; |
| 15 | |
| 16 | use RtCamp\GoogleLogin\Interfaces\Module as ModuleInterface; |
| 17 | use function RtCamp\GoogleLogin\plugin; |
| 18 | |
| 19 | /** |
| 20 | * Class Assets |
| 21 | * |
| 22 | * @package RtCamp\GoogleLogin\Modules |
| 23 | */ |
| 24 | class Assets implements ModuleInterface { |
| 25 | |
| 26 | /** |
| 27 | * Module name. |
| 28 | * |
| 29 | * @return string |
| 30 | */ |
| 31 | public function name(): string { |
| 32 | return 'assets'; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Initialize the assets file |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function init(): void { |
| 41 | add_action( 'login_enqueue_scripts', [ $this, 'enqueue_login_styles' ] ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Register style/script for Login Page. |
| 46 | * |
| 47 | * @action login_enqueue_scripts |
| 48 | * |
| 49 | * @return void |
| 50 | */ |
| 51 | public function register_login_styles(): void { |
| 52 | $this->register_style( 'login-with-google', 'build/css/login.css' ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Enqueue the login stylesheet. |
| 57 | * |
| 58 | * @return void |
| 59 | */ |
| 60 | public function enqueue_login_styles(): void { |
| 61 | /** |
| 62 | * If style is not registered, register it. |
| 63 | */ |
| 64 | if ( ! wp_style_is( 'login-with-google', 'registered' ) ) { |
| 65 | $this->register_login_styles(); |
| 66 | } |
| 67 | |
| 68 | if ( ! wp_script_is( 'login-with-google-script', 'registered' ) ) { |
| 69 | $this->register_script( 'login-with-google-script', 'build/js/login.js' ); |
| 70 | } |
| 71 | |
| 72 | wp_enqueue_script( 'login-with-google-script' ); |
| 73 | wp_enqueue_style( 'login-with-google' ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Register a new script. |
| 78 | * |
| 79 | * @param string $handle Name of the script. Should be unique. |
| 80 | * @param string|bool $file script file, path of the script relative to the assets/build/ directory. |
| 81 | * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array. |
| 82 | * @param string|bool|null $ver Optional. String specifying script version number, if not set, filetime will be used as version number. |
| 83 | * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. |
| 84 | * Default 'false'. |
| 85 | * @return bool Whether the script has been registered. True on success, false on failure. |
| 86 | */ |
| 87 | public function register_script( $handle, $file, $deps = [], $ver = false, $in_footer = true ) { |
| 88 | $src = sprintf( '%1$sassets/%2$s', plugin()->url, $file ); |
| 89 | $version = $this->get_file_version( $file, $ver ); |
| 90 | |
| 91 | return wp_register_script( $handle, $src, $deps, $version, $in_footer ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Register a CSS stylesheet. |
| 96 | * |
| 97 | * @param string $handle Name of the stylesheet. Should be unique. |
| 98 | * @param string|bool $file style file, path of the script relative to the assets/build/ directory. |
| 99 | * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. |
| 100 | * @param string|bool|null $ver Optional. String specifying script version number, if not set, filetime will be used as version number. |
| 101 | * @param string $media Optional. The media for which this stylesheet has been defined. |
| 102 | * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like |
| 103 | * '(orientation: portrait)' and '(max-width: 640px)'. |
| 104 | * |
| 105 | * @return bool Whether the style has been registered. True on success, false on failure. |
| 106 | */ |
| 107 | public function register_style( $handle, $file, $deps = [], $ver = false, $media = 'all' ) { |
| 108 | $src = sprintf( '%1$sassets/%2$s', plugin()->url, $file ); |
| 109 | $version = $this->get_file_version( $file, $ver ); |
| 110 | |
| 111 | return wp_register_style( $handle, $src, $deps, $version, $media ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get file version. |
| 116 | * |
| 117 | * @param string $file File path. |
| 118 | * @param int|string|boolean $ver File version. |
| 119 | * |
| 120 | * @return bool|false|int |
| 121 | */ |
| 122 | private function get_file_version( $file, $ver = false ) { |
| 123 | if ( ! empty( $ver ) ) { |
| 124 | return $ver; |
| 125 | } |
| 126 | |
| 127 | $file_path = sprintf( '%s/%s', plugin()->assets_dir, $file ); |
| 128 | |
| 129 | return file_exists( $file_path ) ? filemtime( $file_path ) : false; |
| 130 | } |
| 131 | } |