s->endpoints); } else { $result->set_data( $this->customizeNotFoundResponse( $result, $request ) ); } } } return $served; } /** * Determines whether the request is madse by plugin. * * @param string $route (Rest route|Full URL) * @param string $slug (Plugin's slug) * @return bool */ public function isRequestOfPlugin($route = '', $slug = '') { $slug = $slug ?: $this->config->get('app.slug'); if (!$route) { if (get_option('permalink_structure')) { $route = $this->request->url(); } else { $route = $this->request->query('rest_route'); } } // For web routing (If web-routing is installed) if (!$route && !$this->request->isRest()) { $route = $this->request->url(); } $parsedUrl = parse_url($route ?? ''); $path = str_replace('/wp-json', '', $parsedUrl['path'] ?? ''); if (is_admin()) { $page = $this->request->query('page'); if ($slug === $page) { $path = $page; } } return str_starts_with(ltrim($path, '/'), $slug); } /** * Determines if the request is made for endpoints. * * @param string $route (Rest route|Full URL) * @return bool */ protected function isRequestForEndpoints($route) { return str_ends_with($route, '__endpoints'); } /** * Prepare a custom not found response. * * @param \WP_Rest_Response $result * @param \WP_Rest_Request $request * * @return array */ public function customizeNotFoundResponse($result, $request) { $response = $result->get_data(); if ($this->env() === 'dev') { $response['data']['wpfluent'] = [ 'env' => $this->env(), 'method' => $request->get_method(), 'request_url' => $this->request->url(), 'route_params' => $request->get_url_params(), 'query_params' => $request->get_query_params(), 'body_params' => $request->get_body_params(), ]; } else { $response['data']['wpfluent'] = [ 'env' => $this->env() ]; } return $response; } /** * Check if running unit test. * * @return boolean */ public function isUnitTesting() { return getenv('ENV') === 'testing'; } /** * Register the rest api init actions and routes * * @param self $app */ protected function addRestApiInitAction($app) { $this->addAction('rest_api_init', function($wpRestServer) use ($app) { try { $this->registerRestRoutes($app->router); } catch (InvalidArgumentException $e) { return $app->response->json([ 'message' => $e->getMessage() ], $e->getCode() ?: 500); } }); } /** * Register rest routes. * * @param \NinjaTables\Framework\Http\Router $router * * @return null */ protected function registerRestRoutes($router) { $router->registerRoutes( $this->requireRouteFile($router) ); } /** * Load (include) routes * * @param \NinjaTables\Framework\Http\Router $router * @return null */ protected function requireRouteFile($router) { require_once $this['path.http'] . 'Routes/routes.php'; } /** * Register plugin booted callbacks. * * @param callable $callback * @return void */ protected function ready(callable $callback) { $this->onReady[] = $callback; } /** * Register Async Actions. * * @return void */ protected function registerAsyncActions() { Client::registerAsyncRequestHandler(); } /** * Execute plugin booted callbacks. * * @param callable $callback * @return void */ protected function callPluginReadyCallbacks() { while ($callback = array_shift($this->onReady)) { $callback($this); } } }