admin
3 months ago
field-groups
3 months ago
fields
1 week ago
fields-settings
3 months ago
forms
3 months ago
locations
3 months ago
modules
1 week ago
screens
3 months ago
acfe-deprecated-functions.php
2 years ago
acfe-field-functions.php
3 months ago
acfe-field-group-functions.php
3 months ago
acfe-file-functions.php
1 year ago
acfe-form-functions.php
3 months ago
acfe-helper-array-functions.php
3 months ago
acfe-helper-functions.php
3 months ago
acfe-helper-multi-functions.php
3 months ago
acfe-helper-string-functions.php
3 months ago
acfe-meta-functions.php
3 months ago
acfe-post-functions.php
3 months ago
acfe-screen-functions.php
3 months ago
acfe-template-functions.php
3 months ago
acfe-term-functions.php
3 months ago
acfe-user-functions.php
3 months ago
acfe-wp-functions.php
3 years ago
assets.php
3 months ago
compatibility-acf-5.8.php
4 months ago
compatibility-acf-5.9.php
3 months ago
compatibility-acf-6.5.php
3 months ago
compatibility-acf-6.8.6.php
1 week ago
compatibility.php
3 months ago
field-extend.php
4 months ago
field.php
4 months ago
hooks.php
3 months ago
init.php
3 months ago
local-meta.php
3 years ago
media.php
3 months ago
module-acf.php
3 months ago
module-db.php
3 months ago
module-l10n.php
3 months ago
module-legacy.php
3 years ago
module-manager.php
4 months ago
module-post.php
3 months ago
module-posts.php
4 months ago
module-upgrades.php
3 years ago
module.php
3 months ago
multilang.php
3 months ago
revisions.php
4 months ago
screen.php
3 months ago
settings.php
3 months ago
template-tags.php
3 months ago
third-party.php
3 years ago
upgrades.php
3 months ago
acfe-file-functions.php
278 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * acfe_get_abs_path_to_url |
| 9 | * |
| 10 | * Converts "/url" to "https://www.domain.com/url" |
| 11 | * |
| 12 | * @param string $path |
| 13 | * |
| 14 | * @return string |
| 15 | */ |
| 16 | function acfe_get_abs_path_to_url($path = ''){ |
| 17 | |
| 18 | $abspath = untrailingslashit(ABSPATH); |
| 19 | |
| 20 | $url = str_replace($abspath, site_url(), $path); |
| 21 | $url = wp_normalize_path($url); |
| 22 | |
| 23 | return esc_url_raw($url); |
| 24 | |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * acfe_locate_file_url |
| 29 | * |
| 30 | * Similar to locate_template(), but locate File URL instead |
| 31 | * Check if file exists locally and return URL (supports parent/child theme) |
| 32 | * |
| 33 | * @param $filenames |
| 34 | * |
| 35 | * @return mixed|string |
| 36 | */ |
| 37 | function acfe_locate_file_url($filenames){ |
| 38 | |
| 39 | $located = ''; |
| 40 | |
| 41 | foreach((array) $filenames as $filename){ |
| 42 | |
| 43 | if(!$filename){ |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | // Direct URL: https://www.domain.com/folder/file.js |
| 48 | if(stripos($filename, 'http://') === 0 || stripos($filename, 'https://') === 0 || stripos($filename, '//') === 0){ |
| 49 | |
| 50 | $located = $filename; |
| 51 | break; |
| 52 | |
| 53 | }else{ |
| 54 | |
| 55 | $_filename = ltrim($filename, '/\\'); |
| 56 | $abspath = untrailingslashit(ABSPATH); |
| 57 | |
| 58 | // Child Theme |
| 59 | if(file_exists(STYLESHEETPATH . '/' . $_filename)){ |
| 60 | |
| 61 | $located = get_stylesheet_directory_uri() . '/' . $_filename; |
| 62 | break; |
| 63 | |
| 64 | } |
| 65 | |
| 66 | // Parent Theme |
| 67 | elseif(file_exists(TEMPLATEPATH . '/' . $_filename)){ |
| 68 | |
| 69 | $located = get_template_directory_uri() . '/' . $_filename; |
| 70 | break; |
| 71 | |
| 72 | } |
| 73 | |
| 74 | // Direct file path |
| 75 | elseif(file_exists($filename)){ |
| 76 | |
| 77 | $located = acfe_get_abs_path_to_url($filename); |
| 78 | break; |
| 79 | |
| 80 | } |
| 81 | |
| 82 | // ABSPATH file path |
| 83 | elseif(file_exists($abspath . '/' . $_filename)){ |
| 84 | |
| 85 | $located = acfe_get_abs_path_to_url($abspath . '/' . $_filename); |
| 86 | break; |
| 87 | |
| 88 | } |
| 89 | |
| 90 | // WP Content Dir |
| 91 | elseif(file_exists(WP_CONTENT_DIR . '/' . $_filename)){ |
| 92 | |
| 93 | $located = WP_CONTENT_URL . '/' . $_filename; |
| 94 | break; |
| 95 | |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | return $located; |
| 103 | |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * acfe_locate_file_path |
| 108 | * |
| 109 | * Similar to locate_template(), but locate File Path instead |
| 110 | * Based on wp-includes\template.php:653 |
| 111 | * |
| 112 | * @param $filenames |
| 113 | * |
| 114 | * @return mixed|string |
| 115 | */ |
| 116 | function acfe_locate_file_path($filenames){ |
| 117 | |
| 118 | $located = ''; |
| 119 | |
| 120 | foreach((array) $filenames as $filename){ |
| 121 | |
| 122 | if(!$filename){ |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | $_filename = ltrim($filename, '/\\'); |
| 127 | $abspath = untrailingslashit(ABSPATH); |
| 128 | |
| 129 | // Stylesheet file path |
| 130 | if(file_exists(STYLESHEETPATH . '/' . $_filename)){ |
| 131 | |
| 132 | $located = STYLESHEETPATH . '/' . $_filename; |
| 133 | break; |
| 134 | |
| 135 | } |
| 136 | |
| 137 | // Template file path |
| 138 | elseif(file_exists(TEMPLATEPATH . '/' . $_filename)){ |
| 139 | |
| 140 | $located = TEMPLATEPATH . '/' . $_filename; |
| 141 | break; |
| 142 | |
| 143 | } |
| 144 | |
| 145 | // Direct file path |
| 146 | elseif(file_exists($filename)){ |
| 147 | |
| 148 | $located = $filename; |
| 149 | break; |
| 150 | |
| 151 | } |
| 152 | |
| 153 | // ABSPATH file path |
| 154 | elseif(file_exists($abspath . '/' . $_filename)){ |
| 155 | |
| 156 | $located = $abspath . '/' . $_filename; |
| 157 | break; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | // WP Content Dir |
| 162 | elseif(file_exists(WP_CONTENT_DIR . '/' . $_filename)){ |
| 163 | |
| 164 | $located = WP_CONTENT_DIR . '/' . $_filename; |
| 165 | break; |
| 166 | |
| 167 | } |
| 168 | |
| 169 | } |
| 170 | |
| 171 | return $located; |
| 172 | |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * acfe_get_human_readable_location |
| 178 | * |
| 179 | * Returns "Located in theme: /acf-json/group_abcdef123456.json" |
| 180 | * Returns "Located in plugin: /my-plugin/group_abcdef123456.json" |
| 181 | * Returns "Located in: /group_abcdef123456.json" |
| 182 | * |
| 183 | * @param $path |
| 184 | * @param $prefix |
| 185 | * @param $new_line |
| 186 | * |
| 187 | * @return string |
| 188 | */ |
| 189 | function acfe_get_human_readable_location($path, $prefix = true, $new_line = true){ |
| 190 | |
| 191 | // vars |
| 192 | $located = ''; |
| 193 | $path = wp_normalize_path($path); |
| 194 | $file_exists = file_exists($path); |
| 195 | |
| 196 | // paths to check |
| 197 | $stylesheet_path = wp_normalize_path(get_stylesheet_directory()); |
| 198 | $template_path = wp_normalize_path(get_template_directory()); |
| 199 | $wp_plugin_dir = wp_normalize_path(WP_PLUGIN_DIR); |
| 200 | $abspath = wp_normalize_path(ABSPATH); |
| 201 | |
| 202 | // prefix labels |
| 203 | $prefix_label = array( |
| 204 | __('Located', 'acfe'), |
| 205 | __('Not found', 'acfe'), |
| 206 | ); |
| 207 | |
| 208 | if(is_array($prefix)){ |
| 209 | $prefix_label = $prefix; |
| 210 | } |
| 211 | |
| 212 | if(strpos($path, $stylesheet_path) !== false){ |
| 213 | |
| 214 | $rel_path = str_replace($stylesheet_path, '', $path); |
| 215 | |
| 216 | if($prefix){ |
| 217 | $located .= ($file_exists ? $prefix_label[0] : $prefix_label[1]) . ' '; |
| 218 | } |
| 219 | |
| 220 | $located .= __('in theme:', 'acfe') . ($new_line ? "<br/>" : ' '); |
| 221 | $located .= $rel_path; |
| 222 | |
| 223 | }elseif(strpos($path, $template_path) !== false){ |
| 224 | |
| 225 | $rel_path = str_replace($template_path, '', $path); |
| 226 | |
| 227 | if($prefix){ |
| 228 | $located .= ($file_exists ? $prefix_label[0] : $prefix_label[1]) . ' '; |
| 229 | } |
| 230 | |
| 231 | $located .= __('in theme:', 'acfe') . ($new_line ? "<br/>" : ' '); |
| 232 | $located .= $rel_path; |
| 233 | |
| 234 | }elseif(strpos($path, $wp_plugin_dir) !== false){ |
| 235 | |
| 236 | $rel_path = str_replace($wp_plugin_dir, '', $path); |
| 237 | |
| 238 | if($prefix){ |
| 239 | $located .= ($file_exists ? $prefix_label[0] : $prefix_label[1]) . ' '; |
| 240 | } |
| 241 | |
| 242 | $located .= __('in plugin:', 'acfe') . ($new_line ? "<br/>" : ' '); |
| 243 | $located .= $rel_path; |
| 244 | |
| 245 | }else{ |
| 246 | |
| 247 | $rel_file = str_replace($abspath, '', $path); |
| 248 | |
| 249 | if($prefix){ |
| 250 | $located .= ($file_exists ? $prefix_label[0] : $prefix_label[1]) . ' '; |
| 251 | } |
| 252 | |
| 253 | $located .= __('in:', 'acfe') . ($new_line ? "<br/>" : ' '); |
| 254 | $located .= $rel_file; |
| 255 | |
| 256 | } |
| 257 | |
| 258 | return $located; |
| 259 | |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /** |
| 264 | * acfe_get_file_extension |
| 265 | * |
| 266 | * @param $file |
| 267 | * |
| 268 | * @return array|string |
| 269 | */ |
| 270 | function acfe_get_file_extension($file){ |
| 271 | |
| 272 | // extract the path component |
| 273 | $path = parse_url($file, PHP_URL_PATH); |
| 274 | |
| 275 | // get the extension of the file |
| 276 | return pathinfo($path, PATHINFO_EXTENSION); |
| 277 | |
| 278 | } |