AsyncProductEditorCategoryField
3 years ago
Blueprint
11 months ago
Fulfillments
4 weeks ago
MarketingRecommendations
3 months ago
Navigation
1 year ago
OnboardingTasks
4 weeks ago
PaymentGatewaySuggestions
1 year ago
ProductBlockEditor
4 weeks ago
ProductDataViews
4 weeks ago
ShippingPartnerSuggestions
1 month ago
Features.php
4 weeks ago
LaunchYourStore.php
4 weeks ago
Onboarding.php
1 year ago
ProductVariationsClassicRedesign.php
4 weeks ago
TransientNotices.php
3 years ago
TransientNotices.php
124 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Transient Notices |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\Features; |
| 7 | |
| 8 | use Automattic\WooCommerce\Internal\Admin\Loader; |
| 9 | |
| 10 | /** |
| 11 | * Shows print shipping label banner on edit order page. |
| 12 | */ |
| 13 | class TransientNotices { |
| 14 | |
| 15 | /** |
| 16 | * Option name for the queue. |
| 17 | */ |
| 18 | const QUEUE_OPTION = 'woocommerce_admin_transient_notices_queue'; |
| 19 | |
| 20 | /** |
| 21 | * Constructor |
| 22 | */ |
| 23 | public function __construct() { |
| 24 | add_filter( 'woocommerce_admin_preload_options', array( $this, 'preload_options' ) ); |
| 25 | } |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * Get all notices in the queue. |
| 30 | * |
| 31 | * @return array |
| 32 | */ |
| 33 | public static function get_queue() { |
| 34 | return get_option( self::QUEUE_OPTION, array() ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get all notices in the queue by a given user ID. |
| 39 | * |
| 40 | * @param int $user_id User ID. |
| 41 | * @return array |
| 42 | */ |
| 43 | public static function get_queue_by_user( $user_id ) { |
| 44 | $notices = self::get_queue(); |
| 45 | |
| 46 | return array_filter( |
| 47 | $notices, |
| 48 | function( $notice ) use ( $user_id ) { |
| 49 | return ! isset( $notice['user_id'] ) || |
| 50 | null === $notice['user_id'] || |
| 51 | $user_id === $notice['user_id']; |
| 52 | } |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get a notice by ID. |
| 58 | * |
| 59 | * @param array $notice_id Notice of ID to get. |
| 60 | * @return array|null |
| 61 | */ |
| 62 | public static function get( $notice_id ) { |
| 63 | $queue = self::get_queue(); |
| 64 | |
| 65 | if ( isset( $queue[ $notice_id ] ) ) { |
| 66 | return $queue[ $notice_id ]; |
| 67 | } |
| 68 | |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Add a notice to be shown. |
| 74 | * |
| 75 | * @param array $notice Notice. |
| 76 | * $notice = array( |
| 77 | * 'id' => (string) Unique ID for the notice. Required. |
| 78 | * 'user_id' => (int|null) User ID to show the notice to. |
| 79 | * 'status' => (string) info|error|success |
| 80 | * 'content' => (string) Content to be shown for the notice. Required. |
| 81 | * 'options' => (array) Array of options to be passed to the notice component. |
| 82 | * See https://developer.wordpress.org/block-editor/reference-guides/data/data-core-notices/#createNotice for available options. |
| 83 | * ). |
| 84 | */ |
| 85 | public static function add( $notice ) { |
| 86 | $queue = self::get_queue(); |
| 87 | |
| 88 | $defaults = array( |
| 89 | 'user_id' => null, |
| 90 | 'status' => 'info', |
| 91 | 'options' => array(), |
| 92 | ); |
| 93 | $notice_data = array_merge( $defaults, $notice ); |
| 94 | $notice_data['options'] = (object) $notice_data['options']; |
| 95 | |
| 96 | $queue[ $notice['id'] ] = $notice_data; |
| 97 | update_option( self::QUEUE_OPTION, $queue ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Remove a notice by ID. |
| 102 | * |
| 103 | * @param array $notice_id Notice of ID to remove. |
| 104 | */ |
| 105 | public static function remove( $notice_id ) { |
| 106 | $queue = self::get_queue(); |
| 107 | unset( $queue[ $notice_id ] ); |
| 108 | update_option( self::QUEUE_OPTION, $queue ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Preload options to prime state of the application. |
| 113 | * |
| 114 | * @param array $options Array of options to preload. |
| 115 | * @return array |
| 116 | */ |
| 117 | public function preload_options( $options ) { |
| 118 | $options[] = self::QUEUE_OPTION; |
| 119 | |
| 120 | return $options; |
| 121 | } |
| 122 | |
| 123 | } |
| 124 |