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 = '' . __('**** 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 .= '' . $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_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" . '' . __( '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 .= '' . $pce_post_type_name . ' [Delete]
' . $pce_post_type_taxonomy_label . '
' . $pce_post_type_term . '
' . $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" . '' . sprintf( __( 'Post type rule successfully deleted.', 'peters_collaboration_emails' ) ); print "\n" . '
' . "\n"; } else { print "\n" . '' . __( 'Are you sure you want to remove this post type rule?', 'peters_collaboration_emails' ) . '
'; print "\n" . ''; print "\n" . ''; } } else { print '' . __( 'That post type rule does not exist.', 'peters_collaboration_emails' ) . '
'; print '' . "\n"; } print '