-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.php
executable file
·71 lines (60 loc) · 1.95 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* @wordpress-plugin
* Plugin Name: Agreable Venues Plugin
* Plugin URI: https://bitbucket.org/ShortlistMedia/agreable-venues-plugin
* Description: Consume venues from Venues CMS (Parse) and render on map.
* Version: 0.1.0
* Author: Shortlist Media
* Author URI: http://shortlistmedia.co.uk
* License: MIT
*/
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ServerException;
use AgreableVenuesPlugin\Helper;
class AgreableVenuesPlugin
{
const FILE_LOCATION = "/tmp/kitchin-brand-response.json";
public function __construct()
{
$ns = Helper::get('agreable_namespace');
add_filter("acf/load_field/key={$ns}_plugin_brand", array($this, 'loadBrands'), 11, 3);
add_action('acf/save_post', array($this, 'clearCachedResponse'), 20);
}
public function clearCachedResponse() {
if (function_exists('get_current_screen')) {
$screen = get_current_screen();
if (isset($screen->id) && (strpos($screen->id, "acf-options") == true)) {
if (file_exists($this::FILE_LOACTION)) {
unlink($this::FILE_LOCATION);
}
}
}
}
public function loadBrands($field) {
$baseUri = getenv('KITCHIN_API');
$client = new Client([
'base_uri' => $baseUri,
'timeout' => 10.0
]);
try {
if (file_exists($this::FILE_LOCATION)) {
$body = file_get_contents($this::FILE_LOCATION);
} else {
$response = $client->get(
'api/v1/brand'
);
$body = (string) $response->getBody();
file_put_contents($this::FILE_LOCATION, $body);
}
$responseObject = json_decode($body, true, JSON_PRETTY_PRINT);
foreach ($responseObject as $key => $brand) {
$field['choices'][$brand['id']] = $brand['name'];
}
} catch (ServerException $exception) {
$field['choices'][1] = "Loading error. The default Brand will be used.";
}
return $field;
}
}
new AgreableVenuesPlugin();