, updated: int}> */ public static function get_presets_for_screen($storage_id) { $storage_id = self::sanitize_storage_id($storage_id); if ('' === $storage_id) { return []; } $all = self::get_all_presets(); $slice = isset($all[ $storage_id ]) && is_array($all[ $storage_id ]) ? $all[ $storage_id ] : []; $out = []; $user_id = self::current_user_id(); foreach ($slice as $preset) { if (! is_array($preset)) { continue; } $normalized = self::normalize_preset_row($preset); if (null === $normalized) { continue; } // Filtered here rather than in the UI: the `meprmf_filter_presets` payload, the // AJAX responses and the localized script all read through this one method, so a // private view of another admin's is never handed out in the first place. if (! self::user_can_see($normalized, $user_id)) { continue; } $out[] = $normalized; } usort( $out, static function ($a, $b) { return strcasecmp((string) $a['name'], (string) $b['name']); } ); /** * Filter saved presets before they are exposed to the admin UI. * * @since 2.0.0 * @param array> $out Preset rows. * @param string $storage_id Screen storage id. */ return apply_filters('meprmf_filter_presets', $out, $storage_id); } /** * Save or update a preset by unique name on one screen. * * @param string $storage_id Screen storage id. * @param string $name Preset display name. * @param array $params Raw param map from request. * @param array $known_params Whitelist of allowed param keys. * @return array{success: bool, code?: string, preset?: array} */ public static function save_preset($storage_id, $name, array $params, array $known_params, $visibility = self::VISIBILITY_SHARED) { $storage_id = self::sanitize_storage_id($storage_id); if ('' === $storage_id) { return [ 'success' => false, 'code' => 'invalid_screen' ]; } $name = self::sanitize_preset_name($name); if ('' === $name) { return [ 'success' => false, 'code' => 'empty_name' ]; } $clean = self::sanitize_preset_params($params, $known_params); if (empty($clean)) { return [ 'success' => false, 'code' => 'empty_params' ]; } $visibility = self::VISIBILITY_PRIVATE === $visibility ? self::VISIBILITY_PRIVATE : self::VISIBILITY_SHARED; $user_id = self::current_user_id(); // A private view needs an owner to be private *to*; without a logged-in user the only // honest answer is shared. if ($user_id < 1) { $visibility = self::VISIBILITY_SHARED; } $all = self::get_all_presets(); $slice = isset($all[ $storage_id ]) && is_array($all[ $storage_id ]) ? $all[ $storage_id ] : []; $updated_at = time(); $found = false; $saved = null; foreach ($slice as $i => $preset) { if (! is_array($preset) || empty($preset['name'])) { continue; } if (0 !== strcasecmp((string) $preset['name'], $name)) { continue; } $row_owner = isset($preset['owner']) ? max(0, (int) $preset['owner']) : 0; $row_visibility = ( $row_owner > 0 && isset($preset['visibility']) && self::VISIBILITY_PRIVATE === $preset['visibility'] ) ? self::VISIBILITY_PRIVATE : self::VISIBILITY_SHARED; // Another admin's private view is invisible, so its name is not taken as far as // this admin is concerned: fall through and save a separate row. if (self::VISIBILITY_PRIVATE === $row_visibility && $row_owner !== $user_id) { continue; } // Overwriting a shared view that belongs to someone else is the concurrent // last-write-wins this change exists to stop. Refuse it by name instead. if ($row_owner > 0 && $row_owner !== $user_id && ! self::user_can_manage_others_views()) { return [ 'success' => false, 'code' => 'name_taken' ]; } $id = isset($preset['id']) ? self::sanitize_preset_id((string) $preset['id']) : ''; if ('' === $id) { $id = self::generate_preset_id(); } $slice[ $i ] = [ 'id' => $id, 'name' => $name, 'params' => $clean, 'updated' => $updated_at, // An ownerless pre-2.2 row is claimed by whoever next saves over it; a row // that already has an owner keeps it, so saving never transfers a view. 'owner' => $row_owner > 0 ? $row_owner : $user_id, 'visibility' => $visibility, ]; $saved = $slice[ $i ]; $found = true; break; } if (! $found) { $max = self::max_presets_per_screen(); if (count($slice) >= $max) { return [ 'success' => false, 'code' => 'limit_reached' ]; } $saved = [ 'id' => self::generate_preset_id(), 'name' => $name, 'params' => $clean, 'updated' => $updated_at, 'owner' => $user_id, 'visibility' => $visibility, ]; $slice[] = $saved; } $all[ $storage_id ] = array_values($slice); self::update_all_presets($all); if (null !== $saved) { $normalized = self::normalize_preset_row($saved); if (null !== $normalized) { $saved = $normalized; } } return [ 'success' => true, 'preset' => $saved, ]; } /** * Delete one preset by id on a screen. * * @param string $storage_id Screen storage id. * @param string $preset_id Preset id. * @return array{success: bool, code?: string} */ public static function delete_preset($storage_id, $preset_id) { $storage_id = self::sanitize_storage_id($storage_id); $preset_id = self::sanitize_preset_id($preset_id); if ('' === $storage_id || '' === $preset_id) { return [ 'success' => false, 'code' => 'invalid_input' ]; } $all = self::get_all_presets(); $slice = isset($all[ $storage_id ]) && is_array($all[ $storage_id ]) ? $all[ $storage_id ] : []; $next = []; $user_id = self::current_user_id(); $removed = false; foreach ($slice as $preset) { if (! is_array($preset)) { continue; } $id = isset($preset['id']) ? self::sanitize_preset_id((string) $preset['id']) : ''; if ($id === $preset_id) { $normalized = self::normalize_preset_row($preset); if (null !== $normalized) { // Another admin's private view is not theirs to know about, let alone // delete, so it reads as missing rather than as refused. if (! self::user_can_see($normalized, $user_id)) { return [ 'success' => false, 'code' => 'not_found' ]; } if (! self::user_can_delete($normalized, $user_id)) { return [ 'success' => false, 'code' => 'not_owner' ]; } } $removed = true; continue; } $next[] = $preset; } if (! $removed) { return [ 'success' => false, 'code' => 'not_found' ]; } // A view being deleted must stop being anybody's default, or every admin who chose it // gets a screen that silently filters on a view that no longer exists. self::forget_default_view_everywhere($storage_id, $preset_id); $all[ $storage_id ] = array_values($next); self::update_all_presets($all); return [ 'success' => true ]; } /** * Allowed filter param keys for one list screen (from the filter registry). * * @param string $storage_id Screen storage id. * @return array */ public static function get_known_params_for_storage_id($storage_id) { $storage_id = self::sanitize_storage_id($storage_id); if ('' === $storage_id) { return []; } $ctx = Meprmf_Screen::context_for_storage_id($storage_id); if (null === $ctx) { return []; } $fields = Meprmf_Filter_Registry::get_normalized_fields_for_context($ctx); // Presets must carry each field's operator and relative-window params too, or a saved // "is not empty" reloads as a plain contains-match against an empty value. $params = [ Meprmf_Util::MATCH_MODE_PARAM ]; foreach ($fields as $field) { foreach (Meprmf_Util::collect_field_request_params($field) as $param) { if ('' !== $param) { $params[] = $param; } } } return array_values(array_unique($params)); } /** * Whitelist and sanitize preset param map. * * @param array $params Raw params. * @param array $known_params Allowed keys. * @return array */ public static function sanitize_preset_params(array $params, array $known_params) { $allowed = []; foreach ($known_params as $key) { $key = Meprmf_Util::sanitize_param((string) $key); if ('' !== $key) { $allowed[ $key ] = true; } } $out = []; foreach ($params as $key => $value) { $key = Meprmf_Util::sanitize_param((string) $key); if ('' === $key || ! isset($allowed[ $key ])) { continue; } if (! is_scalar($value)) { continue; } $val = sanitize_text_field((string) $value); if ('' === $val) { continue; } $out[ $key ] = $val; } return $out; } /** * Save preset from AJAX. * * @return void */ public static function ajax_save_filter_preset() { if (! Meprmf_Capabilities::current_user_can_filter()) { wp_send_json_error([ 'message' => 'forbidden', 'code' => 'forbidden' ], 403); } check_ajax_referer('meprmf_filter_presets', 'nonce'); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified above. $storage_id = isset($_POST['screen']) ? sanitize_text_field(wp_unslash((string) $_POST['screen'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized in save_preset(). $name = isset($_POST['name']) ? (string) wp_unslash($_POST['name']) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized in save_preset(). $params_raw = isset($_POST['params']) ? wp_unslash($_POST['params']) : ''; $known = self::get_known_params_for_storage_id($storage_id); if (empty($known)) { wp_send_json_error( [ 'message' => self::error_message_for_code('invalid_screen'), 'code' => 'invalid_screen', ], 400 ); } $params = []; if (is_string($params_raw) && '' !== $params_raw) { $decoded = json_decode($params_raw, true); if (is_array($decoded)) { $params = $decoded; } } elseif (is_array($params_raw)) { $params = $params_raw; } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified above. $visibility = isset($_POST['visibility']) ? sanitize_text_field(wp_unslash((string) $_POST['visibility'])) : self::VISIBILITY_SHARED; $result = self::save_preset($storage_id, $name, $params, $known, $visibility); if (empty($result['success'])) { wp_send_json_error( [ 'message' => self::error_message_for_code(isset($result['code']) ? (string) $result['code'] : 'unknown'), 'code' => isset($result['code']) ? (string) $result['code'] : 'unknown', ], 400 ); } wp_send_json_success( [ 'preset' => $result['preset'], 'presets' => array_values(self::get_presets_for_screen($storage_id)), 'default' => self::get_default_view_id($storage_id), ] ); } /** * Delete preset from AJAX. * * @return void */ public static function ajax_delete_filter_preset() { if (! Meprmf_Capabilities::current_user_can_filter()) { wp_send_json_error([ 'message' => 'forbidden', 'code' => 'forbidden' ], 403); } check_ajax_referer('meprmf_filter_presets', 'nonce'); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified above. $storage_id = isset($_POST['screen']) ? sanitize_text_field(wp_unslash((string) $_POST['screen'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing $preset_id = isset($_POST['id']) ? sanitize_text_field(wp_unslash((string) $_POST['id'])) : ''; $result = self::delete_preset($storage_id, $preset_id); if (empty($result['success'])) { wp_send_json_error( [ 'message' => self::error_message_for_code(isset($result['code']) ? (string) $result['code'] : 'unknown'), 'code' => isset($result['code']) ? (string) $result['code'] : 'unknown', ], 400 ); } wp_send_json_success( [ 'presets' => array_values(self::get_presets_for_screen($storage_id)), 'default' => self::get_default_view_id($storage_id), ] ); } /** * Current admin's user id, or 0 outside a user context. * * @return int */ private static function current_user_id() { return function_exists('get_current_user_id') ? max(0, (int) get_current_user_id()) : 0; } /** * Whether this admin may act on views that belong to another admin. * * @return bool */ public static function user_can_manage_others_views() { $can = function_exists('current_user_can') ? (bool) current_user_can('manage_options') : false; /** * Whether the current admin may delete or overwrite another admin's shared view. * * @since 2.2.0 * @param bool $can Default: whoever can manage options. */ return (bool) apply_filters('meprmf_can_manage_others_views', $can); } /** * Whether one normalized row is visible to a user. * * @param array $preset Normalized row. * @param int $user_id User id. * @return bool */ private static function user_can_see(array $preset, $user_id) { if (self::VISIBILITY_PRIVATE !== ( isset($preset['visibility']) ? $preset['visibility'] : '' )) { return true; } return (int) $user_id > 0 && (int) $preset['owner'] === (int) $user_id; } /** * Whether one normalized row may be deleted by a user. * * @param array $preset Normalized row. * @param int $user_id User id. * @return bool */ private static function user_can_delete(array $preset, $user_id) { $owner = isset($preset['owner']) ? (int) $preset['owner'] : 0; // Pre-2.2 rows have no owner, so nobody can be locked out of the views they already had. if ($owner < 1 || $owner === (int) $user_id) { return true; } return self::user_can_manage_others_views(); } /** * Select label for a view: the name, marked when it is not shared. * * Built server-side so the PHP `