-
Notifications
You must be signed in to change notification settings - Fork 4
/
wporg-gp-translation-events.php
469 lines (426 loc) · 17.3 KB
/
wporg-gp-translation-events.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
/**
* Plugin Name: Translation Events
* Plugin URI: https://github.com/WordPress/wporg-gp-translation-events/
* Description: A WordPress plugin for creating translation events.
* Version: 1.0.1
* Requires at least: 6.4
* Tested up to: 6.4
* Requires PHP: 7.4
* Author: WordPress Contributors
* Author URI: https://github.com/WordPress/wporg-gp-translation-events/
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: gp-translation-events
*
* @package Translation Events
*/
namespace Wporg\TranslationEvents;
use DateTimeImmutable;
use DateTimeZone;
use Exception;
use GP;
use GP_Locales;
use WP_Post;
use Wporg\TranslationEvents\Attendee\Attendee;
use Wporg\TranslationEvents\Attendee\Attendee_Adder;
use Wporg\TranslationEvents\Attendee\Attendee_Repository;
use Wporg\TranslationEvents\Event\Event_Capabilities;
use Wporg\TranslationEvents\Event\Event_Form_Handler;
use Wporg\TranslationEvents\Event\Event_Repository;
use Wporg\TranslationEvents\Notifications\Notifications_Send;
use Wporg\TranslationEvents\Stats\Stats_Calculator;
use Wporg\TranslationEvents\Stats\Stats_Listener;
class Translation_Events {
public const CPT = 'translation_event';
private Event_Capabilities $event_capabilities;
public static function get_instance(): Translation_Events {
static $instance = null;
if ( null === $instance ) {
require_once __DIR__ . '/autoload.php';
$instance = new self();
}
return $instance;
}
public static function now(): DateTimeImmutable {
static $now = null;
if ( null === $now ) {
$now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) );
}
return $now;
}
public static function get_event_repository(): Event_Repository {
static $event_repository = null;
if ( null === $event_repository ) {
$event_repository = new Event_Repository( self::now(), self::get_attendee_repository() );
}
return $event_repository;
}
public static function get_attendee_repository(): Attendee_Repository {
static $attendee_repository = null;
if ( null === $attendee_repository ) {
$attendee_repository = new Attendee_Repository();
}
return $attendee_repository;
}
public static function get_attendee_adder(): Attendee_Adder {
static $attendee_adder = null;
if ( null === $attendee_adder ) {
$attendee_adder = new Attendee_Adder( self::get_attendee_repository() );
}
return $attendee_adder;
}
public function __construct() {
register_theme_directory( __DIR__ . '/themes' );
add_action( 'wp_ajax_submit_event_ajax', array( $this, 'submit_event_ajax' ) );
add_action( 'wp_ajax_nopriv_submit_event_ajax', array( $this, 'submit_event_ajax' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'register_translation_event_js' ) );
add_action( 'init', array( $this, 'register_event_post_type' ) );
add_action( 'init', array( $this, 'send_notifications' ) );
add_action( 'init', array( $this, 'remove_incorrect_rss_feed' ) );
add_action( 'add_meta_boxes', array( $this, 'event_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_event_meta_boxes' ) );
add_action( 'transition_post_status', array( $this, 'event_status_transition' ), 10, 3 );
add_filter( 'gp_nav_menu_items', array( $this, 'gp_event_nav_menu_items' ), 10, 2 );
add_filter( 'wp_insert_post_data', array( $this, 'generate_event_slug' ), 10, 2 );
add_action( 'gp_init', array( $this, 'gp_init' ) );
add_action( 'gp_before_translation_table', array( $this, 'add_active_events_current_user' ) );
add_filter( 'wp_post_revision_meta_keys', array( $this, 'wp_post_revision_meta_keys' ) );
add_filter( 'pre_wp_unique_post_slug', array( $this, 'pre_wp_unique_post_slug' ), 10, 6 );
if ( is_admin() ) {
Upgrade::upgrade_if_needed();
}
$this->event_capabilities = new Event_Capabilities(
self::now(),
self::get_event_repository(),
self::get_attendee_repository(),
new Stats_Calculator()
);
$this->event_capabilities->register_hooks();
}
public function gp_init() {
$locale = '(' . implode( '|', wp_list_pluck( GP_Locales::locales(), 'slug' ) ) . ')';
$slug = '((?:2[0-9]{3}/)?[a-z0-9_-]+)';
$status = '(waiting)';
$id = '(\d+)';
GP::$router->add( '/events?', array( 'Wporg\TranslationEvents\Routes\Event\List_Route', 'handle' ) );
GP::$router->add( '/events/trashed?', array( 'Wporg\TranslationEvents\Routes\Event\List_Trashed_Route', 'handle' ) );
GP::$router->add( '/events/new', array( 'Wporg\TranslationEvents\Routes\Event\Create_Route', 'handle' ) );
GP::$router->add( "/events/edit/$id", array( 'Wporg\TranslationEvents\Routes\Event\Edit_Route', 'handle' ) );
GP::$router->add( "/events/trash/$id", array( 'Wporg\TranslationEvents\Routes\Event\Trash_Route', 'handle' ) );
GP::$router->add( "/events/delete/$id", array( 'Wporg\TranslationEvents\Routes\Event\Delete_Route', 'handle' ) );
GP::$router->add( "/events/image/$id", array( 'Wporg\TranslationEvents\Routes\Event\Image_Route', 'handle' ) );
GP::$router->add( "/events/attend/$id", array( 'Wporg\TranslationEvents\Routes\User\Attend_Event_Route', 'handle' ), 'post' );
GP::$router->add( "/events/host/$id/$id", array( 'Wporg\TranslationEvents\Routes\User\Host_Event_Route', 'handle' ), 'post' );
GP::$router->add( '/events/my-events', array( 'Wporg\TranslationEvents\Routes\User\My_Events_Route', 'handle' ) );
GP::$router->add( '/events/feed', array( 'Wporg\TranslationEvents\Routes\Event\Rss_Route', 'handle' ) );
GP::$router->add( '/events/rss', array( 'Wporg\TranslationEvents\Routes\Event\Rss_Route', 'handle' ) );
GP::$router->add( "/events/$slug/translations/$locale/$status", array( 'Wporg\TranslationEvents\Routes\Event\Translations_Route', 'handle' ) );
GP::$router->add( "/events/$slug/translations/$locale", array( 'Wporg\TranslationEvents\Routes\Event\Translations_Route', 'handle' ) );
GP::$router->add( "/events/$slug", array( 'Wporg\TranslationEvents\Routes\Event\Details_Route', 'handle' ) );
GP::$router->add( "/events/$slug/attendees", array( 'Wporg\TranslationEvents\Routes\Attendee\List_Route', 'handle' ) );
GP::$router->add( "/events/$id/attendees/remove/$id", array( 'Wporg\TranslationEvents\Routes\Attendee\Remove_Attendee_Route', 'handle' ) );
GP::$router->add( "/events/attendance-mode/$id/$id", array( 'Wporg\TranslationEvents\Routes\User\Attendance_Mode_Route', 'handle' ), 'get' );
$stats_listener = new Stats_Listener( self::get_event_repository() );
$stats_listener->start();
}
public function register_translation_event_js() {
wp_register_script(
'translation-events-js',
plugins_url( 'assets/js/translation-events.js', __FILE__ ),
array( 'jquery', 'gp-common' ),
filemtime( __DIR__ . '/assets/js/translation-events.js' ),
false
);
gp_enqueue_script( 'translation-events-js' );
wp_localize_script(
'translation-events-js',
'$translation_event',
array(
'url' => admin_url( 'admin-ajax.php' ),
'_event_nonce' => wp_create_nonce( self::CPT ),
)
);
}
/**
* Register the event post type.
*/
public function register_event_post_type() {
$labels = array(
'name' => 'Translation Events',
'singular_name' => 'Translation Event',
'menu_name' => 'Translation Events',
'add_new' => 'Add New',
'add_new_item' => 'Add New Translation Event',
'edit_item' => 'Edit Translation Event',
'new_item' => 'New Translation Event',
'view_item' => 'View Translation Event',
'search_items' => 'Search Translation Events',
'not_found' => 'No translation events found',
'not_found_in_trash' => 'No translation events found in trash',
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_rest' => true,
'has_archive' => true,
'hierarchical' => true,
'menu_icon' => 'dashicons-calendar',
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions', 'page-attributes' ),
'rewrite' => array( 'slug' => 'events' ),
);
register_post_type( self::CPT, $args );
}
/**
* Add meta boxes for the event post type.
*/
public function event_meta_boxes() {
add_meta_box( 'event_dates', 'Event Dates', array( $this, 'event_dates_meta_box' ), self::CPT, 'normal', 'high' );
add_meta_box( 'hosts', 'Hosts', array( $this, 'hosts_meta_box' ), self::CPT, 'normal', 'high' );
}
/**
* Output the event dates meta box.
*
* @param WP_Post $post The current post object.
*/
public function event_dates_meta_box( WP_Post $post ) {
wp_nonce_field( 'event_dates_nonce', 'event_dates_nonce' );
$event = self::get_event_repository()->get_event( $post->ID );
?>
<label for="event_start">Start Date (UTC): </label>
<input type="datetime-local" id="event_start" name="event_start" value="<?php echo esc_attr( $event->start() ); ?>" required><br>
<label for="event_end">End Date (UTC): </label>
<input type="datetime-local" id="event_end" name="event_end" value="<?php echo esc_attr( $event->end() ); ?>" required><br>
<label for="event-timezone">Timezone: </label>
<select id="event-timezone" name="event_timezone" required>
<?php
echo wp_kses(
wp_timezone_choice( $event->timezone()->getName(), get_user_locale() ),
array(
'optgroup' => array( 'label' => array() ),
'option' => array(
'value' => array(),
'selected' => array(),
),
)
);
?>
</select>
<?php
}
/**
* Output the event dates meta box.
*
* @param WP_Post $post The current post object.
*/
public function hosts_meta_box( WP_Post $post ) {
$hosts = self::get_attendee_repository()->get_hosts( $post->ID );
$hosts_list = array_map(
function ( Attendee $host ) {
$user = get_user_by( 'ID', $host->user_id() );
if ( ! $user ) {
return '<i>Unknown user id: ' . esc_html( $host->user_id() ) . '</i>';
}
return '<a href="' . esc_attr( get_author_posts_url( $host->user_id() ) ) . '">' . esc_html( $user->display_name ) . '</a>';
},
$hosts
);
echo wp_kses(
implode( ', ', $hosts_list ),
array(
'a' => array( 'href' => array() ),
)
);
}
/**
* Save the event meta boxes.
*
* @param int $post_id The current post ID.
*/
public function save_event_meta_boxes( int $post_id ) {
$nonces = array( 'event_dates' );
foreach ( $nonces as $nonce ) {
$nonce_name = $nonce . '_nonce';
if ( ! isset( $_POST[ $nonce_name ] ) ) {
return;
}
$nonce_value = sanitize_text_field( wp_unslash( $_POST[ $nonce_name ] ) );
if ( ! wp_verify_nonce( $nonce_value, $nonce_name ) ) {
return;
}
}
$fields = array( 'event_start', 'event_end', 'event_timezone' );
foreach ( $fields as $field ) {
if ( isset( $_POST[ $field ] ) ) {
update_post_meta( $post_id, '_' . $field, sanitize_text_field( wp_unslash( $_POST[ $field ] ) ) );
}
}
}
/**
* Handle the event form submission for the creation, editing, and deletion of events. This function is called via AJAX.
*/
public function submit_event_ajax() {
$form_handler = new Event_Form_Handler( self::now(), self::get_event_repository() );
// Nonce verification is done by the form handler.
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$form_handler->handle( $_POST );
}
/**
* Handle the event status transition.
*
* The user who creates the event will assist to it when it's published.
*
* @param string $new_status The new post status.
* @param string $old_status The old post status.
* @param WP_Post $post The post object.
*
* @throws Exception
*/
public function event_status_transition( string $new_status, string $old_status, WP_Post $post ): void {
if ( self::CPT !== $post->post_type ) {
return;
}
if ( 'publish' === $new_status && ( 'new' === $old_status || 'draft' === $old_status ) ) {
$event = self::get_event_repository()->get_event( $post->ID );
if ( ! $event ) {
return;
}
$user_id = $post->post_author;
$attendee_repository = self::get_attendee_repository();
$attendee = $attendee_repository->get_attendee_for_event_for_user( $event->id(), $user_id );
if ( null === $attendee ) {
$attendee = new Attendee( $event->id(), $user_id, true );
self::get_attendee_adder()->add_to_event( $event, $attendee );
}
}
}
/**
* Add the events link to the GlotPress main menu.
*
* @param array $items The menu items.
* @param string $location The menu location.
* @return array The modified menu items.
*/
public function gp_event_nav_menu_items( array $items, string $location ): array {
if ( 'main' !== $location ) {
return $items;
}
$new[ esc_url( Urls::events_home() ) ] = esc_html__( 'Events', 'gp-translation-events' );
return array_merge( $items, $new );
}
/**
* Generate a slug for the event post type when we save a draft event or when we publish an event.
*
* Generate a slug based on the event title if:
* - The event is a draft.
* - The event is published and it was a draft just before.
*
* @param array $data An array of slashed post data.
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
* @return array The modified post data.
*/
public function generate_event_slug( array $data, array $postarr ): array {
if ( self::CPT === $data['post_type'] ) {
if ( isset( $data['post_name'] ) && preg_match( '/^2[0-9]+$/', $data['post_name'] ) ) {
return $data;
}
if ( 'draft' === $data['post_status'] ) {
$data['post_name'] = sanitize_title( $data['post_title'] );
}
if ( 'publish' === $data['post_status'] ) {
if ( is_numeric( $postarr['ID'] ) && 0 !== $postarr['ID'] ) {
$post = get_post( $postarr['ID'] );
if ( $post instanceof WP_Post ) {
if ( 'draft' === $post->post_status ) {
$data['post_name'] = sanitize_title( $data['post_title'] );
}
}
}
}
}
return $data;
}
/**
* Add the active events for the current user before the translation table.
*
* @throws Exception
*/
public function add_active_events_current_user(): void {
if ( ! is_user_logged_in() ) {
return;
}
$event_repository = self::get_event_repository();
$user_attending_events_query = $event_repository->get_current_events_for_user( get_current_user_id() );
$events = $user_attending_events_query->events;
$number_of_events = count( $events );
if ( 0 === $number_of_events ) {
return;
}
$content = '<div id="active-events-before-translation-table" class="active-events-before-translation-table" style="width: 100%; border: 1px solid var(--gp-color-border-default); background: var(--gp-color-status-waiting-subtle); margin: 1rem 0; padding: 0.3rem 0.8rem;">';
/* translators: %d: Number of events */
$content .= sprintf( _n( 'Contributing to %d event:', 'Contributing to %d events:', $number_of_events, 'gp-translation-events' ), $number_of_events );
$content .= ' ';
foreach ( array_splice( $events, 0, 2 ) as $event ) {
$content .= '<span class="active-events-before-translation-table" style="font-weight: 500; color: var(--gp-color-bubble-inactive-project-text); border: 1px solid var(--gp-color-bubble-inactive-project-text); font-size: .7em; margin-right: 0.3em; width: 6em; text-align: center; padding: 0.2em 0.5em; border-radius: 1em; text-transform: capitalize;"><a href="' . Urls::event_details( $event->id() ) . '" target="_blank" style="color: var(--gp-color-bubble-inactive-project-text); text-decoration: none;">' . esc_html( $event->title() ) . '</a></span>';
}
if ( $number_of_events > 3 ) {
$remaining_events = $number_of_events - 2;
/* translators: %d: Number of remaining events */
$content .= '<span class="remaining-events"><a href="' . esc_url( Urls::events_home() ) . '" target="_blank">' . sprintf( esc_html__( ' and %d more events.', 'gp-translation-events' ), $remaining_events ) . '</a></span>';
}
$content .= '</div>';
echo wp_kses(
$content,
array(
'div' => array(
'id' => array(),
'class' => array(),
'style' => array(),
),
'span' => array(
'class' => array(),
'style' => array(),
),
'a' => array(
'href' => array(),
'target' => array(),
'style' => array(),
),
)
);
}
/**
* Send notifications for the events.
*/
public function send_notifications() {
new Notifications_Send( self::now(), self::get_event_repository(), self::get_attendee_repository() );
}
/**
* Remove the incorrect RSS feed.
*/
public function remove_incorrect_rss_feed() {
remove_action( 'wp_head', 'feed_links', 2 );
}
/**
* Add the event meta keys to the list of meta keys to keep in post revisions.
*
* @param array $keys The list of meta keys to keep in post revisions.
*
* @return array The modified list of meta keys to keep in post revisions.
*/
public function wp_post_revision_meta_keys( array $keys ): array {
$meta_keys_to_keep = array( '_event_start', '_event_end', '_event_timezone', '_hosts', '_event_attendance_mode' );
return array_merge( $keys, $meta_keys_to_keep );
}
public function pre_wp_unique_post_slug( $override_slug, string $slug, int $post_id, string $post_status, string $post_type, int $post_parent ) {
if ( self::CPT !== $post_type || $post_parent ) {
return $override_slug;
}
// Normally the slug is not allowed to be a year, this overrides it since we have a CPT and translate.wordpress.org doesn't have blog posts.
if ( preg_match( '/^2[0-9]{3}$/', $slug ) ) {
return $slug;
}
return $override_slug;
}
}
Translation_Events::get_instance();