PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Campaigns / Actions / DuplicateCampaign.php

DuplicateCampaign.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Campaigns/Actions/DuplicateCampaign.php

86 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Campaigns\Actions;
4
5 use Give\Campaigns\Models\Campaign;
6 use Give\Campaigns\Models\CampaignPage;
7 use Give\Campaigns\Repositories\CampaignRepository;
8 use Give\Campaigns\ValueObjects\CampaignPageStatus;
9 use Give_Form_Duplicator;
10
11 class DuplicateCampaign
12 {
13 /**
14 * Duplicate a campaign
15 *
16 * @since 4.13.1
17 */
18 public function __invoke(Campaign $campaign): Campaign
19 {
20 require_once(GIVE_PLUGIN_DIR . '/includes/admin/forms/class-give-form-duplicator.php');
21
22 $forms = $campaign->forms();
23 $campaignRepository = give(CampaignRepository::class);
24
25 $campaign->id = null;
26 $campaign->title = sprintf(__('%s (copy)', 'give'), $campaign->title);
27 $campaign->save();
28
29 foreach ($forms->getAll() as $form) {
30 if (! $post = get_post($form->id)) {
31 continue;
32 }
33
34 $isDefaultForm = $campaign->defaultFormId === $form->id;
35
36 $newFormId = wp_insert_post([
37 'comment_status' => $post->comment_status,
38 'ping_status' => $post->ping_status,
39 'post_author' => get_current_user_id(),
40 'post_content' => $post->post_content,
41 'post_date_gmt' => current_time('mysql', true),
42 'post_excerpt' => $post->post_excerpt,
43 'post_name' => $post->post_name,
44 'post_parent' => $post->post_parent,
45 'post_password' => $post->post_password,
46 'post_status' => $isDefaultForm ? 'publish' : 'draft',
47 'post_title' => $post->post_title,
48 'post_type' => $post->post_type,
49 'to_ping' => $post->to_ping,
50 'menu_order' => $post->menu_order,
51 ]);
52
53 Give_Form_Duplicator::duplicate_taxonomies($newFormId, $post);
54 Give_Form_Duplicator::duplicate_meta_data($newFormId, $post);
55 Give_Form_Duplicator::reset_stats($newFormId);
56
57 if ($isDefaultForm) {
58 $campaign->defaultFormId = $newFormId;
59 }
60
61 $campaignRepository->addCampaignForm($campaign, $newFormId, $isDefaultForm);
62 }
63
64 if ($campaignPage = CampaignPage::find($campaign->pageId)) {
65 $campaignPage->id = null;
66 $campaignPage->status = CampaignPageStatus::DRAFT();
67 $campaignPage->campaignId = $campaign->id;
68
69 // update campaign id attribute
70 $campaignPage->content = preg_replace(
71 '/"campaignId":(\d+)/',
72 '"campaignId":' . $campaign->id,
73 $campaignPage->content
74 );
75
76 $campaignPage->save();
77
78 $campaign->pageId = $campaignPage->id;
79 }
80
81 $campaign->save();
82
83 return $campaign;
84 }
85 }
86