' : '
'); } } /* Save a settings - multiplurally */ function add_sircontheme_settings($array_key_val){ foreach($array_key_val as $key => $val){ add_sircontheme_setting($key, $val); } } /* Save a setting */ function add_sircontheme_setting($key, $val){ global $sircontheme_settings; $sircontheme_settings[$key] = $val; } /* Fetch a setting */ function get_sircontheme_setting($key){ global $sircontheme_settings; if(isset($sircontheme_settings[$key])){ return $sircontheme_settings[$key]; } return false; } /* Are we on https:// ? */ function is_https(){ if ( (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) return true; else return false; } /* Is the current user logged in as "sircon" ? */ if(!function_exists('is_user_sircon')){ function is_user_sircon() { if(!is_user_logged_in || !current_user_can('manage_options')){ return false; } $current_user = wp_get_current_user(); return ($current_user->user_login == 'sircon'); } } /* Get real if frontpage */ function is_sircontheme_frontpage(){ $is_default_homepage = is_front_page() && is_home(); $is_static_homepage = !$is_default_homepage && is_front_page(); return $is_default_homepage || $is_static_homepage; } /* Manual way of checking if real frontpage */ function is_sircontheme_frontpage2(){ if(get_option('show_on_front') == 'posts' && is_home()){return true;} //Get current url $current_url = is_https() ? 'https' : 'http'; $current_url .= '://'.$_SERVER['HTTP_HOST'].''.$_SERVER['REQUEST_URI']; //Get the wordpress known frontpage url $wp_frontpage_url = home_url(); //Watch for sneaky trailing slash if(substr($current_url, -1) === '/'){$current_url = substr($current_url, 0, -1);} if(substr($wp_frontpage_url, -1) === '/'){$wp_frontpage_url = substr($wp_frontpage_url, 0, -1);} //Now compare! if($current_url == $wp_frontpage_url){ return true; //IS frontpage } return false; //NOT frontpage } /*Give filename, removes appended and extra quotemarks Example give: "'file.php?lol_break_your_filename=true'" returns "file.php" */ function sircontheme_clean_filename($filename){ $filename = str_replace(array('"', "'"), '', $filename); $splitters = array('#', '?'); foreach($splitters as $splitter){ $splitter_pos = strpos($filename, $splitter); if($splitter_pos !== false){ $filesplit = explode($splitter, $filename); $filename = array_shift($filesplit); }; } return $filename; } //Helpful for filters function sircontheme_return_false($a='', $b=''){ return false;} //Helpful for image size dropdowns function array_insert($array,$values,$offset) { return array_slice($array, 0, $offset, true) + $values + array_slice($array, $offset, NULL, true); } /* Get a complete shortcode raw from content */ function get_sircontheme_raw_shortcode($tag, $content, $return_with_tags = true){ if(!has_shortcode($content, $tag)){return '';} $content_split = explode('['.$tag.']', $content); $content_split2 = explode('[/'.$tag.']', array_pop($content_split)); $shortcode_content = array_shift($content_split2); if($return_with_tags){ $shortcode_content = '['.$tag.']'.$shortcode_content.'[/'.$tag.']'; } return $shortcode_content; } global $sircontheme_wpfile_creds; function sircontheme_prepare_wpfile_creds(){ global $sircontheme_wpfile_creds; if(!WP_Filesystem($sircontheme_wpfile_creds)){ $sircontheme_wpfile_creds = request_filesystem_credentials('', '', false, false, null); } } /********************************************************** * Simple functions for adding or removing body classes ***********************************************************/ /* adds a class to the body class */ function add_sircontheme_body_class($class) { global $sircontheme_extra_body_classes; if(!is_array($sircontheme_extra_body_classes)){$sircontheme_extra_body_classes = array();} if(in_array($class, $sircontheme_extra_body_classes)){ return; //already added } $sircontheme_extra_body_classes[] = $class; } /* removes a class from the body_class */ function remove_sircontheme_body_class($class) { global $sircontheme_extra_body_classes; if(!is_array($sircontheme_extra_body_classes)){$sircontheme_extra_body_classes = array();} $where = array_search($class, $sircontheme_extra_body_classes); if($where === false){return;} //Not found unset($sircontheme_extra_body_classes[$where]); } /* -- Ok this does something - it modifies NONE of the body classes if the functions above are not used */ add_filter('body_class', function($c){ global $sircontheme_extra_body_classes; if(!$sircontheme_extra_body_classes){return $c;} foreach($sircontheme_extra_body_classes as $extra_class){ $c[] = $extra_class; } return $c; }); /********************************************************** * Post class function ************************************************************ function post_class() { //does nothing } /********************************************************** * Optimize files! ***********************************************************/ function sircontheme_optimize_js_file($js_filename, $source_folder, $target_folder){ $source_file = $source_folder.$js_filename; $target_file = $target_folder.basename($js_filename); //If the file exists, we only need to update it if the source file has changed since the last time if(file_exists($target_file)){ //Check if changed $option_name = 'js-modified-'.$js_filename; $last_saved = get_option($option_name); $last_modded = filemtime($source_file); $changed = ($last_saved != $last_modded); if(!$changed){ return false; //Do nothing! nothing changed, and file exists } } sircontheme_prepare_wpfile_creds(); global $wp_filesystem; //File has changed, save new content! $most_current_js = $wp_filesystem->get_contents($source_file); //#### DO optimizations here if(!class_exists('JSMin')){ //Fetch this fantastic thing to supercrunch javascript in a safe way. Less requests with fewer kilobytes is always good! include_once(SIRCONTHEME_INCLUDES_PATH.'classes/jsmin.php'); } //Crunch $optimized_content = JSMin::minify($most_current_js); //##### Optimizations done //Save to cache file $wp_filesystem->put_contents($target_file, $optimized_content, FS_CHMOD_FILE); //Update last saved update_option($option_name, $last_modded); return true; //Something was changed or created } // Give list of files, and a target file. Target file will contain all listed files content function sircontheme_many_files_to_one($source_folder, $source_files, $target_file){ //Missing the target file, or at least one css file changed! Create a new one-and-only css-file $single_file_content = ''; sircontheme_prepare_wpfile_creds(); global $wp_filesystem; foreach($source_files as $filename){ //Load css from cache file $source_file = $source_folder.basename($filename); $content_from_source = $wp_filesystem->get_contents($source_file); $readable_path_split = explode('public_html', $source_file); $readable_path = array_pop($readable_path_split); $single_file_content .= '/* Put into this file from '.$readable_path.' */'."\n".$content_from_source."\n\n"; } $wp_filesystem->put_contents($target_file, $single_file_content, FS_CHMOD_FILE); } /* give a word and a text. Will extract the line of text containing that word */ function get_sircontheme_line_with_word($word, $text){ $lines = explode("\n", $text); foreach($lines as $line){ if(strpos($line, $word) !== false){ return $line; } } } /* Speciality function for replacing scss values on the fly */ function get_sircontheme_values_as_scss($vars, $text){ $text = ''; //Add new variables foreach($vars as $varname => $value){ $text .= "\n"; $text .= '$'.$varname.': '.$value.';'; } //Informative text on first line. $informative_txt = '/* Delete this file to reset - You should not need to perform changes in this file. */'; $has_informative = (strpos($text, $informative_txt) !== false); if(!$has_informative){ $text = $informative_txt."\n\n".$text; } return $text; } ?>