woocommerce-multilingual
/
addons
/
wpml-dependencies
/
lib
/
classes
/
notices
/
class-wpml-notice.php
pages
4 years ago
DismissNotices.php
4 weeks ago
class-wpml-notice-action.php
4 weeks ago
class-wpml-notice-render.php
4 weeks ago
class-wpml-notice.php
4 weeks ago
class-wpml-notices.php
4 weeks ago
class-wpml-notice.php
282 lines
| 1 | <?php |
| 2 | |
| 3 | class WPML_Notice { |
| 4 | private $display_callbacks = array(); |
| 5 | private $id; |
| 6 | private $text; |
| 7 | private $collapsed_text; |
| 8 | private $group = 'default'; |
| 9 | private $restricted_to_user_ids = array(); |
| 10 | |
| 11 | private $actions = array(); |
| 12 | private $css_class_types = array(); |
| 13 | private $css_classes = array(); |
| 14 | private $dismissible = false; |
| 15 | private $exclude_from_pages = array(); |
| 16 | private $hideable = false; |
| 17 | private $collapsable = false; |
| 18 | private $restrict_to_pages = array(); |
| 19 | private $restrict_to_page_prefixes = array(); |
| 20 | private $restrict_to_screen_ids = array(); |
| 21 | private $hide_if_notice_exists = null; |
| 22 | private $dismissible_for_different_text = true; |
| 23 | |
| 24 | private $default_group_name = 'default'; |
| 25 | |
| 26 | private $capabilities = array(); |
| 27 | |
| 28 | private $dismiss_reset = false; |
| 29 | |
| 30 | private $flash = false; |
| 31 | |
| 32 | private $nonce_action; |
| 33 | |
| 34 | private $text_only = false; |
| 35 | |
| 36 | public function __construct( $id, $text, $group = 'default' ) { |
| 37 | $this->id = $id; |
| 38 | $this->text = $text; |
| 39 | $this->group = $group ? $group : $this->default_group_name; |
| 40 | } |
| 41 | |
| 42 | public function add_action( WPML_Notice_Action $action ) { |
| 43 | $this->actions[] = $action; |
| 44 | |
| 45 | if ( $action->can_dismiss() ) { |
| 46 | $this->dismissible = true; |
| 47 | } |
| 48 | if ( ! $action->can_dismiss_different_text() ) { |
| 49 | $this->dismissible_for_different_text = false; |
| 50 | } |
| 51 | if ( $action->can_hide() ) { |
| 52 | $this->hideable = true; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public function add_exclude_from_page( $page ) { |
| 57 | $this->exclude_from_pages[] = $page; |
| 58 | } |
| 59 | |
| 60 | public function add_restrict_to_page( $page ) { |
| 61 | $this->restrict_to_pages[] = $page; |
| 62 | } |
| 63 | |
| 64 | public function add_user_restriction( $user_id ) { |
| 65 | $user_id = (int) $user_id; |
| 66 | $this->restricted_to_user_ids[ $user_id ] = $user_id; |
| 67 | } |
| 68 | |
| 69 | public function remove_user_restriction( $user_id ) { |
| 70 | unset( $this->restricted_to_user_ids[ (int) $user_id ] ); |
| 71 | } |
| 72 | |
| 73 | public function get_restricted_user_ids() { |
| 74 | return $this->restricted_to_user_ids; |
| 75 | } |
| 76 | |
| 77 | public function is_user_restricted() { |
| 78 | return (bool) $this->restricted_to_user_ids; |
| 79 | } |
| 80 | |
| 81 | public function is_for_current_user() { |
| 82 | return ! $this->restricted_to_user_ids |
| 83 | || array_key_exists( get_current_user_id(), $this->restricted_to_user_ids ); |
| 84 | } |
| 85 | |
| 86 | public function is_user_cap_allowed() { |
| 87 | $user_can = true; |
| 88 | foreach ( $this->capabilities as $cap ) { |
| 89 | $user_can = current_user_can( $cap ); |
| 90 | |
| 91 | if ( $user_can ) { |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return $user_can; |
| 97 | } |
| 98 | |
| 99 | public function can_be_dismissed() { |
| 100 | return $this->dismissible; |
| 101 | } |
| 102 | |
| 103 | public function can_be_dismissed_for_different_text() { |
| 104 | return $this->dismissible_for_different_text; |
| 105 | } |
| 106 | |
| 107 | public function can_be_hidden() { |
| 108 | return $this->hideable; |
| 109 | } |
| 110 | |
| 111 | public function can_be_collapsed() { |
| 112 | return $this->collapsable; |
| 113 | } |
| 114 | |
| 115 | public function add_display_callback( $callback ) { |
| 116 | if ( ! is_callable( $callback ) ) { |
| 117 | throw new UnexpectedValueException( '\WPML_Notice::add_display_callback expects a callable', 1 ); |
| 118 | } |
| 119 | $this->display_callbacks[] = $callback; |
| 120 | } |
| 121 | |
| 122 | public function add_capability_check( array $cap ) { |
| 123 | $this->capabilities = $cap; |
| 124 | } |
| 125 | |
| 126 | public function get_display_callbacks() { |
| 127 | $isNotIncompleteObject = function( $callback ) { |
| 128 | return ! ( isset( $callback[0] ) && $callback[0] instanceof __PHP_Incomplete_Class ); |
| 129 | }; |
| 130 | |
| 131 | return array_filter( $this->display_callbacks, $isNotIncompleteObject ); |
| 132 | } |
| 133 | |
| 134 | public function get_actions() { |
| 135 | return $this->actions; |
| 136 | } |
| 137 | |
| 138 | public function get_css_classes() { |
| 139 | return $this->css_classes; |
| 140 | } |
| 141 | |
| 142 | public function set_css_classes( $css_classes ) { |
| 143 | if ( ! is_array( $css_classes ) ) { |
| 144 | $css_classes = explode( ' ', $css_classes ); |
| 145 | } |
| 146 | $this->css_classes = $css_classes; |
| 147 | } |
| 148 | |
| 149 | public function get_exclude_from_pages() { |
| 150 | return $this->exclude_from_pages; |
| 151 | } |
| 152 | |
| 153 | public function get_group() { |
| 154 | return $this->group; |
| 155 | } |
| 156 | |
| 157 | public function get_id() { |
| 158 | return $this->id; |
| 159 | } |
| 160 | |
| 161 | public function set_restrict_to_page_prefixes( array $page_prefixes ) { |
| 162 | $this->restrict_to_page_prefixes = $page_prefixes; |
| 163 | } |
| 164 | |
| 165 | public function get_restrict_to_page_prefixes() { |
| 166 | return $this->restrict_to_page_prefixes; |
| 167 | } |
| 168 | |
| 169 | public function get_restrict_to_pages() { |
| 170 | return $this->restrict_to_pages; |
| 171 | } |
| 172 | |
| 173 | public function set_restrict_to_screen_ids( array $screens ) { |
| 174 | $this->restrict_to_screen_ids = $screens; |
| 175 | } |
| 176 | |
| 177 | public function get_restrict_to_screen_ids() { |
| 178 | return $this->restrict_to_screen_ids; |
| 179 | } |
| 180 | |
| 181 | public function get_nonce_action() { |
| 182 | return $this->nonce_action; |
| 183 | } |
| 184 | |
| 185 | public function get_text() { |
| 186 | $notice = array( |
| 187 | 'id' => $this->get_id(), |
| 188 | 'group' => $this->get_group(), |
| 189 | ); |
| 190 | $this->text = apply_filters( 'wpml_notice_text', $this->text, $notice ); |
| 191 | |
| 192 | return $this->text; |
| 193 | } |
| 194 | |
| 195 | public function get_css_class_types() { |
| 196 | return $this->css_class_types; |
| 197 | } |
| 198 | |
| 199 | public function get_collapsed_text() { |
| 200 | return $this->collapsed_text; |
| 201 | } |
| 202 | |
| 203 | public function set_css_class_types( $types ) { |
| 204 | $this->css_class_types = is_array( $types ) ? $types : explode( ' ', $types ); |
| 205 | |
| 206 | return $this; |
| 207 | } |
| 208 | |
| 209 | public function set_dismissible( $dismissible ) { |
| 210 | $this->dismissible = $dismissible; |
| 211 | } |
| 212 | |
| 213 | public function set_exclude_from_pages( array $pages ) { |
| 214 | $this->exclude_from_pages = $pages; |
| 215 | } |
| 216 | |
| 217 | public function set_hide_if_notice_exists( $notice_id, $notice_group = null ) { |
| 218 | $this->hide_if_notice_exists = array( |
| 219 | 'id' => $notice_id, |
| 220 | 'group' => $notice_group, |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | public function get_hide_if_notice_exists() { |
| 225 | return $this->hide_if_notice_exists; |
| 226 | } |
| 227 | |
| 228 | public function set_hideable( $hideable ) { |
| 229 | $this->hideable = $hideable; |
| 230 | } |
| 231 | |
| 232 | public function set_collapsable( $collapsable ) { |
| 233 | $this->collapsable = $collapsable; |
| 234 | } |
| 235 | |
| 236 | public function set_nonce_action( $action ) { |
| 237 | $this->nonce_action = $action; |
| 238 | } |
| 239 | |
| 240 | public function set_collapsed_text( $collapsed_text ) { |
| 241 | $this->collapsed_text = $collapsed_text; |
| 242 | } |
| 243 | |
| 244 | public function set_restrict_to_pages( array $pages ) { |
| 245 | $this->restrict_to_pages = $pages; |
| 246 | } |
| 247 | |
| 248 | public function reset_dismiss() { |
| 249 | $this->dismiss_reset = true; |
| 250 | } |
| 251 | |
| 252 | public function must_reset_dismiss() { |
| 253 | return $this->dismiss_reset; |
| 254 | } |
| 255 | |
| 256 | public function is_different( WPML_Notice $other_notice ) { |
| 257 | return serialize( $this ) !== serialize( $other_notice ); |
| 258 | } |
| 259 | |
| 260 | public function set_flash( $flash = true ) { |
| 261 | $this->flash = (bool) $flash; |
| 262 | |
| 263 | return $this; |
| 264 | } |
| 265 | |
| 266 | public function is_flash() { |
| 267 | return $this->flash; |
| 268 | } |
| 269 | |
| 270 | public function should_be_text_only() { |
| 271 | return $this->text_only; |
| 272 | } |
| 273 | |
| 274 | public function set_text_only( $text_only ) { |
| 275 | $this->text_only = $text_only; |
| 276 | } |
| 277 | |
| 278 | public static function make( $id, $text, $group = 'default' ) { |
| 279 | return new WPML_Notice( $id, $text, $group ); |
| 280 | } |
| 281 | } |
| 282 |