-
Notifications
You must be signed in to change notification settings - Fork 3
/
gb-fullcalendar.php
209 lines (184 loc) · 6.76 KB
/
gb-fullcalendar.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Plugin Name: GB FullCalendar
* Plugin URI: https://github.com/oberhauser-dev/gb-fullcalendar/
* Description: GB FullCalendar is a Gutenberg block for displaying events.
* Version: 0.2.1
* Requires at least: 5.3.2
* Tested up to: 5.7
* Requires PHP: 7.0.0
* Author: August Oberhauser
* Author URI: https://www.oberhauser.dev/
* License: GPL3+
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
* Text Domain: gb-fullcalendar
*
* @package oberhauser-dev
*/
include_once(ABSPATH . 'wp-admin/includes/plugin.php'); // load method for front-end
require_once 'php/gb-fc.php';
include_once 'php/gb-fc-ajax.php';
include_once 'php/gb-fc-actions.php';
if (!is_plugin_active('wp-fullcalendar/wp-fullcalendar.php')) {
// Define WPFC-Version to enable EM-wpfc API (ajax);
if (!defined('WPFC_VERSION'))
define('WPFC_VERSION', '2.1.0');
}
/**
* 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/
*/
function create_block_gb_fullcalendar_block_init()
{
$dir = dirname(__FILE__);
$script_asset_path = "$dir/build/index.asset.php";
if (!file_exists($script_asset_path)) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "oberhauser-dev/gb-fullcalendar" block first.'
);
}
$index_js = 'build/index.js';
$script_asset = require($script_asset_path);
wp_register_script(
'gb-fullcalendar-block-editor',
plugins_url($index_js, __FILE__),
$script_asset['dependencies'],
$script_asset['version']
);
$editor_css = 'build/index.css';
wp_register_style(
'gb-fullcalendar-block-editor',
plugins_url($editor_css, __FILE__),
array(),
filemtime("$dir/$editor_css")
);
// Replaced by client.css
/*$style_css = 'build/style-index.css';
wp_register_style(
'gb-fullcalendar-block',
plugins_url($style_css, __FILE__),
array(),
filemtime("$dir/$style_css")
);*/
// TODO may only load, if block is present, if possible.
$client_js = 'build/client.js';
wp_register_script(
'gb-fullcalendar-block-client',
plugins_url($client_js, __FILE__),
$script_asset['dependencies'],
$script_asset['version']
);
$client_css = 'build/client.css';
wp_register_style(
'gb-fullcalendar-block-client',
plugins_url($client_css, __FILE__),
array(),
filemtime("$dir/$client_css")
);
register_block_type('oberhauser-dev/gb-fullcalendar', array(
'editor_script' => 'gb-fullcalendar-block-editor',
'editor_style' => 'gb-fullcalendar-block-editor',
'script' => 'gb-fullcalendar-block-client',
'style' => 'gb-fullcalendar-block-client',
));
if (is_admin()) {
// Call always as admin, otherwise block cannot be added dynamically.
localize_script();
include_once('php/gb-fc-admin.php');
} else {
// Add shortcode
add_shortcode('fullcalendar', 'calendar_via_shortcode');
}
/**
* Create ajax endpoints.
* https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)
*/
// TODO some time rename "WP_FullCalendar" to "gbfc_events"
//overrides the ajax calls for event data
if (defined('DOING_AJAX') && DOING_AJAX && !empty($_REQUEST['type'])) { //only needed during ajax requests anyway
if ($_REQUEST['type'] === EM_POST_TYPE_EVENT) {
add_filter('wpfc_fullcalendar_args', ['GbFcAjax', 'filter_ajax_em_event_args']);
} else {
add_action('wp_ajax_WP_FullCalendar', ['GbFcAjax', 'ajax_events']);
add_action('wp_ajax_nopriv_WP_FullCalendar', ['GbFcAjax', 'ajax_events']);
}
}
add_action('wp_ajax_gbfc_tooltip_content', ['GbFcAjax', 'ajax_tooltip_content']);
add_action('wp_ajax_nopriv_gbfc_tooltip_content', ['GbFcAjax', 'ajax_tooltip_content']);
}
add_action('init', 'create_block_gb_fullcalendar_block_init');
/**
* Only localize js variables if block is present in front-end.
*/
function create_block_gbfc_block_enqueue_script()
{
// Always enqueue script, as shortcode need localized script, too.
// TODO may fix that only load, when needed.
// if (has_block('oberhauser-dev/gb-fullcalendar')) {
localize_script();
// }
}
add_action('wp_enqueue_scripts', 'create_block_gbfc_block_enqueue_script');
// action links (e.g. Settings)
function gbfc_settings_link($links)
{
array_unshift($links, '<a href="' . admin_url('options-general.php?page=gb-fullcalendar') . '">' . __('Settings', 'gb-fullcalendar') . '</a>');
// Add remove wipe data option.
$plugin_data = get_plugin_data(__FILE__);
$url = wp_nonce_url(admin_url('admin-post.php?action=gbfc_uninstall'), 'gbfc_uninstall');
$links[] = '<span class="delete"><a href="' . $url
. '" onclick="return confirm(\'Are you sure you want to uninstall ' . $plugin_data['Name']
. '? All preferences will be removed!\')">Uninstall</a></span>';
return $links;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'gbfc_settings_link', 10, 1);
/**
* Admin post action hook, without the need of specifying own endpoint / handler.
* https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)
*/
function gbfc_admin_uninstall()
{
check_admin_referer('gbfc_uninstall');
$plugins = [plugin_basename(__FILE__)];
deactivate_plugins($plugins);
GbFcActions::deleteOptions();
delete_plugins($plugins);
wp_redirect($_SERVER['HTTP_REFERER']);
exit();
}
add_action('admin_post_gbfc_uninstall', 'gbfc_admin_uninstall');
function gbfc_admin_reset()
{
check_admin_referer('gbfc_reset');
GbFcActions::resetOptions();
wp_redirect($_SERVER['HTTP_REFERER']);
exit();
}
add_action('admin_post_gbfc_reset', 'gbfc_admin_reset');
function gbfc_admin_resetToWpFc()
{
check_admin_referer('gbfc_resetToWpFc');
GbFcActions::resetToWpFcOptions();
wp_redirect($_SERVER['HTTP_REFERER']);
exit();
}
add_action('admin_post_gbfc_resetToWpFc', 'gbfc_admin_resetToWpFc');
/**
* Localize javascript variables for gb-fullcalendar.
*/
function localize_script()
{
wp_localize_script(
'gb-fullcalendar-block-client',
'GbFcGlobal', // Array containing dynamic data for a JS Global.
[
'pluginDirPath' => plugin_dir_path(__DIR__),
'pluginDirUrl' => plugin_dir_url(__DIR__),
// Add more data here that you want to access from `cgbGlobal` object.
'fc' => getFullCalendarArgs(),
'fcExtra' => getFullCalendarExtraArgs(),
]
);
}