Changeset 6862 for trunk/src/includes/core/template-functions.php
- Timestamp:
- 09/05/2018 06:55:06 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/core/template-functions.php
r6795 r6862 105 105 106 106 return $located; 107 } 108 109 /** 110 * Locate an enqueueable file on the server. Used before being enqueued. 111 * 112 * If SCRIPT_DEBUG is set and the file includes a .min suffix, this function 113 * will automatically attempt to locate a non-minified version of that file. 114 * 115 * If SCRIPT_DEBUG is not set and the file exclude a .min suffix, this function 116 * will automatically attempt to locate a minified version of that file. 117 * 118 * See: https://bbpress.trac.wordpress.org/ticket/3218 119 * 120 * @since 2.6.0 121 * 122 * @param string $file 123 * 124 * @return boolean 125 */ 126 function bbp_locate_enqueueable( $file = '' ) { 127 128 // Bail if no file to locate 129 if ( empty( $file ) ) { 130 return false; 131 } 132 133 // Add file to files array 134 $files = array( $file ); 135 136 // Get the file variant (minified or not, but opposite of $file) 137 $file_is_min = ( false !== strpos( $file, '.min' ) ); 138 $file_variant = ( false === $file_is_min ) 139 ? str_replace( array( '.css', '.js' ), array( '.min.css', '.min.js' ), $file ) 140 : str_replace( '.min', '', $file ); 141 142 // Are we debugging? 143 $script_debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; 144 145 // Debugging, so prefer unminified files 146 if ( true === $script_debug ) { 147 if ( true === $file_is_min ) { 148 array_unshift( $files, $file_variant ); 149 } else { 150 array_push( $files, $file_variant ); 151 } 152 153 // Not debugging, so prefer minified files 154 } elseif ( false === $script_debug ) { 155 if ( true === $file_is_min ) { 156 array_push( $files, $file_variant ); 157 } else { 158 array_unshift( $files, $file_variant ); 159 } 160 } 161 162 // Return first found file location in the stack 163 return bbp_locate_template( $files, false, false ); 164 } 165 166 /** 167 * Convert an enqueueable file path to a URL 168 * 169 * @since 2.6.0 170 * @param string $file 171 * 172 * @return string 173 */ 174 function bbp_urlize_enqueueable( $file = '' ) { 175 176 // Get DIR and URL 177 $content_dir = constant( 'WP_CONTENT_DIR' ); 178 $content_url = content_url(); 179 180 // IIS (Windows) here 181 // Replace back slashes with forward slash 182 if ( false !== strpos( $file, '\\' ) ) { 183 $file = str_replace( '\\', '/', $file ); 184 $content_dir = str_replace( '\\', '/', $content_dir ); 185 } 186 187 // Return path to file relative to site URL 188 return str_replace( $content_dir, $content_url, $file ); 107 189 } 108 190 … … 124 206 * 'screen', 'tty', or 'tv'. 125 207 * 126 * @return string The style filename if one is located.208 * @return mixed The style filename if one is located. False if not. 127 209 */ 128 210 function bbp_enqueue_style( $handle = '', $file = '', $deps = array(), $ver = false, $media = 'all' ) { 129 211 130 // No file found yet 131 $located = false; 132 133 // Trim off any slashes from the template name 134 $file = ltrim( $file, '/' ); 135 136 // Make sure there is always a version 137 if ( empty( $ver ) ) { 138 $ver = bbp_get_version(); 139 } 140 141 // Loop through template stack 142 foreach ( (array) bbp_get_template_stack() as $template_location ) { 143 144 // Continue if $template_location is empty 145 if ( empty( $template_location ) ) { 146 continue; 147 } 148 149 // Check child theme first 150 if ( file_exists( trailingslashit( $template_location ) . $file ) ) { 151 $located = trailingslashit( $template_location ) . $file; 152 break; 153 } 154 } 212 // Attempt to locate an enqueueable 213 $located = bbp_locate_enqueueable( $file ); 155 214 156 215 // Enqueue if located 157 216 if ( ! empty( $located ) ) { 158 217 159 $content_dir = constant( 'WP_CONTENT_DIR' ); 160 161 // IIS (Windows) here 162 // Replace back slashes with forward slash 163 if ( strpos( $located, '\\' ) !== false ) { 164 $located = str_replace( '\\', '/', $located ); 165 $content_dir = str_replace( '\\', '/', $content_dir ); 166 } 167 168 // Make path to file relative to site URL 169 $located = str_replace( $content_dir, content_url(), $located ); 218 // Make sure there is always a version 219 if ( empty( $ver ) ) { 220 $ver = bbp_get_version(); 221 } 222 223 // Make path to file relative to site URL 224 $located = bbp_urlize_enqueueable( $located ); 170 225 171 226 // Register the style … … 195 250 * Default 'false'. Accepts 'false' or 'true'. 196 251 * 197 * @return string The script filename if one is located.252 * @return mixed The script filename if one is located. False if not. 198 253 */ 199 254 function bbp_enqueue_script( $handle = '', $file = '', $deps = array(), $ver = false, $in_footer = false ) { 200 255 201 // No file found yet 202 $located = false; 203 204 // Trim off any slashes from the template name 205 $file = ltrim( $file, '/' ); 206 207 // Make sure there is always a version 208 if ( empty( $ver ) ) { 209 $ver = bbp_get_version(); 210 } 211 212 // Loop through template stack 213 foreach ( (array) bbp_get_template_stack() as $template_location ) { 214 215 // Continue if $template_location is empty 216 if ( empty( $template_location ) ) { 217 continue; 218 } 219 220 // Check child theme first 221 if ( file_exists( trailingslashit( $template_location ) . $file ) ) { 222 $located = trailingslashit( $template_location ) . $file; 223 break; 224 } 225 } 256 // Attempt to locate an enqueueable 257 $located = bbp_locate_enqueueable( $file ); 226 258 227 259 // Enqueue if located 228 260 if ( ! empty( $located ) ) { 229 261 230 $content_dir = constant( 'WP_CONTENT_DIR' ); 231 232 // IIS (Windows) here 233 // Replace back slashes with forward slash 234 if ( strpos( $located, '\\' ) !== false ) { 235 $located = str_replace( '\\', '/', $located ); 236 $content_dir = str_replace( '\\', '/', $content_dir ); 237 } 238 239 // Make path to file relative to site URL 240 $located = str_replace( $content_dir, content_url(), $located ); 262 // Make sure there is always a version 263 if ( empty( $ver ) ) { 264 $ver = bbp_get_version(); 265 } 266 267 // Make path to file relative to site URL 268 $located = bbp_urlize_enqueueable( $located ); 241 269 242 270 // Register the style
Note: See TracChangeset
for help on using the changeset viewer.