PluginProbe
Tiered Pricing Table for WooCommerce / 6.5.0
Tiered Pricing Table for WooCommerce v6.5.0
8.0.2 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 All 91 releases
tier-pricing-table / src / Addons / Tools / API / ToolsEndpoints.php

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

402 lines 12.3 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 $message = sprintf( 'Successfully cleaned up %d product records.', $deletedCount );
243
244 return rest_ensure_response( array(
245 'success' => true,
246 'deleted_rows' => $deletedCount,
247 'message' => $message,
248 ) );
249 }
250
251 public function resetSettings( WP_REST_Request $request ) {
252 global $wpdb;
253
254 if ( class_exists( '\TierPricingTable\Settings\Settings' ) ) {
255 \TierPricingTable\Settings\Settings::deleteOptions();
256 }
257
258 // Wipe all other plugin settings that might be stored via different addons
259 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'tier_pricing_table_%'" );
260 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'tiered_pricing_table/%'" );
261 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'tpt_custom_table_columns%'" );
262
263 return rest_ensure_response( array(
264 'success' => true,
265 'message' => 'Plugin settings were successfully reset to defaults.',
266 ) );
267 }
268
269 public function deleteGlobalRules( WP_REST_Request $request ) {
270 $globalRules = get_posts( array(
271 'numberposts' => - 1,
272 'post_type' => GlobalTieredPricingCPT::SLUG,
273 'post_status' => 'any',
274 'fields' => 'ids',
275 ) );
276
277 $deletedCount = 0;
278 foreach ( $globalRules as $ruleId ) {
279 if ( wp_delete_post( $ruleId, true ) ) {
280 $deletedCount ++;
281 }
282 }
283
284 return rest_ensure_response( array(
285 'success' => true,
286 'deleted_rows' => $deletedCount,
287 'message' => sprintf( 'Successfully deleted %d global rules.', $deletedCount ),
288 ) );
289 }
290
291 public function getRolesAndCapabilities() {
292 global $wp_roles;
293 if ( ! isset( $wp_roles ) ) {
294 $wp_roles = new \WP_Roles();
295 }
296
297 $allRoles = $wp_roles->roles;
298
299 $allCapabilities = array();
300 foreach ( $allRoles as $role ) {
301 if ( ! empty( $role['capabilities'] ) ) {
302 foreach ( $role['capabilities'] as $cap => $granted ) {
303 $allCapabilities[ $cap ] = true;
304 }
305 }
306 }
307
308 $rolesResponse = array();
309 foreach ( $allRoles as $key => $roleData ) {
310 $rolesResponse[] = array(
311 'key' => $key,
312 'name' => $roleData['name'],
313 'capabilities' => ! empty( $roleData['capabilities'] ) ? $roleData['capabilities'] : array(),
314 );
315 }
316
317 $capabilitiesList = array_keys( $allCapabilities );
318 sort( $capabilitiesList );
319
320 return rest_ensure_response( array(
321 'roles' => $rolesResponse,
322 'all_capabilities' => $capabilitiesList,
323 ) );
324 }
325
326 public function createRole( WP_REST_Request $request ) {
327 $roleKey = sanitize_key( $request->get_param( 'key' ) );
328 $roleName = sanitize_text_field( $request->get_param( 'name' ) );
329 $capabilities = $request->get_param( 'capabilities' );
330
331 if ( empty( $roleKey ) || empty( $roleName ) ) {
332 return rest_ensure_response( array( 'success' => false, 'message' => 'Role key and name are required.' ) );
333 }
334
335 if ( get_role( $roleKey ) ) {
336 return rest_ensure_response( array( 'success' => false, 'message' => 'Role already exists.' ) );
337 }
338
339 $capsToSave = array();
340 if ( ! empty( $capabilities ) && is_array( $capabilities ) ) {
341 foreach ( $capabilities as $cap ) {
342 $capsToSave[ $cap ] = true;
343 }
344 }
345
346 $result = add_role( $roleKey, $roleName, $capsToSave );
347
348 if ( null !== $result ) {
349 return rest_ensure_response( array( 'success' => true, 'message' => 'Role created successfully.' ) );
350 }
351
352 return rest_ensure_response( array( 'success' => false, 'message' => 'Failed to create role.' ) );
353 }
354
355 public function updateRoleCapabilities( WP_REST_Request $request ) {
356 $roleKey = sanitize_key( $request->get_param( 'key' ) );
357 $capabilities = $request->get_param( 'capabilities' );
358
359 $role = get_role( $roleKey );
360 if ( ! $role ) {
361 return rest_ensure_response( array( 'success' => false, 'message' => 'Role not found.' ) );
362 }
363
364 foreach ( $role->capabilities as $cap => $granted ) {
365 $role->remove_cap( $cap );
366 }
367
368 if ( ! empty( $capabilities ) && is_array( $capabilities ) ) {
369 foreach ( $capabilities as $cap ) {
370 $role->add_cap( $cap );
371 }
372 }
373
374 return rest_ensure_response( array( 'success' => true, 'message' => 'Role capabilities updated.' ) );
375 }
376
377 public function deleteRole( WP_REST_Request $request ) {
378 $roleKey = sanitize_key( $request->get_param( 'key' ) );
379
380 if ( empty( $roleKey ) ) {
381 return rest_ensure_response( array( 'success' => false, 'message' => 'Role key is required.' ) );
382 }
383
384 $protected_roles = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
385 if ( in_array( $roleKey, $protected_roles ) ) {
386 return rest_ensure_response( array(
387 'success' => false,
388 'message' => 'Cannot delete a core WordPress role.',
389 ) );
390 }
391
392 $role = get_role( $roleKey );
393 if ( ! $role ) {
394 return rest_ensure_response( array( 'success' => false, 'message' => 'Role not found.' ) );
395 }
396
397 remove_role( $roleKey );
398
399 return rest_ensure_response( array( 'success' => true, 'message' => 'Role deleted successfully.' ) );
400 }
401 }
402