PluginProbe
Tiered Pricing Table for WooCommerce / trunk
Tiered Pricing Table for WooCommerce vtrunk
7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 All 90 releases
tier-pricing-table / src / Addons / Tools / API / ToolsEndpoints.php

ToolsEndpoints.php in Tiered Pricing Table for WooCommerce trunk, at src/Addons/Tools/API/ToolsEndpoints.php

404 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\Tools\API;
2
3 use TierPricingTable\Addons\GlobalTieredPricing\CPT\GlobalTieredPricingCPT;
4 use WP_REST_Request;
5
6 class ToolsEndpoints {
7
8 const NAMESPACE = 'tier-pricing-table/features/tools/v1';
9
10 public function __construct() {
11 add_action( 'rest_api_init', array( $this, 'registerRoutes' ) );
12 }
13
14 public function registerRoutes() {
15
16 register_rest_route( self::NAMESPACE, '/cleanup', array(
17 array(
18 'methods' => 'POST',
19 'callback' => array( $this, 'cleanupData' ),
20 'permission_callback' => function () {
21 return current_user_can( 'manage_options' );
22 },
23 ),
24 ) );
25
26 register_rest_route( self::NAMESPACE, '/reset_settings', array(
27 array(
28 'methods' => 'POST',
29 'callback' => array( $this, 'resetSettings' ),
30 'permission_callback' => function () {
31 return current_user_can( 'manage_options' );
32 },
33 ),
34 ) );
35
36 register_rest_route( self::NAMESPACE, '/delete_global_rules', array(
37 array(
38 'methods' => 'POST',
39 'callback' => array( $this, 'deleteGlobalRules' ),
40 'permission_callback' => function () {
41 return current_user_can( 'manage_options' );
42 },
43 ),
44 ) );
45
46 register_rest_route( self::NAMESPACE, '/roles', array(
47 array(
48 'methods' => 'GET',
49 'callback' => array( $this, 'getRoles' ),
50 'permission_callback' => function () {
51 return current_user_can( 'manage_options' );
52 },
53 ),
54 ) );
55
56 register_rest_route( self::NAMESPACE, '/roles_capabilities', array(
57 array(
58 'methods' => 'GET',
59 'callback' => array( $this, 'getRolesAndCapabilities' ),
60 'permission_callback' => function () {
61 return current_user_can( 'manage_options' );
62 },
63 ),
64 array(
65 'methods' => 'POST',
66 'callback' => array( $this, 'createRole' ),
67 'permission_callback' => function () {
68 return current_user_can( 'manage_options' );
69 },
70 ),
71 array(
72 'methods' => 'PUT',
73 'callback' => array( $this, 'updateRoleCapabilities' ),
74 'permission_callback' => function () {
75 return current_user_can( 'manage_options' );
76 },
77 ),
78 array(
79 'methods' => 'DELETE',
80 'callback' => array( $this, 'deleteRole' ),
81 'permission_callback' => function () {
82 return current_user_can( 'manage_options' );
83 },
84 ),
85 ) );
86 }
87
88 public function getRoles() {
89 global $wp_roles;
90 if ( ! isset( $wp_roles ) ) {
91 $wp_roles = new \WP_Roles();
92 }
93 $roles = $wp_roles->get_names();
94
95 $response = array();
96 foreach ( $roles as $key => $name ) {
97 $response[] = array(
98 'value' => $key,
99 'label' => $name,
100 );
101 }
102
103 return rest_ensure_response( $response );
104 }
105
106 public function cleanupData( WP_REST_Request $request ) {
107 $cleanStandard = filter_var( $request->get_param( 'clean_standard' ), FILTER_VALIDATE_BOOLEAN );
108 $cleanUsers = filter_var( $request->get_param( 'clean_users' ), FILTER_VALIDATE_BOOLEAN );
109 $cleanAllRoles = filter_var( $request->get_param( 'clean_all_roles' ), FILTER_VALIDATE_BOOLEAN );
110
111 if ( $cleanAllRoles ) {
112 global $wp_roles;
113 if ( ! isset( $wp_roles ) ) {
114 $wp_roles = new \WP_Roles();
115 }
116 $roles = array_keys( $wp_roles->get_names() );
117 } else {
118 $roles = $request->get_param( 'roles' ); // array of role keys
119 }
120
121 $categories = $request->get_param( 'categories' ); // array of term ids
122 $products = $request->get_param( 'products' ); // array of product ids
123
124 $metaKeysToDelete = array();
125
126 if ( $cleanStandard ) {
127 $metaKeysToDelete = array_merge( $metaKeysToDelete, array(
128 '_fixed_price_rules',
129 '_percentage_price_rules',
130 '_tiered_price_rules_type',
131 '_tiered_price_minimum_qty',
132 '_tiered_pricing_maximum_quantity',
133 '_tiered_pricing_group_of_quantity',
134 '_tiered_pricing_template',
135 '_tiered_pricing_base_unit_name',
136 '_tiered_pricing_default_variation_id',
137 '_tiered_pricing_variable_product_same_prices',
138 ) );
139 }
140
141 if ( ! empty( $roles ) && is_array( $roles ) ) {
142 foreach ( $roles as $roleKey ) {
143 $roleKey = sanitize_key( $roleKey );
144 $metaKeysToDelete = array_merge( $metaKeysToDelete, array(
145 "_{$roleKey}_tiered_price_pricing_type",
146 "_{$roleKey}_tiered_price_regular_price",
147 "_{$roleKey}_tiered_price_sale_price",
148 "_{$roleKey}_tiered_price_discount",
149 "_{$roleKey}_tiered_price_discount_type",
150 "_{$roleKey}_tiered_price_rules_type",
151 "_{$roleKey}_percentage_price_rules",
152 "_{$roleKey}_fixed_price_rules",
153 "_{$roleKey}_tiered_price_minimum_qty",
154 "_{$roleKey}_tiered_pricing_maximum_quantity",
155 "_{$roleKey}_tiered_pricing_group_of_quantity",
156 ) );
157 }
158 }
159
160 $deletedCount = 0;
161
162 if ( ! empty( $metaKeysToDelete ) ) {
163 global $wpdb;
164 $metaKeysIn = "'" . implode( "','", array_map( 'esc_sql', $metaKeysToDelete ) ) . "'";
165
166 $postIds = array();
167
168 if ( ! empty( $products ) && is_array( $products ) ) {
169 $postIds = array_map( 'intval', $products );
170 }
171
172 if ( ! empty( $categories ) && is_array( $categories ) ) {
173 $categoryIds = array_map( 'intval', $categories );
174 $args = array(
175 'limit' => - 1,
176 'return' => 'ids',
177 'category' => array_map( function ( $id ) {
178 $term = get_term( $id, 'product_cat' );
179
180 return $term ? $term->slug : '';
181 }, $categoryIds ),
182 );
183
184 $catProducts = wc_get_products( $args );
185 $postIds = array_unique( array_merge( $postIds, $catProducts ) );
186 }
187
188 if ( empty( $products ) && empty( $categories ) ) {
189 $query = "DELETE FROM {$wpdb->postmeta} WHERE meta_key IN ($metaKeysIn)";
190 $deletedCount = $wpdb->query( $query );
191 } else {
192 if ( ! empty( $postIds ) ) {
193 $postIdsIn = implode( ',', $postIds );
194 $query = "DELETE FROM {$wpdb->postmeta} WHERE meta_key IN ($metaKeysIn) AND post_id IN ($postIdsIn)";
195 $deletedCount = $wpdb->query( $query );
196 }
197 }
198
199 if ( $deletedCount === false ) {
200 return rest_ensure_response( array(
201 'success' => false,
202 'message' => 'Database error during cleanup.',
203 ) );
204 }
205 }
206
207 if ( $cleanUsers ) {
208 global $wpdb;
209
210 $userMetaLikeQueries = array(
211 "meta_key LIKE '\_user\_%\_tiered_price\_pricing\_type'",
212 "meta_key LIKE '\_user\_%\_tiered_price\_regular\_price'",
213 "meta_key LIKE '\_user\_%\_tiered_price\_sale\_price'",
214 "meta_key LIKE '\_user\_%\_tiered_price\_discount'",
215 "meta_key LIKE '\_user\_%\_tiered_price\_discount\_type'",
216 "meta_key LIKE '\_user\_%\_tiered_price\_rules\_type'",
217 "meta_key LIKE '\_user\_%\_percentage\_price\_rules'",
218 "meta_key LIKE '\_user\_%\_fixed\_price\_rules'",
219 "meta_key LIKE '\_user\_%\_tiered_price\_minimum\_qty'",
220 "meta_key LIKE '\_user\_%\_tiered_price\_tax\_status'",
221 "meta_key LIKE '\_user\_%\_tiered_price\_tax\_class'"
222 );
223
224 $likeQuery = implode( ' OR ', $userMetaLikeQueries );
225
226 if ( empty( $products ) && empty( $categories ) ) {
227 $query = "DELETE FROM {$wpdb->postmeta} WHERE ($likeQuery)";
228 $userDeletedCount = $wpdb->query( $query );
229 } else {
230 if ( ! empty( $postIds ) ) {
231 $postIdsIn = implode( ',', $postIds );
232 $query = "DELETE FROM {$wpdb->postmeta} WHERE ($likeQuery) AND post_id IN ($postIdsIn)";
233 $userDeletedCount = $wpdb->query( $query );
234 }
235 }
236
237 if ( $userDeletedCount !== false ) {
238 $deletedCount += $userDeletedCount;
239 }
240 }
241
242 delete_option( \TierPricingTable\Admin\Tips\Tip::SEEN_TIPS_OPTION_KEY );
243
244 $message = sprintf( 'Successfully cleaned up %d product records.', $deletedCount );
245
246 return rest_ensure_response( array(
247 'success' => true,
248 'deleted_rows' => $deletedCount,
249 'message' => $message,
250 ) );
251 }
252
253 public function resetSettings( WP_REST_Request $request ) {
254 global $wpdb;
255
256 if ( class_exists( '\TierPricingTable\Settings\Settings' ) ) {
257 \TierPricingTable\Settings\Settings::deleteOptions();
258 }
259
260 // Wipe all other plugin settings that might be stored via different addons
261 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'tier_pricing_table_%'" );
262 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'tiered_pricing_table/%'" );
263 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'tpt_custom_table_columns%'" );
264
265 return rest_ensure_response( array(
266 'success' => true,
267 'message' => 'Plugin settings were successfully reset to defaults.',
268 ) );
269 }
270
271 public function deleteGlobalRules( WP_REST_Request $request ) {
272 $globalRules = get_posts( array(
273 'numberposts' => - 1,
274 'post_type' => GlobalTieredPricingCPT::SLUG,
275 'post_status' => 'any',
276 'fields' => 'ids',
277 ) );
278
279 $deletedCount = 0;
280 foreach ( $globalRules as $ruleId ) {
281 if ( wp_delete_post( $ruleId, true ) ) {
282 $deletedCount ++;
283 }
284 }
285
286 return rest_ensure_response( array(
287 'success' => true,
288 'deleted_rows' => $deletedCount,
289 'message' => sprintf( 'Successfully deleted %d global rules.', $deletedCount ),
290 ) );
291 }
292
293 public function getRolesAndCapabilities() {
294 global $wp_roles;
295 if ( ! isset( $wp_roles ) ) {
296 $wp_roles = new \WP_Roles();
297 }
298
299 $allRoles = $wp_roles->roles;
300
301 $allCapabilities = array();
302 foreach ( $allRoles as $role ) {
303 if ( ! empty( $role['capabilities'] ) ) {
304 foreach ( $role['capabilities'] as $cap => $granted ) {
305 $allCapabilities[ $cap ] = true;
306 }
307 }
308 }
309
310 $rolesResponse = array();
311 foreach ( $allRoles as $key => $roleData ) {
312 $rolesResponse[] = array(
313 'key' => $key,
314 'name' => $roleData['name'],
315 'capabilities' => ! empty( $roleData['capabilities'] ) ? $roleData['capabilities'] : array(),
316 );
317 }
318
319 $capabilitiesList = array_keys( $allCapabilities );
320 sort( $capabilitiesList );
321
322 return rest_ensure_response( array(
323 'roles' => $rolesResponse,
324 'all_capabilities' => $capabilitiesList,
325 ) );
326 }
327
328 public function createRole( WP_REST_Request $request ) {
329 $roleKey = sanitize_key( $request->get_param( 'key' ) );
330 $roleName = sanitize_text_field( $request->get_param( 'name' ) );
331 $capabilities = $request->get_param( 'capabilities' );
332
333 if ( empty( $roleKey ) || empty( $roleName ) ) {
334 return rest_ensure_response( array( 'success' => false, 'message' => 'Role key and name are required.' ) );
335 }
336
337 if ( get_role( $roleKey ) ) {
338 return rest_ensure_response( array( 'success' => false, 'message' => 'Role already exists.' ) );
339 }
340
341 $capsToSave = array();
342 if ( ! empty( $capabilities ) && is_array( $capabilities ) ) {
343 foreach ( $capabilities as $cap ) {
344 $capsToSave[ $cap ] = true;
345 }
346 }
347
348 $result = add_role( $roleKey, $roleName, $capsToSave );
349
350 if ( null !== $result ) {
351 return rest_ensure_response( array( 'success' => true, 'message' => 'Role created successfully.' ) );
352 }
353
354 return rest_ensure_response( array( 'success' => false, 'message' => 'Failed to create role.' ) );
355 }
356
357 public function updateRoleCapabilities( WP_REST_Request $request ) {
358 $roleKey = sanitize_key( $request->get_param( 'key' ) );
359 $capabilities = $request->get_param( 'capabilities' );
360
361 $role = get_role( $roleKey );
362 if ( ! $role ) {
363 return rest_ensure_response( array( 'success' => false, 'message' => 'Role not found.' ) );
364 }
365
366 foreach ( $role->capabilities as $cap => $granted ) {
367 $role->remove_cap( $cap );
368 }
369
370 if ( ! empty( $capabilities ) && is_array( $capabilities ) ) {
371 foreach ( $capabilities as $cap ) {
372 $role->add_cap( $cap );
373 }
374 }
375
376 return rest_ensure_response( array( 'success' => true, 'message' => 'Role capabilities updated.' ) );
377 }
378
379 public function deleteRole( WP_REST_Request $request ) {
380 $roleKey = sanitize_key( $request->get_param( 'key' ) );
381
382 if ( empty( $roleKey ) ) {
383 return rest_ensure_response( array( 'success' => false, 'message' => 'Role key is required.' ) );
384 }
385
386 $protected_roles = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
387 if ( in_array( $roleKey, $protected_roles ) ) {
388 return rest_ensure_response( array(
389 'success' => false,
390 'message' => 'Cannot delete a core WordPress role.',
391 ) );
392 }
393
394 $role = get_role( $roleKey );
395 if ( ! $role ) {
396 return rest_ensure_response( array( 'success' => false, 'message' => 'Role not found.' ) );
397 }
398
399 remove_role( $roleKey );
400
401 return rest_ensure_response( array( 'success' => true, 'message' => 'Role deleted successfully.' ) );
402 }
403 }
404