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

263 lines 7.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 add_filter( 'convertkit_output_append_form_to_content_form_id', array( $this, 'preview_form' ), 99999 );
29 add_filter( 'convertkit_block_form_render', array( $this, 'maybe_append_edit_form_link_to_form_block' ), 10, 3 );
30 add_filter( 'convertkit_frontend_append_form', array( $this, 'maybe_append_edit_form_link_to_form' ), 10, 4 );
31
32 }
33
34
35 /**
36 * Changes the form to display for the given Post ID if the request is
37 * from a logged in user who has clicked a preview link in the Plugin Setup Wizard.
38 *
39 * @since 1.9.8.5
40 *
41 * @param int $form_id ConvertKit Form ID.
42 * @return int ConvertKit Form ID
43 */
44 public function preview_form( $form_id ) {
45
46 // Bail if the user isn't logged in.
47 if ( ! is_user_logged_in() ) {
48 return $form_id;
49 }
50
51 // Bail if no nonce field exists.
52 if ( ! isset( $_REQUEST['convertkit-preview-form-nonce'] ) ) {
53 return $form_id;
54 }
55
56 // Bail if the nonce verification fails.
57 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['convertkit-preview-form-nonce'] ) ), 'convertkit-preview-form' ) ) {
58 return $form_id;
59 }
60
61 // Determine the form to preview.
62 $preview_form_id = (int) ( isset( $_REQUEST['convertkit_form_id'] ) ? sanitize_text_field( $_REQUEST['convertkit_form_id'] ) : 0 );
63
64 // Return.
65 return $preview_form_id;
66
67 }
68
69 /**
70 * Returns the URL for the most recent published Post based on the supplied Post Type,
71 * with a preview form nonce included in the URL.
72 *
73 * @since 1.9.8.5
74 *
75 * @param string $post_type Post Type.
76 * @return bool|string false | URL
77 */
78 public function get_preview_form_url( $post_type = 'post' ) {
79
80 // Get most recently published Post/Page ID.
81 $post_id = $this->get_most_recent( $post_type );
82
83 // Bail if no Post/Page exists.
84 if ( ! $post_id ) {
85 return false;
86 }
87
88 // Return preview URL.
89 return add_query_arg(
90 array(
91 'convertkit-preview-form-nonce' => $this->get_preview_form_nonce(),
92 ),
93 get_permalink( $post_id )
94 );
95
96 }
97
98 /**
99 * Appends an "Edit form in ConvertKit" link to the given ConvertKit Form block,
100 * if the request is to preview a Page and the logged in WordPress user can
101 * edit the Page.
102 *
103 * @since 2.0.8
104 *
105 * @param string $form_html ConvertKit Form HTML.
106 * @param array $atts ConvertKit Form block attributes.
107 * @param int $form_id ConvertKit Form ID.
108 * @return string
109 */
110 public function maybe_append_edit_form_link_to_form_block( $form_html, $atts, $form_id ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
111
112 return $this->maybe_append_edit_form_link( $form_html, $form_id );
113
114 }
115
116 /**
117 * Appends an "Edit form in ConvertKit" link to the ConvertKit Form defined in
118 * the Page's settings, if the request is to preview a Page and the
119 * logged in WordPress user can edit the Page.
120 *
121 * @since 2.0.8
122 *
123 * @param string $content Post Content, including ConvertKit Form HTML.
124 * @param string $form_html ConvertKit Form HTML.
125 * @param int $post_id Post ID.
126 * @param int $form_id ConvertKit Form ID.
127 */
128 public function maybe_append_edit_form_link_to_form( $content, $form_html, $post_id, $form_id ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
129
130 return $this->maybe_append_edit_form_link( $content, $form_id );
131
132 }
133
134 /**
135 * Appends an "Edit form in ConvertKit" link to the given ConvertKit Form HTML,
136 * if the request is to preview a Page and the logged in WordPress user can
137 * edit the Page.
138 *
139 * @since 2.0.8
140 *
141 * @param string $form_html ConvertKit Form HTML.
142 * @param int $form_id ConvertKit Form ID.
143 * @return string
144 */
145 private function maybe_append_edit_form_link( $form_html, $form_id ) {
146
147 // Bail if the user isn't logged in.
148 if ( ! is_user_logged_in() ) {
149 return $form_html;
150 }
151
152 // Bail if the request isn't to preview a Page.
153 if ( ! is_preview() ) {
154 return $form_html;
155 }
156
157 // Bail if the user does not have the WordPress capabilities to edit the Page / Post.
158 if ( ! current_user_can( 'edit_post', get_the_ID() ) ) {
159 return $form_html;
160 }
161
162 // Fetch Form.
163 $convertkit_forms = new ConvertKit_Resource_Forms();
164 $form = $convertkit_forms->get_by_id( (int) $form_id );
165
166 // Bail if the Form doesn't exist (this shouldn't happen, but you never know).
167 if ( ! $form ) {
168 return $form_html;
169 }
170
171 // Bail if the Form's format isn't an inline form - we don't want to show an edit link for
172 // e.g. a sticky bar form.
173 // Legacy Forms won't have a format array key.
174 if ( array_key_exists( 'format', $form ) && $form['format'] !== 'inline' ) {
175 return $form_html;
176 }
177
178 // If no format array key is specified, this is a Legacy Form, which has a different edit URL on ConvertKit.
179 if ( ! array_key_exists( 'format', $form ) ) {
180 // Legacy Form.
181 $link = add_query_arg(
182 array(
183 'utm_source' => 'wordpress',
184 'utm_term' => get_locale(),
185 'utm_content' => 'convertkit',
186 ),
187 sprintf(
188 'https://app.convertkit.com/landing_pages/%s/edit/',
189 esc_attr( (string) $form_id )
190 )
191 );
192 } else {
193 $link = add_query_arg(
194 array(
195 'utm_source' => 'wordpress',
196 'utm_term' => get_locale(),
197 'utm_content' => 'convertkit',
198 ),
199 sprintf(
200 'https://app.convertkit.com/forms/designers/%s/edit/',
201 esc_attr( (string) $form_id )
202 )
203 );
204 }
205
206 // Append a link to edit the Form on ConvertKit.
207 $form_html .= sprintf(
208 '<div style="margin:0;padding:5px;text-align:right;font-size:13px;"><a href="%s" target="_blank">%s</a></div>',
209 esc_url( $link ),
210 esc_html__( 'Edit form in ConvertKit', 'convertkit' )
211 );
212
213 return $form_html;
214
215 }
216
217 /**
218 * Returns the nonce to preview a form on a Page, Post or Custom Post Type.
219 *
220 * @since 1.9.8.5
221 *
222 * @return string Nonce
223 */
224 private function get_preview_form_nonce() {
225
226 return wp_create_nonce( 'convertkit-preview-form' );
227
228 }
229
230 /**
231 * Returns the most recent published Post ID for the given Post Type.
232 *
233 * @since 1.9.8.5
234 *
235 * @param string $post_type Post Type.
236 * @return false|int Post ID
237 */
238 private function get_most_recent( $post_type = 'post' ) {
239
240 // Run query.
241 $query = new WP_Query(
242 array(
243 'post_type' => $post_type,
244 'post_status' => 'publish',
245 'posts_per_page' => 1,
246 'orderby' => 'date',
247 'order' => 'DESC',
248 'fields' => 'ids',
249 )
250 );
251
252 // Return false if no Posts exist for the given type.
253 if ( empty( $query->posts ) ) {
254 return false;
255 }
256
257 // Return the Post ID.
258 return $query->posts[0];
259
260 }
261
262 }
263