This repository was archived by the owner on Jun 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-rest-api.php
185 lines (106 loc) · 5.42 KB
/
wp-rest-api.php
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* The class responsible for interacting with the Wordpress REST API
*
* @since 0.3.0
* @version 0.9.0
*/
defined( 'ABSPATH' ) or die( 'You do not have sufficient permissions to access this page.' );
add_action('plugins_loaded', array('Tfs_WP_REST_API', 'init') );
class Tfs_WP_REST_API {
public static function init() {
$tfs_rest_api = __CLASS__;
new $tfs_rest_api;
add_action('admin_enqueue_scripts', array( $tfs_rest_api, 'tfs_localize_wp_rest_api_object' ), 999 );
add_action('rest_api_init', array( $tfs_rest_api, 'tfs_wp_rest_api_validate_endpoint' ) );
}
public static function tfs_localize_wp_rest_api_object() {
//DEFINE THE NONCE AND THE API URL
wp_localize_script( 'tfs_woo_amz_int', 'tfs_woo_amz_int_object',
array(
'api_nonce' => wp_create_nonce( 'wp_rest' ),
'api_url' => site_url('/wp-json/rest/v3/')
)
);
}
public static function tfs_wp_rest_api_validate_endpoint() {
//declare a $namespace for the quick view endpoint
$namespace = 'rest/v3';
//register the rest api route
register_rest_route( $namespace, '/woo-amz-feed/', array(
'methods' => 'GET',
'callback' => function($request) {
$params = $request->get_params();
$dbman = new TFS_DB_MAN();
$feed_status = $dbman->tfs_check_product_feed_download_status();
$completed = 0;
if ( $feed_status !== NULL && $feed_status->completed ) {
$completed = $feed_status->completed;
}
if ( isset( $params['enabled'] ) && $params['enabled'] == TRUE ) {
if ( Woo_REST_API::$feed_running !== TRUE ) {
/**
* reset the feed and return continue_after_reset as true
* so the feed will reset from the next REST API call
*/
if ( $params['restart'] == "true" ) {
//get an instance of the file handler class
$file_handler = new Woo_Amz_File_Handler();
$file_handler->tfs_delete_inv_file();
$dbman->tfs_delete_row();
$dbman->tfs_insert_row( 0, 0, 0, 0 );
if ( $feed_status !== NULL ) {
$status = array(
'status' => $feed_status,
'continue_after_reset' => TRUE
);
return wp_json_encode( $status ); //wp_json_encode( $params );
}
/**
* Run the feed or continue its execution
*/
} elseif ( $params['run'] == "true" ) {
$init_woo_rest_api = new Woo_REST_API();
if ( $feed_status !== NULL ) {
$status = array(
'status' => $feed_status,
'continue_after_reset' => FALSE
);
return wp_json_encode( $status );
}
/**
* Submit feed to Amazon MWS
*/
} elseif ( $params['sendFeed'] == "true" ) {
$init_submit_feed = new TFS_MWS_FEED( 'SubmitFeed' );
return wp_json_encode( TFS_MWS_FEED::$submit_feed_response );
/**
* Check Feed Submission processing status
*/
} elseif ( $params['feedList'] == "true" ) {
$init_feed_list = new TFS_MWS_FEED( 'FeedList' );
return wp_json_encode( TFS_MWS_FEED::$feed_list_response );
/**
* Return the feed completion response
*/
} elseif ( $params['feedCompletionResponse'] == "true" ) {
$init_feed_result = new TFS_MWS_FEED( 'FeedResult' );
return wp_json_encode( TFS_MWS_FEED::$feed_result_response );
} else {
if ( $feed_status !== NULL ) {
$status = array(
'status' => $feed_status,
'continue_after_reset' => FALSE
);
return wp_json_encode( $status );
}
}
}
//continue the feed
}
} //end callback
) ); //end array()
}
public function tfs_endpoint_callback() {
}
} // End class Tfs_WP_REST_API