-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule-block.php
111 lines (94 loc) · 2.57 KB
/
schedule-block.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
<?php
/**
* Plugin Name: Schedule block
* Description: Add scheduling options to WordPress blocks (Gutenberg).
* Author: superhuit
* Author URI: https://www.superhuit.ch
* Version: 1.0.0
* Requires PHP: 7.4
* Text Domain: schedule-block
* Requires at least: 5.0
* Tested up to: 6.2
*
* @package ScheduleBlock
* @category Core
* @author Superhuit, Kuuak
* @version 1.0.0
*/
use SUPT\ScheduleBlock\Editor;
use SUPT\ScheduleBlock\I18n;
use SUPT\ScheduleBlock\PostSave;
use SUPT\ScheduleBlock\RenderBlock;
// Exit if accessed directly.
defined( 'ABSPATH' ) or die( 'Cheatin’ uh?' );
define( 'SCHEDULE_BLOCK_PLUGIN_VERSION', '1.0.0' );
define( 'SCHEDULE_BLOCK_PATH', __DIR__ );
define( 'SCHEDULE_BLOCK_URL', plugin_dir_url(__FILE__) );
// Load dependencies
// ====
if ( ! file_exists(__DIR__ .'/vendor/autoload.php') ) {
add_action( 'admin_notices', function() {
?>
<div class="notice notice-error">
<p><?php _e( 'Please install composer dependencies for Schedule Block plugin to work', 'schedule-block' ); ?></p>
</div>
<?php
} );
return;
}
require_once __DIR__ . '/vendor/autoload.php';
new I18n();
new Editor();
new RenderBlock();
new PostSave();
// ====
// Action & filter hooks
// ====
register_activation_hook( __FILE__, 'schedule_block_activate' );
register_deactivation_hook( __FILE__, 'schedule_block_deactivate' );
register_uninstall_hook(__FILE__, 'schedule_block_uninstall');
/**
* Execute anything necessary on plugin activation
*/
function schedule_block_activate() {
// e.g. Save default options to database
}
/**
* Execute anything necessary on plugin deactivation
*/
function schedule_block_deactivate() {
// e.g. delete cache or temp options
}
/**
* Execute anything necessary on plugin uninstall (deletion)
*/
function schedule_block_uninstall() {
// e.g. remove plugin options from database
}
/**
* API functions
*/
/**
* Check if given block should be visible or not.
*
* @since 1.0.0
*
* @param WP_Block_Parser_Block $block.
* @return boolean
*/
function schedule_block_is_block_visible( $block ) {
$is_visible = true;
if ( isset($block['attrs']['schedule']) ) {
$now = new DateTime();
$now->setTimezone( new DateTimeZone( 'Z' ) );
if ( !empty($block['attrs']['schedule']['start']) ) {
$start = new DateTime( $block['attrs']['schedule']['start'] );
$is_visible = $now >= $start;
}
if ( $is_visible && !empty($block['attrs']['schedule']['end']) ) {
$end = new DateTime( $block['attrs']['schedule']['end'] );
$is_visible = $now < $end;
}
}
return $is_visible;
}