PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.7
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.7
2.2.14 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.7, at includes/Admin/Api/OnboardData.php

114 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TableBuilder\Admin\Api;
4
5 defined( 'ABSPATH' ) || exit;
6
7 class OnboardData {
8 private const PLUGIN_SUBSCRIBE_URL = 'https://api.wpmet.com/public/plugin-subscribe/';
9
10 public function __construct() {
11 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
12 }
13
14 private function send_email_subscribe_data( string $email ): void {
15 wp_remote_post(
16 self::PLUGIN_SUBSCRIBE_URL,
17 [
18 'method' => 'POST',
19 'headers' => [
20 'Accept' => '*/*',
21 'Content-Type' => 'application/json',
22 ],
23 'body' => wp_json_encode(
24 [
25 'email' => $email,
26 'slug' => 'tablekit',
27 ]
28 ),
29 ]
30 );
31 }
32
33 public function register_routes(): void {
34 register_rest_route(
35 'tablebuilder/v1',
36 'onboard',
37 [
38 'methods' => \WP_REST_Server::READABLE,
39 'callback' => [ $this, 'action_get_onboard' ],
40 'permission_callback' => '__return_true',
41 ]
42 );
43
44 register_rest_route(
45 'tablebuilder/v1',
46 'onboard',
47 [
48 'methods' => \WP_REST_Server::EDITABLE,
49 'callback' => [ $this, 'action_update_onboard' ],
50 'permission_callback' => '__return_true',
51 ]
52 );
53 }
54
55 private function is_request_allowed( $request ): bool {
56 if ( ! wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' ) ) {
57 return false;
58 }
59
60 if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
61 return false;
62 }
63
64 return true;
65 }
66
67 public function action_get_onboard( $request ) {
68 if ( ! $this->is_request_allowed( $request ) ) {
69 return [
70 'status' => 'fail',
71 'message' => [ __( 'Access denied.', 'table-builder-block' ) ],
72 ];
73 }
74
75 return [
76 'status' => 'success',
77 'onboard' => [
78 'completed' => (bool) get_option( 'tablebuilder_onboard_completed', false ),
79 'completedAt' => get_option( 'tablebuilder_onboard_completed_at', '' ),
80 ],
81 ];
82 }
83
84 public function action_update_onboard( $request ) {
85 if ( ! $this->is_request_allowed( $request ) ) {
86 return [
87 'status' => 'fail',
88 'message' => [ __( 'Access denied.', 'table-builder-block' ) ],
89 ];
90 }
91
92 $completed = (bool) $request->get_param( 'completed' );
93 $user_mail = sanitize_email( wp_unslash( (string) $request->get_param( 'userMail' ) ) );
94
95 update_option( 'tablebuilder_onboard_completed', $completed ? 1 : 0 );
96
97 if ( $completed ) {
98 if ( ! empty( $user_mail ) && is_email( $user_mail ) ) {
99 $this->send_email_subscribe_data( $user_mail );
100 }
101
102 update_option( 'tablebuilder_onboard_completed_at', current_time( 'mysql' ) );
103 delete_transient( 'tablebuilder_show_onboard' );
104 }
105
106 return [
107 'status' => 'success',
108 'onboard' => [
109 'completed' => $completed,
110 'completedAt' => get_option( 'tablebuilder_onboard_completed_at', '' ),
111 ],
112 ];
113 }
114 }