prefix . 'collaboration'; global $pce_db_collab; // Name of the database table that will hold group - collaborator associations $pce_db_collab = $wpdb->prefix . 'collabwriters'; global $pce_db_cats; // Name of the database table that will hold category-specific moderators // This table is no longer used, but defined here for upgrading and uninstalling purposes $pce_db_cats = $wpdb->prefix . 'collabcats'; global $pce_db_collabrules; // Name of the database table that will hold post-type-specific moderators $pce_db_collabrules = $wpdb->prefix . 'collabrules'; // ------------------------------------------- // You should not have to edit below this line // ------------------------------------------- global $pce_version; $pce_version = '1.6.2'; // Enable translations add_action('init', 'pce_textdomain'); function pce_textdomain() { load_plugin_textdomain('peters_collaboration_emails', PLUGINDIR . '/' . dirname(plugin_basename(__FILE__)), dirname(plugin_basename(__FILE__))); } // Call jQuery function pce_js_admin_header() { wp_enqueue_script( 'jquery' ); } add_action( 'admin_print_scripts', 'pce_js_admin_header' ); function pce_pending($pce_newstatus, $pce_oldstatus, $pce_object) { global $wpdb, $pce_db_group, $pce_db_collab, $pce_siteurl, $pce_blogname, $pce_fromname, $pce_fromaddress, $pce_whoapproved, $pce_emails_to_send, $user_identity, $user_email, $_POST; // The person who wrote the post $pce_thisuser = get_userdata($pce_object->post_author); // Get information about the currently logged in user, as the person submitting the post for review or approving it // Their name is mapped to $user_identity and their e-mail address is mapped to $user_email get_currentuserinfo(); // If specified in the settings, assign the current user values as the e-mail sender information if (!$pce_fromname) $pce_fromname = $user_identity; if (!$pce_fromaddress) $pce_fromaddress = $user_email; // Line break, which we will use many times in constructing e-mails $pce_eol = "\r\n"; // If a note was submitted, we will use it in the e-mails if (isset($_POST['ppn_post_note']) && $_POST['ppn_post_note'] != '') { $pce_post_note = stripslashes( $_POST['ppn_post_note'] ); } // Make sure the mail client knows it's a UTF-8 e-mail $pce_headers = 'Content-Type: text/plain; charset=utf-8' . $pce_eol; // E-mail moderator(s) for pending posts if( $pce_emails_to_send['pending'] && 'pending' == $pce_newstatus && 'pending' != $pce_oldstatus ) { $pce_moderators_unserialized = array(); // Get the moderator information based on the collaboration rules $pce_collabgroups = $wpdb->get_results('SELECT groupid FROM ' . $pce_db_collab . ' WHERE writerid = ' . $pce_object->post_author, ARRAY_N); // If they are part of groups, get the moderator info for each group if ($pce_collabgroups) { foreach ($pce_collabgroups as $pce_collabgroup) { $pce_moderators = $wpdb->get_var('SELECT moderators FROM ' . $pce_db_group . ' WHERE collabgroup = ' . $pce_collabgroup[0]); $pce_moderators_unserialized = array_merge(unserialize($pce_moderators), $pce_moderators_unserialized); } } // Get post type rules $pce_moderators = pce_get_post_type_moderators( $pce_object ); if( count( $pce_moderators ) ) { foreach( $pce_moderators as $pce_moderator ) { $pce_moderators_unserialized = array_merge( unserialize( $pce_moderator ), $pce_moderators_unserialized ); } } // Remove duplicate entries for groups and categories $pce_moderators_unserialized = array_unique($pce_moderators_unserialized); // Get the default moderator information if (count($pce_moderators_unserialized) == 0) { $pce_moderators = $wpdb->get_var('SELECT moderators FROM ' . $pce_db_group . ' WHERE collabgroup = 1'); $pce_moderators_unserialized = unserialize($pce_moderators); } $pce_moderators_emails = array(); foreach ($pce_moderators_unserialized as $pce_moderator_unserialized) { if (is_numeric($pce_moderator_unserialized)) { $pce_moderator_data = get_userdata($pce_moderator_unserialized); $pce_moderators_emails[] = $pce_moderator_data->user_email; } elseif($pce_moderator_unserialized == 'admin') { $pce_moderators_emails[] = get_option('admin_email'); } // must be an e-mail address else { $pce_moderators_emails[] = $pce_moderator_unserialized; } } // Remove duplicate entries after converting to e-mail addresses $pce_moderators_emails = array_unique($pce_moderators_emails); $pce_moderator = implode (', ', $pce_moderators_emails); // Header stuff for a pending post // Header stuff from http://ca.php.net/mail $pce_headers .= 'From:' . $pce_fromname . ' <' . $pce_fromaddress . '>'. $pce_eol; $pce_headers .= 'Reply-To:' . $pce_fromname . ' <'. $pce_fromaddress . '>' . $pce_eol; $pce_headers .= 'Return-Path:' . $pce_fromname . ' <'. $pce_fromaddress . '>' . $pce_eol; // Body of the e-mail for a pending post $pce_body = sprintf(__('There is a new post to review, written by %s.', 'peters_collaboration_emails'), $pce_fromname) . $pce_eol . $pce_eol; // Insert note if applicable if(isset($pce_post_note)) { $pce_body .= sprintf(__('Accompanying note from %s:', 'peters_collaboration_emails'), $pce_fromname) . $pce_eol; $pce_body .= $pce_post_note . $pce_eol . $pce_eol; } $pce_body .= __('Review and publish it here: ', 'peters_collaboration_emails') . $pce_siteurl . '/wp-admin/post.php?action=edit&post=' . $pce_object->ID; // E-mail subject for a pending post $pce_subject = '[' . $pce_blogname . '] "' . $pce_object->post_title . '" ' . __('pending', 'peters_collaboration_emails'); // Send the notification e-mail for a pending post wp_mail($pce_moderator, $pce_subject, $pce_body, $pce_headers); } // E-mail the post author when a post is approved elseif( $pce_emails_to_send['approved'] && 'pending' == $pce_oldstatus && 'publish' == $pce_newstatus ) { // Header stuff for an approved post // Header stuff from http://ca.php.net/mail $pce_headers .= 'From: ' . $pce_fromname . ' <' . $pce_fromaddress . '>' . $pce_eol; $pce_headers .= 'Reply-To: ' . $pce_fromname . ' <' . $pce_fromaddress . '>' . $pce_eol; $pce_headers .= 'Return-Path: ' . $pce_fromname. ' <' . $pce_fromaddress .'>' . $pce_eol; // E-mail body for an approved post $pce_body = sprintf( __( 'Hi %s!', 'peters_collaboration_emails' ), $pce_thisuser->display_name ) . $pce_eol . $pce_eol; if( $pce_whoapproved ) { $pce_body .= sprintf( __( 'Your post has been approved by %s and is now published', 'peters_collaboration_emails' ), $pce_fromname ); } else { $pce_body .= __( 'Your post has been approved', 'peters_collaboration_emails' ); } $pce_body .= $pce_eol . $pce_eol; // Insert note if applicable if(isset($pce_post_note)) { $pce_body .= __('Accompanying note:', 'peters_collaboration_emails') . $pce_eol; $pce_body .= $pce_post_note . $pce_eol . $pce_eol; } $pce_body .= __('See it here:', 'peters_collaboration_emails') . ' ' . get_permalink($pce_object->ID); // E-mail subject for an approved post $pce_subject = '[' . $pce_blogname . '] "' . $pce_object->post_title . '" ' . __('published', 'peters_collaboration_emails'); // Send the notification e-mail for an approved post wp_mail($pce_thisuser->user_email, $pce_subject, $pce_body, $pce_headers); } // E-mail the post author when a post is scheduled to be published elseif( $pce_emails_to_send['future'] && 'pending' == $pce_oldstatus && 'future' == $pce_newstatus ) { // Header stuff for an approved post // Header stuff from http://ca.php.net/mail $pce_headers .= 'From: ' . $pce_fromname . ' <' . $pce_fromaddress . '>' . $pce_eol; $pce_headers .= 'Reply-To: ' . $pce_fromname . ' <' . $pce_fromaddress . '>' . $pce_eol; $pce_headers .= 'Return-Path: ' . $pce_fromname. ' <' . $pce_fromaddress .'>' . $pce_eol; // E-mail body for a scheduled post $pce_body = sprintf(__('Hi %s!', 'peters_collaboration_emails'), $pce_thisuser->display_name) . $pce_eol . $pce_eol; if( $pce_whoapproved ) { $pce_body .= sprintf( __( 'Your post has been approved by %s and is scheduled to be published on %s UTC %s', 'peters_collaboration_emails' ), $pce_fromname, $pce_object->post_date, get_option( 'gmt_offset' ) ); } else { $pce_body .= __( 'Your post has been approved', 'peters_collaboration_emails' ); } $pce_body .= $pce_eol . $pce_eol; // Insert note if applicable if(isset($pce_post_note)) { $pce_body .= __('Accompanying note:', 'peters_collaboration_emails') . $pce_eol; $pce_body .= $pce_post_note . $pce_eol . $pce_eol; } // E-mail subject for an approved post $pce_subject = '[' . $pce_blogname . '] "' . $pce_object->post_title . '" ' . __('approved and scheduled', 'peters_collaboration_emails'); // Send the notification e-mail for an approved post wp_mail($pce_thisuser->user_email, $pce_subject, $pce_body, $pce_headers); } // E-mail the post author if their post is back to draft status elseif( $pce_emails_to_send['backtodraft'] && 'pending' == $pce_oldstatus && 'draft' == $pce_newstatus ) { // E-mail the post author to let them know that their post has been published // Header stuff for a "back to draft" post // Header stuff from http://ca.php.net/mail $pce_headers .= 'From: ' . $pce_fromname . ' <' . $pce_fromaddress . '>' . $pce_eol; $pce_headers .= 'Reply-To: ' . $pce_fromname . ' <' . $pce_fromaddress . '>' . $pce_eol; $pce_headers .= 'Return-Path: ' . $pce_fromname. ' <' . $pce_fromaddress .'>' . $pce_eol; // E-mail body for a "back to draft" post $pce_body = sprintf(__('Hi %s!', 'peters_collaboration_emails'), $pce_thisuser->display_name) . $pce_eol . $pce_eol; if( $pce_whoapproved ) { $pce_body .= sprintf( __( 'Your post has been reverted back to draft status by %s.', 'peters_collaboration_emails' ), $pce_fromname ); } else { $pce_body .= __( 'Your post has been reverted back to draft status.', 'peters_collaboration_emails' ); } $pce_body .= $pce_eol . $pce_eol; if(isset($pce_post_note)) { $pce_body .= __('Accompanying note:', 'peters_collaboration_emails') . $pce_eol; $pce_body .= $pce_post_note . $pce_eol . $pce_eol; } $pce_body .= __('Edit it again here:', 'peters_collaboration_emails') . ' ' . $pce_siteurl . '/wp-admin/post.php?action=edit&post=' . $pce_object->ID; // E-mail subject for a "back to draft" post $pce_subject = '[' . $pce_blogname . '] "' . $pce_object->post_title . '" ' . __('back to draft', 'peters_collaboration_emails'); // Send the notification e-mail for a "back to draft" post wp_mail($pce_thisuser->user_email, $pce_subject, $pce_body, $pce_headers); } // E-mail author when his/her scheduled post is published elseif( $pce_emails_to_send['wentlive'] && 'future' == $pce_oldstatus && 'publish' == $pce_newstatus ) { $pce_fromaddress = get_option('admin_email'); // Header stuff for a pending post // Header stuff from http://ca.php.net/mail $pce_headers .= 'From: ' . $pce_blogname . ' <' . $pce_fromaddress . '>'. $pce_eol; $pce_headers .= 'Reply-To: ' . $pce_blogname . ' <'. $pce_fromaddress . '>' . $pce_eol; $pce_headers .= 'Return-Path: ' . $pce_blogname . ' <'. $pce_fromaddress . '>' . $pce_eol; // Body of the e-mail for a previously-scheduled, now published post $pce_body = sprintf(__('Hi %s!', 'peters_collaboration_emails'), $pce_thisuser->display_name) . $pce_eol . $pce_eol; $pce_body .= __('Your post is now live.', 'peters_collaboration_emails') . $pce_eol . $pce_eol; $pce_body .= __('See it here:', 'peters_collaboration_emails') . ' ' . get_permalink($pce_object->ID); // E-mail subject for a previously-scheduled, now published post $pce_subject = '[' . $pce_blogname . '] "' . $pce_object->post_title . '" ' . __('is now live', 'peters_collaboration_emails'); // Send the notification e-mail for a previously-scheduled, now published post wp_mail($pce_thisuser->user_email, $pce_subject, $pce_body, $pce_headers); } } function pce_get_post_type_moderators( $post_object ) { global $wpdb, $pce_db_collabrules; $post_type_moderators = array(); // Get this post's taxonomies // First, find out its post type // If post type is a revision, fetch the post's parent if( 'revision' == $post_object->post_type ) { $post_object = get_post( $post_object->post_parent ); } // Get moderators for $post_object->post_type $post_type_rules = $wpdb->get_results( 'SELECT taxonomy, term, moderators FROM ' . $pce_db_collabrules . ' WHERE post_type = \'' . $post_object->post_type . '\'', OBJECT ); if( $post_type_rules ) { // Prepare the list of moderator rules for this post type in an easily accessible PHP variable $post_type_moderator_rules = array(); foreach( $post_type_rules as $post_type_rule ) { $term = strtolower( $post_type_rule->term ); if( isset( $post_type_moderator_rules[$post_type_rule->taxonomy] ) ) { $post_type_moderator_rules[$post_type_rule->taxonomy][$term] = $post_type_rule->moderators; // Store the term as its name as well // This is because they're submitted as their name or ID (in the case of categories) // We're also storing them as their ID if they exist if( is_numeric( $post_type_rule->term ) ) { $term_object = get_term( $post_type_rule->term, $post_type_rule->taxonomy ); if( $term_object ) { $term = strtolower( $term_object->name ); $post_type_moderator_rules[$post_type_rule->taxonomy][$term] = $post_type_rule->moderators; } } } else { $post_type_moderator_rules[$post_type_rule->taxonomy] = array( $term => $post_type_rule->moderators ); // Same as above regarding term name and ID if( is_numeric( $post_type_rule->term ) ) { $term_object = get_term( $post_type_rule->term, $post_type_rule->taxonomy ); if( $term_object ) { $term = strtolower( $term_object->name ); $post_type_moderator_rules[$post_type_rule->taxonomy][$term] = $post_type_rule->moderators; } } } } // Loop through the categories if( isset( $_POST['post_category'] ) ) { $post_categories = $_POST['post_category']; if( 0 == count( $post_categories ) || ! is_array( $post_categories) ) { $post_categories = array( get_option( 'default_category' ) ); } foreach( $post_categories as $post_category ) { if( isset( $post_type_moderator_rules['category'][$post_category] ) ) { $post_type_moderators[] = $post_type_moderator_rules['category'][$post_category]; } } } // Loop through all taxonomies if( isset( $_POST['tax_input'] ) ) { $post_taxonomies = $_POST['tax_input']; foreach( $post_taxonomies as $taxonomy => $term_list ) { // In editor mode, terms are either in an array checkbox (like categories) or freeform listed, comma-separated (like tags) if( is_array( $term_list ) ) { $terms = $term_list; } else { $terms = explode( ',', $term_list ); } if( $terms ) { foreach( $terms as $term ) { $term = strtolower( trim( $term ) ); if( isset( $post_type_moderator_rules[$taxonomy][$term] ) ) { $post_type_moderators[] = $post_type_moderator_rules[$taxonomy][$term]; } } } } } // Add moderators for "all" taxonomies if( isset( $post_type_moderator_rules['all'] ) ) { $post_type_moderators[] = $post_type_moderator_rules['all']['']; } } return $post_type_moderators; } add_filter('transition_post_status', 'pce_pending','',3); if( is_admin() ) { // This line makes sure that all of this code below only runs if someone is in the WordPress back-end // This generates an option of checkbox output for contributors or editors and administrators in the system, as well as an "admin" and "other" choice function pce_usersoptions($pce_existingmoderators = array(), $pce_contributors_or_moderators, $pce_optionsoutput = true, $pce_numbered = 0) { global $wpdb, $pce_contributor_roles, $pce_moderator_roles, $pce_moderatorcache; $pce_usersoptions = ''; // Build SQL query portion to filter contributors or approvers $pce_contrib_approve_code = ''; switch ($pce_contributors_or_moderators) { case 'contributors': $pce_filter_roles = $pce_contributor_roles; break; case 'moderators': default: $pce_filter_roles = $pce_moderator_roles; break; } $delimiter = ''; foreach( $pce_filter_roles as $pce_filter_role ) { $pce_contrib_approve_code .= $delimiter; $pce_contrib_approve_code .= "'%" . $pce_filter_role . "%'"; $delimiter = ' OR ' . $wpdb->usermeta . '.meta_value LIKE '; } if (isset($pce_userresultscache) && $pce_contributors_or_moderators != 'contributors') { $pce_userresults = $pce_moderatorcache; } else { $pce_userresults = $wpdb->get_results("SELECT ID, $wpdb->users.display_name, $wpdb->users.user_email FROM $wpdb->users, $wpdb->usermeta WHERE $wpdb->users.ID = $wpdb->usermeta.user_id AND $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND ($wpdb->usermeta.meta_value LIKE " . $pce_contrib_approve_code . ") ORDER BY $wpdb->users.display_name", ARRAY_N); } if ($pce_userresults) { $i = $pce_numbered; foreach ($pce_userresults as $pce_userresult) { if (isset($pce_existingmoderators[$pce_userresult[0]])) { continue; } if ($pce_optionsoutput) { $pce_usersoptions .= "\n" . ' '; } else { $pce_usersoptions .= "\n" . '

' . $pce_userresult[1] . '

'; } ++$i; } } if ($pce_contributors_or_moderators == 'moderators' && $pce_optionsoutput) { $pce_moderatorcache = $pce_userresults; if (!isset($pce_existingmoderators['admin'])) { $pce_usersoptions .= "\n" . ' '; } $pce_usersoptions .= "\n" . ' '; } return $pce_usersoptions; } // All sorts of validation on moderators, returning either an error or an array of moderators function pce_mod_array($pce_mods, $pce_add, $pce_other_field) { $pce_return_mods = array(); $i = 0; foreach ($pce_mods as $pce_mod) { // Check that it is a valid user if (is_numeric($pce_mod)) { $pce_validuser = get_userdata($pce_mod); if (!$pce_validuser) { return __('**** ERROR: Invalid moderator user ID ****', 'peters_collaboration_emails'); } $pce_return_mods[$i] = intval($pce_mod); } // If it's a checkbox, we need the value of the dropdown list elseif ($pce_mod == 'on') { // If the dropdown equals "other" then look for content in the "other" field, which had better be an e-mail address if ($pce_add == 'other' && is_email($pce_other_field)) { $pce_return_mods[$i] = $pce_other_field; } elseif (is_numeric($pce_add)) { $pce_validuser = get_userdata($pce_add); if (!$pce_validuser) { return __('**** ERROR: Invalid moderator user ID ****', 'peters_collaboration_emails'); } $pce_return_mods[$i] = intval($pce_add); } elseif ($pce_add == 'admin') { $pce_return_mods[$i] = $pce_add; } else { return __('**** ERROR: Invalid moderator e-mail address submitted ****', 'peters_collaboration_emails'); } } // Must be an e-mail address or admin elseif (is_email($pce_mod) || $pce_mod == 'admin') { $pce_return_mods[$i] = $pce_mod; } else { return __('**** ERROR: Invalid e-mail address submitted ****', 'peters_collaboration_emails'); } ++$i; } return $pce_return_mods; } // Processes changes to the moderator rules (who approves whose posts) function pce_modsubmit() { global $wpdb, $pce_db_group; // ---------------------------------- // Process the default mod changes // ---------------------------------- $pce_defaultmods = $_POST['pce_defaultmod']; // An array of default moderators (contains User IDs, "admin" or strictly e-mail addresses) $pce_defaultmods_update = array(); if ($pce_defaultmods) { $pce_defaultmods_update = pce_mod_array($pce_defaultmods, $_POST['adddefaultmod'], $_POST['pce_defaultmodadd']); // Nicely scrubbed array of mods to serialize if (is_array($pce_defaultmods_update)) { $pce_defaultmod_serialized = serialize($pce_defaultmods_update); } // It return an error else { return array( 'error', $pce_process_close ); } $pce_defaultmodsuccess = $wpdb->query('UPDATE ' . $pce_db_group . ' SET moderators = \'' . $pce_defaultmod_serialized . '\' WHERE collabgroup = 1'); if ($pce_defaultmodsuccess) { return array( 'success', __( 'Default moderators updated.', 'peters_collaboration_emails' ) ); } } else { return array( 'error', __('You must have at least one default mod.', 'peters_collaboration_emails') ); } // We've made it this far, so nothing to report return false; } function pce_rulesubmit() { global $wpdb, $pce_db_group; // ---------------------------------- // Process the rule changes // ---------------------------------- $updated = false; $pce_usermods = $_POST['pce_usermod']; // An array of moderators for each group (contains User IDs, "admin" or strictly e-mail addresses) $pce_groupids = $_POST['pce_groupid']; // An array of group IDs whose moderators need to be updated $pce_num_submits = array_keys($pce_groupids); if ($pce_num_submits) { foreach($pce_num_submits as $pce_num_submit) { $pce_usermods_update = array(); $pce_usermod = $pce_usermods[$pce_num_submit]; $pce_groupid = intval($pce_groupids[$pce_num_submit]); // Does this group exist? $pce_groupname = $wpdb->get_var('SELECT groupname FROM ' . $pce_db_group . ' WHERE collabgroup = ' . $pce_groupid); if (!$pce_groupname) { return array( 'error', sprintf(__('**** ERROR: Group with ID of %d does not exist ****', 'peters_collaboration_emails'), $pce_groupid ) ); } if ($pce_usermod) { $pce_usermod_update = pce_mod_array($pce_usermod, $_POST['addusermod'][$pce_num_submit], $_POST['pce_usermodadd'][$pce_num_submit]); // Nicely scrubbed array of mods to serialize if (is_array($pce_usermod_update)) { $pce_usermod_serialized = serialize($pce_usermod_update); } // It returns an error else { return array( 'error', $pce_process_close ); } $pce_usermodsuccess = $wpdb->query('UPDATE ' . $pce_db_group . ' SET moderators = \'' . $pce_usermod_serialized . '\' WHERE collabgroup = ' . $pce_groupid); if( $pce_usermodsuccess ) { $updated = true; } } else { return array( 'error', sprintf( __( 'You must have at least one default mod for the group "%s".', 'peters_collaboration_emails' ), $pce_groupname ) ); } } } if( $updated ) { // We've made it this far, so success! return array( 'success', sprintf( __( 'Group moderators updated.', 'peters_collaboration_emails' ) ) ); } else { return false; } } function pce_groupsubmit() { global $wpdb, $pce_db_group, $pce_db_collab; // ---------------------------------- // Process a new group addition // ---------------------------------- if (!empty($_POST['newgroupname']) && $_POST['addrule'] != -1 && $_POST['addgroupmod'] != -1) { $newgroupname = $_POST['newgroupname']; $addrule = intval($_POST['addrule']); $addgroupmod = $_POST['addgroupmod']; // Check a contributor (basically that this contributor exists) $check_contributor = get_userdata($addrule); if (!$check_contributor) { return array( 'error', __( '**** ERROR: Invalid new group contributor user ID ****', 'peters_collaboration_emails' ) ); } // Check the added group moderator (admin, user ID, or e-mail address) // Check that it is a valid user if (is_numeric($addgroupmod)) { $pce_validuser = get_userdata($addgroupmod); if (!$pce_validuser) { return array( 'error', __( '**** ERROR: Invalid new group moderator user ID ****', 'peters_collaboration_emails' ) ); } $addgroupmod = intval($addgroupmod); } // If the dropdown equals "other" then look for content in pce_groupmodadd, which had better be an e-mail address elseif ($addgroupmod == 'other' && is_email($_POST['pce_groupmodadd'])) { $addgroupmod = $_POST['pce_groupmodadd']; } elseif ($addgroupmod != 'admin') { return array( 'error', __( '**** ERROR: Invalid new group moderator submitted ****', 'peters_collaboration_emails' ) ); } $addgroupmod_serialized = serialize(array($addgroupmod)); $pce_addgroupsuccess = $wpdb->query('INSERT INTO ' . $pce_db_group . ' (moderators, groupname) VALUES(\'' . $addgroupmod_serialized . '\', \'' . $newgroupname . '\')'); if ($pce_addgroupsuccess) { $pce_addwritersuccess = $wpdb->query('INSERT INTO ' . $pce_db_collab . ' (groupid, writerid) VALUES (LAST_INSERT_ID(), ' . $addrule . ')'); if ($pce_addwritersuccess) { return array( 'success', __( 'New group created.', 'peters_collaboration_emails' ) ); } else { return array( 'error', __( '**** ERROR: Unknown query error when adding a collaborator to the new group ****', 'peters_collaboration_emails' ) ); } } else { return array( 'error', __( '**** ERROR: Unknown query error when creating new group ****', 'peters_collaboration_emails' ) ); } } else { return array( 'error', __( '**** ERROR: Not all necessary group information was submitted to add a group ****', 'peters_collaboration_emails' ) ); } // We've made it this far, so nothing to do return false; } // Processes changes to a group name and its members function pce_edit_group_submit() { global $wpdb, $pce_db_group, $pce_db_collab; $pce_groupid = intval($_GET['group']); $pce_groupname = $wpdb->get_var('SELECT groupname FROM ' . $pce_db_group . ' WHERE collabgroup = ' . $pce_groupid); if (!$pce_groupname) { die(__('That group does not exist.', 'peters_collaboration_emails')); } // Open the informational div $pce_process_submit = '
' . "\n"; // Code to close the informational div $pce_process_close = '
' . "\n"; if (!empty($_POST['pce_groupname']) && !empty($_POST['pce_contributors'])) { $pce_groupname = $_POST['pce_groupname']; $pce_contributors = $_POST['pce_contributors']; } else { $pce_process_submit .= '

' . __('**** ERROR: Insufficient group name or contributor information ****', 'peters_collaboration_emails') . '

' . "\n"; $pce_process_submit .= '

' . __('**** Make sure that there is at least one contributor. ****', 'peters_collaboration_emails') . '

' . "\n"; $pce_process_submit .= $pce_process_close; return $pce_process_submit; } // ---------------------------------- // Process the group changes // ---------------------------------- // First find out which contributors already exist $pce_existing_contributors = $wpdb->get_results('SELECT writerid FROM ' . $pce_db_collab . ' WHERE groupid = ' . $pce_groupid, ARRAY_N); $pce_existing_contributor_array = array(); if ($pce_existing_contributors) { foreach($pce_existing_contributors as $pce_existing_contributor) { $pce_existing_contributor_array[$pce_existing_contributor[0]] = $pce_existing_contributor[0]; } } $pce_insert_writer = false; $pce_contributors_update = array(); foreach ($pce_contributors as $pce_contributor) { // Check that it is a valid user if (is_numeric($pce_contributor)) { $pce_validcontributor = get_userdata($pce_contributor); if (!$pce_validcontributor) { $pce_process_submit .= '

' . __('**** ERROR: Invalid contributor user ID ****', 'peters_collaboration_emails') . '

' . "\n"; $pce_process_submit .= $pce_process_close; return $pce_process_submit; } if (isset($pce_existing_contributor_array[$pce_contributor])) { unset($pce_existing_contributor_array[$pce_contributor]); } else { $pce_insert_success = $wpdb->query('INSERT INTO ' . $pce_db_collab . ' (groupid, writerid) VALUES (' . $pce_groupid . ', ' . $pce_contributor. ')'); if ($pce_insert_success && !$pce_insert_writer) { $pce_insert_writer = true; } } } } if (!empty($pce_existing_contributor_array)) { $pce_delete_contributors = $wpdb->query('DELETE FROM ' . $pce_db_collab . ' WHERE groupid = ' . $pce_groupid . ' AND writerid IN (' . implode(',', $pce_existing_contributor_array) . ')'); if ($pce_delete_contributors && !$pce_insert_writer) { $pce_insert_writer = true; } } if ($pce_insert_writer) { $pce_process_submit .= '

' . __('Collaborators updated.', 'peters_collaboration_emails') . '

' . "\n"; } $pce_groupname_success = $wpdb->query('UPDATE ' . $pce_db_group . ' SET groupname = \'' . $pce_groupname . '\' WHERE collabgroup = ' . $pce_groupid); if ($pce_groupname_success) { $pce_process_submit .= '

' . __('Group name updated.', 'peters_collaboration_emails') . '

' . "\n"; } // Close the informational div $pce_process_submit .= $pce_process_close; // We've made it this far, so success! return $pce_process_submit; } // Deletes a group function pce_delete_group_submit() { global $wpdb, $pce_db_group, $pce_db_collab; $pce_groupid = intval($_POST['pce_groupid']); $pce_groupname = $wpdb->get_var('SELECT groupname FROM ' . $pce_db_group . ' WHERE collabgroup = ' . $pce_groupid); if (!$pce_groupname) { return array( 'error', __( '**** ERROR: That group does not exist ****', 'peters_collaboration_emails' ) ); } // ---------------------------------- // Process the group deletion // ---------------------------------- // Remove all contributors $pce_remove_contributors = $wpdb->query('DELETE FROM ' . $pce_db_collab . ' WHERE groupid = ' . $pce_groupid); // Remove the group $pce_remove_group = $wpdb->query('DELETE FROM ' . $pce_db_group . ' WHERE collabgroup = ' . $pce_groupid . ' LIMIT 1'); if ($pce_remove_contributors && $pce_remove_group) { return array( 'success', sprintf( __( 'Group %s successfully deleted.', 'peters_collaboration_emails' ), $pce_groupname ) ); } else { return array( 'error', __( '**** ERROR: Database problem in removing the group. ****', 'peters_collaboration_emails' ) ); } // Nothing to say here return false; } // This is the options page in the WordPress admin panel that enables you to set moderators on a per-user basis function pce_optionsmenu() { if( isset( $_GET['group'] ) ) { pce_groupoptionsmenu( $_GET['group'] ); } elseif( isset( $_GET['delete_post_type_rule'] ) ) { pceFunctionCollection::pce_delete_post_type_rule( $_GET['delete_post_type_rule'] ); } else { pce_mainoptionsmenu(); } } function pce_groupoptionsmenu( $pce_groupid ) { global $wpdb, $pce_db_group, $pce_db_collab; $pce_groupid = intval( $pce_groupid ); $pce_process_submit = ''; // Update the group name and contributors if( isset( $_POST['pce_edit_group_submit'] ) ) { $pce_process_submit = pce_edit_group_submit(); } $pce_groupname = $wpdb->get_var('SELECT groupname FROM ' . $pce_db_group . ' WHERE collabgroup = ' . $pce_groupid); if( !$pce_groupname ) { die( __( 'That group does not exist.', 'peters_collaboration_emails' ) ); } $pce_groupname = htmlspecialchars($pce_groupname, ENT_QUOTES); $pce_contributors = $wpdb->get_results('SELECT writerid FROM ' . $pce_db_collab . ' WHERE groupid = ' . $pce_groupid, ARRAY_N); $pce_contributors_current = ''; $pce_contributors_whitespace = ' '; if ($pce_contributors) { $pce_contributors_array = array(); $i = 0; foreach ($pce_contributors as $pce_contributor) { $pce_contributor_data = get_userdata($pce_contributor[0]); if ($pce_contributor_data) { $pce_contributors_current .= "\n" . $pce_contributors_whitespace . '

