core = $core; // FOR DEBUG // For experiencing the UI behavior on a slower install. // sleep(1); // For experiencing the UI behavior on a buggy install. // trigger_error( "Error", E_USER_ERROR); // trigger_error( "Warning", E_USER_WARNING); // trigger_error( "Notice", E_USER_NOTICE); // trigger_error( "Deprecated", E_USER_DEPRECATED); add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); } function rest_api_init() { // Settings register_rest_route( $this->namespace, '/update_option/', array( 'methods' => 'POST', 'permission_callback' => array( $this->core, 'can_access_settings' ), 'callback' => array( $this, 'rest_update_option' ) ) ); register_rest_route( $this->namespace, '/all_settings/', array( 'methods' => 'GET', 'permission_callback' => array( $this->core, 'can_access_settings' ), 'callback' => array( $this, 'rest_all_settings' ) ) ); // Gutenberg Block register_rest_route( $this->namespace, '/preview', array( 'methods' => 'POST', 'permission_callback' => array( $this->core, 'can_access_features' ), 'callback' => array( $this, 'preview' ), ) ); } function preview( WP_REST_Request $request ) { $params = $request->get_body(); $params = json_decode( $params ); $params->ids = implode( ',', $params->ids ); $atts = (array) $params; $html = $this->core->gallery( $atts, true ); return new WP_REST_Response( [ 'success' => true, 'data' => $html ], 200 ); } function rest_all_settings() { return new WP_REST_Response( [ 'success' => true, 'data' => $this->core->get_all_options() ], 200 ); } function rest_update_option( $request ) { try { $params = $request->get_json_params(); $value = $params['options']; $options = $this->core->update_options( $value ); $success = !!$options; $message = __( $success ? 'OK' : "Could not update options.", MGL_DOMAIN ); return new WP_REST_Response([ 'success' => $success, 'message' => $message, 'options' => $success ? $options : null ], 200 ); } catch ( Exception $e ) { return new WP_REST_Response([ 'success' => false, 'message' => $e->getMessage() ], 500 ); } } } ?>