Frontend
3 months ago
views
1 year ago
CapabilityCheck.php
3 years ago
ConditionCallbacks.php
3 years ago
ConditionalBlocksIntegration.php
3 years ago
ContentConditions.php
7 months ago
ElementorDisplayCondition.php
1 month ago
ElementorRestriction.php
3 years ago
Init.php
1 month ago
NavMenuProtection.php
3 years ago
SettingsPage.php
1 year ago
WPListTable.php
1 year ago
index.php
5 years ago
SettingsPage.php
300 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ContentProtection; |
| 4 | |
| 5 | use ProfilePress\Core\Admin\SettingsPages\AbstractSettingsPage; |
| 6 | use ProfilePress\Core\Classes\PROFILEPRESS_sql; |
| 7 | use ProfilePress\Custom_Settings_Page_Api; |
| 8 | |
| 9 | class SettingsPage extends AbstractSettingsPage |
| 10 | { |
| 11 | public $content_protection_rule_errors; |
| 12 | |
| 13 | /** @var WPListTable */ |
| 14 | private $myListTable; |
| 15 | |
| 16 | const META_DATA_KEY = 'content_restrict_data'; |
| 17 | |
| 18 | function __construct() |
| 19 | { |
| 20 | add_action('ppress_register_menu_page', [$this, 'register_cpf_settings_page']); |
| 21 | add_action('ppress_admin_settings_page_content-protection', [$this, 'settings_page_function']); |
| 22 | |
| 23 | add_filter('set-screen-option', [__CLASS__, 'set_screen'], 10, 3); |
| 24 | add_filter('set_screen_option_rules_per_page', [__CLASS__, 'set_screen'], 10, 3); |
| 25 | |
| 26 | add_action('admin_init', function () { |
| 27 | if (ppressGET_var('page') == PPRESS_CONTENT_PROTECTION_SETTINGS_SLUG) { |
| 28 | |
| 29 | if (isset($_GET['add']) && $_GET['add'] == 'new') { |
| 30 | $this->save_rule('add'); |
| 31 | } |
| 32 | |
| 33 | if (isset($_GET['action']) && $_GET['action'] == 'edit') { |
| 34 | $this->save_rule('edit'); |
| 35 | } |
| 36 | } |
| 37 | }); |
| 38 | |
| 39 | add_action('ppress_admin_notices', [$this, 'logic_upgrade_notice']); |
| 40 | } |
| 41 | |
| 42 | public function logic_upgrade_notice() |
| 43 | { |
| 44 | if (ppressGET_var('page') == PPRESS_CONTENT_PROTECTION_SETTINGS_SLUG && ppressGET_var('action') == 'edit') { |
| 45 | $id = absint(ppressGET_var('id')); |
| 46 | $db = PROFILEPRESS_sql::get_meta_value($id, self::META_DATA_KEY); |
| 47 | if (ppress_var($db, 'is_new') == 'true') return; |
| 48 | ?> |
| 49 | <div class="notice notice-error"> |
| 50 | <p style="font-size: 14px;"><?php printf( |
| 51 | esc_html__('We have changed the logic of protecting content. %1$sOR%2$s is now %1$sAND%2$s while %1$sAND%2$s has been changed to %1$sOR%2$s. Please review the "Content to Protect" setup and ensure everything is perfect before saving.', 'wp-user-avatar'), |
| 52 | '<strong>', '</strong>', |
| 53 | ); ?></p> |
| 54 | </div> |
| 55 | <?php |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public function admin_page_title() |
| 60 | { |
| 61 | $title = esc_html__('Content Protection', 'wp-user-avatar'); |
| 62 | |
| 63 | if (isset($_GET['action']) && $_GET['action'] == 'edit') { |
| 64 | $title = esc_html__('Edit Protection Rule', 'wp-user-avatar'); |
| 65 | } |
| 66 | |
| 67 | if (isset($_GET['add']) && $_GET['add'] == 'new') { |
| 68 | $title = esc_html__('Add Protection Rule', 'wp-user-avatar'); |
| 69 | } |
| 70 | |
| 71 | return apply_filters('ppress_content_protection_settings_page_title', $title); |
| 72 | } |
| 73 | |
| 74 | public function register_cpf_settings_page() |
| 75 | { |
| 76 | $hook = add_submenu_page( |
| 77 | PPRESS_DASHBOARD_SETTINGS_SLUG, |
| 78 | 'ProfilePress ' . $this->admin_page_title(), |
| 79 | esc_html__('Content Protection', 'wp-user-avatar'), |
| 80 | 'manage_options', |
| 81 | PPRESS_CONTENT_PROTECTION_SETTINGS_SLUG, |
| 82 | array($this, 'admin_page_callback')); |
| 83 | |
| 84 | add_action("load-$hook", array($this, 'add_options')); |
| 85 | } |
| 86 | |
| 87 | public function header_menu_tabs() |
| 88 | { |
| 89 | $tabs = apply_filters('ppress_membership_content_protection_settings_page_tabs', [ |
| 90 | 5 => [ |
| 91 | 'id' => $this->default_header_menu(), |
| 92 | 'url' => PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE, |
| 93 | 'label' => esc_html__('Content Protection', 'wp-user-avatar') |
| 94 | ], |
| 95 | ]); |
| 96 | |
| 97 | ksort($tabs); |
| 98 | |
| 99 | return $tabs; |
| 100 | } |
| 101 | |
| 102 | public function default_header_menu() |
| 103 | { |
| 104 | return 'content-protection'; |
| 105 | } |
| 106 | |
| 107 | public static function set_screen($status, $option, $value) |
| 108 | { |
| 109 | return $value; |
| 110 | } |
| 111 | |
| 112 | public function add_options() |
| 113 | { |
| 114 | $args = [ |
| 115 | 'label' => esc_html__('Protection Rules', 'wp-user-avatar'), |
| 116 | 'default' => 10, |
| 117 | 'option' => 'rules_per_page' |
| 118 | ]; |
| 119 | |
| 120 | add_screen_option('per_page', $args); |
| 121 | |
| 122 | add_filter('screen_options_show_screen', '__return_false'); |
| 123 | |
| 124 | $this->myListTable = new WPListTable(); |
| 125 | } |
| 126 | |
| 127 | public function sanitize_data($data) |
| 128 | { |
| 129 | if (is_string($data)) { |
| 130 | return sanitize_text_field($data); |
| 131 | } |
| 132 | |
| 133 | $sanitized_data = []; |
| 134 | |
| 135 | foreach ($data as $key => $value) { |
| 136 | |
| 137 | if (is_array($data[$key])) { |
| 138 | $sanitized_data[$key] = self::sanitize_data($value); |
| 139 | } else { |
| 140 | if ($key == 'noaccess_action_message_custom') { |
| 141 | $sanitized_data[$key] = wp_kses_post($value); |
| 142 | } else { |
| 143 | $sanitized_data[$key] = sanitize_textarea_field($value); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return $sanitized_data; |
| 149 | } |
| 150 | |
| 151 | public function save_rule($type) |
| 152 | { |
| 153 | if ( ! isset($_POST['ppress_save_rule'])) return; |
| 154 | |
| 155 | check_admin_referer('wp-csa-nonce', 'wp_csa_nonce'); |
| 156 | |
| 157 | if ( ! current_user_can('manage_options')) return; |
| 158 | |
| 159 | if (empty($_POST['ppress_cc_data']['title'])) { |
| 160 | return $this->content_protection_rule_errors = esc_html__('Title cannot be empty.', 'wp-user-avatar'); |
| 161 | } |
| 162 | |
| 163 | if (isset($_POST['ppress_cc_data']) && is_array($_POST['ppress_cc_data'])) { |
| 164 | $_POST['ppress_cc_data']['is_new'] = 'true'; |
| 165 | } |
| 166 | |
| 167 | if ('add' == $type) { |
| 168 | $id = PROFILEPRESS_sql::add_meta_data(self::META_DATA_KEY, $this->sanitize_data($_POST['ppress_cc_data'])); |
| 169 | |
| 170 | if (is_int($id)) { |
| 171 | wp_safe_redirect(add_query_arg([ |
| 172 | 'action' => 'edit', |
| 173 | 'id' => $id, |
| 174 | 'rule-updated' => 'true' |
| 175 | ], PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE)); |
| 176 | exit; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if ('edit' == $type) { |
| 181 | $rule_id = absint($_GET['id']); |
| 182 | $response = PROFILEPRESS_sql::update_meta_value($rule_id, self::META_DATA_KEY, $this->sanitize_data($_POST['ppress_cc_data'])); |
| 183 | |
| 184 | if (false !== $response) { |
| 185 | wp_safe_redirect(add_query_arg([ |
| 186 | 'action' => 'edit', |
| 187 | 'id' => $rule_id, |
| 188 | 'rule-updated' => 'true' |
| 189 | ], PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE)); |
| 190 | exit; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | public function admin_notices() |
| 196 | { |
| 197 | if ( ! isset($_GET['rule-updated']) && ! isset($this->content_protection_rule_errors)) return; |
| 198 | |
| 199 | $status = 'updated'; |
| 200 | if (isset($this->content_protection_rule_errors)) { |
| 201 | $message = $this->content_protection_rule_errors; |
| 202 | $status = 'error'; |
| 203 | } |
| 204 | |
| 205 | if (isset($_GET['rule-updated'])) { |
| 206 | $message = esc_html__('Changes saved.', 'wp-user-avatar'); |
| 207 | } |
| 208 | |
| 209 | printf('<div id="message" class="%s notice is-dismissible"><p>%s</strong></p></div>', $status, $message); |
| 210 | } |
| 211 | |
| 212 | public function settings_page_function() |
| 213 | { |
| 214 | add_filter('wp_cspa_main_content_area', array($this, 'admin_settings_page_callback'), 10, 2); |
| 215 | add_action('wp_cspa_before_closing_header', [$this, 'add_new_button']); |
| 216 | |
| 217 | $is_sidebar = ! isset($_GET['action'], $_GET['id']) && ! isset($_GET['add']); |
| 218 | |
| 219 | $instance = Custom_Settings_Page_Api::instance(); |
| 220 | $instance->option_name('ppview'); // adds ppview css class to #poststuff |
| 221 | $instance->page_header($this->admin_page_title()); |
| 222 | |
| 223 | if ($is_sidebar) { |
| 224 | |
| 225 | $sidebar_args = [ |
| 226 | [ |
| 227 | 'section_title' => esc_html__('Restrict Content Shortcode', 'wp-user-avatar'), |
| 228 | 'content' => (function () { |
| 229 | ob_start(); |
| 230 | require dirname(__FILE__) . '/views/include.shortcode-doc-sidebar.php'; |
| 231 | |
| 232 | return ob_get_clean(); |
| 233 | })() |
| 234 | ], |
| 235 | [ |
| 236 | 'section_title' => esc_html__('WordPress Menu Protection', 'wp-user-avatar'), |
| 237 | 'content' => (function () { |
| 238 | ob_start(); |
| 239 | require dirname(__FILE__) . '/views/include.menu-protection.php'; |
| 240 | |
| 241 | return ob_get_clean(); |
| 242 | })() |
| 243 | ] |
| 244 | ]; |
| 245 | |
| 246 | if (defined('ELEMENTOR_VERSION')) { |
| 247 | $sidebar_args[] = [ |
| 248 | 'section_title' => esc_html__('Elementor Restriction', 'wp-user-avatar'), |
| 249 | 'content' => (function () { |
| 250 | ob_start(); |
| 251 | require dirname(__FILE__) . '/views/include.elementor-protection.php'; |
| 252 | |
| 253 | return ob_get_clean(); |
| 254 | })() |
| 255 | ]; |
| 256 | } |
| 257 | |
| 258 | $instance->sidebar($sidebar_args); |
| 259 | } |
| 260 | |
| 261 | $instance->build(! $is_sidebar); |
| 262 | } |
| 263 | |
| 264 | public function add_new_button() |
| 265 | { |
| 266 | if ( ! isset($_GET['add']) && ! isset($_GET['action'])) { |
| 267 | $url = esc_url_raw(add_query_arg('add', 'new', PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE)); |
| 268 | echo "<a class=\"add-new-h2\" href=\"$url\">" . esc_html__('Add a Protection Rule', 'wp-user-avatar') . '</a>'; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | public function admin_settings_page_callback() |
| 273 | { |
| 274 | $this->myListTable->prepare_items(); // has to be here. |
| 275 | |
| 276 | if (isset($_GET['add']) || isset($_GET['action'])) { |
| 277 | $this->admin_notices(); |
| 278 | require_once dirname(__FILE__) . '/views/include.view.php'; |
| 279 | |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | echo '<form method="post">'; |
| 284 | $this->myListTable->display(); |
| 285 | echo '</form>'; |
| 286 | |
| 287 | do_action('ppress_content_protection_wp_list_table_bottom'); |
| 288 | } |
| 289 | |
| 290 | public static function get_instance() |
| 291 | { |
| 292 | static $instance = null; |
| 293 | |
| 294 | if (is_null($instance)) { |
| 295 | $instance = new self(); |
| 296 | } |
| 297 | |
| 298 | return $instance; |
| 299 | } |
| 300 | } |