cloudflare/Cloudflare-WordPress
Publicmirrored fromhttps://github.com/cloudflare/Cloudflare-WordPress
cloudflare.loader.php
67lines · modecode
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__.'/vendor/autoload.php'; |
| 4 | |
| 5 | use CloudFlare\IpRewrite; |
| 6 | |
| 7 | // Exit if accessed directly |
| 8 | if (!defined('ABSPATH')) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | // Rewrites Cloudflare IP |
| 13 | $ipRewrite = new IpRewrite(); |
| 14 | |
| 15 | $is_cf = $ipRewrite->isCloudFlare(); |
| 16 | if ($is_cf) { |
| 17 | // Fixes Flexible SSL |
| 18 | if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { |
| 19 | $_SERVER['HTTPS'] = 'on'; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // Initiliaze Hooks class which contains WordPress hook functions |
| 24 | $cloudflareHooks = new \CF\WordPress\Hooks(); |
| 25 | |
| 26 | // Enable HTTP2 Server Push |
| 27 | if (defined('CLOUDFLARE_HTTP2_SERVER_PUSH_ACTIVE') && CLOUDFLARE_HTTP2_SERVER_PUSH_ACTIVE) { |
| 28 | add_action('init', array($cloudflareHooks, 'http2ServerPushInit')); |
| 29 | } |
| 30 | |
| 31 | if (is_admin()) { |
| 32 | //Register proxy AJAX endpoint |
| 33 | add_action('wp_ajax_cloudflare_proxy', array($cloudflareHooks, 'initProxy')); |
| 34 | |
| 35 | //Add CloudFlare Plugin homepage to admin settings menu |
| 36 | add_action('admin_menu', array($cloudflareHooks, 'cloudflareConfigPage')); |
| 37 | |
| 38 | //Add CloudFlare Plugin homepage to admin settings menu |
| 39 | add_action('plugin_action_links_cloudflare/cloudflare.php', array($cloudflareHooks, 'pluginActionLinks')); |
| 40 | |
| 41 | // Load Activation Script |
| 42 | register_activation_hook(CLOUDFLARE_PLUGIN_DIR.'cloudflare.php', array($cloudflareHooks, 'activate')); |
| 43 | |
| 44 | // Load Deactivation Script |
| 45 | register_deactivation_hook(CLOUDFLARE_PLUGIN_DIR.'cloudflare.php', array($cloudflareHooks, 'deactivate')); |
| 46 | } |
| 47 | |
| 48 | // Load Automatic Cache Purge |
| 49 | $cloudflarePurgeEverythingActions = array( |
| 50 | 'autoptimize_action_cachepurged', // Compat with https://wordpress.org/plugins/autoptimize |
| 51 | 'switch_theme', // Switch theme |
| 52 | 'customize_save_after' // Edit theme |
| 53 | ); |
| 54 | |
| 55 | foreach ($cloudflarePurgeEverythingActions as $action) { |
| 56 | add_action($action, array($cloudflareHooks, 'purgeCacheEverything')); |
| 57 | } |
| 58 | |
| 59 | $cloudflarePurgeURLActions = array( |
| 60 | 'deleted_post', // Delete a post |
| 61 | 'edit_post', // Edit a post - includes leaving comments |
| 62 | 'delete_attachment', // Delete an attachment - includes re-uploading |
| 63 | ); |
| 64 | |
| 65 | foreach ($cloudflarePurgeURLActions as $action) { |
| 66 | add_action($action, array($cloudflareHooks, 'purgeCacheByRevelantURLs'), 10, 2); |
| 67 | } |