/** * Add/Remove a user from the list of users * @param int/boolean $user_id The User ID to add/update * @param string/boolean $user_key The user's Pushover API Key * @return boolean If the status was updated successfully */ function ckpn_update_user_to_keys_list($user_id = false, $user_key = false) { if (false === $user_id || false === $user_key) { return; } $current_users_with_keys = ckpn_get_users_with_keys(); if (empty($user_key)) { unset($current_users_with_keys[$user_id]); } else { $new_user[$user_id] = $user_key; // Using + instead of array_merge to preserve keys, takes left key over right in the case of duplicates $current_users_with_keys = $new_user + $current_users_with_keys; } return update_option('_ckpn_users_with_keys', apply_filters('ckpn_update_user_to_keys_list', $current_users_with_keys)); }
/** * 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); } } }