PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / trunk
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages vtrunk
3.4.2 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 All 195 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 trunk, at includes/class-convertkit-preview-output.php

352 lines 9.1 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 = ( isset( $_REQUEST['convertkit_form_id'] ) ? absint( wp_unslash( $_REQUEST['convertkit_form_id'] ) ) : 0 );
68
69 // Return.
70 return $preview_form_id;
71
72 }
73
74 /**
75 * Adds non-inline form(s) 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 // Bail if no form(s) to preview.
103 if ( ! isset( $_REQUEST['convertkit_form_id'] ) ) {
104 return;
105 }
106
107 // Determine form ID(s) to preview.
108 $convertkit_forms = new ConvertKit_Resource_Forms();
109 $preview_form_ids = explode( ',', sanitize_text_field( wp_unslash( $_REQUEST['convertkit_form_id'] ) ) );
110
111 foreach ( $preview_form_ids as $preview_form_id ) {
112 // Get form.
113 $form = $convertkit_forms->get_by_id( (int) $preview_form_id );
114
115 // Bail if the Form doesn't exist (this shouldn't happen, but you never know).
116 if ( ! $form ) {
117 continue;
118 }
119
120 // Add the form to the scripts array so it is included in the preview.
121 add_filter(
122 'convertkit_output_scripts_footer',
123 function ( $scripts ) use ( $form ) {
124
125 $scripts[] = array(
126 'async' => true,
127 'data-uid' => $form['uid'],
128 'src' => $form['embed_js'],
129 );
130
131 return $scripts;
132
133 }
134 );
135 }
136
137 }
138
139 /**
140 * Returns the URL for the most recent published Post based on the supplied Post Type,
141 * with a preview form nonce included in the URL.
142 *
143 * @since 1.9.8.5
144 *
145 * @param string $post_type Post Type.
146 * @return bool|string false | URL
147 */
148 public function get_preview_form_url( $post_type = 'post' ) {
149
150 // Get most recently published Post/Page ID.
151 $post_id = $this->get_most_recent( $post_type );
152
153 // Bail if no Post/Page exists.
154 if ( ! $post_id ) {
155 return false;
156 }
157
158 // Return preview URL.
159 return add_query_arg(
160 array(
161 'convertkit-preview-form-nonce' => $this->get_preview_form_nonce(),
162 ),
163 get_permalink( $post_id )
164 );
165
166 }
167
168 /**
169 * Returns the URL for the home page, with a preview form nonce included in the URL.
170 *
171 * @since 2.3.3
172 *
173 * @return string
174 */
175 public function get_preview_form_home_url() {
176
177 // Return preview URL.
178 return add_query_arg(
179 array(
180 'convertkit-preview-form-nonce' => $this->get_preview_form_nonce(),
181 ),
182 get_home_url()
183 );
184
185 }
186
187 /**
188 * Appends an "Edit form in ConvertKit" link to the given ConvertKit Form block,
189 * if the request is to preview a Page and the logged in WordPress user can
190 * edit the Page.
191 *
192 * @since 2.0.8
193 *
194 * @param string $form_html ConvertKit Form HTML.
195 * @param array $atts ConvertKit Form block attributes.
196 * @param int $form_id ConvertKit Form ID.
197 * @return string
198 */
199 public function maybe_append_edit_form_link_to_form_block( $form_html, $atts, $form_id ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
200
201 return $this->maybe_append_edit_form_link( $form_html, $form_id );
202
203 }
204
205 /**
206 * Appends an "Edit form in ConvertKit" link to the ConvertKit Form defined in
207 * the Page's settings, if the request is to preview a Page and the
208 * logged in WordPress user can edit the Page.
209 *
210 * @since 2.0.8
211 *
212 * @param string $content Post Content, including ConvertKit Form HTML.
213 * @param string $form_html ConvertKit Form HTML.
214 * @param int $post_id Post ID.
215 * @param int $form_id ConvertKit Form ID.
216 */
217 public function maybe_append_edit_form_link_to_form( $content, $form_html, $post_id, $form_id ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
218
219 return $this->maybe_append_edit_form_link( $content, $form_id );
220
221 }
222
223 /**
224 * Appends an "Edit form in ConvertKit" link to the given ConvertKit Form HTML,
225 * if the request is to preview a Page and the logged in WordPress user can
226 * edit the Page.
227 *
228 * @since 2.0.8
229 *
230 * @param string $form_html ConvertKit Form HTML.
231 * @param int $form_id ConvertKit Form ID.
232 * @return string
233 */
234 private function maybe_append_edit_form_link( $form_html, $form_id ) {
235
236 // Bail if the user isn't logged in.
237 if ( ! is_user_logged_in() ) {
238 return $form_html;
239 }
240
241 // Bail if the request isn't to preview a Page.
242 if ( ! is_preview() ) {
243 return $form_html;
244 }
245
246 // Bail if the user does not have the WordPress capabilities to edit the Page / Post.
247 if ( ! current_user_can( 'edit_post', get_the_ID() ) ) {
248 return $form_html;
249 }
250
251 // Fetch Form.
252 $convertkit_forms = new ConvertKit_Resource_Forms();
253 $form = $convertkit_forms->get_by_id( (int) $form_id );
254
255 // Bail if the Form doesn't exist (this shouldn't happen, but you never know).
256 if ( ! $form ) {
257 return $form_html;
258 }
259
260 // Bail if the Form's format isn't an inline form - we don't want to show an edit link for
261 // e.g. a sticky bar form.
262 // Legacy Forms won't have a format array key.
263 if ( array_key_exists( 'format', $form ) && $form['format'] !== 'inline' ) {
264 return $form_html;
265 }
266
267 // If no format array key is specified, this is a Legacy Form, which has a different edit URL on ConvertKit.
268 if ( ! array_key_exists( 'format', $form ) ) {
269 // Legacy Form.
270 $link = add_query_arg(
271 array(
272 'utm_source' => 'wordpress',
273 'utm_term' => get_locale(),
274 'utm_content' => 'convertkit',
275 ),
276 sprintf(
277 'https://app.kit.com/landing_pages/%s/edit/',
278 esc_attr( (string) $form_id )
279 )
280 );
281 } else {
282 $link = add_query_arg(
283 array(
284 'utm_source' => 'wordpress',
285 'utm_term' => get_locale(),
286 'utm_content' => 'convertkit',
287 ),
288 sprintf(
289 'https://app.kit.com/forms/designers/%s/edit/',
290 esc_attr( (string) $form_id )
291 )
292 );
293 }
294
295 // Append a link to edit the Form on ConvertKit.
296 $form_html .= sprintf(
297 '<div style="margin:0;padding:5px;text-align:right;font-size:13px;"><a href="%s" target="_blank">%s</a></div>',
298 esc_url( $link ),
299 esc_html__( 'Edit form in Kit', 'convertkit' )
300 );
301
302 return $form_html;
303
304 }
305
306 /**
307 * Returns the nonce to preview a form on a Page, Post or Custom Post Type.
308 *
309 * @since 1.9.8.5
310 *
311 * @return string Nonce
312 */
313 private function get_preview_form_nonce() {
314
315 return wp_create_nonce( 'convertkit-preview-form' );
316
317 }
318
319 /**
320 * Returns the most recent published Post ID for the given Post Type.
321 *
322 * @since 1.9.8.5
323 *
324 * @param string $post_type Post Type.
325 * @return false|int Post ID
326 */
327 private function get_most_recent( $post_type = 'post' ) {
328
329 // Run query.
330 $query = new WP_Query(
331 array(
332 'post_type' => $post_type,
333 'post_status' => 'publish',
334 'posts_per_page' => 1,
335 'orderby' => 'date',
336 'order' => 'DESC',
337 'fields' => 'ids',
338 )
339 );
340
341 // Return false if no Posts exist for the given type.
342 if ( empty( $query->posts ) ) {
343 return false;
344 }
345
346 // Return the Post ID.
347 return $query->posts[0];
348
349 }
350
351 }
352