ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

functions.php (34433B)


      1 <?php
      2 materialis_require("inc/variables.php");
      3 materialis_require("inc/defaults-dark.php");
      4 materialis_require("inc/defaults.php");
      5 
      6 function materialis_mod_default($name, $fallback = null)
      7 {
      8     if (materialis_has_in_memory('materialis_mod_default')) {
      9         $defaults = materialis_get_from_memory('materialis_mod_defaults');
     10     } else {
     11         $defaults = materialis_theme_defaults();
     12 
     13         $default = $fallback;
     14 
     15         if (isset($defaults[$name])) {
     16             $default = $defaults[$name];
     17         }
     18     }
     19 
     20     return $default;
     21 }
     22 
     23 
     24 function materialis_get_theme_mod($key, $fallback = null)
     25 {
     26     return get_theme_mod($key, materialis_mod_default($key, $fallback));
     27 }
     28 
     29 function materialis_set_in_memory($key, $value = false)
     30 {
     31     
     32     if ( ! isset($GLOBALS['MATERIALIS_MEMORY_CACHE'])) {
     33         $GLOBALS['MATERIALIS_MEMORY_CACHE'] = array();
     34     }
     35     
     36     $GLOBALS['MATERIALIS_MEMORY_CACHE'][$key] = $value;
     37 }
     38 
     39 function materialis_has_in_memory($key)
     40 {
     41     
     42     if (isset($GLOBALS['MATERIALIS_MEMORY_CACHE']) && isset($GLOBALS['MATERIALIS_MEMORY_CACHE'][$key])) {
     43         return $key;
     44     } else {
     45         return false;
     46     }
     47 }
     48 
     49 function materialis_get_from_memory($key)
     50 {
     51     if (materialis_has_in_memory($key)) {
     52         return $GLOBALS['MATERIALIS_MEMORY_CACHE'][$key];
     53     }
     54     
     55     return false;
     56 }
     57 
     58 function materialis_skip_customize_register()
     59 {
     60     return isset($_REQUEST['materialis_skip_customize_register']);
     61 }
     62 
     63 function materialis_get_cache_option_key()
     64 {
     65     return "__materialis_cached_values__";
     66 }
     67 
     68 function materialis_can_show_cached_value($slug)
     69 {
     70     global $wp_customize;
     71     
     72     if ($wp_customize || materialis_is_customize_preview() || wp_doing_ajax() || WP_DEBUG ) {
     73         return false;
     74     }
     75     
     76     if ($value = materialis_get_from_memory("materialis_can_show_cached_value_{$slug}")) {
     77         return $value;
     78     }
     79     
     80     $result = (materialis_get_cached_value($slug) !== null);
     81     
     82     materialis_set_in_memory("materialis_can_show_cached_value_{$slug}", $result);
     83     
     84     return $result;
     85 }
     86 
     87 function materialis_cache_value($slug, $value, $cache_on_ajax = false)
     88 {
     89     
     90     if (wp_doing_ajax()) {
     91         if ( ! $cache_on_ajax) {
     92             return;
     93         }
     94     }
     95     
     96     if (materialis_is_customize_preview()) {
     97         return;
     98     }
     99 
    100     $cached_values = get_option(materialis_get_cache_option_key(), array());
    101     
    102     $cached_values[$slug] = $value;
    103     
    104     update_option(materialis_get_cache_option_key(), $cached_values, 'yes');
    105     
    106 }
    107 
    108 function materialis_remove_cached_value($slug)
    109 {
    110     $cached_values = get_option(materialis_get_cache_option_key(), array());
    111     
    112     if (isset($cached_values[$slug])) {
    113         unset($cached_values[$slug]);
    114     }
    115     
    116     update_option(materialis_get_cache_option_key(), $cached_values, 'yes');
    117 }
    118 
    119 function materialis_get_cached_value($slug)
    120 {
    121     $cached_values = get_option(materialis_get_cache_option_key(), array());
    122     
    123     if (isset($cached_values[$slug])) {
    124         return $cached_values[$slug];
    125     }
    126     
    127     return null;
    128 }
    129 
    130 function materialis_clear_cached_values()
    131 {
    132     // cleanup old cached values
    133     $slugs = get_option('materialis_cached_values_slugs', array());
    134     
    135     if (count($slugs)) {
    136         foreach ($slugs as $slug) {
    137             materialis_remove_cached_value($slug);
    138         }
    139         
    140         delete_option('materialis_cached_values_slugs');
    141     }
    142     // cleanup old cached values
    143     
    144     delete_option(materialis_get_cache_option_key());
    145     
    146     if (class_exists('autoptimizeCache')) {
    147         autoptimizeCache::clearall();
    148     }
    149 }
    150 
    151 add_action('cloudpress\companion\clear_caches', 'materialis_clear_cached_values');
    152 
    153 function materialis_get_var($name)
    154 {
    155     global $materialis_variables;
    156 
    157     return $materialis_variables[$name];
    158 }
    159 
    160 function materialis_wrap_with_single_quote($element)
    161 {
    162     return "&apos;{$element}&apos;";
    163 }
    164 
    165 function materialis_wrap_with_double_quotes($element)
    166 {
    167     return "&quot;{$element}&quot;";
    168 }
    169 
    170 function materialis_wp_kses_post($text)
    171 {
    172     // fix the issue with rgb / rgba colors in style atts
    173 
    174     $rgbRegex = "#rgb\(((?:\s*\d+\s*,){2}\s*[\d]+)\)#i";
    175     $text     = preg_replace($rgbRegex, "rgb__$1__rgb", $text);
    176 
    177     $rgbaRegex = "#rgba\(((\s*\d+\s*,){3}[\d\.]+)\)#i";
    178     $text      = preg_replace($rgbaRegex, "rgba__$1__rgb", $text);
    179 
    180 
    181     // fix google fonts
    182     $fontsOption       = apply_filters('materialis_google_fonts', materialis_get_general_google_fonts());
    183     $fonts             = array_keys($fontsOption);
    184     $singleQuotedFonts = array_map('materialis_wrap_with_single_quote', $fonts);
    185     $doubleQuotedFonts = array_map('materialis_wrap_with_double_quotes', $fonts);
    186 
    187 
    188     $text = str_replace($singleQuotedFonts, $fonts, $text);
    189     $text = str_replace($doubleQuotedFonts, $fonts, $text);
    190 
    191 
    192     $text = wp_kses_post($text);
    193 
    194 
    195     $text = str_replace("rgba__", "rgba(", $text);
    196     $text = str_replace("rgb__", "rgb(", $text);
    197     $text = str_replace("__rgb", ")", $text);
    198 
    199     return $text;
    200 }
    201 
    202 /**
    203  * wrapper over esc_url with small fixes
    204  */
    205 function materialis_esc_url($url)
    206 {
    207     $url = str_replace("^", "%5E", $url); // fix ^ in file name before escape
    208 
    209     return esc_url($url);
    210 }
    211 
    212 function materialis_setup()
    213 {
    214     global $content_width;
    215 
    216     if ( ! isset($content_width)) {
    217         $content_width = 3840; // 4k :) - content width should be adapted from css not hardcoded
    218     }
    219 
    220     load_theme_textdomain('materialis', get_template_directory() . '/languages');
    221 
    222     add_theme_support('automatic-feed-links');
    223     add_theme_support('title-tag');
    224     add_theme_support('post-thumbnails');
    225 
    226     set_post_thumbnail_size(1232, 0, false);
    227 
    228     register_default_headers(array(
    229         'homepage-image' => array(
    230             'url'           => '%s/assets/images/header-bg-image-default.jpg',
    231             'thumbnail_url' => '%s/assets/images/header-bg-image-default.jpg',
    232             'description'   => esc_html__('Homepage Header Image', 'materialis'),
    233         ),
    234     ));
    235 
    236     add_theme_support('custom-header', apply_filters('materialis_custom_header_args', array(
    237         'default-image' => materialis_mod_default('inner_page_header_background', get_template_directory_uri() . "/assets/images/header-bg-image-default.jpg"),
    238         'width'         => 1920,
    239         'height'        => 800,
    240         'flex-height'   => true,
    241         'flex-width'    => true,
    242         'header-text'   => false,
    243     )));
    244 
    245     add_theme_support('custom-logo', array(
    246         'flex-height' => true,
    247         'flex-width'  => true,
    248         'width'       => 150,
    249         'height'      => 70,
    250     ));
    251 
    252     add_theme_support('customize-selective-refresh-widgets');
    253 
    254     add_theme_support('custom-background', array(
    255         'default-color' => '#f5fafd',
    256     ));
    257 
    258     add_image_size('materialis-full-hd', 1920, 1080);
    259 
    260     register_nav_menus(array(
    261         'primary'     => esc_html__('Primary Menu', 'materialis'),
    262         'footer_menu' => esc_html__('Footer Menu', 'materialis'),
    263     ));
    264 
    265     include_once get_template_directory() . '/customizer/kirki/kirki.php';
    266 
    267     Kirki::add_config('materialis', array(
    268         'capability'  => 'edit_theme_options',
    269         'option_type' => 'theme_mod',
    270     ));
    271 
    272     materialis_theme_page();
    273     materialis_suggest_plugins();
    274 
    275 }
    276 
    277 add_action('after_setup_theme', 'materialis_setup');
    278 
    279 function materialis_full_hd_image_size_label($sizes)
    280 {
    281     return array_merge($sizes, array(
    282         'materialis-full-hd' => __('Full HD', 'materialis'),
    283     ));
    284 }
    285 
    286 add_filter('image_size_names_choose', 'materialis_full_hd_image_size_label');
    287 
    288 function materialis_suggest_plugins()
    289 {
    290 
    291     require_once get_template_directory() . '/inc/companion.php';
    292 
    293     /* tgm-plugin-activation */
    294     require_once get_template_directory() . '/class-tgm-plugin-activation.php';
    295 
    296     $plugins = array(
    297         'materialis-companion' => array(
    298             'title'       => esc_html__('Materialis Companion', 'materialis'),
    299             'description' => esc_html__('Materialis Companion plugin adds drag and drop functionality and many other features to the Materialis theme.', 'materialis'),
    300             'activate'    => array(
    301                 'label' => esc_html__('Activate', 'materialis'),
    302             ),
    303             'install'     => array(
    304                 'label' => esc_html__('Install', 'materialis'),
    305             ),
    306         ),
    307         'contact-form-7'       => array(
    308             'title'       => esc_html__('Contact Form 7', 'materialis'),
    309             'description' => esc_html__('The Contact Form 7 plugin is recommended for the Materialis contact section.', 'materialis'),
    310             'activate'    => array(
    311                 'label' => esc_html__('Activate', 'materialis'),
    312             ),
    313             'install'     => array(
    314                 'label' => esc_html__('Install', 'materialis'),
    315             ),
    316         ),
    317     );
    318     $plugins = apply_filters('materialis_theme_info_plugins', $plugins);
    319     \Materialis\Companion_Plugin::init(array(
    320         'slug'           => 'materialis-companion',
    321         'activate_label' => esc_html__('Activate Materialis Companion', 'materialis'),
    322         'activate_msg'   => esc_html__('This feature requires the Materialis Companion plugin to be activated.', 'materialis'),
    323         'install_label'  => esc_html__('Install Materialis Companion', 'materialis'),
    324         'install_msg'    => esc_html__('This feature requires the Materialis Companion plugin to be installed.', 'materialis'),
    325         'plugins'        => $plugins,
    326     ));
    327 }
    328 
    329 function materialis_tgma_suggest_plugins()
    330 {
    331     $plugins = array(
    332         array(
    333             'name'     => 'Materialis Companion',
    334             'slug'     => 'materialis-companion',
    335             'required' => false,
    336         ),
    337 
    338         array(
    339             'name'     => 'Contact Form 7',
    340             'slug'     => 'contact-form-7',
    341             'required' => false,
    342         ),
    343     );
    344 
    345     $plugins = apply_filters('materialis_tgmpa_plugins', $plugins);
    346 
    347     $config = array(
    348         'id'           => 'materialis',
    349         'default_path' => '',
    350         'menu'         => 'tgmpa-install-plugins',
    351         'has_notices'  => true,
    352         'dismissable'  => true,
    353         'dismiss_msg'  => '',
    354         'is_automatic' => false,
    355         'message'      => '',
    356     );
    357 
    358     $config = apply_filters('materialis_tgmpa_config', $config);
    359 
    360     tgmpa($plugins, $config);
    361 }
    362 
    363 add_action('tgmpa_register', 'materialis_tgma_suggest_plugins');
    364 
    365 function materialis_can_show_demo_content()
    366 {
    367     return apply_filters("materialis_can_show_demo_content", current_user_can('edit_theme_options'));
    368 }
    369 
    370 function materialis_get_version()
    371 {
    372     $theme = wp_get_theme();
    373 
    374     if ($theme->get('Template')) {
    375         $theme = wp_get_theme($theme->get('Template'));
    376     }
    377 
    378     $ver = $theme->get('Version');
    379     $ver = apply_filters('materialis_get_version', $ver);
    380 
    381     if ($ver === '@@buildnumber@@') {
    382         $ver = "99.99." . time();
    383     }
    384 
    385     return $ver;
    386 }
    387 
    388 function materialis_get_text_domain()
    389 {
    390     $theme = wp_get_theme();
    391 
    392     $textDomain = $theme->get('TextDomain');
    393 
    394     if ($theme->get('Template')) {
    395         $templateData = wp_get_theme($theme->get('Template'));
    396         $textDomain   = $templateData->get('TextDomain');
    397     }
    398 
    399     return $textDomain;
    400 }
    401 
    402 function materialis_require($path)
    403 {
    404     $path = trim($path, "\\/");
    405 
    406     $isInPro = locate_template("/pro/$path") && ! (defined("MATERIALIS_ONLY_FREE") && MATERIALIS_ONLY_FREE);
    407 
    408     if ($isInPro) {
    409         require_once get_template_directory() . "/{$path}";
    410     } else {
    411         if (file_exists(get_template_directory() . "/{$path}")) {
    412             require_once get_template_directory() . "/{$path}";
    413         }
    414     }
    415 
    416 }
    417 
    418 if ( ! class_exists("Kirki")) {
    419     include_once get_template_directory() . '/customizer/kirki/kirki.php';
    420 }
    421 
    422 materialis_require('/inc/templates-functions.php');
    423 materialis_require('/inc/theme-options.php');
    424 
    425 
    426 function materialis_add_kirki_field($args)
    427 {
    428    $has_cached_values = materialis_can_show_cached_value("materialis_cached_kirki_style_materialis");
    429    
    430 	if ( ! $has_cached_values) {
    431         $args = apply_filters('materialis_kirki_field_filter', $args);
    432 
    433 		$fallback = isset($args['default']) ? $args['default'] : null;
    434    	 	$default  = materialis_mod_default($args['settings'], $fallback);
    435     	$args['default'] = $default;
    436     	
    437 		Kirki::add_field('materialis', $args);
    438     }
    439 }
    440 
    441 // SCRIPTS AND STYLES
    442 
    443 function materialis_replace_file_extension($filename, $old_extenstion, $new_extension)
    444 {
    445     return preg_replace('#\\' . $old_extenstion . '$#', $new_extension, $filename);
    446 }
    447 
    448 
    449 function materialis_enqueue($type = 'style', $handle = '', $args = array())
    450 {
    451     $theme = wp_get_theme();
    452     $ver   = $theme->get('Version');
    453     $data  = array_merge(array(
    454         'src'        => '',
    455         'deps'       => array(),
    456         'has_min'    => false,
    457         'in_footer'  => true,
    458         'media'      => 'all',
    459         'ver'        => $ver,
    460         'in_preview' => true,
    461     ), $args);
    462 
    463     if (materialis_is_customize_preview() && $data['in_preview'] === false) {
    464         return;
    465     }
    466 
    467     $isScriptDebug = defined("SCRIPT_DEBUG") && SCRIPT_DEBUG;
    468 
    469     if ($data['has_min'] && ! $isScriptDebug) {
    470         if ($type === 'style') {
    471             $data['src'] = materialis_replace_file_extension($data['src'], '.css', '.min.css');
    472         }
    473 
    474         if ($type === 'script') {
    475             $data['src'] = materialis_replace_file_extension($data['src'], '.js', '.min.js');
    476         }
    477     }
    478 
    479     if ($type == 'style') {
    480         wp_enqueue_style($handle, $data['src'], $data['deps'], $data['ver'], $data['media']);
    481     }
    482 
    483     if ($type == 'script') {
    484         wp_enqueue_script($handle, $data['src'], $data['deps'], $data['ver'], $data['in_footer']);
    485     }
    486 
    487 }
    488 
    489 function materialis_enqueue_style($handle, $args)
    490 {
    491     materialis_enqueue('style', $handle, $args);
    492 }
    493 
    494 function materialis_enqueue_script($handle, $args)
    495 {
    496     materialis_enqueue('script', $handle, $args);
    497 }
    498 
    499 function materialis_add_script_data($data)
    500 {
    501 
    502     add_filter('materialis_script_data', function ($value) use ($data) {
    503         foreach ((array)$data as $key => $value) {
    504             $value[$key] = $value;
    505         }
    506 
    507         return $value;
    508     });
    509 
    510 }
    511 
    512 function materialis_associative_array_splice($oldArray, $offset, $key, $data)
    513 {
    514     $newArray = array_slice($oldArray, 0, $offset, true) +
    515     array($key => $data) +
    516     array_slice($oldArray, $offset, null, true);
    517 
    518     return $newArray;
    519 }
    520 
    521 function materialis_enqueue_styles($textDomain, $ver, $is_child)
    522 {
    523     
    524     materialis_enqueue_style(
    525         $textDomain . '-style',
    526         array(
    527             'src'     => get_stylesheet_uri(),
    528             'has_min' => apply_filters('materialis_stylesheet_has_min', ! $is_child),
    529             'deps'    => apply_filters('materialis_stylesheet_deps', array()),
    530         )
    531     );
    532     
    533     if (apply_filters('materialis_load_bundled_version', true)) {
    534 
    535         /*
    536             icon font files have relative paths to ../../assets/fonts/vendor/mdi/ that break for pro path
    537             and can't be bundled and webpack crashes on second sass compile for material-icons
    538         */
    539 
    540         materialis_enqueue_style(
    541             $textDomain . '-material-icons',
    542             array(
    543                 'src'     => get_template_directory_uri() . '/assets/css/material-icons.css',
    544                 'has_min' => true,
    545             )
    546         );
    547 
    548         materialis_enqueue_style(
    549             $textDomain . '-style-bundle',
    550             array(
    551                 'src' => get_template_directory_uri() . '/assets/css/theme.bundle.min.css',
    552             )
    553         );
    554 
    555     } else {
    556 
    557 	    materialis_enqueue_style(
    558 		$textDomain . '-material-icons',
    559 		array(
    560 		    'src'     => get_template_directory_uri() . '/assets/css/material-icons.css',
    561 		    'has_min' => true,
    562 		)
    563 	    );
    564 
    565 	    materialis_enqueue_style(
    566 		'animate',
    567 		array(
    568 		    'src'     => get_template_directory_uri() . '/assets/css/animate.css',
    569 		    'has_min' => true,
    570 		)
    571 	    );
    572 
    573 	    materialis_enqueue_style(
    574 		$textDomain . '-webgradients',
    575 		array(
    576 		    'src'     => get_template_directory_uri() . '/assets/css/webgradients.css',
    577 		    'has_min' => true,
    578 		)
    579 	    );
    580      }
    581 }
    582 
    583 function materialis_defer_js_scripts($tag)
    584 {
    585     $matches = array(
    586         'theme.bundle.min.js',
    587         'companion.bundle.min.js',
    588         includes_url('/js/masonry.min.js'),
    589         includes_url('/js/imagesloaded.min.js'),
    590         includes_url('/js/wp-embed.min.js'),
    591     );
    592     
    593     foreach ($matches as $match) {
    594         if (strpos($tag, $match) !== false) {
    595             return str_replace('src', ' defer="defer" src', $tag);
    596         }
    597     }
    598     
    599     return $tag;
    600     
    601 }
    602 
    603 add_filter('script_loader_tag', 'materialis_defer_js_scripts', 11, 1);
    604 
    605 function materialis_defer_css_scripts($tag)
    606 {
    607     if (!is_admin())
    608     {
    609         $matches = array(
    610             'fonts.googleapis.com',
    611             'companion.bundle.min.css',
    612         );
    613     
    614     } else {
    615         $matches = array();
    616 
    617     }
    618     
    619     if ( ! materialis_is_customize_preview()) {
    620         foreach ($matches as $match) {
    621             if (strpos($tag, $match) !== false) {
    622                 return str_replace('href', ' data-href', $tag);
    623             }
    624         }
    625     }
    626     
    627     return $tag;
    628 }
    629 
    630 add_filter('style_loader_tag', 'materialis_defer_css_scripts', 11, 1);
    631 
    632 add_action('wp_head', function () {
    633     ?>
    634     <script type="text/javascript" data-name="async-styles">
    635         (function () {
    636             var links = document.querySelectorAll('link[data-href]');
    637             for (var i = 0; i < links.length; i++) {
    638                 var item = links[i];
    639                 item.href = item.getAttribute('data-href')
    640             }
    641         })();
    642     </script>
    643     <?php
    644 });
    645 function materialis_print_scripts_data()
    646 {
    647     $data      = apply_filters('materialis_theme_data_script', array());
    648     $data_text = json_encode($data);
    649     $script    = "MaterialisTheme = {$data_text}";
    650     wp_add_inline_script('jquery-core', $script, 'after');
    651 }
    652 
    653 add_action('wp_enqueue_scripts', 'materialis_print_scripts_data', 40);
    654 
    655 function materialis_enqueue_scripts($textDomain, $ver, $is_child)
    656 {
    657     
    658     if (apply_filters('materialis_load_bundled_version', true)) {
    659         $theme_deps = array('jquery', 'jquery-effects-core', 'jquery-effects-slide', 'masonry');
    660         materialis_enqueue_script(
    661             $textDomain . '-theme',
    662             array(
    663                 "src"  => get_template_directory_uri() . '/assets/js/theme.bundle.min.js',
    664                 "deps" => $theme_deps,
    665             )
    666         );
    667         
    668     } else {
    669 
    670 	    materialis_enqueue_script(
    671 		$textDomain . '-smoothscroll',
    672 		array(
    673 		    'src'     => get_template_directory_uri() . '/assets/js/smoothscroll.js',
    674 		    'deps'    => array('jquery', 'jquery-effects-core'),
    675 		    'has_min' => true,
    676 		)
    677 	    );
    678 
    679 	    materialis_enqueue_script(
    680 		$textDomain . '-ddmenu',
    681 		array(
    682 		    'src'     => get_template_directory_uri() . '/assets/js/drop_menu_selection.js',
    683 		    'deps'    => array('jquery-effects-slide', 'jquery'),
    684 		    'has_min' => true,
    685 		)
    686 	    );
    687 
    688 	    materialis_enqueue_script(
    689 		'kube',
    690 		array(
    691 		    'src'     => get_template_directory_uri() . '/assets/js/kube.js',
    692 		    'deps'    => array('jquery'),
    693 		    'has_min' => true,
    694 		)
    695 	    );
    696 
    697 	    materialis_enqueue_script(
    698 		$textDomain . '-fixto',
    699 		array(
    700 		    'src'     => get_template_directory_uri() . '/assets/js/libs/fixto.js',
    701 		    'deps'    => array('jquery'),
    702 		    'has_min' => true,
    703 		)
    704 	    );
    705 
    706 	    wp_enqueue_script($textDomain . '-sticky', get_template_directory_uri() . '/assets/js/sticky.js', array($textDomain . '-fixto'), $ver, true);
    707 	    $theme_deps = apply_filters("materialis_theme_deps", array('jquery', 'masonry'));
    708 
    709 	    wp_enqueue_script($textDomain . '-theme', get_template_directory_uri() . '/assets/js/theme.js', $theme_deps, $ver, true);
    710 
    711 	    materialis_add_script_data(apply_filters('materialis_theme_data_script', array()));
    712     }	
    713 
    714     $maxheight = intval(materialis_get_theme_mod('logo_max_height', 70));
    715     wp_add_inline_style($textDomain . '-style', sprintf('img.logo.dark, img.custom-logo{width:auto;max-height:%1$s;}', $maxheight . "px"));
    716 
    717     if (is_singular() && comments_open() && get_option('thread_comments')) {
    718         wp_enqueue_script('comment-reply');
    719     }
    720 }
    721 
    722 
    723 function materialis_do_enqueue_assets()
    724 {
    725 
    726     $theme        = wp_get_theme();
    727     $ver          = $theme->get('Version');
    728     $isChildTheme = ($theme->get('Template'));
    729     $textDomain   = materialis_get_text_domain();
    730 
    731     materialis_enqueue_styles($textDomain, $ver, $isChildTheme);
    732     materialis_enqueue_scripts($textDomain, $ver, $isChildTheme);
    733 }    
    734  
    735 add_action('wp_enqueue_scripts', 'materialis_do_enqueue_assets');
    736 
    737 
    738 function materialis_customize_controls_enqueue_scripts_spectrum()
    739 {
    740 
    741     $theme = wp_get_theme();
    742     $ver   = $theme->get('Version');
    743 
    744     if ( ! apply_filters('materialis_load_bundled_version', true)) {
    745     wp_enqueue_style('materialis-customizer-spectrum', get_template_directory_uri() . '/customizer/libs/spectrum.css', array(), $ver);
    746     wp_enqueue_script('materialis-customizer-spectrum', get_template_directory_uri() . '/customizer/libs/spectrum.js', array(), $ver, true);
    747     }
    748 }
    749 
    750 add_action('customize_controls_enqueue_scripts', 'materialis_customize_controls_enqueue_scripts_spectrum');
    751 
    752 function materialis_get_general_google_fonts()
    753 {
    754     return array(
    755         array(
    756             'family'  => 'Roboto',
    757             "weights" => array("300", "300italic", "400", "400italic", "500", "500italic", "700", "700italic", "900", "900italic",),
    758         ),
    759         array(
    760             'family'  => 'Playfair Display',
    761             "weights" => array("400", "400italic", "700", "700italic"),
    762         ),
    763     );
    764 }
    765 
    766 function materialis_do_enqueue_google_fonts()
    767 {
    768     $fontsURL = array();
    769     if (materialis_can_show_cached_value('materialis_google_fonts')) {
    770         
    771         $fontsURL = materialis_get_cached_value('materialis_google_fonts');
    772     } else {
    773 	    $gFonts = materialis_get_general_google_fonts();
    774 
    775 	    $fonts = array();
    776 
    777 	    foreach ($gFonts as $font) {
    778 		$fonts[$font['family']] = $font;
    779 	    }
    780 
    781 	    $gFonts    = apply_filters("materialis_google_fonts", $fonts);
    782 	    $fontQuery = array();
    783 	    foreach ($gFonts as $family => $font) {
    784 		$fontQuery[] = $family . ":" . implode(',', $font['weights']);
    785 	    }
    786 
    787 	    $query_args = array(
    788 		'family' => implode('%7C', $fontQuery),
    789 		'subset' => 'latin,latin-ext',
    790 	    );
    791 
    792         $fontsURL = add_query_arg($query_args, 'https://fonts.googleapis.com/css');
    793 
    794         materialis_cache_value('materialis_google_fonts', $fontsURL);
    795     }
    796 
    797     wp_enqueue_style('materialis-fonts', $fontsURL, array(), null);
    798 }
    799 
    800 add_action('wp_enqueue_scripts', 'materialis_do_enqueue_google_fonts');
    801 /**
    802  * Add a pingback url auto-discovery header for singularly identifiable articles.
    803  */
    804 function materialis_pingback_header()
    805 {
    806     if (is_singular() && pings_open()) {
    807         printf('<link rel="pingback" href="%s">' . "\n", esc_url(get_bloginfo('pingback_url')));
    808     }
    809 }
    810 
    811 add_action('wp_head', 'materialis_pingback_header');
    812 
    813 
    814 /**
    815  * Register sidebar
    816  */
    817 function materialis_widgets_init()
    818 {
    819 
    820     $sidebars_defaults = array(
    821         'before_widget' => '<div id="%1$s" class="widget %2$s mdc-elevation--z5">',
    822         'after_widget'  => '</div>',
    823         'before_title'  => '<h5 class="widgettitle"><i class="mdi widget-icon"></i>',
    824         'after_title'   => '</h5>',
    825     );
    826 
    827     register_sidebar(array_merge(array(
    828         'name' => esc_html__('Sidebar widget area', 'materialis'),
    829         'id'   => 'sidebar-1',
    830     ), $sidebars_defaults));
    831 
    832     register_sidebar(array_merge(array(
    833         'name' => esc_html__('Pages Sidebar', 'materialis'),
    834         'id'   => "materialis_pages_sidebar",
    835     ), $sidebars_defaults));
    836 
    837     register_sidebar(array(
    838         'name'          => esc_html__("Footer First Box Widgets", 'materialis'),
    839         'id'            => "first_box_widgets",
    840         'title'         => esc_html__("Widget Area", 'materialis'),
    841         'before_widget' => '<div id="%1$s" class="widget %2$s ">',
    842         'after_widget'  => '</div>',
    843         'before_title'  => '<h4 class="widgettitle">',
    844         'after_title'   => '</h4>',
    845     ));
    846 
    847     register_sidebar(array(
    848         'name'          => esc_html__("Footer Second Box Widgets", 'materialis'),
    849         'id'            => "second_box_widgets",
    850         'title'         => esc_html__("Widget Area", 'materialis'),
    851         'before_widget' => '<div id="%1$s" class="widget %2$s">',
    852         'after_widget'  => '</div>',
    853         'before_title'  => '<h4 class="widgettitle">',
    854         'after_title'   => '</h4>',
    855     ));
    856 
    857     register_sidebar(array(
    858         'name'          => esc_html__("Footer Third Box Widgets", 'materialis'),
    859         'id'            => "third_box_widgets",
    860         'title'         => esc_html__("Widget Area", 'materialis'),
    861         'before_widget' => '<div id="%1$s" class="widget %2$s">',
    862         'after_widget'  => '</div>',
    863         'before_title'  => '<h4 class="widgettitle">',
    864         'after_title'   => '</h4>',
    865     ));
    866 }
    867 
    868 add_action('widgets_init', 'materialis_widgets_init');
    869 
    870 /**
    871  * Replaces "[...]" (appended to automatically generated excerpts) with ... and
    872  * a 'Read more' link.
    873  *
    874  * @return string '... Read more'
    875  */
    876 function materialis_excerpt_more($link)
    877 {
    878     if (is_admin()) {
    879         return $link;
    880     }
    881 
    882     return '&nbsp;&hellip;';
    883 }
    884 
    885 add_filter('excerpt_more', 'materialis_excerpt_more');
    886 
    887 // UTILS
    888 
    889 
    890 function materialis_nomenu_fallback($walker = '')
    891 {
    892     $drop_down_menu_classes      = apply_filters('materialis_primary_drop_menu_classes', array('default'));
    893     $drop_down_menu_classes      = array_merge($drop_down_menu_classes, array('main-menu', 'dropdown-menu'));
    894     $drop_down_menu_main_classes = array_merge($drop_down_menu_classes, array('row'));
    895 
    896     return wp_page_menu(array(
    897         "menu_class" => esc_attr(implode(" ", $drop_down_menu_main_classes)),
    898         "menu_id"    => 'mainmenu_container',
    899         'before'     => '<ul id="main_menu" class="' . esc_attr(implode(" ", $drop_down_menu_classes)) . '">',
    900         'after'      => apply_filters('materialis_nomenu_after', '') . "</ul>",
    901         'walker'     => $walker,
    902     ));
    903 }
    904 
    905 
    906 function materialis_nomenu_cb()
    907 {
    908     return materialis_nomenu_fallback('');
    909 }
    910 
    911 function materialis_footer_nomenu_cb_filter_depth($args)
    912 {
    913     $args['depth'] = 1;
    914 
    915     return $args;
    916 }
    917 
    918 function materialis_footer_nomenu_cb()
    919 {
    920     add_filter('wp_page_menu_args', 'materialis_footer_nomenu_cb_filter_depth');
    921     $menu = wp_page_menu(array(
    922         "menu_class" => "materialis-footer-menu",
    923         "menu_id"    => 'materialis-footer-menu',
    924         'before'     => '<ul id="materialis-footer-menu" class="materialis-footer-menu">',
    925         'after'      => apply_filters('materialis_nomenu_after', '') . "</ul>",
    926         'walker'     => '',
    927     ));
    928     remove_filter('wp_page_menu_args', 'materialis_footer_nomenu_cb_filter_depth');
    929 
    930     return $menu;
    931 }
    932 
    933 function materialis_no_hamburger_menu_cb()
    934 {
    935     return wp_page_menu(array(
    936         "menu_class" => 'offcanvas_menu',
    937         "menu_id"    => 'offcanvas_menu',
    938         'before'     => '<ul id="offcanvas_menu" class="offcanvas_menu">',
    939         'after'      => apply_filters('materialis_nomenu_after', '') . "</ul>",
    940     ));
    941 }
    942 
    943 function materialis_title()
    944 {
    945     $title = '';
    946 
    947     if (is_404()) {
    948         $title = __('Page not found', 'materialis');
    949     } else if (is_search()) {
    950         $title = sprintf(__('Search Results for &#8220;%s&#8221;', 'materialis'), get_search_query());
    951     } else if (is_home()) {
    952         if (is_front_page()) {
    953             $title = get_bloginfo('name');
    954         } else {
    955             $title = single_post_title();
    956         }
    957     } else if (is_archive()) {
    958         if (is_post_type_archive()) {
    959             $title = post_type_archive_title('', false);
    960         } else {
    961             $title = get_the_archive_title();
    962         }
    963     } else if (is_single()) {
    964         $title = get_bloginfo('name');
    965 
    966         global $post;
    967         if ($post) {
    968             // apply core filter
    969             $title = apply_filters('single_post_title', $post->post_title, $post);
    970         }
    971     } else {
    972         $title = get_the_title();
    973     }
    974 
    975     $value = apply_filters('materialis_header_title', materialis_wp_kses_post($title));
    976 
    977     return $value;
    978 }
    979 
    980 function materialis_bold_text($str)
    981 {
    982     $bold = materialis_get_theme_mod('bold_logo', true);
    983 
    984     if ( ! $bold) {
    985         return $str;
    986     }
    987 
    988     $str   = trim($str);
    989     $words = preg_split("/(?<=[a-z])(?=[A-Z])|(?=[\s]+)/x", $str);
    990 
    991     $result = "";
    992     $c      = 0;
    993     for ($i = 0; $i < count($words); $i++) {
    994         $word = $words[$i];
    995         if (preg_match("/^\s*$/", $word)) {
    996             $result .= $words[$i];
    997         } else {
    998             $c++;
    999             if ($c % 2) {
   1000                 $result .= $words[$i];
   1001             } else {
   1002                 $result .= '<span style="font-weight: 300;" class="span12">' . esc_html($words[$i]) . "</span>";
   1003             }
   1004         }
   1005     }
   1006 
   1007     return $result;
   1008 }
   1009 
   1010 
   1011 function materialis_sanitize_checkbox($val)
   1012 {
   1013     return (isset($val) && $val == true ? true : false);
   1014 }
   1015 
   1016 function materialis_sanitize_textfield($val)
   1017 {
   1018     return wp_kses_post(force_balance_tags($val));
   1019 }
   1020 
   1021 if ( ! function_exists('materialis_post_type_is')) {
   1022     function materialis_post_type_is($type)
   1023     {
   1024         global $wp_query;
   1025 
   1026         $post_type = $wp_query->query_vars['post_type'] ? $wp_query->query_vars['post_type'] : 'post';
   1027 
   1028         if ( ! is_array($type)) {
   1029             $type = array($type);
   1030         }
   1031 
   1032         return in_array($post_type, $type);
   1033     }
   1034 }
   1035 
   1036 function materialis_to_bool($value)
   1037 {
   1038     if (is_bool($value)) {
   1039         return $value;
   1040     }
   1041 
   1042     if (is_string($value)) {
   1043         if (strtolower($value) === "yes" || strtolower($value) === "true") {
   1044             return true;
   1045         }
   1046 
   1047         if (strtolower($value) === "no" || strtolower($value) === "false") {
   1048             return false;
   1049         }
   1050     }
   1051 
   1052     if (is_numeric()) {
   1053         return ! ! intval($value);
   1054     }
   1055 
   1056     return ! ! $value;
   1057 }
   1058 
   1059 //////////////////////////////////////////////////////////////////////////////////////
   1060 
   1061 
   1062 function materialis_footer_container($class)
   1063 {
   1064     $attrs = array(
   1065         'class' => "footer " . $class . " ",
   1066     );
   1067 
   1068     $result = "";
   1069 
   1070     $attrs = apply_filters('materialis_footer_container_atts', $attrs);
   1071 
   1072     foreach ($attrs as $key => $value) {
   1073         $value  = esc_attr(trim($value));
   1074         $key    = esc_attr($key);
   1075         $result .= " {$key}='{$value}'";
   1076     }
   1077 
   1078     return $result;
   1079 }
   1080 
   1081 
   1082 // THEME PAGE
   1083 function materialis_theme_page()
   1084 {
   1085     add_action('admin_menu', 'materialis_register_theme_page');
   1086 }
   1087 
   1088 function materialis_load_theme_partial()
   1089 {
   1090     require_once get_template_directory() . '/inc/companion.php';
   1091     require_once get_template_directory() . "/inc/theme-info.php";
   1092     wp_enqueue_style('materialis-theme-info', get_template_directory_uri() . "/assets/css/theme-info.css");
   1093     wp_enqueue_script('materialis-theme-info', get_template_directory_uri() . "/assets/js/theme-info.js", array('jquery'), '', true);
   1094 }
   1095 
   1096 function materialis_register_theme_page()
   1097 {
   1098     add_theme_page(__('Materialis Info', 'materialis'), __('Materialis Info', 'materialis'), 'activate_plugins', 'materialis-welcome', 'materialis_load_theme_partial');
   1099 }
   1100 
   1101 
   1102 function materialis_instantiate_widget($widget, $args = array())
   1103 {
   1104 
   1105     ob_start();
   1106     the_widget($widget, array(), $args);
   1107     $content = ob_get_contents();
   1108     ob_end_clean();
   1109 
   1110     if (isset($args['wrap_tag'])) {
   1111         $tag     = $args['wrap_tag'];
   1112         $class   = isset($args['wrap_class']) ? $args['wrap_class'] : "";
   1113         $content = "<{$tag} class='{$class}'>{$content}</{$tag}>";
   1114     }
   1115 
   1116     return $content;
   1117 
   1118 }
   1119 
   1120 // load support for woocommerce
   1121 if (class_exists('WooCommerce')) {
   1122     require_once get_template_directory() . "/inc/woocommerce/woocommerce.php";
   1123 } else {
   1124     require_once get_template_directory() . "/inc/woocommerce/woocommerce-ready.php";
   1125 }
   1126 
   1127 materialis_require("/inc/integrations/index.php");
   1128 
   1129 function materialis_is_woocommerce_page()
   1130 {
   1131 
   1132     if (function_exists("is_woocommerce") && is_woocommerce()) {
   1133         return true;
   1134     }
   1135 
   1136     $woocommerce_keys = array(
   1137         "woocommerce_shop_page_id",
   1138         "woocommerce_terms_page_id",
   1139         "woocommerce_cart_page_id",
   1140         "woocommerce_checkout_page_id",
   1141         "woocommerce_pay_page_id",
   1142         "woocommerce_thanks_page_id",
   1143         "woocommerce_myaccount_page_id",
   1144         "woocommerce_edit_address_page_id",
   1145         "woocommerce_view_order_page_id",
   1146         "woocommerce_change_password_page_id",
   1147         "woocommerce_logout_page_id",
   1148         "woocommerce_lost_password_page_id",
   1149     );
   1150 
   1151     foreach ($woocommerce_keys as $wc_page_id) {
   1152         if (get_the_ID() == get_option($wc_page_id, 0)) {
   1153             return true;
   1154         }
   1155     }
   1156 
   1157     return false;
   1158 }
   1159 
   1160 function materialis_customize_save_clear_data($value)
   1161 {
   1162     
   1163     if ( ! isset($value['changeset_status']) || $value['changeset_status'] !== "auto-draft") {
   1164         materialis_clear_cached_values();
   1165     }
   1166     
   1167     return $value;
   1168 }
   1169 
   1170 add_filter("customize_save_response", "materialis_customize_save_clear_data");
   1171 
   1172 
   1173 function materialis_skip_link_focus_fix()
   1174 {
   1175     // The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
   1176     ?>
   1177     <script>
   1178         /(trident|msie)/i.test(navigator.userAgent) && document.getElementById && window.addEventListener && window.addEventListener("hashchange", function() {
   1179             var t, e = location.hash.substring(1);
   1180             /^[A-z0-9_-]+$/.test(e) && (t = document.getElementById(e)) && (/^(?:a|select|input|button|textarea)$/i.test(t.tagName) || (t.tabIndex = -1), t.focus())
   1181         }, !1);
   1182     </script>
   1183 <?php
   1184 }
   1185 
   1186 add_action('wp_print_footer_scripts', 'materialis_skip_link_focus_fix');
   1187 
   1188 function materialis_color_picker_scripts() {
   1189 	wp_enqueue_style('wp-color-picker' );
   1190 	wp_enqueue_script('iris', admin_url('js/iris.min.js'), array('jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch'), false, true);
   1191 	wp_enqueue_script('wp-color-picker', admin_url('js/color-picker.min.js'), array('iris', 'wp'), false, true);
   1192 
   1193 	$colorpicker_l10n = array(
   1194 		'clear' => __( 'Clear', 'materialis' ),
   1195 		'defaultString' => __( 'Default', 'materialis' ), 
   1196 		'pick' => __( 'Select Color', 'materialis' ),	
   1197 		'current' => __( 'Current Color', 'materialis' )
   1198 	);
   1199 	wp_localize_script( 'wp-color-picker', 'wpColorPickerL10n', $colorpicker_l10n );
   1200 }
   1201 
   1202 if (is_customize_preview()) {
   1203 	add_action( 'init', 'materialis_color_picker_scripts');
   1204 }