PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.13
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.13
2.2.13 2.2.12 2.2.11 2.2.10 2.2.9 2.2.8 2.2.7 2.2.6 2.2.5 2.2.4 2.2.3 trunk 1.0.0 1.0.1 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2
table-builder-block / includes / Admin / Api / OnboardData.php

OnboardData.php in TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables 2.2.13, at includes/Admin/Api/OnboardData.php

151 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST endpoints for the plugin's onboarding flow
4 *
5 * @package TableKit
6 */
7
8 namespace TableBuilder\Admin\Api;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Registers and serves the tablebuilder/v1/onboard REST routes.
14 */
15 class OnboardData {
16 private const PLUGIN_SUBSCRIBE_URL = 'https://api.wpmet.com/public/plugin-subscribe/';
17
18 /**
19 * Hooks onboarding REST route registration into WordPress.
20 */
21 public function __construct() {
22 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
23 }
24
25 /**
26 * Sends the user's email to Wpmet's subscribe endpoint when onboarding completes.
27 *
28 * @param string $email User email to subscribe.
29 * @return void
30 */
31 private function send_email_subscribe_data( string $email ): void {
32 wp_remote_post(
33 self::PLUGIN_SUBSCRIBE_URL,
34 array(
35 'method' => 'POST',
36 'headers' => array(
37 'Accept' => '*/*',
38 'Content-Type' => 'application/json',
39 ),
40 'body' => wp_json_encode(
41 array(
42 'email' => $email,
43 'slug' => 'tablekit',
44 )
45 ),
46 )
47 );
48 }
49
50 /**
51 * Registers the tablebuilder/v1/onboard GET/POST REST routes.
52 *
53 * @return void
54 */
55 public function register_routes(): void {
56 register_rest_route(
57 'tablebuilder/v1',
58 'onboard',
59 array(
60 'methods' => \WP_REST_Server::READABLE,
61 'callback' => array( $this, 'action_get_onboard' ),
62 'permission_callback' => array( $this, 'is_request_allowed' ),
63 )
64 );
65
66 register_rest_route(
67 'tablebuilder/v1',
68 'onboard',
69 array(
70 'methods' => \WP_REST_Server::EDITABLE,
71 'callback' => array( $this, 'action_update_onboard' ),
72 'permission_callback' => array( $this, 'is_request_allowed' ),
73 )
74 );
75 }
76
77 /**
78 * Verifies nonce and capability. Used as permission_callback so
79 * WordPress returns a proper 401/403 status on failure.
80 *
81 * @param \WP_REST_Request $request Request; only its nonce header is used.
82 * @return true|\WP_Error
83 */
84 public function is_request_allowed( $request ) {
85 if ( ! wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' ) ) {
86 return new \WP_Error(
87 'rest_cookie_invalid_nonce',
88 __( 'Nonce mismatch.', 'table-builder-block' ),
89 array( 'status' => 403 )
90 );
91 }
92
93 if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
94 return new \WP_Error(
95 'rest_forbidden',
96 __( 'Access denied.', 'table-builder-block' ),
97 array( 'status' => 403 )
98 );
99 }
100
101 return true;
102 }
103
104 /**
105 * REST callback: get current onboarding completion status.
106 *
107 * @param \WP_REST_Request $request Unused; no params required.
108 * @return array{status:string,onboard:array{completed:bool,completedAt:string}}
109 */
110 public function action_get_onboard( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- required by the register_rest_route() "callback" signature; not needed in the body.
111 return array(
112 'status' => 'success',
113 'onboard' => array(
114 'completed' => (bool) get_option( 'tablebuilder_onboard_completed', false ),
115 'completedAt' => get_option( 'tablebuilder_onboard_completed_at', '' ),
116 ),
117 );
118 }
119
120 /**
121 * REST callback: mark onboarding completed/incomplete, optionally
122 * subscribing the given email when marking it completed.
123 *
124 * @param \WP_REST_Request $request Request with "completed" (bool) and optional "userMail" params.
125 * @return array{status:string,onboard:array{completed:bool,completedAt:string}}
126 */
127 public function action_update_onboard( $request ) {
128 $completed = (bool) $request->get_param( 'completed' );
129 $user_mail = sanitize_email( wp_unslash( (string) $request->get_param( 'userMail' ) ) );
130
131 update_option( 'tablebuilder_onboard_completed', $completed ? 1 : 0 );
132
133 if ( $completed ) {
134 if ( ! empty( $user_mail ) && is_email( $user_mail ) ) {
135 $this->send_email_subscribe_data( $user_mail );
136 }
137
138 update_option( 'tablebuilder_onboard_completed_at', current_time( 'mysql' ) );
139 delete_transient( 'tablebuilder_show_onboard' );
140 }
141
142 return array(
143 'status' => 'success',
144 'onboard' => array(
145 'completed' => $completed,
146 'completedAt' => get_option( 'tablebuilder_onboard_completed_at', '' ),
147 ),
148 );
149 }
150 }
151