1<?php
2/**
3 * Copyright © 2019-2026 Rhubarb Tech Inc. All Rights Reserved.
4 *
5 * The Object Cache Pro Software and its related materials are property and confidential
6 * information of Rhubarb Tech Inc. Any reproduction, use, distribution, or exploitation
7 * of the Object Cache Pro Software and its related materials, in whole or in part,
8 * is strictly forbidden unless prior permission is obtained from Rhubarb Tech Inc.
9 *
10 * In addition, any reproduction, use, distribution, or exploitation of the Object Cache Pro
11 * Software and its related materials, in whole or in part, is subject to the End-User License
12 * Agreement accessible in the included `LICENSE` file, or at: https://objectcache.pro/eula
13 */
14
15declare(strict_types=1);
16
17namespace RedisCachePro\Plugin;
18
19use RedisCachePro\Diagnostics\Diagnostics;
20
21/**
22 * @mixin \RedisCachePro\Plugin
23 */
24trait Assets
25{
26 /**
27 * Boot Assets component.
28 *
29 * @return void
30 */
31 public function bootAssets()
32 {
33 //
34 }
35
36 /**
37 * Returns the URL to the given asset.
38 *
39 * @param string $path
40 * @return string|false
41 */
42 public function asset($path)
43 {
44 if (Diagnostics::isMustUse()) {
45 $plugin = $this->muAssetPath();
46
47 return $plugin
48 ? plugins_url("resources/{$path}", $plugin)
49 : false;
50 }
51
52 return plugins_url("resources/{$path}", $this->filename);
53 }
54
55 /**
56 * Returns the contents of the given asset.
57 *
58 * @param string $path
59 * @return string
60 */
61 public function inlineAsset($path)
62 {
63 $asset = (string) file_get_contents(
64 "{$this->directory}/resources/{$path}"
65 );
66
67 if (! defined('SCRIPT_DEBUG') || ! SCRIPT_DEBUG) {
68 $asset = preg_replace('/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|")\/\/.*))/', '', $asset);
69 $asset = preg_replace('/(\v|\s{2,})/', ' ', $asset);
70 $asset = preg_replace('/\s+/', ' ', $asset);
71 $asset = trim($asset);
72 }
73
74 return $asset;
75 }
76
77 /**
78 * Returns the must-use plugin path for usage with `plugins_url()`.
79 *
80 * @return string|null
81 */
82 protected function muAssetPath()
83 {
84 static $path;
85
86 if (! $path) {
87 $paths = [
88 defined('WP_REDIS_DIR') ? rtrim(WP_REDIS_DIR, '/') : '',
89 WPMU_PLUGIN_DIR . '/redis-cache-pro',
90 WPMU_PLUGIN_DIR . '/object-cache-pro',
91 ];
92
93 foreach ($paths as $mupath) {
94 if (strpos($mupath, WPMU_PLUGIN_DIR) === 0 && file_exists("{$mupath}/api.php")) {
95 $path = "{$mupath}/RedisWannaMine.php";
96 }
97 }
98 }
99
100 return $path;
101 }
102}
103