PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.4.1
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.4.1
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / includes / class-convertkit-preview-output.php

class-convertkit-preview-output.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.4.1, at includes/class-convertkit-preview-output.php

345 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Preview Output class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Handles:
11 * - previewing a supplied Form ID in the URL when viewing a Page or Post,
12 * when the user clicks a preview link in the Plugin Setup Wizard,
13 * - appending an 'Edit form in ConvertKit' link when previewing a Page or Post
14 * as a user who can edit the Page.
15 *
16 * @package ConvertKit
17 * @author ConvertKit
18 */
19 class ConvertKit_Preview_Output {
20
21 /**
22 * Registers action and filter hooks.
23 *
24 * @since 1.9.8.5
25 */
26 public function __construct() {
27
28 // Preview inline forms.
29 add_filter( 'convertkit_output_append_form_to_content_form_id', array( $this, 'preview_form' ), 99999 );
30
31 // Preview non-inline forms.
32 add_filter( 'template_redirect', array( $this, 'preview_non_inline_form' ) );
33
34 // Append edit link to inline forms.
35 add_filter( 'convertkit_block_form_render', array( $this, 'maybe_append_edit_form_link_to_form_block' ), 10, 3 );
36 add_filter( 'convertkit_frontend_append_form', array( $this, 'maybe_append_edit_form_link_to_form' ), 10, 4 );
37
38 }
39
40 /**
41 * Changes the form to display for the given Post ID if the request is
42 * from a logged in user who has clicked a preview link in the Plugin Setup Wizard.
43 *
44 * @since 1.9.8.5
45 *
46 * @param int $form_id ConvertKit Form ID.
47 * @return int ConvertKit Form ID
48 */
49 public function preview_form( $form_id ) {
50
51 // Bail if the user isn't logged in.
52 if ( ! is_user_logged_in() ) {
53 return $form_id;
54 }
55
56 // Bail if no nonce field exists.
57 if ( ! isset( $_REQUEST['convertkit-preview-form-nonce'] ) ) {
58 return $form_id;
59 }
60
61 // Bail if the nonce verification fails.
62 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['convertkit-preview-form-nonce'] ) ), 'convertkit-preview-form' ) ) {
63 return $form_id;
64 }
65
66 // Determine the form to preview.
67 $preview_form_id = (int) ( isset( $_REQUEST['convertkit_form_id'] ) ? sanitize_text_field( $_REQUEST['convertkit_form_id'] ) : 0 );
68
69 // Return.
70 return $preview_form_id;
71
72 }
73
74 /**
75 * Adds a non-inline form for display if the request is from a logged in user who has clicked a preview link
76 * and is viewing the home page.
77 *
78 * @since 2.3.3
79 */
80 public function preview_non_inline_form() {
81
82 // Bail if not on the home page.
83 if ( ! is_home() ) {
84 return;
85 }
86
87 // Bail if the user isn't logged in.
88 if ( ! is_user_logged_in() ) {
89 return;
90 }
91
92 // Bail if no nonce field exists.
93 if ( ! isset( $_REQUEST['convertkit-preview-form-nonce'] ) ) {
94 return;
95 }
96
97 // Bail if the nonce verification fails.
98 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['convertkit-preview-form-nonce'] ) ), 'convertkit-preview-form' ) ) {
99 return;
100 }
101
102 // Determine the form to preview.
103 $preview_form_id = (int) ( isset( $_REQUEST['convertkit_form_id'] ) ? sanitize_text_field( $_REQUEST['convertkit_form_id'] ) : 0 );
104
105 // Get form.
106 $convertkit_forms = new ConvertKit_Resource_Forms();
107 $form = $convertkit_forms->get_by_id( $preview_form_id );
108
109 // Bail if the Form doesn't exist (this shouldn't happen, but you never know).
110 if ( ! $form ) {
111 return;
112 }
113
114 // Add the form to the scripts array so it is included in the preview.
115 add_filter(
116 'convertkit_output_scripts_footer',
117 function ( $scripts ) use ( $form ) {
118
119 $scripts[] = array(
120 'async' => true,
121 'data-uid' => $form['uid'],
122 'src' => $form['embed_js'],
123 );
124
125 return $scripts;
126
127 }
128 );
129
130 }
131
132 /**
133 * Returns the URL for the most recent published Post based on the supplied Post Type,
134 * with a preview form nonce included in the URL.
135 *
136 * @since 1.9.8.5
137 *
138 * @param string $post_type Post Type.
139 * @return bool|string false | URL
140 */
141 public function get_preview_form_url( $post_type = 'post' ) {
142
143 // Get most recently published Post/Page ID.
144 $post_id = $this->get_most_recent( $post_type );
145
146 // Bail if no Post/Page exists.
147 if ( ! $post_id ) {
148 return false;
149 }
150
151 // Return preview URL.
152 return add_query_arg(
153 array(
154 'convertkit-preview-form-nonce' => $this->get_preview_form_nonce(),
155 ),
156 get_permalink( $post_id )
157 );
158
159 }
160
161 /**
162 * Returns the URL for the home page, with a preview form nonce included in the URL.
163 *
164 * @since 2.3.3
165 *
166 * @return string
167 */
168 public function get_preview_form_home_url() {
169
170 // Return preview URL.
171 return add_query_arg(
172 array(
173 'convertkit-preview-form-nonce' => $this->get_preview_form_nonce(),
174 ),
175 get_home_url()
176 );
177
178 }
179
180 /**
181 * Appends an "Edit form in ConvertKit" link to the given ConvertKit Form block,
182 * if the request is to preview a Page and the logged in WordPress user can
183 * edit the Page.
184 *
185 * @since 2.0.8
186 *
187 * @param string $form_html ConvertKit Form HTML.
188 * @param array $atts ConvertKit Form block attributes.
189 * @param int $form_id ConvertKit Form ID.
190 * @return string
191 */
192 public function maybe_append_edit_form_link_to_form_block( $form_html, $atts, $form_id ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
193
194 return $this->maybe_append_edit_form_link( $form_html, $form_id );
195
196 }
197
198 /**
199 * Appends an "Edit form in ConvertKit" link to the ConvertKit Form defined in
200 * the Page's settings, if the request is to preview a Page and the
201 * logged in WordPress user can edit the Page.
202 *
203 * @since 2.0.8
204 *
205 * @param string $content Post Content, including ConvertKit Form HTML.
206 * @param string $form_html ConvertKit Form HTML.
207 * @param int $post_id Post ID.
208 * @param int $form_id ConvertKit Form ID.
209 */
210 public function maybe_append_edit_form_link_to_form( $content, $form_html, $post_id, $form_id ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
211
212 return $this->maybe_append_edit_form_link( $content, $form_id );
213
214 }
215
216 /**
217 * Appends an "Edit form in ConvertKit" link to the given ConvertKit Form HTML,
218 * if the request is to preview a Page and the logged in WordPress user can
219 * edit the Page.
220 *
221 * @since 2.0.8
222 *
223 * @param string $form_html ConvertKit Form HTML.
224 * @param int $form_id ConvertKit Form ID.
225 * @return string
226 */
227 private function maybe_append_edit_form_link( $form_html, $form_id ) {
228
229 // Bail if the user isn't logged in.
230 if ( ! is_user_logged_in() ) {
231 return $form_html;
232 }
233
234 // Bail if the request isn't to preview a Page.
235 if ( ! is_preview() ) {
236 return $form_html;
237 }
238
239 // Bail if the user does not have the WordPress capabilities to edit the Page / Post.
240 if ( ! current_user_can( 'edit_post', get_the_ID() ) ) {
241 return $form_html;
242 }
243
244 // Fetch Form.
245 $convertkit_forms = new ConvertKit_Resource_Forms();
246 $form = $convertkit_forms->get_by_id( (int) $form_id );
247
248 // Bail if the Form doesn't exist (this shouldn't happen, but you never know).
249 if ( ! $form ) {
250 return $form_html;
251 }
252
253 // Bail if the Form's format isn't an inline form - we don't want to show an edit link for
254 // e.g. a sticky bar form.
255 // Legacy Forms won't have a format array key.
256 if ( array_key_exists( 'format', $form ) && $form['format'] !== 'inline' ) {
257 return $form_html;
258 }
259
260 // If no format array key is specified, this is a Legacy Form, which has a different edit URL on ConvertKit.
261 if ( ! array_key_exists( 'format', $form ) ) {
262 // Legacy Form.
263 $link = add_query_arg(
264 array(
265 'utm_source' => 'wordpress',
266 'utm_term' => get_locale(),
267 'utm_content' => 'convertkit',
268 ),
269 sprintf(
270 'https://app.convertkit.com/landing_pages/%s/edit/',
271 esc_attr( (string) $form_id )
272 )
273 );
274 } else {
275 $link = add_query_arg(
276 array(
277 'utm_source' => 'wordpress',
278 'utm_term' => get_locale(),
279 'utm_content' => 'convertkit',
280 ),
281 sprintf(
282 'https://app.convertkit.com/forms/designers/%s/edit/',
283 esc_attr( (string) $form_id )
284 )
285 );
286 }
287
288 // Append a link to edit the Form on ConvertKit.
289 $form_html .= sprintf(
290 '<div style="margin:0;padding:5px;text-align:right;font-size:13px;"><a href="%s" target="_blank">%s</a></div>',
291 esc_url( $link ),
292 esc_html__( 'Edit form in ConvertKit', 'convertkit' )
293 );
294
295 return $form_html;
296
297 }
298
299 /**
300 * Returns the nonce to preview a form on a Page, Post or Custom Post Type.
301 *
302 * @since 1.9.8.5
303 *
304 * @return string Nonce
305 */
306 private function get_preview_form_nonce() {
307
308 return wp_create_nonce( 'convertkit-preview-form' );
309
310 }
311
312 /**
313 * Returns the most recent published Post ID for the given Post Type.
314 *
315 * @since 1.9.8.5
316 *
317 * @param string $post_type Post Type.
318 * @return false|int Post ID
319 */
320 private function get_most_recent( $post_type = 'post' ) {
321
322 // Run query.
323 $query = new WP_Query(
324 array(
325 'post_type' => $post_type,
326 'post_status' => 'publish',
327 'posts_per_page' => 1,
328 'orderby' => 'date',
329 'order' => 'DESC',
330 'fields' => 'ids',
331 )
332 );
333
334 // Return false if no Posts exist for the given type.
335 if ( empty( $query->posts ) ) {
336 return false;
337 }
338
339 // Return the Post ID.
340 return $query->posts[0];
341
342 }
343
344 }
345