-
Notifications
You must be signed in to change notification settings - Fork 11
/
content-warning-v2.php
389 lines (329 loc) · 8.81 KB
/
content-warning-v2.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
<?php
/*
Plugin Name: Content Warning v2
Plugin URI: http://plugish.com/plugins/content-warning-v2
Description: A WordPress Plugin to allow site owners to display an acceptance dialog. Used mainly for NSFW websites, this plugin provides a dialog popup to warn viewers of it's possible content.
Author: Jerry Wood Jr.
Version: 3.7.2
Author URI: http://plugish.com
Text Domain: content-warning-v2
Domain Path: /lang
*/
require_once 'includes/api.php';
function cwv2_autoload_classes( $class_name ) {
if ( 0 != strpos( $class_name, 'CWV2_' ) ) {
return false;
}
$filename = strtolower( str_ireplace(
array( 'CWV2_', '_' ),
array( '', '-' ),
$class_name
) );
ContentWarning_v2::include_file( $filename );
return true;
}
spl_autoload_register( 'cwv2_autoload_classes' );
/**
* Class ContentWarning_v2
*
* @property string $version The current plugin version
*
* @author JayWood
*/
class ContentWarning_v2 {
/**
* Instance of ContentWarning_v2
* @var ContentWarning_v2
*/
public static $instance = null;
const VERSION = '3.7.2';
/**
* Rather or not scripts should be minified
* @var string
*/
public $min = '';
/**
* @var CWV2_Admin
*/
public $admin;
/**
* @var CWV2_Settings
*/
public $settings;
private function __construct() {
$this->min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
}
public static function init() {
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Gotta get them hooks yo!
*
* @author JayWood
*/
public function hooks() {
add_action( 'init', array( $this, 'register_frontend_data' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'load_deps' ) );
add_action( 'wp_footer', array( $this, 'render_dialog' ) );
add_action( 'wp_head', array( $this, 'override_css' ) );
if ( ! is_admin() ) {
}
$this->plugin_classes();
}
/**
* Get Cookie Name
*
* If the cookie is to be shown, this function will return the ID, and the javascript
* will handle the rest of it. If this function returns false, the javascript will not show a popup.
*
* @author JayWood
*
* @since 3.6.3
* @return string|int String if special page like homepage, or post_id otherwise.
*/
public function get_cookie_name() {
global $post;
$site_wide = get_option( 'cwv3_sitewide' );
$homepage = get_option( 'cwv3_homepage' );
$misc = get_option( 'cwv3_misc' );
$should_gate = apply_filters( 'cwv3_should_gate', true, $post );
if ( false === $should_gate ) {
return false;
}
if ( 'enabled' == ! empty( $site_wide ) ) {
return 'sitewide';
}
if ( 'enabled' == ! empty( $homepage ) && is_front_page() ) {
return 'homepage';
}
if ( 'enabled' == ! empty( $misc ) && ( is_search() || is_archive() ) ) {
return 'misc';
}
if ( is_attachment() && isset( $post->post_parent ) ) {
// Special consideration needs to be taken to check if the post parent is in-fact
// gated in any way.
$cat_gated = $this->is_cat_gated( $post->post_parent );
if ( $cat_gated ) {
// Return the category cookie name like _cat_###
return '_cat_' . $cat_gated;
} elseif ( $this->is_gated( $post->post_parent ) ) {
return $post->post_parent;
}
}
if ( is_singular() && isset( $post->ID ) ) {
$cat_gated = $this->is_cat_gated( $post->ID );
if ( $cat_gated ) {
// Return the category cookie name like _cat_###
return '_cat_' . $cat_gated;
} elseif ( $this->is_gated( $post->ID ) ) {
return $post->ID;
}
}
return false;
}
/**
* Is Gated
*
* Checks a post ID to see if it's supposed to be
* gated in any way, either by metabox, or category from the
* regular category taxonomy.
*
* @author JayWood
* @since 3.6.3
*
* @param int $post_id Post ID
*
* @return bool TRUE | FALSE
*/
public function is_gated( $post_id ) {
$meta = get_post_meta( $post_id, 'cwv3_auth', true );
if ( ! empty( $meta ) ) {
return true;
}
return false;
}
/**
* Is Cat Gated
*
* Determines if a post is within a gated category, if so, will
* return the category id for use in cookie names like so '_cat_###'
*
* @author JayWood
* @since 3.6.3
*
* @param int $post_id Post ID
*
* @return boolean|string False on failure, cookie string otherwise
*/
public function is_cat_gated( $post_id ) {
$cat_settings = get_option( 'cwv3_cat_list', array() );
if ( ! empty( $cat_settings ) ) {
$post_categories = get_the_category( $post_id );
return $this->in_cat( $cat_settings, $post_categories );
}
return false;
}
/**
* In Cat
*
* Checks to see if the current post is within the set
* categories in the options panel, if so, returns the ID of the
* category that it resides in.
*
* @author JayWood
* @since 3.6.3
*
* @param array $cat_settings Array of categories from settings page
* @param array $post_categories Array of categories from get_the_category()
*
* @return boolean|int False on failure, category ID on success
*/
public function in_cat( $cat_settings, $post_categories ) {
if ( ! is_array( $cat_settings ) ) {
$cat_settings = array(); // Empty
}
foreach ( $post_categories as $post_category ) {
if ( in_array( $post_category->term_id, $cat_settings ) ) {
return $post_category->term_id;
} else {
continue;
}
}
return false;
}
/**
* Load Dependancies
*
* Pretty self-explanatory, loads all the data that needs to be loaded beforehand.
*
* @author JayWood
* @since 3.6.3
* @return null
*/
public function load_deps() {
if ( current_user_can( 'manage_options' ) ) {
return;
}
wp_enqueue_style( 'cwv3_css' );
wp_enqueue_script( 'cwv3_js' );
$cookie_death = get_option( 'cwv3_death', 1 );
$de = get_option( 'cwv3_denial', 'enabled' );
$localized_data = array(
'opacity' => get_option( 'cwv3_bg_opacity', 0.85 ),
'cookie_path' => COOKIEPATH,
'cookie_name' => $this->get_cookie_name(),
'cookie_time' => intval( $cookie_death ) > 365 ? 365 : intval( $cookie_death ),
// Max at one year if it's over 365 days.
'denial_enabled' => is_array( $de ) && ! empty( $de ) ? true : false,
'denial_method' => get_option( 'cwv3_method', 'redirect' ),
'redirect_url' => esc_js( get_option( 'cwv3_exit_link', '#' ) ),
);
wp_localize_script( 'cwv3_js', 'cwv3_params', $localized_data );
}
/**
* Override CSS
* Placeholder method that uses the new API in inc/api.php
*
* @author JayWood
* @since 3.6.3
* @see cwv3_the_css()
*/
public function override_css() {
if ( ! is_admin() ) {
cwv3_the_css();
}
}
/**
* Register Frontend Data
*
* @author JayWood
* @since 3.6.3
* @return null
*/
public function register_frontend_data() {
load_plugin_textdomain( 'content-warning-v2', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
// Jquery Cookie
wp_register_script( 'jquery_cookie', $this->url( "js/jquery_cookie{$this->min}.js" ), array( 'jquery' ), '1.4.1', true );
// Main data
wp_register_script( 'cwv3_js', $this->url( "js/cwv3{$this->min}.js" ), array( 'jquery_cookie' ), '3.6.0', true );
wp_register_style( 'cwv3_css', $this->url( "css/cwv3{$this->min}.css" ), '', '1.0' );
}
/**
* Render Dialog
*
* Redirect method to use the API.php that was created
*
* @author JayWood
* @since 3.6.3
* @see cwv3_js_dialog()
*/
public function render_dialog() {
cwv3_js_dialog();
}
/**
* Uses autoloader for plugin classes
*
* @author JayWood
*/
private function plugin_classes() {
$this->settings = new CWV2_Settings( $this );
$this->admin = new CWV2_Admin( $this );
}
/**
* This plugin's directory
*
* @since 3.7
*
* @param string $path (optional) appended path
*
* @return string Directory and path
*/
public static function dir( $path = '' ) {
static $dir;
$dir = $dir ? $dir : trailingslashit( dirname( __FILE__ ) );
return $dir . $path;
}
/**
* This plugin's url
*
* @since 3.7
*
* @param string $path (optional) appended path
*
* @return string URL and path
*/
public static function url( $path = '' ) {
static $url;
$url = $url ? $url : trailingslashit( plugin_dir_url( __FILE__ ) );
return $url . $path;
}
/**
* Include a file from the includes directory
*
* @since 3.7
*
* @param string $filename Name of the file to be included
*
* @return bool Result of include call.
*/
public static function include_file( $filename ) {
$file = self::dir( 'includes/' . $filename . '.php' );
if ( file_exists( $file ) ) {
return include_once( $file );
}
return false;
}
public function __get( $field ) {
if ( $field == 'version' ) {
return self::VERSION;
}
return $this->$field;
}
}
function content_warning_v2() {
return ContentWarning_v2::init();
}
add_action( 'plugins_loaded', array( content_warning_v2(), 'hooks' ) );