/** * Adds in the Pushover Notifications Preferences Profile Section * @param object $user The User object being viewed * @return void Displays HTML */ function ckpn_add_profile_settings($user) { if (!get_user_meta($user->ID, 'ckpn_user_key', true)) { return; } $gets_post_notifications = false; $new_post_roles = ckpn_get_option('new_post_roles'); foreach ($new_post_roles as $role => $value) { if (current_user_can($role) && !empty($value)) { $gets_post_notifications = true; } if ($gets_post_notifications == true) { continue; } } ?> <h3><?php _e('Pushover Notifications Preferences', CKPN_CORE_TEXT_DOMAIN); ?> </h3> <table class="form-table"> <tr> <th><?php _e('Notify Me Of …', CKPN_CORE_TEXT_DOMAIN); ?> </th> <td> <?php if ($gets_post_notifications) { ?> <input type="checkbox" name="ckpn_user_notify_posts" id="ckpn_user_notify_posts" value="1" <?php checked('1', get_user_meta($user->ID, 'ckpn_user_notify_posts', true), true); ?> /> <label for="ckpn_user_notify_posts"><?php _e('New Posts Being Published', CKPN_CORE_TEXT_DOMAIN); ?> </label> <?php } ?> </td> </tr> </table> <?php }
/** * Fires when a new blog post is moved into the published status * @param string $new_status Status the blog post is moving to * @param string $old_status Previous post status * @param object $post The Post Object * @return void */ function ckpn_post_published($new_status, $old_status, $post) { // By default only send on post and page publishing $allowed_post_types = apply_filters('ckpn_post_publish_types', array('post', 'page')); // Only do this when a post transitions to being published if (in_array($post->post_type, $allowed_post_types) && $new_status == 'publish' && $old_status != 'publish') { $user_keys = array(); $title = get_bloginfo('name') . ': ' . __('New Post', CKPN_CORE_TEXT_DOMAIN); $title = apply_filters('ckpn_new_post_title', $title, $post); $author_data = get_userdata($post->post_author); $author_name = $author_data->display_name; $message = get_the_title($post->ID) . __(' by ', CKPN_CORE_TEXT_DOMAIN) . $author_name; $message = apply_filters('ckpn_new_post_message', $message, $post); $url = get_permalink($post->ID); $url_title = __('View Post', CKPN_CORE_TEXT_DOMAIN); $args = array('title' => $title, 'message' => $message, 'url' => $url, 'url_title' => $url_title); $new_post_roles = ckpn_get_option('new_post_roles'); $user_array = array(); foreach ($new_post_roles as $role => $value) { $user_args = array('role' => $role, 'fields' => 'ID'); $users = get_users($user_args); $user_array = array_unique(array_merge($users, $user_array)); } $super_admins = array(); if (defined('MULTISITE') && MULTISITE) { $super_admin_logins = get_super_admins(); foreach ($super_admin_logins as $super_admin_login) { $user = get_user_by('login', $super_admin_login); if ($user) { $super_admins[] = $user->ID; } } } $users_to_alert = array_unique(array_merge($user_array, $super_admins)); $current_user = wp_get_current_user(); // Unset the Post Author for non-scheduled posts if ($old_status !== 'future' && ($key = array_search($post->post_author, $users_to_alert)) !== false && $current_user->ID == $post->post_author) { unset($users_to_alert[$key]); } $options = ckpn_get_options(); // Add the default admin key from settings if it's different than the authors if (get_user_meta($post->post_author, 'ckpn_user_key', true) !== $options['api_key']) { $user_keys = array($options['api_key']); } $users_with_keys = ckpn_get_users_with_keys(); // Search the users for their Keys and send the posts foreach ($users_to_alert as $user) { $selected = get_user_meta($user, 'ckpn_user_notify_posts', true); if ($selected && array_key_exists($user, $users_with_keys)) { $user_keys[] = $users_with_keys[$user]; } } $user_keys = array_unique($user_keys); foreach ($user_keys as $user) { $args['user'] = $user; ckpn_send_notification($args); } } }