-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity-date.php
78 lines (67 loc) · 2.41 KB
/
activity-date.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
<?php
/**
* Plugin Name: Activity Date
* Plugin URI: https://github.com/willsides/activity-date
* Description: Displays the date(s) of a post activity from the 'activity-date' and 'activity-end-date' post meta fields
* Requires at least: 5.8
* Requires PHP: 7.0
* Version: 1.0.0
* Author: Will Sides
* License: GPL-3.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: activity-date
*
* @package willsides
*/
function render_block_willsides_activity_date( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}
$activity_date = get_post_meta($block->context['postId'], 'activity_date', true);
$formatted_activity_date = mysql2date( 'F j, Y', $activity_date);
if ( ! $formatted_activity_date ) {
return '';
}
$activity_end_date = get_post_meta($block->context['postId'], 'activity_end_date', true);
$formatted_end_date = mysql2date( 'F j, Y', $activity_end_date);
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
$opening_tag = sprintf(
'<div %1$s><time datetime="%2$s">%3$s</time>',
$wrapper_attributes,
$activity_date,
$formatted_activity_date );
$closing_tag = ( ! $formatted_end_date ) ? '</div>' : sprintf(
' - <time datetime="%1$s">%2$s</time></div>',
$activity_end_date,
$formatted_end_date);
return $opening_tag . $closing_tag;
}
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function willsides_activity_date_block_init() {
register_block_type(
__DIR__ . '/build' ,
array(
'render_callback' => 'render_block_willsides_activity_date',
)
);
register_post_meta( 'post', 'activity_date', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => 'YYYY-MM-DD'
) );
register_post_meta( 'post', 'activity_end_date', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => 'YYYY-MM-DD'
) );
}
add_action( 'init', 'willsides_activity_date_block_init' );