Skip to content

Commit e072279

Browse files
committed
Support duplicating groups of fields
Auto rebuild assets DFV work for duplicate group Separate the duplicate group endpoint Remove temp debug Auto rebuild assets
1 parent ce61d8f commit e072279

File tree

21 files changed

+956
-77
lines changed

21 files changed

+956
-77
lines changed

classes/PodsAPI.php

Lines changed: 80 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6504,9 +6504,9 @@ public function duplicate_pod( $params, $strict = false ) {
65046504
$params = array( 'name' => $params );
65056505
}
65066506

6507-
$params = (object) pods_sanitize( $params );
6507+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
65086508
} else {
6509-
$params = (object) pods_sanitize( $params );
6509+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
65106510
}
65116511

65126512
$pod = $this->load_pod( $params, false );
@@ -6559,7 +6559,7 @@ public function duplicate_pod( $params, $strict = false ) {
65596559
}
65606560

65616561
$check_name = $check_name_limited . $try;
6562-
$new_label = $pod['label'] . $try;
6562+
$new_label = $pod['label'] . ' ' . $try;
65636563
}
65646564

65656565
$pod['name'] = $check_name;
@@ -6632,6 +6632,7 @@ public function duplicate_pod( $params, $strict = false ) {
66326632
* $params['id'] int The Group ID.
66336633
* $params['name'] string The Group name.
66346634
* $params['new_name'] string The new Group name.
6635+
* $params['duplicate_fields'] bool Whether to duplicate the fields.
66356636
*
66366637
* @since 2.8.0
66376638
*
@@ -6649,9 +6650,9 @@ public function duplicate_group( $params, $strict = false ) {
66496650
$params = array( 'name' => $params );
66506651
}
66516652

6652-
$params = (object) pods_sanitize( $params );
6653+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
66536654
} else {
6654-
$params = (object) pods_sanitize( $params );
6655+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
66556656
}
66566657

