Skip to content

Commit 555a615

Browse files
committed
Initial commit
0 parents  commit 555a615

7 files changed

Lines changed: 1478 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

README.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
```

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "wp-forge/wp-update-handler",
3+
"description": "A WordPress package for updating plugins via a custom API.",
4+
"license": "GPL-2.0-or-later",
5+
"authors": [
6+
{
7+
"name": "Micah Wood",
8+
"email": "micah@wpscholar.com"
9+
}
10+
],
11+
"autoload": {
12+
"psr-4": {
13+
"WP_Forge\\WPUpdateHandler\\": "includes/"
14+
}
15+
},
16+
"require": {
17+
"wp-forge/helpers": "^1.1"
18+
},
19+
"require-dev": {
20+
"wpscholar/phpcs-standards-wpscholar": "^1.0"
21+
},
22+
"scripts": {
23+
"lint": [
24+
"vendor/bin/phpcs -s --standard=WPScholar ."
25+
],
26+
"fix": [
27+
"vendor/bin/phpcbf -s --standard=WPScholar ."
28+
]
29+
}
30+
}

0 commit comments

Comments
 (0)