request = $request; if ( ! current_user_can( 'edit_posts' ) ) { return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to change the Templately toolbar visibility.', 'templately' ), [ 'status' => rest_authorization_required_code() ] ); } return true; } public function register_routes() { $this->register_endpoint( 'gutenberg/visibility', [ $this, 'update_visibility' ], [ 'visibility' => [ 'required' => true, 'type' => 'string', 'description' => __( 'Desired visibility of the Templately editor toolbar buttons: "visible" or "hidden".', 'templately' ), ], ], WP_REST_Server::EDITABLE ); } public function update_visibility() { $visibility = $this->get_param( 'visibility', '', 'sanitize_key' ); if ( ! in_array( $visibility, [ 'visible', 'hidden' ], true ) ) { return $this->error( 'invalid_visibility', __( 'The visibility value must be either "visible" or "hidden".', 'templately' ), 'gutenberg/visibility', 400 ); } // Same site-wide option key the ajax handler wrote to. 'hidden' => 'yes'. $hide_buttons = 'hidden' === $visibility ? 'yes' : 'no'; update_option( 'templately-gutenberg-hide-buttons', $hide_buttons ); // Re-read and normalize exactly as the ajax handler did (any non-'yes' => 'no'). $stored = get_option( 'templately-gutenberg-hide-buttons', 'no' ); $hide_buttons = 'yes' === $stored ? 'yes' : 'no'; return $this->success( [ 'visibility' => 'yes' === $hide_buttons ? 'hidden' : 'visible', 'hide_buttons' => $hide_buttons, ] ); } }