PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.0.0
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.0.0
1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / duplicate-form.php

duplicate-form.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 1.0.0, at inc/duplicate-form.php

213 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * SureDonation form duplication.
4 *
5 * @package SureDonation
6 * @since 0.0.1
7 */
8
9 namespace SureDonation\Inc;
10
11 use SureDonation\Inc\Post_Types\Donation_Form;
12 use SureDonation\Inc\Traits\Get_Instance;
13
14 // Exit if accessed directly.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Duplicate Form Class.
21 *
22 * @since 0.0.1
23 */
24 class Duplicate_Form {
25 use Get_Instance;
26
27 /**
28 * Duplicate a form with all its metadata.
29 *
30 * @param int $form_id Form ID to duplicate.
31 * @param string $title_suffix Suffix to append to title. Default ' (Copy)'.
32 * @return array<string, mixed>|\WP_Error Result with new form ID or error.
33 * @since 0.0.1
34 */
35 public function duplicate_form( $form_id, $title_suffix = ' (Copy)' ) {
36 $form_id = intval( $form_id );
37 if ( $form_id <= 0 ) {
38 return new \WP_Error(
39 'invalid_form_id',
40 __( 'Invalid form ID provided.', 'suredonation' ),
41 [ 'status' => 400 ]
42 );
43 }
44
45 $source_form = get_post( $form_id );
46
47 if ( ! $source_form ) {
48 return new \WP_Error(
49 'form_not_found',
50 __( 'Source form not found.', 'suredonation' ),
51 [ 'status' => 404 ]
52 );
53 }
54
55 if ( Donation_Form::POST_TYPE !== $source_form->post_type ) {
56 return new \WP_Error(
57 'invalid_post_type',
58 __( 'The specified post is not a donation form.', 'suredonation' ),
59 [ 'status' => 400 ]
60 );
61 }
62
63 $post_meta = get_post_meta( $form_id );
64 $new_title = $this->generate_unique_title( $source_form->post_title, $title_suffix );
65
66 // wp_insert_post() internally calls wp_unslash() which removes backslashes.
67 // Use wp_slash() to preserve unicode escapes in block attributes.
68 $new_post_args = [
69 'post_title' => $new_title,
70 'post_content' => wp_slash( $source_form->post_content ),
71 'post_status' => 'draft',
72 'post_type' => Donation_Form::POST_TYPE,
73 'post_author' => get_current_user_id(),
74 ];
75
76 $new_form_id = wp_insert_post( $new_post_args );
77
78 if ( ! is_int( $new_form_id ) || $new_form_id <= 0 ) {
79 return new \WP_Error(
80 'duplication_failed',
81 __( 'Failed to create duplicate form.', 'suredonation' ),
82 [ 'status' => 500 ]
83 );
84 }
85
86 // Update formId in Gutenberg blocks.
87 $updated_content = $this->update_block_form_ids( $source_form->post_content, $form_id, $new_form_id );
88
89 wp_update_post(
90 [
91 'ID' => $new_form_id,
92 'post_content' => wp_slash( $updated_content ),
93 ]
94 );
95
96 // Copy all post meta.
97 if ( is_array( $post_meta ) ) {
98 foreach ( $post_meta as $meta_key => $meta_values ) {
99 if ( ! is_string( $meta_key ) ) {
100 continue;
101 }
102
103 // Skip WordPress internal meta keys.
104 if ( '_edit_lock' === $meta_key || '_edit_last' === $meta_key ) {
105 continue;
106 }
107
108 if ( is_array( $meta_values ) && isset( $meta_values[0] ) ) {
109 $meta_value = maybe_unserialize( $meta_values[0] );
110 add_post_meta( $new_form_id, $meta_key, $meta_value );
111 }
112 }
113 }
114
115 /**
116 * Fires after a donation form has been duplicated.
117 *
118 * @param int $new_form_id New form ID.
119 * @param int $form_id Original form ID.
120 * @since 0.0.1
121 */
122 do_action( 'suredonation_after_form_duplicated', $new_form_id, $form_id );
123
124 $edit_url = admin_url( 'post.php?post=' . $new_form_id . '&action=edit' );
125
126 return [
127 'success' => true,
128 'original_form_id' => $form_id,
129 'new_form_id' => $new_form_id,
130 'new_form_title' => $new_title,
131 'edit_url' => $edit_url,
132 ];
133 }
134
135 /**
136 * Handle duplicate form REST API request.
137 *
138 * @param \WP_REST_Request $request Full details about the request.
139 * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error on failure.
140 * @since 0.0.1
141 */
142 public function handle_duplicate_form_rest( $request ) {
143 $form_id = absint( $request->get_param( 'form_id' ) );
144 $title_suffix = sanitize_text_field( $request->get_param( 'title_suffix' ) );
145
146 $result = $this->duplicate_form( $form_id, $title_suffix );
147
148 if ( is_wp_error( $result ) ) {
149 return $result;
150 }
151
152 return new \WP_REST_Response( $result, 200 );
153 }
154
155 /**
156 * Generate unique title by appending suffix.
157 *
158 * @param string $base_title Original form title.
159 * @param string $suffix Suffix to append.
160 * @return string Unique title.
161 * @since 0.0.1
162 */
163 private function generate_unique_title( $base_title, $suffix = ' (Copy)' ) {
164 $new_title = $base_title . $suffix;
165 $counter = 2;
166
167 while ( $this->title_exists( $new_title ) ) {
168 $new_title = $base_title . $suffix . ' ' . $counter;
169 ++$counter;
170 }
171
172 return $new_title;
173 }
174
175 /**
176 * Check if a form title already exists.
177 *
178 * @param string $title Title to check.
179 * @return bool True if title exists.
180 * @since 0.0.1
181 */
182 private function title_exists( $title ) {
183 global $wpdb;
184
185 $query = $wpdb->prepare(
186 "SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s AND post_status != 'trash' LIMIT 1",
187 $title,
188 Donation_Form::POST_TYPE
189 );
190
191 $existing = $wpdb->get_var( $query ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
192
193 return ! empty( $existing );
194 }
195
196 /**
197 * Update formId in Gutenberg blocks.
198 *
199 * @param string $content Post content with blocks.
200 * @param int $old_id Original form ID.
201 * @param int $new_id New form ID.
202 * @return string Updated content.
203 * @since 0.0.1
204 */
205 private function update_block_form_ids( $content, $old_id, $new_id ) {
206 return str_replace(
207 '"formId":' . intval( $old_id ),
208 '"formId":' . intval( $new_id ),
209 $content
210 );
211 }
212 }
213