-
Notifications
You must be signed in to change notification settings - Fork 3
/
blocks-for-eventbrite.php
137 lines (123 loc) · 4.17 KB
/
blocks-for-eventbrite.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
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Plugin Name: Blocks for Eventbrite
* Description: Gutenberg blocks that display eventbrite events
* Version: 1.1.3
* Author: Jon Waldstein
* Author URI: https://jonwaldstein.com
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: blocks-for-eventbrite
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
// Setup constants
const BLOCKS_FOR_EVENTBRITE_SCRIPT_ASSET_PATH = __DIR__ . '/build/index.asset.php';
define("BLOCKS_FOR_EVENTBRITE_SCRIPT_ASSET", require(BLOCKS_FOR_EVENTBRITE_SCRIPT_ASSET_PATH));
const BLOCKS_FOR_EVENTBRITE_INDEX_JS = 'build/index.js';
const BLOCKS_FOR_EVENTBRITE_LOCALIZED_SCRIPT_NAME = 'blocksForEventbrite';
const BLOCKS_FOR_EVENTBRITE_SCRIPT_NAME = 'blocks-for-eventbrite-script';
// include class with no auto-loading
include_once('src/api/RenderBlocksForEventbriteCard.php');
/**
* Registers all block assets so that they can be enqueued through the block editor
* in the corresponding context.
*
* @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/applying-styles-with-stylesheets/
*/
add_action('init', function () {
if (!file_exists(BLOCKS_FOR_EVENTBRITE_SCRIPT_ASSET_PATH)) {
throw new Error(
'Block script not found. Please contact support.'
);
}
if (!function_exists('register_block_type')) {
// Gutenberg is not active.
return;
}
wp_register_script(
BLOCKS_FOR_EVENTBRITE_SCRIPT_NAME,
plugins_url(BLOCKS_FOR_EVENTBRITE_INDEX_JS, __FILE__),
BLOCKS_FOR_EVENTBRITE_SCRIPT_ASSET['dependencies'],
BLOCKS_FOR_EVENTBRITE_SCRIPT_ASSET['version']
);
wp_set_script_translations(BLOCKS_FOR_EVENTBRITE_SCRIPT_NAME, 'blocks-for-eventbrite');
// mock our data in js for the editor
wp_localize_script(
BLOCKS_FOR_EVENTBRITE_SCRIPT_NAME,
BLOCKS_FOR_EVENTBRITE_LOCALIZED_SCRIPT_NAME,
[
'events' => [],
'attributes' => [],
'assets' => [
'placeholderImage' => plugins_url('src/img/placeholder.jpg', __FILE__),
],
]
);
register_block_type('blocks-for-eventbrite/events-card', array(
'editor_script' => BLOCKS_FOR_EVENTBRITE_SCRIPT_NAME,
'render_callback' => 'render_blocks_for_eventbrite_card',
'attributes' => [
'status' => [
'type' => 'string',
'default' => 'live',
],
'orderBy' => [
'type' => 'string',
'default' => 'start_asc',
],
'noEventsText' => [
'type' => 'string',
'default' => 'There are no events at this time. Please check back for upcoming events.'
],
'dateFormat' => [
'type' => 'string',
'default' => get_option('date_format')
],
'timeFormat' => [
'type' => 'string',
'default' => get_option('time_format')
],
'signUpButtonText' => [
'type' => 'string',
'default' => 'Sign Up'
],
'pageSize' => [
'type' => 'number',
'default' => 50
],
]
));
});
/**
* Add a block category for "Eventbrite Blocks" if it doesn't exist already.
*
* @param array $categories Array of block categories.
*
* @return array
*/
add_filter('block_categories', function ($categories) {
$category_slugs = wp_list_pluck($categories, 'slug');
return in_array('blocks-for-eventbrite', $category_slugs, true) ? $categories : array_merge(
$categories,
array(
array(
'slug' => 'blocks-for-eventbrite',
'title' => __('Blocks For Eventbrite', 'blocks-for-eventbrite'),
'icon' => null,
),
)
);
});
/**
* Register block type callback render_callback
*
* @param $attributes
* @return false|string
*/
function render_blocks_for_eventbrite_card($attributes)
{
return (new RenderBlocksForEventbriteCard($attributes))->render();
}