Source of file class.gb-styles.php
Size: 8,737 Bytes - Last Modified: 2017-07-17T16:46:02+00:00
/home/travis/build/Limych/GeniBase/src/gb/class.gb-styles.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 | <?php /** * GeniBase Styles enqueue. * * These classes were refactored from the GeniBase GB_Scripts and GeniBase * script enqueue API. * * @package GeniBase * @since 2.0.0 * * @copyright Copyright © WordPress Team * @copyright Partially copyright © 2015, Andrey Khrolenok (andrey@khrolenok.ru) */ // Direct execution forbidden for this script if (! defined('GB_VERSION') || count(get_included_files()) == 1) die('<b>ERROR:</b> Direct execution forbidden!'); /** * GeniBase Styles enqueue class. * * @package GeniBase * @uses GB_Dependencies * @since 2.0.0 */ class GB_Styles extends GB_Dependencies { public $base_url; public $content_url; public $default_version; public $text_direction = 'ltr'; public $do_concat = false; public $concat_tag = ''; public $concat_code = ''; public $print_html = ''; public $print_code = ''; public $default_dirs; public function __construct() { /** * Fires when the GB_Styles instance is initialized. * * @since 2.1.1 * * @param * GB_Styles &$this GB_Styles instance, passed by reference. */ GB_Hooks::do_action_ref_array('gb_default_styles', array( &$this )); } static function get_file($path) { $path = realpath($path); if (! $path || ! @is_file($path)) return ''; return @file_get_contents($path); } /** * * @param string $handle * @return bool */ public function do_item($handle) { if (! parent::do_item($handle)) return false; $obj = $this->registered[$handle]; if (null === $obj->ver) $ver = ''; else $ver = $obj->ver ? $obj->ver : $this->default_version; if (isset($this->args[$handle])) $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle]; if ($this->do_concat) { if ($this->in_default_dir($obj->src) && ! isset($obj->extra['conditional']) && ! isset($obj->extra['alt'])) { $this->concat_tag .= "$handle,"; $this->concat_code .= self::get_file(BASE_DIR . $obj->src); $this->print_code .= $this->print_inline_style($handle, false); return true; } } if (isset($obj->args)) $media = esc_attr($obj->args); else $media = 'all'; $href = $this->_css_href($obj->src, $ver, $handle); if (empty($href)) { // Turns out there is nothing to print. return true; } $rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet'; $title = isset($obj->extra['title']) ? " title='" . esc_attr($obj->extra['title']) . "'" : ''; $sri_checking = ''; if (isset($obj->extra['integrity']) && !empty($obj->extra['integrity'])) { $sri_checking = " integrity='" . $obj->extra['integrity'] . "' crossorigin='" . (isset($obj->extra['crossorigin']) ? $obj->extra['crossorigin'] : 'anonymous') . "'"; } /** * Filter the HTML link tag of an enqueued style. * * @since 2.1.1 * * @param * string The link tag for the enqueued style. * @param string $handle * The style's registered handle. */ $tag = GB_Hooks::apply_filters('style_loader_tag', "<link rel='$rel' id='$handle-css'$title href='$href' type='text/css' media='$media'$sri_checking />\n", $handle); if ('rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl']) { if (is_bool($obj->extra['rtl']) || 'replace' === $obj->extra['rtl']) { $suffix = isset($obj->extra['suffix']) ? $obj->extra['suffix'] : ''; $rtl_href = str_replace("{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href($obj->src, $ver, "$handle-rtl")); } else { $rtl_href = $this->_css_href($obj->extra['rtl'], $ver, "$handle-rtl"); } /** * This filter is documented in gb/class.gb-styles.php */ $rtl_tag = GB_Hooks::apply_filters('style_loader_tag', "<link rel='$rel' id='$handle-rtl-css'$title href='$rtl_href' type='text/css' media='$media'$sri_checking />\n", $handle); if ($obj->extra['rtl'] === 'replace') { $tag = $rtl_tag; } else { $tag .= $rtl_tag; } } $conditional_pre = $conditional_post = ''; if (isset($obj->extra['conditional']) && $obj->extra['conditional']) { $conditional_pre = "<!--[if {$obj->extra['conditional']}]>\n"; $conditional_post = "<![endif]-->\n"; } if ($this->do_concat) { $this->print_html .= $conditional_pre; $this->print_html .= $tag; if ($inline_style = $this->print_inline_style($handle, false)) $this->print_html .= sprintf("<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr($handle), $inline_style); $this->print_html .= $conditional_post; } else { echo $conditional_pre; echo $tag; $this->print_inline_style($handle); echo $conditional_post; } return true; } /** * * @param string $handle * @param string $code */ public function add_inline_style($handle, $code) { if (! $code) { return false; } $after = $this->get_data($handle, 'after'); if (! $after) { $after = array(); } $after[] = $code; return $this->add_data($handle, 'after', $after); } /** * * @param string $handle * @param bool $echo * @return bool */ public function print_inline_style($handle, $echo = true) { $output = $this->get_data($handle, 'after'); if (empty($output)) { return false; } $output = implode("\n", $output); if (! $echo) { return $output; } printf("<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr($handle), $output); return true; } /** * * @param mixed $handles * @param bool $recursion * @param mixed $group * @return bool */ public function all_deps($handles, $recursion = false, $group = false) { $r = parent::all_deps($handles, $recursion); if (! $recursion) { /** * Filter the array of enqueued styles before processing for output. * * @since 2.1.0 * * @param array $to_do * The list of enqueued styles about to be processed. */ $this->to_do = GB_Hooks::apply_filters('print_styles_array', $this->to_do); } return $r; } /** * * @param string $src * @param string $ver * @param string $handle * @return string */ public function _css_href($src, $ver, $handle) { if (! is_bool($src) && ! preg_match('|^(https?:)?//|', $src) && ! ($this->content_url && 0 === strpos($src, $this->content_url))) { $src = $this->base_url . $src; } if (! empty($ver)) $src = add_query_arg('ver', $ver, $src); /** * Filter an enqueued style's fully-qualified URL. * * @since 2.1.0 * * @param string $src * The source URL of the enqueued style. * @param string $handle * The style's registered handle. */ $src = GB_Hooks::apply_filters('style_loader_src', $src, $handle); return esc_url($src); } /** * * @param string $src * @return bool */ public function in_default_dir($src) { if (! $this->default_dirs) return true; foreach ((array) $this->default_dirs as $test) { if (0 === strpos($src, $test)) return true; } return false; } public function do_footer_items() { // HTML 5 allows styles in the body, grab late enqueued items and output them in the footer. $this->do_items(false, 1); return $this->done; } public function reset() { $this->do_concat = false; $this->concat = ''; $this->concat_version = ''; $this->print_html = ''; } } |