-
Notifications
You must be signed in to change notification settings - Fork 11
/
plugin.php
163 lines (145 loc) · 4.61 KB
/
plugin.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
<?php
/**
* Plugin Name: Meeting Calendar
* Description: This provides a way of scheduling recurring meetings, and displaying a calendar or timetable..
* Plugin URI: https://github.com/WordPress/meeting-calendar
* Author: WordPress.org Contributors
* Version: 1.1.0
* Text Domain: wporg-meeting-calendar
* License: GPL2+
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt
*/
namespace WordPressdotorg\Meeting_Calendar;
/**
* Retrieves meetings
*
* @param integer $per_page Number of meetings per page.
* @return string List of meetings in JSON format
*/
function get_meeting_data( $per_page ) {
$date = gmdate( 'Y-m-d', strtotime( 'first day of this month' ) );
$request = new \WP_REST_Request( 'GET', '/wp/v2/meetings/from/' . $date );
$request->set_query_params( array( 'per_page' => $per_page ) );
$response = rest_do_request( $request );
$server = rest_get_server();
$data = $server->response_to_data( $response, false );
return wp_json_encode( $data );
}
/**
* Render the block content (html) on the frontend of the site.
*
* @param array $attributes
* @param string $content
* @return string HTML output used by the calendar JS.
*/
function render_callback( $attributes, $content ) {
$meetings = get_meeting_data( 12 );
return sprintf(
'<div class="alignwide wporg-block-meeting-calendar" id="%s" data-meetings="%s">Loading Calendar ...</div>',
'wporg-meeting-calendar-js',
htmlspecialchars( $meetings, ENT_QUOTES )
);
}
/**
* Register scripts, styles, and block.
*/
function register_assets() {
$block_deps_path = __DIR__ . '/build/index.asset.php';
$frontend_deps_path = __DIR__ . '/build/calendar.asset.php';
if ( ! file_exists( $block_deps_path ) || ! file_exists( $frontend_deps_path ) ) {
return;
}
$block_info = require $block_deps_path;
$frontend_info = require $frontend_deps_path;
// Register our block script with WordPress.
wp_register_script(
'wporg-calendar-block-script',
plugins_url( 'build/index.js', __FILE__ ),
$block_info['dependencies'],
$block_info['version'],
false
);
// Add translation support.
wp_set_script_translations( 'wporg-calendar-block-script', 'wporg-meeting-calendar' );
// Register our block's base CSS.
wp_register_style(
'wporg-calendar-block-style',
plugins_url( 'style.css', __FILE__ ),
array(),
$block_info['version']
);
// No frontend scripts in the editor
if ( ! is_admin() ) {
wp_register_script(
'wporg-calendar-script',
plugin_dir_url( __FILE__ ) . 'build/calendar.js',
$frontend_info['dependencies'],
$frontend_info['version'],
false
);
// Add translation support.
wp_set_script_translations( 'wporg-calendar-script', 'wporg-meeting-calendar' );
wp_register_style(
'wporg-calendar-style',
plugin_dir_url( __FILE__ ) . 'build/style-calendar.css',
array( 'wp-components' ),
$frontend_info['version']
);
}
// Enqueue the script in the editor.
register_block_type(
'wporg-meeting-calendar/main',
array(
'editor_script' => 'wporg-calendar-block-script',
'editor_style' => 'wporg-calendar-block-style',
'script' => 'wporg-calendar-script',
'style' => 'wporg-calendar-style',
'render_callback' => __NAMESPACE__ . '\render_callback',
)
);
}
add_action( 'init', __NAMESPACE__ . '\register_assets' );
/**
* Conditionally remove the Script/Style assets added through `register_block_type()`.
*/
function conditionally_load_assets() {
if ( ! is_singular() || ! has_block( 'wporg-meeting-calendar/main' ) ) {
wp_dequeue_script( 'wporg-calendar-script' );
wp_dequeue_style( 'wporg-calendar-style' );
}
}
add_action( 'enqueue_block_assets', __NAMESPACE__ . '\conditionally_load_assets' );
/**
* Set up the Meetings post type.
*/
function init() {
require_once __DIR__ . '/includes/wporg-meeting-posttype.php';
new \Meeting_Post_Type();
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\init' );
/**
* Set up the ICS support.
*/
function ics_init() {
require __DIR__ . '/includes/ical-functions.php';
require __DIR__ . '/includes/ical-generator-functions.php';
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\ics_init' );
/**
* First-Install activation hook.
*
* Creates some sample data, and sets up the Rewrite rules.
*/
function install( $is_network_wide ) {
if ( ! $is_network_wide ) {
// We need the CPT to be registered to install.
init();
$meeting_post_type = new \Meeting_Post_Type();
$meeting_post_type->getInstance()->register_meeting_post_type();
require_once __DIR__ . '/includes/wporg-meeting-install.php';
wporg_meeting_install();
ics_init();
ICS\on_activate();
}
}
register_activation_hook( __FILE__, __NAMESPACE__ . '\install' );