diff --git a/content/themes/default/functions.php b/content/themes/default/functions.php index 95fa6c8..11c01ce 100644 --- a/content/themes/default/functions.php +++ b/content/themes/default/functions.php @@ -208,25 +208,25 @@ public function after_theme_setup() { // Load Font Awesome. if ( ENVIRONMENT === 'development') { - add_style( 'opensans', 'assets/css/open-sans.min', null, true); - add_style( 'fontawesome', get_common_url( 'css/font-awesome.min' ), null, true); - add_style( 'bootstrap', 'assets/css/bootstrap.min', null, true); - add_script( 'bootstrap', 'assets/js/bootstrap.min'); + add_style( 'opensans', get_theme_url('assets/css/open-sans.min.css', ''), null, true); + add_style( 'fontawesome', get_common_url( 'css/font-awesome.min.css', ''), null, true); + add_style( 'bootstrap', get_theme_url('assets/css/bootstrap.min.css', ''), null, true); + add_script( 'bootstrap', get_theme_url('assets/js/bootstrap.min.js', '')); } else { - add_style( 'opensans', 'https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i', null, true); + add_style( 'opensans', 'https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i', null, true); add_style( 'fontawesome', 'https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css', null, true); - add_style( 'bootstrap', 'https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', null, true); - add_script( 'bootstrap', 'https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'); + add_style( 'bootstrap', 'https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', null, true); + add_script( 'bootstrap', 'https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'); } // Load main theme style. - add_style( 'style', 'style', null, true); + add_style( 'style', get_theme_url('style.min.css', ''), null, true); // Right to left language? if (langinfo('direction' ) === 'rtl' ) { - add_style( 'bootstrap-rtl', 'assets/css/bootstrap-rtl.min', null, true); - add_style( 'style-rtl', 'style-rtl', null, true); + add_style( 'bootstrap-rtl', get_theme_url('assets/css/bootstrap-rtl.min.css', ''), null, true); + add_style( 'style-rtl', get_theme_url('style-rtl.min.css', ''), null, true); } } diff --git a/skeleton/core/Admin_Controller.php b/skeleton/core/Admin_Controller.php index e3457af..bc74dfa 100644 --- a/skeleton/core/Admin_Controller.php +++ b/skeleton/core/Admin_Controller.php @@ -171,9 +171,13 @@ public function _remap($method, $params = array()) ? array_clean($this->styles) : array_unique(array_filter(array_map('trim', $this->styles))); - $this->theme - ->no_extension() - ->add('css', admin_url('load/styles?load='.implode(',', $this->styles), 'http'), null, null, true); + $this->theme->add( + 'css', + admin_url('load/styles?load='.implode(',', $this->styles), ''), + null, // No handle. + null, // No version. + true // At the top + ); } // Do we have any JS files to laod? @@ -183,17 +187,15 @@ public function _remap($method, $params = array()) ? array_clean($this->scripts) : array_unique(array_filter(array_map('trim', $this->scripts))); - $this->theme - ->no_extension() - ->add('js', admin_url('load/scripts?load='.implode(',', $this->scripts), 'http'), null, null, true); + $this->theme->add( + 'js', + admin_url('load/scripts?load='.implode(',', $this->scripts), ''), + null, // No handle. + null, // No version. + true // At the top + ); } - /** - * We make sure to make theme library put back extensions. - * @since 1.5.0 - */ - $this->theme->do_extension(); - /** * Admin menu is called only of method that load views. * @since 2.1.0 diff --git a/skeleton/libraries/Theme.php b/skeleton/libraries/Theme.php index 93774dd..16ee6d7 100644 --- a/skeleton/libraries/Theme.php +++ b/skeleton/libraries/Theme.php @@ -711,7 +711,7 @@ private function _get_theme_details($folder) } } // If the screenshot was provided, we make sure it points to the URL. - elseif (false === filter_var($headers['screenshot'], FILTER_VALIDATE_URL)) + elseif (path_is_url($headers['screenshot'])) { $headers['screenshot'] = $this->themes_url($folder.'/'.$headers['screenshot']); } @@ -881,12 +881,13 @@ public function themes_path($uri = '') /** * Returns URI to the folder containing themes. * @access protected - * @param none + * @param string $uri + * @param string $protocol * @return string */ - public function themes_url($uri = '') + public function themes_url($uri = '', $protocol = null) { - return base_url("{$this->_themes_dir}/{$uri}"); + return base_url($this->_themes_dir.'/'.$uri, $protocol); } // -------------------------------------------------------------------- @@ -921,18 +922,20 @@ public function get_theme() * Returns theme url. * @access protected * @param string $uri uri to append to url. + * @param string $protocol * @return string. */ - public function theme_url($uri = '') + public function theme_url($uri = '', $protocol = null) { // We make sure to cache base URL and all possible URIs. - static $base_url, $cached_uris; + static $base_url, $_protocol, $cached_uris; - if (empty($base_url)) + if (empty($base_url) OR $_protocol !== $protocol) { + $_protocol = $protocol; // Use site base URL or provided CDN server. $base_url = path_join( - $this->cdn_enabled() ? $this->_cdn_server : base_url(), + $this->cdn_enabled() ? $this->_cdn_server : base_url(null, $_protocol), $this->_themes_dir.'/'.$this->_theme ); } @@ -1007,18 +1010,21 @@ public function theme_path($uri = '') * Return a URL to the uploads folder. * @access protected * @param string $uri + * @param string $protocol * @return string */ - public function upload_url($uri = '') + public function upload_url($uri = '', $protocol = null) { // We make sure to cache base URL and all possible URIs. - static $base_url, $cached_uris; + static $base_url, $_protocol, $cached_uris; - if (empty($base_url)) + if (empty($base_url) OR $_protocol !== $protocol) { + $_protocol = $protocol; + // Use site base URL or provided CDN server. $base_url = path_join( - $this->cdn_enabled() ? $this->_cdn_server : base_url(), + $this->cdn_enabled() ? $this->_cdn_server : base_url(null, $_protocol), $this->_uploads_dir ); } @@ -1090,18 +1096,20 @@ public function upload_path($uri = '') * Return a URL to the common folder. * @access protected * @param string $uri + * @param string $protocol * @return string */ - public function common_url($uri = '') + public function common_url($uri = '', $protocol = null) { // We make sure to cache base URL and all possible URIs. - static $base_url, $cached_uris; + static $base_url, $_protocol, $cached_uris; - if (empty($base_url)) + if (empty($base_url) OR $_protocol !== $protocol) { + $_protocol = $protocol; // Use site base URL or provided CDN server. $base_url = path_join( - $this->cdn_enabled() ? $this->_cdn_server : base_url(), + $this->cdn_enabled() ? $this->_cdn_server : base_url(null, $_protocol), $this->_common_dir ); } @@ -1636,9 +1644,6 @@ public function add($type = 'css', $file = null, $handle = null, $ver = null, $p return $this; } - // We start by removing the extension. - $file = $this->_remove_extension($file, $type); - // If the $handle is not provided, we generate it. if (empty($handle)) { @@ -1655,21 +1660,6 @@ public function add($type = 'css', $file = null, $handle = null, $ver = null, $p // We make sure $handle is always lowercased. $handle = strtolower($handle); - /** - * If the file is a full URL (cdn or using get_theme_url(..)) - * we use as it is, otherwise, we force get_theme_url() - */ - if (false === filter_var($file, FILTER_VALIDATE_URL)) - { - $func = 'theme'; - if (false !== strpos($file, ':')) - { - list($func, $file) = explode(':', $file); - } - - $file = $this->{$func.'_url'}($file); - } - // If the version is provided, use it. if ( ! empty($ver)) { @@ -1711,7 +1701,6 @@ public function add($type = 'css', $file = null, $handle = null, $ver = null, $p } $this->{$files}[$handle] = $attributes; - $this->_remove_extension = true; return $this; } @@ -1801,9 +1790,6 @@ public function replace($type = 'css', $file = null, $handle = null, $ver = null return $this; } - // We start by removing the extension. - $file = $this->_remove_extension($file, $type); - // If the $handle is not provided, we generate it. if (empty($handle)) { @@ -1820,15 +1806,6 @@ public function replace($type = 'css', $file = null, $handle = null, $ver = null // We make sure $handle is always lowercased. $handle = strtolower($handle); - /** - * If the file is a full URL (cdn or using get_theme_url(..)) - * we use as it is, otherwise, we force get_theme_url() - */ - if (false === filter_var($file, FILTER_VALIDATE_URL)) - { - $file = $this->theme_url($file); - } - // If the version is provided, use it. if ( ! empty($ver)) { @@ -1861,7 +1838,6 @@ public function replace($type = 'css', $file = null, $handle = null, $ver = null $this->_scripts[$handle] = $attributes; } - $this->_remove_extension = true; return $this; } @@ -2379,10 +2355,10 @@ public function get_footer($name = null) if ( ! in_array('modernizr-js', $this->_removed_scripts)) { // Default is from CDN. - $modernizr_url = 'https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js'; + $modernizr_url = '//cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js'; if (false === $this->cdn_enabled(false) OR 'development' === ENVIRONMENT) { - $modernizr_url = $this->common_url('js/modernizr-2.8.3.min.js'); + $modernizr_url = $this->common_url('js/modernizr-2.8.3.min.js', ''); } $this->add('js', $modernizr_url, 'modernizr', null, true); } @@ -2390,10 +2366,10 @@ public function get_footer($name = null) // We now add jQuery. if ( ! in_array('jquery-js', $this->_removed_scripts)) { - $jquery_url = 'https://code.jquery.com/jquery-3.2.1.min.js'; + $jquery_url = '//code.jquery.com/jquery-3.2.1.min.js'; if (false === $this->cdn_enabled(false) OR 'development' === ENVIRONMENT) { - $jquery_url = $this->common_url('js/jquery-3.2.1.min.js'); + $jquery_url = $this->common_url('js/jquery-3.2.1.min.js', ''); } $this->add('js', $jquery_url, 'jquery', null, true); } @@ -3440,115 +3416,6 @@ private function _load($view, $data = array()) // Utilities // -------------------------------------------------------------------- - /** - * Whether to use _remove_extension or not. - * @var bool - */ - private $_remove_extension = true; - - /** - * Disable the use of _remove_extension method. - * @access public - * @return object - */ - public function no_extension() - { - $this->_remove_extension = false; - return $this; - } - - // ------------------------------------------------------------------------ - - /** - * do_extension - * - * Method for making sure this library puts back files extensions in case - * the "no_extension" was called. Make sure to call it right after you - * call the late one. - * - * @author Kader Bouyakoub - * @link https://github.com/bkader - * @since 1.5.0 - * - * @access public - * @param none - * @return void - */ - public function do_extension() - { - $this->_remove_extension = true; - return $this; - } - - // -------------------------------------------------------------------- - - /** - * Removes files extension - * @access protected - * @param mixed string or array - * @return mixed string or array - */ - public function _remove_extension($file, $ext = 'css') - { - // In case of multiple items - if (is_array($file)) - { - $temp_files = array(); - foreach ($file as $index => $single_file) - { - $temp_files[$index] = $this->_remove_extension($single_file, $ext); - } - return $temp_files; - } - - // Removing extension is disabled? Return the file as-is. - if ($this->_remove_extension === false) - { - return $file; - } - - /** - * Ignore files with full URLs. THe user must provided extension. - * @since 2.0.0 - */ - if (false !== filter_var($file, FILTER_VALIDATE_URL) - && false === stripos($file, base_url())) - { - return $file; - } - - /** - * If the file comes with extension, return in as it is. - * @since 2.0.0 - */ - if ($ext === substr($file, - strlen($ext))) - { - return $file; - } - - /** - * Added the minified version of the file if not already set. - * @since 2.0.0 - */ - if ('production' === ENVIRONMENT && '.min' !== substr($file, -4)) - { - $file .= '.min'; - } - - /** - * Let's first check if the file extension is - * present or not. If not, add it. - */ - if ($ext !== pathinfo($file, PATHINFO_EXTENSION)) - { - $file .= '.'.$ext; - } - - return $file; - } - - // -------------------------------------------------------------------- - /** * Make sure the .htaccess file that denies direct * access to folder is present. @@ -3868,11 +3735,12 @@ function get_body_class() /** * Returns the URL to the theme folder. * @param string $uri string to be appended. + * @param string $protocol * @return string. */ - function get_theme_url($uri = '') + function get_theme_url($uri = '', $protocol = null) { - return get_instance()->theme->theme_url($uri); + return get_instance()->theme->theme_url($uri, $protocol); } } @@ -3883,10 +3751,11 @@ function get_theme_url($uri = '') /** * Unlike the function above, this one echoes the URL. * @param string $uri string to be appended. + * @param string $protocol */ - function theme_url($uri = '') + function theme_url($uri = '', $protocol = null) { - echo get_instance()->theme->theme_url($uri); + echo get_instance()->theme->theme_url($uri, $protocol); } } @@ -3928,11 +3797,12 @@ function theme_path($uri = '') /** * Returns the URL to the uploads folder. * @param string $uri string to be appended. + * @param string $protocol * @return string. */ - function get_upload_url($uri = '') + function get_upload_url($uri = '', $protocol = null) { - return get_instance()->theme->upload_url($uri); + return get_instance()->theme->upload_url($uri, $protocol); } } @@ -3943,10 +3813,11 @@ function get_upload_url($uri = '') /** * Unlike the function above, this one echoes the URL. * @param string $uri string to be appended. + * @param string $protocol */ - function upload_url($uri = '') + function upload_url($uri = '', $protocol = null) { - echo get_instance()->theme->upload_url($uri); + echo get_instance()->theme->upload_url($uri, $protocol); } } @@ -3988,11 +3859,12 @@ function upload_path($uri = '') /** * Returns the URL to the commons folder. * @param string $uri string to be appended. + * @param string $protocol * @return string. */ - function get_common_url($uri = '') + function get_common_url($uri = '', $protocol = null) { - return get_instance()->theme->common_url($uri); + return get_instance()->theme->common_url($uri, $protocol); } } @@ -4003,10 +3875,11 @@ function get_common_url($uri = '') /** * Unlike the function above, this one echoes the URL. * @param string $uri string to be appended. + * @param string $protocol */ - function common_url($uri = '') + function common_url($uri = '', $protocol = null) { - echo get_instance()->theme->common_url($uri); + echo get_instance()->theme->common_url($uri, $protocol); } } @@ -4051,19 +3924,20 @@ function common_path($uri = '') * if you want to use a different approach. * @param string $file The file your want to generate URL to. * @param bool $common Load it from common or theme folder. + * @param string $protocol * @return string The full URL to the file. */ - function assets_url($file = null, $common = false) + function assets_url($file = null, $common = false, $protocol = null) { // If a full link is passed, return it as it is. - if (filter_var($file, FILTER_VALIDATE_URL) !== false) + if (path_is_url($file)) { return $file; } return ($common === true) - ? get_common_url($file) - : get_theme_url($file); + ? get_common_url($file, $protocol) + : get_theme_url($file, $protocol); } } @@ -4160,13 +4034,13 @@ function the_extra_head($content = null, $echo = true) */ function add_ie9_support(&$output, $remote = true) { - $html5shiv = 'https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js'; - $respond = 'https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js'; + $html5shiv = '//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js'; + $respond = '//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js'; if ($remote === false) { - $html5shiv = get_common_url('js/html5shiv-3.7.3.min.js'); - $respond = get_common_url('js/respond-1.4.2.min.js'); + $html5shiv = get_common_url('js/html5shiv-3.7.3.min.js', ''); + $respond = get_common_url('js/respond-1.4.2.min.js', ''); } $output .= << diff --git a/skeleton/third_party/bkader/base.php b/skeleton/third_party/bkader/base.php index 73ba106..f27402b 100644 --- a/skeleton/third_party/bkader/base.php +++ b/skeleton/third_party/bkader/base.php @@ -106,6 +106,27 @@ function path_is_stream($path) { // ------------------------------------------------------------------------ +if ( ! function_exists('path_is_url')) { + /** + * Tests if the given path is a URL with or without protocol. + * + * @since 2.1.1 + * + * @param string $path The resource path or URL. + * @return bool true if the path is a valid URL, else false. + */ + function path_is_url($path) { + static $regex; + if (is_null($regex)) { + $regex = '/(?:https?:\/\/)?(?:[a-zA-Z0-9.-]+?\.(?:[a-zA-Z])|\d+\.\d+\.\d+\.\d+)/'; + } + + return preg_match($regex, $path); + } +} + +// ------------------------------------------------------------------------ + if ( ! function_exists('path_mkdir')) { /** * Recursive directory creation based on full path.