| 1 |
<?php |
| 2 |
|
| 3 |
namespace PEB; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class RestAPI { |
| 10 |
public function __construct() { |
| 11 |
add_action( 'wp_ajax_pebSaveUninstallOption', [ $this, 'peb_save_uninstall_option' ] ); |
| 12 |
add_action( 'wp_ajax_pebSaveGlobalViewerOptions', [ $this, 'peb_save_global_viewer_options' ] ); |
| 13 |
} |
| 14 |
|
| 15 |
public function peb_save_uninstall_option() { |
| 16 |
// Check nonce. |
| 17 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 18 |
if ( ! wp_verify_nonce( $nonce, 'peb_uninstall_nonce' ) ) { |
| 19 |
wp_send_json_error( array( 'message' => __( 'Invalid security token.', 'pdf-embed-block' ) ) ); |
| 20 |
} |
| 21 |
|
| 22 |
// Check user capability. |
| 23 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 24 |
wp_send_json_error( array( 'message' => __( 'You do not have permission to manage options.', 'pdf-embed-block' ) ) ); |
| 25 |
} |
| 26 |
|
| 27 |
// Check enabled post value. |
| 28 |
$enabled = isset( $_POST['enabled'] ) && filter_var( wp_unslash( $_POST['enabled'] ), FILTER_VALIDATE_BOOLEAN ); |
| 29 |
|
| 30 |
// Update option. |
| 31 |
update_option( 'peb_delete_data_on_uninstall', $enabled ); |
| 32 |
|
| 33 |
wp_send_json_success( array( |
| 34 |
'enabled' => $enabled, |
| 35 |
'message' => $enabled |
| 36 |
? __( 'Data will be deleted on uninstall.', 'pdf-embed-block' ) |
| 37 |
: __( 'Data will be preserved on uninstall.', 'pdf-embed-block' ) |
| 38 |
) ); |
| 39 |
} |
| 40 |
|
| 41 |
public function peb_save_global_viewer_options() { |
| 42 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 43 |
if ( ! wp_verify_nonce( $nonce, 'peb_global_options_nonce' ) ) { |
| 44 |
wp_send_json_error( array( 'message' => __( 'Invalid security token.', 'pdf-embed-block' ) ) ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 48 |
wp_send_json_error( array( 'message' => __( 'You do not have permission to manage options.', 'pdf-embed-block' ) ) ); |
| 49 |
} |
| 50 |
|
| 51 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- $_POST['options'] is a JSON string, sanitized after json_decode. |
| 52 |
$options_raw = isset( $_POST['options'] ) ? wp_unslash( $_POST['options'] ) : ''; |
| 53 |
if ( is_string( $options_raw ) ) { |
| 54 |
$options_raw = json_decode( $options_raw, true ); |
| 55 |
} |
| 56 |
|
| 57 |
$sanitized = array( |
| 58 |
'showDownloadPDF' => isset( $options_raw['showDownloadPDF'] ) ? filter_var( $options_raw['showDownloadPDF'], FILTER_VALIDATE_BOOLEAN ) : false, |
| 59 |
'showPrintPDF' => isset( $options_raw['showPrintPDF'] ) ? filter_var( $options_raw['showPrintPDF'], FILTER_VALIDATE_BOOLEAN ) : false, |
| 60 |
'showFullScreen' => isset( $options_raw['showFullScreen'] ) ? filter_var( $options_raw['showFullScreen'], FILTER_VALIDATE_BOOLEAN ) : true, |
| 61 |
'forceGlobal' => isset( $options_raw['forceGlobal'] ) ? filter_var( $options_raw['forceGlobal'], FILTER_VALIDATE_BOOLEAN ) : false, |
| 62 |
); |
| 63 |
|
| 64 |
update_option( 'peb_global_viewer_options', $sanitized ); |
| 65 |
|
| 66 |
wp_send_json_success( array( |
| 67 |
'options' => $sanitized, |
| 68 |
'message' => __( 'Global PDF viewer settings profile saved successfully.', 'pdf-embed-block' ) |
| 69 |
) ); |
| 70 |
} |
| 71 |
|
| 72 |
} |