66576658
if ( ! empty( $params->pod_id ) ) {
@@ -6660,7 +6661,7 @@ public function duplicate_group( $params, $strict = false ) {
66606661
$load_params['pod'] = $params->pod;
66616662
}
66626663

6663-
$group = $this->load_group( $params, false );
6664+
$group = $this->load_group( $load_params, false );
66646665

66656666
if ( empty( $group ) ) {
66666667
if ( false !== $strict ) {
@@ -6670,7 +6671,11 @@ public function duplicate_group( $params, $strict = false ) {
66706671
return false;
66716672
}
66726673

6674+
$pod_data = null;
6675+
66736676
if ( $group instanceof Group ) {
6677+
$pod_data = $group->get_parent_object();
6678+
66746679
$group = $group->export(
66756680
[
66766681
'include_fields' => true,
@@ -6693,15 +6698,21 @@ public function duplicate_group( $params, $strict = false ) {
66936698
$try ++;
66946699

66956700
$check_name = $group['name'] . $try;
6696-
$new_label = $group['label'] . $try;
6701+
$new_label = $group['label'] . ' ' . $try;
66976702
}
66986703

66996704
$group['name'] = $check_name;
67006705
$group['label'] = $new_label;
67016706

67026707
$fields = $group['fields'];
67036708

6704-
unset( $group['id'], $group['parent'], $group['object_type'], $group['object_storage_type'], $group['fields'] );
6709+
unset( $group['id'], $group['object_type'], $group['object_storage_type'], $group['fields'] );
6710+
6711+
if ( $pod_data ) {
6712+
unset( $group['parent'] );
6713+
6714+
$group['pod_data'] = $pod_data;
6715+
}
67056716

67066717
try {
67076718
$group_id = $this->save_group( $group );
@@ -6715,16 +6726,24 @@ public function duplicate_group( $params, $strict = false ) {
67156726
return false;
67166727
}
67176728

6718-
foreach ( $fields as $field => $field_data ) {
6719-
unset( $field_data['id'], $field_data['parent'], $field_data['object_type'], $field_data['object_storage_type'], $field_data['group'] );
6729+
$group_data = $this->load_group( [ 'id' => $group_id ] );
67206730

6721-
$field_data['group_id'] = $group_id;
6731+
if ( ! empty( $params->duplicate_fields ) ) {
6732+
foreach ( $fields as $field_data ) {
6733+
try {
6734+
$field_params = [
6735+
'pod' => $pod_data,
6736+
'id' => $field_data['id'],
6737+
'name' => $field_data['name'],
6738+
'new_group' => $group_data,
6739+
'new_group_id' => $group_id,
6740+
];
67226741

6723-
try {
6724-
$this->save_field( $field_data );
6725-
} catch ( Exception $exception ) {
6726-
// Field not saved.
6727-
pods_debug_log( $exception );
6742+
$this->duplicate_field( $field_params, true || $strict );
6743+
} catch ( Exception $exception ) {
6744+
// Field not saved.
6745+
pods_debug_log( $exception );
6746+
}
67286747
}
67296748
}
67306749

@@ -6735,10 +6754,12 @@ public function duplicate_group( $params, $strict = false ) {
67356754
* Duplicate a Field.
67366755
*
67376756
* $params['pod_id'] int The Pod ID.
6738-
* $params['pod'] string The Pod name.
6757+
* $params['pod'] string|Whatsit The Pod name or object.
67396758
* $params['id'] int The Field ID.
67406759
* $params['name'] string The Field name.
67416760
* $params['new_name'] string The new Field name.
6761+
* $params['new_group'] string|Whatsit The new Group name or object.
6762+
* $params['new_group_id'] string The new Group ID.
67426763
*
67436764
* @since 2.3.10
67446765
*
@@ -6757,7 +6778,7 @@ public function duplicate_field( $params, $strict = false ) {
67576778
}
67586779
}
67596780

6760-
$params = (object) pods_sanitize( $params );
6781+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
67616782

67626783
$load_params = array();
67636784

@@ -6783,10 +6804,28 @@ public function duplicate_field( $params, $strict = false ) {
67836804
return false;
67846805
}
67856806

6807+
$pod_data = $field->get_parent_object();
6808+
67866809
if ( $field instanceof Field ) {
67876810
$field = $field->export();
67886811
}
67896812

6813+
$new_group = null;
6814+
6815+
$load_group_params = [];
6816+
6817+
if ( ! empty( $params->new_group_id ) ) {
6818+
$load_group_params['pod'] = $pod_data;
6819+
$load_group_params['id'] = $params->new_group_id;
6820+
} elseif ( ! empty( $params->new_group ) ) {
6821+
$load_group_params['pod'] = $pod_data;
6822+
$load_group_params['name'] = $params->new_group;
6823+
}
6824+
6825+
if ( $load_group_params ) {
6826+
$new_group = $this->load_group( $load_group_params, $strict );
6827+
}
6828+
67906829
if ( isset( $params->new_name ) ) {
67916830
$field['name'] = $params->new_name;
67926831
}
@@ -6803,12 +6842,20 @@ public function duplicate_field( $params, $strict = false ) {
68036842
$try ++;
68046843

68056844
$check_name = $field['name'] . $try;
6806-
$new_label = $field['label'] . $try;
6845+
$new_label = $field['label'] . ' ' . $try;
68076846
}
68086847

68096848
$field['name'] = $check_name;
68106849
$field['label'] = $new_label;
68116850

6851+
if ( $new_group ) {
6852+
$field['group'] = $new_group;
6853+
}
6854+
6855+
if ( $pod_data ) {
6856+
$field['pod'] = $pod_data;
6857+
}
6858+
68126859
unset( $field['id'], $field['object_type'], $field['object_storage_type'] );
68136860

68146861
return $this->save_field( $field, true, true );
@@ -6831,7 +6878,7 @@ public function duplicate_field( $params, $strict = false ) {
68316878
*/
68326879
public function duplicate_pod_item( $params ) {
68336880

6834-
$params = (object) pods_sanitize( $params );
6881+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
68356882

68366883
$load_pod_params = array(
68376884
'name' => $params->pod,
@@ -7222,7 +7269,7 @@ private function export_pod_item_level( $pod, $params ) {
72227269
*/
72237270
public function reorder_pod_item( $params ) {
72247271

7225-
$params = (object) pods_sanitize( $params );
7272+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
72267273

72277274
if ( null === pods_v( 'pod', $params, null, true ) ) {
72287275
return pods_error( __( '$params->pod is required', 'pods' ), $this );
@@ -7289,7 +7336,7 @@ public function reset_pod( $params, $pod = false ) {
72897336
}
72907337

72917338
if ( is_array( $params ) || is_object( $params ) ) {
7292-
$params = (object) pods_sanitize( $params );
7339+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
72937340
} else {
72947341
$params = new stdClass();
72957342
}
@@ -7434,7 +7481,7 @@ public function delete_pod( $params, $strict = false, $delete_all = false ) {
74347481
global $wpdb;
74357482

74367483
if ( is_array( $params ) || is_object( $params ) ) {
7437-
$params = (object) pods_sanitize( $params );
7484+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
74387485
} else {
74397486
$params = new stdClass();
74407487
}
@@ -7740,9 +7787,9 @@ public function delete_group( $params, $strict = false, $delete_all = false ) {
77407787
$params = [ 'name' => $params ];
77417788
}
77427789

7743-
$params = (object) pods_sanitize( $params );
7790+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
77447791
} else {
7745-
$params = (object) pods_sanitize( $params );
7792+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
77467793
}
77477794

77487795
if ( ! isset( $params->delete_all ) ) {
@@ -8285,7 +8332,7 @@ public function pod_exists( $params, $type = null ) {
82858332
$params = array( 'name' => $params );
82868333
}
82878334

8288-
$params = (object) pods_sanitize( $params );
8335+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
82898336

82908337
if ( ! empty( $params->id ) || ! empty( $params->name ) ) {
82918338
if ( ! isset( $params->name ) ) {
@@ -8644,6 +8691,7 @@ public function load_field( $params, $strict = false ) {
86448691

86458692
$object = $this->_load_object( $params );
86468693

8694+
86478695
if ( $object ) {
86488696
return $object;
86498697
}
@@ -8940,6 +8988,10 @@ public function load_group( $params, $strict = false ) {
89408988
$params = get_object_vars( (object) $params );
89418989
}
89428990

8991+
if ( ! empty( $params['name'] ) && $params['name'] instanceof Group ) {
8992+
return $params['name'];
8993+
}
8994+
89438995
// Check if we need to bypass cache automatically.
89448996
if ( ! isset( $params['bypass_cache'] ) ) {
89458997
$api_cache = pods_api_cache();
@@ -9341,7 +9393,7 @@ public function load_helpers( $params = null ) {
93419393
*/
93429394
public function load_pod_item( $params ) {
93439395

9344-
$params = (object) pods_sanitize( $params );
9396+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
93459397

93469398
if ( ! isset( $params->pod ) || empty( $params->pod ) ) {
93479399
return pods_error( __( 'Pod name required', 'pods' ), $this );
@@ -9386,7 +9438,7 @@ public function load_pod_item( $params ) {
93869438
*/
93879439
public function load_sister_fields( $params, $pod = null ) {
93889440

9389-
$params = (object) pods_sanitize( $params );
9441+
$params = (object) pods_sanitize( $params, [ 'allow_pods_objects' => true ] );
93909442

93919443
if ( empty( $pod ) ) {
93929444
$pod = $this->load_pod( array( 'name' => $params->pod ), false );

includes/data.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ function pods_sanitize( $input, $params = array() ) {
2626
$output = array();
2727

2828
$defaults = array(
29-
'nested' => false,
30-
'type' => null,
29+
'nested' => false,
30+
'type' => null,
3131
// %s %d %f etc
32+
'allow_pods_objects' => false,
3233
);
3334

3435
if ( ! is_array( $params ) ) {
@@ -40,6 +41,10 @@ function pods_sanitize( $input, $params = array() ) {
4041
}
4142

4243
if ( is_object( $input ) ) {
44+
if ( $params['allow_pods_objects'] && $input instanceof Whatsit ) {
45+
return $input;
46+
}
47+
4348
$input = get_object_vars( $input );
4449

4550
$n_params = $params;

includes/general.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function pods_error( $error, $obj = null ) {
271271
$error_mode = $display_errors;
272272
}
273273

274-
if ( is_object( $error ) && 'Exception' === get_class( $error ) ) {
274+
if ( is_object( $error ) && $error instanceof Exception ) {
275275
$error_mode = 'exception';
276276

277277
if ( 'final_exception' === $display_errors ) {

0 commit comments

Comments
 (0)