', 'rocket' ), [ 'status' => 500 ] ); } $this->clean_url_cache( $url ); $pages_count = $this->query->get_total_count( false ); $source_raw = $request->get_param( 'source' ); $source = is_string( $source_raw ) && '' !== $source_raw ? sanitize_key( $source_raw ) : 'manual'; $tracked_event = [ 'button' => 'rocket cdn add page', 'is_homepage' => Utils::is_home( $url ), 'pages_count' => $pages_count, 'source' => $source, ]; if ( Utils::is_home( $url ) ) { $tracked_event['button'] = 'rocket cdn add homepage'; unset( $tracked_event['is_homepage'] ); unset( $tracked_event['pages_count'] ); } $this->track_event( 'Button Clicked', $tracked_event ); return new WP_REST_Response( $this->get_pages_data(), 201 ); } /** * Cleans the cache for the provided URL to ensure changes are reflected in CDN delivery. * * @param string $url URL to clear. * * @return void */ private function clean_url_cache( string $url ): void { if ( Utils::is_home( $url ) ) { rocket_clean_home(); return; } rocket_clean_files( [ $url ] ); } /** * Removes a page from CDN free-tier delivery by DB record ID. * * Deletes the DB record and returns the updated list. * * @param WP_REST_Request $request REST request. * @return WP_REST_Response|WP_Error */ public function remove_page( WP_REST_Request $request ) { $id = $request->get_param( 'id' ); $item = $this->query->get_item( $id ); if ( empty( $item ) ) { return new WP_Error( 'rocketcdn_page_not_found', __( 'The specified page was not found.', 'rocket' ), [ 'status' => 404 ] ); } $removed_url = $item->url; $this->query->delete_item( $id ); $this->clean_url_cache( $removed_url ); $pages_count = $this->query->get_total_count( false ); $this->track_event( 'Button Clicked', [ 'button' => 'rocket cdn remove page', 'is_homepage' => Utils::is_home( $removed_url ), 'pages_count' => $pages_count, ] ); return new WP_REST_Response( $this->get_pages_data(), 200 ); } /** * Returns all registered free-tier pages with count and limit info. * * @return WP_REST_Response */ public function get_pages(): WP_REST_Response { return new WP_REST_Response( $this->get_pages_data(), 200 ); } /** * Quick-adds the site homepage as a free-tier CDN page. * * @return WP_REST_Response|WP_Error */ public function add_homepage() { $request = new WP_REST_Request( 'POST' ); $request->set_param( 'url', untrailingslashit( home_url() ) ); $request->set_param( 'source', 'add_homepage_button' ); return $this->add_page( $request ); } /** * Saves CDN driver state options. * * Persists the paused state. * * @param WP_REST_Request $request REST request. * @return WP_REST_Response */ public function save_pause_state( WP_REST_Request $request ): WP_REST_Response { $paused = (int) $request->get_param( 'paused' ); $this->options->set( 'cdn', $paused ); $this->options_api->set( 'settings', $this->options->get_options() ); $status = 0 === $paused ? 'paused' : 'active'; $action = 0 === $paused ? 'user_paused' : 'user_resume'; do_action( 'rocket_rocketcdn_cdn_state_changed', $status, $action ); return new WP_REST_Response( [ 'paused' => $this->options->get( 'cdn', 0 ), ], 200 ); } /** * Checks whether the free-tier page limit has been reached. * * @return bool True if the count is at or above the limit. */ public function is_limit_reached(): bool { return $this->query->get_total_count() >= $this->get_free_page_limit(); } /** * Builds the pages response payload. * * @return array */ private function get_pages_data(): array { $pages = $this->query->get_all(); $pages_count = $this->query->get_total_count( false ); return [ 'pages' => array_map( function ( $page ) { return [ 'id' => (int) $page->id, 'url' => $page->url, 'title' => $page->title, ]; }, $pages ), 'count' => $pages_count, 'limit' => $this->get_free_page_limit(), 'items_html' => $this->render_controller->get_built_in_page_list(), 'status_indicator_html' => $this->render_controller->get_status_indicator_html( $pages_count ), 'is_subscription_creation_loading' => $this->subscription_controller->is_subscription_creation_loading(), ]; } /** * Return the total number of free pages allowed for CDN delivery. * * @return int */ protected function get_free_page_limit(): int { return $this->context->get_free_page_limit(); } /** * Save cdn driver * * Persists the active driver tab selection so the UI * can restore the correct view after a page refresh. * * @param WP_REST_Request $request REST request. * @return WP_REST_Response */ public function save_cdn_type( WP_REST_Request $request ) { $cdn_type = $request->get_param( 'driver' ); $this->options->set( 'cdn_type', $cdn_type ); $this->options_api->set( 'settings', $this->options->get_options() ); return new WP_REST_Response( [ 'cdn_type' => $cdn_type, 'disable_rocket_cdn_elements' => $this->render_controller->should_disable_element_for_rocketcdn(), ], 200 ); } /** * Get subscription details. * * @return WP_REST_Response */ public function get_subscription(): WP_REST_Response { $subscription = $this->subscription_controller->get_subscription(); if ( empty( $subscription ) ) { return new WP_REST_Response( null, 204 ); } return new WP_REST_Response( $subscription, 200 ); } /** * Get URL validation payload. * * @param string $url URL to validate. * * @return array */ private function get_url_validation_payload( string $url ): array { $payload = $this->get_page_url_validation_payload( $url ); // Check for same host. $url_host = wp_parse_url( $url ); $site_host = wp_parse_url( home_url() ); // Check that URL has a valid host component. if ( ! isset( $url_host['host'] ) ) { $payload['error'] = true; $payload['message'] = __( 'Invalid URL provided.', 'rocket' ); } // Check that URL host matches site host. if ( $url_host['host'] !== $site_host['host'] ) { $payload['error'] = true; $payload['message'] = __( 'URL must be on the same domain as the site.', 'rocket' ); } return $payload; } }