1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
<?php /** * REST API Notice controller * * Handles requests to /notice/ */
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Admin\PluginsHelper;
defined( 'ABSPATH' ) || exit;
/** * Notice Controller. * * @internal * @extends WC_REST_Data_Controller */ class Notice extends \WC_REST_Data_Controller {
/** * Endpoint namespace. * * @var string */ protected $namespace = 'wc-admin';
/** * Route base. * * @var string */ protected $rest_base = 'notice';
/** * Register the routes for admin notes. */ public function register_routes() { register_rest_route( $this->namespace, '/' . $this->rest_base . '/dismiss', array( array( 'methods' => 'POST', 'callback' => array( $this, 'dissmiss_notice' ), 'permission_callback' => array( $this, 'get_permission' ), ), ) ); }
/** * Save notice dismiss information in user meta. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error */ public function dissmiss_notice( $request ) { if ( ! isset( $request['dismiss_notice_nonce'] ) || ! wp_verify_nonce( $request['dismiss_notice_nonce'], 'dismiss_notice' ) ) { return new WP_Error( 'unauthorized', 'Invalid nonce.', array( 'status' => 401 ) ); } $notice_id = isset( $request['notice_id'] ) ? sanitize_text_field( wp_unslash( $request['notice_id'] ) ) : ''; $dismissed = false; switch ( $notice_id ) { case 'woo-subscription-expired-notice': update_user_meta( get_current_user_id(), PluginsHelper::DISMISS_EXPIRED_SUBS_NOTICE, time() ); $dismissed = true; break; case 'woo-subscription-expiring-notice': update_user_meta( get_current_user_id(), PluginsHelper::DISMISS_EXPIRING_SUBS_NOTICE, time() ); $dismissed = true; break; case 'woo-disconnect-notice': update_user_meta( get_current_user_id(), PluginsHelper::DISMISS_DISCONNECT_NOTICE, time() ); $dismissed = true; break; }
return rest_ensure_response( array( 'success' => $dismissed, ) ); }
/** * Check user has the necessary permissions to perform this action. * * @return bool */ public function get_permission(): bool { return current_user_can( 'manage_woocommerce' ); } }
|