File: /home/mmickelson/theflexguy.com/wp-content/themes/rt_affinity_wp/includes/common.php
<?php
// Enable shortcodes in widgets
add_filter('widget_text', 'do_shortcode');
// Sample Data Import Helper
add_action('import_start', 'rt_change_path');
add_action('import_end', 'rt_unlink_file');
function rt_change_path() {
global $wp_import;
$xml = file_get_contents($wp_import->file);
$xml = preg_replace("/\@RT_SITE_URL\@/", get_bloginfo('wpurl'), $xml);
$temp = tempnam(sys_get_temp_dir(), "rt_wp_import");
$handle = fopen($temp, "w");
fwrite($handle, $xml);
$wp_import->file = $temp;
fclose($handle);
}
function rt_unlink_file() {
global $wp_import;
unlink($wp_import->file);
}
// Layout Ninja
function layout_ninja() {
$option = get_option('affinity-options');
$layout_option = $option['affinity_layout'];
$layout_sub_option = $option['affinity_layout_sub'];
switch($layout_option) {
case 's-c-x':
$option['affinity_leftsidebar'] = 'true';
$option['affinity_rightsidebar'] = 'false';
update_option('affinity-options', $option);
break;
case 'x-c-s':
$option['affinity_leftsidebar'] = 'false';
$option['affinity_rightsidebar'] = 'true';
update_option('affinity-options', $option);
break;
case 's-c-s':
$option['affinity_leftsidebar'] = 'true';
$option['affinity_rightsidebar'] = 'true';
update_option('affinity-options', $option);
break;
case 'x-c-x':
$option['affinity_leftsidebar'] = 'false';
$option['affinity_rightsidebar'] = 'false';
update_option('affinity-options', $option);
break;
}
switch($layout_sub_option) {
case 's-c-x':
$option['affinity_leftsidebar_sub'] = 'true';
$option['affinity_rightsidebar_sub'] = 'false';
update_option('affinity-options', $option);
break;
case 'x-c-s':
$option['affinity_leftsidebar_sub'] = 'false';
$option['affinity_rightsidebar_sub'] = 'true';
update_option('affinity-options', $option);
break;
case 's-c-s':
$option['affinity_leftsidebar_sub'] = 'true';
$option['affinity_rightsidebar_sub'] = 'true';
update_option('affinity-options', $option);
break;
case 'x-c-x':
$option['affinity_leftsidebar_sub'] = 'false';
$option['affinity_rightsidebar_sub'] = 'false';
update_option('affinity-options', $option);
break;
}
}
add_action('init', 'layout_ninja');
// Theme Math
function theme_math() {
$option = get_option('affinity-options');
$sitewidth = $option['affinity_sitewidth'];
$active_leftsidebar = $option['affinity_leftsidebar'];
$active_rightsidebar = $option['affinity_rightsidebar'];
$active_leftsidebar_sub = $option['affinity_leftsidebar_sub'];
$active_rightsidebar_sub = $option['affinity_rightsidebar_sub'];
switch($active_rightsidebar) {
case 'true':
$option['affinity_mainbody_size'] = $sitewidth - $option['affinity_rightsidebar_size'] - 20;
update_option('affinity-options', $option);
break;
case 'false':
$option['affinity_mainbody_size'] = $sitewidth - 20;
update_option('affinity-options', $option);
break;
}
switch($active_leftsidebar) {
case 'true':
$option['affinity_maincol_size'] = $option['affinity_mainbody_size'] - $option['affinity_leftsidebar_size'] - 45;
update_option('affinity-options', $option);
break;
case 'false':
$option['affinity_maincol_size'] = $option['affinity_mainbody_size'] - 45;
update_option('affinity-options', $option);
break;
}
switch($active_rightsidebar_sub) {
case 'true':
$option['affinity_mainbody_sub_size'] = $sitewidth - $option['affinity_rightsidebar_size'] - 20;
update_option('affinity-options', $option);
break;
case 'false':
$option['affinity_mainbody_sub_size'] = $sitewidth - 20;
update_option('affinity-options', $option);
break;
}
switch($active_leftsidebar_sub) {
case 'true':
$option['affinity_maincol_sub_size'] = $option['affinity_mainbody_sub_size'] - $option['affinity_leftsidebar_size'] - 45;
update_option('affinity-options', $option);
break;
case 'false':
$option['affinity_maincol_sub_size'] = $option['affinity_mainbody_sub_size'] - 45;
update_option('affinity-options', $option);
break;
}
}
add_action('init', 'theme_math');
// Widgets Width Math
function widget_math() {
$option = get_option('affinity-options');
$sitewidth = $option['affinity_sitewidth'];
$option['affinity_showcasewidth'] = $sitewidth - 20;
$option['affinity_userwgetwidth'] = (int) ($sitewidth/3)-13;
update_option('affinity-options', $option);
}
add_action('init', 'widget_math');
// Swap empty titles with
add_filter('widget_title', 'empty_title_swap');
function empty_title_swap($title, $restult='') {
switch($title) {
case '':
$result = ' ';
break;
case $title:
$result = $title;
break;
}
return $result;
}
// Style Switcher
function rok_switcher() {
global $cookie_prefix, $presetstyle;
$cookie_prefix = "affinity";
$cookie_time = time()+31536000;
$grab_theme = $_GET['tstyle'];
$option = get_option('affinity-options');
$presetstyle = $option['affinity_preset'];
$tstyle = $presetstyle;
if (isset($_REQUEST['tstyle'])) {
setcookie ($cookie_prefix.'-tstyle', $grab_theme, $cookie_time, '/', false);
header("Location: ". $_SERVER['PHP_SELF']);
global $tstyle;
}
if(isset($_COOKIE[$cookie_prefix."-tstyle"])) {
rebuildColorParams($_COOKIE[$cookie_prefix."-tstyle"]);
}
$delete_cookies = $_GET['delete_cookies'];
if($delete_cookies == 'true') {
ob_start();
setcookie($cookie_prefix.'-tstyle', '', $cookie_time, '/', false);
header("Location: ". $_SERVER['PHP_SELF']);
exit;
ob_end_flush();
}
}
function rebuildColorParams($grab_theme) {
global $stylesList, $presetstyle;
$style = $stylesList[$grab_theme];
$presetstyle = $style[0];
}
function getSelected($style) {
global $cookie_prefix;
$grab_theme = $_COOKIE[$cookie_prefix."-tstyle"];
if ($style==$grab_theme) return ' selected="selected"';
else return "";
}
// IE version checker
function rok_isIe($version = false) {
$agent=$_SERVER['HTTP_USER_AGENT'];
$found = strpos($agent,'MSIE ');
if ($found) {
if ($version) {
$ieversion = substr(substr($agent,$found+5),0,1);
if ($ieversion == $version) return true;
else return false;
} else {
return true;
}
} else {
return false;
}
if (stristr($agent, 'msie'.$ieversion)) return true;
return false;
}
// Language Handler
rockettheme_translation();
function rockettheme_translation() {
load_theme_textdomain('affinity_lang');
}
function _r($str)
{
return __($str, 'affinity_lang');
}
function _re($str)
{
_e($str, 'affinity_lang');
}
if (!function_exists ("mysql_real_escape_string"))
{
function mysql_real_escape_string ($str)
{
return mysql_escape_string ($str);
}
}
// Update default frontpage post count
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
update_option( 'posts_per_page', 2 );
}
// Catch first image from post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
return $first_img;
}
// WordPress MU Styling
function mu_signup_css() { ?>
<style type="text/css">
.widecolumn .entry p {font-size: 1.05em;}
.widecolumn {line-height: 1.6em;}
.widecolumn {padding: 10px 0 20px 0;margin: 0 auto;width: 450px; margin-bottom: 30px;}
.widecolumn .post {margin: 0;}
</style>
<?php }
function mu_add_signup_css () {
add_action('wp_head','mu_signup_css', 99);
}
add_action('signup_header','mu_add_signup_css');
?>