PluginProbe
JCH Optimize / trunk
JCH Optimize vtrunk
6.0.2 6.0.1 trunk 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.10.0 2.2.0 2.2.1 All 58 releases
jch-optimize / src / Platform / Paths.php

Paths.php in JCH Optimize trunk, at src/Platform/Paths.php

309 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * JCH Optimize - Performs several front-end optimizations for fast downloads
5 *
6 * @package jchoptimize/wordpress-platform
7 * @author Samuel Marshall <samuel@jch-optimize.net>
8 * @copyright Copyright (c) 2020 Samuel Marshall / JCH Optimize
9 * @license GNU/GPLv3, or later. See LICENSE file
10 *
11 * If LICENSE file missing, see <http://www.gnu.org/licenses/>.
12 */
13
14 namespace JchOptimize\WordPress\Platform;
15
16 defined('_WP_EXEC') or die('Restricted access');
17
18 use JchOptimize\Core\Helper;
19 use JchOptimize\Core\Platform\PathsInterface;
20 use JchOptimize\Core\SystemUri;
21 use JchOptimize\Core\Uri\Utils;
22
23 use function add_query_arg;
24 use function admin_url;
25 use function dirname;
26 use function get_current_blog_id;
27 use function get_option;
28 use function home_url;
29 use function is_multisite;
30 use function rtrim;
31 use function set_url_scheme;
32 use function str_replace;
33 use function trailingslashit;
34 use function wp_upload_dir;
35
36 use const DIRECTORY_SEPARATOR;
37 use const JCH_CACHE_DIR;
38
39 class Paths implements PathsInterface
40 {
41 public function mediaUrl(): string
42 {
43 return JCH_PLUGIN_URL . 'media';
44 }
45
46 /**
47 * Find the absolute path to a resource given a root relative path
48 *
49 * @param string $url Root relative path of resource on the site
50 *
51 * @return string
52 */
53 public function absolutePath(string $url): string
54 {
55 $home_path = trailingslashit(self::rootPath());
56 $rootPath = str_replace('/\\', DIRECTORY_SEPARATOR, $home_path);
57
58 //We can now concatenate root path to url path to get absolute path on filesystem
59 return rtrim($rootPath, '/\\') . DIRECTORY_SEPARATOR . ltrim($url, '\\/');
60 }
61
62 /**
63 * @return string Absolute path to root of site
64 */
65 public function rootPath(): string
66 {
67 $home = set_url_scheme((string)get_option('home'), 'http');
68 $site_url = set_url_scheme((string)get_option('siteurl'), 'http');
69
70 if (!empty($home) && 0 < strcasecmp($home, $site_url)) {
71 $wp_path_rel_to_home = str_ireplace($home, '', $site_url); /* $site_url - $home */
72 $pos = strripos(
73 str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']),
74 trailingslashit($wp_path_rel_to_home)
75 );
76 $home_path = substr($_SERVER['SCRIPT_FILENAME'], 0, $pos);
77 } elseif (!empty($home) && 0 > strcasecmp($home, $site_url)) {
78 $wp_path_rel_to_home = str_ireplace($home, '', $site_url); /* $site_url - $home */
79 $pos = strripos(str_replace('\\', '/', ABSPATH), trailingslashit($wp_path_rel_to_home));
80 $home_path = substr(ABSPATH, 0, $pos);
81 } else {
82 $home_path = ABSPATH;
83 }
84
85 return untrailingslashit($home_path);
86 }
87
88 public function basePath(): string
89 {
90 return $this->rootPath();
91 }
92
93 /**
94 * Returns root relative path to the /assets/ folder
95 *
96 * @param bool $pathonly
97 *
98 * @return string
99 */
100 public function relAssetPath(bool $pathonly = false): string
101 {
102 if ($pathonly) {
103 return SystemUri::currentBasePath() . 'jch-optimize/media/assets';
104 }
105
106 return plugins_url() . '/jch-optimize/media/assets';
107 }
108
109 /**
110 * The base folder for rewrites when the combined files are delivered with PHP using mod_rewrite. Generally the parent directory for the
111 * /media/ folder with a root relative path
112 *
113 * @return string
114 */
115 public function rewriteBaseFolder(): string
116 {
117 static $rewrite_base;
118
119 if (!isset($rewrite_base)) {
120 $uri = Utils::uriFor(plugins_url());
121 $rewrite_base = trailingslashit($uri->getPath());
122 }
123
124 return $rewrite_base;
125 }
126
127 /**
128 * Path to the directory where generated sprite images are saved
129 *
130 * @param bool $isRootRelative If true, return the root relative path; if false, return the absolute path.
131 *
132 * @return string
133 */
134 public function spritePath(bool $isRootRelative = false): string
135 {
136 if ($isRootRelative) {
137 return JCH_PLUGIN_URL . 'media/sprites';
138 }
139
140 return JCH_PLUGIN_DIR . 'media/sprites';
141 }
142
143 /**
144 * Convert the absolute filepath of a resource to a url
145 *
146 * @param string $path Absolute path of resource
147 *
148 * @return string
149 */
150 public function path2Url(string $path): string
151 {
152 $oUri = Utils::uriFor(SystemUri::toString());
153 $sBaseFolder = SystemUri::currentBasePath();
154
155 $abs_path = str_replace(DIRECTORY_SEPARATOR, '/', self::rootPath());
156 $path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
157
158 return (string)$oUri->withPath(
159 $sBaseFolder .
160 (str_replace($abs_path . DIRECTORY_SEPARATOR, '', $path))
161 );
162 }
163
164 /**
165 * Parent directory of the folder where the original images are backed up in the Optimize Image Feature
166 *
167 * @return string
168 */
169 public function backupImagesParentDir(): string
170 {
171 return WP_CONTENT_DIR . DIRECTORY_SEPARATOR;
172 }
173
174 public function nextGenImagesPath(bool $isRootRelative = false): string
175 {
176 $wp_upload_dir = wp_upload_dir();
177 $dir = '/jch-optimize/ng';
178
179 if ($isRootRelative) {
180 $uri = Utils::uriFor($wp_upload_dir['baseurl'] . $dir);
181
182 return (string)$uri->withScheme('')->withHost('')->withUserInfo('');
183 }
184
185 return $wp_upload_dir['basedir'] . $dir;
186 }
187
188 public function iconsUrl(): string
189 {
190 return JCH_PLUGIN_URL . 'media/core/icons';
191 }
192
193 public function getLogsPath(): string
194 {
195 return JCH_PLUGIN_DIR . 'logs';
196 }
197
198 public function homeBasePath(): string
199 {
200 $home = (string) get_option('home');
201 $home = set_url_scheme($home, 'relative');
202
203 return trailingslashit($home);
204 }
205
206 public function homeBaseFullPath(): string
207 {
208 $home = (string) get_option('home');
209 $home = set_url_scheme($home);
210
211 return trailingslashit($home);
212 }
213
214 /**
215 * Url used in administrator settings page to perform certain tasks
216 *
217 * @param string $name
218 *
219 * @return string
220 */
221 public function adminController(string $name): string
222 {
223 $url = add_query_arg(['task' => $name], admin_url('admin-ajax.php?action=onclickicon'));
224
225 return wp_nonce_url($url, $name);
226 }
227
228 /**
229 * Url to access Ajax functionality
230 *
231 * @param string $function Action to be performed by Ajax function
232 *
233 * @return string
234 */
235 public function ajaxUrl(string $function): string
236 {
237 return add_query_arg(['action' => $function], admin_url('admin-ajax.php'));
238 }
239
240 public function captureCacheDir(bool $isRootRelative = false): string
241 {
242 $captureCacheDir = self::cachePath($isRootRelative) . '/html';
243
244 if ($isRootRelative) {
245 $captureCacheURI = Utils::uriFor($captureCacheDir);
246
247 return $captureCacheURI->getPath();
248 }
249
250 return $captureCacheDir;
251 }
252
253 /**
254 * Returns path to the directory where static combined css/js files are saved.
255 *
256 * @param bool $isRootRelative If true, returns root relative path, otherwise, the absolute path
257 *
258 * @return string
259 */
260 public function cachePath(bool $isRootRelative = true): string
261 {
262 $id = '';
263
264 if (is_multisite()) {
265 $id = get_current_blog_id();
266 }
267
268 if ($isRootRelative) {
269 return JCH_CACHE_URL . 'assets' . $id;
270 } else {
271 return JCH_CACHE_DIR . 'assets' . $id;
272 }
273 }
274
275 public function cacheDir(): string
276 {
277 return JCH_CACHE_DIR;
278 }
279
280 public function templateCachePath(): string
281 {
282 return JCH_CACHE_DIR . 'compiled_templates';
283 }
284
285 public function templatePath(): string
286 {
287 return dirname(__FILE__, 3) . '/tmpl';
288 }
289
290 public function responsiveImagePath(bool $isRootRelative = false): string
291 {
292 $wp_upload_dir = wp_upload_dir();
293 $dir = '/jch-optimize/rs';
294
295 if ($isRootRelative) {
296 $uri = Utils::uriFor($wp_upload_dir['baseurl'] . $dir);
297
298 return (string)$uri->withScheme('')->withHost('')->withUserInfo('');
299 }
300
301 return $wp_upload_dir['basedir'] . $dir;
302 }
303
304 public function liveSite(): string
305 {
306 return home_url('/');
307 }
308 }
309