| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
|
| 6 |
if ( !class_exists( 'PS_Settings' ) ) { |
| 7 |
class PS_Settings |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Sections array. |
| 11 |
* |
| 12 |
* @var array |
| 13 |
* @since 1.0.0 |
| 14 |
*/ |
| 15 |
private $sections_array = array() ; |
| 16 |
/** |
| 17 |
* Fields array. |
| 18 |
* |
| 19 |
* @var array |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
private $fields_array = array() ; |
| 23 |
/** |
| 24 |
* Constructor. |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
*/ |
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
// Enqueue the admin scripts. |
| 31 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); |
| 32 |
// Hook it up. |
| 33 |
add_action( 'admin_init', array( $this, 'admin_init' ) ); |
| 34 |
// Menu. |
| 35 |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Admin Scripts. |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
public function admin_scripts() |
| 44 |
{ |
| 45 |
// jQuery is needed. |
| 46 |
wp_enqueue_script( 'jquery' ); |
| 47 |
// Color Picker. |
| 48 |
wp_enqueue_script( |
| 49 |
'iris', |
| 50 |
admin_url( 'js/iris.min.js' ), |
| 51 |
array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), |
| 52 |
false, |
| 53 |
1 |
| 54 |
); |
| 55 |
// Media Uploader. |
| 56 |
wp_enqueue_media(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Set Sections. |
| 61 |
* |
| 62 |
* @param array $sections |
| 63 |
* |
| 64 |
* @since 1.0.0 |
| 65 |
*/ |
| 66 |
public function set_sections( $sections ) |
| 67 |
{ |
| 68 |
// Bail if not array. |
| 69 |
if ( !is_array( $sections ) ) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
// Assign to the sections array. |
| 73 |
$this->sections_array = $sections; |
| 74 |
return $this; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Add a single section. |
| 79 |
* |
| 80 |
* @param array $section |
| 81 |
* |
| 82 |
* @since 1.0.0 |
| 83 |
*/ |
| 84 |
public function add_section( $section ) |
| 85 |
{ |
| 86 |
// Bail if not array. |
| 87 |
if ( !is_array( $section ) ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
// Assign the section to sections array. |
| 91 |
$this->sections_array[] = $section; |
| 92 |
return $this; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Set Fields. |
| 97 |
* |
| 98 |
* @since 1.0.0 |
| 99 |
*/ |
| 100 |
public function set_fields( $fields ) |
| 101 |
{ |
| 102 |
// Bail if not array. |
| 103 |
if ( !is_array( $fields ) ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
// Assign the fields. |
| 107 |
$this->fields_array = $fields; |
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Add a single field. |
| 113 |
* |
| 114 |
* @since 1.0.0 |
| 115 |
*/ |
| 116 |
public function add_field( $section, $field_array ) |
| 117 |
{ |
| 118 |
// Set the defaults |
| 119 |
$defaults = array( |
| 120 |
'id' => '', |
| 121 |
'name' => '', |
| 122 |
'desc' => '', |
| 123 |
'type' => 'text', |
| 124 |
); |
| 125 |
// Combine the defaults with user's arguements. |
| 126 |
$arg = wp_parse_args( $field_array, $defaults ); |
| 127 |
// Each field is an array named against its section. |
| 128 |
$this->fields_array[$section][] = $arg; |
| 129 |
return $this; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Initialize API. |
| 134 |
* |
| 135 |
* Initializes and registers the settings sections and fields. |
| 136 |
* Usually this should be called at `admin_init` hook. |
| 137 |
* |
| 138 |
* @since 1.0.0 |
| 139 |
*/ |
| 140 |
function admin_init() |
| 141 |
{ |
| 142 |
/** |
| 143 |
* Register the sections. |
| 144 |
* |
| 145 |
* Sections array is like this: |
| 146 |
* |
| 147 |
* $sections_array = array ( |
| 148 |
* $section_array, |
| 149 |
* $section_array, |
| 150 |
* $section_array, |
| 151 |
* ); |
| 152 |
* |
| 153 |
* Section array is like this: |
| 154 |
* |
| 155 |
* $section_array = array ( |
| 156 |
* 'id' => 'section_id', |
| 157 |
* 'title' => 'Section Title' |
| 158 |
* ); |
| 159 |
* |
| 160 |
* @since 1.0.0 |
| 161 |
*/ |
| 162 |
foreach ( $this->sections_array as $section ) { |
| 163 |
if ( false == get_option( $section['id'] ) ) { |
| 164 |
// Add a new field as section ID. |
| 165 |
add_option( $section['id'] ); |
| 166 |
} |
| 167 |
// Deals with sections description. |
| 168 |
|
| 169 |
if ( isset( $section['desc'] ) && !empty($section['desc']) ) { |
| 170 |
// Build HTML. |
| 171 |
$section['desc'] = '<div class="inside">' . $section['desc'] . '</div>'; |
| 172 |
// Create the callback for description. |
| 173 |
$callback = function () use( $section ) { |
| 174 |
echo str_replace( '"', '\\"', $section['desc'] ) ; |
| 175 |
}; |
| 176 |
} elseif ( isset( $section['callback'] ) ) { |
| 177 |
$callback = $section['callback']; |
| 178 |
} else { |
| 179 |
$callback = null; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Add a new section to a settings page. |
| 184 |
* |
| 185 |
* @param string $id |
| 186 |
* @param string $title |
| 187 |
* @param callable $callback |
| 188 |
* @param string $page | Page is same as section ID. |
| 189 |
* |
| 190 |
* @since 1.0.0 |
| 191 |
*/ |
| 192 |
add_settings_section( |
| 193 |
$section['id'], |
| 194 |
$section['title'], |
| 195 |
$callback, |
| 196 |
$section['id'] |
| 197 |
); |
| 198 |
} |
| 199 |
// foreach ended. |
| 200 |
/** |
| 201 |
* Register settings fields. |
| 202 |
* |
| 203 |
* Fields array is like this: |
| 204 |
* |
| 205 |
* $fields_array = array ( |
| 206 |
* $section => $field_array, |
| 207 |
* $section => $field_array, |
| 208 |
* $section => $field_array, |
| 209 |
* ); |
| 210 |
* |
| 211 |
* |
| 212 |
* Field array is like this: |
| 213 |
* |
| 214 |
* $field_array = array ( |
| 215 |
* 'id' => 'id', |
| 216 |
* 'name' => 'Name', |
| 217 |
* 'type' => 'text', |
| 218 |
* ); |
| 219 |
* |
| 220 |
* @since 1.0.0 |
| 221 |
*/ |
| 222 |
foreach ( $this->fields_array as $section => $field_array ) { |
| 223 |
foreach ( $field_array as $field ) { |
| 224 |
// ID. |
| 225 |
$id = ( isset( $field['id'] ) ? $field['id'] : false ); |
| 226 |
// Type. |
| 227 |
$type = ( isset( $field['type'] ) ? $field['type'] : 'text' ); |
| 228 |
// Name. |
| 229 |
$name = ( isset( $field['name'] ) ? $field['name'] : 'No Name Added' ); |
| 230 |
// Label for. |
| 231 |
$label_for = "{$section}[{$field['id']}]"; |
| 232 |
// Description. |
| 233 |
$description = ( isset( $field['desc'] ) ? $field['desc'] : '' ); |
| 234 |
// Premium. |
| 235 |
$premium = ( isset( $field['premium'] ) ? $field['premium'] : '' ); |
| 236 |
// Min and max. |
| 237 |
$min = ( isset( $field['min'] ) ? $field['min'] : '' ); |
| 238 |
$max = ( isset( $field['max'] ) ? $field['max'] : '' ); |
| 239 |
// Size. |
| 240 |
$size = ( isset( $field['size'] ) ? $field['size'] : null ); |
| 241 |
// Options. |
| 242 |
$options = ( isset( $field['options'] ) ? $field['options'] : '' ); |
| 243 |
// Standard default value. |
| 244 |
$default = ( isset( $field['default'] ) ? $field['default'] : '' ); |
| 245 |
// Standard default placeholder. |
| 246 |
$placeholder = ( isset( $field['placeholder'] ) ? $field['placeholder'] : '' ); |
| 247 |
// Sanitize Callback. |
| 248 |
$sanitize_callback = ( isset( $field['sanitize_callback'] ) ? $field['sanitize_callback'] : '' ); |
| 249 |
$args = array( |
| 250 |
'id' => $id, |
| 251 |
'type' => $type, |
| 252 |
'name' => $name, |
| 253 |
'label_for' => $label_for, |
| 254 |
'desc' => $description, |
| 255 |
'premium' => $premium, |
| 256 |
'min' => $min, |
| 257 |
'max' => $max, |
| 258 |
'section' => $section, |
| 259 |
'size' => $size, |
| 260 |
'options' => $options, |
| 261 |
'std' => $default, |
| 262 |
'placeholder' => $placeholder, |
| 263 |
'sanitize_callback' => $sanitize_callback, |
| 264 |
); |
| 265 |
/** |
| 266 |
* Add a new field to a section of a settings page. |
| 267 |
* |
| 268 |
* @param string $id |
| 269 |
* @param string $title |
| 270 |
* @param callable $callback |
| 271 |
* @param string $page |
| 272 |
* @param string $section = 'default' |
| 273 |
* @param array $args = array() |
| 274 |
* |
| 275 |
* @since 1.0.0 |
| 276 |
*/ |
| 277 |
// @param string $id |
| 278 |
$field_id = $section . '[' . $field['id'] . ']'; |
| 279 |
add_settings_field( |
| 280 |
$field_id, |
| 281 |
$name, |
| 282 |
array( $this, 'callback_' . $type ), |
| 283 |
$section, |
| 284 |
$section, |
| 285 |
$args |
| 286 |
); |
| 287 |
} |
| 288 |
// foreach ended. |
| 289 |
} |
| 290 |
// foreach ended. |
| 291 |
// Creates our settings in the fields table. |
| 292 |
foreach ( $this->sections_array as $section ) { |
| 293 |
/** |
| 294 |
* Registers a setting and its sanitization callback. |
| 295 |
* |
| 296 |
* @param string $field_group | A settings group name. |
| 297 |
* @param string $field_name | The name of an option to sanitize and save. |
| 298 |
* @param callable $sanitize_callback = '' |
| 299 |
* |
| 300 |
* @since 1.0.0 |
| 301 |
*/ |
| 302 |
register_setting( $section['id'], $section['id'], array( $this, 'sanitize_fields' ) ); |
| 303 |
} |
| 304 |
// foreach ended. |
| 305 |
} |
| 306 |
|
| 307 |
// admin_init() ended. |
| 308 |
/** |
| 309 |
* Sanitize callback for Settings API fields. |
| 310 |
* |
| 311 |
* @since 1.0.0 |
| 312 |
*/ |
| 313 |
public function sanitize_fields( $fields ) |
| 314 |
{ |
| 315 |
foreach ( $fields as $field_slug => $field_value ) { |
| 316 |
$sanitize_callback = $this->get_sanitize_callback( $field_slug ); |
| 317 |
// If callback is set, call it. |
| 318 |
|
| 319 |
if ( $sanitize_callback ) { |
| 320 |
$fields[$field_slug] = call_user_func( $sanitize_callback, $field_value ); |
| 321 |
continue; |
| 322 |
} |
| 323 |
|
| 324 |
} |
| 325 |
return $fields; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Get sanitization callback for given option slug |
| 330 |
* |
| 331 |
* @param string $slug option slug. |
| 332 |
* |
| 333 |
* @return mixed string | bool false |
| 334 |
* @since 1.0.0 |
| 335 |
*/ |
| 336 |
function get_sanitize_callback( $slug = '' ) |
| 337 |
{ |
| 338 |
if ( empty($slug) ) { |
| 339 |
return false; |
| 340 |
} |
| 341 |
// Iterate over registered fields and see if we can find proper callback. |
| 342 |
foreach ( $this->fields_array as $section => $field_array ) { |
| 343 |
foreach ( $field_array as $field ) { |
| 344 |
if ( $field['name'] != $slug ) { |
| 345 |
continue; |
| 346 |
} |
| 347 |
// Return the callback name. |
| 348 |
return ( isset( $field['sanitize_callback'] ) && is_callable( $field['sanitize_callback'] ) ? $field['sanitize_callback'] : false ); |
| 349 |
} |
| 350 |
} |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Get field description for display |
| 356 |
* |
| 357 |
* @param array $args settings field args |
| 358 |
*/ |
| 359 |
public function get_field_description( $args ) |
| 360 |
{ |
| 361 |
|
| 362 |
if ( !empty($args['desc']) ) { |
| 363 |
$desc = sprintf( '<p class="description">%s</p>', $args['desc'] ); |
| 364 |
} else { |
| 365 |
$desc = ''; |
| 366 |
} |
| 367 |
|
| 368 |
return $desc; |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Displays a title field for a settings field |
| 373 |
* |
| 374 |
* @param array $args settings field args |
| 375 |
*/ |
| 376 |
function callback_title( $args ) |
| 377 |
{ |
| 378 |
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); |
| 379 |
|
| 380 |
if ( '' !== $args['name'] ) { |
| 381 |
$name = $args['name']; |
| 382 |
} else { |
| 383 |
} |
| 384 |
|
| 385 |
$type = ( isset( $args['type'] ) ? $args['type'] : 'title' ); |
| 386 |
$html = ''; |
| 387 |
echo $html ; |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* Displays a documentation field for a settings field |
| 392 |
* |
| 393 |
* @param array $args settings field args |
| 394 |
*/ |
| 395 |
function callback_documentation( $args ) |
| 396 |
{ |
| 397 |
$value = esc_attr( $this->get_option( |
| 398 |
$args['id'], |
| 399 |
$args['section'], |
| 400 |
$args['std'], |
| 401 |
$args['desc'] |
| 402 |
) ); |
| 403 |
|
| 404 |
if ( '' !== $args['name'] ) { |
| 405 |
$name = $args['name']; |
| 406 |
} else { |
| 407 |
} |
| 408 |
|
| 409 |
if ( !empty($args['desc']) ) { |
| 410 |
$html = sprintf( '<p class="description">%s</p>', $args['desc'] ); |
| 411 |
} |
| 412 |
$type = ( isset( $args['type'] ) ? $args['type'] : 'documentation' ); |
| 413 |
echo $html ; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Displays a list table |
| 418 |
* |
| 419 |
* @param array $args settings field args |
| 420 |
*/ |
| 421 |
function callback_list_table( $args ) |
| 422 |
{ |
| 423 |
$list_table = new PS_List_Table(); |
| 424 |
$list_table->prepare_items(); |
| 425 |
?> |
| 426 |
|
| 427 |
<div class="wrap password-list-table"> |
| 428 |
<form id="password_list-filter" method="get"> |
| 429 |
<input type="hidden" name="page" value="<?php |
| 430 |
echo $_REQUEST['page'] ; |
| 431 |
?>"/> |
| 432 |
<?php |
| 433 |
$list_table->display(); |
| 434 |
?> |
| 435 |
<div class="alignright"> |
| 436 |
<span class="button button-primary action new-password-list"><?php |
| 437 |
_e( 'Add new Password List', 'content-protector' ); |
| 438 |
?></span> |
| 439 |
</div> |
| 440 |
</form> |
| 441 |
</div> |
| 442 |
<?php |
| 443 |
new PS_Password_Lists(); |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* Displays a documentation field for a settings field |
| 448 |
* |
| 449 |
* @param array $args settings field args |
| 450 |
*/ |
| 451 |
function callback_shortcode() |
| 452 |
{ |
| 453 |
$shortcode_options = get_option( 'passster_shortcode' ); |
| 454 |
|
| 455 |
if ( !empty($shortcode_options) ) { |
| 456 |
$shortcode = '[passster '; |
| 457 |
if ( is_array( $shortcode_options ) && array_key_exists( 'passster_shortcode_protection_type', $shortcode_options ) ) { |
| 458 |
|
| 459 |
if ( 'passwords' == $shortcode_options['passster_shortcode_protection_type'] ) { |
| 460 |
$shortcode .= 'passwords="' . $shortcode_options['passster_shortcode_passwords'] . '"'; |
| 461 |
} elseif ( 'recaptcha' == $shortcode_options['passster_shortcode_protection_type'] ) { |
| 462 |
$shortcode .= 'captcha="recaptcha"'; |
| 463 |
} elseif ( 'captcha' == $shortcode_options['passster_shortcode_protection_type'] ) { |
| 464 |
$shortcode .= 'captcha="captcha"'; |
| 465 |
} elseif ( 'password' == $shortcode_options['passster_shortcode_protection_type'] ) { |
| 466 |
$shortcode .= 'password="' . $shortcode_options['passster_shortcode_password'] . '"'; |
| 467 |
} |
| 468 |
|
| 469 |
} |
| 470 |
if ( isset( $shortcode_options['passster_shortcode_user_groups'] ) && !empty($shortcode_options['passster_shortcode_user_groups']) ) { |
| 471 |
$shortcode .= ' role="' . $shortcode_options['passster_shortcode_user_groups'] . '"'; |
| 472 |
} |
| 473 |
if ( isset( $shortcode_options['passster_shortcode_users'] ) && !empty($shortcode_options['passster_shortcode_users']) ) { |
| 474 |
$shortcode .= ' user="' . $shortcode_options['passster_shortcode_users'] . '"'; |
| 475 |
} |
| 476 |
|
| 477 |
if ( isset( $shortcode_options['passster_shortcode_select_password_list'] ) && !empty($shortcode_options['passster_shortcode_select_password_list']) ) { |
| 478 |
var_dump( $shortcode_options['passster_shortcode_select_password_list'] ); |
| 479 |
$shortcode .= ' password_list="' . $shortcode_options['passster_shortcode_select_password_list'] . '"'; |
| 480 |
} |
| 481 |
|
| 482 |
$shortcode .= ']Your content here[/passster]'; |
| 483 |
$html = '<code class="shortcode-copy">' . $shortcode . '</code>'; |
| 484 |
$html .= '<br><br><span class="copy" data-clipboard-target=".shortcode-copy">' . __( 'Copy Shortcode', 'content-protector' ) . '</button>'; |
| 485 |
echo $html ; |
| 486 |
} |
| 487 |
|
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Displays a documentation field for a settings field |
| 492 |
* |
| 493 |
* @param array $args settings field args |
| 494 |
*/ |
| 495 |
function callback_shortlink() |
| 496 |
{ |
| 497 |
$link_options = get_option( 'passster_link' ); |
| 498 |
|
| 499 |
if ( !empty($link_options) ) { |
| 500 |
$shortlink = $link_options['passster_shorten_link']; |
| 501 |
$html = '<code class="link-copy">' . $shortlink . '</code>'; |
| 502 |
$html .= '<span class="copy" data-clipboard-target=".link-copy">' . __( 'Copy Link', 'content-protector' ) . '</button>'; |
| 503 |
echo $html ; |
| 504 |
} |
| 505 |
|
| 506 |
} |
| 507 |
|
| 508 |
/** |
| 509 |
* Displays a text field for a settings field |
| 510 |
* |
| 511 |
* @param array $args settings field args |
| 512 |
*/ |
| 513 |
function callback_text( $args ) |
| 514 |
{ |
| 515 |
$value = esc_attr( $this->get_option( |
| 516 |
$args['id'], |
| 517 |
$args['section'], |
| 518 |
$args['std'], |
| 519 |
$args['placeholder'] |
| 520 |
) ); |
| 521 |
$size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular' ); |
| 522 |
$type = ( isset( $args['type'] ) ? $args['type'] : 'text' ); |
| 523 |
$html = sprintf( |
| 524 |
'<input type="%1$s" class="%2$s-text ' . $args['premium'] . '" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"placeholder="%6$s"/>', |
| 525 |
$type, |
| 526 |
$size, |
| 527 |
$args['section'], |
| 528 |
$args['id'], |
| 529 |
$value, |
| 530 |
$args['placeholder'] |
| 531 |
); |
| 532 |
$html .= $this->get_field_description( $args ); |
| 533 |
echo $html ; |
| 534 |
} |
| 535 |
|
| 536 |
/** |
| 537 |
* Displays a url field for a settings field |
| 538 |
* |
| 539 |
* @param array $args settings field args |
| 540 |
*/ |
| 541 |
function callback_url( $args ) |
| 542 |
{ |
| 543 |
$this->callback_text( $args ); |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* Displays a number field for a settings field |
| 548 |
* |
| 549 |
* @param array $args settings field args |
| 550 |
*/ |
| 551 |
function callback_number( $args ) |
| 552 |
{ |
| 553 |
$value = esc_attr( $this->get_option( |
| 554 |
$args['id'], |
| 555 |
$args['section'], |
| 556 |
$args['std'], |
| 557 |
$args['placeholder'] |
| 558 |
) ); |
| 559 |
$size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular' ); |
| 560 |
$type = ( isset( $args['type'] ) ? $args['type'] : 'number' ); |
| 561 |
$min = ( isset( $args['min'] ) ? 'min="' . $args['min'] . '"' : '' ); |
| 562 |
$max = ( isset( $args['max'] ) ? 'max="' . $args['max'] . '"' : '' ); |
| 563 |
$html = sprintf( |
| 564 |
'<input type="%1$s" class="%2$s-text ' . $args['premium'] . '" id="%3$s[%4$s]" name="%3$s[%4$s]" ' . $min . $max . ' value="%5$s"placeholder="%6$s"/>', |
| 565 |
$type, |
| 566 |
$size, |
| 567 |
$args['section'], |
| 568 |
$args['id'], |
| 569 |
$value, |
| 570 |
$args['placeholder'] |
| 571 |
); |
| 572 |
$html .= $this->get_field_description( $args ); |
| 573 |
echo $html ; |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 577 |
* Displays a checkbox for a settings field |
| 578 |
* |
| 579 |
* @param array $args settings field args |
| 580 |
*/ |
| 581 |
function callback_checkbox( $args ) |
| 582 |
{ |
| 583 |
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); |
| 584 |
$html = '<fieldset>'; |
| 585 |
$html .= sprintf( '<label for="wposa-%1$s[%2$s]">', $args['section'], $args['id'] ); |
| 586 |
$html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id'] ); |
| 587 |
$html .= sprintf( |
| 588 |
'<input type="checkbox" class="checkbox" id="wposa-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', |
| 589 |
$args['section'], |
| 590 |
$args['id'], |
| 591 |
checked( $value, 'on', false ) |
| 592 |
); |
| 593 |
$html .= sprintf( '%1$s</label>', $args['desc'] ); |
| 594 |
$html .= '</fieldset>'; |
| 595 |
echo $html ; |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Displays a multicheckbox a settings field |
| 600 |
* |
| 601 |
* @param array $args settings field args |
| 602 |
*/ |
| 603 |
function callback_multicheck( $args ) |
| 604 |
{ |
| 605 |
$value = $this->get_option( $args['id'], $args['section'], $args['std'] ); |
| 606 |
$html = '<fieldset>'; |
| 607 |
foreach ( $args['options'] as $key => $label ) { |
| 608 |
$checked = ( isset( $value[$key] ) ? $value[$key] : '0' ); |
| 609 |
$html .= sprintf( |
| 610 |
'<label for="wposa-%1$s[%2$s][%3$s]">', |
| 611 |
$args['section'], |
| 612 |
$args['id'], |
| 613 |
$key |
| 614 |
); |
| 615 |
$html .= sprintf( |
| 616 |
'<input type="checkbox" class="checkbox" id="wposa-%1$s[%2$s][%3$s]" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />', |
| 617 |
$args['section'], |
| 618 |
$args['id'], |
| 619 |
$key, |
| 620 |
checked( $checked, $key, false ) |
| 621 |
); |
| 622 |
$html .= sprintf( '%1$s</label><br>', $label ); |
| 623 |
} |
| 624 |
$html .= $this->get_field_description( $args ); |
| 625 |
$html .= '</fieldset>'; |
| 626 |
echo $html ; |
| 627 |
} |
| 628 |
|
| 629 |
/** |
| 630 |
* Displays a multicheckbox a settings field |
| 631 |
* |
| 632 |
* @param array $args settings field args |
| 633 |
*/ |
| 634 |
function callback_radio( $args ) |
| 635 |
{ |
| 636 |
$value = $this->get_option( $args['id'], $args['section'], $args['std'] ); |
| 637 |
$html = '<fieldset>'; |
| 638 |
foreach ( $args['options'] as $key => $label ) { |
| 639 |
$html .= sprintf( |
| 640 |
'<label for="wposa-%1$s[%2$s][%3$s]">', |
| 641 |
$args['section'], |
| 642 |
$args['id'], |
| 643 |
$key |
| 644 |
); |
| 645 |
$html .= sprintf( |
| 646 |
'<input type="radio" class="radio" id="wposa-%1$s[%2$s][%3$s]" name="%1$s[%2$s]" value="%3$s" %4$s />', |
| 647 |
$args['section'], |
| 648 |
$args['id'], |
| 649 |
$key, |
| 650 |
checked( $value, $key, false ) |
| 651 |
); |
| 652 |
$html .= sprintf( '%1$s</label><br>', $label ); |
| 653 |
} |
| 654 |
$html .= $this->get_field_description( $args ); |
| 655 |
$html .= '</fieldset>'; |
| 656 |
echo $html ; |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
* Displays a selectbox for a settings field |
| 661 |
* |
| 662 |
* @param array $args settings field args |
| 663 |
*/ |
| 664 |
function callback_select( $args ) |
| 665 |
{ |
| 666 |
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); |
| 667 |
$size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular' ); |
| 668 |
$html = sprintf( |
| 669 |
'<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">', |
| 670 |
$size, |
| 671 |
$args['section'], |
| 672 |
$args['id'] |
| 673 |
); |
| 674 |
foreach ( $args['options'] as $key => $label ) { |
| 675 |
$html .= sprintf( |
| 676 |
'<option value="%s"%s>%s</option>', |
| 677 |
$key, |
| 678 |
selected( $value, $key, false ), |
| 679 |
$label |
| 680 |
); |
| 681 |
} |
| 682 |
$html .= sprintf( '</select>' ); |
| 683 |
$html .= $this->get_field_description( $args ); |
| 684 |
echo $html ; |
| 685 |
} |
| 686 |
|
| 687 |
/** |
| 688 |
* Displays a textarea for a settings field |
| 689 |
* |
| 690 |
* @param array $args settings field args |
| 691 |
*/ |
| 692 |
function callback_textarea( $args ) |
| 693 |
{ |
| 694 |
$value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); |
| 695 |
$size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular' ); |
| 696 |
$html = sprintf( |
| 697 |
'<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]">%4$s</textarea>', |
| 698 |
$size, |
| 699 |
$args['section'], |
| 700 |
$args['id'], |
| 701 |
$value |
| 702 |
); |
| 703 |
$html .= $this->get_field_description( $args ); |
| 704 |
echo $html ; |
| 705 |
} |
| 706 |
|
| 707 |
/** |
| 708 |
* Displays a textarea for a settings field |
| 709 |
* |
| 710 |
* @param array $args settings field args. |
| 711 |
* |
| 712 |
* @return string |
| 713 |
*/ |
| 714 |
function callback_html( $args ) |
| 715 |
{ |
| 716 |
echo $this->get_field_description( $args ) ; |
| 717 |
} |
| 718 |
|
| 719 |
/** |
| 720 |
* Displays a rich text textarea for a settings field |
| 721 |
* |
| 722 |
* @param array $args settings field args. |
| 723 |
*/ |
| 724 |
function callback_wysiwyg( $args ) |
| 725 |
{ |
| 726 |
$value = $this->get_option( $args['id'], $args['section'], $args['std'] ); |
| 727 |
$size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : '500px' ); |
| 728 |
echo '<div style="max-width: ' . $size . ';">' ; |
| 729 |
$editor_settings = array( |
| 730 |
'teeny' => true, |
| 731 |
'textarea_name' => $args['section'] . '[' . $args['id'] . ']', |
| 732 |
'textarea_rows' => 10, |
| 733 |
); |
| 734 |
if ( isset( $args['options'] ) && is_array( $args['options'] ) ) { |
| 735 |
$editor_settings = array_merge( $editor_settings, $args['options'] ); |
| 736 |
} |
| 737 |
wp_editor( $value, $args['section'] . '-' . $args['id'], $editor_settings ); |
| 738 |
echo '</div>' ; |
| 739 |
echo $this->get_field_description( $args ) ; |
| 740 |
} |
| 741 |
|
| 742 |
/** |
| 743 |
* Displays a toggle for a settings field |
| 744 |
* |
| 745 |
* @param array $args settings field args |
| 746 |
*/ |
| 747 |
function callback_toggle( $args ) |
| 748 |
{ |
| 749 |
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); |
| 750 |
$html = '<fieldset>'; |
| 751 |
$html .= sprintf( '<label class="switch" for="wposa-%1$s[%2$s]">', $args['section'], $args['id'] ); |
| 752 |
$html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id'] ); |
| 753 |
$html .= sprintf( |
| 754 |
'<input type="checkbox" class="toggle-checkbox ' . $args['premium'] . '" id="wposa-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', |
| 755 |
$args['section'], |
| 756 |
$args['id'], |
| 757 |
checked( $value, 'on', false ) |
| 758 |
); |
| 759 |
$html .= '<span class="slider round"></span>'; |
| 760 |
$html .= '</label>'; |
| 761 |
$html .= '</fieldset>'; |
| 762 |
echo $html ; |
| 763 |
} |
| 764 |
|
| 765 |
/** |
| 766 |
* Displays a file upload field for a settings field |
| 767 |
* |
| 768 |
* @param array $args settings field args. |
| 769 |
*/ |
| 770 |
function callback_file( $args ) |
| 771 |
{ |
| 772 |
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); |
| 773 |
$size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular' ); |
| 774 |
$id = $args['section'] . '[' . $args['id'] . ']'; |
| 775 |
$label = ( isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : __( 'Choose File' ) ); |
| 776 |
$html = sprintf( |
| 777 |
'<input type="text" class="%1$s-text wpsa-url" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', |
| 778 |
$size, |
| 779 |
$args['section'], |
| 780 |
$args['id'], |
| 781 |
$value |
| 782 |
); |
| 783 |
$html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />'; |
| 784 |
$html .= $this->get_field_description( $args ); |
| 785 |
echo $html ; |
| 786 |
} |
| 787 |
|
| 788 |
/** |
| 789 |
* Displays an image upload field with a preview |
| 790 |
* |
| 791 |
* @param array $args settings field args. |
| 792 |
*/ |
| 793 |
function callback_image( $args ) |
| 794 |
{ |
| 795 |
$value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); |
| 796 |
$size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular' ); |
| 797 |
$id = $args['section'] . '[' . $args['id'] . ']'; |
| 798 |
$label = ( isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : __( 'Choose Image' ) ); |
| 799 |
$html = sprintf( |
| 800 |
'<input type="text" class="%1$s-text wpsa-url" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', |
| 801 |
$size, |
| 802 |
$args['section'], |
| 803 |
$args['id'], |
| 804 |
$value |
| 805 |
); |
| 806 |
$html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />'; |
| 807 |
$html .= $this->get_field_description( $args ); |
| 808 |
$html .= '<p class="wpsa-image-preview"><img src=""/></p>'; |
| 809 |
echo $html ; |
| 810 |
} |
| 811 |
|
| 812 |
/** |
| 813 |
* Displays a color picker field for a settings field |
| 814 |
* |
| 815 |
* @param array $args settings field args |
| 816 |
*/ |
| 817 |
function callback_color( $args ) |
| 818 |
{ |
| 819 |
$value = esc_attr( $this->get_option( |
| 820 |
$args['id'], |
| 821 |
$args['section'], |
| 822 |
$args['std'], |
| 823 |
$args['placeholder'] |
| 824 |
) ); |
| 825 |
$size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular' ); |
| 826 |
$html = sprintf( |
| 827 |
'<input type="text" class="%1$s-text color-picker" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s" data-default-color="%5$s" placeholder="%6$s" />', |
| 828 |
$size, |
| 829 |
$args['section'], |
| 830 |
$args['id'], |
| 831 |
$value, |
| 832 |
$args['std'], |
| 833 |
$args['placeholder'] |
| 834 |
); |
| 835 |
$html .= $this->get_field_description( $args ); |
| 836 |
echo $html ; |
| 837 |
} |
| 838 |
|
| 839 |
/** |
| 840 |
* Displays a separator field for a settings field |
| 841 |
* |
| 842 |
* @param array $args settings field args |
| 843 |
*/ |
| 844 |
function callback_separator( $args ) |
| 845 |
{ |
| 846 |
$type = ( isset( $args['type'] ) ? $args['type'] : 'separator' ); |
| 847 |
$html = ''; |
| 848 |
$html .= '<div class="wpsa-settings-separator"></div>'; |
| 849 |
echo $html ; |
| 850 |
} |
| 851 |
|
| 852 |
/** |
| 853 |
* Get the value of a settings field |
| 854 |
* |
| 855 |
* @param string $option settings field name. |
| 856 |
* @param string $section the section name this field belongs to. |
| 857 |
* @param string $default default text if it's not found. |
| 858 |
* |
| 859 |
* @return string |
| 860 |
*/ |
| 861 |
function get_option( $option, $section, $default = '' ) |
| 862 |
{ |
| 863 |
$options = get_option( $section ); |
| 864 |
if ( isset( $options[$option] ) ) { |
| 865 |
return $options[$option]; |
| 866 |
} |
| 867 |
return $default; |
| 868 |
} |
| 869 |
|
| 870 |
/** |
| 871 |
* Add submenu page to the Settings main menu. |
| 872 |
* |
| 873 |
* @param string $page_title |
| 874 |
* @param string $menu_title |
| 875 |
* @param string $capability |
| 876 |
* @param string $menu_slug |
| 877 |
* @param callable $function = '' |
| 878 |
* |
| 879 |
* @author Ahmad Awais |
| 880 |
* @since [version] |
| 881 |
*/ |
| 882 |
public function admin_menu() |
| 883 |
{ |
| 884 |
add_menu_page( |
| 885 |
__( 'Passster', 'content-protector' ), |
| 886 |
'Passster', |
| 887 |
'manage_options', |
| 888 |
'passster', |
| 889 |
array( $this, 'plugin_page' ), |
| 890 |
'dashicons-lock', |
| 891 |
85 |
| 892 |
); |
| 893 |
add_submenu_page( |
| 894 |
'passster', |
| 895 |
__( 'Options', 'content-protector' ), |
| 896 |
__( 'Options', 'content-protector' ), |
| 897 |
'manage_options', |
| 898 |
'passster_settings', |
| 899 |
array( $this, 'plugin_page' ) |
| 900 |
); |
| 901 |
} |
| 902 |
|
| 903 |
/** |
| 904 |
* Add admin page content. |
| 905 |
* |
| 906 |
* @return void |
| 907 |
*/ |
| 908 |
public function plugin_page() |
| 909 |
{ |
| 910 |
?> |
| 911 |
<div class="passster-admin-header"> |
| 912 |
<div class="logo"><img src="<?php |
| 913 |
echo PASSSTER_URL . '/assets/admin/passster-logo.svg' ; |
| 914 |
?>" /></div> |
| 915 |
<div class="info-links"> |
| 916 |
<?php |
| 917 |
?> |
| 918 |
<a href="https://patrickposner.dev/plugins/passster" target="_blank">Go Pro</a> |
| 919 |
<a href="https://patrickposner.dev/docs/passster" target="_blank">Documentation</a> |
| 920 |
<a href="https://wordpress.org/plugins/content-protector/" target="_blank">Support</a> |
| 921 |
<?php |
| 922 |
?> |
| 923 |
</div> |
| 924 |
</div> |
| 925 |
<div class="wrap passster-admin"> |
| 926 |
<?php |
| 927 |
$this->show_navigation(); |
| 928 |
?> |
| 929 |
<?php |
| 930 |
$this->show_forms(); |
| 931 |
?> |
| 932 |
</div> |
| 933 |
<?php |
| 934 |
} |
| 935 |
|
| 936 |
/** |
| 937 |
* Show navigations as tab |
| 938 |
* |
| 939 |
* Shows all the settings section labels as tab |
| 940 |
*/ |
| 941 |
function show_navigation() |
| 942 |
{ |
| 943 |
$html = '<h2 class="nav-tab-wrapper">'; |
| 944 |
foreach ( $this->sections_array as $tab ) { |
| 945 |
$html .= sprintf( '<a href="#%1$s" class="nav-tab" id="%1$s-tab">%2$s</a>', $tab['id'], $tab['title'] ); |
| 946 |
} |
| 947 |
$html .= '</h2>'; |
| 948 |
echo $html ; |
| 949 |
} |
| 950 |
|
| 951 |
/** |
| 952 |
* Show the section settings forms |
| 953 |
* |
| 954 |
* This function displays every sections in a different form |
| 955 |
*/ |
| 956 |
function show_forms() |
| 957 |
{ |
| 958 |
?> |
| 959 |
<div class="metabox-holder"> |
| 960 |
<?php |
| 961 |
foreach ( $this->sections_array as $form ) { |
| 962 |
?> |
| 963 |
<!-- style="display: none;" --> |
| 964 |
<div id="<?php |
| 965 |
echo $form['id'] ; |
| 966 |
?>" class="group"> |
| 967 |
<form method="post" action="options.php"> |
| 968 |
<?php |
| 969 |
do_action( 'wsa_form_top_' . $form['id'], $form ); |
| 970 |
settings_fields( $form['id'] ); |
| 971 |
do_settings_sections( $form['id'] ); |
| 972 |
do_action( 'wsa_form_bottom_' . $form['id'], $form ); |
| 973 |
?> |
| 974 |
<div style="padding-left: 10px"> |
| 975 |
<?php |
| 976 |
submit_button(); |
| 977 |
?> |
| 978 |
</div> |
| 979 |
</form> |
| 980 |
</div> |
| 981 |
<?php |
| 982 |
} |
| 983 |
?> |
| 984 |
</div> |
| 985 |
<?php |
| 986 |
$this->script(); |
| 987 |
} |
| 988 |
|
| 989 |
/** |
| 990 |
* Tabbable JavaScript codes & Initiate Color Picker |
| 991 |
* |
| 992 |
* This code uses localstorage for displaying active tabs |
| 993 |
*/ |
| 994 |
function script() |
| 995 |
{ |
| 996 |
?> |
| 997 |
<script> |
| 998 |
jQuery(document).ready(function ($) { |
| 999 |
|
| 1000 |
//Initiate Color Picker. |
| 1001 |
$('.color-picker').iris(); |
| 1002 |
|
| 1003 |
// Switches option sections |
| 1004 |
$('.group').hide(); |
| 1005 |
var activetab = ''; |
| 1006 |
if ('undefined' != typeof localStorage) { |
| 1007 |
activetab = localStorage.getItem('activetab'); |
| 1008 |
} |
| 1009 |
if ('' != activetab && $(activetab).length) { |
| 1010 |
$(activetab).fadeIn(); |
| 1011 |
} else { |
| 1012 |
$('.group:first').fadeIn(); |
| 1013 |
} |
| 1014 |
$('.group .collapsed').each(function () { |
| 1015 |
$(this) |
| 1016 |
.find('input:checked') |
| 1017 |
.parent() |
| 1018 |
.parent() |
| 1019 |
.parent() |
| 1020 |
.nextAll() |
| 1021 |
.each(function () { |
| 1022 |
if ($(this).hasClass('last')) { |
| 1023 |
$(this).removeClass('hidden'); |
| 1024 |
return false; |
| 1025 |
} |
| 1026 |
$(this) |
| 1027 |
.filter('.hidden') |
| 1028 |
.removeClass('hidden'); |
| 1029 |
}); |
| 1030 |
}); |
| 1031 |
|
| 1032 |
if ('' != activetab && $(activetab + '-tab').length) { |
| 1033 |
$(activetab + '-tab').addClass('nav-tab-active'); |
| 1034 |
} else { |
| 1035 |
$('.nav-tab-wrapper a:first').addClass('nav-tab-active'); |
| 1036 |
} |
| 1037 |
$('.nav-tab-wrapper a').click(function (evt) { |
| 1038 |
$('.nav-tab-wrapper a').removeClass('nav-tab-active'); |
| 1039 |
$(this) |
| 1040 |
.addClass('nav-tab-active') |
| 1041 |
.blur(); |
| 1042 |
var clicked_group = $(this).attr('href'); |
| 1043 |
if ('undefined' != typeof localStorage) { |
| 1044 |
localStorage.setItem('activetab', $(this).attr('href')); |
| 1045 |
} |
| 1046 |
$('.group').hide(); |
| 1047 |
$(clicked_group).fadeIn(); |
| 1048 |
evt.preventDefault(); |
| 1049 |
}); |
| 1050 |
|
| 1051 |
$('.wpsa-browse').on('click', function (event) { |
| 1052 |
event.preventDefault(); |
| 1053 |
|
| 1054 |
var self = $(this); |
| 1055 |
|
| 1056 |
// Create the media frame. |
| 1057 |
var file_frame = (wp.media.frames.file_frame = wp.media({ |
| 1058 |
title: self.data('uploader_title'), |
| 1059 |
button: { |
| 1060 |
text: self.data('uploader_button_text') |
| 1061 |
}, |
| 1062 |
multiple: false |
| 1063 |
})); |
| 1064 |
|
| 1065 |
file_frame.on('select', function () { |
| 1066 |
attachment = file_frame |
| 1067 |
.state() |
| 1068 |
.get('selection') |
| 1069 |
.first() |
| 1070 |
.toJSON(); |
| 1071 |
|
| 1072 |
self |
| 1073 |
.prev('.wpsa-url') |
| 1074 |
.val(attachment.url) |
| 1075 |
.change(); |
| 1076 |
}); |
| 1077 |
|
| 1078 |
// Finally, open the modal |
| 1079 |
file_frame.open(); |
| 1080 |
}); |
| 1081 |
|
| 1082 |
$('input.wpsa-url') |
| 1083 |
.on('change keyup paste input', function () { |
| 1084 |
var self = $(this); |
| 1085 |
self |
| 1086 |
.next() |
| 1087 |
.parent() |
| 1088 |
.children('.wpsa-image-preview') |
| 1089 |
.children('img') |
| 1090 |
.attr('src', self.val()); |
| 1091 |
}) |
| 1092 |
.change(); |
| 1093 |
}); |
| 1094 |
|
| 1095 |
</script> |
| 1096 |
|
| 1097 |
<style type="text/css"> |
| 1098 |
/** WordPress 3.8 Fix **/ |
| 1099 |
.form-table th { |
| 1100 |
padding: 20px 10px; |
| 1101 |
} |
| 1102 |
|
| 1103 |
#wpbody-content .metabox-holder { |
| 1104 |
padding-top: 5px; |
| 1105 |
} |
| 1106 |
|
| 1107 |
.wpsa-image-preview img { |
| 1108 |
height: auto; |
| 1109 |
max-width: 70px; |
| 1110 |
} |
| 1111 |
|
| 1112 |
.wpsa-settings-separator { |
| 1113 |
background: #ccc; |
| 1114 |
border: 0; |
| 1115 |
color: #ccc; |
| 1116 |
height: 1px; |
| 1117 |
position: absolute; |
| 1118 |
left: 0; |
| 1119 |
width: 99%; |
| 1120 |
} |
| 1121 |
|
| 1122 |
.group .form-table input.color-picker { |
| 1123 |
max-width: 100px; |
| 1124 |
} |
| 1125 |
</style> |
| 1126 |
<?php |
| 1127 |
} |
| 1128 |
|
| 1129 |
} |
| 1130 |
// WP_OSA ended. |
| 1131 |
} |
| 1132 |
|