|
| 1 | +<?php |
| 2 | +// Disable a plugin based on environment. We don't want to purge cache during local/staging development |
| 3 | +// unless needed for specific testing, in which case we can enable it using global variables in functions.php |
| 4 | +// Also allows for listening to graphcdn_purge event to perform additional invalidation |
| 5 | + |
| 6 | +// add_action('admin_init', 'disable_stellate_plugin'); |
| 7 | + |
| 8 | + |
| 9 | +function disable_stellate_plugin() |
| 10 | +{ |
| 11 | + $plugin_slug = 'stellate/wp-stellate.php'; |
| 12 | + if (!defined('WP_STELLATE_ENABLED')) { |
| 13 | + if (defined('WP_HOST') && WP_HOST == "production") { |
| 14 | + global $stellate_production_enabled; |
| 15 | + if ($stellate_production_enabled) { |
| 16 | + define('WP_STELLATE_ENABLED', true); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + if (defined('WP_HOST') && WP_HOST == "staging") { |
| 21 | + global $stellate_staging_enabled; |
| 22 | + if ($stellate_staging_enabled) { |
| 23 | + define('WP_STELLATE_ENABLED', true); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + if (defined('WP_HOST') && WP_HOST == "development") { |
| 28 | + global $stellate_development_enabled; |
| 29 | + if ($stellate_development_enabled) { |
| 30 | + define('WP_STELLATE_ENABLED', true); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + if (defined('WP_STELLATE_ENABLED') && WP_STELLATE_ENABLED) { |
| 37 | + // leave enabled |
| 38 | + } else { |
| 39 | + // disable |
| 40 | + deactivate_plugins($plugin_slug); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +global $stellate_purge_redirection; |
| 45 | +global $stellate_purge_acf_options; |
| 46 | + |
| 47 | +if ($stellate_purge_redirection) { |
| 48 | + function purge_redirection() |
| 49 | + { |
| 50 | + stellate_add_purge_entity('purged_types', 'RedirectionRedirects'); |
| 51 | + } |
| 52 | + add_action('redirection_redirect_updated', 'purge_redirection'); |
| 53 | + add_action('redirection_redirect_deleted', 'purge_redirection'); |
| 54 | +} |
| 55 | + |
| 56 | +if ($stellate_purge_acf_options) { |
| 57 | + function purge_acf_options() |
| 58 | + { |
| 59 | + stellate_add_purge_entity('purged_types', 'AcfOptionsThemeSettings'); |
| 60 | + } |
| 61 | + add_action('acf/options_page/save', 'purge_acf_options'); |
| 62 | +} |
| 63 | + |
| 64 | +add_action('redirection_redirect_updated', 'purge_redirection'); |
| 65 | +add_action('redirection_redirect_deleted', 'purge_redirection'); |
| 66 | +add_action('acf/options_page/save', 'purge_acf_options'); |
| 67 | + |
| 68 | + |
| 69 | +function vercel_revalidate($urls) |
| 70 | +{ |
| 71 | + global $headless_domain; |
| 72 | + |
| 73 | + if (defined('WP_ENV') && WP_ENV == 'development') { |
| 74 | + $api_domain = 'http://host.docker.internal:3000'; |
| 75 | + } else { |
| 76 | + $api_domain = $headless_domain; |
| 77 | + } |
| 78 | + |
| 79 | + $paths = []; |
| 80 | + |
| 81 | + if (!defined('HEADLESS_REVALIDATE_SECRET')) { |
| 82 | + error_log('HEADLESS_REVALIDATE_SECRET not defined'); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + foreach ($urls as $url) { |
| 87 | + if (strpos($url, '://')) { |
| 88 | + $parsed = parse_url($url); |
| 89 | + $url = $parsed['path']; |
| 90 | + } |
| 91 | + if ($url !== '/') { |
| 92 | + $url = rtrim($url, '/'); // Remove trailing slash |
| 93 | + } |
| 94 | + array_push($paths, $url); |
| 95 | + } |
| 96 | + |
| 97 | + $post_url = $api_domain . '/api/revalidate'; |
| 98 | + |
| 99 | + $body = array( |
| 100 | + 'paths' => $paths, |
| 101 | + 'secret' => HEADLESS_REVALIDATE_SECRET |
| 102 | + ); |
| 103 | + |
| 104 | + $args = array( |
| 105 | + 'body' => json_encode($body), |
| 106 | + 'headers' => array('Content-Type' => 'application/json') |
| 107 | + ); |
| 108 | + |
| 109 | + wp_remote_post($post_url, $args); |
| 110 | +} |
| 111 | + |
| 112 | +function stellate_purge_callback($purge_data) |
| 113 | +{ |
| 114 | + $paths = []; |
| 115 | + |
| 116 | + // Uncomment to add logs to debug.log |
| 117 | + // error_log('attempting handle_stellate_purge'); |
| 118 | + // error_log(json_encode($purge_data)); |
| 119 | + |
| 120 | + if ($purge_data['has_purged_all']) { |
| 121 | + $query = new WP_Query(array('posts_per_page' => -1, 'fields' => 'ids')); |
| 122 | + foreach ($query as $id) { |
| 123 | + array_push($paths, get_permalink($id)); |
| 124 | + } |
| 125 | + vercel_revalidate($paths); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + // Process purged types |
| 130 | + foreach ($purge_data['purged_types'] as $type) { |
| 131 | + $path = get_post_type_archive_link($type); |
| 132 | + if ($path) { |
| 133 | + array_push($paths, $path); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (isset($purge_data['purged_types'])) { |
| 138 | + unset($purge_data['purged_types']); |
| 139 | + } |
| 140 | + |
| 141 | + foreach ($purge_data as $type => $ids) { |
| 142 | + if (is_array($ids) && !empty($ids)) { |
| 143 | + foreach ($ids as $id) { |
| 144 | + $path = get_permalink($id); |
| 145 | + if ($path) { |
| 146 | + array_push($paths, $path); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + vercel_revalidate($paths); |
| 153 | +} |
| 154 | + |
| 155 | +add_action('stellate_purge', 'stellate_purge_callback', 10, 2); |
0 commit comments