-
Notifications
You must be signed in to change notification settings - Fork 0
/
election-engine.php
120 lines (107 loc) · 3.8 KB
/
election-engine.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
<?php
/**
* Plugin Name: Election Engine
* Description: Visualisations for the South African general elections.
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: 0.9.1
* Author: 10Layer
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: election-engine
*
* @package CreateBlock
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
define("ELECTIONENGINE_VERSION", '0.9.1');
define("ELECTIONENGINE_DIST_DIR", __DIR__ . '/packages/election-engine-wordpress-block/dist/');
define("ELECTIONENGINE_DIST_URL", plugins_url('packages/election-engine-wordpress-block/dist/', __FILE__));
define("ELECTIONENGINE_BLOCK_NAME", 'tenlayer/election-engine');
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function election_engine_init()
{
$dir = __DIR__;
wp_register_script(
'election-engine-app',
ELECTIONENGINE_DIST_URL . "app/election-engine.umd.js",
array('wp-blocks', 'wp-element', 'wp-editor', 'jquery'),
filemtime(ELECTIONENGINE_DIST_DIR . "app/election-engine.umd.js")
);
wp_register_script(
'election-engine-admin',
ELECTIONENGINE_DIST_URL . "admin/election-engine-admin.umd.js",
array('wp-blocks', 'wp-element', 'wp-editor', 'jquery'),
filemtime(ELECTIONENGINE_DIST_DIR . "admin/election-engine-admin.umd.js")
);
register_block_type($dir . '/packages/election-engine-wordpress-block/build');
}
add_action('init', 'election_engine_init');
function elections_engine_enqueue_script()
{
if (!has_block(ELECTIONENGINE_BLOCK_NAME)) return;
wp_enqueue_script('election-engine-app');
wp_enqueue_style(
'election-engine-app',
ELECTIONENGINE_DIST_URL . "app/style.css",
array(),
filemtime(ELECTIONENGINE_DIST_DIR . "app/style.css")
);
}
add_action('wp_enqueue_scripts', 'elections_engine_enqueue_script', 9999);
function elections_engine_enqueue_admin_script()
{
wp_enqueue_script('election-engine-admin');
wp_enqueue_style(
'election-engine-admin',
ELECTIONENGINE_DIST_URL . "admin/election-engine-admin.css",
array(),
filemtime(ELECTIONENGINE_DIST_DIR . "admin/election-engine-admin.css")
);
// Add site_url to the script
wp_localize_script('election-engine-admin', 'election_engine_admin', array('site_url' => site_url()));
}
add_action('admin_enqueue_scripts', 'elections_engine_enqueue_admin_script', 9999);
function elections_engine_insert_admin_div()
{
?>
<div id="ElectionsEngineAdminModal"></div>
<?php
}
add_action('admin_footer', 'elections_engine_insert_admin_div');
function electionengine_shortcode($atts)
{
$atts = shortcode_atts(array(
'visualisation' => 'hemicycle',
'selected_election' => 'National Assembly',
'selected_year' => '2019',
'selected_region' => 'National',
'selected_ballot' => 'Combined',
'show_title' => 'true',
'show_blurb' => 'true',
'show_buttons' => 'false',
), $atts);
require_once plugin_dir_path(__FILE__) . 'packages/election-engine-wordpress-block/php/views/election-engine-shortcode-view.php';
$election_view = new ElectionEngineShortcodeView($atts);
return $election_view->generate();
}
add_shortcode('election-engine', 'electionengine_shortcode');
function electionengine_media_button()
{
require_once plugin_dir_path(__FILE__) . 'packages/election-engine-wordpress-block/php/views/election-engine-mediabutton.php';
}
add_action('media_buttons', 'electionengine_media_button');
// Serve Embed
function embed_init()
{
require_once(plugin_dir_path(__FILE__) . 'packages/election-engine-wordpress-block/php/embed.php');
new ElectionEngineEmbed();
}
add_action('init', 'embed_init');