Example #1
0
 /**
  * Adds the important tables to the wordpress database
  * @global wpdb $wpdb
  * @global CTXPSC_Tables $ctxpsdb
  */
 public static function plugin_install()
 {
     global $wpdb, $ctxpsdb;
     self::check_php_version();
     $linkBack = admin_url();
     //Build our SQL scripts to create the new db tables
     $sql_create_groups = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n            `ID` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,\n            `group_title` varchar(40) NOT NULL COMMENT 'The name of the group',\n            `group_description` text COMMENT 'A description of or notes about the group',\n            `group_creator` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'The id of the user who created the group',\n            `group_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT 'The datetime the group was created',\n            `group_system_id` varchar(5) UNIQUE NULL COMMENT 'A unique system id for system groups',\n            `group_site_access` varchar(20) DEFAULT 'none' COMMENT 'If site security is enabled, this dictates how much access this group has. Values: none,limited,full',\n            PRIMARY KEY (`ID`)\n        )", $ctxpsdb->groups);
     $sql_create_group_relationships = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n            `ID` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,\n            `grel_group_id` bigint(20) UNSIGNED NOT NULL COMMENT 'The group id that the user is attached to',\n            `grel_user_id` bigint(20) UNSIGNED NOT NULL COMMENT 'The user id to attach to the group',\n            `grel_expires` datetime COMMENT 'If set, user cannot access content after this date',\n            PRIMARY KEY (`ID`)\n        )", $ctxpsdb->group_rels);
     $sql_create_security = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n            `ID` bigint(20) UNSIGNED NOT NULL auto_increment,\n            `sec_protect_type` varchar(10) NOT NULL DEFAULT 'post' COMMENT 'What type of item is being protected? (post, term, media, archive, etc)',\n            `sec_protect_id` bigint(20) unsigned NOT NULL COMMENT 'The id of the item (post, page, etc)',\n            `sec_access_type` varchar(10) NOT NULL DEFAULT 'group' COMMENT 'Specifies whether this security entry pertains to a user, group, or role.',\n            `sec_access_id` bigint(20) NOT NULL COMMENT 'The id of the user, group, or role this pertains to.',\n            `sec_setting` varchar(10) NOT NULL DEFAULT 'allow' COMMENT 'Set to either allow or restrict',\n            `sec_cascades` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'If true, these settings inherit down through the pages ancestors. If false (default), settings affect this page only.',\n            PRIMARY KEY (`ID`)\n        )", $ctxpsdb->security);
     $sql_create_term_meta = sprintf('CREATE TABLE IF NOT EXISTS `%stermmeta` (
     `meta_id` bigint(20) UNSIGNED NOT NULL auto_increment,
     `term_id` bigint(20) UNSIGNED NOT NULL,
     `meta_key` varchar(255),
     `meta_value` longtext,
     PRIMARY KEY (`meta_id`)
 )', $wpdb->prefix);
     //deactivate_plugins($ctxpsdb->pluginbase);
     //wp_die('<pre>'.print_r($ctxpsdb,true).'</pre>');
     //wp_die($ctxpsdb->security);
     //Create the tables
     $wpdb->show_errors();
     $wpdb->query($sql_create_groups);
     $wpdb->query($sql_create_group_relationships);
     $wpdb->query($sql_create_security);
     $wpdb->query($sql_create_term_meta);
     //Record what version of the db we're using (only works if option not already set - handy for ensuring upgrade path works as planned)
     add_option("contexture_ps_db_version", "1.5");
     //Set plugin options (not db version)
     CTXPS_Queries::set_options();
     /********* START UPGRADE PATH < 1.1 ***********/
     $dbver = get_option("contexture_ps_db_version");
     if ($dbver == "" || (double) $dbver < 1.1) {
         $wpdb->query("ALTER TABLE `" . $ctxpsdb->groups . "` ADD COLUMN `group_system_id` varchar(5) UNIQUE NULL COMMENT 'A unique system id for system groups' AFTER `group_date`");
         update_option("contexture_ps_db_version", "1.1");
     }
     /******** END UPGRADE PATH < 1.1 **************/
     /********* START UPGRADE PATH < 1.2 ***********/
     $dbver = get_option("contexture_ps_db_version");
     if ($dbver == "" || (double) $dbver < 1.2) {
         $wpdb->query("ALTER TABLE `" . $ctxpsdb->group_rels . "` ADD COLUMN `grel_expires` datetime COMMENT 'If set, user cannot access content after this date' AFTER `grel_user_id`");
         update_option("contexture_ps_db_version", "1.2");
     }
     /******** END UPGRADE PATH < 1.2 **************/
     /********* START UPGRADE PATH < 1.3 ***********/
     //Skip 1.3 - DB versions will now match major PSC releases
     /******** END UPGRADE PATH < 1.3 **************/
     /********* START UPGRADE PATH < 1.4 ***********/
     $dbver = get_option("contexture_ps_db_version");
     if ($dbver == "" || (double) $dbver < 1.4) {
         $wpdb->query("ALTER TABLE `" . $ctxpsdb->groups . "` ADD COLUMN `group_site_access` varchar(20) DEFAULT 'none' COMMENT 'If site security is enabled, this dictates how much access this group has. Values: none,limited,full'");
         update_option("contexture_ps_db_version", "1.4");
     }
     /******** END UPGRADE PATH < 1.4 **************/
     /********* START UPGRADE PATH < 1.5 ***********/
     //termmeta table added. No other changes necessary.
     $dbver = get_option("contexture_ps_db_version");
     if ($dbver == "" || (double) $dbver < 1.5) {
         //Default for posts/pages is now 'post' to correctly match WP conventions
         $wpdb->query("ALTER TABLE `" . $ctxpsdb->security . "` ALTER COLUMN `sec_protect_type` SET DEFAULT 'post'");
         $wpdb->query("UPDATE `" . $ctxpsdb->security . "` SET `sec_protect_type`='post' WHERE `sec_protect_type`='page'");
         update_option("contexture_ps_db_version", "1.5");
     }
     /******** END UPGRADE PATH < 1.5 **************/
     /********* START UPGRADE PATH < 1.6 ***********/
     //No changes to 1.6. JS updates only.
     /******** END UPGRADE PATH < 1.6 **************/
     //Check if our "Registered Users" group already exists
     $CntRegSmrtGrp = (bool) $wpdb->get_var("SELECT COUNT(*) FROM `" . $ctxpsdb->groups . "` WHERE `group_system_id` = 'CPS01' LIMIT 1");
     if (!$CntRegSmrtGrp) {
         //Adds the Registered Users system group (if it doesnt exist)
         $wpdb->insert($ctxpsdb->groups, array('group_title' => __('Registered Users', 'contexture-page-security'), 'group_description' => __('This group automatically applies to all authenticated users.', 'contexture-page-security'), 'group_creator' => '0', 'group_system_id' => 'CPS01'));
     }
 }
        } else {
            //Checkbox is not set, so we're not using pages
            $newopts['ad_msg_usepages'] = 'false';
        }
        //Update filtering options
        $newopts['force-public-pages'] = $_POST['force-public-pages'];
        $newopts['ad_msg_usefilter_menus'] = isset($_POST['filter-menus']) ? 'true' : 'false';
        $newopts['ad_msg_usefilter_rss'] = isset($_POST['filter-rss']) ? 'true' : 'false';
        //Set option for AD replacement
        $newopts['ad_opt_page_replace'] = $_POST['ad-page-replace'] === 'replace' ? 'true' : 'false';
        //Set option for sitewide lockdown
        $newopts['ad_opt_protect_site'] = isset($_POST['ad-protect-site']) ? 'true' : 'false';
        //Set option for redirecting anonymous users to login if accessing restricted content
        $newopts['ad_opt_login_anon'] = isset($_POST['ad-msg-forcelogin']) ? 'true' : 'false';
        //Update the options array
        $saveStatus = CTXPS_Queries::set_options($newopts);
        //If save was successful, show the message
        if (isset($saveStatus)) {
            $updatesettingsMessage = '<div id="message" class="updated below-h2 fade"><p><strong>' . __('Page Security settings saved.', 'contexture-page-security') . '</strong></p></div>';
        }
    }
}
//Get AD messages from options
$ADMsg = get_option('contexture_ps_options');
$ProtPages = CTXPS_Queries::get_protected_posts();
//wp_die($ProtPages);
//Generate ddls with page heirarchy
$pageDDLAuth = wp_dropdown_pages(array('name' => 'ad-page-auth', 'show_option_none' => __('-- Choose Access Denied Page --', 'contexture-page-security'), 'show_option_none_value' => 0, 'selected' => $ADMsg['ad_page_auth_id'], 'echo' => 0, 'exclude' => $ProtPages));
$pageDDLAnon = wp_dropdown_pages(array('name' => 'ad-page-anon', 'show_option_none' => __('-- Choose Access Denied Page --', 'contexture-page-security'), 'show_option_none_value' => 0, 'selected' => $ADMsg['ad_page_anon_id'], 'echo' => 0, 'exclude' => $ProtPages));
//If there aren't any pages that can be used for AD, replace with this helpful message
if (empty($pageDDLAuth)) {