helpers_adapter = $helpers_adapter; } /** * Registers the REST API routes for the BackWPup plugin. * * This method is responsible for defining the routes that the plugin * exposes via the WordPress REST API. Each route should be registered * with its corresponding callback and permissions. * * @return void */ public function register_routes(): void { register_rest_route( self::ROUTE_NAMESPACE, '/getblock', [ 'methods' => 'POST', 'callback' => [ $this, 'getblock' ], 'permission_callback' => [ $this, 'has_permission' ], ] ); } /** * Checks if the current user has the necessary permissions to perform the action. * * @return bool True if the user has permission, false otherwise. */ public function has_permission(): bool { return current_user_can( 'backwpup' ); } /** * Get the block content. * * This function retrieves the HTML content for a specified block based on the provided parameters. * It checks for the presence of 'block_name' and 'block_type' in the request parameters and * calls the appropriate method to get the block content. * * @param \WP_REST_Request $request The REST request object containing the parameters. * * @return \WP_HTTP_Response The response object containing the HTML content and status. * * @throws \Exception If an error occurs during the process. */ public function getblock( \WP_REST_Request $request ) { $params = $request->get_params(); $html = ''; $status = 200; try { if ( ! isset( $params['block_name'] ) ) { throw new \Exception( __( 'No block name set.', 'backwpup' ) ); } if ( ! isset( $params['block_type'] ) || ! in_array( $params['block_type'], self::$authorised_blocks_type, true ) ) { throw new \Exception( __( 'Wrong block type set.', 'backwpup' ) ); } $method = $params['block_type']; $data = $params['block_data'] ?? []; if ( 'component' === $method ) { $html = $this->helpers_adapter->$method( $params['block_name'], $data, true ); } else { $html = $this->helpers_adapter->$method( $params['block_name'], true, $data ); } } catch ( \Exception $e ) { $html = $this->helpers_adapter->component( 'alerts/info', [ 'type' => 'alert', 'font' => 'xs', 'content' => $e->getMessage(), ], true ); $status = 500; } return rest_ensure_response( new \WP_REST_Response( $html, $status ) ); } }