Skip to content

Instantly share code, notes, and snippets.

@jb510
Last active September 26, 2024 05:23
Show Gist options
  • Save jb510/1ffd6a317da62c3910ae19268ef436be to your computer and use it in GitHub Desktop.
Save jb510/1ffd6a317da62c3910ae19268ef436be to your computer and use it in GitHub Desktop.
WordPress Plugin to use CloudFlare Worker Proxy
<?php
/*
Plugin Name: Cloudflare Proxy for WordPress Updates
Description: Proxy WordPress.org theme and plugin update requests via a Cloudflare Worker.
Version: 1.0.0
Author: 9seeds, Jon Brown
Author URI: https://9seeds.com/
License: GPL-2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
Copyright: 2024 Jon Brown
Text Domain: wporg-cf-proxy
*/
/* NOTE: this plugin has no settings page yet, it's mostly a POC.
* To use it you must update <your-worker> everywhere it appears below with the name of your worker
* see https://gist.github.com/jb510/b898c75df7879c38ba052d97cc9a04ff for an example of the worker
*/
function cf_update_proxy_plugins($update, $plugin_data, $plugin_file) {
// Define the Cloudflare Worker URL
$proxy_url = 'https://<your-worker>.workers.dev/plugins/';
// Build the API request URL for the plugin info
$plugin_slug = dirname($plugin_file);
$url = $proxy_url . $plugin_slug;
// Make a request to the Cloudflare Worker
$response = wp_remote_get($url);
if (is_wp_error($response)) {
return false;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (empty($data)) {
return false;
}
return $data;
}
add_filter('plugins_api', 'cf_update_proxy_plugins', 10, 3);
function cf_update_proxy_themes($response, $theme_slug) {
// Define the Cloudflare Worker URL
$proxy_url = 'https://<your-worker>.workers.dev/themes/';
// Build the API request URL for the theme info
$url = $proxy_url . $theme_slug;
// Make a request to the Cloudflare Worker
$response = wp_remote_get($url);
if (is_wp_error($response)) {
return false;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (empty($data)) {
return false;
}
return $data;
}
add_filter('themes_api', 'cf_update_proxy_themes', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment