/** * Save alert meta data to parent post ID * * @since 0.1.0 * * @param string $new_status * @param string $old_status * @param WP_Post $post */ function wp_user_alerts_save_alerts_metabox($new_status, $old_status, $post = null) { // Bail if already published if (in_array($old_status, wp_user_alerts_get_allowed_post_statuses(), true)) { return; } // Bail on autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } // Bail if not supported if (!post_type_supports($post->post_type, 'alerts')) { return; } // Delete all relative meta data by matching key delete_post_meta($post->ID, 'wp_user_alerts_user'); delete_post_meta($post->ID, 'wp_user_alerts_role'); delete_post_meta($post->ID, 'wp_user_alerts_method'); delete_post_meta($post->ID, 'wp_user_alerts_priority'); delete_post_meta($post->ID, 'wp_user_alerts_message'); // Delete any other metas do_action('wp_user_alerts_delete_metas', $post->ID); // Users if (!empty($_POST['wp_user_alerts_users'])) { foreach ($_POST['wp_user_alerts_users'] as $user_id) { add_post_meta($post->ID, 'wp_user_alerts_user', (int) $user_id); } } // Roles if (!empty($_POST['wp_user_alerts_roles'])) { foreach ($_POST['wp_user_alerts_roles'] as $role_id) { add_post_meta($post->ID, 'wp_user_alerts_role', $role_id); } } // Methods if (!empty($_POST['wp_user_alerts_methods'])) { $methods = wp_user_alerts_get_alert_methods(); foreach ($_POST['wp_user_alerts_methods'] as $method_id) { if (!isset($methods[$method_id])) { continue; } add_post_meta($post->ID, 'wp_user_alerts_method', $method_id); } } // Priorities if (!empty($_POST['wp_user_alerts_priorities'])) { $priorities = wp_user_alerts_get_alert_priorities(); foreach ($_POST['wp_user_alerts_priorities'] as $priority_id) { if (!isset($priorities[$priority_id])) { continue; } add_post_meta($post->ID, 'wp_user_alerts_priority', sanitize_key($priority_id)); } } // Message if (!empty($_POST['wp_user_alerts_message'])) { $message = wp_kses($_POST['wp_user_alerts_message'], array()); if (!empty($message)) { add_post_meta($post->ID, 'wp_user_alerts_message', $message); } } // Add any other metas do_action('wp_user_alerts_add_metas', $post->ID, $post); }
/** * Get all alerts * * @since 0.1.0 * * @param array $args */ function wp_user_alerts_get_posts($args = array()) { // Parse arguments $r = wp_parse_args($args, array('post_type' => wp_user_alerts_get_allowed_post_types(), 'post_status' => wp_user_alerts_get_allowed_post_statuses(), 'meta_query' => array(array()))); // Filter the alert arguments $posts = apply_filters('wp_user_alerts_get_alerts', $r, $args); // Get the posts return get_posts($posts); }