Thank you for your answer. For those who cross by, I have made a method to be used in the WP API that does the job. Company (empresa) has many Collaborators (colaborador) and this code copies the collaborators from one company to another given their slugs.
public function copy_collaborators($request) {
$source_slug = $request->get_param('source_slug');
$target_slug = $request->get_param('target_slug');
// Get the source and target company post IDs
// debug($request->get_params());
// debug($source_slug, $target_slug);
$source_company = get_page_by_path($source_slug, OBJECT, 'empresa');
$target_company = get_page_by_path($target_slug, OBJECT, 'empresa');
if (!$source_company) {
return new WP_REST_Response(['error' => 'Invalid source company slug provided: ' . $source_slug], 404);
}
if (!$target_company) {
return new WP_REST_Response(['error' => 'Invalid target company slug provided: ' . $target_slug], 404);
}
$source_company_id = $source_company->ID;
$target_company_id = $target_company->ID;
// Validate relationship
$source_main_account = get_post_meta($source_company_id, 'wpcf-main-account', true);
$target_main_account = get_post_meta($target_company_id, 'wpcf-main-account', true);
if ($source_main_account && $target_main_account) {
// Both are subaccounts, check if they belong to the same parent account
if ($source_main_account !== $target_main_account) {
return new WP_REST_Response([
'error' => 'Source and target subaccounts must belong to the same parent account',
'source_slug' => $source_slug,
'target_slug' => $target_slug,
'source_main_company' => $source_main_account,
'target_main_company' => $target_main_account,
], 400);
}
} elseif ($source_main_account && !$target_main_account) {
// Source is a subaccount and target is an account, invalid
return new WP_REST_Response([
'error' => 'Cannot copy collaborators from a subaccount to an account',
'source_slug' => $source_slug,
'target_slug' => $target_slug,
'source_main_company' => $source_main_account,
'target_main_company' => $target_main_account,
], 400);
} elseif (!$source_main_account && $target_main_account) {
// Source is an account and target is a subaccount, valid
// Check if the target's main account is the source company
if ($target_main_account !== $source_company->post_title) {
return new WP_REST_Response([
'error' => 'Target subaccount must belong to the source account',
'source_slug' => $source_slug,
'target_slug' => $target_slug,
'source_main_company' => $source_company->post_title,
'target_main_company' => $target_main_account,
], 400);
}
} else {
// Both are accounts, invalid
return new WP_REST_Response(['error' => 'Cannot copy collaborators from an account to another account'], 400);
}
// Get collaborators of the source company
$collaborators = toolset_get_related_posts($source_company_id, 'empresa_colaborador', [
'query_by_role' => 'parent',
'return' => 'post_object',
'posts_per_page' => -1
]);
$num_source_collabs = count($collaborators);
$num_collabs_created = 0;
$num_collabs_skipped = 0;
$collabs_skipped = [];
foreach ($collaborators as $collaborator) {
// Retrieve the author of the source collaborator
$author_id = $collaborator->post_author;
$author = get_userdata($author_id);
$author_display_name = $author->display_name;
// Check if the collaborator already exists for this user and target company
$existing_collabs = get_posts([
'post_type' => 'colaborador',
'posts_per_page' => -1,
'author' => $author_id,
]);
$found_parent = null;
foreach ($existing_collabs as $existing_collab) {
$parent = toolset_get_related_posts($existing_collab->ID, 'empresa_colaborador', [
'query_by_role' => 'child',
'return' => 'post_object'
]);
if ($parent && $parent[0] && $parent[0]->ID == $target_company_id) {
$found_parent = $parent[0];
break;
}
}
// If a collaborator already exists, skip creating a new one
if ($found_parent) {
$num_collabs_skipped++;
$collabs_skipped[] = [
'collaborator_id' => $collaborator->ID,
'collaborator_name' => $collaborator->post_title,
'reason' => 'Collaborator already exists for target company'
];
continue;
}
// Construct the new title
$new_title = $author_display_name . ' - ' . $target_company->post_title;
// Duplicate collaborator with the updated author and title
$new_collaborator_id = wp_insert_post(array(
'post_title' => $new_title,
'post_content' => $collaborator->post_content,
'post_status' => 'publish',
'post_type' => 'colaborador',
'post_author' => $author_id
));
// Copy custom fields and associate with target company
if ($new_collaborator_id) {
$custom_fields = get_post_custom($collaborator->ID);
foreach ($custom_fields as $key => $value) {
update_post_meta($new_collaborator_id, $key, maybe_unserialize($value[0]));
}
// Connect the new collaborator with the target company
toolset_connect_posts('empresa_colaborador', $target_company_id, $new_collaborator_id);
$num_collabs_created++;
}
}
$response_data = [
'source_company' => [
'id' => $source_company->ID,
'name' => $source_company->post_title,
'slug' => $source_company->post_name
],
'target_company' => [
'id' => $target_company->ID,
'name' => $target_company->post_title,
'slug' => $target_company->post_name
],
'num_source_collabs' => $num_source_collabs,
'num_collabs_created' => $num_collabs_created,
'num_collabs_skipped' => $num_collabs_skipped,
'collabs_skipped' => $collabs_skipped,
'total_collabs_processed' => $num_source_collabs
];
return new WP_REST_Response($response_data, 200);
}