/**
  * Here we install the tables and initial data needed to
  * power our special functions
  */
 function install()
 {
     global $wpdb, $db_version;
     require_once ABSPATH . 'wp-admin/includes/upgrade.php';
     $table = $wpdb->prefix . 'wik_sources';
     $link = $wpdb->prefix . 'wik_source_types';
     $life = $wpdb->prefix . 'wik_life_data';
     $q = '';
     if ($wpdb->get_var("show tables like '{$table}'") != $table) {
         $q = "CREATE TABLE " . $table . "( \r\n\t\t\t\tid int NOT NULL AUTO_INCREMENT,\r\n\t\t\t\ttitle varchar(255) NOT NULL,\r\n\t\t\t\tprofile_url varchar(255) NOT NULL,\r\n\t\t\t\tfeed_url varchar(255) NOT NULL,\r\n\t\t\t\ttype boolean NOT NULL,\r\n\t\t\t\tlifestream boolean NOT NULL,\r\n\t\t\t\tupdates boolean NOT NULL,\r\n\t\t\t\tfavicon varchar(255) NOT NULL,\r\n\t\t\t\tUNIQUE KEY id (id)\r\n\t\t\t);";
     }
     if ($wpdb->get_var("show tables like '{$life}'") != $life) {
         $q .= "CREATE TABLE " . $life . "(\r\n\t\t\t\tid int NOT NULL AUTO_INCREMENT,\r\n\t\t\t\tname varchar(255) NOT NULL,\r\n\t\t\t\tcontent TEXT NOT NULL,\r\n\t\t\t\tdate varchar(255) NOT NULL,\r\n\t\t\t\tlink varchar(255) NOT NULL,\r\n\t\t\t\tenabled boolean NOT NULL,\r\n\t\t\t\tUNIQUE KEY id (id)\r\n\t\t\t);";
     }
     if ($wpdb->get_var("show tables like '{$link}'") != $link) {
         $q .= "CREATE TABLE " . $link . "( \r\n\t\t\t\tid int NOT NULL AUTO_INCREMENT,\r\n\t\t\t\ttype_id tinyint(4) NOT NULL,\r\n\t\t\t\tname varchar(255) NOT NULL,\r\n\t\t\t\tUNIQUE KEY id (id)\r\n\t\t\t);";
     }
     if ($q != '') {
         dbDelta($q);
     }
     $types = array(array('type_id' => '3', 'name' => 'Social Network'), array('type_id' => '2', 'name' => 'Website/Blog'), array('type_id' => '1', 'name' => 'Images'));
     foreach ($types as $type) {
         if (!$wpdb->get_var("SELECT type_id FROM {$link} WHERE type_id = " . $type['type_id'])) {
             $i = "INSERT INTO " . $link . " (id,type_id,name) VALUES('', '" . $type['type_id'] . "','" . $type['name'] . "')";
             $query = $wpdb->query($i);
         }
     }
     wp_add_option('wik_db_version', $db_version);
 }
function save($data, $array)
{
    check_admin_referer('wicketpixie-settings');
    foreach ($array as $value) {
        if (!wp_add_option($value['id'], $_POST[$value['id']])) {
            wp_update_option($value['id'], $_POST[$value['id']]);
        }
    }
    foreach ($array as $value) {
        if ($value['type'] == 'checkbox') {
            if (isset($_POST[$value['id']])) {
                if (!wp_add_option($value['id'], '1')) {
                    wp_update_option($value['id'], '1');
                }
            } else {
                if (!wp_add_option($value['id'], '0')) {
                    wp_update_option($value['id'], '0');
                }
            }
        } elseif ($value['type'] != 'checkbox') {
            if (!wp_add_option($value['id'], $_POST[$value['id']])) {
                wp_update_option($value['id'], $_POST[$value['id']]);
            }
        } else {
            if (!wp_add_option($value['id'], $_POST[$value['id']])) {
                wp_update_option($value['id'], $_POST[$value['id']]);
            }
        }
    }
    wp_redirect($_SERVER['PHP_SELF'] . "?page=" . basename(__FILE__) . "&saved=true");
    die;
}
 /**
  * Just calling WP's method to add a new menu to the design section.
  */
 function addMenu()
 {
     global $homeoptions;
     if ('save' == $_POST['action']) {
         foreach ($homeoptions as $value) {
             if (!wp_add_option($value['id'], $_POST[$value['id']])) {
                 wp_update_option($value['id'], $_POST[$value['id']]);
             }
         }
         foreach ($homeoptions as $value) {
             if ($value['type'] == 'checkbox') {
                 if (isset($_POST[$value['id']])) {
                     if (!wp_add_option($value['id'], '1')) {
                         wp_update_option($value['id'], '1');
                     }
                 } else {
                     if (!wp_add_option($value['id'], '0')) {
                         wp_update_option($value['id'], '0');
                     }
                 }
             } elseif ($value['type'] != 'checkbox') {
                 if (!wp_add_option($value['id'], $_POST[$value['id']])) {
                     wp_update_option($value['id'], $_POST[$value['id']]);
                 }
             } else {
                 if (!wp_add_option($value['id'], $_POST[$value['id']])) {
                     wp_update_option($value['id'], $_POST[$value['id']]);
                 }
             }
         }
         wp_redirect($_SERVER['PHP_SELF'] . "?page=homeeditor.php&saved=true");
         die;
     }
     add_submenu_page('wicketpixie-admin.php', __('WicketPixie Home Editor'), __('Home Editor'), 9, basename(__FILE__), array('HomeAdmin', 'homeMenu'));
 }
 function pub_id($_REQUEST)
 {
     $args = $_REQUEST;
     if (wp_get_option('adsense_pubid')) {
         wp_update_option('adsense_pubid', $args['pubid']);
     } else {
         wp_add_option('adsense_pubid', $args['pubid']);
     }
 }
