-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspaceapi-widget.php
97 lines (84 loc) · 2.81 KB
/
spaceapi-widget.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
<?php
/*
Plugin Name: SpaceAPI Widget
Description: This plugin provides a widget that can display data from a Space API endpoint.
Author: Written by Coredump hackerspace in Switzerland(https://www.coredump.ch).
License: GPL version 2 or later.
*/
// Load Twig template library
require_once 'vendor/Twig/Autoloader.php';
Twig_Autoloader::register();
// Constants
define('PLUGIN_ID', 'space_api_widget');
define('PLUGIN_NAME', 'SpaceAPI Widget');
define('PLUGIN_DOMAIN', 'coredump.ch');
define('WIDGET_DESCRIPTION', 'Display data from a Space API endpoint.');
// Widget class
class SpaceApiWidget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of the widget
PLUGIN_ID,
// Widget name, appears in the UI
__(PLUGIN_NAME, PLUGIN_DOMAIN),
// Widget description
array('description' => __(WIDGET_DESCRIPTION, PLUGIN_DOMAIN))
);
$loader = new Twig_Loader_Filesystem(dirname(__FILE__) . '/templates');
$this->twig = new Twig_Environment($loader, array(
'cache' => false,//dirname(__FILE__) . '/cache'
));
}
// Widget frontend
public function widget($args, $instance) {
// Enqueue JavaScript
wp_enqueue_script(
'space_api_query',
plugins_url('/js/spaceapi.js', __FILE__),
array('jquery')
);
wp_localize_script(
'space_api_query',
'php_vars',
$instance
);
// Render template
$template = $this->twig->loadTemplate('widget.html');
$title = apply_filters('widget_title', $instance['title']);
echo $template->render(array(
'title' => $title,
'args' => $args,
'instance' => $instance
));
}
// Widget backend
public function form($instance) {
$template = $this->twig->loadTemplate('form.html');
if (isset($instance['title'])) {
$title = $instance['title'];
} else {
$title = __('Space API Status', PLUGIN_DOMAIN);
}
$url = isset($instance['url']) ? $instance['url'] : '';
echo $template->render(array(
'title' => array(
'value' => esc_attr($title),
'id' => $this->get_field_id('title'),
'name' => $this->get_field_name('title'),
'label' => 'Title'
),
'url' => array(
'value' => esc_attr($url),
'id' => $this->get_field_id('url'),
'name' => $this->get_field_name('url'),
'label' => 'API URL'
)
));
}
}
// Loading function
function spaceapi_load_widget() {
register_widget('SpaceApiWidget');
}
add_action('widgets_init', 'spaceapi_load_widget');
?>