### Initialize Fastly Plugin Singleton in PHP Source: https://context7.com/fastly/wordpress-plugin/llms.txt Demonstrates how to get the singleton instance of the Purgely class, access plugin and VCL versions, and check the connection status. This is the primary way to interact with the plugin's core functionality. ```php version; // "1.2.29" // Access VCL version echo $purgely->vcl_last_version; // "1.1.1" // Check connection status $status = $purgely->connection_status; // Returns: ['status' => true, 'message' => 'Connection Successful on service *MyService*'] ``` -------------------------------- ### Configure Fastly Edge Modules in WordPress Source: https://context7.com/fastly/wordpress-plugin/llms.txt This PHP code demonstrates how to retrieve available Edge Modules from the Purgely instance and provides an example of the JSON structure for an Edge Module configuration. Edge Modules allow enabling pre-built VCL functionality via the WordPress admin. ```php fastly_edge_modules(); // Returns associative array of module definitions: /* [ 'cors_headers' => { 'id' => 'cors_headers', 'name' => 'CORS headers', 'description' => 'Set CORS headers', 'properties' => [...], 'vcl' => [...] }, 'blackfire_integration' => {...}, 'countryblock' => {...}, 'datadome_integration' => {...}, ... ] */ // Edge module JSON structure (example: cors_headers.json) /* { "id": "cors_headers", "name": "CORS headers", "description": "Set CORS headers", "version": 1, "properties": [ { "name": "origin", "label": "Origins allowed", "type": "select", "options": { "anyone": "Allow anyone (*)", "regex-match": "Regex matching set of origins" }, "default": "anyone", "required": true }, { "name": "cors_allowed_methods", "label": "Allowed HTTP Methods", "type": "string", "default": "GET,HEAD,POST,OPTIONS", "required": true } ], "vcl": [ { "type": "deliver", "template": "VCL template with {{property}} placeholders" } ] } */ // Available edge modules: // - blackfire_integration: Enable Blackfire profiling // - cors_headers: Set CORS headers // - countryblock: Block requests by country // - datadome_integration: Datadome bot detection // - netacea_integration: Netacea bot detection // - redirect_hosts: Redirect domains // - url_rewrites: Rewrite URLs // - mobile_device_detection: Mobile theme support // - other_cms_integration: Integrate external backends // - increase_timeouts_long_jobs: Extend timeouts for long operations // - force_cache_miss_on_hard_reload_for_admins: Admin cache bypass // - nocache: Bypass caching for specific paths ``` -------------------------------- ### Add Custom Related Cache Keys in WordPress with Fastly Source: https://github.com/fastly/wordpress-plugin/blob/master/readme.txt This example shows how to add custom keys to the cache invalidation process using the 'purgely_related_keys' filter. This is useful for ensuring that related content or specific identifiers are also purged when a post is updated. The function receives the current keys array and the post object as arguments. ```php add_filter('purgely_related_keys', 'custom_related_keys', 10, 2); function custom_related_keys($keys_array, $post_object) { $keys_array[] = 'custom-key'; return $keys_array; } ``` -------------------------------- ### Fastly Cache Management WP-CLI Commands (Bash) Source: https://context7.com/fastly/wordpress-plugin/llms.txt Provides command-line interface tools for managing Fastly cache and plugin configuration. Commands include purging all content, specific posts by ID, or specific URLs. Configuration can be listed and updated for various sections like general, advanced, and webhooks. ```bash # Purge all cached content wp fastly purge all # Purge a specific post by ID (purges post and all related keys) wp fastly purge id 123 # Purge a specific URL wp fastly purge url https://example.com/my-page/ # List configuration settings for a section wp fastly configlist general wp fastly configlist advanced wp fastly configlist webhooks # Set a configuration option # Usage: wp fastly configset {section} {option} {value} wp fastly configset general fastly_api_key YOUR_API_KEY wp fastly configset general fastly_service_id YOUR_SERVICE_ID wp fastly configset advanced surrogate_control_ttl 86400 wp fastly configset advanced default_purge_type soft wp fastly configset advanced enable_stale_while_revalidate true wp fastly configset webhooks webhooks_activate true wp fastly configset webhooks webhooks_channel my-channel # Boolean values use true/false wp fastly configset advanced allow_purge_all true wp fastly configset advanced fastly_debug_mode false ``` -------------------------------- ### Manage Fastly Plugin Settings in PHP Source: https://context7.com/fastly/wordpress-plugin/llms.txt Shows how to retrieve individual settings, all settings as an array, and registered settings with their default values and sanitization callbacks using the Purgely_Settings class. It also illustrates defining settings via constants in wp-config.php. ```php ['sanitize_callback' => 'purgely_sanitize_key', 'default' => ''], 'fastly_service_id' => ['sanitize_callback' => 'purgely_sanitize_key', 'default' => ''], 'surrogate_control_ttl' => ['sanitize_callback' => 'absint', 'default' => 86400], 'enable_stale_while_revalidate' => ['sanitize_callback' => 'purgely_sanitize_checkbox', 'default' => true], 'stale_while_revalidate_ttl' => ['sanitize_callback' => 'absint', 'default' => 86400], ... ] */ // Define settings via constants in wp-config.php define('PURGELY_FASTLY_KEY', 'your-api-token-here'); define('PURGELY_FASTLY_SERVICE_ID', 'your-service-id'); define('PURGELY_SURROGATE_CONTROL_TTL', 86400); define('PURGELY_ENABLE_STALE_WHILE_REVALIDATE', true); define('PURGELY_DEFAULT_PURGE_TYPE', 'soft'); ``` -------------------------------- ### Configure Fastly WordPress Plugin via wp-config.php Constants Source: https://context7.com/fastly/wordpress-plugin/llms.txt This snippet demonstrates how to define constants in `wp-config.php` to configure the Fastly WordPress Plugin. It covers API credentials, cache Time-To-Live (TTL) settings, stale content handling, purge types, logging, multisite identification, cache tags, image optimization, webhook integration, and API limits. Ensure sensitive information like API tokens are kept secure. ```php add_key('custom-key'); // Add multiple keys at once $keys_header->add_keys(['key-1', 'key-2', 'key-3']); // Get all registered keys $keys = $keys_header->get_keys(); // Returns: ['p-123', 'custom-key', 'key-1', 'key-2', 'key-3'] // Get the formatted header value (space-separated, sanitized) $value = $keys_header->get_value(); // Returns: "p-123 custom-key key-1 key-2 key-3" // Send the header (called automatically during 'wp' action) $keys_header->send_header(); // Sends: Surrogate-Key: p-123 custom-key key-1 key-2 key-3 // For multisite or sitecode configurations, keys are prefixed: // If sitecode is 'mysite', keys become: "mysite-p-123 mysite-custom-key" ``` -------------------------------- ### Enable Fastly Image Optimization in WordPress Source: https://context7.com/fastly/wordpress-plugin/llms.txt This PHP code snippet shows how to enable and configure Fastly Image Optimization (IO) within WordPress by defining constants in `wp-config.php`. It also illustrates the automatic modification of image attributes for optimized output and lists available settings. ```php // Optimized: // Settings to configure: // io_enable_wp: Main switch for WordPress IO // io_adaptive_pixel_ratios: Enable adaptive pixel ratio srcset // io_adaptive_pixel_ratio_sizes: Array of DPR values ['2x', '3x'] // io_adaptive_pixel_ratios_content: Apply IO to content images (not just featured images) ``` -------------------------------- ### Perform Cache Purging Operations in PHP Source: https://context7.com/fastly/wordpress-plugin/llms.txt Illustrates how to use the Purgely_Purge class to invalidate cache by URL, surrogate key collection, or purge all content. It also shows how to retrieve available purge types and their corresponding constants. ```php purge(Purgely_Purge::URL, 'https://example.com/my-page/'); // Returns: true on success, false on failure // Purge by surrogate key collection $keys = ['p-123', 't-45', 'a-1']; // post ID 123, term ID 45, author ID 1 $result = $purgely->purge(Purgely_Purge::KEY_COLLECTION, $keys); // Purge all cached content (requires 'allow_purge_all' setting enabled) $result = $purgely->purge(Purgely_Purge::ALL, ''); // Get available purge types $types = Purgely_Purge::get_purge_types(); // Returns: ['all', 'url', 'key-collection'] // Purge type constants Purgely_Purge::ALL; // 'all' Purgely_Purge::URL; // 'url' Purgely_Purge::KEY_COLLECTION; // 'key-collection' ``` -------------------------------- ### Fastly API Class for Service Management (PHP) Source: https://context7.com/fastly/wordpress-plugin/llms.txt The Fastly_Api class allows direct interaction with the Fastly API to manage service versions, VCL snippets, ACLs, and dictionaries. It provides methods for cloning, validating, activating, and retrieving version data, as well as managing VCL snippets and other service configurations. ```php get_active_version(); // Returns object: { number: 5, active: true, ... } // Clone the active version for modifications $new_version = $api->clone_active_version(); // Returns object: { number: 6, active: false, ... } // Clone a specific version $cloned = $api->clone_version(5); // Validate a version before activation $is_valid = $api->validate_version(6); // Returns: true or false // Activate a version $activated = $api->activate_version(6); // Returns the activated version object // Get all VCL snippets for active version $snippets = $api->get_all_snippets(); // Returns array of snippet objects // Get a specific snippet $snippet = $api->get_snippet('wordpress_recv', 5); // Returns snippet object or error // Check if snippet exists $exists = $api->snippet_exists('wordpress_recv', 5); // Returns: true or false // Upload/update a VCL snippet $snippet_data = [ 'name' => 'custom_snippet', 'type' => 'recv', 'priority' => 100, 'content' => 'if (req.url ~ "/api/") { set req.http.X-API = \"true\"; }', 'dynamic' => 0 ]; $result = $api->upload_snippet(6, $snippet_data); // Delete a snippet $deleted = $api->delete_snippet(6, 'custom_snippet'); // Get all ACLs and dictionaries $acls = $api->get_all_acls(); $dictionaries = $api->get_all_dictionaries(); ``` -------------------------------- ### Test Fastly API Connection (PHP) Source: https://context7.com/fastly/wordpress-plugin/llms.txt This utility function verifies Fastly API credentials and connectivity. It takes hostname, service ID, and API key as input and returns a status indicating success or failure, along with a descriptive message. The connection result is cached within the plugin instance for the current request. ```php true, 'message' => 'Connection Successful on service *MyServiceName*' ] */ // Failure response: /* [ 'status' => false, 'message' => 'Please enter credentials first' // Or API error message ] */ // The result is cached in the plugin instance for the request $purgely = Purgely::instance(); echo $purgely->service_name; // 'MyServiceName' ``` -------------------------------- ### Fastly Webhook Integration for Slack Notifications (PHP) Source: https://context7.com/fastly/wordpress-plugin/llms.txt Enables the plugin to send notifications to Slack for cache purges or detected errors. Configuration can be done via plugin settings or constants in wp-config.php. It includes functions to send custom messages and test webhook connections, with automatic logging of purge events when activated. ```php true, 'message' => 'Connection Successful!'] Failure: ['status' => false, 'message' => 'Error message'] */ // Configure via constants in wp-config.php define('PURGELY_WEBHOOKS_URL_ENDPOINT', 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL'); define('PURGELY_WEBHOOKS_USERNAME', 'wordpress-bot'); define('PURGELY_WEBHOOKS_CHANNEL', 'cache-notifications'); define('PURGELY_WEBHOOKS_ACTIVATE', true); // Logging is handled automatically via handle_logging() // When webhooks_activate is true, purge events are sent to Slack ``` -------------------------------- ### Add Custom Surrogate Keys in WordPress with Fastly Source: https://github.com/fastly/wordpress-plugin/blob/master/readme.txt This snippet illustrates how to add custom surrogate keys to the cache using the 'purgely_pre_send_keys' action hook. Surrogate keys are used by Fastly to identify and purge specific cache objects. This function allows you to programmatically add custom keys that will be associated with the content being served. ```php add_action('purgely_pre_send_keys', 'custom_surrogate_keys'); function custom_surrogate_keys($keys_object) { $keys_object->add_key('custom-key'); } ``` -------------------------------- ### Generate Surrogate Keys for Current Request (PHP) Source: https://context7.com/fastly/wordpress-plugin/llms.txt The `Purgely_Surrogate_Key_Collection` class generates surrogate keys based on the current page's `WP_Query` object. These keys are automatically set during the WordPress 'wp' action but can be used manually for custom implementations. The class supports various key prefixes like 'p-' for Post ID, 't-' for Term ID, 'a-' for Author ID, and 'tm-' for Template type. ```php get_keys(); // Returns: ['p-123', 'p-456', 'tm-archive', 't-5', 'a-1'] // Key prefixes used: // 'p-' = Post ID (e.g., 'p-123') // 't-' = Term ID (e.g., 't-45') // 'a-' = Author ID (e.g., 'a-1') // 'tm-' = Template type (e.g., 'tm-single', 'tm-archive', 'tm-home') // Template types detected: $template_types = Purgely_Surrogate_Key_Collection::$types; /* ['single', 'preview', 'front_page', 'page', 'archive', 'date', 'year', 'month', 'day', 'time', 'author', 'category', 'tag', 'tax', 'search', 'feed', 'comment_feed', 'trackback', 'home', '404', 'paged', 'admin', 'attachment', 'singular', 'robots', 'posts_page', 'post_type_archive'] */ // Get custom TTL for template if configured $custom_ttl = $key_collection->get_custom_ttl(); ``` -------------------------------- ### Customize Caching Behavior with WordPress Hooks (PHP) Source: https://context7.com/fastly/wordpress-plugin/llms.txt The Purgely plugin offers various action and filter hooks to customize caching behavior, surrogate keys, and header management. These hooks allow developers to modify surrogate keys before sending, add custom related keys for purging, adjust TTL settings, and control cache control headers. They provide a flexible way to integrate custom logic into the plugin's caching mechanisms. ```php post_type === 'product') { $keys_array[] = 'all-products'; } return $keys_array; }, 10, 2); // Modify surrogate control headers (TTL settings) add_action('purgely_pre_send_surrogate_control', function($header_object) { // Set custom max-age for specific conditions if (is_single()) { $header_object->edit_headers(['max-age' => '3600']); // 1 hour } }); add_action('purgely_post_send_surrogate_control', function($header_object) { // Actions after surrogate control header is sent }); // Modify cache control headers add_action('purgely_pre_send_cache_control', function($header_object) { $header_object->edit_headers(['max-age' => '300']); // 5 minutes browser cache }); // Add custom surrogate keys before header is sent add_action('purgely_pre_send_keys', function($keys_object) { $keys_object->add_key('custom-key'); }); // Filter which taxonomies generate surrogate keys add_filter('purgely_taxonomy_keys', function($taxonomies) { // Remove specific taxonomies from key generation unset($taxonomies['post_tag']); return $taxonomies; }); ``` -------------------------------- ### Collect Related Surrogate Keys for a Post (PHP) Source: https://context7.com/fastly/wordpress-plugin/llms.txt The `Purgely_Related_Surrogate_Keys` class identifies all surrogate keys associated with a specific post, including taxonomy terms, author, and template keys. These keys are crucial for cache invalidation when content changes. The class returns keys chunked to adhere to API limits, and provides a static method to retrieve always-purged keys. ```php locate_all(); // Returns arrays of keys chunked for API limits: /* [ ['p-123', 't-5', 't-12', 'a-1', 'tm-post', 'tm-home', 'tm-front_page', 'tm-feed', 'holos', 'tm-404'], // Additional chunks if keys exceed FASTLY_MAX_HEADER_KEY_SIZE (256) ] */ // Get keys that are always purged (home, feed, front page, 404) $always_purged = Purgely_Related_Surrogate_Keys::get_always_purged_types(); /* Returns: ['tm-post', 'tm-home', 'tm-front_page', 'tm-feed', 'holos', 'tm-404'] */ // Automatic purging on post actions (handled internally) // These WordPress actions trigger automatic purging: // - save_post // - deleted_post // - trashed_post // - delete_attachment // - future_to_publish ``` -------------------------------- ### Edit HTTP Headers in WordPress with Fastly Source: https://github.com/fastly/wordpress-plugin/blob/master/readme.txt This snippet demonstrates how to modify outgoing HTTP headers using the 'purgely_pre_send_surrogate_control' action hook. It allows custom header values to be set, such as cache control directives or custom identifiers. Ensure other caching plugins are disabled to avoid conflicts. ```php add_action('purgely_pre_send_surrogate_control', 'custom_headers_edit'); function custom_headers_edit($header_object) { $header_object->edit_headers(array('custom-header' => '555', 'max-age' => '99')); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.