Created
October 15, 2010 14:32
-
-
Save mjangda/628265 to your computer and use it in GitHub Desktop.
Add-on for Edit Flow that let's you set a "Reviewer" usergroups for individual users. All notifications related to that user go their assigned usergroup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Edit Flow - Reviewer Add-on | |
| Plugin URI: http://editflow.wordpress.com | |
| Description: An add-on for Edit Flow that allows you to assign review groups for users that will be notified any time a user updates a post. | |
| Author: Mohammad Jangda | |
| Version: 0.1 | |
| Author URI: http://www.digitalize.ca | |
| Copyright 2009-2010 Mohammad Jangda | |
| GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/> | |
| This program is free software; you can redistribute it and/or modify | |
| it under the terms of the GNU General Public License as published by | |
| the Free Software Foundation; either version 2 of the License, or | |
| (at your option) any later version. | |
| This program is distributed in the hope that it will be useful, | |
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| GNU General Public License for more details. | |
| You should have received a copy of the GNU General Public License | |
| along with this program; if not, write to the Free Software | |
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| */ | |
| define( 'EF_R_REVIEWER_METAKEY', 'ef_r_reviewer' ); | |
| function ef_r_admin_init( ) { | |
| global $edit_flow; | |
| if( !isset($edit_flow) ) | |
| return; | |
| // Action to add dropdown to user profile | |
| add_action('show_user_profile', 'ef_r_select_reviewer'); | |
| add_action('edit_user_profile', 'ef_r_select_reviewer'); | |
| // action to save reviewer group to user profile | |
| add_action('user_profile_update_errors', 'ef_r_save_reviewer', 10, 3); | |
| // Filter the recipient list | |
| add_filter('ef_notification_recipients', 'ef_r_filter_recipients', 10, 2); | |
| } | |
| /** | |
| * Adds stuff to the user profile page to allow adding usergroup selecting options | |
| */ | |
| function ef_r_select_reviewer( ) { | |
| global $user_id, $profileuser; | |
| if( !$user_id ) return; | |
| // Only enable editing if user has the proper cap | |
| if( current_user_can('edit_usergroups') ) { | |
| // Assemble all necessary data | |
| $usergroups = ef_get_usergroups(); | |
| $reviewer = ef_r_get_reviewer($user_id); | |
| if( !empty( $usergroups ) ) { | |
| $review_select = '<select name="'. EF_R_REVIEWER_METAKEY .'">'; | |
| $review_select .= '<option value="">'. __('Not Assigned', 'edit-flow-reviewer') .'</option>'; | |
| foreach( $usergroups as $usergroup ) { | |
| $selected = ( $reviewer == $usergroup->slug ) ? ' selected="selected"' : ''; | |
| $review_select .= '<option value="'. $usergroup->slug .'"'. $selected .'>'; | |
| $review_select .= $usergroup->name; | |
| $review_select .= '</option>'; | |
| } | |
| $review_select .= '</select>'; | |
| } | |
| ?> | |
| <table class="form-table"> | |
| <tbody> | |
| <tr> | |
| <th> | |
| <h3><?php _e('Review Group', 'edit-flow') ?></h3> | |
| <p> | |
| <?php _e('Select the usergroup responsible for reviewing this user\'s post. Memebers of the selected usergroups will be included in all post update notifications', 'edit-flow-reviewer') ?><br /> | |
| </p> | |
| </th> | |
| <td> | |
| <?php echo $review_select ?> | |
| </td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| <?php | |
| } | |
| } | |
| /** | |
| * Function called when a user's profile is updated | |
| * Adds user to specified usergroups | |
| * @param | |
| * @param | |
| * @param | |
| * @return | |
| */ | |
| function ef_r_save_reviewer( $errors, $update, $user ) { | |
| if($update) { | |
| $user_id = $user->ID; | |
| if ( !current_user_can('edit_user', $user_id) ) | |
| wp_die(__('Hey now, you don\'t have permission to edit this user.', 'edit-flow-reviewer')); | |
| if ( current_user_can( 'edit_usergroups' ) ) { | |
| // Get the POSTed reviewer data | |
| $reviewer = sanitize_text_field($_POST[EF_R_REVIEWER_METAKEY]); | |
| // Save the data! | |
| update_metadata( 'user', $user_id, EF_R_REVIEWER_METAKEY, $reviewer, false ); | |
| } | |
| } | |
| return array( &$errors, $update, &$user ); | |
| } | |
| function ef_r_get_reviewer( $user_id ) { | |
| if( !$user_id ) return false; | |
| $reviewer = get_metadata('user', $user_id, EF_R_REVIEWER_METAKEY, true); | |
| if( !$reviewer || is_wp_error($reviewer) ) | |
| $reviewer = false; | |
| return $reviewer; | |
| } | |
| function ef_r_filter_recipients( $recipients, $post ) { | |
| $user_id = $post->post_author; | |
| if( !$user_id ) return array(); | |
| $reviewer = ef_r_get_reviewer( $user_id ); | |
| if( $reviewer ) | |
| $reviewer_emails = ef_get_users_by_usermeta( EDIT_FLOW_USERGROUPS_USERMETA, $reviewer, 'user_email' ); | |
| if( !$reviewer_emails ) | |
| $reviewer_emails = array(); | |
| $recipients = array_merge($recipients, $reviewer_emails); | |
| /* | |
| echo '<p>Before:</p>'; | |
| print_r($recipients); | |
| echo '<p>After:</p>'; | |
| print_r($reviewer_emails); | |
| die(); | |
| */ | |
| return $recipients; | |
| } | |
| // Core hook to initialize the plugin | |
| add_action('admin_init', 'ef_r_admin_init'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment