| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
class PS_Admin |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Contains instance or null |
| 9 |
* |
| 10 |
* @var object|null |
| 11 |
*/ |
| 12 |
private static $instance = null ; |
| 13 |
/** |
| 14 |
* Returns instance of PS_Admin. |
| 15 |
* |
| 16 |
* @return object |
| 17 |
*/ |
| 18 |
public static function get_instance() |
| 19 |
{ |
| 20 |
if ( null === self::$instance ) { |
| 21 |
self::$instance = new self(); |
| 22 |
} |
| 23 |
return self::$instance; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Setup the passster admin area |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function __construct() |
| 32 |
{ |
| 33 |
// Check permissions before include settings. |
| 34 |
if ( !current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) ) ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_scripts' ) ); |
| 38 |
add_action( 'init', array( $this, 'register_password_areas' ) ); |
| 39 |
add_filter( 'manage_protected_areas_posts_columns', array( $this, 'set_area_columns' ) ); |
| 40 |
add_action( |
| 41 |
'manage_protected_areas_posts_custom_column', |
| 42 |
array( $this, 'set_area_columns_content' ), |
| 43 |
10, |
| 44 |
2 |
| 45 |
); |
| 46 |
$settings = new PS_Settings(); |
| 47 |
$settings->add_section( array( |
| 48 |
'id' => 'passster_general_settings', |
| 49 |
'title' => __( 'Options', 'content-protector' ), |
| 50 |
) ); |
| 51 |
$settings->add_field( 'passster_general_settings', array( |
| 52 |
'id' => 'passster_advanced_cookie_title', |
| 53 |
'type' => 'title', |
| 54 |
'name' => '<h3>' . __( 'Cookie', 'content-protector' ) . '</h3>', |
| 55 |
) ); |
| 56 |
$settings->add_field( 'passster_general_settings', array( |
| 57 |
'id' => 'toggle_cookie', |
| 58 |
'type' => 'toggle', |
| 59 |
'default' => 'on', |
| 60 |
'name' => __( 'Use Cookie', 'content-protector' ), |
| 61 |
) ); |
| 62 |
$settings->add_field( 'passster_general_settings', array( |
| 63 |
'id' => 'passster_cookie_duration', |
| 64 |
'type' => 'number', |
| 65 |
'name' => __( 'Cookie Duration', 'content-protector' ), |
| 66 |
'desc' => __( 'Duration (in days) for your cookie. Once a cookie expires, the user will have to enter the password again.', 'content-protector' ), |
| 67 |
'default' => '2', |
| 68 |
'min' => 1, |
| 69 |
) ); |
| 70 |
$settings->add_field( 'passster_general_settings', array( |
| 71 |
'id' => 'passster_advanced_compatibility_mode', |
| 72 |
'type' => 'title', |
| 73 |
'name' => '<h3>' . __( 'Compatibility Mode', 'content-protector' ) . '</h3>', |
| 74 |
) ); |
| 75 |
$settings->add_field( 'passster_general_settings', array( |
| 76 |
'id' => 'toggle_ajax', |
| 77 |
'type' => 'toggle', |
| 78 |
'default' => 'off', |
| 79 |
'name' => __( 'Reload after successful validation', 'content-protector' ), |
| 80 |
) ); |
| 81 |
$settings->add_field( 'passster_general_settings', array( |
| 82 |
'id' => 'passster_advanced_third_party_title', |
| 83 |
'type' => 'title', |
| 84 |
'name' => '<h3>' . __( 'Third-Party Support', 'content-protector' ) . '</h3>', |
| 85 |
) ); |
| 86 |
$settings->add_field( 'passster_general_settings', array( |
| 87 |
'id' => 'third_party_shortcodes', |
| 88 |
'type' => 'textarea', |
| 89 |
'name' => __( 'Third-Party Shortcodes', 'content-protector' ), |
| 90 |
'desc' => __( 'Add a comma separated list of shortcodes you want to use inside of Passster. <br>You can also use <b>{post-id}</b> to add the ID of the current page/post dynamically. ', 'content-protector' ), |
| 91 |
) ); |
| 92 |
$settings->add_field( 'passster_general_settings', array( |
| 93 |
'id' => 'passster_advanced_amp_title', |
| 94 |
'type' => 'title', |
| 95 |
'name' => '<h3>' . __( 'AMP', 'content-protector' ) . '</h3>', |
| 96 |
) ); |
| 97 |
$settings->add_field( 'passster_general_settings', array( |
| 98 |
'id' => 'toggle_amp', |
| 99 |
'type' => 'toggle', |
| 100 |
'default' => 'off', |
| 101 |
'name' => __( 'Activate AMP Support', 'content-protector' ), |
| 102 |
) ); |
| 103 |
$settings->add_field( 'passster_general_settings', array( |
| 104 |
'id' => 'passster_advanced_delete_title', |
| 105 |
'type' => 'title', |
| 106 |
'name' => '<h3>' . __( 'Uninstall', 'content-protector' ) . '</h3>', |
| 107 |
) ); |
| 108 |
$settings->add_field( 'passster_general_settings', array( |
| 109 |
'id' => 'passster_advanced_delete_options', |
| 110 |
'type' => 'checkbox', |
| 111 |
'name' => __( 'Delete Plugin Options On Uninstall', 'content-protector' ), |
| 112 |
'desc' => __( 'If checked, all plugin options will be deleted if the plugin is unstalled.', 'content-protector' ), |
| 113 |
) ); |
| 114 |
$settings->add_section( array( |
| 115 |
'id' => 'passster_advanced_settings', |
| 116 |
'title' => __( 'External Services', 'content-protector' ), |
| 117 |
) ); |
| 118 |
$settings->add_field( 'passster_advanced_settings', array( |
| 119 |
'id' => 'passster_recaptcha_title', |
| 120 |
'type' => 'title', |
| 121 |
'name' => '<h3>' . __( 'Google Recaptcha', 'content-protector' ) . '</h3>', |
| 122 |
) ); |
| 123 |
$settings->add_field( 'passster_advanced_settings', array( |
| 124 |
'id' => 'passster_recaptcha_type', |
| 125 |
'type' => 'select', |
| 126 |
'name' => __( 'Recaptcha Version', 'content-protector' ), |
| 127 |
'options' => array( |
| 128 |
'v3' => 'V3 (invisible Captcha)', |
| 129 |
'v2' => 'V2 (Checkbox)', |
| 130 |
), |
| 131 |
) ); |
| 132 |
$settings->add_field( 'passster_advanced_settings', array( |
| 133 |
'id' => 'passster_recaptcha_site_key', |
| 134 |
'type' => 'text', |
| 135 |
'name' => __( 'Site Key', 'content-protector' ), |
| 136 |
'desc' => __( 'Add your Google ReCaptcha Site Key', 'content-protector' ), |
| 137 |
'premium' => 'premium', |
| 138 |
) ); |
| 139 |
$settings->add_field( 'passster_advanced_settings', array( |
| 140 |
'id' => 'passster_recaptcha_secret', |
| 141 |
'type' => 'text', |
| 142 |
'name' => __( 'Secret', 'content-protector' ), |
| 143 |
'desc' => __( 'Add your Google ReCaptcha Secret', 'content-protector' ), |
| 144 |
'premium' => 'premium', |
| 145 |
) ); |
| 146 |
$settings->add_field( 'passster_advanced_settings', array( |
| 147 |
'id' => 'passster_recaptcha_language', |
| 148 |
'type' => 'text', |
| 149 |
'name' => __( 'Language', 'content-protector' ), |
| 150 |
'desc' => __( 'Add your language shortcode. For example "en" for english or "de" for german. ', 'content-protector' ), |
| 151 |
'default' => 'en', |
| 152 |
'premium' => 'premium', |
| 153 |
) ); |
| 154 |
$settings->add_field( 'passster_advanced_settings', array( |
| 155 |
'id' => 'passster_bitly_title', |
| 156 |
'type' => 'title', |
| 157 |
'name' => '<h3>' . __( 'Bitly', 'content-protector' ) . '</h3>', |
| 158 |
) ); |
| 159 |
$settings->add_field( 'passster_advanced_settings', array( |
| 160 |
'id' => 'passster_bitly_access_key', |
| 161 |
'type' => 'text', |
| 162 |
'name' => __( 'Bitly Access Token', 'content-protector' ), |
| 163 |
'desc' => __( 'Add your bitly access token. You can get one here: <a target="_blank" href="https://bitly.com/">bitly.com</a>', 'content-protector' ), |
| 164 |
'premium' => 'premium', |
| 165 |
) ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Add admin assets |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
public function add_admin_scripts() |
| 174 |
{ |
| 175 |
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ); |
| 176 |
wp_enqueue_style( |
| 177 |
'passster-admin-css', |
| 178 |
PASSSTER_URL . '/assets/admin/passster-admin.css', |
| 179 |
PASSSTER_VERSION, |
| 180 |
'all' |
| 181 |
); |
| 182 |
wp_enqueue_script( |
| 183 |
'passster-admin-js', |
| 184 |
PASSSTER_URL . '/assets/admin/passster-admin' . $suffix . '.js', |
| 185 |
array( 'jquery' ), |
| 186 |
PASSSTER_VERSION, |
| 187 |
false |
| 188 |
); |
| 189 |
wp_localize_script( 'passster-admin-js', 'ps_admin_ajax', array( |
| 190 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 191 |
'nonce' => wp_create_nonce( 'passster-admin-nonce' ), |
| 192 |
'reset_message' => __( 'Usage data resetted.', 'content-protector' ), |
| 193 |
'reset_concurrent_message' => __( 'Concurrent data resetted.', 'content-protector' ), |
| 194 |
) ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Register post type "protected areas" |
| 199 |
* |
| 200 |
* @return void |
| 201 |
*/ |
| 202 |
public function register_password_areas() |
| 203 |
{ |
| 204 |
$labels = array( |
| 205 |
'name' => _x( 'Protected Areas', 'post type general name', 'content-protector' ), |
| 206 |
'singular_name' => _x( 'Protected Area', 'post type singular name', 'content-protector' ), |
| 207 |
'menu_name' => _x( 'Protected Areas', 'admin menu', 'content-protector' ), |
| 208 |
'name_admin_bar' => _x( 'Protected Area', 'add new on admin bar', 'content-protector' ), |
| 209 |
'add_new' => _x( 'Add New', 'content-protector' ), |
| 210 |
'add_new_item' => __( 'Add New Protected Area', 'content-protector' ), |
| 211 |
'new_item' => __( 'New Protected Area', 'content-protector' ), |
| 212 |
'edit_item' => __( 'Edit Protected Area', 'content-protector' ), |
| 213 |
'view_item' => __( 'View Protected Area', 'content-protector' ), |
| 214 |
'all_items' => __( 'Protected Areas', 'content-protector' ), |
| 215 |
'search_items' => __( 'Search Protected Areas', 'content-protector' ), |
| 216 |
'parent_item_colon' => __( 'Parent Protected Area', 'content-protector' ), |
| 217 |
'not_found' => __( 'No Protected Areas found.', 'content-protector' ), |
| 218 |
'not_found_in_trash' => __( 'No Protected Areas found in Trash.', 'content-protector' ), |
| 219 |
); |
| 220 |
$args = array( |
| 221 |
'labels' => $labels, |
| 222 |
'description' => __( 'Manageable protected areas', 'content-protector' ), |
| 223 |
'public' => false, |
| 224 |
'publicly_queryable' => false, |
| 225 |
'show_ui' => true, |
| 226 |
'show_in_menu' => 'passster', |
| 227 |
'query_var' => true, |
| 228 |
'rewrite' => false, |
| 229 |
'capability_type' => 'post', |
| 230 |
'has_archive' => false, |
| 231 |
'hierarchical' => false, |
| 232 |
'menu_position' => null, |
| 233 |
'supports' => array( 'title', 'editor' ), |
| 234 |
'show_in_rest' => true, |
| 235 |
); |
| 236 |
|
| 237 |
if ( current_user_can( 'administrator' ) && (is_plugin_active( 'elementor/elementor.php' ) || is_plugin_active( 'divi-builder/divi-builder.php' )) ) { |
| 238 |
$args['public'] = true; |
| 239 |
$args['publicly_queryable'] = true; |
| 240 |
} |
| 241 |
|
| 242 |
$theme = wp_get_theme(); |
| 243 |
|
| 244 |
if ( current_user_can( 'administrator' ) && 'Divi' == $theme->name || 'Divi' == $theme->parent_theme ) { |
| 245 |
$args['public'] = true; |
| 246 |
$args['publicly_queryable'] = true; |
| 247 |
} |
| 248 |
|
| 249 |
register_post_type( 'protected_areas', $args ); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Set column headers password lists post type |
| 254 |
* |
| 255 |
* @param array $columns array of columns. |
| 256 |
* |
| 257 |
* @return array |
| 258 |
*/ |
| 259 |
public function set_area_columns( array $columns ) : array |
| 260 |
{ |
| 261 |
$columns['shortcode'] = __( 'Shortcode', 'content-protector' ); |
| 262 |
unset( $columns['date'] ); |
| 263 |
return $columns; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Add content to registered columns for password list post type. |
| 268 |
* |
| 269 |
* @param string $column name of the column. |
| 270 |
* @param int $post_id current id. |
| 271 |
* |
| 272 |
* @return void |
| 273 |
*/ |
| 274 |
public function set_area_columns_content( string $column, int $post_id ) |
| 275 |
{ |
| 276 |
switch ( $column ) { |
| 277 |
case 'shortcode': |
| 278 |
$shortcode = get_post_meta( $post_id, 'passster_area_shortcode', true ); |
| 279 |
echo esc_html( $shortcode ) ; |
| 280 |
break; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Return default based on option name. |
| 286 |
* |
| 287 |
* @param string $option_name name of the option. |
| 288 |
* |
| 289 |
* @return array |
| 290 |
*/ |
| 291 |
public static function get_defaults( string $option_name ) : array |
| 292 |
{ |
| 293 |
switch ( $option_name ) { |
| 294 |
case 'passster_general_settings': |
| 295 |
return array( |
| 296 |
'toggle_cookie' => 'on', |
| 297 |
'toggle_ajax' => 'on', |
| 298 |
'passster_cookie_duration' => 2, |
| 299 |
'toggle_amp' => 'off', |
| 300 |
'passster_advanced_delete_options' => 'off', |
| 301 |
'passster_activate_concurrent' => 'off', |
| 302 |
'passster_number_of_concurrent_logins' => 1, |
| 303 |
); |
| 304 |
break; |
| 305 |
case 'passster_advanced_settings': |
| 306 |
return array( |
| 307 |
'passster_recaptcha_site_key' => '', |
| 308 |
'passster_recaptcha_type' => 'v3', |
| 309 |
'passster_recaptcha_secret' => '', |
| 310 |
'passster_recaptcha_language' => '', |
| 311 |
'passster_bitly_access_key' => '', |
| 312 |
); |
| 313 |
break; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
} |