' . $pce_contributor_data->display_name . '

'; $pce_contributors_array[$pce_contributor[0]] = ''; } ++$i; } } // Contributors that aren't part of this group $pce_contributors_remaining = pce_usersoptions($pce_contributors_array, 'contributors', false, $i); ?>

get_var('SELECT moderators FROM ' . $pce_db_group . ' WHERE collabgroup = 1'); // Put this list into an array since it is stored in the database as serialized $pce_defaultmods = unserialize($pce_defaultmods_serialized); // Build the list of options based on this array // Set up the default options variable $pce_defaultoptions = ''; // Whitespace! $pce_defaultoptionswhitespace = ' '; // Establish a counter for the checkboxes $i = 0; $pce_existingmods = array(); foreach ($pce_defaultmods as $pce_defaultmod) { // If they've chosen a user ID, get the e-mail address associated with that user ID if (is_numeric($pce_defaultmod)) { $pce_userinfo = get_userdata($pce_defaultmod); $pce_defaultoptions .= "\n" . $pce_defaultoptionswhitespace . '

' . $pce_userinfo->display_name . ' (' . $pce_userinfo->user_email . ')

'; $pce_existingmods[$pce_defaultmod] = ''; } // If they've chosen it to be the site admin, get the site admin e-mail address elseif ($pce_defaultmod == 'admin') { $pce_defaultoptions .= "\n" . $pce_defaultoptionswhitespace . '

' . sprintf( __( 'General admin (%s)', 'peters_collaboration_emails' ), get_option( 'admin_email' ) ) . '

'; $pce_existingmods['admin'] = ''; } // Whatever is left should be a custom e-mail address else { $pce_defaultoptions .= "\n" . $pce_defaultoptionswhitespace . '

' . $pce_defaultmod . '

'; } ++$i; } $pce_defaultoptions .= "\n" . $pce_defaultoptionswhitespace . '

' . __( 'Add:', 'peters_collaboration_emails' ) . '

E-mail:

'; // ----------------------------------- // Get the group-specific moderator rules // ----------------------------------- // Set up the default options variable $pce_useroptions = ''; $pce_usermods_results = $wpdb->get_results('SELECT collabgroup, moderators, groupname FROM ' . $pce_db_group . ' WHERE collabgroup != 1 ORDER BY groupname', ARRAY_N); if( $pce_usermods_results ) { $i_m = 0; foreach ($pce_usermods_results as $pce_usermod_result) { // Define the group name $pce_groupname = htmlspecialchars($pce_usermod_result[2], ENT_QUOTES); $pce_useroptions .= '' . "\n"; $pce_useroptions .= '

' . $pce_groupname . ' [Edit]

'; // Define the group ID $pce_groupid = $pce_usermod_result[0]; // Get the writers in this group $pce_writers = $wpdb->get_results('SELECT writerid FROM ' . $pce_db_collab . ' WHERE groupid = ' . $pce_groupid, ARRAY_N); if ($pce_writers) { $pce_useroptions .= "\n" . '

'; foreach ($pce_writers as $pce_writer) { $pce_thiswriter = get_userdata($pce_writer[0]); $pce_useroptions .= "\n" . $pce_thiswriter->display_name . '
'; } $pce_useroptions .= "\n" . '

'; } $pce_useroptions .= "\n" . ''; // Put this list of e-mail addresses an array since it is stored in the database as serialized $pce_usermods = unserialize($pce_usermod_result[1]); // Build the list of options based on this array // Establish a counter for the checkboxes $i = 0; $pce_useroptions .= "\n" . ''; $pce_existingmods = array(); foreach ($pce_usermods as $pce_usermod) { // If they've chosen a user ID, get the e-mail address associated with that user ID if (is_int($pce_usermod)) { $pce_userinfo = get_userdata($pce_usermod); $pce_useroptions .= "\n" . '

' . $pce_userinfo->display_name . ' (' . $pce_userinfo->user_email . ')

'; $pce_existingmods[$pce_usermod] = ''; } // If they've chosen it to be the site admin, get the site admin e-mail address elseif ($pce_usermod == 'admin') { $pce_useroptions .= "\n" . '

' . sprintf( __( 'General admin (%s)', 'peters_collaboration_emails' ), get_option( 'admin_email' ) ) . '

'; $pce_existingmods['admin'] = ''; } // Whatever is left should be a custom e-mail address else { $pce_useroptions .= "\n" . '

' . $pce_usermod . '

'; } ++$i; } $pce_useroptions .= "\n" . '

' . __( 'Add:', 'peters_collaboration_emails' ) . '

E-mail:

'; $pce_useroptions .= "\n" . ''; $pce_useroptions .= "\n" . ''; ++$i_m; } } // -------------------------------------------------------------------- // Form to add a group, needing at least one user and at least one moderator // -------------------------------------------------------------------- $pce_groupoptions = ''; $pce_groupoptions .= '

' . __( 'Group name:', 'peters_collaboration_emails' ) . '

'; $pce_groupoptions .= "\n" . '

' . __('Add contributor:', 'peters_collaboration_emails') . ' '; $pce_groupoptions .= "\n" . '

' . __('Add moderator:', 'peters_collaboration_emails') . '

E-mail:

'; // ----------------------------------- // Get the post-type-specific moderator rules // ----------------------------------- // Set up the default options variable $pce_post_type_rules = ''; $pce_post_type_mods_results = $wpdb->get_results( 'SELECT rule_id, post_type, taxonomy, term, moderators FROM ' . $pce_db_collabrules . ' ORDER BY post_type ASC, taxonomy ASC, term ASC', OBJECT ); if( $pce_post_type_mods_results ) { $i_p = 0; foreach( $pce_post_type_mods_results as $pce_post_type_mods_result ) { $pce_post_type_rules .= '' . "\n"; $pce_rule_id = $pce_post_type_mods_result ->rule_id; // Output the post type name $pce_post_type_name = htmlspecialchars($pce_post_type_mods_result->post_type, ENT_QUOTES ); $pce_post_type_rules .= '

' . $pce_post_type_name . ' [Delete]

'; // Output the post type taxonomy $pce_post_type_taxonomy = htmlspecialchars( $pce_post_type_mods_result->taxonomy, ENT_QUOTES ); if( 'all' == $pce_post_type_taxonomy ) { $pce_post_type_taxonomy_label = __( 'All', 'peters_collaboration_emails' ); } else { $pce_post_type_taxonomy_label = get_taxonomy( $pce_post_type_taxonomy ); if( '' == $pce_post_type_taxonomy_label ) { $pce_post_type_taxonomy_label = __( '*** undefined ***', 'peters_collaboration_emails' ); } else { $pce_post_type_taxonomy_label = $pce_post_type_taxonomy_label->label; } } $pce_post_type_rules .= '

' . $pce_post_type_taxonomy_label . '

'; $pce_post_type_term = htmlspecialchars( $pce_post_type_mods_result->term, ENT_QUOTES ); // Output the post type term if( 'all' == $pce_post_type_taxonomy ) { $pce_post_type_term = __( '*** n/a ***', 'peters_collaboration_emails' ); } elseif( is_numeric( $pce_post_type_term ) ) { $pce_post_type_term_object = get_term( $pce_post_type_term, $pce_post_type_taxonomy ); if( $pce_post_type_term_object && property_exists( $pce_post_type_term_object, 'name' ) ) { $pce_post_type_term = $pce_post_type_term_object->name; } } $pce_post_type_rules .= '

' . $pce_post_type_term . '

'; // Put this list of e-mail addresses an array since it is stored in the database as serialized $pce_post_type_mods = unserialize( $pce_post_type_mods_result->moderators ); // Build the list of options based on this array // Establish a counter for the checkboxes $i = 0; $pce_post_type_rules .= "\n" . ''; $pce_existingmods = array(); foreach( $pce_post_type_mods as $pce_post_type_mod ) { // If they've chosen a user ID, get the e-mail address associated with that user ID if( is_int( $pce_post_type_mod ) ) { $pce_userinfo = get_userdata( $pce_post_type_mod ); $pce_post_type_rules .= "\n" . '

' . $pce_userinfo->display_name . ' (' . $pce_userinfo->user_email . ')

'; $pce_existingmods[$pce_post_type_mod] = ''; } // If they've chosen it to be the site admin, get the site admin e-mail address elseif( 'admin' == $pce_post_type_mod ) { $pce_post_type_rules .= "\n" . '

' . __( 'General admin', 'peters_collaboration_emails' ) . '(' . get_option('admin_email') . ')

'; $pce_existingmods['admin'] = ''; } // Whatever is left should be a custom e-mail address else { $pce_post_type_rules .= "\n" . '

' . $pce_post_type_mod . '

'; } ++$i; } $pce_post_type_rules .= "\n" . '

' . __( 'Add:', 'peters_collaboration_emails' ) . '

E-mail:

'; $pce_post_type_rules .= "\n" . ''; $pce_post_type_rules .= "\n" . ''; ++$i_p; } } ?>

' . "\n"; print $pce_process_submit[1]; print '
' . "\n"; } ?>

'; print ''; foreach( $post_types as $post_type ) { // Don't show revisions, attachments, or navigation menu items, as they're not traditional posts if( ! in_array( $post_type->name, array( 'revision', 'attachment', 'nav_menu_item' ) ) ) { print ''; } } print '

'; print __( 'Taxonomy:' ) . '

'; print __( 'Term:' ) . ' '; print ''; print '

' . __('Add moderator:', 'peters_collaboration_emails') . '

E-mail:

'; ?>

array( $post_type ) ), 'objects' ); if( $taxonomies ) { $taxonomy_array = array(); foreach( $taxonomies as $taxonomy ) { // Skip a few built-in taxonomy types for now, as we're not going to support them if( ! in_array( $taxonomy->name, array( 'nav_menu', 'link_category', 'post_format' ) ) ) { if( '' == $taxonomy->label ) { $taxonomy_label = $taxonomy->name; } else { $taxonomy_label = $taxonomy->label; } $taxonomy_array[] = array( 'name' => $taxonomy->name, 'label' => $taxonomy_label ); } } print json_encode( $taxonomy_array ); die(); } } print '0'; die(); } // Ajax function to return JSON-encoded terms for a given taxonomy function pce_get_taxonomy_terms() { $taxonomy = $_POST['taxonomy']; if( taxonomy_exists( $taxonomy ) ) { $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) ); if( $terms ) { $terms_array = array(); foreach( $terms as $term ) { $terms_array[] = array( 'id' => $term->term_id, 'name' => $term->name ); } print json_encode( $terms_array ); die(); } else { print '1'; die(); } } print '0'; die(); } function pce_add_post_type_submit() { global $wpdb, $pce_db_collabrules; // ---------------------------------- // Process a new post type rule submission // ---------------------------------- if( -1 != $_POST['add_post_type_rule_mod'] ) { $post_type = $_POST['post_types']; $taxonomy_type = $_POST['taxonomy_types']; if( '' != trim( $_POST['taxonomy_term'] ) ) { $taxonomy_term = trim( $_POST['taxonomy_term'] ); } elseif( 'all' == $taxonomy_type ) { $taxonomy_term = ''; } else { $taxonomy_term = $_POST['taxonomy_terms']; } $post_type_mod = $_POST['add_post_type_rule_mod']; // Check: Is this taxonomy available for this post type? $post_type_taxonomies = get_taxonomies( array( 'object_type' => array( $post_type ) ), 'names' ); if( ! in_array( $taxonomy_type, $post_type_taxonomies ) && 'all' != $taxonomy_type ) { return array( 'error', __( '**** ERROR: That taxonomy type does not exist for that post type ****', 'peters_collaboration_emails' ) ); } // No check if taxonomy term is available for this taxonomy because you can often have freeform terms $taxonomy_term = substr( $taxonomy_term, 0, 255 ); // Check to make sure that this rule doesn't already exist with the same post type, taxonomy type, and taxonomy term // It might still exist if you had first submitted the term freeform and now you're submitting after the term was officially added and vice versa // Possible TODO: Check for both the ID and the text version of the term $rule_exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $pce_db_collabrules WHERE post_type = %s AND taxonomy = %s AND term = %s;" , $post_type, $taxonomy_type, $taxonomy_term ) ); if( $rule_exists ) { return array( 'error', __( '**** ERROR: That post type rule already exists. Please add or remove moderators to the existing rule. ****', 'peters_collaboration_emails' ) ); } // Check the added moderator (admin, user ID, or e-mail address) // Check that it is a valid user if( is_numeric( $post_type_mod ) ) { $pce_validuser = get_userdata( $post_type_mod ); if( !$pce_validuser ) { return array( 'error', __( '**** ERROR: Invalid new moderator user ID ****', 'peters_collaboration_emails' ) ); } $post_type_mod = intval( $post_type_mod ); } // If the dropdown equals "other" then look for content in pce_post_type_rule_mod_add, which had better be an e-mail address elseif( 'other' == $post_type_mod && is_email( $_POST['pce_post_type_rule_mod_add'] ) ) { $post_type_mod = $_POST['pce_post_type_rule_mod_add']; } elseif( 'admin' != $post_type_mod ) { return array( 'error', __( '**** ERROR: Invalid new moderator submitted ****', 'peters_collaboration_emails' ) ); } $post_type_mod_serialized = serialize( array( $post_type_mod ) ); $pce_add_post_type_mod_success = $wpdb->insert( $pce_db_collabrules, array( 'post_type' => $post_type , 'taxonomy' => $taxonomy_type , 'term' => $taxonomy_term , 'moderators' => $post_type_mod_serialized ) ); if( $pce_add_post_type_mod_success ) { return array( 'success', sprintf( __( 'New moderator added for the post type rule.', 'peters_collaboration_emails' ) ) ); } else { return array( 'error', __( '**** ERROR: Unknown query error when adding a new moderator for the post type rule ****', 'peters_collaboration_emails' ) ); } } else { return array( 'error', __( '**** ERROR: No moderator was submitted for the post type rule ****', 'peters_collaboration_emails' ) ); } // We've made it this far, so nothing to do! return false; } // Edit post type moderators function pce_post_type_submit() { global $wpdb, $pce_db_collabrules; // ---------------------------------- // Process the post-type-specific moderator changes // ---------------------------------- $updated = false; $pce_post_type_mods = $_POST['pce_post_type_mod']; // An array of moderators for each post type rule (contains User IDs, "admin" or strictly e-mail addresses) $pce_rule_ids = $_POST['pce_rule_id']; // An array of post type rules to be updated $pce_num_submits = array_keys( $pce_rule_ids ); if( $pce_num_submits ) { foreach( $pce_num_submits as $pce_num_submit ) { $pce_post_type_mods_update = array(); $pce_post_type_mod = $pce_post_type_mods[$pce_num_submit]; $pce_rule_id = intval( $pce_rule_ids[$pce_num_submit] ); // Does this post type rule exist? $post_type_rule_exists = pceFunctionCollection::post_type_rule_exists( $pce_rule_id ); if( ! $post_type_rule_exists ) { return array( 'error', sprintf(__('**** ERROR: Post type rule with ID of %d does not exist ****', 'peters_collaboration_emails'), $pce_rule_id ) ); } if( $pce_post_type_mod ) { $pce_post_type_mod_update = pce_mod_array( $pce_post_type_mod, $_POST['add_post_type_mod'][$pce_num_submit], $_POST['pce_post_type_mod_add'][$pce_num_submit] ); // Nicely scrubbed array of mods to serialize if( is_array( $pce_post_type_mod_update ) ) { $pce_post_type_mod_serialized = serialize( $pce_post_type_mod_update ); } // It returns an error else { return array( 'error', $pce_post_type_mod_update ); } $pce_post_type_mod_success = $wpdb->update( $pce_db_collabrules , array( 'moderators' => $pce_post_type_mod_serialized ) , array( 'rule_id' => $pce_rule_id ) ); if( $pce_post_type_mod_success ) { $updated = true; } } else { return array( 'error', __( 'You must have at least one default moderator for each rule. Otherwise, delete the rule.', 'peters_collaboration_emails' ) ); } } } // We've made it this far, so success! if( $updated ) { return array( 'success', __( 'Moderators for the post type rules updated.', 'peters_collaboration_emails' ) ); } else { return false; } } function pce_delete_post_type_rule( $rule_id ) { global $wpdb, $pce_db_collabrules; $rule_id = intval( $rule_id ); print '
'; print '

' . __( 'Delete post type rule', 'peters_collaboration_emails' ) . '

'; if( pceFunctionCollection::post_type_rule_exists( $rule_id ) ) { // If they actually wanted to delete the moderators for this category, let them know the result if( isset( $_POST['pce_delete_post_type_rule_yes'] ) ) { $wpdb->query( 'DELETE FROM ' . $pce_db_collabrules . ' WHERE rule_id = ' . $rule_id . ' LIMIT 1' ); print "\n" . '

' . sprintf( __( 'Post type rule successfully deleted.', 'peters_collaboration_emails' ) ); print "\n" . '

Back

' . "\n"; } else { print "\n" . '

' . __( 'Are you sure you want to remove this post type rule?', 'peters_collaboration_emails' ) . '

'; print "\n" . '
'; print "\n" . '

'; print "\n" . '
'; print "\n" . '
'; print "\n" . '

'; print "\n" . '
'; } } else { print '

' . __( 'That post type rule does not exist.', 'peters_collaboration_emails' ) . '

'; print '

Back

' . "\n"; } print '
'; } function post_type_rule_exists( $rule_id ) { global $wpdb, $pce_db_collabrules; $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $pce_db_collabrules WHERE rule_id = %d" , intval( $rule_id ) ) ); return $exists; } function create_post_type_table() { global $wpdb, $pce_db_collabrules; if( $wpdb->get_var( 'SHOW TABLES LIKE \'' . $pce_db_collabrules . '\'' ) != $pce_db_collabrules ) { $sql = "CREATE TABLE $pce_db_collabrules ( rule_id int(11) NOT NULL auto_increment, post_type varchar(50) NOT NULL, taxonomy varchar(50) NOT NULL, term varchar(255) NOT NULL, moderators longtext NOT NULL, UNIQUE KEY rule_id (rule_id) );"; $wpdb->query( $sql ); } } } add_action( 'wp_ajax_get_post_type_taxonomies', array( 'pceFunctionCollection', 'pce_get_post_type_taxonomies' ) ); add_action( 'wp_ajax_get_taxonomy_terms', array( 'pceFunctionCollection', 'pce_get_taxonomy_terms' ) ); function pce_addoptionsmenu() { global $pce_required_capability; add_options_page( __( 'Collaboration e-mails', 'peters_collaboration_emails' ), __( 'Collaboration e-mails', 'peters_collaboration_emails' ), $pce_required_capability, basename( __FILE__ ), 'pce_optionsmenu' ); } add_action('admin_menu','pce_addoptionsmenu',1); // Perform upgrade functions // Some newer operations are duplicated from pce_install() as there's no guarantee that the user will follow a specific upgrade procedure function pce_upgrade() { global $wpdb, $pce_db_cats, $pce_db_collabrules, $pce_version; // Turn version into an integer for comparisons $current_version = intval( str_replace( '.', '', get_option( 'pce_version' ) ) ); if( $current_version < 150 ) { pceFunctionCollection::create_post_type_table(); if( $wpdb->get_var( 'SHOW TABLES LIKE \'' . $pce_db_cats . '\'' ) == $pce_db_cats ) { // Transfer all category-specific rules $category_rules = $wpdb->get_results( 'SELECT catid, moderators FROM ' . $pce_db_cats, OBJECT ); if( $category_rules ) { foreach( $category_rules as $category_rule ) { // Add these rules for both posts and pages and let the user delete the post/post rules if not applicable $wpdb->insert( $pce_db_collabrules, array( 'post_type' => 'post' , 'taxonomy' => 'category' , 'term' => $category_rule->catid , 'moderators' => $category_rule->moderators ) ); $wpdb->insert( $pce_db_collabrules, array( 'post_type' => 'page' , 'taxonomy' => 'category' , 'term' => $category_rule->catid , 'moderators' => $category_rule->moderators ) ); } } // Delete old category table $wpdb->query( 'DROP TABLE ' . $pce_db_cats ); } // Future versions // update_option( 'pce_version', '1.5.0', '', 'no' ); } if( $current_version != intval( str_replace( '.', '', $pce_version ) ) ) { // Add the version number to the database delete_option( 'pce_version' ); add_option( 'pce_version', $pce_version, '', 'no' ); } } // Add and remove database tables when installing and uninstalling function pce_install() { global $wpdb, $pce_db_group, $pce_db_collab, $pce_db_collabrules, $pce_version; // Add the table to hold group information and moderator rules if($wpdb->get_var('SHOW TABLES LIKE \'' . $pce_db_group . '\'') != $pce_db_group) { $sql = 'CREATE TABLE ' . $pce_db_group . ' ( collabgroup bigint(20) NOT NULL auto_increment, moderators longtext NOT NULL, groupname varchar(255) NOT NULL, KEY collabgroup (collabgroup) ) AUTO_INCREMENT=2;'; $wpdb->query($sql); } // Insert the default moderator rule $sql = 'INSERT INTO ' . $pce_db_group . ' (collabgroup, moderators, groupname) VALUES (1, \'a:1:{i:0;s:5:"admin";}\', \'Default\')'; $wpdb->query($sql); // Add the table to hold group - collaborator associations if($wpdb->get_var('SHOW TABLES LIKE \'' . $pce_db_collab . '\'') != $pce_db_collab) { $sql = 'CREATE TABLE ' . $pce_db_collab . ' ( groupid bigint(20) NOT NULL, writerid bigint(20) NOT NULL, UNIQUE KEY groupwriter (groupid, writerid) )'; $wpdb->query($sql); } pceFunctionCollection::create_post_type_table(); pce_upgrade(); } function pce_uninstall() { global $wpdb, $pce_db_group, $pce_db_collab, $pce_db_cats, $pce_db_collabrules; if($wpdb->get_var('SHOW TABLES LIKE \'' . $pce_db_group . '\'') == $pce_db_group) { $sql = 'DROP TABLE ' . $pce_db_group; $wpdb->query($sql); } if($wpdb->get_var('SHOW TABLES LIKE \'' . $pce_db_collab . '\'') == $pce_db_collab) { $sql = 'DROP TABLE ' . $pce_db_collab; $wpdb->query($sql); } if($wpdb->get_var('SHOW TABLES LIKE \'' . $pce_db_cats . '\'') == $pce_db_cats) { $sql = 'DROP TABLE ' . $pce_db_cats; $wpdb->query($sql); } if($wpdb->get_var('SHOW TABLES LIKE \'' . $pce_db_collabrules . '\'') == $pce_db_collabrules) { $sql = 'DROP TABLE ' . $pce_db_collabrules; $wpdb->query($sql); } delete_option( 'pce_version' ); } register_activation_hook( __FILE__, 'pce_install' ); register_uninstall_hook( __FILE__, 'pce_uninstall' ); } // This closes that initial check to make sure someone is actually logged in ?>