-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheygov.php
127 lines (98 loc) · 3.28 KB
/
heygov.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
Plugin Name: HeyGov
Plugin URI: https://heygov.com
Description: Manage the HeyGov widget on your municipality WordPress website
Version: 1.8.2
Requires at least: 5.0
Requires PHP: 7.0
Author: HeyGov
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
define('HEYGOV_URL', plugin_dir_url( __FILE__ ));
define('HEYGOV_DIR', plugin_dir_path( __FILE__ ));
require_once 'includes/class/heygov-resource.php';
require_once 'includes/class/heygov-settings.php';
require_once 'includes/class/events-venues-integration.php';
function heyGovID() {
return get_option('heygov_id');
}
function heyGovVenues($forceUpdate = false) {
$venues = [];
// If no HeyGovID, return empty array
if (!heyGovID()) {
return $venues;
}
$forceUpdate = isset($_REQUEST['heygov-refresh-venues']) || $forceUpdate;
if ( false === ( $venues = get_transient( 'heygov-venues' ) ) || $forceUpdate ) {
// It wasn't there, so regenerate the data and save the transient
$venues = wp_remote_get('https://api.heygov.com/' . heyGovID() . '/venues?orderBy=name&order=asc');
if (is_wp_error($venues)) {
$venues = [];
} else {
$venues = wp_remote_retrieve_body($venues);
$venues = json_decode($venues);
set_transient( 'heygov-venues', $venues, 12 * HOUR_IN_SECONDS );
}
}
return $venues;
}
function heygov_validate_id(string $id) {
if (empty($id)) {
return new \WP_Error('heygov_empty_id', 'HeyGov ID is empty');
}
$id = strtolower(trim($id));
$re = wp_remote_get('https://api.heygov.com/' . $id);
if (is_wp_error($re)) {
return $re;
}
$data = wp_remote_retrieve_body($re);
$data = json_decode($data);
if (isset($data->slug)) {
$id = $data->slug;
} else {
return new \WP_Error('heygov_invalid_id', 'HeyGov ID is invalid');
}
return $id;
}
class HeyGov {
public function __construct() {
$setting = new HeyGovSettings();
$resource = new HeyGovResource();
new HeyGovVenuesIntegration();
if (is_admin()) {
add_action('admin_menu', array($setting, 'add_admin_menu'));
add_action('admin_enqueue_scripts', array($resource, 'load_admin_includes'));
} else {
add_action('wp_enqueue_scripts', array($resource, 'load_site_includes'));
add_action('wp_footer', array($resource, 'load_widget'));
add_action('wp_footer', array($resource, 'load_apps_banner'));
add_shortcode('heygov-widget', array($setting, 'heygov_shortcode'));
add_shortcode('heygov-forms', array($resource, 'heygov_forms_shortcode'));
add_shortcode('heygov-venue', array($resource, 'heygov_venue_shortcode'));
}
add_filter('plugin_action_links_heygov/heygov.php', [$this, 'actionLinks']);
add_action('init', 'heygov_load_module');
add_action('rest_api_init', array($resource, 'register_api'));
}
public function actionLinks(array $links) {
return array_merge([
'settings' => '<a href="' . menu_page_url('heygov_settings', false) . '">' . __('Settings', 'heygov') . '</a>'
], $links);
}
public function enablePlugin() {
$id = heygov_validate_id($_SERVER['HTTP_HOST']);
if (!is_wp_error($id)) {
update_option('heygov_id', $id);
}
}
}
function heygov_load_module() {
if ( class_exists( 'FLBuilder' ) ) {
require_once 'pagebuilder-module/heygov-pb-module.php';
}
}
// Start the plugin
$heygov = new HeyGov;
register_activation_hook(__FILE__, [$heygov, 'enablePlugin']);