api = null !== $api ? $api : new API(); } /** * Check if Cloudflare cache is enabled and configured. * * @return bool */ public function is_enabled() { return (bool) get_option( 'ajaxpress_cf_cache_enabled', false ); } /** * Get current configuration status. * * @return array */ public function get_status() { $enabled = $this->is_enabled(); $zone_id = get_option( 'ajaxpress_cf_zone_id', '' ); $zone_name = get_option( 'ajaxpress_cf_zone_name', '' ); $route_id = get_option( 'ajaxpress_cf_route_id', '' ); $account_id = get_option( 'ajaxpress_cf_account_id', '' ); return array( 'enabled' => $enabled, 'connected' => ! empty( $zone_id ) && ! empty( $route_id ), 'zone_id' => $zone_id, 'zone_name' => $zone_name, 'route_id' => $route_id, 'route_pattern' => get_option( 'ajaxpress_cf_route_pattern', '' ), 'account_id' => $account_id, 'worker_name' => self::WORKER_NAME, 'auto_purge' => (bool) get_option( 'ajaxpress_cf_auto_purge', true ), ); } /** * Deploy worker and create route. * * @param string $zone_id Zone ID. * @return array|WP_Error */ public function deploy( $zone_id ) { // Get zone info. $zone_result = $this->api->get_zone( $zone_id ); if ( is_wp_error( $zone_result ) ) { return $zone_result; } $zone = $zone_result['result']; // Verify zone is proxied/active. if ( 'active' !== $zone['status'] ) { return new \WP_Error( 'zone_not_active', __( 'Zone is not active. Please ensure your domain is properly configured in Cloudflare.', 'ajaxpress' ) ); } // Get account ID from zone. $account_id = $zone['account']['id']; // Get site host for route pattern (handles subdomains). $site_host = wp_parse_url( home_url(), PHP_URL_HOST ); // Verify site host matches zone. if ( ! $this->domain_matches_zone( $site_host, $zone['name'] ) ) { return new \WP_Error( 'domain_mismatch', sprintf( // translators: 1: site domain, 2: Cloudflare zone name. __( 'Your site domain (%1$s) does not match the selected Cloudflare zone (%2$s).', 'ajaxpress' ), $site_host, $zone['name'] ) ); } // Get bypass patterns from settings. $bypass_patterns_raw = get_option( 'ajaxpress_cf_bypass_patterns', "/wp-admin\n/wp-json\n/checkout\n/cart" ); $bypass_patterns = array_filter( array_map( 'trim', explode( "\n", $bypass_patterns_raw ) ) ); $bypass_cookies_raw = get_option( 'ajaxpress_cf_bypass_cookies', 'wordpress_logged_in_' ); $bypass_cookies = array_filter( array_map( 'trim', explode( "\n", $bypass_cookies_raw ) ) ); // Generate worker script. $script = $this->api->get_worker_script( array( 'bypass_patterns' => $bypass_patterns, 'bypass_cookies' => $bypass_cookies, ) ); // Upload worker. $upload_result = $this->api->upload_worker( $account_id, self::WORKER_NAME, $script ); if ( is_wp_error( $upload_result ) ) { return $upload_result; } // Create route pattern using site host (handles subdomains). $pattern = $site_host . '/*'; // Check if route already exists. $routes = $this->api->list_routes( $zone_id ); if ( ! is_wp_error( $routes ) && ! empty( $routes['result'] ) ) { foreach ( $routes['result'] as $route ) { if ( $pattern === $route['pattern'] && self::WORKER_NAME === $route['script'] ) { // Route already exists, store info and return. $this->save_deployment_info( $zone, $route['id'], $pattern, $account_id ); return array( 'success' => true, 'route_id' => $route['id'], 'route_pattern' => $pattern, 'message' => __( 'Route already exists. Configuration updated.', 'ajaxpress' ), ); } } } // Create route. $route_result = $this->api->create_route( $zone_id, $pattern, self::WORKER_NAME ); if ( is_wp_error( $route_result ) ) { // Cleanup worker if route creation fails. $this->api->delete_worker( $account_id, self::WORKER_NAME ); return $route_result; } $route_id = $route_result['result']['id']; // Save deployment info. $this->save_deployment_info( $zone, $route_id, $pattern, $account_id ); return array( 'success' => true, 'route_id' => $route_id, 'route_pattern' => $pattern, 'message' => __( 'Cloudflare cache deployed successfully.', 'ajaxpress' ), ); } /** * Check if site domain matches a zone. * * @param string $site_host Site hostname. * @param string $zone_name Zone name. * @return bool */ private function domain_matches_zone( $site_host, $zone_name ) { if ( $site_host === $zone_name ) { return true; } if ( substr( $site_host, -strlen( '.' . $zone_name ) ) === '.' . $zone_name ) { return true; } return false; } /** * Save deployment information. * * @param array $zone Zone info. * @param string $route_id Route ID. * @param string $pattern Route pattern. * @param string $account_id Account ID. */ private function save_deployment_info( $zone, $route_id, $pattern, $account_id ) { update_option( 'ajaxpress_cf_cache_enabled', true ); update_option( 'ajaxpress_cf_zone_id', $zone['id'] ); update_option( 'ajaxpress_cf_zone_name', $zone['name'] ); update_option( 'ajaxpress_cf_route_id', $route_id ); update_option( 'ajaxpress_cf_route_pattern', $pattern ); update_option( 'ajaxpress_cf_account_id', $account_id ); } /** * Remove worker and route (teardown). * * @return array|WP_Error */ public function teardown() { $zone_id = get_option( 'ajaxpress_cf_zone_id', '' ); $route_id = get_option( 'ajaxpress_cf_route_id', '' ); $account_id = get_option( 'ajaxpress_cf_account_id', '' ); $errors = array(); // Delete route. if ( $zone_id && $route_id ) { $route_result = $this->api->delete_route( $zone_id, $route_id ); if ( is_wp_error( $route_result ) ) { $errors[] = $route_result->get_error_message(); } } // Delete worker. if ( $account_id ) { $worker_result = $this->api->delete_worker( $account_id, self::WORKER_NAME ); if ( is_wp_error( $worker_result ) ) { $errors[] = $worker_result->get_error_message(); } } // Clear stored settings. $this->clear_settings(); if ( ! empty( $errors ) ) { return new \WP_Error( 'teardown_partial', implode( ' ', $errors ) ); } return array( 'success' => true, 'message' => __( 'Cloudflare cache disconnected successfully.', 'ajaxpress' ), ); } /** * Clear all CF settings. */ private function clear_settings() { delete_option( 'ajaxpress_cf_cache_enabled' ); delete_option( 'ajaxpress_cf_zone_id' ); delete_option( 'ajaxpress_cf_zone_name' ); delete_option( 'ajaxpress_cf_route_id' ); delete_option( 'ajaxpress_cf_route_pattern' ); delete_option( 'ajaxpress_cf_account_id' ); } /** * Purge specific URLs. * * @param array $urls URLs to purge. * @return array|WP_Error */ public function purge_urls( $urls ) { $zone_id = get_option( 'ajaxpress_cf_zone_id', '' ); if ( empty( $zone_id ) ) { return new \WP_Error( 'not_configured', __( 'Cloudflare cache is not configured.', 'ajaxpress' ) ); } if ( empty( $urls ) ) { return new \WP_Error( 'no_urls', __( 'No URLs provided to purge.', 'ajaxpress' ) ); } // Batch URLs (max 30 per request). $batches = array_chunk( $urls, 30 ); $results = array(); foreach ( $batches as $batch ) { $result = $this->api->purge_urls( $zone_id, $batch ); if ( is_wp_error( $result ) ) { return $result; } $results[] = $result; } return array( 'success' => true, 'purged' => count( $urls ), 'message' => sprintf( // translators: %d: number of URLs purged. __( 'Purged %d URL(s) from cache.', 'ajaxpress' ), count( $urls ) ), ); } /** * Purge all cache. * * @return array|WP_Error */ public function purge_all() { $zone_id = get_option( 'ajaxpress_cf_zone_id', '' ); if ( empty( $zone_id ) ) { return new \WP_Error( 'not_configured', __( 'Cloudflare cache is not configured.', 'ajaxpress' ) ); } $result = $this->api->purge_all( $zone_id ); if ( is_wp_error( $result ) ) { return $result; } return array( 'success' => true, 'message' => __( 'Entire cache purged successfully.', 'ajaxpress' ), ); } /** * Get URLs related to a post for purging. * * @param int $post_id Post ID. * @return array URLs to purge. */ public function get_post_urls( $post_id ) { $urls = array(); // Post permalink. $permalink = get_permalink( $post_id ); if ( $permalink ) { $urls[] = $permalink; } // Home page. $urls[] = home_url( '/' ); // Post type archive. $post_type = get_post_type( $post_id ); if ( $post_type && 'page' !== $post_type ) { $archive = get_post_type_archive_link( $post_type ); if ( $archive ) { $urls[] = $archive; } } // Categories. $categories = get_the_category( $post_id ); if ( $categories ) { foreach ( $categories as $category ) { $urls[] = get_category_link( $category->term_id ); } } // Tags. $tags = get_the_tags( $post_id ); if ( $tags ) { foreach ( $tags as $tag ) { $urls[] = get_tag_link( $tag->term_id ); } } // Author archive. $author_id = get_post_field( 'post_author', $post_id ); if ( $author_id ) { $urls[] = get_author_posts_url( $author_id ); } // Apply filter for custom URLs. $urls = apply_filters( 'ajaxpress_cf_purge_post_urls', array_unique( $urls ), $post_id ); return $urls; } /** * Static helper to purge cache. * * Usage: * \AjaxPress\Cloudflare\Cache::purge( 'https://example.com/page' ); * \AjaxPress\Cloudflare\Cache::purge( [ 'https://example.com/page1', 'https://example.com/page2' ] ); * \AjaxPress\Cloudflare\Cache::purge(); // Purge all * * @param string|array|null $urls URL(s) to purge, or null to purge all. * @return array|WP_Error */ public static function purge( $urls = null ) { $cache = new self(); if ( null === $urls ) { return $cache->purge_all(); } if ( is_string( $urls ) ) { $urls = array( $urls ); } return $cache->purge_urls( $urls ); } } } /** * Global helper function to purge AjaxPress cache. * * Usage: * ajaxpress_purge_cache( 'https://example.com/page' ); * ajaxpress_purge_cache( [ 'url1', 'url2' ] ); * ajaxpress_purge_cache(); // Purge all * * @param string|array|null $urls URL(s) to purge, or null to purge all. * @return array|WP_Error */ function ajaxpress_purge_cache( $urls = null ) { return \AjaxPress\Cloudflare\Cache::purge( $urls ); }