Skip to content

Commit ea04a94

Browse files
authored
Support duplicating groups of fields (#7426)
2 parents 5ec4015 + 2ab2a0c commit ea04a94

File tree

15 files changed

+468
-79
lines changed

15 files changed

+468
-79
lines changed

classes/PodsAPI.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*
@@ -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,
@@ -6701,7 +6706,13 @@ public function duplicate_group( $params, $strict = false ) {
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

src/Pods/REST/V1/Endpoints/Group_Duplicate.php

Lines changed: 186 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Pods/REST/V1/Service_Provider.php

Lines changed: 12 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/js/dfv/pods-dfv.min.asset.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"dependencies":["lodash","moment","react","react-dom","react-jsx-runtime","regenerator-runtime","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-hooks","wp-i18n","wp-keycodes","wp-plugins","wp-primitives","wp-url"],"version":"80b6069739008a83b26c"}
1+
{"dependencies":["lodash","moment","react","react-dom","react-jsx-runtime","regenerator-runtime","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-hooks","wp-i18n","wp-keycodes","wp-plugins","wp-primitives","wp-url"],"version":"4b9cc806655e49cfbca5"}

ui/js/dfv/pods-dfv.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/js/dfv/src/admin/edit-pod/main-tabs/field-group.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import SettingsModal from './settings-modal';
1212
import FieldList from 'dfv/src/admin/edit-pod/main-tabs/field-list';
1313
import { GROUP_PROP_TYPE_SHAPE } from 'dfv/src/config/prop-types';
1414

15-
import { SAVE_STATUSES, DELETE_STATUSES } from 'dfv/src/store/constants';
15+
import {
16+
SAVE_STATUSES,
17+
DUPLICATE_STATUSES,
18+
DELETE_STATUSES,
19+
} from 'dfv/src/store/constants';
1620

1721
import './field-group.scss';
1822

@@ -31,8 +35,10 @@ const FieldGroup = ( props ) => {
3135
hasMovedFields,
3236
saveStatus,
3337
saveMessage,
38+
duplicateStatus,
3439
deleteStatus,
3540
resetGroupSaveStatus,
41+
duplicateGroup,
3642
deleteGroup,
3743
removeGroupFromPod,
3844
saveGroup,
@@ -48,6 +54,8 @@ const FieldGroup = ( props ) => {
4854
fields,
4955
} = group;
5056

57+
const isDuplicating = DUPLICATE_STATUSES.DUPLICATING === duplicateStatus;
58+
const hasDuplicateFailed = DUPLICATE_STATUSES.DUPLICATE_ERROR === duplicateStatus;
5159
const isDeleting = DELETE_STATUSES.DELETING === deleteStatus;
5260
const hasDeleteFailed = DELETE_STATUSES.DELETE_ERROR === deleteStatus;
5361

@@ -129,6 +137,21 @@ const FieldGroup = ( props ) => {
129137
);
130138
};
131139

140+
const onDuplicateGroupClick = ( event ) => {
141+
event.stopPropagation();
142+
143+
if ( hasMovedFields ) {
144+
// eslint-disable-next-line no-alert
145+
alert(
146+
__( 'You moved fields outside of this group but did not save your changes to the Pod yet. To duplicate this Group, save changes for your Pod first.', 'pods' ),
147+
);
148+
149+
return;
150+
}
151+
152+
duplicateGroup( groupID, groupName );
153+
};
154+
132155
const onDeleteGroupClick = ( event ) => {
133156
event.stopPropagation();
134157

@@ -159,6 +182,8 @@ const FieldGroup = ( props ) => {
159182
classnames(
160183
'pods-field-group-wrapper',
161184
hasMoved && 'pods-field-group-wrapper--unsaved',
185+
isDuplicating && 'pods-field-group-wrapper--duplicating',
186+
hasDuplicateFailed && 'pods-field-group-wrapper--errored',
162187
isDeleting && 'pods-field-group-wrapper--deleting',
163188
hasDeleteFailed && 'pods-field-group-wrapper--errored',
164189
)
@@ -188,6 +213,12 @@ const FieldGroup = ( props ) => {
188213

189214
{ groupLabel }
190215

216+
{ hasDuplicateFailed ? (
217+
<div className="pods-field-group_name__error">
218+
{ __( 'Duplication failed. Try again?', 'pods' ) }
219+
</div>
220+
) : null }
221+
191222
{ hasDeleteFailed ? (
192223
<div className="pods-field-group_name__error">
193224
{ __( 'Delete failed. Try again?', 'pods' ) }
@@ -223,6 +254,14 @@ const FieldGroup = ( props ) => {
223254
{ __( 'Edit', 'pods' ) }
224255
</button>
225256
|
257+
<button
258+
className="pods-field-group_button pods-field-group_duplicate"
259+
onClick={ ( event ) => onDuplicateGroupClick( event ) }
260+
aria-label={ __( 'Duplicate this field group for the Pod', 'pods' ) }
261+
>
262+
{ __( 'Duplicate', 'pods' ) }
263+
</button>
264+
|
226265
<button
227266
className="pods-field-group_button pods-field-group_delete"
228267
onClick={ onDeleteGroupClick }
@@ -305,9 +344,11 @@ FieldGroup.propTypes = {
305344
hasMovedFields: PropTypes.bool.isRequired,
306345
saveStatus: PropTypes.string,
307346
saveMessage: PropTypes.string,
347+
duplicateStatus: PropTypes.string,
308348
deleteStatus: PropTypes.string,
309349

310350
toggleExpanded: PropTypes.func.isRequired,
351+
duplicateGroup: PropTypes.func.isRequired,
311352
deleteGroup: PropTypes.func.isRequired,
312353
removeGroupFromPod: PropTypes.func.isRequired,
313354
saveGroup: PropTypes.func.isRequired,

0 commit comments

Comments
 (0)