class-modal-dialog.php
2 years ago
class-notice.php
2 years ago
class-redirect-message.php
2 years ago
class-ui-elements.php
2 weeks ago
class-ui-settings.php
2 years ago
class-ui.php
2 years ago
class-redirect-message.php
139 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Frontend redirect messages. |
| 4 | * |
| 5 | * @package wp-job-manager |
| 6 | * @since 2.2.0 |
| 7 | */ |
| 8 | |
| 9 | namespace WP_Job_Manager\UI; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; // Exit if accessed directly. |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Redirect after an action is finished and make sure a message can displayed on the next page load. |
| 17 | * |
| 18 | * Stores the messages in a transient with an ID, and passes along the ID as a query string to the redirected URL. |
| 19 | * |
| 20 | * @since 2.2.0 |
| 21 | * |
| 22 | * @internal This API is still under development and subject to change. |
| 23 | * @access private |
| 24 | */ |
| 25 | class Redirect_Message { |
| 26 | |
| 27 | const DEFAULT_QUERY_VAR = 'message'; |
| 28 | const TRANSIENT_PREFIX = 'wpjm_message_'; |
| 29 | |
| 30 | /** |
| 31 | * Redirect to a URL with a message to be displayed on the next page load. |
| 32 | * |
| 33 | * @param string $url URL to redirect to. |
| 34 | * @param string $message Optional message to be displayed on the next page load. |
| 35 | * @param string $query_var A query variable added to the redirect URL, referencing the message transient. |
| 36 | */ |
| 37 | public static function redirect( $url, $message = null, $query_var = '' ) { |
| 38 | if ( empty( $query_var ) ) { |
| 39 | $query_var = self::DEFAULT_QUERY_VAR; |
| 40 | } |
| 41 | |
| 42 | if ( ! empty( $message ) ) { |
| 43 | $url = add_query_arg( [ $query_var => self::set_transient_message( $message ) ], $url ); |
| 44 | } |
| 45 | wp_safe_redirect( $url ); |
| 46 | exit; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get a message set during the previous redirect. |
| 51 | * |
| 52 | * @param string $query_var Optional namespace for the message. |
| 53 | * |
| 54 | * @return string|null Message content if there is one. |
| 55 | */ |
| 56 | public static function get_message( $query_var = '' ) { |
| 57 | |
| 58 | if ( empty( $query_var ) ) { |
| 59 | $query_var = self::DEFAULT_QUERY_VAR; |
| 60 | } |
| 61 | |
| 62 | //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No action is performed. |
| 63 | $key = sanitize_key( wp_unslash( $_GET[ $query_var ] ?? null ) ); |
| 64 | |
| 65 | if ( ! $key ) { |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | $content = self::get_transient_message( $key ); |
| 70 | |
| 71 | if ( ! $content ) { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | $content .= self::add_inline_script( $query_var ); |
| 76 | |
| 77 | UI::ensure_styles(); |
| 78 | |
| 79 | return $content; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Add a message for the next page load. |
| 84 | * |
| 85 | * @param string $message Message to be displayed on the next page load. |
| 86 | * |
| 87 | * @return string Transient key. |
| 88 | */ |
| 89 | private static function set_transient_message( $message ) { |
| 90 | $key = uniqid(); |
| 91 | |
| 92 | set_transient( self::TRANSIENT_PREFIX . $key, $message, 10 * MINUTE_IN_SECONDS ); |
| 93 | |
| 94 | return $key; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get a message for the given transient key, and optionally delete the transient. |
| 99 | * |
| 100 | * @param string $key Transient key. |
| 101 | * @param bool $delete Whether to delete the message after retrieving it. |
| 102 | * |
| 103 | * @return string|null Message to be displayed on the next page load. |
| 104 | */ |
| 105 | private static function get_transient_message( $key, $delete = true ) { |
| 106 | $content = get_transient( self::TRANSIENT_PREFIX . $key ); |
| 107 | |
| 108 | if ( ! is_string( $content ) ) { |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | if ( $delete ) { |
| 113 | delete_transient( self::TRANSIENT_PREFIX . $key ); |
| 114 | } |
| 115 | |
| 116 | return $content; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Add a small script to remove the redirect message query var from the URL upon page load. |
| 121 | * |
| 122 | * @param string $query_var Query var to remove. |
| 123 | * |
| 124 | * @return string Script HTML. |
| 125 | */ |
| 126 | private static function add_inline_script( $query_var ) { |
| 127 | $query_var = esc_js( $query_var ); |
| 128 | return <<<HTML |
| 129 | <script> |
| 130 | const url = new URL( location.href ); |
| 131 | url.searchParams.delete('{$query_var}'); |
| 132 | history.replaceState(null, '', url) |
| 133 | |
| 134 | </script> |
| 135 | HTML; |
| 136 | } |
| 137 | |
| 138 | } |
| 139 |