assets_path = ROCKSITE_THEME_ASSETS; $this->hooks(); $this->run_adds(); } public function hooks() { $this->add_action( 'wp_enqueue_scripts', $this, 'enqueue_css_assets' ); $this->add_action( 'wp_enqueue_scripts', $this, 'enqueue_js_assets' ); $this->add_action( 'wp_enqueue_scripts', $this, 'inline_css_variables' ); $this->add_action( 'admin_enqueue_scripts', $this, 'enqueue_admin_css_assets' ); $this->add_action( 'admin_enqueue_scripts', $this, 'inline_css_variables' ); //$this->add_filter('body_class', $this, 'body_class_modyficator'); } /** * Regster all scripts from the config file */ private function register_scripts() { $scripts = $this->config->get_js_assets(); if ( is_array( $scripts ) ) { foreach ( $scripts as $key => $file ) { $handle = $key; if ( WP_DEBUG !== FALSE ) { $src = $this->assets_path . $file['path_full']; } else { $src = $this->assets_path . $file['path']; } $deps = isset( $file['deps'] ) ? $file['deps'] : array(); $in_footer = isset( $file['in_footer'] ) ? $file['in_footer'] : FALSE; wp_register_script( $handle, $src, $deps, $this->config->version(), $in_footer ); } } } /** * Rgester all styles from the config */ private function register_styles() { //Register fonts firstly $this->register_fonts(); $styles = $this->config->get_css_assets(); if ( is_array( $styles ) ) { foreach ( $styles as $key => $file ) { $handle = $key; if ( WP_DEBUG !== FALSE ) { $src = $this->assets_path . $file['path_full']; } else { $src = $this->assets_path . $file['path']; } $deps = isset( $file['deps'] ) ? $file['deps'] : array(); $media = isset( $file['media'] ) ? $file['media'] : FALSE; wp_register_style( $handle, $src, $deps, $this->config->version(), $media ); } } } /** * Add assets to editor */ public function enqueue_admin_css_assets() { $this->register_fonts(); $this->register_styles(); wp_enqueue_style( 'canvi-custom-fonts' ); wp_enqueue_style( 'canvi-variables' ); } public function enqueue_css_assets() { //1_ Register styles $this->register_styles(); //2_ Enqueue styles wp_enqueue_style( 'canvi-main' ); } public function enqueue_js_assets() { $this->register_scripts(); wp_enqueue_script( 'canvi-main' ); } public function register_fonts() { $fonts = $this->config->get_project_variables(); if ( is_array( $fonts ) ) { $custom_font_string = ''; foreach ( $fonts as $handle => $font ) { $custom_font = wp_kses_post( get_theme_mod( $handle ) ); if ( $custom_font !== FALSE && strlen( $custom_font ) != 0 ) { $font_array = json_decode( $custom_font, TRUE ); } else { $font_array = json_decode( $font, TRUE ); } if ( isset( $font_array['font'] ) && ( substr_count( $custom_font_string, $font_array['font'] ) == 0 ) ) { $custom_font_string .= str_replace( ' ', '+', $font_array['font'] ) . ':'; if ( isset( $font_array['regularweight'] ) ) { $custom_font_string .= $font_array['regularweight'] . ','; } if ( isset( $font_array['italicweight'] ) ) { $custom_font_string .= $font_array['italicweight'] . ','; } if ( isset( $font_array['boldweight'] ) ) { $custom_font_string .= $font_array['boldweight'] . ','; } $custom_font_string = substr( $custom_font_string, 0, strlen( $custom_font_string ) - 1 ); $custom_font_string .= '|'; } } wp_register_style( 'canvi-custom-fonts', html_entity_decode( esc_attr( $this->google_fonts_url . substr( $custom_font_string, 0, - 1 ) ) . '&display=swap&subset=latin-ext' ), array(), $this->config->version(), 'all' ); } } /** * Displays custom CSS variables based on customizer values */ public function inline_css_variables() { $start_css = ":root {" . "\n"; $ouput_css = ""; $project_variables = $this->config->get_project_variables(); foreach ( $project_variables as $css_variable => $css_value ) { $value = get_theme_mod( $css_variable ); if ( $value !== FALSE && ! is_array( $value ) && stripos( $value, '{"' ) === FALSE ) { $ouput_css .= '--' . $css_variable . ':' . $value . ';' . "\n"; } if ( stripos( $value, 'font' ) !== FALSE && stripos( $value, '{"' ) !== FALSE ) { $objVariables = json_decode( $value ); if ( isset( $objVariables->font ) ) { $ouput_css .= '--' . $css_variable . ':"' . $objVariables->font . '";' . "\n"; } } } $end_css = '}'; if ( strlen( $ouput_css ) > 0 ) { $header_css = $start_css . $ouput_css . $end_css; // pass style name whoch is upper than main style wp_add_inline_style( 'canvi-variables', $header_css ); } } } }