-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcf7_gated_content.php
514 lines (450 loc) · 14.9 KB
/
cf7_gated_content.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
/**
* Plugin Name: Contact Form 7 Gated Content
* Description: An add-on for Contact Form 7 that allows you to gate content behind form submission
* Version: 1.5.0
* Author: Apsis Labs
* Author URI: http://apsis.io
* License: GPLv3
* Requires Plugins: contact-form-7
*
* @package cf7_gated_content
*/
namespace Apsis;
const METABOX_NONCE_SECRET = 'cf7_gated_content_metaboxes';
const METABOX_NONCE_KEY = 'cf7_gated_content_metaboxes_nonce';
const GATED_CONTENT_COOKIE_KEY = 'cf7_gated_content_';
const MINIMUM_CF7_VERSION = "5.2.0";
class ContactFormGatedContent
{
/**
* Initialize the plugin, register actions and filters
*
* @since 1.0.0
* @static
* @access public
*/
public static function init()
{
add_action('wpcf7_editor_panels', array(static::class, 'registerPanels'));
add_action('wpcf7_after_save', array(static::class, 'saveGatedContentForm'));
add_action('wpcf7_mail_sent', array(static::class, 'setCookieServerSide'));
add_action('wpcf7_enqueue_scripts', array(static::class, 'clientSideScript'));
add_action('wpcf7_enqueue_styles', array(static::class, 'clientSideStyles'));
add_action( 'admin_notices', array(static::class, 'warnVersion'));
add_action('admin_enqueue_scripts', array(static::class, 'adminScript'));
add_action('wp_ajax_getDownloadButton', array(static::class, 'getDownloadButton'));
add_action('wp_ajax_nopriv_getDownloadButton', array(static::class, 'getDownloadButton'));
add_filter('do_shortcode_tag', array(static::class, 'outputShortcode'), 10, 4);
add_filter('wpcf7_form_response_output', array(static::class, 'addFormResponse'), 10, 4);
}
/**
* Register the gated contact form panel to display in CF7
*
* @since 1.0.0
* @static
* @access public
*/
public static function warnVersion()
{
if ( version_compare( MINIMUM_CF7_VERSION, WPCF7_VERSION, '>' ) ) {
$message = sprintf(
/* translators: 1: version of Contact Form 7, 2: version of WordPress, 3: URL */
__( '<strong>CF7 Gated Content requires Contact Form 7 v%1$s or higher.</strong> Please <a href="%2$s">update Contact Form 7</a> plugin or disable the CF7 Gated Content plugin.', 'apsis_wp' ),
MINIMUM_CF7_VERSION,
admin_url( 'update-core.php' )
);
wp_admin_notice( $message, 'type=warning' );
}
}
/**
* Register the gated contact form panel to display in CF7
*
* @since 1.0.0
* @static
* @access public
*/
public static function registerPanels($panels)
{
$panels['gated-content'] = array(
'title' => __('Gated Content', 'apsis_wp'),
'callback' => [static::class, 'drawGatedContentPanel']
);
return $panels;
}
/**
* Render the panel content
*
* @since 1.0.0
* @static
* @access public
*/
public static function drawGatedContentPanel($post)
{
wp_nonce_field(METABOX_NONCE_SECRET, METABOX_NONCE_KEY);
if ($post->id()) {
// Fetch settings
$image_attachment_url = get_post_meta($post->id(), 'image_attachment_url', true);
$image_attachment_id = get_post_meta($post->id(), 'image_attachment_id', true);
$download_content = get_post_meta($post->id(), 'download_content', true);
$download_button_text = get_post_meta($post->id(), 'download_button_text', true);
$download_button_classes = get_post_meta($post->id(), 'download_button_classes', true);
$always_require_form = get_post_meta($post->id(), 'always_require_form', true);
$show_download_for_admin = get_post_meta($post->id(), 'show_download_for_admin', true);
$open_in_new_tab = get_post_meta($post->id(), 'open_in_new_tab', true);
$enable_gated_content = get_post_meta($post->id(), 'enable_gated_content', true);
$include_default_css = get_post_meta($post->id(), 'include_default_css', true);
$use_relative_url = get_post_meta($post->id(), 'use_relative_url', true);
// Fetch attachment value
$attachment_meta = static::getAttachmentMeta($image_attachment_id);
$user_settings = array_filter(compact(
"image_attachment_url",
"image_attachment_id",
"download_content",
"download_button_text",
"download_button_classes",
"always_require_form",
"show_download_for_admin",
"open_in_new_tab",
"enable_gated_content",
"attachment_meta",
"include_default_css",
"use_relative_url"
), function ($v) {
return !is_null($v);
});
// Merge with default values
$values = array_merge(static::getDefaults(), $user_settings);
} else {
$values = static::getDefaults();
}
// Render metabox
echo static::renderTemplate(dirname(__FILE__) . '/templates/metabox.php', $values);
}
/**
* Save gated content settings when saving the CF7 form
*
* @since 1.0.0
* @static
* @access public
*/
public static function saveGatedContentForm($contact_form)
{
global $post;
$contact_form_id = $contact_form->id();
$meta = array();
// Validate user permissions
if (!current_user_can('edit_post', $contact_form_id)) {
return $contact_form_id;
}
// Validate admin nonce
if (!isset($_POST[METABOX_NONCE_KEY]) || !wp_verify_nonce($_POST[METABOX_NONCE_KEY], METABOX_NONCE_SECRET)) {
return $contact_form_id;
}
// Don't save during autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $contact_form_id;
}
// Don't save on revisions
if ($post && 'revision' === $post->post_type) {
return $contact_form_id;
}
$meta['image_attachment_id'] = sanitize_text_field($_POST['image_attachment_id']);
$meta['image_attachment_url'] = sanitize_text_field($_POST['image_attachment_url']);
$meta['download_button_text'] = sanitize_text_field($_POST['download_button_text']);
$meta['download_button_classes'] = sanitize_text_field($_POST['download_button_classes']);
$meta['always_require_form'] = isset($_POST['always_require_form']) ? !!$_POST['always_require_form'] : false;
$meta['show_download_for_admin'] = isset($_POST['show_download_for_admin']) ? !!$_POST['show_download_for_admin'] : false;
$meta['open_in_new_tab'] = isset($_POST['open_in_new_tab']) ? !!$_POST['open_in_new_tab'] : false;
$meta['enable_gated_content'] = isset($_POST['enable_gated_content']) ? !!$_POST['enable_gated_content'] : false;
$meta['include_default_css'] = isset($_POST['include_default_css']) ? !!$_POST['include_default_css'] : false;
$meta['use_relative_url'] = isset($_POST['use_relative_url']) ? !!$_POST['use_relative_url'] : false;
$meta['download_content'] = wp_kses_post($_POST['download_content']);
// wp_die(print_r($meta, true));
foreach ($meta as $key => $value) {
static::storeMetaValue($contact_form_id, $key, $value);
}
}
/**
* Set cookie when processing a contact form on the server side
*
* @since 1.0.0
* @static
* @access public
*/
public static function setCookieServerSide($contact_form)
{
setcookie(GATED_CONTENT_COOKIE_KEY . $contact_form->id(), 1);
}
/**
* Register client side styles
*
* @since 1.0.0
* @static
* @access public
*/
public static function clientSideStyles()
{
wp_register_style('cf7_gated_content', plugin_dir_url(__FILE__) . 'css/cf7_gated_content.css');
}
/**
* Enqueue all scripts for admin panels
*
* @since 1.0.0
* @static
* @access public
*/
public static function adminScript()
{
wp_enqueue_media();
wp_enqueue_script(
'cf7_gated_content_admin',
plugin_dir_url(__FILE__) . 'js/cf7_gated_content_admin.js',
['jquery']
);
wp_localize_script('cf7_gated_content_admin', 'wpcf7gc', array(
'mediaTitle' => __('Select gated content', 'apsis_wp')
));
wp_enqueue_style('cf7_gated_content_admin', plugin_dir_url(__FILE__) . 'css/cf7_gated_content_admin.css');
}
/**
* Enqueue all styles for client side forms
*
* @since 1.0.0
* @static
* @access public
*/
public static function clientSideScript()
{
wp_enqueue_script('cf7_gated_content', plugin_dir_url(__FILE__) . 'js/cf7_gated_content.js', ['jquery'], true);
wp_localize_script('cf7_gated_content', 'wpcf7gc', array(
'wpDebug' => WP_DEBUG,
'ajaxurl' => admin_url('admin-ajax.php'),
'cookieKey' => GATED_CONTENT_COOKIE_KEY
));
}
/**
* Modify the output of the contact form shortcode to render
* the download box if form has been previously submitted.
*
* @since 1.0.0
* @static
* @access public
*/
public static function outputShortcode($output, $tag, $atts, $m)
{
if ($tag != 'contact-form-7') {
return $output;
}
$contact_form = static::getContactFormFromShortcodeAtts($atts);
if (!$contact_form) {
return $output;
}
$id = $contact_form->id();
$gated_content_url = get_post_meta($id, 'image_attachment_url', true);
$include_default_css = get_post_meta($id, 'include_default_css', true);
$always_require_form = get_post_meta($id, 'always_require_form', true);
$show_download_for_admin = get_post_meta($id, 'show_download_for_admin', true);
$enable_gated_content = get_post_meta($id, 'enable_gated_content', true);
if ($include_default_css) {
wp_enqueue_style('cf7_gated_content');
}
if ($gated_content_url && $enable_gated_content) {
$cookie_key = GATED_CONTENT_COOKIE_KEY . $id;
$previously_submitted = isset($_COOKIE[$cookie_key]);
$show_download = !$always_require_form && $previously_submitted || ($show_download_for_admin && current_user_can('administrator'));
if ($show_download) {
if ($always_require_form) {
$output .= static::renderDownloadButton($id);
} else {
$output = static::renderDownloadButton($id);
}
}
}
return $output;
}
/**
* Get the contact-form instance from the contact-form-7 shortcode
*
* @since 1.5.0
* @static
* @access public
*/
public static function getContactFormFromShortcodeAtts($atts)
{
$contact_form = null;
$atts = shortcode_atts(
array(
'id' => '',
'title' => '',
),
$atts, 'wpcf7'
);
$id = trim( $atts['id'] );
$title = trim( $atts['title'] );
$contact_form = wpcf7_get_contact_form_by_hash( $id );
if ( ! $contact_form ) {
$contact_form = wpcf7_contact_form( $id );
}
if ( ! $contact_form ) {
$contact_form = wpcf7_get_contact_form_by_title( $title );
}
return $contact_form;
}
/**
* Modify the output of the form response if the form was posted
* without AJAX and includes the sent-ok class.
*
* @since 1.4.2
* @static
* @access public
*/
public static function addFormResponse($output, $class, $_content, $form)
{
if ($form->is_posted() && strpos($class, 'wpcf7-mail-sent-ok')) {
$output = sprintf("%s%s", $output, static::renderDownloadButton($form->id()));
}
return $output;
}
/**
* Render the download button. The resulting value is passed through
* a filter, allowing customization of output for your theme or plugin.
*
* <code>
* function wpcf7_gated_content_button($url, $button_text, $button_classes, $content)
* {
* return "<a href='$url'>$button_text</a>";
* }
* add_filter('wpcf7_gated_content_button', 'my_content_button', 10, 3)
* </code>
*
* @since 1.0.0
* @static
* @access public
*/
public static function renderDownloadButton($contact_form_id)
{
$enabled = get_post_meta($contact_form_id, 'enable_gated_content', true);
$url = get_post_meta($contact_form_id, 'image_attachment_url', true);
if ($enabled && $url) {
$button_text = get_post_meta($contact_form_id, 'download_button_text', true);
$button_classes = get_post_meta($contact_form_id, 'download_button_classes', true);
$content = wp_kses_post(get_post_meta($contact_form_id, 'download_content', true));
$open_in_new_tab = get_post_meta($contact_form_id, 'open_in_new_tab', true);
$use_relative_url = get_post_meta($contact_form_id, 'use_relative_url', true);
$template_path = dirname(__FILE__) . '/templates/download_button.php';
$target = $open_in_new_tab ? "_blank" : "_self";
if ($use_relative_url) {
$parsed_url = parse_url($url);
$url = $parsed_url['path'];
}
$output = static::renderTemplate($template_path, compact(
"url",
"button_text",
"button_classes",
"content",
"target"
));
return apply_filters(
'wpcf7_gated_content_button',
$output,
$url,
$button_text,
$button_classes,
$content
);
}
}
/**
* AJAX response to render download button for JS
* requests.
*
* @since 1.0.0
* @static
* @access public
*/
public static function getDownloadButton()
{
$contact_form_id = intval($_POST['contactFormId']);
wp_send_json_success(static::renderDownloadButton($contact_form_id));
}
/**
* Get and format attachment meta for display in
* attachment box.
*
* @return Array the meta values for the attached file
* @since 1.0.0
* @static
* @access public
*/
public static function getAttachmentMeta($id)
{
$file = get_attached_file($id);
$url = wp_get_attachment_url($id);
return array(
'filename' => basename($file),
'url' => $url
);
}
/**
* Utility function for getting rendered file as a string.
*
* @since 1.0.0
* @static
*/
static function renderTemplate($path, $params = array())
{
if (!empty($path)) {
ob_start();
extract($params, EXTR_SKIP);
include $path;
return ob_get_clean();
}
}
/**
* Get default values for gated content settings
*
* @return Array the array of default values
* @since 1.0.0
* @static
*/
static function getDefaults()
{
return array(
"image_attachment_url" => null,
"image_attachment_id" => null,
"download_content" => null,
"download_button_text" => __("Download", "apsis_wp"),
"download_button_classes" => null,
"always_require_form" => false,
"show_download_for_admin" => false,
"open_in_new_tab" => true,
"enable_gated_content" => true,
"attachment_meta" => null,
"include_default_css" => true,
"use_relative_url" => false,
);
}
/**
* Helper to store meta values
*
* @since 1.0.0
* @static
*/
static function storeMetaValue($post_id, $key, $value)
{
if (!isset($value)) {
// Delete the meta key if there's no value
delete_post_meta($post_id, $key);
} else {
if (get_post_meta($post_id, $key, false)) {
// If the custom field already has a value, update it.
update_post_meta($post_id, $key, $value);
} else {
// If the custom field doesn't have a value, add it.
add_post_meta($post_id, $key, $value);
}
}
}
}
ContactFormGatedContent::init();