forked from chrisguitarguy/WPSE-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accept-terms.php
324 lines (288 loc) · 8.54 KB
/
accept-terms.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
<?php
/*
Plugin Name: Accept Terms to Read
Plugin URI: http://wordpress.stackexchange.com/q/52793/6035
Description: Make users accept terms before they can view a given page.
Author: Christopher Davis
Author URI: http://christopherdavis.me
License: GPL2
Copyright 2012 Christopher Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
!defined('ABSPATH') && exit;
WPSE_52793::init();
class WPSE_52793
{
/**
* Query variable we'll use for rewrites and catching the form handler
*
*/
const Q_VAR = 'wpse52793_handler';
/**
* Form key field name.
*
*/
const F_KEY = 'wpse52793_accepted';
/**
* Form field nonce.
*
*/
const NONCE = 'wpse52793_fnonce';
/**
* Cookie key.
*
*/
const COOKIE = 'wpse52793_agreed';
/**
* Settings key.
*
*/
const SETTING = 'wpse52793_options';
/********** Basic setup fluff **********/
private static $ins = null;
public static function instance()
{
is_null(self::$ins) && self::$ins = new self;
return self::$ins;
}
public static function init()
{
add_action('plugins_loaded', array(self::instance(), '_setup'));
register_activation_hook(__FILE__, array(__CLASS__, 'activate'));
register_deactivation_hook(__FILE__, array(__CLASS__, 'deactivate'));
}
/**
* Activation hook. Calls our rewrite method to register the rules
* then flush the rewrite rules to get them working.
*
* @access public
* @uses flush_rewrite_rules
* @return void
*/
public static function activate()
{
self::instance()->rewrite();
flush_rewrite_rules();
}
/**
* Deactivation hook. Remove the rewrite rules.
*
* @access public
* @uses flush_rewrite_rules
* @return void
*/
public static function deactivate()
{
flush_rewrite_rules();
}
/**
* Hooked into `plugins_loaded`. Actually adds the actions.
*
* @access public
* @uses add_action
* @uses add_filter
* @return void
*/
public function _setup()
{
add_action('init', array($this, 'rewrite'));
add_filter('query_vars', array($this, 'add_var'));
add_action('template_redirect', array($this, 'catch_handler'));
add_action('init', array($this, 'shortcode'));
add_action('admin_init', array($this, 'settings'));
}
/**
* Registers a new rewrite rule for the form handler.
*
* @access public
* @uses add_rewrite_rule
* @return void
*/
public function rewrite()
{
add_rewrite_rule(
'^terms-handler/?$',
'index.php?' . self::Q_VAR . '=1',
'top'
);
}
/**
* Add our custom query variable so WordPress doesn't strip it out.
*
* @access public
* @return array The modified query variables
*/
public function add_var($v)
{
$v[] = self::Q_VAR;
return $v;
}
/**
* Hooked into template redirect. Catches our query var for the form
* handler and take takes care of setting a cookie and redirecting the user
* to the appropriate page.
*
* @access public
* @return void
*/
public function catch_handler()
{
if(!get_query_var(self::Q_VAR))
return;
// only allow post request, check for empty post, and make sure we
// have a page_id
if(
'POST' != $_SERVER['REQUEST_METHOD'] ||
empty($_POST) ||
empty($_POST['page_id'])
) {
wp_redirect(home_url());
exit;
}
// if we're here everything should be good.
// fetch the permalink
$r = get_permalink(absint($_POST['page_id']));
if(!$r)
{
// bad permalink for some reason, bail
wp_redirect(home_url());
exit;
}
if(
!isset($_POST[self::NONCE]) ||
!wp_verify_nonce($_POST[self::NONCE], self::NONCE) ||
empty($_POST[self::F_KEY])
) {
// bad nonce or they didn't check the box, try again
wp_redirect($r);
exit;
}
// whew, they've agreed. Set a cookie, and send them back to the page.
setcookie(
self::COOKIE,
'1',
strtotime('+30 Days'), // might want to change this?
'/',
COOKIE_DOMAIN, // WP constant
false,
true
);
wp_redirect($r);
exit;
}
public function shortcode()
{
add_shortcode('terms_required', array($this, 'shortcode_cb'));
}
public function shortcode_cb($opts, $content=null)
{
if(!empty($_COOKIE[self::COOKIE]))
return $content;
// if we're here, they haven't agreed. Show the terms.
ob_start();
?>
<div class="terms-and-conditions">
<?php echo apply_filters('wpse52793_terms', get_option(self::SETTING, '')); ?>
</div>
<form method="post" action="<?php echo home_url('/terms-handler/'); ?>">
<?php wp_nonce_field(self::NONCE, self::NONCE, false); ?>
<input type="hidden" name="page_id" value="<?php echo get_queried_object_id(); ?>" />
<label for="agree">
<input type="checkbox" name="<?php echo esc_attr(self::F_KEY); ?>" value="agree" id="agree" />
<?php esc_html_e('I agree to these terms and conditions.', 'wpse'); ?>
</label>
<p><input type="submit" value="<?php esc_attr_e('Submit', 'wpse'); ?>" /></p>
</form>
<?php
return ob_get_clean();
}
/**
* Hooked into `admin_init` -- registers the custom setting and adds a new
* field and section to the Options > Reading page.
*
* @access public
* @uses register_setting
* @uses add_settings_section
* @uses add_settings_field
* @return void
*/
public function settings()
{
register_setting(
'reading',
self::SETTING,
array($this, 'validate_cb')
);
add_settings_section(
'terms-conditions',
__('Terms and Conditions', 'wpse'),
array($this, 'section_cb'),
'reading'
);
add_settings_field(
'terms-conditions',
__('Terms & Conditions', 'wpse'),
array($this, 'field_cb'),
'reading',
'terms-conditions',
array('label_for' => self::SETTING)
);
}
/**
* Settings validation callback. Checks to see if the user can post
* unfiltered html and return the raw text or a kses filter string
* where appropriate.
*
* @access public
* @uses current_user_can
* @uses wp_filter_post_kses
* @return string
*/
public function validate_cb($dirty)
{
return current_user_can('unfiltered_html') ?
$dirty : wp_filter_post_kses($dirty);
}
/**
* Callback for the settings section. Displays some help text.
*
* @access public
* @uses esc_html__
* @return void
*/
public function section_cb()
{
echo '<p class="description">',
esc_html__('The terms and conditions that get displayed when a user '.
'visits a page protected by the `terms_required` shortcode.', 'wpse'),
'</p>';
}
/**
* Callback for the settings field. Creates a new editor on the screen.
*
* @access public
* @uses wp_editor
* @uses get_option
* @return void
*/
public function field_cb($args)
{
wp_editor(
get_option(self::SETTING, ''),
self::SETTING,
array(
'wpautop' => false,
'textarea_rows' => 10,
)
);
}
}