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.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / helpers / UrlHelper.php
tutor / helpers Last commit date
ComponentHelper.php 1 day ago DateTimeHelper.php 1 day ago HttpHelper.php 11 months ago PluginInstaller.php 1 year ago QueryHelper.php 1 day ago SessionHelper.php 3 years ago TimerHelper.php 1 day ago UrlHelper.php 1 day ago ValidationHelper.php 10 months ago
UrlHelper.php
193 lines
1 <?php
2 /**
3 * URL helper.
4 *
5 * @package Tutor\Helper
6 * @since 4.0.0
7 */
8
9 namespace Tutor\Helpers;
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Class UrlHelper
15 *
16 * @since 4.0.0
17 */
18 class UrlHelper {
19
20 /**
21 * Get AJAX URL.
22 *
23 * @since 4.0.0
24 *
25 * @return string
26 */
27 public static function ajax(): string {
28 return admin_url( 'admin-ajax.php' );
29 }
30
31 /**
32 * Get plugin asset URL.
33 *
34 * @since 4.0.0
35 *
36 * @param string $path Relative asset path.
37 * @return string
38 */
39 public static function asset( $path = '' ): string {
40 return tutor()->assets_url . $path;
41 }
42
43 /**
44 * Find the first existing file across asset sources for a given path,
45 * respecting kids-mode variant resolution.
46 *
47 * @since 4.0.0
48 *
49 * @param string $path Relative asset path (no leading slash).
50 * @return array{path: string, url: string}|null
51 */
52 public static function resolve_asset( string $path ): ?array {
53 if ( '' === $path ) {
54 return null;
55 }
56
57 $sources = array(
58 array(
59 'path' => tutor()->path . 'assets/',
60 'url' => tutor()->assets_url,
61 ),
62 );
63
64 if ( function_exists( 'tutor_pro' ) ) {
65 $sources[] = array(
66 'path' => tutor_pro()->path . 'assets/',
67 'url' => tutor_pro()->assets,
68 );
69 }
70
71 $candidates = array( $path );
72
73 if ( tutor_utils()->is_kids_mode() && false === strpos( $path, '/kids/' ) ) {
74 $directory = pathinfo( $path, PATHINFO_DIRNAME );
75 $basename = pathinfo( $path, PATHINFO_BASENAME );
76
77 if ( '' !== $basename ) {
78 $kids_path = ( '.' === $directory || '' === $directory )
79 ? 'kids/' . $basename
80 : trailingslashit( $directory ) . 'kids/' . $basename;
81
82 array_unshift( $candidates, $kids_path );
83 }
84 }
85
86 foreach ( $candidates as $candidate ) {
87 foreach ( $sources as $source ) {
88 if ( file_exists( $source['path'] . $candidate ) ) {
89 return array(
90 'path' => $source['path'] . $candidate,
91 'url' => $source['url'] . $candidate,
92 );
93 }
94 }
95 }
96
97 return null;
98 }
99
100 /**
101 * Get a theme-aware plugin asset URL.
102 *
103 * @since 4.0.0
104 *
105 * @param string $path Relative asset path.
106 * @return string
107 */
108 public static function themed_asset( string $path = '' ): string {
109 static $cache = array();
110
111 $path = ltrim( $path, '/' );
112
113 if ( '' === $path ) {
114 return self::asset( $path );
115 }
116
117 if ( isset( $cache[ $path ] ) ) {
118 return $cache[ $path ];
119 }
120
121 $resolved = self::resolve_asset( $path );
122 $cache[ $path ] = $resolved ? $resolved['url'] : self::asset( $path );
123
124 return $cache[ $path ];
125 }
126
127 /**
128 * Get current URL.
129 *
130 * @since 4.0.0
131 *
132 * @return string
133 */
134 public static function current(): string {
135 global $wp;
136
137 return home_url(
138 add_query_arg( array(), $wp->request )
139 );
140 }
141
142 /**
143 * Add query params to URL.
144 *
145 * @since 4.0.0
146 *
147 * @param string $url URL.
148 * @param array $query_params Query params.
149 *
150 * @return string
151 */
152 public static function add_query_params( $url, array $query_params = array() ): string {
153 $url = ltrim( $url, '/' );
154
155 if ( ! empty( $query_params ) ) {
156 $url = add_query_arg( $query_params, $url );
157 }
158
159 return $url;
160 }
161
162 /**
163 * Remove query params from URL.
164 *
165 * @since 4.0.0
166 *
167 * @param string $url URL.
168 * @param array $query_params Query params.
169 *
170 * @return string
171 */
172 public static function remove_query_params( $url, array $query_params = array() ): string {
173 return remove_query_arg( $query_params, $url );
174 }
175
176 /**
177 * Get back URL.
178 *
179 * @since 4.0.0
180 *
181 * @param string $fallback fallback URL.
182 *
183 * @return string
184 */
185 public static function back( $fallback = '' ): string {
186 $back_url = wp_get_referer();
187 if ( empty( $back_url ) ) {
188 $back_url = empty( $fallback ) ? self::current() : $fallback;
189 }
190 return $back_url;
191 }
192 }
193