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-notice.php
188 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Notice component for the frontend. |
| 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 | * Notice component for the frontend. |
| 17 | * |
| 18 | * @since 2.2.0 |
| 19 | * |
| 20 | * @internal This API is still under development and subject to change. |
| 21 | * @access private |
| 22 | */ |
| 23 | class Notice { |
| 24 | |
| 25 | /** |
| 26 | * A success message. |
| 27 | * |
| 28 | * @param string|array $args A message, or an array of options accepted by Notice::render. |
| 29 | */ |
| 30 | public static function success( $args ) { |
| 31 | $args = is_array( $args ) ? $args : [ 'message' => $args ]; |
| 32 | |
| 33 | return self::render( |
| 34 | array_merge( |
| 35 | [ 'icon' => 'check-circle' ], |
| 36 | $args |
| 37 | ) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * An error message. |
| 43 | * |
| 44 | * @param string|array $args A message, or an array of options accepted by Notice::render. |
| 45 | */ |
| 46 | public static function error( $args ) { |
| 47 | $args = is_array( $args ) ? $args : [ 'message' => $args ]; |
| 48 | |
| 49 | return self::render( |
| 50 | array_merge( |
| 51 | [ 'icon' => 'alert' ], |
| 52 | $args, |
| 53 | [ |
| 54 | 'classes' => array_merge( [ 'color-error' ], $args['classes'] ?? [] ), |
| 55 | ] |
| 56 | ) |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * A permanent informational message. |
| 62 | * |
| 63 | * @param string|array $args A message, or an array of options accepted by Notice::render. |
| 64 | */ |
| 65 | public static function hint( $args ) { |
| 66 | $args = is_array( $args ) ? $args : [ 'message' => $args ]; |
| 67 | |
| 68 | return self::render( |
| 69 | array_merge( |
| 70 | $args, |
| 71 | [ |
| 72 | 'classes' => array_merge( [ 'type-hint' ], $args['classes'] ?? [] ), |
| 73 | ], |
| 74 | ) |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * A permanent message that requires action to proceed. |
| 80 | * |
| 81 | * @param string|array $args |
| 82 | */ |
| 83 | public static function dialog( $args ) { |
| 84 | $args = is_array( $args ) ? $args : [ 'message' => $args ]; |
| 85 | |
| 86 | return self::render( |
| 87 | array_merge( |
| 88 | $args, |
| 89 | [ |
| 90 | 'classes' => array_merge( [ 'type-dialog' ], $args['classes'] ?? [] ), |
| 91 | ] |
| 92 | ) |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Notice element. |
| 98 | * |
| 99 | * @param array $options { |
| 100 | * Options for the notice. |
| 101 | * |
| 102 | * @type string $id Unique ID of the notice. |
| 103 | * @type string $title Optional title. |
| 104 | * @type string $icon Notice icon. Should be a supported icon name, or safe SVG code. |
| 105 | * @type string $message Main notice message. |
| 106 | * @type string $html Raw HTML content. User input must be escaped by the caller function. |
| 107 | * @type array $buttons Action links styled as buttons. |
| 108 | * @type array $links Action links. |
| 109 | * @type array $classes Additional classes for the notice. Possible options: |
| 110 | * color-error: Error-style coloring. |
| 111 | * color-success: Green highlight. |
| 112 | * color-info: Blue highlight. |
| 113 | * color-strong: Stronger border opacity highlight. |
| 114 | * type-hint: Variation for a persistent hint. |
| 115 | * message-icon: Show the icon next to the message instead of the title. |
| 116 | * alignwide: Wide notice, with actions on the side. |
| 117 | * } |
| 118 | * |
| 119 | * @return string HTML. |
| 120 | */ |
| 121 | public static function render( $options ) { |
| 122 | |
| 123 | UI::ensure_styles(); |
| 124 | |
| 125 | $options = wp_parse_args( |
| 126 | $options, |
| 127 | [ |
| 128 | 'id' => '', |
| 129 | 'title' => '', |
| 130 | 'icon' => '', |
| 131 | 'message' => '', |
| 132 | 'html' => '', |
| 133 | 'buttons' => [], |
| 134 | 'links' => [], |
| 135 | 'classes' => [], |
| 136 | ] |
| 137 | ); |
| 138 | |
| 139 | $actions = UI_Elements::actions( $options['buttons'], $options['links'] ); |
| 140 | |
| 141 | $icon = UI_Elements::icon( $options['icon'] ); |
| 142 | |
| 143 | ob_start(); |
| 144 | |
| 145 | $template_options = [ |
| 146 | 'options' => $options, |
| 147 | 'title' => $options['title'], |
| 148 | 'classes' => $options['classes'] ?? [], |
| 149 | 'message' => $options['message'], |
| 150 | 'content_html' => $options['html'], |
| 151 | 'icon_html' => $icon, |
| 152 | 'actions_html' => $actions, |
| 153 | ]; |
| 154 | |
| 155 | get_job_manager_template( |
| 156 | 'notice.php', |
| 157 | $template_options |
| 158 | ); |
| 159 | |
| 160 | $notice_html = ob_get_clean(); |
| 161 | |
| 162 | /** |
| 163 | * Filters notices. Return false to disable the notice. |
| 164 | * |
| 165 | * @since 2.2.0 |
| 166 | * |
| 167 | * @param string $notice_html Generated HTML for the notice. |
| 168 | * @param array $options Notice template options. |
| 169 | */ |
| 170 | $notice_html = apply_filters( 'wpjm_notice', $notice_html, $template_options ); |
| 171 | |
| 172 | if ( ! empty( $options['id'] ) ) { |
| 173 | /** |
| 174 | * Filters an individual notice. Return false to disable the notice. |
| 175 | * |
| 176 | * @since 2.2.0 |
| 177 | * |
| 178 | * @param string $notice_html Generated HTML for the notice. |
| 179 | * @param array $options Notice template options. |
| 180 | */ |
| 181 | $notice_html = apply_filters( 'wpjm_notice_' . $options['id'], $notice_html, $template_options ); |
| 182 | } |
| 183 | |
| 184 | return is_string( $notice_html ) ? $notice_html : ''; |
| 185 | } |
| 186 | |
| 187 | } |
| 188 |