142 lines
4.3 KiB
PHP
142 lines
4.3 KiB
PHP
<?php
|
|
|
|
if (!defined('ABSPATH')) exit;
|
|
|
|
class LCP_Paywall {
|
|
private $rules;
|
|
private $admin;
|
|
|
|
public function init() {
|
|
$this->rules = new LCP_Paywall_Rules();
|
|
|
|
if (is_admin()) {
|
|
$this->admin = new LCP_Paywall_Admin();
|
|
}
|
|
|
|
add_action('init', array($this, 'init_hooks'));
|
|
add_filter('the_content', array($this, 'maybe_restrict_content'), 999);
|
|
}
|
|
|
|
public function init_hooks() {
|
|
// Register scripts and styles
|
|
add_action('admin_enqueue_scripts', array($this, 'register_admin_assets'));
|
|
}
|
|
|
|
public function register_admin_assets() {
|
|
wp_register_style(
|
|
'lcp-paywall-admin',
|
|
LCP_PAYWALL_URL . 'assets/css/admin.css',
|
|
array(),
|
|
LCP_PAYWALL_VERSION
|
|
);
|
|
|
|
wp_register_script(
|
|
'lcp-paywall-admin',
|
|
LCP_PAYWALL_URL . 'assets/js/admin.js',
|
|
array('jquery', 'jquery-ui-sortable'),
|
|
LCP_PAYWALL_VERSION,
|
|
true
|
|
);
|
|
}
|
|
|
|
public function maybe_restrict_content($content) {
|
|
if (!is_singular()) return $content;
|
|
|
|
$post_type = get_post_type();
|
|
$post_id = get_the_ID();
|
|
|
|
if ($this->should_restrict_access($post_type, $post_id)) {
|
|
return $this->get_nag_message();
|
|
}
|
|
|
|
return $content;
|
|
}
|
|
|
|
private function should_restrict_access($post_type, $post_id) {
|
|
$post_type_rules = $this->rules->get_rules_for_post_type($post_type);
|
|
|
|
// Get default lock status
|
|
$default_lock_status = isset($post_type_rules['settings']['default_lock_status']) ?
|
|
$post_type_rules['settings']['default_lock_status'] === 'locked' : true;
|
|
|
|
// If no rules exist, use default lock status
|
|
if (empty($post_type_rules['rules'])) {
|
|
return $default_lock_status;
|
|
}
|
|
|
|
// Check each rule in order until one evaluates to true
|
|
foreach ($post_type_rules['rules'] as $rule) {
|
|
if ($this->evaluate_rule($rule, $post_id)) {
|
|
// First matching rule determines the lock status
|
|
return $rule['action'] === 'lock';
|
|
}
|
|
}
|
|
|
|
// If no rules match, use default lock status
|
|
return $default_lock_status;
|
|
}
|
|
|
|
private function evaluate_rule($rule, $post_id) {
|
|
if ($rule['condition'] === 'user') {
|
|
return $this->evaluate_user_rule($rule);
|
|
} else if ($rule['condition'] === 'post') {
|
|
return $this->evaluate_post_rule($rule, $post_id);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private function evaluate_user_rule($rule) {
|
|
if ($rule['value_type'] === 'logged_in') {
|
|
$is_logged_in = is_user_logged_in();
|
|
|
|
switch ($rule['operator']) {
|
|
case '=':
|
|
case 'is':
|
|
return $is_logged_in;
|
|
case '!=':
|
|
case 'isnot':
|
|
return !$is_logged_in;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private function evaluate_post_rule($rule, $post_id) {
|
|
if ($rule['value_type'] === 'taxonomy' && !empty($rule['taxonomy']) && !empty($rule['term'])) {
|
|
$terms = wp_get_post_terms($post_id, $rule['taxonomy'], array('fields' => 'ids'));
|
|
|
|
if (is_wp_error($terms)) {
|
|
return false;
|
|
}
|
|
|
|
switch ($rule['operator']) {
|
|
case '=':
|
|
case 'in':
|
|
return in_array((int)$rule['term'], $terms);
|
|
case '!=':
|
|
case 'notin':
|
|
return !in_array((int)$rule['term'], $terms);
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private function get_nag_message() {
|
|
$settings = get_option('lcp_paywall_settings', array());
|
|
$message = isset($settings['nag_message']) ? $settings['nag_message'] : '';
|
|
|
|
if (empty($message)) {
|
|
$message = '<div class="lcp-paywall-nag">
|
|
<h3>This content is locked</h3>
|
|
<p>Please upgrade your subscription to access this content.</p>
|
|
</div>';
|
|
}
|
|
|
|
return apply_filters('lcp_paywall_nag_message', $message);
|
|
}
|
|
}
|