PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.1.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.1.0
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / customizer / packages / utils / url-getter / src / URL.php
kirki / customizer / packages / utils / url-getter / src Last commit date
URL.php 5 months ago
URL.php
204 lines
1 <?php
2 /**
3 * Get the URL of any file in WordPress.
4 *
5 * @package kirki-framework/url-getter
6 * @author Themeum
7 * @copyright Copyright (c) 2023, Themeum
8 * @license https://opensource.org/licenses/MIT
9 * @since 1.0
10 */
11
12 namespace Kirki;
13
14 /**
15 * A collection of methods to get the URL of files.
16 *
17 * @since 1.0
18 */
19 class URL {
20
21 /**
22 * An array of instances.
23 *
24 * Used for performance reasons in case we need
25 * the same url over and over again.
26 *
27 * @static
28 * @access private
29 * @since 1.0.2
30 * @var array
31 */
32 private static $instances = [];
33
34 /**
35 * The file path.
36 *
37 * @access private
38 * @since 1.0
39 * @var string
40 */
41 private $path;
42
43 /**
44 * The content path.
45 *
46 * @static
47 * @access private
48 * @since 1.0
49 * @var string
50 */
51 private static $content_path;
52
53 /**
54 * The content RL.
55 *
56 * @static
57 * @access private
58 * @since 1.0
59 * @var string
60 */
61 private static $content_url;
62
63 /**
64 * The file URL.
65 *
66 * @access private
67 * @since 1.0
68 * @var string
69 */
70 private $url;
71
72 /**
73 * Gets an instance based on the path.
74 *
75 * @static
76 * @access public
77 * @since 1.0.2
78 * @param string $path Absolute path to a file.
79 * @return URL An instance of this object.
80 */
81 public static function get_instance( $path ) {
82 $path = \wp_normalize_path( $path );
83 if ( ! isset( self::$instances[ $path ] ) ) {
84 self::$instances[ $path ] = new self( $path );
85 }
86 return self::$instances[ $path ];
87 }
88
89 /**
90 * Constructor.
91 *
92 * @access private
93 * @since 1.0
94 * @param string $path Absolute path to a file.
95 */
96 private function __construct( $path ) {
97 $this->path = ( $path );
98 $this->set_content_url();
99 $this->set_content_path();
100 }
101
102 /**
103 * Get a URL from a path.
104 *
105 * @static
106 * @access public
107 * @since 1.0.2
108 * @param string $path The file path.
109 * @return string
110 */
111 public static function get_from_path( $path ) {
112 return self::get_instance( $path )->get_url();
113 }
114
115 /**
116 * Get the file URL.
117 *
118 * @access public
119 * @since 1.0
120 * @return string
121 */
122 public function get_url() {
123
124 /**
125 * Start by replacing ABSPATH with site_url.
126 * This is not accurate at all and only serves as a fallback in case everything else fails.
127 */
128 $this->url = \str_replace( ABSPATH, \trailingslashit( \site_url() ), $this->path );
129
130 /**
131 * If the file-path is inside wp-content replace the content-path with the content-url.
132 * This serves as a fallback in case the other tests below fail.
133 */
134 if ( false !== \strpos( $this->path, self::$content_path ) ) {
135 $this->url = \str_replace( self::$content_path, self::$content_url, $this->path );
136 }
137
138 /**
139 * If the file is in a parent theme use the template directory.
140 */
141 if ( $this->in_parent_theme() ) {
142 $this->url = \get_template_directory_uri() . \str_replace( \get_template_directory(), '', $this->path );
143 }
144
145 /**
146 * If the file is in a child-theme use the stylesheet directory.
147 */
148 if ( ! $this->in_parent_theme() && $this->in_child_theme() ) {
149 $this->url = \get_stylesheet_directory_uri() . \str_replace( \get_stylesheet_directory(), '', $this->path );
150 }
151
152 $this->url = \set_url_scheme( $this->url );
153 return \apply_filters( 'kirki_path_url', $this->url, $this->path );
154 }
155
156 /**
157 * Check if the path is inside a parent theme.
158 *
159 * @access public
160 * @since 1.0
161 * @return bool
162 */
163 public function in_parent_theme() {
164 return ( 0 === \strpos( $this->path, \get_template_directory() ) );
165 }
166
167 /**
168 * Check if the path is inside a child theme.
169 *
170 * @access public
171 * @since 1.0
172 * @return bool
173 */
174 public function in_child_theme() {
175 return ( 0 === \strpos( $this->path, \get_stylesheet_directory() ) );
176 }
177
178 /**
179 * Set the $content_url.
180 *
181 * @access private
182 * @since 1.0
183 * @return void
184 */
185 private function set_content_url() {
186 if ( ! self::$content_url ) {
187 self::$content_url = \untrailingslashit( \content_url() );
188 }
189 }
190
191 /**
192 * Set the $content_path.
193 *
194 * @access private
195 * @since 1.0
196 * @return void
197 */
198 private function set_content_path() {
199 if ( ! self::$content_path ) {
200 self::$content_path = \wp_normalize_path( \untrailingslashit( WP_CONTENT_DIR ) );
201 }
202 }
203 }
204