function wicketpixie_add_admin()
{
    global $themename, $options;
    if (isset($_GET['page']) && $_GET['page'] == basename(__FILE__)) {
        if ('save' == $_POST['action']) {
            check_admin_referer('wicketpixie-settings');
            foreach ($options as $value) {
                if (!wp_add_option($value['id'], $_POST[$value['id']])) {
                    wp_update_option($value['id'], $_POST[$value['id']]);
                }
            }
            foreach ($options as $value) {
                if (isset($_POST[$value['id']])) {
                    if (!wp_add_option($value['id'], $_POST[$value['id']])) {
                        wp_update_option($value['id'], $_POST[$value['id']]);
                    }
                } else {
                    if (wp_get_option($value['id'])) {
                        wp_delete_option($value['id']);
                    }
                }
            }
            if ($_POST['no_image']) {
                if (!wp_add_option('body_bg_image', '0')) {
                    wp_update_option('body_bg_image', '0');
                }
            }
            if ($_POST['completed'] == 1 && $_FILES['body_bg_image']['tmp_name'] != '') {
                $new_name = $_FILES['body_bg_image']['name'];
                $new_home = TEMPLATEPATH . '/images/backgrounds/' . $new_name;
                if (move_uploaded_file($_FILES['body_bg_image']['tmp_name'], $new_home)) {
                    if (!wp_add_option('body_bg_image', $new_name)) {
                        wp_update_option('body_bg_image', $new_name);
                    }
                } else {
                    error_log('No joy, no uploaded file');
                }
            }
            if ($_POST['saved_images'] != '') {
                if (!wp_add_option('body_bg_image', $_POST['saved_images'])) {
                    wp_update_option('body_bg_image', $_POST['saved_images']);
                }
            }
            wp_redirect($_SERVER['PHP_SELF'] . "?page=functions.php&saved=true");
            die;
        } elseif ('reset' == $_POST['action']) {
            check_admin_referer('wicketpixie-settings');
            foreach ($options as $value) {
                if (wp_get_option($value['id'])) {
                    wp_delete_option($value['id']);
                }
            }
            wp_redirect($_SERVER['PHP_SELF'] . "?page=functions.php&saved=true");
            die;
        }
    }
    /*
        add_theme_page($themename." Options", "WicketPixie Options", 'edit_themes', basename(__FILE__), 'wicketpixie_admin');
    */
    add_submenu_page('wicketpixie-admin.php', 'WicketPixie Theme Options', 'Theme Options', 'edit_themes', basename(__FILE__), 'wicketpixie_admin');
}
function wp_plugins_toplevel_admin()
{
    global $plugins;
    if ('save_plugins' == $_POST['action']) {
        check_admin_referer('wicketpixie-settings');
        //Special considerations for the Google 404
        $aa404 = false;
        foreach ($plugins as $value) {
            if (!empty($_POST[$value['id']])) {
                if (strpos($_POST[$value['id']], "aagoog404") !== false) {
                    $aa404 = true;
                }
            }
            if (!wp_add_option($value['id'], $_POST[$value['id']])) {
                wp_update_option($value['id'], $_POST[$value['id']]);
            }
        }
        if ($aa404) {
            if (!class_exists('AskApacheGoogle404')) {
                include TEMPLATEPATH . "/plugins/askapache-google-404/askapache-google-404.php";
            }
            $tmp = new AskApacheGoogle404();
            $tmp->activate();
        } else {
            if (!class_exists('AskApacheGoogle404')) {
                include TEMPLATEPATH . "/plugins/askapache-google-404/askapache-google-404.php";
            }
            $tmp = new AskApacheGoogle404();
            $tmp->deactivate();
        }
        foreach ($plugins as $value) {
            if ($value['type'] == 'checkbox') {
                if (isset($_POST[$value['id']])) {
                    if (!wp_add_option($value['id'], '1')) {
                        wp_update_option($value['id'], '1');
                    }
                } else {
                    if (!wp_add_option($value['id'], '0')) {
                        wp_update_option($value['id'], '0');
                    }
                }
            } elseif ($value['type'] != 'checkbox') {
                if (!wp_add_option($value['id'], $_POST[$value['id']])) {
                    wp_update_option($value['id'], $_POST[$value['id']]);
                }
            } else {
                if (!wp_add_option($value['id'], $_POST[$value['id']])) {
                    wp_update_option($value['id'], $_POST[$value['id']]);
                }
            }
        }
        wp_redirect($_SERVER['PHP_SELF'] . "?page=wp_plugins.php&saved=true");
        die;
    }
    add_menu_page('WiPi Plugins', 'WiPi Plugins', 'edit_themes', 'wp_plugins.php', 'wp_plugins_admin_index', get_template_directory_uri() . '/images/wicketsmall.png');
}