options = get_option('tigerstyle_heat_amp', array());
$this->cache_dir = WP_CONTENT_DIR . '/cache/tigerstyle-heat-amp/';
$this->sxg_enabled = isset($this->options['sxg_enabled']) ? $this->options['sxg_enabled'] : false;
$this->init();
}
/**
* Initialize the module
*/
private function init() {
// Create cache directory
if (!file_exists($this->cache_dir)) {
wp_mkdir_p($this->cache_dir);
}
// AMP detection and generation
add_action('template_redirect', array($this, 'handle_amp_request'));
add_action('wp_head', array($this, 'add_amp_link'));
// SXG support
if ($this->sxg_enabled) {
add_action('template_redirect', array($this, 'handle_sxg_request'), 5);
add_filter('wp_headers', array($this, 'add_sxg_headers'));
}
// Admin hooks
if (is_admin()) {
add_action('admin_post_update_amp_settings', array($this, 'handle_form_submission'));
}
}
/**
* Handle AMP requests
*/
public function handle_amp_request() {
if (!isset($_GET['amp']) || is_admin()) {
return;
}
// Generate AMP page
$this->generate_amp_page();
exit;
}
/**
* Add AMP link to head
*/
public function add_amp_link() {
if (is_singular() && !is_admin()) {
$amp_url = add_query_arg('amp', '1', get_permalink());
echo '' . "\n";
}
}
/**
* Generate AMP page
*/
private function generate_amp_page() {
// Basic AMP page structure
?>
sxg_enabled) {
return;
}
// Check if SXG service is available
if (!$this->is_sxg_service_available()) {
wp_die('SXG service not available', 'Service Unavailable', ['response' => 503]);
}
$this->serve_signed_exchange();
exit;
}
/**
* Check if SXG service is available
*/
private function is_sxg_service_available() {
return !empty($this->options['sxg_api_key']) && !empty($this->options['sxg_api_endpoint']);
}
/**
* Serve Signed Exchange
*/
public function serve_signed_exchange() {
$current_url = $this->get_current_url();
// Check cache first
$cache_key = 'sxg_' . md5($current_url);
$cached_sxg = get_transient($cache_key);
if ($cached_sxg !== false) {
header('Content-Type: application/signed-exchange;v=b3');
header('Cache-Control: public, max-age=300');
echo $cached_sxg;
return;
}
// Generate fresh SXG
ob_start();
$this->generate_amp_page();
$amp_content = ob_get_clean();
$sxg_data = $this->create_signed_exchange($amp_content, $current_url);
if ($sxg_data) {
// Cache for 5 minutes
set_transient($cache_key, $sxg_data, 300);
header('Content-Type: application/signed-exchange;v=b3');
header('Cache-Control: public, max-age=300');
echo $sxg_data;
} else {
wp_die('Failed to create signed exchange', 'SXG Error', ['response' => 500]);
}
}
/**
* Create signed exchange using API
*/
private function create_signed_exchange($content, $url) {
$api_endpoint = $this->options['sxg_api_endpoint'];
$api_key = $this->options['sxg_api_key'];
$response = wp_remote_post($api_endpoint, array(
'headers' => array(
'Authorization' => 'Bearer ' . $api_key,
'Content-Type' => 'application/json'
),
'body' => json_encode(array(
'url' => $url,
'content' => $content,
'headers' => array(
'content-type' => 'text/html; charset=utf-8'
)
)),
'timeout' => 30
));
if (is_wp_error($response)) {
error_log('TigerStyle Heat: SXG API error: ' . $response->get_error_message());
return false;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (isset($data['sxg_package'])) {
return base64_decode($data['sxg_package']);
}
return false;
}
/**
* Get current URL
*/
private function get_current_url() {
return (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
/**
* Add SXG headers
*/
public function add_sxg_headers($headers) {
if ($this->sxg_enabled && isset($_GET['amp'])) {
$headers['Link'] = '; rel="alternate"; type="application/cert-chain+cbor"';
$headers['Vary'] = 'Accept, AMP-Cache-Transform';
}
return $headers;
}
/**
* Handle form submission
*/
public function handle_form_submission() {
if (!isset($_POST['amp_nonce']) || !wp_verify_nonce($_POST['amp_nonce'], 'update_amp_settings')) {
wp_die('Security check failed');
}
$options = array();
$options['sxg_enabled'] = isset($_POST['sxg_enabled']) ? 1 : 0;
$options['sxg_api_endpoint'] = sanitize_url($_POST['sxg_api_endpoint']);
$options['sxg_api_key'] = sanitize_text_field($_POST['sxg_api_key']);
update_option('tigerstyle_heat_amp', $options);
wp_redirect(admin_url('admin.php?page=tigerstyle-heat#amp-tab&updated=1'));
exit;
}
/**
* Render admin page
*/
public function render_admin_page() {
$options = $this->options;
?>