|
| 1 | +# WordPress Update Handler |
| 2 | + |
| 3 | +A WordPress package for updating custom plugins and themes based on an JSON REST API response from a custom update |
| 4 | +server. |
| 5 | + |
| 6 | +Check out the [WordPress GitHub Release API](https://github.com/wp-forge/worker-wp-github-release-api) repository to |
| 7 | +learn how to quickly launch a custom update server that fetches releases from GitHub using Cloudflare Workers. |
| 8 | + |
| 9 | +## Plugins |
| 10 | + |
| 11 | +This package expects your custom plugin info API to respond with the same shape as |
| 12 | +the [WordPress plugin info API](https://codex.wordpress.org/WordPress.org_API#Plugins). However, if your API response |
| 13 | +has a different shape, you can map fields to those returned by your API. |
| 14 | + |
| 15 | +### Usage |
| 16 | + |
| 17 | +Basic example: |
| 18 | + |
| 19 | +```php |
| 20 | +<?php |
| 21 | +/** |
| 22 | + * Plugin Name: My Plugin |
| 23 | + */ |
| 24 | + |
| 25 | +require __DIR__ . '/vendor/autoload.php'; |
| 26 | + |
| 27 | +use WP_Forge\WPUpdateHandler\PluginUpdater; |
| 28 | + |
| 29 | +$url = 'https://my-update-api.com/plugins/plugin-name'; // Custom API GET endpoint |
| 30 | + |
| 31 | +new PluginUpdater( __FILE__, $url ); |
| 32 | + |
| 33 | +``` |
| 34 | + |
| 35 | +Advanced example with data mapping and data overrides: |
| 36 | + |
| 37 | +```php |
| 38 | +<?php |
| 39 | +/** |
| 40 | + * Plugin Name: My Plugin |
| 41 | + */ |
| 42 | + |
| 43 | +require __DIR__ . '/vendor/autoload.php'; |
| 44 | + |
| 45 | +use WP_Forge\WPUpdateHandler\PluginUpdater; |
| 46 | + |
| 47 | +$file = __FILE__; // Can be absolute path to main plugin file, or the plugin basename. |
| 48 | +$url = 'https://my-update-api.com/plugins/plugin-name'; // Custom API GET endpoint |
| 49 | + |
| 50 | +$pluginUpdater = new PluginUpdater( $file, $url ); |
| 51 | + |
| 52 | +/* |
| 53 | + * Keys are the fields that WordPress is expecting (look at the WP Plugin Info API response). |
| 54 | + * Values are the keys returned by your custom API. |
| 55 | + * |
| 56 | + * Use dot notation to map nested keys. |
| 57 | + */ |
| 58 | +$pluginUpdater->setDataMap( |
| 59 | + [ |
| 60 | + 'requires' => 'requires.wp', |
| 61 | + 'requires' => 'requires.php', |
| 62 | + 'banners.2x' => 'banners.retina', |
| 63 | + ] |
| 64 | +); |
| 65 | + |
| 66 | +/* |
| 67 | + * Explicitly set specific values that will be provided to WordPress. |
| 68 | + */ |
| 69 | +$pluginUpdater->setDataOverrides( |
| 70 | + [ |
| 71 | + 'banners' => [ |
| 72 | + '2x' => 'https://my.cdn.com/banner-123-retina.jpg', |
| 73 | + '1x' => 'https://my.cdn.com/banner-123.jpg', |
| 74 | + ], |
| 75 | + 'icons' => [ |
| 76 | + '2x' => 'https://my.cdn.com/icon-123-retina.jpg', |
| 77 | + '1x' => 'https://my.cdn.com/icon-123.jpg', |
| 78 | + ], |
| 79 | + ] |
| 80 | +); |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +## Themes |
| 85 | + |
| 86 | +This package expects your custom theme info API to respond with the same shape as |
| 87 | +the [WordPress theme info API](https://codex.wordpress.org/WordPress.org_API#Themes). However, if your API response has |
| 88 | +a different shape, you can map fields to those returned by your API. |
| 89 | + |
| 90 | +### Usage |
| 91 | + |
| 92 | +Basic example: |
| 93 | + |
| 94 | +```php |
| 95 | +<?php |
| 96 | +/** |
| 97 | + * Theme Name: My Theme |
| 98 | + */ |
| 99 | + |
| 100 | +require __DIR__ . '/vendor/autoload.php'; |
| 101 | + |
| 102 | +use WP_Forge\WPUpdateHandler\ThemeUpdater; |
| 103 | + |
| 104 | +$url = 'https://my-update-api.com/theme/theme-name'; // Custom API GET endpoint |
| 105 | + |
| 106 | +new ThemeUpdater( wp_get_theme('my-theme'), $url ); |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +Advanced example with data mapping and data overrides: |
| 111 | + |
| 112 | +```php |
| 113 | +<?php |
| 114 | +/** |
| 115 | + * Theme Name: My Theme |
| 116 | + */ |
| 117 | + |
| 118 | +require __DIR__ . '/vendor/autoload.php'; |
| 119 | + |
| 120 | +use WP_Forge\WPUpdateHandler\ThemeUpdater; |
| 121 | + |
| 122 | +$theme = wp_get_theme('my-theme'); // Get the theme's WP_Theme instance. |
| 123 | +$url = 'https://my-update-api.com/themes/theme-name'; // Custom API GET endpoint |
| 124 | + |
| 125 | +$themeUpdater = new ThemeUpdater( $file, $url ); |
| 126 | + |
| 127 | +/* |
| 128 | + * Keys are the fields that WordPress is expecting (look at the WP Theme Info API response). |
| 129 | + * Values are the keys returned by your custom API. |
| 130 | + * |
| 131 | + * Use dot notation to map nested keys. |
| 132 | + */ |
| 133 | +$themeUpdater->setDataMap( |
| 134 | + [ |
| 135 | + 'requires' => 'requires.wp', |
| 136 | + 'requires' => 'requires.php', |
| 137 | + 'banners.2x' => 'banners.retina', |
| 138 | + ] |
| 139 | +); |
| 140 | + |
| 141 | +/* |
| 142 | + * Explicitly set specific values that will be provided to WordPress. |
| 143 | + */ |
| 144 | +$themeUpdater->setDataOverrides( |
| 145 | + [ |
| 146 | + 'banners' => [ |
| 147 | + '2x' => 'https://my.cdn.com/banner-123-retina.jpg', |
| 148 | + '1x' => 'https://my.cdn.com/banner-123.jpg', |
| 149 | + ], |
| 150 | + 'icons' => [ |
| 151 | + '2x' => 'https://my.cdn.com/icon-123-retina.jpg', |
| 152 | + '1x' => 'https://my.cdn.com/icon-123.jpg', |
| 153 | + ], |
| 154 | + ] |
| 155 | +); |
| 156 | + |
| 157 | +``` |
0 commit comments