Skip to content

Commit 6f8c81f

Browse files
committed
Field groups, fields and registration forms screens now working properly when object caching enabled.
1 parent 415d0f5 commit 6f8c81f

File tree

5 files changed

+167
-44
lines changed

5 files changed

+167
-44
lines changed

includes/wpum-database/class-wpum-db-fields-groups.php

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,17 @@ public function get_last_changed() {
164164
return $last_changed;
165165
}
166166

167-
/**
168-
* Retrieve groups from the database
169-
*
170-
* @access public
171-
*
172-
* @param array $args {
173-
* Query arguments.
174-
* }
175-
*
176-
* @return array $groups Array of `WPUM_Field_Group` objects.
177-
*/
178-
public function get_groups( $args = array() ) {
167+
protected function get_cache_key( $args ) {
168+
return md5( 'wpum_fields_groups_' . serialize( $args ) );
169+
}
170+
171+
public function get_cache_key_from_args( $args ) {
172+
$args = $this->get_args( $args );
173+
174+
return $this->get_cache_key( $args );
175+
}
176+
177+
protected function get_args( $args = array() ) {
179178
global $wpdb;
180179

181180
$defaults = array(
@@ -199,11 +198,31 @@ public function get_groups( $args = array() ) {
199198
$args['search'] = $wpdb->esc_like( $args['search'] );
200199
}
201200

202-
$where = $this->parse_where( $args );
203201

204202
$args['orderby'] = ! array_key_exists( $args['orderby'], $this->get_columns() ) ? 'id' : $args['orderby'];
205203

206-
$cache_key = md5( 'wpum_fields_groups_' . serialize( $args ) );
204+
return $args;
205+
}
206+
207+
/**
208+
* Retrieve groups from the database
209+
*
210+
* @access public
211+
*
212+
* @param array $args {
213+
* Query arguments.
214+
* }
215+
*
216+
* @return array $groups Array of `WPUM_Field_Group` objects.
217+
*/
218+
public function get_groups( $args = array() ) {
219+
global $wpdb;
220+
221+
$args = $this->get_args( $args );
222+
223+
$where = $this->parse_where( $args );
224+
225+
$cache_key = $this->get_cache_key( $args );
207226

208227
$groups = wp_cache_get( $cache_key, $this->cache_group );
209228

includes/wpum-database/class-wpum-db-fields.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,17 @@ public function get_last_changed() {
158158
return $last_changed;
159159
}
160160

161-
/**
162-
* Retrieve fields from the database
163-
*
164-
* @access public
165-
*
166-
* @param array $args
167-
*
168-
* @return array $groups Array of `WPUM_Field` objects.
169-
*/
170-
public function get_fields( $args = array() ) {
161+
protected function get_cache_key( $args ) {
162+
return md5( 'wpum_fields_' . serialize( $args ) );
163+
}
164+
165+
public function get_cache_key_from_args( $args ) {
166+
$args = $this->get_args( $args );
167+
168+
return $this->get_cache_key( $args );
169+
}
171170

171+
protected function get_args( $args = array() ) {
172172
global $wpdb;
173173

174174
$defaults = array(
@@ -191,11 +191,29 @@ public function get_fields( $args = array() ) {
191191
$args['search'] = $wpdb->esc_like( $args['search'] );
192192
}
193193

194-
$where = $this->parse_where( $args );
195-
196194
$args['orderby'] = ! array_key_exists( $args['orderby'], $this->get_columns() ) ? 'id' : $args['orderby'];
197195

198-
$cache_key = md5( 'wpum_fields_' . serialize( $args ) );
196+
return $args;
197+
}
198+
199+
/**
200+
* Retrieve fields from the database
201+
*
202+
* @access public
203+
*
204+
* @param array $args
205+
*
206+
* @return array $groups Array of `WPUM_Field` objects.
207+
*/
208+
public function get_fields( $args = array() ) {
209+
210+
global $wpdb;
211+
212+
$args = $this->get_args( $args );
213+
214+
$where = $this->parse_where( $args );
215+
216+
$cache_key = $this->get_cache_key( $args );
199217

200218
$fields = wp_cache_get( $cache_key, $this->cache_group );
201219

includes/wpum-database/class-wpum-db-registration-forms.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,17 @@ public function get_last_changed() {
148148
return $last_changed;
149149
}
150150

151-
/**
152-
* Retrieve forms from the database
153-
*
154-
* @access public
155-
*
156-
* @param array $args {
157-
* Query arguments.
158-
* }
159-
*
160-
* @return array $forms Array of `WPUM_Registration_Form` objects.
161-
*/
162-
public function get_forms( $args = array() ) {
151+
protected function get_cache_key( $args ) {
152+
return md5( 'wpum_registration_forms_' . serialize( $args ) );
153+
}
154+
155+
public function get_cache_key_from_args( $args = [] ) {
156+
$args = $this->get_args( $args );
157+
158+
return $this->get_cache_key( $args );
159+
}
160+
161+
protected function get_args( $args = array() ) {
163162
global $wpdb;
164163

165164
$defaults = array(
@@ -180,11 +179,30 @@ public function get_forms( $args = array() ) {
180179
$args['search'] = $wpdb->esc_like( $args['search'] );
181180
}
182181

183-
$where = $this->parse_where( $args );
184-
185182
$args['orderby'] = ! array_key_exists( $args['orderby'], $this->get_columns() ) ? 'id' : $args['orderby'];
186183

187-
$cache_key = md5( 'wpum_registration_forms_' . serialize( $args ) );
184+
return $args;
185+
}
186+
187+
/**
188+
* Retrieve forms from the database
189+
*
190+
* @access public
191+
*
192+
* @param array $args {
193+
* Query arguments.
194+
* }
195+
*
196+
* @return array $forms Array of `WPUM_Registration_Form` objects.
197+
*/
198+
public function get_forms( $args = array() ) {
199+
global $wpdb;
200+
201+
$args = $this->get_args( $args );
202+
203+
$where = $this->parse_where( $args );
204+
205+
$cache_key = $this->get_cache_key( $args );
188206

189207
$forms = wp_cache_get( $cache_key, $this->cache_group );
190208

includes/wpum-fields/class-wpum-fields-editor.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ private function init_hooks() {
4848
add_action( 'wp_ajax_wpum_update_fields_order', [ $this, 'update_fields_order' ] );
4949
add_action( 'wp_ajax_wpum_get_field_settings', [ $this, 'get_field_settings' ] );
5050
add_action( 'wp_ajax_wpum_update_field', [ $this, 'update_field' ] );
51+
// Object Caching hooks
52+
add_action( 'wpum_field_group_insert', [ $this, 'trigger_delete_groups_cache'] );
53+
add_action( 'wpum_field_group_delete', [ $this, 'trigger_delete_groups_cache'] );
54+
add_action( 'wpum_field_group_delete', [ $this, 'trigger_delete_groups_cache_by_id'] );
55+
add_action( 'wpum_field_insert', [ $this, 'trigger_delete_group_fields_cache'], 10, 2 );
56+
add_action( 'wpum_before_field_delete', [ $this, 'trigger_delete_group_fields_cache_by_id'] );
5157
}
5258

5359
/**
@@ -252,7 +258,7 @@ public function update_groups_order() {
252258
} else {
253259
wp_die( esc_html__( 'Something went wrong: could not update the groups order.', 'wp-user-manager' ), 403 );
254260
}
255-
261+
$this->delete_groups_cache();
256262
wp_send_json_success( $groups );
257263

258264
}
@@ -287,6 +293,8 @@ public function update_group() {
287293
wp_die( esc_html__( 'Something went wrong: could not update the group details.', 'wp-user-manager' ), 403 );
288294
}
289295

296+
$this->delete_groups_cache();
297+
290298
wp_send_json_success(
291299
[
292300
'id' => $group_id,
@@ -297,6 +305,30 @@ public function update_group() {
297305

298306
}
299307

308+
protected function delete_groups_cache() {
309+
$args = [
310+
'orderby' => 'group_order',
311+
'order' => 'ASC',
312+
];
313+
314+
$cache_key = WPUM()->fields_groups->get_cache_key_from_args( $args );
315+
316+
wp_cache_delete( $cache_key, WPUM()->fields_groups->cache_group );
317+
}
318+
319+
protected function delete_group_fields_cache( $group_id, $parent = 0 ) {
320+
$args = [
321+
'group_id' => (int) $group_id,
322+
'orderby' => 'field_order',
323+
'order' => 'ASC',
324+
'parent' => $parent,
325+
];
326+
327+
$cache_key = WPUM()->fields->get_cache_key_from_args( $args );
328+
329+
wp_cache_delete( $cache_key, WPUM()->fields->cache_group );
330+
}
331+
300332
/**
301333
* Retrieve fields from the database given a group id.
302334
*
@@ -372,9 +404,10 @@ public function update_fields_order() {
372404
}
373405

374406
$fields = isset( $_POST['fields'] ) && is_array( $_POST['fields'] ) && ! empty( $_POST['fields'] ) ? $_POST['fields'] : false;
375-
407+
$group_id = false;
376408
if ( $fields ) {
377409
foreach ( $fields as $order => $field ) {
410+
$group_id = $field['group_id'];
378411
$field_id = (int) $field['id'];
379412
if ( $field_id ) {
380413
$updated_field = WPUM()->fields->update( $field_id, [ 'field_order' => $order ] );
@@ -384,6 +417,7 @@ public function update_fields_order() {
384417
wp_die( esc_html__( 'Something went wrong: could not update the fields order.', 'wp-user-manager' ), 403 );
385418
}
386419

420+
$this->delete_group_fields_cache( $group_id );
387421
wp_send_json_success( $fields );
388422

389423
}
@@ -597,6 +631,25 @@ private function get_setting_value( $wpum_field_id, $setting_id, $type ) {
597631
return $value;
598632
}
599633

634+
public function trigger_delete_groups_cache() {
635+
$this->delete_groups_cache();
636+
}
637+
638+
public function trigger_delete_groups_cache_by_id( $group_id ) {
639+
$this->delete_groups_cache();
640+
$this->delete_group_fields_cache( $group_id );
641+
}
642+
643+
public function trigger_delete_group_fields_cache( $data, $field_id ) {
644+
$field = new WPUM_Field( $field_id );
645+
$this->delete_group_fields_cache( $field->get_group_id() );
646+
}
647+
648+
public function trigger_delete_group_fields_cache_by_id( $field_id ) {
649+
$field = new WPUM_Field( $field_id );
650+
$this->delete_group_fields_cache( $field->get_group_id() );
651+
}
652+
600653
/**
601654
* Update a field within the database.
602655
* Verify the field exists first, sanitize the given data and then update the db.
@@ -693,6 +746,7 @@ public function update_field() {
693746
$field_to_update->update_meta( 'dropdown_options', $options );
694747
}
695748

749+
$this->delete_group_fields_cache( $field_to_update->get_group_id() );
696750
wp_send_json_success( $data );
697751

698752
} else {

includes/wpum-forms/class-wpum-registration-forms-editor.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public function init_hooks() {
4646
add_filter( 'wpum_form_settings_sanitize_multicheckbox', array( $this, 'sanitize_multiple_field' ) );
4747
add_filter( 'wpum_form_settings_sanitize_file', array( $this, 'sanitize_file_field' ) );
4848

49+
// Object Caching hooks
50+
add_action( 'wpum_registration_form_insert', [ $this, 'delete_registration_forms_cache'] );
51+
add_action( 'wpum_before_registration_form_delete', [ $this, 'delete_registration_forms_cache'] );
52+
add_action( 'wpum_registration_form_duplicated', [ $this, 'delete_registration_forms_cache'] );
4953
}
5054

5155
/**
@@ -231,6 +235,8 @@ public function update_form() {
231235

232236
$updated_form = WPUM()->registration_forms->update( $form_id, $data );
233237

238+
$this->delete_registration_forms_cache();
239+
234240
} else {
235241
wp_die( esc_html__( 'Something went wrong: could not update the registration form details.', 'wp-user-manager' ), 403 );
236242
}
@@ -422,6 +428,8 @@ public function save_form() {
422428

423429
if( ! empty( $fields_to_save ) ) {
424430
$registration_form->update_meta( 'fields', $fields_to_save );
431+
432+
$this->delete_registration_forms_cache();
425433
wp_send_json_success();
426434
}
427435

@@ -630,6 +638,12 @@ private function send_json_error() {
630638
wp_send_json_error( null, 403 );
631639
}
632640

641+
public function delete_registration_forms_cache() {
642+
$cache_key = WPUM()->registration_forms->get_cache_key_from_args();
643+
644+
wp_cache_delete( $cache_key, WPUM()->registration_forms->cache_group );
645+
}
646+
633647
}
634648

635649
$wpum_registration_forms_editor = new WPUM_Registration_Forms_Editor;

0 commit comments

Comments
 (0)