PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.81
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.81
5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 5.5.43 All 159 releases
wp-data-access / WPDataAccess / API / WPDA_Export.php

WPDA_Export.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.81, at WPDataAccess/API/WPDA_Export.php

194 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDataAccess\API {
4
5 use WPDataAccess\Drive\WPDA_Drives;
6 use WPDataAccess\Utilities\WPDA_Export_Scheduler;
7 use WPDataAccess\Utilities\WPDA_Mail;
8 use WPDataAccess\WPDA;
9
10 class WPDA_Export extends WPDA_API_Core {
11
12 public function register_rest_routes() {
13
14 register_rest_route(
15 WPDA_API::WPDA_NAMESPACE,
16 'export/cron/schedules',
17 array(
18 'methods' => array( 'POST' ),
19 'callback' => array( $this, 'cron_schedules' ),
20 'permission_callback' => '__return_true',
21 'args' => array(),
22 )
23 );
24
25 register_rest_route(
26 WPDA_API::WPDA_NAMESPACE,
27 'export/cron/add',
28 array(
29 'methods' => array( 'POST' ),
30 'callback' => array( $this, 'cron_add' ),
31 'permission_callback' => '__return_true',
32 'args' => array(
33 'name' => $this->get_param( 'name' ),
34 'params' => $this->get_param( 'params' ),
35 'start' => array(
36 'required' => true,
37 'type' => 'integer',
38 'description' => __( 'Start date/time', 'wp-data-access' ),
39 'sanitize_callback' => 'sanitize_text_field',
40 'validate_callback' => 'rest_validate_request_arg',
41 ),
42 'interval' => array(
43 'required' => true,
44 'type' => 'string',
45 'description' => __( 'Recurrence', 'wp-data-access' ),
46 'sanitize_callback' => 'sanitize_text_field',
47 'validate_callback' => 'rest_validate_request_arg',
48 ),
49 ),
50 )
51 );
52
53 register_rest_route(
54 WPDA_API::WPDA_NAMESPACE,
55 'export/cron/delete',
56 array(
57 'methods' => array( 'POST' ),
58 'callback' => array( $this, 'cron_delete' ),
59 'permission_callback' => '__return_true',
60 'args' => array(
61 'name' => $this->get_param( 'name' ),
62 ),
63 )
64 );
65
66 }
67
68 public function cron_schedules( $request ) {
69
70 if ( ! $this->current_user_can_access() ) {
71 return $this->unauthorized();
72 }
73
74 if ( ! $this->current_user_token_valid( $request ) ) {
75 return $this->invalid_nonce();
76 }
77
78 return $this->WPDA_Rest_Response(
79 array(
80 'cron' => WPDA_Export_Scheduler::wpda_cron_events(),
81 'schedules' => wp_get_schedules(),
82 'drives' => WPDA_Drives::get_drive_names( true ),
83 'mail' => WPDA_Mail::mail_activated(),
84 )
85 );
86
87 }
88
89 public function cron_add( $request ) {
90
91 if ( ! $this->current_user_can_access() ) {
92 return $this->unauthorized();
93 }
94
95 if ( ! $this->current_user_token_valid( $request ) ) {
96 return $this->invalid_nonce();
97 }
98
99 $name = $request->get_param('name');
100 $params = $request->get_param('params');
101 $start = $request->get_param('start');
102 $interval = $request->get_param('interval');
103
104 if ( false !== WPDA_Export_Scheduler::get_export( $name ) ) {
105 return new \WP_Error(
106 'error',
107 'An export with this name is already scheduled',
108 array('status' => 403)
109 );
110 }
111
112 $args = array(
113 'args' => array(
114 'name' => $name,
115 'params' => $params,
116 )
117 );
118
119 if ('nonrepeating' === $interval) {
120 $error = wp_schedule_single_event(
121 $start,
122 WPDA_Export_Scheduler::SCHEDULER_HOOK_NAME,
123 $args
124 );
125 } else {
126 $error = wp_schedule_event(
127 $start,
128 $interval,
129 WPDA_Export_Scheduler::SCHEDULER_HOOK_NAME,
130 $args
131 );
132 }
133
134 if (is_wp_error($error)) {
135 return new \WP_Error(
136 'error',
137 $error->get_error_message(),
138 array('status' => 403)
139 );
140 } else {
141 return $this->WPDA_Rest_Response(
142 'Successfully scheduled export'
143 );
144 }
145
146 }
147
148 public function cron_delete( $request ) {
149
150 if ( ! $this->current_user_can_access() ) {
151 return $this->unauthorized();
152 }
153
154 if ( ! $this->current_user_token_valid( $request ) ) {
155 return $this->invalid_nonce();
156 }
157
158 $name = $request->get_param('name');
159 $jobs = WPDA_Export_Scheduler::get_export( $name );
160
161 if ( false === $jobs ) {
162 return new \WP_Error(
163 'error',
164 'Export with name "' . $name . '" is not found',
165 array('status' => 403)
166 );
167 } else {
168 $error = wp_unschedule_event(
169 $jobs['time_stamp'],
170 WPDA_Export_Scheduler::SCHEDULER_HOOK_NAME,
171 $jobs['args'],
172 true
173 );
174
175 if ( is_wp_error( $error ) ) {
176 return new \WP_Error(
177 'error',
178 $error->get_error_message(),
179 array('status' => 403)
180 );
181 }
182
183 return $this->WPDA_Rest_Response(
184 'Successfully deleted export',
185 null,
186 $error
187 );
188 }
189
190 }
191
192 }
193
194 }