| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_MGL_Rest |
| 4 |
{ |
| 5 |
private $core; |
| 6 |
private $namespace = 'meow-gallery/v1'; |
| 7 |
|
| 8 |
public function __construct( $core ) { |
| 9 |
$this->core = $core; |
| 10 |
|
| 11 |
// FOR DEBUG |
| 12 |
// For experiencing the UI behavior on a slower install. |
| 13 |
// sleep(1); |
| 14 |
// For experiencing the UI behavior on a buggy install. |
| 15 |
// trigger_error( "Error", E_USER_ERROR); |
| 16 |
// trigger_error( "Warning", E_USER_WARNING); |
| 17 |
// trigger_error( "Notice", E_USER_NOTICE); |
| 18 |
// trigger_error( "Deprecated", E_USER_DEPRECATED); |
| 19 |
|
| 20 |
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
function rest_api_init() { |
| 25 |
|
| 26 |
// Settings |
| 27 |
register_rest_route( $this->namespace, '/update_option/', array( |
| 28 |
'methods' => 'POST', |
| 29 |
'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 30 |
'callback' => array( $this, 'rest_update_option' ) |
| 31 |
) ); |
| 32 |
register_rest_route( $this->namespace, '/all_settings/', array( |
| 33 |
'methods' => 'GET', |
| 34 |
'permission_callback' => array( $this->core, 'can_access_settings' ), |
| 35 |
'callback' => array( $this, 'rest_all_settings' ) |
| 36 |
) ); |
| 37 |
|
| 38 |
// Gutenberg Block |
| 39 |
register_rest_route( $this->namespace, '/preview', array( |
| 40 |
'methods' => 'POST', |
| 41 |
'permission_callback' => array( $this->core, 'can_access_features' ), |
| 42 |
'callback' => array( $this, 'preview' ), |
| 43 |
) ); |
| 44 |
} |
| 45 |
|
| 46 |
function preview( WP_REST_Request $request ) { |
| 47 |
$params = $request->get_body(); |
| 48 |
$params = json_decode( $params ); |
| 49 |
$params->ids = implode( ',', $params->ids ); |
| 50 |
$atts = (array) $params; |
| 51 |
$html = $this->core->gallery( $atts, true ); |
| 52 |
return new WP_REST_Response( [ 'success' => true, 'data' => $html ], 200 ); |
| 53 |
} |
| 54 |
|
| 55 |
function rest_all_settings() { |
| 56 |
return new WP_REST_Response( [ 'success' => true, 'data' => $this->core->get_all_options() ], 200 ); |
| 57 |
} |
| 58 |
|
| 59 |
function rest_update_option( $request ) { |
| 60 |
try { |
| 61 |
$params = $request->get_json_params(); |
| 62 |
$value = $params['options']; |
| 63 |
$options = $this->core->update_options( $value ); |
| 64 |
$success = !!$options; |
| 65 |
$message = __( $success ? 'OK' : "Could not update options.", MGL_DOMAIN ); |
| 66 |
return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $success ? $options : null ], 200 ); |
| 67 |
} |
| 68 |
catch ( Exception $e ) { |
| 69 |
return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
?> |