PluginProbe
OPcache Manager / 3.3.0
OPcache Manager v3.3.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 2.0.0 2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.14.0 2.2.0 2.3.0 2.3.1 2.3.2 2.4.0 2.5.0 2.6.0 All 36 releases
opcache-manager / includes / system / class-assets.php

class-assets.php in OPcache Manager 3.3.0, at includes/system/class-assets.php

119 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 OPcacheManager\System;
11
12 use OPcacheManager\System\Environment;
13 use OPcacheManager\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 /**
26 * Initializes the class and set its properties.
27 *
28 * @since 1.0.0
29 */
30 public function __construct() {
31 }
32
33 /**
34 * Echoes DNS prefetch.
35 *
36 * @since 1.0.0
37 */
38 public function prefetch() {
39 if ( Option::network_get( 'use_cdn' ) && OPCM_CDN_AVAILABLE ) {
40 echo '<meta http-equiv="x-dns-prefetch-control" content="on">';
41 echo '<link rel="dns-prefetch" href="//cdn.jsdelivr.net" />';
42 }
43 }
44
45 /**
46 * Registers (but don't enqueues) a style asset of the plugin.
47 *
48 * Regarding user's option, asset is ready to enqueue from local plugin dir or from CDN (jsDelivr)
49 *
50 * @param string $handle Name of the stylesheet. Should be unique.
51 * @param string|bool $src Full path of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
52 * If $src is set to false, stylesheet is an alias of other stylesheets it depends on.
53 * @param string $file The style file name.
54 * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
55 * @param string $media Optional. The media for which this stylesheet has been defined.
56 * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
57 * '(orientation: portrait)' and '(max-width: 640px)'.
58 * @return bool Whether the style has been registered. True on success, false on failure.
59 * @since 1.0.0
60 */
61 public function register_style( $handle, $src, $file, $deps = [], $media = 'all' ) {
62 if ( Option::network_get( 'use_cdn' ) && OPCM_CDN_AVAILABLE ) {
63 if ( OPCM_ADMIN_URL === $src ) {
64 $file = 'https://cdn.jsdelivr.net/wp/' . OPCM_SLUG . '/tags/' . OPCM_VERSION . '/admin/' . $file;
65 } else {
66 $file = 'https://cdn.jsdelivr.net/wp/' . OPCM_SLUG . '/tags/' . OPCM_VERSION . '/public/' . $file;
67 }
68 // phpcs:ignore
69 return wp_register_style( $handle, $file, $deps, null, $media );
70 } else {
71 if ( Environment::is_plugin_in_production_mode() ) {
72 $version = OPCM_VERSION;
73 } else {
74 $version = UUID::generate_unique_id( 20 );
75 }
76 if ( Environment::is_plugin_in_dev_mode() ) {
77 $file = str_replace( '.min', '', $file );
78 }
79 return wp_register_style( $handle, $src . $file, $deps, $version, $media );
80 }
81 }
82
83 /**
84 * Registers (but don't enqueues) a script asset of the plugin.
85 *
86 * Regarding user's option, asset is ready to enqueue from local plugin dir or from CDN (jsDelivr)
87 *
88 * @param string $handle Name of the script. Should be unique.
89 * @param string|bool $src Full path of the script, or path of the script relative to the WordPress root directory.
90 * If $src is set to false, script is an alias of other scripts it depends on.
91 * @param string $file The style file name.
92 * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
93 * @return bool Whether the script has been registered. True on success, false on failure.
94 * @since 1.0.0
95 */
96 public function register_script( $handle, $src, $file, $deps = [] ) {
97 if ( Option::network_get( 'use_cdn' ) && OPCM_CDN_AVAILABLE ) {
98 if ( OPCM_ADMIN_URL === $src ) {
99 $file = 'https://cdn.jsdelivr.net/wp/' . OPCM_SLUG . '/tags/' . OPCM_VERSION . '/admin/' . $file;
100 } else {
101 $file = 'https://cdn.jsdelivr.net/wp/' . OPCM_SLUG . '/tags/' . OPCM_VERSION . '/public/' . $file;
102 }
103 // phpcs:ignore
104 return wp_register_script( $handle, $file, $deps, null, Option::network_get( 'script_in_footer' ) );
105 } else {
106 if ( Environment::is_plugin_in_production_mode() ) {
107 $version = OPCM_VERSION;
108 } else {
109 $version = UUID::generate_unique_id( 20 );
110 }
111 if ( Environment::is_plugin_in_dev_mode() ) {
112 $file = str_replace( '.min', '', $file );
113 }
114 return wp_register_script( $handle, $src . $file, $deps, $version, Option::network_get( 'script_in_footer' ) );
115 }
116 }
117
118 }
119