WordPress Performance at Lightning Speed. Advanced optimization with Brotli compression, smart caching, and cat-like speed bursts that dash past the competition. - Brotli and Gzip compression with configurable levels - WP Rocket-inspired smart caching - Bandwidth monitoring - Automatic optimization - Performance status dashboard Includes build.sh and .distignore for WordPress-installable release ZIPs.
372 lines
12 KiB
PHP
372 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* CDN Engine for TigerStyle Dash
|
|
* Global content delivery at cat-like speed with Vultr CDN integration
|
|
*/
|
|
|
|
// Prevent direct access
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class TigerStyle_Dash_CDN {
|
|
private static $instance = null;
|
|
|
|
/**
|
|
* CDN settings from database
|
|
* @var array
|
|
*/
|
|
private static $settings;
|
|
|
|
public static function instance() {
|
|
if (is_null(self::$instance)) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
private function __construct() {
|
|
$this->init_cdn();
|
|
}
|
|
|
|
/**
|
|
* Initialize CDN functionality
|
|
*/
|
|
public function init_cdn() {
|
|
// Load CDN settings
|
|
self::$settings = $this->get_cdn_settings();
|
|
|
|
// Admin hooks
|
|
if (is_admin()) {
|
|
add_action('admin_post_update_cdn_settings', array($this, 'handle_cdn_form_submission'));
|
|
add_action('wp_ajax_tigerstyle_test_cdn', array($this, 'ajax_test_cdn_connection'));
|
|
add_action('wp_ajax_tigerstyle_purge_cdn', array($this, 'ajax_purge_cdn_cache'));
|
|
}
|
|
|
|
// Only start URL rewriting if CDN is enabled and configured
|
|
if ($this->is_cdn_enabled() && !empty(self::$settings['cdn_url'])) {
|
|
$this->start_url_rewriting();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Start CDN URL rewriting
|
|
* Integrates with existing TigerStyle Dash performance buffer
|
|
*/
|
|
private function start_url_rewriting() {
|
|
// Hook into the performance engine's output buffer
|
|
add_filter('tigerstyle_dash_output_buffer', array($this, 'rewrite_urls_for_cdn'), 20);
|
|
|
|
// Also handle direct output if performance buffer isn't active
|
|
if (!get_option('tigerstyle_dash_compression_enabled', false)) {
|
|
add_action('template_redirect', array($this, 'start_cdn_buffer'), 5);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Start output buffering for CDN URL rewriting
|
|
*/
|
|
public function start_cdn_buffer() {
|
|
if ($this->should_process_request()) {
|
|
ob_start(array($this, 'process_cdn_output'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Process output for CDN URL rewriting
|
|
* @param string $content
|
|
* @return string
|
|
*/
|
|
public function process_cdn_output($content) {
|
|
return $this->rewrite_urls_for_cdn($content);
|
|
}
|
|
|
|
/**
|
|
* Rewrite URLs to point to CDN endpoints
|
|
* @param string $content
|
|
* @return string
|
|
*/
|
|
public function rewrite_urls_for_cdn($content) {
|
|
if (empty($content) || empty(self::$settings['cdn_url'])) {
|
|
return $content;
|
|
}
|
|
|
|
// Get site URL without protocol for flexible matching
|
|
$site_url = preg_replace('#^https?://#', '', home_url());
|
|
$cdn_url = rtrim(self::$settings['cdn_url'], '/');
|
|
|
|
// File extensions to rewrite
|
|
$extensions = $this->get_cdn_file_extensions();
|
|
$extensions_pattern = implode('|', array_map('preg_quote', $extensions));
|
|
|
|
// Build regex pattern for URL rewriting
|
|
$pattern = '#(?:https?:)?//(?:www\.)?' . preg_quote($site_url, '#') .
|
|
'(/[^"\'>\s]*\.(?:' . $extensions_pattern . '))(?:\?[^"\'>\s]*)?#i';
|
|
|
|
// Rewrite URLs to CDN
|
|
$content = preg_replace_callback($pattern, function($matches) use ($cdn_url) {
|
|
return $cdn_url . $matches[1];
|
|
}, $content);
|
|
|
|
return $content;
|
|
}
|
|
|
|
/**
|
|
* Get file extensions that should be served via CDN
|
|
* @return array
|
|
*/
|
|
private function get_cdn_file_extensions() {
|
|
$default_extensions = array(
|
|
'css', 'js', 'jpg', 'jpeg', 'png', 'gif', 'webp', 'avif',
|
|
'svg', 'ico', 'woff', 'woff2', 'ttf', 'eot', 'mp4', 'webm',
|
|
'pdf', 'zip', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx'
|
|
);
|
|
|
|
$custom_extensions = isset(self::$settings['file_extensions'])
|
|
? explode(',', str_replace(' ', '', self::$settings['file_extensions']))
|
|
: array();
|
|
|
|
return array_unique(array_merge($default_extensions, $custom_extensions));
|
|
}
|
|
|
|
/**
|
|
* Check if CDN should process this request
|
|
* @return bool
|
|
*/
|
|
private function should_process_request() {
|
|
// Skip admin pages
|
|
if (is_admin()) {
|
|
return false;
|
|
}
|
|
|
|
// Skip if user is logged in and preview mode is disabled
|
|
if (is_user_logged_in() && !self::$settings['enable_for_logged_in']) {
|
|
return false;
|
|
}
|
|
|
|
// Skip REST API requests
|
|
if (defined('REST_REQUEST') && REST_REQUEST) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get CDN settings with defaults
|
|
* @return array
|
|
*/
|
|
public function get_cdn_settings() {
|
|
$defaults = array(
|
|
'enabled' => false,
|
|
'provider' => 'vultr',
|
|
'cdn_url' => '',
|
|
'vultr_api_key' => '',
|
|
'file_extensions' => '',
|
|
'enable_for_logged_in' => false,
|
|
'purge_on_update' => true,
|
|
'test_mode' => false
|
|
);
|
|
|
|
$saved_settings = get_option('tigerstyle_dash_cdn_settings', array());
|
|
return wp_parse_args($saved_settings, $defaults);
|
|
}
|
|
|
|
/**
|
|
* Check if CDN is enabled
|
|
* @return bool
|
|
*/
|
|
public function is_cdn_enabled() {
|
|
return !empty(self::$settings['enabled']) && self::$settings['enabled'];
|
|
}
|
|
|
|
/**
|
|
* Handle CDN settings form submission
|
|
*/
|
|
public function handle_cdn_form_submission() {
|
|
// Verify nonce
|
|
if (!wp_verify_nonce($_POST['tigerstyle_dash_cdn_nonce'], 'tigerstyle_dash_cdn_settings')) {
|
|
wp_die(__('Security check failed.', 'tigerstyle-dash'));
|
|
}
|
|
|
|
// Check user permissions
|
|
if (!current_user_can('manage_options')) {
|
|
wp_die(__('You do not have permission to access this page.', 'tigerstyle-dash'));
|
|
}
|
|
|
|
// Sanitize and save settings
|
|
$settings = array(
|
|
'enabled' => isset($_POST['cdn_enabled']) ? (bool)$_POST['cdn_enabled'] : false,
|
|
'provider' => sanitize_text_field($_POST['cdn_provider'] ?? 'vultr'),
|
|
'cdn_url' => esc_url_raw($_POST['cdn_url'] ?? ''),
|
|
'vultr_api_key' => sanitize_text_field($_POST['vultr_api_key'] ?? ''),
|
|
'file_extensions' => sanitize_text_field($_POST['file_extensions'] ?? ''),
|
|
'enable_for_logged_in' => isset($_POST['enable_for_logged_in']) ? (bool)$_POST['enable_for_logged_in'] : false,
|
|
'purge_on_update' => isset($_POST['purge_on_update']) ? (bool)$_POST['purge_on_update'] : true,
|
|
'test_mode' => isset($_POST['test_mode']) ? (bool)$_POST['test_mode'] : false
|
|
);
|
|
|
|
update_option('tigerstyle_dash_cdn_settings', $settings);
|
|
|
|
// Update instance settings
|
|
self::$settings = $settings;
|
|
|
|
// Redirect back with success message
|
|
$redirect_url = add_query_arg(
|
|
array('page' => 'tigerstyle-dash', 'tab' => 'cdn', 'updated' => '1'),
|
|
admin_url('admin.php')
|
|
);
|
|
|
|
wp_redirect($redirect_url);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* AJAX handler for testing CDN connection
|
|
*/
|
|
public function ajax_test_cdn_connection() {
|
|
// Verify nonce
|
|
if (!wp_verify_nonce($_POST['nonce'], 'tigerstyle_dash_ajax')) {
|
|
wp_die('Security check failed');
|
|
}
|
|
|
|
$cdn_url = sanitize_text_field($_POST['cdn_url'] ?? '');
|
|
|
|
if (empty($cdn_url)) {
|
|
wp_send_json_error('CDN URL is required');
|
|
}
|
|
|
|
// Test CDN by trying to fetch a small asset
|
|
$test_url = rtrim($cdn_url, '/') . '/wp-includes/js/jquery/jquery.min.js';
|
|
$response = wp_remote_get($test_url, array('timeout' => 10));
|
|
|
|
if (is_wp_error($response)) {
|
|
wp_send_json_error('CDN test failed: ' . $response->get_error_message());
|
|
}
|
|
|
|
$response_code = wp_remote_retrieve_response_code($response);
|
|
if ($response_code === 200) {
|
|
wp_send_json_success('CDN connection successful! ⚡');
|
|
} else {
|
|
wp_send_json_error('CDN returned status code: ' . $response_code);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* AJAX handler for purging CDN cache
|
|
*/
|
|
public function ajax_purge_cdn_cache() {
|
|
// Verify nonce
|
|
if (!wp_verify_nonce($_POST['nonce'], 'tigerstyle_dash_ajax')) {
|
|
wp_die('Security check failed');
|
|
}
|
|
|
|
$result = $this->purge_cdn_cache();
|
|
|
|
if ($result) {
|
|
wp_send_json_success('CDN cache purged successfully! 🔥');
|
|
} else {
|
|
wp_send_json_error('Failed to purge CDN cache');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Purge CDN cache (provider-specific implementation)
|
|
* @return bool
|
|
*/
|
|
public function purge_cdn_cache() {
|
|
if (empty(self::$settings['provider']) || empty(self::$settings['vultr_api_key'])) {
|
|
return false;
|
|
}
|
|
|
|
switch (self::$settings['provider']) {
|
|
case 'vultr':
|
|
return $this->purge_vultr_cache();
|
|
default:
|
|
// For custom CDN providers, we can't purge programmatically
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Purge Vultr CDN cache via API
|
|
* @return bool
|
|
*/
|
|
private function purge_vultr_cache() {
|
|
$api_key = self::$settings['vultr_api_key'];
|
|
|
|
if (empty($api_key)) {
|
|
return false;
|
|
}
|
|
|
|
// Vultr CDN API endpoint for cache purging
|
|
$api_url = 'https://api.vultr.com/v2/cdn/purge';
|
|
|
|
$response = wp_remote_post($api_url, array(
|
|
'headers' => array(
|
|
'Authorization' => 'Bearer ' . $api_key,
|
|
'Content-Type' => 'application/json'
|
|
),
|
|
'body' => json_encode(array(
|
|
'files' => array('*') // Purge all files
|
|
)),
|
|
'timeout' => 30
|
|
));
|
|
|
|
if (is_wp_error($response)) {
|
|
return false;
|
|
}
|
|
|
|
$response_code = wp_remote_retrieve_response_code($response);
|
|
return $response_code >= 200 && $response_code < 300;
|
|
}
|
|
|
|
/**
|
|
* Get CDN analytics data
|
|
* @return array
|
|
*/
|
|
public function get_cdn_analytics() {
|
|
$analytics = array(
|
|
'enabled' => $this->is_cdn_enabled(),
|
|
'provider' => self::$settings['provider'] ?? 'none',
|
|
'cdn_url' => self::$settings['cdn_url'] ?? '',
|
|
'files_served' => 0,
|
|
'bandwidth_saved' => 0,
|
|
'performance_improvement' => 0
|
|
);
|
|
|
|
// Get basic metrics (enhanced in future versions)
|
|
if ($this->is_cdn_enabled()) {
|
|
$analytics['files_served'] = get_option('tigerstyle_dash_cdn_files_served', 0);
|
|
$analytics['bandwidth_saved'] = get_option('tigerstyle_dash_cdn_bandwidth_saved', 0);
|
|
}
|
|
|
|
return $analytics;
|
|
}
|
|
|
|
/**
|
|
* Get Vultr referral information
|
|
* @return array
|
|
*/
|
|
public function get_vultr_info() {
|
|
return array(
|
|
'referral_url' => 'https://supported.systems/vultr',
|
|
'description' => 'High-performance global CDN with lightning-fast edge locations worldwide',
|
|
'benefits' => array(
|
|
'⚡ Global edge network for cat-like speed',
|
|
'💰 Pay-as-you-go pricing with no minimums',
|
|
'🌍 24 worldwide locations for optimal performance',
|
|
'📊 Real-time analytics and monitoring',
|
|
'🔧 Simple API integration with TigerStyle Dash',
|
|
'💎 Enterprise-grade performance at affordable prices'
|
|
),
|
|
'setup_steps' => array(
|
|
'1. Sign up for Vultr via our referral link',
|
|
'2. Create a new CDN service in your Vultr dashboard',
|
|
'3. Copy your CDN URL and API key',
|
|
'4. Enter the details in TigerStyle Dash settings',
|
|
'5. Test the connection and start dashing!'
|
|
)
|
|
);
|
|
}
|
|
} |