Skip to content

Support duplicating groups of fields #7426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions classes/PodsAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -6632,6 +6632,7 @@ public function duplicate_pod( $params, $strict = false ) {
* $params['id'] int The Group ID.
* $params['name'] string The Group name.
* $params['new_name'] string The new Group name.
* $params['duplicate_fields'] bool Whether to duplicate the fields.
*
* @since 2.8.0
*
Expand Down Expand Up @@ -6670,7 +6671,11 @@ public function duplicate_group( $params, $strict = false ) {
return false;
}

$pod_data = null;

if ( $group instanceof Group ) {
$pod_data = $group->get_parent_object();

$group = $group->export(
[
'include_fields' => true,
Expand Down Expand Up @@ -6701,7 +6706,13 @@ public function duplicate_group( $params, $strict = false ) {

$fields = $group['fields'];

unset( $group['id'], $group['parent'], $group['object_type'], $group['object_storage_type'], $group['fields'] );
unset( $group['id'], $group['object_type'], $group['object_storage_type'], $group['fields'] );

if ( $pod_data ) {
unset( $group['parent'] );

$group['pod_data'] = $pod_data;
}

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

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

$field_data['group_id'] = $group_id;
if ( ! empty( $params->duplicate_fields ) ) {
foreach ( $fields as $field_data ) {
try {
$field_params = [
'pod' => $pod_data,
'id' => $field_data['id'],
'name' => $field_data['name'],
'new_group' => $group_data,
'new_group_id' => $group_id,
];

try {
$this->save_field( $field_data );
} catch ( Exception $exception ) {
// Field not saved.
pods_debug_log( $exception );
$this->duplicate_field( $field_params, true || $strict );
} catch ( Exception $exception ) {
// Field not saved.
pods_debug_log( $exception );
}
}
}

Expand Down
186 changes: 186 additions & 0 deletions src/Pods/REST/V1/Endpoints/Group_Duplicate.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions src/Pods/REST/V1/Service_Provider.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/js/dfv/pods-dfv.min.asset.json
Original file line number Diff line number Diff line change
@@ -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"}
{"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"}
2 changes: 1 addition & 1 deletion ui/js/dfv/pods-dfv.min.js

Large diffs are not rendered by default.

43 changes: 42 additions & 1 deletion ui/js/dfv/src/admin/edit-pod/main-tabs/field-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import SettingsModal from './settings-modal';
import FieldList from 'dfv/src/admin/edit-pod/main-tabs/field-list';
import { GROUP_PROP_TYPE_SHAPE } from 'dfv/src/config/prop-types';

import { SAVE_STATUSES, DELETE_STATUSES } from 'dfv/src/store/constants';
import {
SAVE_STATUSES,
DUPLICATE_STATUSES,
DELETE_STATUSES,
} from 'dfv/src/store/constants';

import './field-group.scss';

Expand All @@ -31,8 +35,10 @@ const FieldGroup = ( props ) => {
hasMovedFields,
saveStatus,
saveMessage,
duplicateStatus,
deleteStatus,
resetGroupSaveStatus,
duplicateGroup,
deleteGroup,
removeGroupFromPod,
saveGroup,
Expand All @@ -48,6 +54,8 @@ const FieldGroup = ( props ) => {
fields,
} = group;

const isDuplicating = DUPLICATE_STATUSES.DUPLICATING === duplicateStatus;
const hasDuplicateFailed = DUPLICATE_STATUSES.DUPLICATE_ERROR === duplicateStatus;
const isDeleting = DELETE_STATUSES.DELETING === deleteStatus;
const hasDeleteFailed = DELETE_STATUSES.DELETE_ERROR === deleteStatus;

Expand Down Expand Up @@ -129,6 +137,21 @@ const FieldGroup = ( props ) => {
);
};

const onDuplicateGroupClick = ( event ) => {
event.stopPropagation();

if ( hasMovedFields ) {
// eslint-disable-next-line no-alert
alert(
__( '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' ),
);

return;
}

duplicateGroup( groupID, groupName );
};

const onDeleteGroupClick = ( event ) => {
event.stopPropagation();

Expand Down Expand Up @@ -159,6 +182,8 @@ const FieldGroup = ( props ) => {
classnames(
'pods-field-group-wrapper',
hasMoved && 'pods-field-group-wrapper--unsaved',
isDuplicating && 'pods-field-group-wrapper--duplicating',
hasDuplicateFailed && 'pods-field-group-wrapper--errored',
isDeleting && 'pods-field-group-wrapper--deleting',
hasDeleteFailed && 'pods-field-group-wrapper--errored',
)
Expand Down Expand Up @@ -188,6 +213,12 @@ const FieldGroup = ( props ) => {

{ groupLabel }

{ hasDuplicateFailed ? (
<div className="pods-field-group_name__error">
{ __( 'Duplication failed. Try again?', 'pods' ) }
</div>
) : null }

{ hasDeleteFailed ? (
<div className="pods-field-group_name__error">
{ __( 'Delete failed. Try again?', 'pods' ) }
Expand Down Expand Up @@ -223,6 +254,14 @@ const FieldGroup = ( props ) => {
{ __( 'Edit', 'pods' ) }
</button>
|
<button
className="pods-field-group_button pods-field-group_duplicate"
onClick={ ( event ) => onDuplicateGroupClick( event ) }
aria-label={ __( 'Duplicate this field group for the Pod', 'pods' ) }
>
{ __( 'Duplicate', 'pods' ) }
</button>
|
<button
className="pods-field-group_button pods-field-group_delete"
onClick={ onDeleteGroupClick }
Expand Down Expand Up @@ -305,9 +344,11 @@ FieldGroup.propTypes = {
hasMovedFields: PropTypes.bool.isRequired,
saveStatus: PropTypes.string,
saveMessage: PropTypes.string,
duplicateStatus: PropTypes.string,
deleteStatus: PropTypes.string,

toggleExpanded: PropTypes.func.isRequired,
duplicateGroup: PropTypes.func.isRequired,
deleteGroup: PropTypes.func.isRequired,
removeGroupFromPod: PropTypes.func.isRequired,
saveGroup: PropTypes.func.isRequired,
Expand Down
Loading
Loading