Frontend
5 years ago
views
5 years ago
ConditionCallbacks.php
5 years ago
ContentConditions.php
5 years ago
Init.php
5 years ago
SettingsPage.php
5 years ago
WPListTable.php
5 years ago
index.php
5 years ago
SettingsPage.php
196 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('admin_menu', array($this, 'register_cpf_settings_page')); |
| 21 | add_filter('set-screen-option', [__CLASS__, 'set_screen'], 10, 3); |
| 22 | add_filter('set_screen_option_rules_per_page', [__CLASS__, 'set_screen'], 10, 3); |
| 23 | } |
| 24 | |
| 25 | public function admin_page_title() |
| 26 | { |
| 27 | $title = esc_html__('Content Protection', 'wp-user-avatar'); |
| 28 | |
| 29 | if (isset($_GET['action']) && $_GET['action'] == 'edit') { |
| 30 | $title = esc_html__('Edit Protection Rule', 'wp-user-avatar'); |
| 31 | } |
| 32 | |
| 33 | if (isset($_GET['add']) && $_GET['add'] == 'new') { |
| 34 | $title = esc_html__('Add Protection Rule', 'wp-user-avatar'); |
| 35 | } |
| 36 | |
| 37 | return $title; |
| 38 | } |
| 39 | |
| 40 | public function register_cpf_settings_page() |
| 41 | { |
| 42 | $hook = add_submenu_page( |
| 43 | PPRESS_SETTINGS_SLUG, |
| 44 | $this->admin_page_title() . ' - ProfilePress', |
| 45 | esc_html__('Content Protection', 'wp-user-avatar'), |
| 46 | 'manage_options', |
| 47 | PPRESS_CONTENT_PROTECTION_SETTINGS_SLUG, |
| 48 | array($this, 'settings_page_function')); |
| 49 | |
| 50 | add_action("load-$hook", array($this, 'add_options')); |
| 51 | } |
| 52 | |
| 53 | public static function set_screen($status, $option, $value) |
| 54 | { |
| 55 | return $value; |
| 56 | } |
| 57 | |
| 58 | public function add_options() |
| 59 | { |
| 60 | $args = [ |
| 61 | 'label' => esc_html__('Protection Rules', 'wp-user-avatar'), |
| 62 | 'default' => 10, |
| 63 | 'option' => 'rules_per_page' |
| 64 | ]; |
| 65 | |
| 66 | add_screen_option('per_page', $args); |
| 67 | |
| 68 | $this->myListTable = new WPListTable(); |
| 69 | } |
| 70 | |
| 71 | public function sanitize_data($data) |
| 72 | { |
| 73 | if (is_string($data)) { |
| 74 | return sanitize_text_field($data); |
| 75 | } |
| 76 | |
| 77 | $sanitized_data = []; |
| 78 | |
| 79 | foreach ($data as $key => $value) { |
| 80 | |
| 81 | if (is_array($data[$key])) { |
| 82 | $sanitized_data[$key] = self::sanitize_data($data[$key]); |
| 83 | } else { |
| 84 | $sanitized_data[$key] = sanitize_text_field($data[$key]); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return $sanitized_data; |
| 89 | } |
| 90 | |
| 91 | public function save_rule($type) |
| 92 | { |
| 93 | if ( ! isset($_POST['ppress_save_rule'])) return; |
| 94 | |
| 95 | check_admin_referer('wp-csa-nonce', 'wp_csa_nonce'); |
| 96 | |
| 97 | if ( ! current_user_can('manage_options')) return; |
| 98 | |
| 99 | if (empty($_POST['ppress_cc_data']['title'])) { |
| 100 | return $this->content_protection_rule_errors = esc_html__('Title cannot be empty.', 'wp-user-avatar'); |
| 101 | } |
| 102 | |
| 103 | if ('add' == $type) { |
| 104 | $id = PROFILEPRESS_sql::add_meta_data(self::META_DATA_KEY, $this->sanitize_data($_POST['ppress_cc_data'])); |
| 105 | |
| 106 | if (is_int($id)) { |
| 107 | wp_safe_redirect(add_query_arg(['action' => 'edit', 'id' => $id, 'rule-updated' => 'true'], PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE)); |
| 108 | exit; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if ('edit' == $type) { |
| 113 | $rule_id = absint($_GET['id']); |
| 114 | $response = PROFILEPRESS_sql::update_meta_value($rule_id, self::META_DATA_KEY, $this->sanitize_data($_POST['ppress_cc_data'])); |
| 115 | |
| 116 | if (false !== $response) { |
| 117 | wp_safe_redirect(add_query_arg(['action' => 'edit', 'id' => $rule_id, 'rule-updated' => 'true'], PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE)); |
| 118 | exit; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public function admin_notices() |
| 124 | { |
| 125 | if ( ! isset($_GET['rule-updated']) && ! isset($this->content_protection_rule_errors)) return; |
| 126 | |
| 127 | $status = 'updated'; |
| 128 | if (isset($this->content_protection_rule_errors)) { |
| 129 | $message = $this->content_protection_rule_errors; |
| 130 | $status = 'error'; |
| 131 | } |
| 132 | |
| 133 | if (isset($_GET['rule-updated'])) { |
| 134 | $message = esc_html__('Changes saved.', 'wp-user-avatar'); |
| 135 | } |
| 136 | |
| 137 | printf('<div id="message" class="%s notice is-dismissible"><p>%s</strong></p></div>', $status, $message); |
| 138 | } |
| 139 | |
| 140 | public function settings_page_function() |
| 141 | { |
| 142 | add_action('wp_cspa_main_content_area', array($this, 'admin_page_callback'), 10, 2); |
| 143 | add_action('wp_cspa_before_closing_header', [$this, 'add_new_button']); |
| 144 | |
| 145 | $instance = Custom_Settings_Page_Api::instance(); |
| 146 | $instance->option_name('ppview'); // adds ppview css class to #poststuff |
| 147 | $instance->page_header($this->admin_page_title()); |
| 148 | $this->register_core_settings($instance, true); |
| 149 | $instance->build(true); |
| 150 | } |
| 151 | |
| 152 | public function add_new_button() |
| 153 | { |
| 154 | if ( ! isset($_GET['add']) && ! isset($_GET['action'])) { |
| 155 | $url = esc_url_raw(add_query_arg('add', 'new', PPRESS_CONTENT_PROTECTION_SETTINGS_PAGE)); |
| 156 | echo "<a class=\"add-new-h2\" href=\"$url\">" . esc_html__('Add a Protection Rule', 'wp-user-avatar') . '</a>'; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public function admin_page_callback() |
| 161 | { |
| 162 | $this->myListTable->prepare_items(); // has to be here. |
| 163 | |
| 164 | if (isset($_GET['add']) && $_GET['add'] == 'new') { |
| 165 | $this->save_rule('add'); |
| 166 | } |
| 167 | |
| 168 | if (isset($_GET['action']) && $_GET['action'] == 'edit') { |
| 169 | $this->save_rule('edit'); |
| 170 | } |
| 171 | |
| 172 | if (isset($_GET['add']) || isset($_GET['action'])) { |
| 173 | $this->admin_notices(); |
| 174 | require_once dirname(__FILE__) . '/views/include.view.php'; |
| 175 | |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | echo '<form method="post">'; |
| 180 | $this->myListTable->display(); |
| 181 | echo '</form>'; |
| 182 | |
| 183 | do_action('ppress_content_protection_wp_list_table_bottom'); |
| 184 | } |
| 185 | |
| 186 | public static function get_instance() |
| 187 | { |
| 188 | static $instance = null; |
| 189 | |
| 190 | if (is_null($instance)) { |
| 191 | $instance = new self(); |
| 192 | } |
| 193 | |
| 194 | return $instance; |
| 195 | } |
| 196 | } |