cache_active = true; $this->init_cache_folder (); $clear_cache = isset ( $_REQUEST ['clear_theme_cache'] ) ? $_REQUEST ['clear_theme_cache'] : ''; if ($clear_cache == 'true') { $this->flush_cache (); } add_action ( 'admin_bar_menu', array ($this, "attach_admin_barmenu" ), 89 ); } add_action ( 'wp_enqueue_scripts', array ($this, 'build_user_styles' ) ); add_action ( 'wp_enqueue_scripts', array ($this, 'output_user_styles' ), 99 ); add_action ( 'wp_enqueue_scripts', array ($this, 'output_user_footer_scripts' ), 99 ); } public function attach_admin_barmenu() { global $post; $url = ''; if (isset ( $post )) { $url = get_permalink ( $post->ID ); } else { $url = get_bloginfo ( 'url' ); } $this->add_root_menu ( esc_html__ ( 'Clear Theme Cache', 'brunch-lite' ), 'brunch-lite', esc_url($url) . '?clear_theme_cache=true' ); } function add_root_menu($name, $id, $href = FALSE) { global $wp_admin_bar; if (! is_super_admin () || ! is_admin_bar_showing ()) return; $wp_admin_bar->add_menu ( array ('id' => $id, 'meta' => array (), 'title' => $name, 'href' => $href ) ); } /** * Initialize cache folders path and URL - this is folder where customized * styles * will be saved into single file for faster resource load */ public function init_cache_folder() { $upload_dir = wp_upload_dir (); $base_path = $upload_dir ['basedir'] . '/joy/'; $base_url = $upload_dir ['baseurl'] . '/joy/'; if (is_ssl ()) { $base_url = str_replace ( 'http://', 'https://', $base_url ); } if (! is_dir ( $base_path )) { if (! mkdir ( $base_path, 0777 )) { return false; } } $this->cache_folder = $base_path; $this->cache_url = $base_url; } public function save_in_cache_customized_styles($code = '') { global $wp_filesystem; if ($code == '') { return false; } // Initialize the WP filesystem, no more using 'file-put-contents' function if (empty($wp_filesystem)) { require_once (ABSPATH . '/wp-admin/includes/file.php'); WP_Filesystem(); } $filename = $this->cache_folder . 'joy-user.css'; $contentdir = trailingslashit( $this->cache_folder ); $wp_filesystem->put_contents( $filename, $code, FS_CHMOD_FILE); } public function exist_cache_customized_styles() { $filename = $this->cache_folder . 'joy-user.css'; if (file_exists ( $filename )) { return true; } else { return false; } } public function flush_cache() { if (! $this->cache_active) { return; } $filename = $this->cache_folder . 'joy-user.css'; if (file_exists ( $filename )) { unlink ( $filename ); } } public function compress_css_inline($inline_css) { // Remove comments $inline_css = preg_replace ( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $inline_css ); // Remove space after colons $inline_css = str_replace ( ': ', ':', $inline_css ); // Remove whitespace $inline_css = str_replace ( array ("\r\n", "\r", "\n", "\t", ' ', ' ', ' ' ), '', $inline_css ); // Write everything out return $inline_css; } public function output_user_footer_scripts() { $output_js = joy_option ( 'custom_js_footer', '' ); if ($output_js != '') { wp_add_inline_script('joy-components', $output_js); } } public function output_user_styles() { if (! $this->cache_active) { $output = $this->compile (); $output .= joy_option ( 'custom_css', '' ); if ($output != '') { wp_add_inline_style('brunch-lite', $this->compress_css_inline ( $output )); } } else { if (! $this->exist_cache_customized_styles ()) { $output = $this->compile (); $output .= joy_option ( 'custom_css', '' ); $this->save_in_cache_customized_styles ( $this->compress_css_inline ( $output ) ); } } $output_js = joy_option ( 'custom_js_header', '' ); if ($output_js != '') { wp_add_inline_script('joy-components', $output_js); } } public function build_user_styles() { if (!$this->cache_active || ($this->cache_active && !$this->exist_cache_customized_styles())) { $output = ''; $this->build_global_styles(); } if ($this->cache_active) { wp_enqueue_style ( 'joy-user-styles', $this->cache_url . 'joy-user.css', array (), BRUNCH_VERSION ); } } public function build_global_styles() { $bg_color = joy_option('background_color', '#ffffff'); if (strtoupper($bg_color) != '#FFFFFF') { $this->register_option('body', 'background-color', $bg_color); } $text_color = joy_option('text_color', '#000000'); if ($text_color != '#000000') { $this->register_option('body', 'color', $text_color); } $link_color = joy_option('link_color', '#000000'); if ($link_color != '#000000') { $this->register_option('body a', 'color', $link_color); $this->register_option('.classic-preview .read-more-button a:hover', 'background', $link_color); $this->register_option('.classic-preview .read-more-button a:hover', 'color', '#fff'); } $hover_color = joy_option('hover_color', '#000000'); if ($hover_color != '#000000') { $this->register_option('body a:hover', 'color', $hover_color); } if (joy_option('remove_underline_effect', false)) { $this->register_option('.entry-content a', 'border-bottom', '0'); $this->register_option('.meta a:hover', 'border-bottom-color, .site-footer p:first-child a:hover, .site-footer p + p a:hover', 'transparent'); } $logo_height = joy_option('logo_height', ''); if ($logo_height != '') { $this->register_option('.site-header .custom-logo img', 'max-height', $logo_height.'px'); } $logo_width = joy_option('logo_width', ''); if ($logo_width != '') { $this->register_option('.site-header .custom-logo img', 'min-width', $logo_width.'px'); } /** Menu **/ $menu_link_color = joy_option('menu_link_color', '#000000'); if ($menu_link_color != '#000000') { $this->register_option('.alt-nav li a, .site-nav ul a', 'color', $menu_link_color); $this->register_option('.nav-toggle .bar', 'background-color', $menu_link_color); } $menu_hover_color = joy_option('menu_hover_color', '#000000'); if ($menu_hover_color != '#000000') { $this->register_option('.alt-nav li a:hover, .site-nav ul a:hover, .site-nav ul:last-child .current-menu-item > a', 'color', $menu_hover_color); $this->register_option('.nav-toggle:hover .bar', 'background-color', $menu_hover_color); } $second_text_color = joy_option('second_text_color', '#999999'); if ($second_text_color != '#999999') { $this->register_option('.meta, .comment-metadata, .comments-pagination .page-numbers.dots, .comment-respond .logged-in-as, .post-pagination a, .site-footer p + p, .site-footer p + p a:hover, .site-header .site-title a:hover, .site-nav ul + ul a, .site-nav footer p:last-child', 'color', $second_text_color); } $blog_gutter = joy_option('blog_gutter', '10'); if ($blog_gutter != '10') { $this->register_option('.post-preview', 'padding', $blog_gutter.'px'); $this->register_option('.classic-preview', 'padding', $blog_gutter.'px'); } $submenu_color = joy_option('submenu_color', '#000000'); if ($submenu_color != '#000000') { $this->register_option('.alt-nav ul', 'background', $submenu_color); $this->register_option('.alt-nav ul:after', 'border-bottom-color', $submenu_color); $this->register_option('.alt-nav ul ul:after', 'border-right-color', $submenu_color); $this->register_option('.alt-nav ul ul:after', 'border-bottom-color', 'transparent'); } $submenu_color_link = joy_option('submenu_color_link', '#ffffff'); if ($submenu_color_link != '#ffffff') { $this->register_option('.alt-nav ul a', 'color', $submenu_color_link); } $menu_fontsize = joy_option('menu_fontsize', '13'); if ($menu_fontsize != '13') { $this->register_option('.alt-nav li', 'font-size', $menu_fontsize.'px'); } $menu_transfrom = joy_option('menu_transfrom', 'uppercase'); if ($menu_transfrom != 'uppercase') { $this->register_option('.alt-nav li', 'text-transform', $menu_transfrom); } $menu_fontweight = joy_option('menu_fontweight', 'normal'); if ($menu_fontweight != 'normal') { $this->register_option('.alt-nav li', 'font-weight', $menu_fontweight); } $content_fontsize = joy_option('content_fontsize', '20'); if ($content_fontsize != '20') { $this->register_option('body', 'font-size', $content_fontsize.'px'); } $font_heading1_size = joy_option('font_heading1_size', '40'); if ($font_heading1_size != '40' && $font_heading1_size != '') { $this->register_option('.entry-content h1', 'font-size', $font_heading1_size.'px'); } $font_heading2_size = joy_option('font_heading2_size', '32'); if ($font_heading2_size != '32' && $font_heading2_size != '') { $this->register_option('.entry-content h2', 'font-size', $font_heading2_size.'px'); } $font_heading3_size = joy_option('font_heading3_size', '28'); if ($font_heading3_size != '28' && $font_heading3_size != '') { $this->register_option('.entry-content h3', 'font-size', $font_heading3_size.'px'); } $font_heading4_size = joy_option('font_heading4_size', '24'); if ($font_heading4_size != '24' && $font_heading4_size != '') { $this->register_option('.entry-content h4', 'font-size', $font_heading4_size.'px'); } $font_heading5_size = joy_option('font_heading5_size', '20'); if ($font_heading5_size != '20' && $font_heading5_size != '') { $this->register_option('.entry-content h5', 'font-size', $font_heading5_size.'px'); } $font_heading6_size = joy_option('font_heading6_size', '14'); if ($font_heading6_size != '14' && $font_heading6_size != '') { $this->register_option('.entry-content h6', 'font-size', $font_heading6_size.'px'); } // Google Fonts Loading if needed $default_font_content_family = joy_option('default_font_content_family', ''); $default_font_heading_family = joy_option('default_font_heading_family', ''); $default_font_navigation_family = joy_option('default_font_navigation_family', ''); if ($default_font_content_family != '' || $default_font_heading_family != '' || $default_font_navigation_family != '') { require get_template_directory () . '/includes/class-joy-googlefonts.php'; JoyGoogleFonts::get_instance (); if ($default_font_content_family != '') { if (strpos($default_font_content_family, ' ' !== false)) { $default_font_content_family = "'".$default_font_content_family."'"; } $this->register_option('body', 'font-family', $default_font_content_family); } if ($default_font_heading_family != '') { if (strpos($default_font_heading_family, ' ' !== false)) { $default_font_heading_family = "'".$default_font_heading_family."'"; } $this->register_option('h1,h2,h3,h4,h5,h6', 'font-family', $default_font_heading_family); } if ($default_font_navigation_family != '') { if (strpos($default_font_navigation_family, ' ' !== false)) { $default_font_navigation_family = "'".$default_font_navigation_family."'"; } $this->register_option('.alt-nav,.site-nav .menus', 'font-family', $default_font_navigation_family); } } } /** * Build customized CSS to ready for print in header * * @return string */ public function compile() { $output = ''; foreach ( $this->css_builder as $selector => $properties ) { $output .= $selector . '{'; foreach ( $properties as $key => $value ) { if ($value != '') $output .= $key . ':' . $value . ';'; } $output .= '}'; } $output .= $this->free_css; return $output; } /** * Register multiple selectors at same time with same styles * * @param $selectors unknown_type * @param $type unknown_type * @param $value unknown_type * @param $important unknown_type */ public function register_options($selectors, $type, $value, $important = true) { if (! is_array ( $selectors )) return; foreach ( $selectors as $selector ) { $this->register_option ( $selector, $type, $value, $important ); } } /** * Register generated CSS with property and value * * @param $selector string * @param $type string * @param $value string * @param $important boolean * (optional default is false) */ public function register_option($selector, $type, $value, $important = true) { if (! isset ( $this->css_builder [$selector] )) { $this->css_builder [$selector] = array (); } $this->css_builder [$selector] [$type] = $value . ($important ? '!important' : ''); } public function parse_color($param, $selectors, $property_type = '', $important = true, $merge_selectors = false) { $value = joy_option ( $param, '' ); if ($this->is_default_value ( $value )) return; if ($merge_selectors && is_array ( $selectors )) { $selectors = implode ( ',', $selectors ); } if (is_array ( $selectors )) { $this->register_options ( $selectors, $property_type, $value, $important ); } else { $this->register_option ( $selectors, $property_type, $value, $important ); } } public function get_link_color($param, $state = 'link') { $value = joy_option ( $param, array () ); $result = ''; if (! is_array ( $value )) return $result; return isset ( $value [$state] ) ? $value [$state] : ''; } public function parse_link_color($param, $selectors, $hover_selectors, $active_selectors, $important = true) { $value = joy_option ( $param, array () ); if (! is_array ( $value )) return; $link = isset ( $value ['link'] ) ? $value ['link'] : ''; $hover = isset ( $value ['hover'] ) ? $value ['hover'] : ''; $active = isset ( $value ['active'] ) ? $value ['active'] : ''; if (! $this->is_default_value ( $link )) { if (is_array ( $selectors )) { $this->register_options ( $selectors, 'color', $link, $important ); } else { $this->register_option ( $selectors, 'color', $link, $important ); } } if (! $this->is_default_value ( $hover )) { if (is_array ( $hover_selectors )) { $this->register_options ( $hover_selectors, 'color', $hover, $important ); } else { $this->register_option ( $hover_selectors, 'color', $hover, $important ); } } if (! $this->is_default_value ( $active )) { if (is_array ( $active_selectors )) { $this->register_options ( $active_selectors, 'color', $active, $important ); } else { $this->register_option ( $active_selectors, 'color', $active, $important ); } } } public function parse_option($param, $selectors, $type, $important = true) { $value = joy_option ( $param, '' ); if ($this->is_default_value ( $value )) return; if (is_array ( $selectors )) $this->register_options ( $selectors, $type, $value, $important ); else $this->register_option ( $selectors, $type, $value, $important ); } public function parse_padding($param, $selectors, $important = true) { $value = joy_option ( $param, '' ); if (! is_array ( $value )) { return; } foreach ( $value as $key => $pair ) { if ($this->is_default_value ( $pair )) continue; $register_css = 'padding-' . $key; if (is_array ( $selectors )) { $this->register_options ( $selectors, $register_css, $pair, $important ); } else { $this->register_option ( $selectors, $register_css, $pair, $important ); } } } public function parse_fontfamily($param, $selectors, $important = true) { $value = joy_option ( $param, '' ); if ($value != '') { if (is_array ( $selectors )) { $this->register_options ( $selectors, 'font-family', $value, $important ); } else { $this->register_option ( $selectors, 'font-family', $value, $important ); } $this->has_google_fonts = true; } } public function parse_typography($param, $selectors, $important = true) { $value = joy_option ( $param, '' ); if (! is_array ( $value )) { return; } if ($param == 'default_font_heading' || $param == 'default_font_content') { $is_set_font = isset ( $value ['font-family'] ) ? $value ['font-family'] : ''; if ($is_set_font == '') { return; } } foreach ( $value as $key => $pair ) { $register_css = $key; if ($register_css == 'variant') $register_css = 'font-weight'; // for default font replacement we do not need font weight - // this will avoid CSS issues if ($key == 'variant') { if ($param == 'default_font_heading' || $param == 'default_font_content') { continue; } } if ($key == 'subsets') continue; if ($this->is_default_value ( $pair )) continue; if (is_array ( $selectors )) { $this->register_options ( $selectors, $register_css, $pair, $important ); } else { $this->register_option ( $selectors, $register_css, $pair, $important ); } if ($key == 'font-family' && $pair != '') $this->has_google_fonts = true; } } public function parse_background($param, $selector, $important = true) { $image = joy_option ( $param . '_image', '' ); if ($this->is_default_value ( $image )) { return; } $no_mobile = joy_option ( $param . '_no_mobile', false ); $repeat = joy_option ( $param . '_repeat', '' ); $size = joy_option ( $param . '_size', '' ); $size_custom = joy_option ( $param . '_size_custom', '' ); $attachment = joy_option ( $param . '_attachment', '' ); $position = joy_option ( $param . '_position', '' ); if (! $no_mobile) { $this->register_option ( $selector, 'background-image', 'url(' . $image . ')' ); if ($repeat != '') { $this->register_option ( $selector, 'background-repeat', $repeat ); } if ($size != '' && $size != 'custom') { $this->register_option ( $selector, 'background-size', $size ); } if ($size != '' && $size == 'custom' && $size_custom != '') { $this->register_option ( $selector, 'background-size', $size_custom ); } if ($attachment != '') { $this->register_option ( $selector, 'background-attachment', $attachment ); } if ($position != '') { $this->register_option ( $selector, 'background-position', $position ); } } else { $this->free_css .= '@media screen and (min-width: 960px) {'; $this->free_css .= $selector . '{ background-image: url(' . $image . '); }'; if ($repeat != '') { $this->free_css .= $selector . '{ background-repeat: ' . $repeat . '; }'; } if ($size != '' && $size != 'custom') { $this->free_css .= $selector . '{ background-size: ' . $size . '; }'; } if ($size != '' && $size == 'custom' && $size_custom != '') { $this->free_css .= $selector . '{ background-size: ' . $size_custom . '; }'; } if ($attachment != '') { $this->free_css .= $selector . '{ background-attachment: ' . $attachment . '; }'; } if ($position != '') { $this->free_css .= $selector . '{ background-position: ' . $position . '; }'; } $this->free_css .= '}'; } } /** * Check and return is the value applied in customizer eqaul to the default * * @param $value string * @return boolean */ public function is_default_value($value = '') { if ($value == '') { return true; } else if (strtoupper ( $value ) == 'DEFAULT') { return true; } else if (strtoupper ( $value ) == 'INHERIT') { return true; } else { return false; } } } $joy_style_builder = new JoyStyleBuilder();