| 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' => [ $this, 'is_request_allowed' ], |
| 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' => [ $this, 'is_request_allowed' ], |
| 51 |
] |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Verify nonce and capability. Used as permission_callback so |
| 57 |
* WordPress returns a proper 401/403 status on failure. |
| 58 |
* |
| 59 |
* @return true|\WP_Error |
| 60 |
*/ |
| 61 |
public function is_request_allowed( $request ) { |
| 62 |
if ( ! wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' ) ) { |
| 63 |
return new \WP_Error( |
| 64 |
'rest_cookie_invalid_nonce', |
| 65 |
__( 'Nonce mismatch.', 'table-builder-block' ), |
| 66 |
[ 'status' => 403 ] |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) { |
| 71 |
return new \WP_Error( |
| 72 |
'rest_forbidden', |
| 73 |
__( 'Access denied.', 'table-builder-block' ), |
| 74 |
[ 'status' => 403 ] |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
public function action_get_onboard( $request ) { |
| 82 |
return [ |
| 83 |
'status' => 'success', |
| 84 |
'onboard' => [ |
| 85 |
'completed' => (bool) get_option( 'tablebuilder_onboard_completed', false ), |
| 86 |
'completedAt' => get_option( 'tablebuilder_onboard_completed_at', '' ), |
| 87 |
], |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
public function action_update_onboard( $request ) { |
| 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 |
} |