PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.9.7.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.9.7.2
3.4.3 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 All 196 releases
← All changes | includes/class-convertkit-output.php +22 -120 2.2.81.9.7.2 View file →
@@ -14,17 +14,8 @@
14 14 */
15 15 class ConvertKit_Output {
16 16
17 17 /**
18 - * Holds the ConvertKit Subscriber ID.
19 - *
20 - * @since 2.0.6
21 - *
22 - * @var int|string
23 - */
24 - private $subscriber_id = 0;
25 -
26 - /**
27 18 * Holds the ConvertKit Plugin Settings class
28 19 *
29 20 * @since 1.9.6
30 21 *
@@ -66,14 +57,12 @@
66 57 * @since 1.9.6
67 58 */
68 59 public function __construct() {
69 60
70 - add_action( 'init', array( $this, 'get_subscriber_id_from_request' ), 1 );
71 61 add_action( 'template_redirect', array( $this, 'output_form' ) );
72 62 add_action( 'template_redirect', array( $this, 'page_takeover' ) );
73 63 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
74 64 add_filter( 'the_content', array( $this, 'append_form_to_content' ) );
75 - add_action( 'wp_footer', array( $this, 'output_scripts_footer' ) );
76 65
77 66 }
78 67
79 68 /**
@@ -136,9 +125,9 @@
136 125 *
137 126 * @param int $landing_page_id Landing Page ID
138 127 * @param int $post_id Post ID
139 128 */
140 - $landing_page_id = apply_filters( 'convertkit_output_page_takeover_landing_page_id', $landing_page_id, $post_id );
129 + $landing_page_id = apply_filters( 'convertkit_output_append_form_to_content_form_id', $landing_page_id, $post_id );
141 130
142 131 // Bail if no Landing Page is configured to be output.
143 132 if ( empty( $landing_page_id ) ) {
144 133 return;
@@ -145,9 +134,9 @@
145 134 }
146 135
147 136 // Get available ConvertKit Landing Pages, if they have not yet been loaded.
148 137 if ( ! $this->landing_pages ) {
149 - $this->landing_pages = new ConvertKit_Resource_Landing_Pages( 'output_landing_page' );
138 + $this->landing_pages = new ConvertKit_Resource_Landing_Pages();
150 139 }
151 140
152 141 // Get Landing Page.
153 142 $landing_page = $this->landing_pages->get_html( $this->post_settings->get_landing_page() );
@@ -157,10 +146,9 @@
157 146 return;
158 147 }
159 148
160 149 // Output Landing Page.
161 - // Output is supplied from ConvertKit's API, which is already sanitized.
162 - echo $landing_page; // phpcs:ignore WordPress.Security.EscapeOutput
150 + echo $landing_page; // phpcs:ignore
163 151 exit;
164 152
165 153 }
166 154
@@ -188,14 +176,14 @@
188 176 * Return false to not display any ConvertKit Form.
189 177 *
190 178 * @since 1.9.6
191 179 *
192 - * @param bool|int $form_id Form ID
193 - * @param int $post_id Post ID
180 + * @param int $form_id Form ID
181 + * @param int $post_id Post ID
194 182 */
195 183 $form_id = apply_filters( 'convertkit_output_append_form_to_content_form_id', $form_id, $post_id );
196 184
197 - // Return the Post Content, unedited, if the Form ID is false or zero.
185 + // Return the Post Content, unedited, if no Form ID exists.
198 186 if ( ! $form_id ) {
199 187 return $content;
200 188 }
201 189
@@ -200,50 +188,19 @@
200 188 }
201 189
202 190 // Get available ConvertKit Forms, if they have not yet been loaded.
203 191 if ( ! $this->forms ) {
204 - $this->forms = new ConvertKit_Resource_Forms( 'output_form' );
192 + $this->forms = new ConvertKit_Resource_Forms();
205 193 }
206 194
207 195 // Get Form HTML.
208 196 $form = $this->forms->get_html( $form_id );
209 197
210 - // If an error occured, it could be because the specified Form ID for the Post either:
211 - // - belongs to another ConvertKit account (i.e. API credentials were changed in the Plugin, but this Post's specified Form was not changed), or
212 - // - the form was deleted from the ConvertKit account.
213 - // Attempt to fallback to the default form for this Post Type.
198 + // Return the Post Content, unedited, if an error occured.
214 199 if ( is_wp_error( $form ) ) {
215 - if ( $this->settings->debug_enabled() ) {
216 - $content .= '<!-- ConvertKit append_form_to_content(): ' . $form->get_error_message() . ' Attempting fallback to Default Form. -->';
217 - }
218 -
219 - // Get Default Form ID for this Post's Type.
220 - $form_id = $this->settings->get_default_form( get_post_type( $post_id ) );
221 -
222 - // If no Default Form is specified, just return the Post Content, unedited.
223 - if ( ! $form_id ) {
224 - if ( $this->settings->debug_enabled() ) {
225 - $content .= '<!-- ConvertKit append_form_to_content(): No Default Form exists as a fallback. -->';
226 - }
227 -
228 - return $content;
229 - }
230 -
231 - // Get Form HTML.
232 - $form = $this->forms->get_html( $form_id );
233 -
234 - // If an error occured again, the default form doesn't exist in this ConvertKit account.
235 - // Just return the Post Content, unedited.
236 - if ( is_wp_error( $form ) ) {
237 - if ( $this->settings->debug_enabled() ) {
238 - $content .= '<!-- ConvertKit append_form_to_content(): Default Form: ' . $form->get_error_message() . ' -->';
239 - }
240 -
241 - return $content;
242 - }
200 + return $content;
243 201 }
244 202
245 - // If here, we have a ConvertKit Form.
246 203 // Append form to Post's Content.
247 204 $content = $content .= $form;
248 205
249 206 /**
@@ -370,9 +327,9 @@
370 327 array(
371 328 'ajaxurl' => admin_url( 'admin-ajax.php' ),
372 329 'debug' => $settings->debug_enabled(),
373 330 'nonce' => wp_create_nonce( 'convertkit' ),
374 - 'subscriber_id' => $this->subscriber_id,
331 + 'subscriber_id' => $this->get_subscriber_id_from_request(),
375 332 'tag' => ( ( is_singular() && $convertkit_post->has_tag() ) ? $convertkit_post->get_tag() : false ),
376 333 'post_id' => $post->ID,
377 334 )
378 335 );
@@ -390,82 +347,27 @@
390 347 /**
391 348 * Gets the subscriber ID from the request (either the cookie or the URL).
392 349 *
393 350 * @since 1.9.6
351 + *
352 + * @return int Subscriber ID
394 353 */
395 354 public function get_subscriber_id_from_request() {
396 355
397 - // Use ConvertKit_Subscriber class to fetch and validate the subscriber ID.
398 - $subscriber = new ConvertKit_Subscriber();
399 - $subscriber_id = $subscriber->get_subscriber_id();
400 -
401 - // If an error occured, the subscriber ID in the request/cookie is not a valid subscriber.
402 - if ( is_wp_error( $subscriber_id ) ) {
403 - return;
356 + // If the subscriber ID is included in the URL as a query parameter
357 + // (i.e. 'Add subscriber_id parameter in email links' is enabled at https://app.convertkit.com/account_settings/advanced_settings,
358 + // return it as the subscriber ID.
359 + if ( isset( $_GET['ck_subscriber_id'] ) ) { // phpcs:ignore
360 + return absint( $_GET['ck_subscriber_id'] ); // phpcs:ignore
404 361 }
405 362
406 - $this->subscriber_id = $subscriber_id;
407 -
408 - }
409 -
410 - /**
411 - * Outputs any JS <script> tags registered with the convertkit_output_scripts_footer
412 - * filter
413 - *
414 - * @since 2.1.4
415 - */
416 - public function output_scripts_footer() {
417 -
418 - // Define array of scripts.
419 - $scripts = array();
420 -
421 - /**
422 - * Define an array of scripts to output in the footer of the WordPress site.
423 - *
424 - * @since 2.1.4
425 - *
426 - * @param array $scripts Scripts.
427 - */
428 - $scripts = apply_filters( 'convertkit_output_scripts_footer', $scripts );
429 -
430 - // Bail if no scripts exist.
431 - if ( ! count( $scripts ) ) {
432 - return;
363 + // If the subscriber ID is stored as a cookie (i.e. the user subscribed via a form
364 + // from this Plugin on this site, which sets this cookie), return it as the subscriber ID.
365 + if ( isset( $_COOKIE['ck_subscriber_id'] ) ) {
366 + return absint( $_COOKIE['ck_subscriber_id'] );
433 367 }
434 368
435 - // Define array to store <script> outputs.
436 - $output_scripts = array();
437 -
438 - // Iterate through scripts, building the <script> tag for each.
439 - foreach ( $scripts as $script ) {
440 - $output = '<script';
441 -
442 - foreach ( $script as $attribute => $value ) {
443 - // If the value is true, just output the attribute.
444 - if ( $value === true ) {
445 - $output .= ' ' . esc_attr( $attribute );
446 - continue;
447 - }
448 -
449 - // Output the attribute and value.
450 - $output .= ' ' . esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
451 - }
452 - $output .= '></script>';
453 -
454 - // Add to array.
455 - $output_scripts[] = $output;
456 - }
457 -
458 - // Remove duplicate scripts.
459 - // This prevents the same non-inline form displaying twice. For example, if a modal form is specified both
460 - // in the Page's settings and the Form block, the user would see the same modal form displayed twice
461 - // because the script would be output twice.
462 - $output_scripts = array_unique( $output_scripts );
463 -
464 - // Output scripts.
465 - foreach ( $output_scripts as $output_script ) {
466 - echo $output_script . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
467 - }
369 + return 0;
468 370
469 371 }
470 372
471 373 }