| 1 |
<?php |
| 2 |
class PPProtect |
| 3 |
{ |
| 4 |
|
| 5 |
//This is to have PilotPress set the membership levels so we can use them here |
| 6 |
private $membershipLevels; |
| 7 |
private $siteLevels; |
| 8 |
|
| 9 |
const HOME_PAGE = -1; |
| 10 |
const LOGIN_PAGE = -2; |
| 11 |
const FIVE_MINUTES = 300; //seconds |
| 12 |
|
| 13 |
/* |
| 14 |
* Admin functions & Plugin setup |
| 15 |
*/ |
| 16 |
function __construct() |
| 17 |
{ |
| 18 |
if (defined("ABSPATH")) |
| 19 |
{ |
| 20 |
require_once(ABSPATH . '/wp-admin/includes/upgrade.php'); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
// Add new hooks into WP |
| 25 |
public function ppprotectHooks() |
| 26 |
{ |
| 27 |
// Creates the PPProtect table |
| 28 |
register_activation_hook(__FILE__, array(&$this, 'ppprotectCreateTable')); |
| 29 |
|
| 30 |
// Adds admin styles |
| 31 |
add_action('admin_enqueue_scripts', array(&$this, 'ppprotectAdminStyles')); |
| 32 |
|
| 33 |
// Add new options into edit-tags.php?taxonomy=category |
| 34 |
add_action('category_edit_form_fields', array(&$this, 'ppprotectEditFormFields')); |
| 35 |
add_action('category_add_form_fields', array(&$this, 'ppprotectEditFormFields')); |
| 36 |
|
| 37 |
// Saves new ppp category options |
| 38 |
add_action('created_category', array(&$this, 'ppprotectSaveFields')); |
| 39 |
add_action('edited_category', array(&$this, 'ppprotectSaveFields')); |
| 40 |
|
| 41 |
// Protect home page and archive page posts |
| 42 |
add_action('pre_get_posts', array(&$this, 'ppprotectHomeAndArchivePosts')); |
| 43 |
add_filter('widget_posts_args', array(&$this, 'ppprotectRecentPosts')); |
| 44 |
|
| 45 |
// Protect categories by hooking into any loops |
| 46 |
add_action('template_redirect', array(&$this, 'ppprotectCategory')); |
| 47 |
|
| 48 |
// Protect posts by hooking into any loops |
| 49 |
add_action('template_redirect', array(&$this, 'ppprotectPost')); |
| 50 |
|
| 51 |
// Add admin area warning that post permission levels are being overridden by a category |
| 52 |
add_action('edit_form_after_editor', array(&$this, 'ppprotectPostWarning')); |
| 53 |
|
| 54 |
// Add AJAX function to allow users to override each post manually and ignore the category override |
| 55 |
add_action('wp_ajax_pp_category_override', array(&$this, 'wp_ajax_ppprotectAllowOverride')); |
| 56 |
|
| 57 |
// Add footer JS on category admin pages to alert the user when they perform certain actions |
| 58 |
add_action('admin_footer', array(&$this, 'ppprotectCategoryJS')); |
| 59 |
|
| 60 |
add_action('delete_category', array(&$this, 'ppprotectDeleteCategory')); |
| 61 |
} |
| 62 |
|
| 63 |
// Create a custom table for PPProtect |
| 64 |
public function ppprotectCreateTable() |
| 65 |
{ |
| 66 |
global $wpdb; |
| 67 |
global $ppprotectDbVersion; |
| 68 |
$ppprotectDbVersion = '1.0'; |
| 69 |
|
| 70 |
$table_name = $wpdb->prefix . 'ppprotect'; |
| 71 |
|
| 72 |
$charset_collate = $wpdb->get_charset_collate(); |
| 73 |
|
| 74 |
$sql = "CREATE TABLE $table_name ( |
| 75 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 76 |
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 77 |
type VARCHAR(50) NOT NULL, |
| 78 |
itemId int UNIQUE NOT NULL, |
| 79 |
name VARCHAR(100) NOT NULL, |
| 80 |
levels TEXT NOT NULL, |
| 81 |
redirect VARCHAR(255) NOT NULL, |
| 82 |
protectposts int(2) NOT NULL, |
| 83 |
UNIQUE KEY id (id) |
| 84 |
) $charset_collate;"; |
| 85 |
|
| 86 |
dbDelta( $sql ); |
| 87 |
|
| 88 |
add_option( 'ppprotectDbVersion', $ppprotectDbVersion ); |
| 89 |
} |
| 90 |
|
| 91 |
// Saves ppprotect data in the ppprotect table in the site's database |
| 92 |
private function ppprotectInsertInDb( $type, $id, $name, $levels, $redirect, $protectposts ) |
| 93 |
{ |
| 94 |
global $wpdb; |
| 95 |
$table_name = $wpdb->prefix . 'ppprotect'; |
| 96 |
$wpdb->replace( $table_name, array( |
| 97 |
'type' => $type, |
| 98 |
'itemId' => $id, |
| 99 |
'name' => $name, |
| 100 |
'levels' => $levels, |
| 101 |
'redirect' => $redirect, |
| 102 |
'protectposts' => $protectposts |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
// Gets ppprotect data from the ppprotect table in the site's database |
| 108 |
private function ppprotectGetFromDb( $itemId ) |
| 109 |
{ |
| 110 |
global $wpdb; |
| 111 |
$id = intval($itemId); |
| 112 |
$table = $wpdb->prefix . 'ppprotect'; |
| 113 |
|
| 114 |
if( $wpdb->get_var("SHOW TABLES LIKE '$table'") === null ) |
| 115 |
{ |
| 116 |
$this->ppprotectCreateTable(); |
| 117 |
} |
| 118 |
|
| 119 |
$row = $wpdb->get_row('SELECT * FROM ' . $table . ' WHERE itemId = ' . $id); |
| 120 |
|
| 121 |
return $row; |
| 122 |
} |
| 123 |
|
| 124 |
// Deletes $items from the given $table in the site's database. Returns boolean depending upon the success / failure |
| 125 |
private function ppprotectDeleteFromDb( $table, $items ) |
| 126 |
{ |
| 127 |
global $wpdb; |
| 128 |
if( $wpdb->get_var("SHOW TABLES LIKE '$table'") === null ) |
| 129 |
{ |
| 130 |
error_log( 'Cant find table to delete - ' . $table ); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
$row = $wpdb->delete( $table, $items ); |
| 135 |
if ( $row === 0 ) |
| 136 |
{ |
| 137 |
error_log( 'Unable to delete items from the table - Table: ' . $table . ' | Items: ' . json_encode($items) ); |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
return $row; |
| 142 |
} |
| 143 |
|
| 144 |
// Register & enqueues admin styles |
| 145 |
public function ppprotectAdminStyles() |
| 146 |
{ |
| 147 |
wp_register_style( 'ppprotect_admin_css', plugins_url( 'pp-categories-admin-styles.css', __FILE__ ), false ); |
| 148 |
wp_enqueue_style( 'ppprotect_admin_css' ); |
| 149 |
} |
| 150 |
|
| 151 |
// Callback buffer |
| 152 |
public function ppprotectCallback($buffer) |
| 153 |
{ |
| 154 |
return $buffer; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
/* |
| 159 |
* PP Protect Categories |
| 160 |
*/ |
| 161 |
/** |
| 162 |
* @author William DeAngelis |
| 163 |
* @param object $tag An object that contains information about a given category |
| 164 |
* @var integer $tagId The category ID |
| 165 |
* @var array $cLevels An array that contains all of the category permission settings for a given category |
| 166 |
* @var array $memLevels An array that contains all of the possible PilotPress permission levels for this site |
| 167 |
* @var array $pages An array that contains all of the possible pages a user could redirect users to within this site |
| 168 |
* |
| 169 |
* @return Generates the HTML content to be displayed in the add and edit categories sections |
| 170 |
**/ |
| 171 |
public function ppprotectEditFormFields ( $tag ) |
| 172 |
{ |
| 173 |
if ( isset($tag->term_id) ) |
| 174 |
{ |
| 175 |
$tagId = $tag->term_id; |
| 176 |
$cLevels = $this->ppprotectGetFromDb( $tagId ); |
| 177 |
|
| 178 |
if ( isset($cLevels) ) |
| 179 |
{ |
| 180 |
$checkedLevels = json_decode($cLevels->levels); |
| 181 |
$redirectTo = $cLevels->redirect; |
| 182 |
$postProtect = $cLevels->protectposts; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if ( !is_array( $this->siteLevels) || empty( $this->siteLevels ) ) |
| 187 |
{ |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
$memLevels = $this->ppprotectGetPPSiteLevels(); |
| 192 |
|
| 193 |
$ppprotectCat = '<div class="form-field ppprotect-wrap"><label class="ppp-title" for="ppprotect-category">PilotPress Permissions</label><div class="ppprotect-levels-redirect"><div class="ppprotect-levels-message">1. Select the access levels of users that can access this category of posts.</div><div class="ppprotect-category-levels">'; |
| 194 |
|
| 195 |
foreach ( $memLevels as $level ) |
| 196 |
{ |
| 197 |
if ( isset( $checkedLevels ) && in_array( $level, $checkedLevels ) ) |
| 198 |
{ |
| 199 |
$checked = 'checked'; |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
$checked = ''; |
| 204 |
} |
| 205 |
|
| 206 |
$ppprotectCat .= '<div class="ppprotect-cat-level-wrap"><label><input type="checkbox" name="ppprotectCat[' . $level . ']" ' . $checked . ' /> ' . $level . '</label></div>'; |
| 207 |
} |
| 208 |
|
| 209 |
$ppprotectCat .= '<p><em>(Leave blank to allow access to all users.)</em></p></div>'; |
| 210 |
|
| 211 |
// Start redirect code |
| 212 |
$ppprotectCat .= '<div class="ppprotect-on-error"><div class="ppprotect-levels-message" style="margin-top: 10px;">2. If users don\'t have the above selected access levels, redirect to this page on error.</div><select name="ppprotectRedirect"><option value="">' . esc_attr( __( "Select page" ) ) . '</option>'; |
| 213 |
|
| 214 |
$pages = get_pages(); |
| 215 |
|
| 216 |
$selected1 = ''; |
| 217 |
$selected2 = ''; |
| 218 |
if ($redirectTo == '-1') |
| 219 |
{ |
| 220 |
$selected1 = 'selected="selected"'; |
| 221 |
} |
| 222 |
|
| 223 |
if ($redirectTo == '-2') |
| 224 |
{ |
| 225 |
$selected2 = 'selected="selected"'; |
| 226 |
} |
| 227 |
|
| 228 |
$ppprotectCat .= '<option value="-1" ' . $selected1 . '>(homepage)</option>'; |
| 229 |
$ppprotectCat .= '<option value="-2" ' . $selected2 . '>(login page)</option>'; |
| 230 |
|
| 231 |
foreach ( $pages as $page ) |
| 232 |
{ |
| 233 |
if ( isset( $redirectTo ) && $redirectTo == get_page_link( $page->ID ) ) |
| 234 |
{ |
| 235 |
$selected = 'selected="selected"'; |
| 236 |
} |
| 237 |
else |
| 238 |
{ |
| 239 |
$selected = ''; |
| 240 |
} |
| 241 |
|
| 242 |
$ppprotectCat .= '<option value="' . get_page_link( $page->ID ) . '" ' . $selected . '>' . $page->post_title . '</option>'; |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
$ppprotectCat .= '</select></div>'; // End Redirect code |
| 247 |
|
| 248 |
if ( isset( $postProtect ) && $postProtect == true ) |
| 249 |
{ |
| 250 |
$pChecked = 'checked'; |
| 251 |
} |
| 252 |
else |
| 253 |
{ |
| 254 |
$pChecked = ''; |
| 255 |
} |
| 256 |
|
| 257 |
$ppprotectCat .= '<div class="ppprotect-all-posts"><div>3. Protect all individual posts in this category?</div><div class="ppprotect-posts"><label><input type="checkbox" name="ppprotectPosts" ' . $pChecked . ' /> Yes</label></div></div>'; |
| 258 |
|
| 259 |
$ppprotectCat .= '</div></div>'; // End PP Permissions code |
| 260 |
|
| 261 |
echo $ppprotectCat; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* @brief used by PilotPress to send us the user's membership levels |
| 266 |
* |
| 267 |
* @param array $levels of pilotpress membershipLevels |
| 268 |
* |
| 269 |
* @author Aaron Lamar |
| 270 |
**/ |
| 271 |
public function ppprotectSetPPMemLevels($levels) |
| 272 |
{ |
| 273 |
$this->membershipLevels = $levels; |
| 274 |
} |
| 275 |
|
| 276 |
public function ppprotectSetPPSiteLevels($levels) |
| 277 |
{ |
| 278 |
$this->siteLevels = $levels; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
/** |
| 283 |
* @brief gets membership levels set by PilotPress, if not there, checks the session. |
| 284 |
* otherwise returns empty array |
| 285 |
* |
| 286 |
* @return array |
| 287 |
*/ |
| 288 |
protected function ppprotectGetPPMemLevels() |
| 289 |
{ |
| 290 |
$levels = array(); |
| 291 |
if(is_array($this->membershipLevels)) |
| 292 |
{ |
| 293 |
$levels = $this->membershipLevels; |
| 294 |
} |
| 295 |
else if(is_array($_SESSION["user_levels"])) |
| 296 |
{ |
| 297 |
$levels = $_SESSION["user_levels"]; |
| 298 |
} |
| 299 |
|
| 300 |
return $levels; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* @var array $membershipLevels An array containing all of the perm levels for the site |
| 305 |
* |
| 306 |
* @return array |
| 307 |
* |
| 308 |
* @author William DeAngelis |
| 309 |
**/ |
| 310 |
protected function ppprotectGetPPSiteLevels() |
| 311 |
{ |
| 312 |
return $this->siteLevels; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
/** |
| 317 |
* @author William DeAngelis |
| 318 |
* |
| 319 |
* @param string $memLevel A permission level to test if is in the allowed permissions |
| 320 |
* |
| 321 |
* @return Check's the current user's membership level against those in the allowed permissions |
| 322 |
**/ |
| 323 |
protected function ppprotectAccessCheck($memLevel) |
| 324 |
{ |
| 325 |
if(in_array($memLevel, $this->ppprotectGetPPMemLevels())) |
| 326 |
{ |
| 327 |
return true; |
| 328 |
} |
| 329 |
|
| 330 |
return false; |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* @author William DeAngelis |
| 335 |
* @param string $postID The ID of the post to check |
| 336 |
* |
| 337 |
* @return Check's the ID of a post to check to see if it is being protected by category protection and if true then returns the levels of protection. If false then return false. |
| 338 |
**/ |
| 339 |
public function ppprotectCheckForProtection( $postID ) |
| 340 |
{ |
| 341 |
global $wpdb; |
| 342 |
$table = $wpdb->prefix . 'ppprotect'; |
| 343 |
$protectedCategories = array(); |
| 344 |
$postCategories = wp_get_post_categories( $postID ); |
| 345 |
$result = 0; |
| 346 |
|
| 347 |
if( $wpdb->get_var("SHOW TABLES LIKE '$table'") === null ) |
| 348 |
{ |
| 349 |
$this->ppprotectCreateTable(); |
| 350 |
} |
| 351 |
|
| 352 |
$cats = $wpdb->get_results('SELECT itemId FROM ' . $table); |
| 353 |
|
| 354 |
foreach ( $cats as $cat ) |
| 355 |
{ |
| 356 |
$protectedCategories[] = $cat->itemId; |
| 357 |
} |
| 358 |
|
| 359 |
foreach ( $postCategories as $postCategory ) |
| 360 |
{ |
| 361 |
if ( in_array( $postCategory, $protectedCategories ) ) |
| 362 |
{ |
| 363 |
$sql = $wpdb->prepare('SELECT levels FROM ' . $table . ' WHERE itemId = %s', $postCategory); |
| 364 |
|
| 365 |
$levels = $wpdb->get_results($sql); |
| 366 |
if ($levels && $levels[0] && $levels[0]->levels) |
| 367 |
{ |
| 368 |
$result = implode( ', ', json_decode($levels[0]->levels) ); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
return $result; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* @author William DeAngelis |
| 378 |
* @param integer $term_id The ID of the category to be saved |
| 379 |
* @var string $redirect The URL of the page to redirect the user to when they don't have proper perms |
| 380 |
* @var string $protectPosts A checkbox option that tells us whether to protect all the posts in the category or just the category page itself. |
| 381 |
* @var string $name The name of the category being protected |
| 382 |
* @var array $levels The permission levels used to protect the category |
| 383 |
* |
| 384 |
* @return No return. |
| 385 |
**/ |
| 386 |
public function ppprotectSaveFields( $term_id ) |
| 387 |
{ |
| 388 |
if ( !is_array( $this->siteLevels) || empty( $this->siteLevels ) ) |
| 389 |
{ |
| 390 |
return; |
| 391 |
} |
| 392 |
|
| 393 |
// Sanitize URL |
| 394 |
$redirect = esc_url_raw($_POST['ppprotectRedirect']); |
| 395 |
|
| 396 |
// validate ppprotectPosts is either 'on' or not |
| 397 |
$protectPosts = 0; |
| 398 |
if (isset($_POST['ppprotectPosts']) && (string) $_POST['ppprotectPosts'] === 'on') |
| 399 |
{ |
| 400 |
$protectPosts = 1; |
| 401 |
} |
| 402 |
|
| 403 |
$type = 'category'; |
| 404 |
|
| 405 |
if (isset($_POST['name'])) |
| 406 |
{ |
| 407 |
$name = sanitize_title($_POST['name']); |
| 408 |
} |
| 409 |
else |
| 410 |
{ |
| 411 |
$name = get_cat_name( $term_id ); |
| 412 |
} |
| 413 |
|
| 414 |
if ( isset( $_POST['ppprotectCat'] ) ) |
| 415 |
{ |
| 416 |
$pppCategory = array(); |
| 417 |
|
| 418 |
foreach ( $_POST['ppprotectCat'] as $key => $val ) |
| 419 |
{ |
| 420 |
$key = sanitize_text_field($key); |
| 421 |
array_push($pppCategory, $key); |
| 422 |
} |
| 423 |
|
| 424 |
$levels = json_encode($pppCategory); |
| 425 |
} |
| 426 |
else |
| 427 |
{ |
| 428 |
$levels = ''; |
| 429 |
} |
| 430 |
|
| 431 |
if ( (!isset($redirect) || $redirect == '') && $levels != '' ) |
| 432 |
{ |
| 433 |
$redirect = '-2'; |
| 434 |
} |
| 435 |
|
| 436 |
$this->ppprotectInsertInDb( $type, $term_id, $name, $levels, $redirect, $protectPosts ); |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* @author William DeAngelis |
| 441 |
* @var integer $id The ID of the category to be deleted |
| 442 |
* |
| 443 |
* @return boolean $response True or false if the category was deleted from the ppprotect table |
| 444 |
**/ |
| 445 |
public function ppprotectDeleteCategory( $id ) |
| 446 |
{ |
| 447 |
if ( !is_array( $this->membershipLevels) || empty( $this->membershipLevels ) ) |
| 448 |
{ |
| 449 |
return; |
| 450 |
} |
| 451 |
|
| 452 |
global $wpdb; |
| 453 |
|
| 454 |
$table = $wpdb->prefix . 'ppprotect'; |
| 455 |
|
| 456 |
$id = intval($id); |
| 457 |
$items = array( 'itemId' => $id ); |
| 458 |
|
| 459 |
$response = $this->ppprotectDeleteFromDb( $table, $items ); |
| 460 |
|
| 461 |
return $response; |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* @author William DeAngelis |
| 466 |
* @var integer $catId The ID of the category in the loop |
| 467 |
* @var array $perms An array of all the category's permission options for a given category |
| 468 |
* @var array $levels An array that contains all of the user's permission levels |
| 469 |
* @var array $userAccessLevels An array that contains the users permission levels IF they match the levels |
| 470 |
* needed to view this post. IF empty then it will use the redirect |
| 471 |
* |
| 472 |
* @return string Checks the users permission levels and the redirects them if they don't have the proper perms |
| 473 |
**/ |
| 474 |
public function ppprotectCategory() |
| 475 |
{ |
| 476 |
$queried_object = get_queried_object(); |
| 477 |
|
| 478 |
if (isset($queried_object) && |
| 479 |
isset($queried_object->term_id) && |
| 480 |
$queried_object->slug !== 'uncategorized') |
| 481 |
{ |
| 482 |
$catId = $queried_object->term_id; |
| 483 |
$perms = $this->ppprotectGetFromDb($catId); |
| 484 |
|
| 485 |
if (isset($perms) && $perms != null && !empty($perms->levels)) |
| 486 |
{ |
| 487 |
$pass_thru = false; |
| 488 |
|
| 489 |
$levels = json_decode($perms->levels); |
| 490 |
if (is_array($levels)) |
| 491 |
{ |
| 492 |
foreach ($levels as $level) |
| 493 |
{ |
| 494 |
if ($this->ppprotectAccessCheck($level) === true) |
| 495 |
{ |
| 496 |
$pass_thru = true; |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
// If user does not have any access levels granted... redirect them |
| 502 |
if (!$pass_thru) |
| 503 |
{ |
| 504 |
if (!current_user_can('administrator')) |
| 505 |
{ |
| 506 |
if ($perms->redirect == self::LOGIN_PAGE) |
| 507 |
{ |
| 508 |
add_filter('the_content', array(&$this, 'ppprotectLoginPage')); |
| 509 |
return; |
| 510 |
} |
| 511 |
|
| 512 |
if ($perms->redirect == self::HOME_PAGE) |
| 513 |
{ |
| 514 |
wp_safe_redirect(home_url()); |
| 515 |
exit; |
| 516 |
} |
| 517 |
|
| 518 |
wp_safe_redirect($perms->redirect); |
| 519 |
exit; |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
/** |
| 527 |
* @author William DeAngelis |
| 528 |
* @var integer $postID The ID of the current post |
| 529 |
* @var array $catOfPost An array containing all of the categories the post is assigned to |
| 530 |
* @var integer $catId The ID of the category in the loop |
| 531 |
* @var array $perms An array of all the category's permission options for a given category |
| 532 |
* @var array $protectCategories A blank array that gets created here and contains all the possible category |
| 533 |
* permission settings |
| 534 |
* @var array $userAccessLevels An array that contains the users permission levels IF they match the levels |
| 535 |
* needed to view this post. IF empty then it will use the redirect |
| 536 |
* @var string $selectedOverride Variable set by the user. Two options 'post-override' (Allows the post to |
| 537 |
* override the category settings) and 'category-override' (Allows the category's settings to override the |
| 538 |
* post's.). These options are used here to determine which option is currently selected and to update the |
| 539 |
* option accordingly in the dropdown select. |
| 540 |
* |
| 541 |
* @return string Checks the users permission levels and the redirects them if they don't have the proper perms |
| 542 |
**/ |
| 543 |
public function ppprotectPost() |
| 544 |
{ |
| 545 |
if (!is_admin() && is_single()) |
| 546 |
{ |
| 547 |
global $wp_query; |
| 548 |
$postID = $wp_query->post->ID; |
| 549 |
$catOfPost = get_the_category($postID); |
| 550 |
$selectedOverride = get_post_meta($postID, '_ppProtectCatOverride', true); |
| 551 |
|
| 552 |
$userAccessLevels = array(); |
| 553 |
$protectCategories = array(); |
| 554 |
foreach ($catOfPost as $cat) |
| 555 |
{ |
| 556 |
$catId = $cat->term_id; |
| 557 |
$perms = $this->ppprotectGetFromDb($catId); |
| 558 |
|
| 559 |
if (isset($perms)) |
| 560 |
{ |
| 561 |
array_push($protectCategories, $perms); |
| 562 |
} |
| 563 |
|
| 564 |
if (isset($perms)) |
| 565 |
{ |
| 566 |
$levels = json_decode($perms->levels); |
| 567 |
if (isset($levels)) |
| 568 |
{ |
| 569 |
foreach ($levels as $level) |
| 570 |
{ |
| 571 |
if ($this->ppprotectAccessCheck($level) === true) |
| 572 |
{ |
| 573 |
array_push($userAccessLevels, $level); |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
// If user does not have any access levels granted & the post is in a protected category & the post isn't manually specified to use it's own permissions... redirect them to the first categories redirect |
| 581 |
if (empty($userAccessLevels) && !empty($protectCategories) && $selectedOverride != 'post-override') |
| 582 |
{ |
| 583 |
// If admin user is logged in, let them see the page. Comment this out to test functionality |
| 584 |
if (!current_user_can('administrator')) |
| 585 |
{ |
| 586 |
foreach ($protectCategories as $protectedCat) |
| 587 |
{ |
| 588 |
if ($protectedCat->protectposts == 1) |
| 589 |
{ |
| 590 |
if ($protectedCat->redirect == '-1') |
| 591 |
{ |
| 592 |
wp_redirect(home_url()); |
| 593 |
exit; |
| 594 |
} |
| 595 |
else if ( $protectedCat->redirect == '-2' ) |
| 596 |
{ |
| 597 |
add_filter('the_content', array(&$this, 'ppprotectLoginPage')); |
| 598 |
add_filter('comments_open', array(&$this, 'ppprotectCloseComments'), 10, 2); |
| 599 |
add_filter('get_comments_number', array(&$this, 'ppprotectGetCommentsNumber'), 10, 1); |
| 600 |
return; |
| 601 |
} |
| 602 |
|
| 603 |
wp_redirect($protectedCat->redirect); |
| 604 |
exit; |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
/** |
| 614 |
* @author William DeAngelis |
| 615 |
* @var object $content The content being filtered |
| 616 |
* |
| 617 |
* @return string $lp Removes the page content and replaces it with the login_page shortcode content which is a |
| 618 |
* basic WP login form |
| 619 |
* |
| 620 |
**/ |
| 621 |
public function ppprotectLoginPage($content) |
| 622 |
{ |
| 623 |
if(isset($_COOKIE["contact_id"]) && is_numeric($_COOKIE["contact_id"])) |
| 624 |
{ |
| 625 |
$contact_id = (int) $_COOKIE["contact_id"]; |
| 626 |
$category_id = get_queried_object_id(); |
| 627 |
set_transient("pilotpress_redirect_to" . $contact_id, 'category_' . $category_id, self::FIVE_MINUTES); |
| 628 |
return do_shortcode('[login_page]'); |
| 629 |
} |
| 630 |
|
| 631 |
return $content; |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
/** |
| 636 |
* @author William DeAngelis |
| 637 |
* @var boolean $open If the page has comments enabled |
| 638 |
* @var integer $post_id The id of the page |
| 639 |
* |
| 640 |
* @return string $open Disables comments on this page |
| 641 |
* |
| 642 |
**/ |
| 643 |
public function ppprotectCloseComments($open, $post_id) |
| 644 |
{ |
| 645 |
return false; |
| 646 |
} |
| 647 |
|
| 648 |
/** |
| 649 |
* @brief zero out comments number when we wanna close comments so template doesn't load comments section |
| 650 |
* |
| 651 |
* @param $post_id |
| 652 |
* |
| 653 |
* @return int 0 |
| 654 |
*/ |
| 655 |
public function ppprotectGetCommentsNumber($post_id) |
| 656 |
{ |
| 657 |
return 0; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
/** |
| 662 |
* @author William DeAngelis |
| 663 |
* @var object $query The query being called |
| 664 |
* |
| 665 |
* @return string The modified query with protected categories being protected. |
| 666 |
* |
| 667 |
**/ |
| 668 |
function ppprotectHomeAndArchivePosts($query) |
| 669 |
{ |
| 670 |
if (($query->is_home() && $query->is_main_query())) |
| 671 |
{ |
| 672 |
global $wpdb; |
| 673 |
$table = $wpdb->prefix . 'ppprotect'; |
| 674 |
$postCategories = get_terms('category', array('hide_empty' => false)); |
| 675 |
|
| 676 |
if ($wpdb->get_var("SHOW TABLES LIKE '$table'") === null) |
| 677 |
{ |
| 678 |
$this->ppprotectCreateTable(); |
| 679 |
} |
| 680 |
|
| 681 |
$cats = $wpdb->get_results('SELECT itemId,levels FROM ' . $table . ' WHERE protectposts = 1'); |
| 682 |
$protectedCategories = array(); |
| 683 |
foreach ($cats as $key => $value) |
| 684 |
{ |
| 685 |
if (isset($_SESSION['user_levels']) && is_array($_SESSION['user_levels'])) |
| 686 |
{ |
| 687 |
$perm = array_intersect(json_decode($value->levels), $_SESSION['user_levels']); |
| 688 |
if (empty($perm)) |
| 689 |
{ |
| 690 |
$protectedCategories[] = '-' . $value->itemId; |
| 691 |
} |
| 692 |
} |
| 693 |
else |
| 694 |
{ |
| 695 |
$protectedCategories[] = '-' . $value->itemId; |
| 696 |
} |
| 697 |
} |
| 698 |
$protectedCategories = implode(',', $protectedCategories); |
| 699 |
if ($protectedCategories) |
| 700 |
{ |
| 701 |
$query->set('cat', $protectedCategories); |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
/** |
| 707 |
* @brief keeps globally protected posts out of the recent posts widget |
| 708 |
* |
| 709 |
* @param array $params |
| 710 |
* |
| 711 |
* @return array $params |
| 712 |
* |
| 713 |
* @author Richard Young <ryoung@ontraport.com> |
| 714 |
*/ |
| 715 |
function ppprotectRecentPosts($params) |
| 716 |
{ |
| 717 |
global $wpdb; |
| 718 |
$table = $wpdb->prefix . 'ppprotect'; |
| 719 |
$categories = $wpdb->get_results("SELECT itemId, levels FROM $table WHERE protectposts = 1"); |
| 720 |
|
| 721 |
foreach ($categories as $key => $value) |
| 722 |
{ |
| 723 |
if (isset($_SESSION['user_levels']) && is_array($_SESSION['user_levels'])) |
| 724 |
{ |
| 725 |
$perm = array_intersect(json_decode($value->levels), $_SESSION['user_levels']); |
| 726 |
if (empty($perm)) |
| 727 |
{ |
| 728 |
$protectedCategories[] = $value->itemId; |
| 729 |
} |
| 730 |
} |
| 731 |
else |
| 732 |
{ |
| 733 |
$protectedCategories[] = $value->itemId; |
| 734 |
} |
| 735 |
} |
| 736 |
if (isset($protectedCategories)) |
| 737 |
{ |
| 738 |
$params["category__not_in"] = $protectedCategories; |
| 739 |
} |
| 740 |
return $params; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
/** |
| 745 |
* @author William DeAngelis |
| 746 |
* |
| 747 |
* @return string After updating the _ppProtectCatOverride setting that determines whether the category or the post's permissions will be used to protect it, it returns the selected option from the db and gives it to AJAX query set in ppprotectAdminCategoryScripts() |
| 748 |
* |
| 749 |
* @see ppprotectAdminCategoryScripts() The function that uses this function via AJAX to update and check the override setting |
| 750 |
**/ |
| 751 |
public function wp_ajax_ppprotectAllowOverride() |
| 752 |
{ |
| 753 |
if( !empty($_POST) ) |
| 754 |
{ |
| 755 |
// Make sure we have an ID here. |
| 756 |
if (isset($_POST['postID']) && is_numeric($_POST['postID'])) |
| 757 |
{ |
| 758 |
$post_id = (int) $_POST['postID']; |
| 759 |
$override_val = sanitize_text_field($_POST['ppOverride']); |
| 760 |
update_post_meta( $post_id, '_ppProtectCatOverride', $override_val); |
| 761 |
$response = get_post_meta( $post_id, '_ppProtectCatOverride', true ); |
| 762 |
} |
| 763 |
} |
| 764 |
else |
| 765 |
{ |
| 766 |
$response = "No POST detected."; |
| 767 |
} |
| 768 |
|
| 769 |
header( "Content-Type: application/json" ); |
| 770 |
echo json_encode($response); |
| 771 |
exit(); |
| 772 |
} |
| 773 |
|
| 774 |
/** |
| 775 |
* @author William DeAngelis |
| 776 |
* @var integer $postID The ID of the current post |
| 777 |
* @var array $catOfPost An array containing all of the categories the post is assigned to |
| 778 |
* @var integer $catId The ID of the category in the loop |
| 779 |
* @var array $perms An array of all the category's permission options for a given category |
| 780 |
* @var array $protectedCategories An blank array that gets created here and contains all the possible category permission settings |
| 781 |
* @var string $selectedOverride Variable set by the user. Two options 'post-override' (Allows the post to override the category settings) and 'category-override' (Allows the category's settings to override the post's.). These options are used here to determine which option is currently selected and to update the option accordingly in the dropdown select. |
| 782 |
* |
| 783 |
* @return string Warns users when global cateogry protection settings are taking prescendence, provides an interface to see exactly what the category settings will do, and provides an option to override the category's protection settings |
| 784 |
**/ |
| 785 |
public function ppprotectPostWarning() |
| 786 |
{ |
| 787 |
if ( is_admin() ) |
| 788 |
{ |
| 789 |
global $post; |
| 790 |
$postID = $post->ID; |
| 791 |
$catOfPost = get_the_category($postID); |
| 792 |
|
| 793 |
$protectedCategories = array(); |
| 794 |
foreach ( $catOfPost as $cat ) |
| 795 |
{ |
| 796 |
$catId = $cat->term_id; |
| 797 |
$perms = $this->ppprotectGetFromDb( $catId ); |
| 798 |
if ( isset($perms) && $perms->protectposts == 1 ) |
| 799 |
{ |
| 800 |
array_push($protectedCategories, $perms); |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
$selectedOverride = get_post_meta( $postID, '_ppProtectCatOverride', true ); |
| 805 |
switch ($selectedOverride) |
| 806 |
{ |
| 807 |
case 'post-override': |
| 808 |
$postOverride = 'selected="selected"'; |
| 809 |
$catOverride = ''; |
| 810 |
break; |
| 811 |
|
| 812 |
case 'category-override': |
| 813 |
$catOverride = 'selected="selected"'; |
| 814 |
$postOverride = ''; |
| 815 |
break; |
| 816 |
|
| 817 |
default: |
| 818 |
$catOverride = ''; |
| 819 |
$postOverride = ''; |
| 820 |
} |
| 821 |
|
| 822 |
// If post is protected globally, change the PP options to reflect the override. |
| 823 |
if ( !empty($protectedCategories) ) |
| 824 |
{ |
| 825 |
$message = '<div class="ppprotect-protected-global-wrapper inside"> |
| 826 |
<div class="ppprotect-protected-globally"> |
| 827 |
<div class="ppprotect-global-message">This post has global category access permissions set in the following category(s) that take priority over the permissions usually found here.</div> |
| 828 |
<div class="ppprotect-global-cats">'; |
| 829 |
|
| 830 |
foreach ( $protectedCategories as $protectedCategory ) |
| 831 |
{ |
| 832 |
$message .= '<a href="' . site_url() . '/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=' . $protectedCategory->itemId . '">' . $protectedCategory->name . '</a>'; |
| 833 |
|
| 834 |
} |
| 835 |
|
| 836 |
$message .= '</div>'; |
| 837 |
|
| 838 |
if ( is_array($protectedCategories) && count($protectedCategories) !== 1 ) |
| 839 |
{ |
| 840 |
$message .= '<div class="ppprotect-main-override">The category whose redirect will take prescedence is:</div> |
| 841 |
<div class="ppprotect-override-name"><a href="' . site_url() . '/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=' . $protectedCategories[0]->itemId . '">' . $protectedCategories[0]->name . '</a></div>'; |
| 842 |
} |
| 843 |
|
| 844 |
if ( $protectedCategories[0]->redirect == '-1' || $protectedCategories[0]->redirect == '-2' ) |
| 845 |
{ |
| 846 |
if ( $protectedCategories[0]->redirect == '-1' ) |
| 847 |
{ |
| 848 |
$redirect = '(homepage)'; |
| 849 |
} |
| 850 |
else |
| 851 |
{ |
| 852 |
$redirect = '(login page)'; |
| 853 |
} |
| 854 |
|
| 855 |
$message .= '<div class="ppprotect-override-location">It currently redirects users to the:<br />' . $redirect . '</div> |
| 856 |
<div class="ppprotect-override-perms">Users with the following access levels have access to this post:'; |
| 857 |
} |
| 858 |
else |
| 859 |
{ |
| 860 |
$message .= '<div class="ppprotect-override-location">It currently redirects users to:<br /><a href="' . $protectedCategories[0]->redirect . '" target="_blank">' . $protectedCategories[0]->redirect . '</a></div> |
| 861 |
<div class="ppprotect-override-perms">Users with the following access levels have access to this post:'; |
| 862 |
} |
| 863 |
|
| 864 |
if ( isset($protectedCategories[0]->levels) && !empty($protectedCategories[0]->levels) ) |
| 865 |
{ |
| 866 |
foreach ( json_decode($protectedCategories[0]->levels) as $level ) |
| 867 |
{ |
| 868 |
$message .= '<div>' . $level . '</div>'; |
| 869 |
} |
| 870 |
} |
| 871 |
else |
| 872 |
{ |
| 873 |
$message .= '<div>No permissions have been selected.</div>'; |
| 874 |
} |
| 875 |
|
| 876 |
$message .= '</div> |
| 877 |
</div> |
| 878 |
<div class="ppprotect-override-override"> |
| 879 |
<select name="ppprotectManualOverride"> |
| 880 |
<option value="category-override" ' . $catOverride . '>Category override</option> |
| 881 |
<option value="post-override" ' . $postOverride . '>Set permissions manually</option> |
| 882 |
</select> |
| 883 |
</div> |
| 884 |
</div>'; |
| 885 |
|
| 886 |
echo $message; |
| 887 |
|
| 888 |
// Bind the JS to the footer to control the category override settings |
| 889 |
add_action( 'admin_footer', array( &$this, 'ppprotectAdminCategoryScripts') ); |
| 890 |
|
| 891 |
} |
| 892 |
|
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
/** |
| 897 |
* @author William DeAngelis |
| 898 |
* @var integer $postID The ID of the current post |
| 899 |
* @var string $selectedOverride Variable set by the user. Two options 'post-override' (Allows the post to override the category settings) and 'category-override' (Allows the category's settings to override the post's.). These options are used here to determine what to display in the post's PP options metabox. |
| 900 |
* |
| 901 |
* @return string Adds footer scripts to post pages that have category permissions applied to the posts. This js manages the ability to override the category permissions by using an AJAX call to set the $selectedOverride varabile via the wp_ajax_ppprotectAllowOverride function. This variable gets used to control whether the category permission settings or the post's permission settings take prescedence. |
| 902 |
* |
| 903 |
* @see wp_ajax_ppprotectAllowOverride() The function that updates the override variable |
| 904 |
**/ |
| 905 |
public function ppprotectAdminCategoryScripts() |
| 906 |
{ |
| 907 |
global $post; |
| 908 |
$postID = $post->ID; |
| 909 |
|
| 910 |
$selectedOverride = get_post_meta( $postID, '_ppProtectCatOverride', true ); |
| 911 |
|
| 912 |
$jsMods = '<script type="text/javascript"> |
| 913 |
jQuery(document).ready(function() |
| 914 |
{ |
| 915 |
jQuery("#_pilotpress_page_box .inside").addClass("pp-page-box").hide(); |
| 916 |
jQuery(".ppprotect-protected-global-wrapper").appendTo(jQuery("#_pilotpress_page_box"));'; |
| 917 |
|
| 918 |
if ( !isset($selectedOverride) || $selectedOverride == 'post-override' ) |
| 919 |
{ |
| 920 |
$jsMods .= 'jQuery(".pp-page-box").show(); |
| 921 |
jQuery(".ppprotect-protected-globally").hide();'; |
| 922 |
} |
| 923 |
|
| 924 |
$jsMods .= ' |
| 925 |
jQuery(".ppprotect-override-override select").change(function() { |
| 926 |
|
| 927 |
var selectedOption = jQuery(this).val(); |
| 928 |
jQuery.ajax({ |
| 929 |
type: "POST", |
| 930 |
url: ajaxurl, |
| 931 |
data: { |
| 932 |
action: "pp_category_override", |
| 933 |
postID: ' . $postID . ', |
| 934 |
ppOverride: selectedOption |
| 935 |
} |
| 936 |
}).done(function( response ) { |
| 937 |
if ( response == "post-override" ) { |
| 938 |
jQuery(".pp-page-box").show(); |
| 939 |
jQuery(".ppprotect-protected-globally").hide(); |
| 940 |
} |
| 941 |
else if ( response == "category-override" ) { |
| 942 |
jQuery(".pp-page-box").hide(); |
| 943 |
jQuery(".ppprotect-protected-globally").show(); |
| 944 |
} |
| 945 |
}); |
| 946 |
|
| 947 |
}); |
| 948 |
}); |
| 949 |
</script>'; |
| 950 |
|
| 951 |
echo $jsMods; |
| 952 |
} |
| 953 |
|
| 954 |
/** |
| 955 |
* @author William DeAngelis |
| 956 |
* |
| 957 |
* @return string Adds a footer script to admin category setting pages. Informs the user that by choosing to protect all posts in this cateogry the category permissions will override the post permissions. |
| 958 |
**/ |
| 959 |
public function ppprotectCategoryJS() |
| 960 |
{ |
| 961 |
$categories = get_current_screen(); |
| 962 |
if ( isset($categories) ) |
| 963 |
{ |
| 964 |
if ( $categories->base == 'edit-tags' ) |
| 965 |
{ |
| 966 |
|
| 967 |
$catFoot = '<script type="text/javascript"> |
| 968 |
jQuery(document).ready(function() |
| 969 |
{ |
| 970 |
jQuery(".ppprotect-posts input:checkbox").change(function() |
| 971 |
{ |
| 972 |
if ( this.checked === true ) |
| 973 |
{ |
| 974 |
var accept = confirm("IMPORTANT - By selecting this option you will override the PilotPress permission settings you may have already added to any of the posts in this category. This means that the settings you just selected here will take prescedence. Once you save this setting you will still be able to manually set permissions for each post, but you will have to open each post and select the option \'Set permissions manually\' to do so. Are you sure you want to proceed with this setting?"); |
| 975 |
if ( accept != true ) |
| 976 |
{ |
| 977 |
jQuery(this).prop("checked", false); |
| 978 |
} |
| 979 |
} |
| 980 |
}); |
| 981 |
}); |
| 982 |
</script>'; |
| 983 |
|
| 984 |
echo $catFoot; |
| 985 |
|
| 986 |
} |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
} |