| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin assets handling. |
| 4 |
* |
| 5 |
* @package System |
| 6 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Decalog\System; |
| 11 |
|
| 12 |
use Decalog\System\Environment; |
| 13 |
use Decalog\System\UUID; |
| 14 |
|
| 15 |
/** |
| 16 |
* The class responsible to handle assets management. |
| 17 |
* |
| 18 |
* @package System |
| 19 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class Assets { |
| 23 |
|
| 24 |
/** |
| 25 |
* Initializes the class and set its properties. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Echoes DNS prefetch. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
*/ |
| 37 |
public function prefetch() { |
| 38 |
if ( Option::network_get( 'use_cdn' ) && DECALOG_CDN_AVAILABLE ) { |
| 39 |
echo '<meta http-equiv="x-dns-prefetch-control" content="on">'; |
| 40 |
echo '<link rel="dns-prefetch" href="//cdn.jsdelivr.net" />'; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Registers (but don't enqueues) a style asset of the plugin. |
| 46 |
* |
| 47 |
* Regarding user's option, asset is ready to enqueue from local plugin dir or from CDN (jsDelivr) |
| 48 |
* |
| 49 |
* @param string $handle Name of the stylesheet. Should be unique. |
| 50 |
* @param string|bool $src Full path of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
| 51 |
* If $src is set to false, stylesheet is an alias of other stylesheets it depends on. |
| 52 |
* @param string $file The style file name. |
| 53 |
* @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. |
| 54 |
* @param string $media Optional. The media for which this stylesheet has been defined. |
| 55 |
* Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like |
| 56 |
* '(orientation: portrait)' and '(max-width: 640px)'. |
| 57 |
* @return bool Whether the style has been registered. True on success, false on failure. |
| 58 |
* @since 1.0.0 |
| 59 |
*/ |
| 60 |
public function register_style( $handle, $src, $file, $deps = [], $media = 'all' ) { |
| 61 |
if ( Option::network_get( 'use_cdn' ) && DECALOG_CDN_AVAILABLE ) { |
| 62 |
if ( DECALOG_ADMIN_URL === $src ) { |
| 63 |
$file = 'https://cdn.jsdelivr.net/wp/' . DECALOG_SLUG . '/tags/' . DECALOG_VERSION . '/admin/' . $file; |
| 64 |
} else { |
| 65 |
$file = 'https://cdn.jsdelivr.net/wp/' . DECALOG_SLUG . '/tags/' . DECALOG_VERSION . '/public/' . $file; |
| 66 |
} |
| 67 |
// phpcs:ignore |
| 68 |
return wp_register_style( $handle, $file, $deps, null, $media ); |
| 69 |
} else { |
| 70 |
if ( Environment::is_plugin_in_production_mode() ) { |
| 71 |
$version = DECALOG_VERSION; |
| 72 |
} else { |
| 73 |
$version = UUID::generate_unique_id( 20 ); |
| 74 |
} |
| 75 |
if ( Environment::is_plugin_in_dev_mode() ) { |
| 76 |
$file = str_replace( '.min', '', $file ); |
| 77 |
} |
| 78 |
return wp_register_style( $handle, $src . $file, $deps, $version, $media ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Registers (but don't enqueues) a script asset of the plugin. |
| 84 |
* |
| 85 |
* Regarding user's option, asset is ready to enqueue from local plugin dir or from CDN (jsDelivr) |
| 86 |
* |
| 87 |
* @param string $handle Name of the script. Should be unique. |
| 88 |
* @param string|bool $src Full path of the script, or path of the script relative to the WordPress root directory. |
| 89 |
* If $src is set to false, script is an alias of other scripts it depends on. |
| 90 |
* @param string $file The style file name. |
| 91 |
* @param array $deps Optional. An array of registered script handles this script depends on. Default empty array. |
| 92 |
* @return bool Whether the script has been registered. True on success, false on failure. |
| 93 |
* @since 1.0.0 |
| 94 |
*/ |
| 95 |
public function register_script( $handle, $src, $file, $deps = [] ) { |
| 96 |
if ( Option::network_get( 'use_cdn' ) && DECALOG_CDN_AVAILABLE ) { |
| 97 |
if ( DECALOG_ADMIN_URL === $src ) { |
| 98 |
$file = 'https://cdn.jsdelivr.net/wp/' . DECALOG_SLUG . '/tags/' . DECALOG_VERSION . '/admin/' . $file; |
| 99 |
} else { |
| 100 |
$file = 'https://cdn.jsdelivr.net/wp/' . DECALOG_SLUG . '/tags/' . DECALOG_VERSION . '/public/' . $file; |
| 101 |
} |
| 102 |
// phpcs:ignore |
| 103 |
return wp_register_script( $handle, $file, $deps, null, Option::network_get( 'script_in_footer' ) ); |
| 104 |
} else { |
| 105 |
if ( Environment::is_plugin_in_production_mode() ) { |
| 106 |
$version = DECALOG_VERSION; |
| 107 |
} else { |
| 108 |
$version = UUID::generate_unique_id( 20 ); |
| 109 |
} |
| 110 |
if ( Environment::is_plugin_in_dev_mode() ) { |
| 111 |
$file = str_replace( '.min', '', $file ); |
| 112 |
} |
| 113 |
return wp_register_script( $handle, $src . $file, $deps, $version, Option::network_get( 'script_in_footer' ) ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
} |