PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.0.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.0.6
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-output.php

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

410 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Output class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Outputs Forms and Landing Pages on the frontend web site, based on
11 * the Post and Plugin's configuration.
12 *
13 * @since 1.9.6
14 */
15 class ConvertKit_Output {
16
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 * Holds the ConvertKit Plugin Settings class
28 *
29 * @since 1.9.6
30 *
31 * @var bool|ConvertKit_Settings
32 */
33 private $settings = false;
34
35 /**
36 * Holds the ConvertKit Post Settings class
37 *
38 * @since 1.9.6
39 *
40 * @var bool|ConvertKit_Post
41 */
42 private $post_settings = false;
43
44 /**
45 * Holds the available ConvertKit Forms
46 *
47 * @since 1.9.6
48 *
49 * @var bool|ConvertKit_Resource_Forms
50 */
51 private $forms = false;
52
53 /**
54 * Holds the available ConvertKit Landing Pages
55 *
56 * @since 1.9.6
57 *
58 * @var bool|ConvertKit_Resource_Landing_Pages
59 */
60 private $landing_pages = false;
61
62 /**
63 * Constructor. Registers actions and filters to output ConvertKit Forms and Landing Pages
64 * on the frontend web site.
65 *
66 * @since 1.9.6
67 */
68 public function __construct() {
69
70 add_action( 'init', array( $this, 'get_subscriber_id_from_request' ), 1 );
71 add_action( 'template_redirect', array( $this, 'output_form' ) );
72 add_action( 'template_redirect', array( $this, 'page_takeover' ) );
73 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
74 add_filter( 'the_content', array( $this, 'append_form_to_content' ) );
75
76 }
77
78 /**
79 * Runs the `convertkit_output_output_form` action for singular Post Types that don't use the_content()
80 * or apply_filters( 'the_content' ) to output a ConvertKit Form.
81 *
82 * @since 1.9.6
83 */
84 public function output_form() {
85
86 /**
87 * Outputs a ConvertKit Form on singular Post Types that don't use the_content()
88 * or apply_filters( 'the_content' ).
89 *
90 * @since 1.9.6
91 *
92 * @return string Post Content with Form Appended, if applicable
93 */
94 do_action( 'convertkit_output_output_form' );
95
96 }
97
98 /**
99 * Outputs a ConvertKit Landing Page if configured, replacing all output for the singular Post Type.
100 *
101 * @since 1.9.6
102 */
103 public function page_takeover() {
104
105 $queried_object = get_queried_object();
106
107 // Bail if the queried object cannot be inspected.
108 if ( ! isset( $queried_object->post_type ) ) {
109 return;
110 }
111
112 // Get Post ID.
113 $post_id = $queried_object->ID;
114
115 // Bail if the queried object isn't a supported Post Type for Landing Pages.
116 if ( $queried_object->post_type !== 'page' ) {
117 return;
118 }
119
120 // Get ConvertKit Post's Settings, if they have not yet been loaded.
121 if ( ! $this->post_settings ) {
122 $this->post_settings = new ConvertKit_Post( $post_id );
123 }
124
125 // Get Landing Page ID.
126 $landing_page_id = $this->post_settings->get_landing_page();
127
128 /**
129 * Define the ConvertKit Landing Page ID to display for the given Post ID,
130 * overriding the Post settings.
131 *
132 * Return false to not display any ConvertKit Landing Page.
133 *
134 * @since 1.9.6
135 *
136 * @param int $landing_page_id Landing Page ID
137 * @param int $post_id Post ID
138 */
139 $landing_page_id = apply_filters( 'convertkit_output_page_takeover_landing_page_id', $landing_page_id, $post_id );
140
141 // Bail if no Landing Page is configured to be output.
142 if ( empty( $landing_page_id ) ) {
143 return;
144 }
145
146 // Get available ConvertKit Landing Pages, if they have not yet been loaded.
147 if ( ! $this->landing_pages ) {
148 $this->landing_pages = new ConvertKit_Resource_Landing_Pages( 'output_landing_page' );
149 }
150
151 // Get Landing Page.
152 $landing_page = $this->landing_pages->get_html( $this->post_settings->get_landing_page() );
153
154 // Bail if an error occured.
155 if ( is_wp_error( $landing_page ) ) {
156 return;
157 }
158
159 // Output Landing Page.
160 // Output is supplied from ConvertKit's API, which is already sanitized.
161 echo $landing_page; // phpcs:ignore WordPress.Security.EscapeOutput
162 exit;
163
164 }
165
166 /**
167 * Appends a form to the singular Page, Post or Custom Post Type's Content.
168 *
169 * @param string $content Post Content.
170 * @return string Post Content with Form Appended, if applicable
171 */
172 public function append_form_to_content( $content ) {
173
174 // Bail if not a singular Post Type.
175 if ( ! is_singular() ) {
176 return $content;
177 }
178
179 // Get Post ID and ConvertKit Form ID for the Post.
180 $post_id = get_the_ID();
181 $form_id = $this->get_post_form_id( $post_id );
182
183 /**
184 * Define the ConvertKit Form ID to display for the given Post ID,
185 * overriding the Post, Category or Plugin settings.
186 *
187 * Return false to not display any ConvertKit Form.
188 *
189 * @since 1.9.6
190 *
191 * @param bool|int $form_id Form ID
192 * @param int $post_id Post ID
193 */
194 $form_id = apply_filters( 'convertkit_output_append_form_to_content_form_id', $form_id, $post_id );
195
196 // Return the Post Content, unedited, if the Form ID is false or zero.
197 if ( ! $form_id ) {
198 return $content;
199 }
200
201 // Get available ConvertKit Forms, if they have not yet been loaded.
202 if ( ! $this->forms ) {
203 $this->forms = new ConvertKit_Resource_Forms( 'output_form' );
204 }
205
206 // Get Form HTML.
207 $form = $this->forms->get_html( $form_id );
208
209 // If an error occured, it could be because the specified Form ID for the Post either:
210 // - belongs to another ConvertKit account (i.e. API credentials were changed in the Plugin, but this Post's specified Form was not changed), or
211 // - the form was deleted from the ConvertKit account.
212 // Attempt to fallback to the default form for this Post Type.
213 if ( is_wp_error( $form ) ) {
214 if ( $this->settings->debug_enabled() ) {
215 $content .= '<!-- ConvertKit append_form_to_content(): ' . $form->get_error_message() . ' Attempting fallback to Default Form. -->';
216 }
217
218 // Get Default Form ID for this Post's Type.
219 $form_id = $this->settings->get_default_form( get_post_type( $post_id ) );
220
221 // If no Default Form is specified, just return the Post Content, unedited.
222 if ( ! $form_id ) {
223 if ( $this->settings->debug_enabled() ) {
224 $content .= '<!-- ConvertKit append_form_to_content(): No Default Form exists as a fallback. -->';
225 }
226
227 return $content;
228 }
229
230 // Get Form HTML.
231 $form = $this->forms->get_html( $form_id );
232
233 // If an error occured again, the default form doesn't exist in this ConvertKit account.
234 // Just return the Post Content, unedited.
235 if ( is_wp_error( $form ) ) {
236 if ( $this->settings->debug_enabled() ) {
237 $content .= '<!-- ConvertKit append_form_to_content(): Default Form: ' . $form->get_error_message() . ' -->';
238 }
239
240 return $content;
241 }
242 }
243
244 // If here, we have a ConvertKit Form.
245 // Append form to Post's Content.
246 $content = $content .= $form;
247
248 /**
249 * Filter the Post's Content, which includes a ConvertKit Form, immediately before it is output.
250 *
251 * @since 1.9.6
252 *
253 * @param string $content Post Content
254 * @param string $form ConvertKit Form HTML
255 * @param int $post_id Post ID
256 * @param int $form_id ConvertKit Form ID
257 */
258 $content = apply_filters( 'convertkit_frontend_append_form', $content, $form, $post_id, $form_id );
259
260 return $content;
261
262 }
263
264 /**
265 * Returns the Post, Category or Plugin ConvertKit Form ID for the given Post.
266 *
267 * If the Post specifies a form to use, returns that Form ID.
268 * If the Post uses the 'Default' setting, and an assigned Category has a Form ID, uses the Category's Form ID.
269 * Otherwise falls back to the Plugin's Default Form ID (if any).
270 *
271 * @since 1.9.6
272 *
273 * @param int $post_id Post ID.
274 * @return bool|string|int false|'default'|Form ID
275 */
276 private function get_post_form_id( $post_id ) {
277
278 // Get Settings, if they have not yet been loaded.
279 if ( ! $this->settings ) {
280 $this->settings = new ConvertKit_Settings();
281 }
282
283 // Get ConvertKit Post's Settings, if they have not yet been loaded.
284 if ( ! $this->post_settings ) {
285 $this->post_settings = new ConvertKit_Post( $post_id );
286 }
287
288 // If the Post specifies a Form to use, return its ID now.
289 if ( $this->post_settings->has_form() ) {
290 return $this->post_settings->get_form();
291 }
292
293 // If the Post specifies that no Form should be used, return false.
294 if ( $this->post_settings->uses_no_form() ) {
295 return false;
296 }
297
298 // Sanity check that the Post uses the Default Form setting, which should be the case
299 // because the above conditions were not met.
300 if ( ! $this->post_settings->uses_default_form() ) {
301 return false;
302 }
303
304 // Get Post's Categories.
305 $categories = wp_get_post_categories(
306 $post_id,
307 array(
308 'fields' => 'ids',
309 )
310 );
311
312 // If no Categories exist, use the Default Form.
313 if ( ! is_array( $categories ) || ! count( $categories ) ) {
314 // Get Post Type.
315 return $this->settings->get_default_form( get_post_type( $post_id ) );
316 }
317
318 /**
319 * Iterate through Categories in reverse order.
320 * This honors the behaviour < 1.9.6, which states that if multiple Categories each have a Form.
321 * assigned, the last Category with a Form in the wp_get_post_categories() call will be used.
322 */
323 $categories = array_reverse( $categories );
324 foreach ( $categories as $term_id ) {
325 // Load Term Settings.
326 $term_settings = new ConvertKit_Term( $term_id );
327
328 // If a Form ID exists, return it now.
329 if ( $term_settings->has_form() ) {
330 return $term_settings->get_form();
331 }
332 }
333
334 // If here, use the Plugin's Default Form.
335 return $this->settings->get_default_form( get_post_type( $post_id ) );
336
337 }
338
339 /**
340 * Enqueue scripts.
341 *
342 * @since 1.9.6
343 */
344 public function enqueue_scripts() {
345
346 // Get Post.
347 $post = get_post();
348
349 // Bail if no Post could be fetched.
350 if ( ! $post ) {
351 return;
352 }
353
354 // Get ConvertKit Settings and Post's Settings.
355 $settings = new ConvertKit_Settings();
356 $convertkit_post = new ConvertKit_Post( $post->ID );
357
358 // Register scripts that we might use.
359 wp_register_script(
360 'convertkit-js',
361 CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/convertkit.js',
362 array( 'jquery' ),
363 CONVERTKIT_PLUGIN_VERSION,
364 true
365 );
366 wp_localize_script(
367 'convertkit-js',
368 'convertkit',
369 array(
370 'ajaxurl' => admin_url( 'admin-ajax.php' ),
371 'debug' => $settings->debug_enabled(),
372 'nonce' => wp_create_nonce( 'convertkit' ),
373 'subscriber_id' => $this->subscriber_id,
374 'tag' => ( ( is_singular() && $convertkit_post->has_tag() ) ? $convertkit_post->get_tag() : false ),
375 'post_id' => $post->ID,
376 )
377 );
378
379 // Bail if the no scripts setting is enabled.
380 if ( $settings->scripts_disabled() ) {
381 return;
382 }
383
384 // Enqueue.
385 wp_enqueue_script( 'convertkit-js' );
386
387 }
388
389 /**
390 * Gets the subscriber ID from the request (either the cookie or the URL).
391 *
392 * @since 1.9.6
393 */
394 public function get_subscriber_id_from_request() {
395
396 // Use ConvertKit_Subscriber class to fetch and validate the subscriber ID.
397 $subscriber = new ConvertKit_Subscriber();
398 $subscriber_id = $subscriber->get_subscriber_id();
399
400 // If an error occured, the subscriber ID in the request/cookie is not a valid subscriber.
401 if ( is_wp_error( $subscriber_id ) ) {
402 return;
403 }
404
405 $this->subscriber_id = $subscriber_id;
406
407 }
408
409 }
410