generated from mt-support/tec-labs-extension-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tribe-ext-tec-tweaks.php
599 lines (517 loc) · 15.5 KB
/
tribe-ext-tec-tweaks.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
<?php
/**
* Plugin Name: The Events Calendar Extension: Tweaks
* Plugin URI: https://theeventscalendar.com/extensions/the-events-calendar-tweaks/
* GitHub Plugin URI: https://github.com/mt-support/tribe-ext-tec-tweaks/
* Description: A combination of snippets and tweaks for The Events Calendar
* Version: 1.1.2
* Extension Class: Tribe\Extensions\Tec_Tweaks\Main
* Author: The Events Calendar
* Author URI: http://evnt.is/1971
* License: GPL version 3 or any later version
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: tec-labs-tec-tweaks
*
* This plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
namespace Tribe\Extensions\Tec_Tweaks;
use Tribe__Autoloader;
use Tribe__Dependency;
use Tribe__Events__REST__V1__Main;
use Tribe__Extension;
// Do not load unless Tribe Common is fully loaded and our class does not yet exist.
if (
class_exists( 'Tribe__Extension' )
&& ! class_exists( Main::class )
) {
/**
* Extension main class, class begins loading on init() function.
*/
class Main extends Tribe__Extension {
/**
* @var Tribe__Autoloader
*/
private $class_loader;
/**
* @var Settings
*/
private $settings;
/**
* Is Events Calendar PRO active. If yes, we will add some extra functionality.
*
* @return bool
*/
public $ecp_active = false;
/**
* Setup the Extension's properties.
*
* This always executes even if the required plugins are not present.
*/
public function construct() {
$this->add_required_plugin( 'Tribe__Events__Main', '5.0' );
add_action( 'tribe_plugins_loaded', [ $this, 'detect_tec_pro' ], 0 );
}
/**
* Check required plugins after all Tribe plugins have loaded.
*
* Useful for conditionally-requiring a Tribe plugin, whether to add extra functionality
* or require a certain version but only if it is active.
*/
public function detect_tec_pro() {
/** @var Tribe__Dependency $dep */
$dep = tribe( Tribe__Dependency::class );
if ( $dep->is_plugin_active( 'Tribe__Events__Pro__Main' ) ) {
$this->add_required_plugin( 'Tribe__Events__Pro__Main', '5.0' );
$this->ecp_active = true;
}
}
/**
* Get this plugin's options prefix.
*
* Settings_Helper will append a trailing underscore before each option.
*
* @see \Tribe\Extensions\Tec_Tweaks\Settings::set_options_prefix()
*
* @return string
*/
private function get_options_prefix() {
return (string) str_replace( '-', '_', 'tribe-ext-tec-tweaks' );
}
/**
* Get Settings instance.
*
* @return Settings
*/
private function get_settings() {
if ( empty( $this->settings ) ) {
$this->settings = new Settings( $this->get_options_prefix() );
}
return $this->settings;
}
/**
* Extension initialization and hooks.
*/
public function init() {
// Load plugin textdomain.
load_plugin_textdomain( 'tec-labs-tec-tweaks', false, basename( dirname( __FILE__ ) ) . '/languages/' );
if ( ! $this->php_version_check() ) {
return;
}
if ( ! $this->is_using_compatible_view_version() ) {
return;
}
$this->class_loader();
$this->get_settings();
$this->disable_latest_past_events();
$this->hide_tooltip();
$this->hide_past_events_in_month_view();
$this->hide_event_time_in_month_view();
$this->hide_weekends_on_month_view();
$this->remove_archives_from_page_title();
$this->show_past_events_in_reverse_order();
$this->remove_links_from_events();
$this->change_free_in_ticket_cost();
$this->disable_tribe_rest_api();
add_filter( 'tribe_get_events_link', [ $this, 'custom_all_events_url' ] );
$this->template_hijack();
}
/**
* Check if we have a sufficient version of PHP. Admin notice if we don't and user should see it.
*
* @return bool
*/
private function php_version_check() {
$php_required_version = '7.0';
if ( version_compare( PHP_VERSION, $php_required_version, '<' ) ) {
if (
is_admin()
&& current_user_can( 'activate_plugins' )
) {
$message = '<p>';
$message .= sprintf(
// Translators: 1: extension name, 2: required version.
__(
'%1$s requires PHP version %2$s or newer to work. Please contact your website host and inquire about updating PHP.',
'tec-labs-tec-tweaks'
),
$this->get_name(),
$php_required_version
);
$message .= sprintf( ' <a href="%1$s">%1$s</a>', 'https://wordpress.org/about/requirements/' );
$message .= '</p>';
tribe_notice( 'tec-labs-tec-tweaks-php-version', $message, [ 'type' => 'error' ] );
}
return false;
}
return true;
}
/**
* Check if we have the required TEC view. Admin notice if we don't and user should see it.
*
* @return bool
*/
private function is_using_compatible_view_version() {
$view_required_version = 2;
$meets_req = true;
// Is V2 enabled?
if (
function_exists( 'tribe_events_views_v2_is_enabled' )
&& ! empty( tribe_events_views_v2_is_enabled() )
) {
$is_v2 = true;
} else {
$is_v2 = false;
}
// V1 compatibility check.
if (
1 === $view_required_version
&& $is_v2
) {
$meets_req = false;
}
// V2 compatibility check.
if (
2 === $view_required_version
&& ! $is_v2
) {
$meets_req = false;
}
// Notice, if should be shown.
if (
! $meets_req
&& is_admin()
&& current_user_can( 'activate_plugins' )
) {
if ( 1 === $view_required_version ) {
$view_name = _x( 'Legacy Views', 'name of view', 'tec-labs-tec-tweaks' );
} else {
$view_name = _x( 'New (V2) Views', 'name of view', 'tec-labs-tec-tweaks' );
}
$view_name = sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'edit.php?page=tribe-common&tab=display&post_type=tribe_events' ) ),
$view_name
);
// Translators: 1: Extension plugin name, 2: Name of required view, linked to Display tab.
$message = sprintf(
__(
'%1$s requires the "%2$s" so this extension\'s code will not run until this requirement is met. You may want to deactivate this extension or visit its homepage to see if there are any updates available.',
'tec-labs-tec-tweaks'
),
$this->get_name(),
$view_name
);
tribe_notice(
'tec-labs-tec-tweaks-view-mismatch',
'<p>' . $message . '</p>',
[ 'type' => 'error' ]
);
}
return $meets_req;
}
/**
* Use Tribe Autoloader for all class files within this namespace in the 'src' directory.
*
* @return Tribe__Autoloader
*/
public function class_loader() {
if ( empty( $this->class_loader ) ) {
$this->class_loader = new Tribe__Autoloader;
$this->class_loader->set_dir_separator( '\\' );
$this->class_loader->register_prefix(
__NAMESPACE__ . '\\',
__DIR__ . DIRECTORY_SEPARATOR . 'src'
);
}
$this->class_loader->register_autoloader();
return $this->class_loader;
}
/**
* Get all of this extension's options.
*
* @return array
*/
public function get_all_options() {
return $this->settings->get_all_options();
}
/**
* Disables the "Recent Past Events" block.
*/
public function disable_latest_past_events() {
$days_to_show = (bool) $this->settings->get_option( 'disable_recent_past_events', false );
if ( $days_to_show ) {
add_filter( 'tribe_events_views_v2_show_latest_past_events_view', '__return_false' );
}
}
/**
* Hide the tooltip in month view.
*/
public function hide_tooltip() {
$hide_tooltip = (bool) $this->settings->get_option( 'hide_tooltip', false );
if ( $hide_tooltip ) {
add_filter(
'tribe_template_pre_html:events/v2/month/calendar-body/day/calendar-events/calendar-event/tooltip',
'__return_false'
);
}
}
/**
* Hide past events in month view.
*/
public function hide_past_events_in_month_view() {
$hide_past = (bool) $this->settings->get_option( 'hide_past_events_in_month_view', false );
if ( $hide_past ) {
add_action(
'wp_head',
function () {
echo '<style id="tec-labs-tec-tweaks-css-hide-past">.tribe-events-calendar-month__day--past .tribe-events-calendar-month__events, .tribe-events-calendar-month__day--past .tribe-events-calendar-month__more-events {display: none;}</style>';
}
);
}
}
/**
* Hide event times in month view.
*/
public function hide_event_time_in_month_view() {
$hide_event_time_in_month_view = (bool) $this->settings->get_option(
'hide_event_time_in_month_view',
false
);
if ( $hide_event_time_in_month_view ) {
add_action(
'wp_head',
function () {
echo '<style id="tec-labs-tec-tweaks-css-hide-event-time">.tribe-events-calendar-month__calendar-event-datetime{display: none;}</style>';
}
);
}
}
/**
* Hide weekends on month view.
*/
public function hide_weekends_on_month_view() {
$hide_weekends_on_month_view = (bool) $this->settings->get_option(
'hide_weekends_on_month_view',
false
);
// Getting start of the week. 0 = Sunday; 1 = Monday;
$start_of_week = get_option( 'start_of_week' );
if ( $hide_weekends_on_month_view ) {
if ( $start_of_week == 0 ) {
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_hide_weekend_sunday_stylesheet' ] );
} else {
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_hide_weekend_monday_stylesheet' ] );
}
}
}
/**
* Remove "Archives:" from the page titles.
* Some themes add that.
*/
public function remove_archives_from_page_title() {
$remove_archives = (bool) $this->settings->get_option( 'remove_archives_from_page_title', false );
if ( $remove_archives ) {
add_filter(
'get_the_archive_title',
function ( $title ) {
if ( is_post_type_archive( 'tribe_events' ) ) {
$title = sprintf( __( '%s' ), post_type_archive_title( '', false ) );
}
return $title;
}
);
}
}
/**
* Show past events in reverse order.
*/
public function show_past_events_in_reverse_order() {
$show_past_events_in_reverse_order = (bool) $this->settings->get_option(
'show_past_events_in_reverse_order',
false
);
if ( $show_past_events_in_reverse_order ) {
// Change List View to Past Event Reverse Chronological Order.
add_filter(
'tribe_events_views_v2_view_list_template_vars',
[ $this, 'tribe_past_reverse_chronological_v2' ],
100
);
add_filter(
'tribe_events_views_v2_view_photo_template_vars',
[ $this, 'tribe_past_reverse_chronological_v2' ],
100
);
}
}
/**
* Show past events in reverse order.
*
* @param $template_vars
*
* @return mixed
*/
private function tribe_past_reverse_chronological_v2( $template_vars ) {
if ( ! empty( $template_vars['is_past'] ) ) {
$template_vars['events'] = array_reverse( $template_vars['events'] );
}
return $template_vars;
}
/**
* Remove links from event titles. Event titles will not be clickable.
*/
public function remove_links_from_events() {
$remove_links_from_events_views = (array) $this->settings->get_option( 'remove_links_from_events', false );
if ( ! empty ( $remove_links_from_events_views ) ) {
add_action( 'wp_head', [ $this, 'remove_links_html' ] );
}
}
/**
* Code for removing links from event titles.
*/
public function remove_links_html() {
$classes = (array) $this->settings->get_option( 'remove_links_from_events', false );
$html = "\n<style id='tec-labs-tec-tweaks-css'>";
foreach ( $classes as $class ) {
$html .= "\n." . $class . ",";
}
// Remove last comma
$html = substr( $html, 0, - 1 );
$html .= "\n{ pointer-events: none; }\n";
$html .= "</style>\n";
echo $html;
}
/**
* Change "Free" in event cost to custom text.
*/
public function change_free_in_ticket_cost() {
$free = $this->settings->get_option( 'change_free_in_ticket_cost', '0' );
if (
! empty ( $free )
|| $free == '0'
) {
add_filter( 'gettext', [ $this, 'change_free_function' ], 20, 3 );
}
}
/**
* Change "Free" in event cost to custom text.
*
* @param $translation
* @param $text
* @param $domain
*
* @return mixed
*/
public function change_free_function( $translation, $text, $domain ) {
$free = $this->settings->get_option( 'change_free_in_ticket_cost' );
$custom_text = [ 'Free' => $free ];
// If this text domain starts with "tribe-", "the-events-", or "event-" and we have replacement text
if (
0 === strpos( $domain, 'the-events-calendar' )
&& array_key_exists( $translation, $custom_text )
) {
$translation = $custom_text[ $translation ];
}
return $translation;
}
/**
* Reads and returns the custom 'All Events' URL if it is set.
*
* @param $url
*
* @return mixed
*/
public function custom_all_events_url( $url ) {
$custom_url = $this->settings->get_option( 'custom_all_events_url' );
if ( ! empty ( $custom_url ) ) {
$url = $custom_url;
}
return $url;
}
/**
* Disable REST API for The Events Calendar.
*/
public function disable_tribe_rest_api() {
$disable_tribe_rest_api = (bool) $this->settings->get_option( 'disable_tribe_rest_api', false );
if ( $disable_tribe_rest_api ) {
add_action(
'init',
function () {
/** @var Tribe__Events__REST__V1__Main $rest */
$rest = tribe( 'tec.rest-v1.main' );
remove_action( 'rest_api_init', [ $rest, 'register_endpoints' ] );
}
);
}
}
/**
* Template hijacking
*/
public function template_hijack() {
$hijack = $this->settings->get_option( 'template_hijack', 'hijack_none' );
if ( $hijack == 'hijack_none' ) {
return;
}
if ( $hijack == 'hijack_calendar_pages' ) {
add_filter(
'tribe_events_views_v2_use_wp_template_hierarchy',
function ( $hijack, $template, $context, $query ) {
if ( ! is_singular() ) {
$hijack = true;
}
return $hijack;
},
10,
4
);
}
if ( $hijack == 'hijack_single_events' ) {
add_filter(
'tribe_events_views_v2_use_wp_template_hierarchy',
function ( $hijack, $template, $context, $query ) {
if ( is_singular() ) {
$hijack = true;
}
return $hijack;
},
10,
4
);
}
if ( $hijack == 'hijack_all' ) {
add_filter( 'tribe_events_views_v2_use_wp_template_hierarchy', '__return_true' );
}
}
/**
* Sends the hide weekends stylesheet for enqueueing if week starts on Monday
*/
public function enqueue_hide_weekend_monday_stylesheet() {
$this->enqueue_stylesheet( 'hide-weekends-on-month-view-monday.css' );
}
/**
* Sends the hide weekends stylesheet for enqueueing if week starts on Sunday
*/
public function enqueue_hide_weekend_sunday_stylesheet() {
$this->enqueue_stylesheet( 'hide-weekends-on-month-view-sunday.css' );
}
/**
* Enqueues the stylesheet
*
* @param $filename
*/
public function enqueue_stylesheet( $filename ) {
wp_enqueue_style(
'tec-labs-tec-tweaks-' . str_replace( [ '.', 'css', 'js' ], '', $filename ),
plugin_dir_url( __FILE__ ) . 'src/' . $filename
);
}
} // end class
} // end if class_exists check