PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.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.1.0, at src/Addons/Tools/API/ToolsEndpoints.php

355 lines 10.6 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 $roles = $request->get_param( 'roles' ); // array of role keys
109 $categories = $request->get_param( 'categories' ); // array of term ids
110 $products = $request->get_param( 'products' ); // array of product ids
111
112 $metaKeysToDelete = array();
113
114 if ( $cleanStandard ) {
115 $metaKeysToDelete = array_merge( $metaKeysToDelete, array(
116 '_fixed_price_rules',
117 '_percentage_price_rules',
118 '_tiered_price_rules_type',
119 '_tiered_price_minimum_qty',
120 '_tiered_pricing_maximum_quantity',
121 '_tiered_pricing_group_of_quantity',
122 '_tiered_pricing_template',
123 '_tiered_pricing_base_unit_name',
124 '_tiered_pricing_default_variation_id',
125 '_tiered_pricing_variable_product_same_prices',
126 ) );
127 }
128
129 if ( ! empty( $roles ) && is_array( $roles ) ) {
130 foreach ( $roles as $roleKey ) {
131 $roleKey = sanitize_key( $roleKey );
132 $metaKeysToDelete = array_merge( $metaKeysToDelete, array(
133 "_{$roleKey}_tiered_price_pricing_type",
134 "_{$roleKey}_tiered_price_regular_price",
135 "_{$roleKey}_tiered_price_sale_price",
136 "_{$roleKey}_tiered_price_discount",
137 "_{$roleKey}_tiered_price_discount_type",
138 "_{$roleKey}_tiered_price_rules_type",
139 "_{$roleKey}_percentage_price_rules",
140 "_{$roleKey}_fixed_price_rules",
141 "_{$roleKey}_tiered_price_minimum_qty",
142 "_{$roleKey}_tiered_pricing_maximum_quantity",
143 "_{$roleKey}_tiered_pricing_group_of_quantity",
144 ) );
145 }
146 }
147
148 $deletedCount = 0;
149
150 if ( ! empty( $metaKeysToDelete ) ) {
151 global $wpdb;
152 $metaKeysIn = "'" . implode( "','", array_map( 'esc_sql', $metaKeysToDelete ) ) . "'";
153
154 $postIds = array();
155
156 if ( ! empty( $products ) && is_array( $products ) ) {
157 $postIds = array_map( 'intval', $products );
158 }
159
160 if ( ! empty( $categories ) && is_array( $categories ) ) {
161 $categoryIds = array_map( 'intval', $categories );
162 $args = array(
163 'limit' => - 1,
164 'return' => 'ids',
165 'category' => array_map( function ( $id ) {
166 $term = get_term( $id, 'product_cat' );
167
168 return $term ? $term->slug : '';
169 }, $categoryIds ),
170 );
171
172 $catProducts = wc_get_products( $args );
173 $postIds = array_unique( array_merge( $postIds, $catProducts ) );
174 }
175
176 if ( empty( $products ) && empty( $categories ) ) {
177 $query = "DELETE FROM {$wpdb->postmeta} WHERE meta_key IN ($metaKeysIn)";
178 $deletedCount = $wpdb->query( $query );
179 } else {
180 if ( ! empty( $postIds ) ) {
181 $postIdsIn = implode( ',', $postIds );
182 $query = "DELETE FROM {$wpdb->postmeta} WHERE meta_key IN ($metaKeysIn) AND post_id IN ($postIdsIn)";
183 $deletedCount = $wpdb->query( $query );
184 }
185 }
186
187 if ( $deletedCount === false ) {
188 return rest_ensure_response( array(
189 'success' => false,
190 'message' => 'Database error during cleanup.',
191 ) );
192 }
193 }
194
195 $message = sprintf( 'Successfully cleaned up %d product records.', $deletedCount );
196
197 return rest_ensure_response( array(
198 'success' => true,
199 'deleted_rows' => $deletedCount,
200 'message' => $message,
201 ) );
202 }
203
204 public function resetSettings( WP_REST_Request $request ) {
205 global $wpdb;
206
207 if ( class_exists( '\TierPricingTable\Settings\Settings' ) ) {
208 \TierPricingTable\Settings\Settings::deleteOptions();
209 }
210
211 // Wipe all other plugin settings that might be stored via different addons
212 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'tier_pricing_table_%'" );
213 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'tiered_pricing_table/%'" );
214 $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'tpt_custom_table_columns%'" );
215
216 return rest_ensure_response( array(
217 'success' => true,
218 'message' => 'Plugin settings were successfully reset to defaults.',
219 ) );
220 }
221
222 public function deleteGlobalRules( WP_REST_Request $request ) {
223 $globalRules = get_posts( array(
224 'numberposts' => - 1,
225 'post_type' => GlobalTieredPricingCPT::SLUG,
226 'post_status' => 'any',
227 'fields' => 'ids',
228 ) );
229
230 $deletedCount = 0;
231 foreach ( $globalRules as $ruleId ) {
232 if ( wp_delete_post( $ruleId, true ) ) {
233 $deletedCount ++;
234 }
235 }
236
237 return rest_ensure_response( array(
238 'success' => true,
239 'deleted_rows' => $deletedCount,
240 'message' => sprintf( 'Successfully deleted %d global rules.', $deletedCount ),
241 ) );
242 }
243
244 public function getRolesAndCapabilities() {
245 global $wp_roles;
246 if ( ! isset( $wp_roles ) ) {
247 $wp_roles = new \WP_Roles();
248 }
249
250 $allRoles = $wp_roles->roles;
251
252 $allCapabilities = array();
253 foreach ( $allRoles as $role ) {
254 if ( ! empty( $role['capabilities'] ) ) {
255 foreach ( $role['capabilities'] as $cap => $granted ) {
256 $allCapabilities[ $cap ] = true;
257 }
258 }
259 }
260
261 $rolesResponse = array();
262 foreach ( $allRoles as $key => $roleData ) {
263 $rolesResponse[] = array(
264 'key' => $key,
265 'name' => $roleData['name'],
266 'capabilities' => ! empty( $roleData['capabilities'] ) ? $roleData['capabilities'] : array(),
267 );
268 }
269
270 $capabilitiesList = array_keys( $allCapabilities );
271 sort( $capabilitiesList );
272
273 return rest_ensure_response( array(
274 'roles' => $rolesResponse,
275 'all_capabilities' => $capabilitiesList,
276 ) );
277 }
278
279 public function createRole( WP_REST_Request $request ) {
280 $roleKey = sanitize_key( $request->get_param( 'key' ) );
281 $roleName = sanitize_text_field( $request->get_param( 'name' ) );
282 $capabilities = $request->get_param( 'capabilities' );
283
284 if ( empty( $roleKey ) || empty( $roleName ) ) {
285 return rest_ensure_response( array( 'success' => false, 'message' => 'Role key and name are required.' ) );
286 }
287
288 if ( get_role( $roleKey ) ) {
289 return rest_ensure_response( array( 'success' => false, 'message' => 'Role already exists.' ) );
290 }
291
292 $capsToSave = array();
293 if ( ! empty( $capabilities ) && is_array( $capabilities ) ) {
294 foreach ( $capabilities as $cap ) {
295 $capsToSave[ $cap ] = true;
296 }
297 }
298
299 $result = add_role( $roleKey, $roleName, $capsToSave );
300
301 if ( null !== $result ) {
302 return rest_ensure_response( array( 'success' => true, 'message' => 'Role created successfully.' ) );
303 }
304
305 return rest_ensure_response( array( 'success' => false, 'message' => 'Failed to create role.' ) );
306 }
307
308 public function updateRoleCapabilities( WP_REST_Request $request ) {
309 $roleKey = sanitize_key( $request->get_param( 'key' ) );
310 $capabilities = $request->get_param( 'capabilities' );
311
312 $role = get_role( $roleKey );
313 if ( ! $role ) {
314 return rest_ensure_response( array( 'success' => false, 'message' => 'Role not found.' ) );
315 }
316
317 foreach ( $role->capabilities as $cap => $granted ) {
318 $role->remove_cap( $cap );
319 }
320
321 if ( ! empty( $capabilities ) && is_array( $capabilities ) ) {
322 foreach ( $capabilities as $cap ) {
323 $role->add_cap( $cap );
324 }
325 }
326
327 return rest_ensure_response( array( 'success' => true, 'message' => 'Role capabilities updated.' ) );
328 }
329
330 public function deleteRole( WP_REST_Request $request ) {
331 $roleKey = sanitize_key( $request->get_param( 'key' ) );
332
333 if ( empty( $roleKey ) ) {
334 return rest_ensure_response( array( 'success' => false, 'message' => 'Role key is required.' ) );
335 }
336
337 $protected_roles = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
338 if ( in_array( $roleKey, $protected_roles ) ) {
339 return rest_ensure_response( array(
340 'success' => false,
341 'message' => 'Cannot delete a core WordPress role.',
342 ) );
343 }
344
345 $role = get_role( $roleKey );
346 if ( ! $role ) {
347 return rest_ensure_response( array( 'success' => false, 'message' => 'Role not found.' ) );
348 }
349
350 remove_role( $roleKey );
351
352 return rest_ensure_response( array( 'success' => true, 'message' => 'Role deleted successfully.' ) );
353 }
354 }
355