-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver-side-review-tracking-api.php
More file actions
156 lines (128 loc) · 4.29 KB
/
Copy pathserver-side-review-tracking-api.php
File metadata and controls
156 lines (128 loc) · 4.29 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Plugin Name: PUM API - Reviews
* Plugin URI:
* Description: An API endpoint to collect user review request results.
* Version: 1.0.0
* Author: danieliser
* Author URI: https://danieliser.com
* License: GPL2
*
* To use this please include the following credit block as well as completing the following TODOS.
*
* Original Author: danieliser
* Original Author URL: https://danieliser.com
*
* TODO Search & Replace prefix_ with your prefix
* TODO Search & Replace Prefix_ with your prefix
* TODO Search & Replace 'text-domain' with your 'text-domain'
*/
class Prefix_API_Reviews {
public static function init() {
add_action( 'rest_api_init', array( __CLASS__, 'register_endpoints' ) );
add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ) );
}
public static function register_endpoints() {
$version = 1;
$namespace = 'prefix/v' . $version;
register_rest_route( $namespace, '/review_action', array(
'methods' => 'POST',
'callback' => array( __CLASS__, 'review_action_endpoint' ),
'args' => array(
'uuid' => array(
'required' => true,
'description' => __( 'Unique Identifier' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'time' => array(
'description' => __( 'Timestamp' ),
'type' => 'string',
'default' => current_time( 'mysql' ),
'sanitize_callback' => 'sanitize_text_field',
),
'trigger_group' => array(
'required' => true,
'description' => __( 'Trigger Group' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'trigger_code' => array(
'required' => true,
'description' => __( 'Trigger Code' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'reason' => array(
'required' => true,
'description' => __( 'Reason' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
),
) );
}
public static function review_action_endpoint( \WP_REST_Request $request ) {
$params = $request->get_params();
if ( ! $params || empty( $params ) ) {
return new WP_Error( 'missing_params', __( 'Missing Parameters.' ), array( 'status' => 404 ) );
}
return static::insert_record( $params );
}
public static function insert_record( $values = array() ) {
global $wpdb;
$values = shortcode_atts( array(
'time' => current_time( 'mysql' ),
'trigger_group' => '',
'trigger_code' => '',
'reason' => '',
'uuid' => '',
), $values );
// Install / Upgrade table if needed.
static::install();
// Insert record.
$wpdb->insert( $wpdb->prefix_reviews, $values, array( '%s', '%s', '%s', '%s' ) );
return $wpdb->insert_id > 0 ? 1 : - 1;
}
public static function install() {
global $wpdb;
$version = 2;
$current_version = get_option( '_prefix_review_tracking_table_ver', 0 );
$wpdb->prefix_reviews = $wpdb->prefix . "prefix_reviews";
if ( $current_version < $version ) {
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $wpdb->prefix_reviews (
id bigint(20) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
trigger_group varchar(255) DEFAULT '' NOT NULL,
trigger_code varchar(255) DEFAULT '' NOT NULL,
uuid varchar(32) DEFAULT '' NOT NULL,
reason varchar(32) DEFAULT '' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
update_option( '_prefix_review_tracking_table_ver', $version );
}
}
public static function template_redirect() {
if ( ! isset( $_REQUEST['prefix-reviews'] ) ) {
return;
}
$args = wp_parse_args( $_REQUEST, array(
'time' => current_time( 'mysql' ),
'trigger_group' => '',
'trigger_code' => '',
'reason' => '',
'uuid' => '',
'redirect' => false,
) );
$tracked = static::insert_record( $args );
if ( $args['redirect'] ) {
wp_redirect( $args['redirect'] );
} else {
wp_send_json_success( $tracked );
}
}
}
add_action( 'plugins_loaded', array( 'Prefix_API_Reviews', 'init' ) );