-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshb-pageteaser.php
91 lines (73 loc) · 3.07 KB
/
shb-pageteaser.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
<?php
/**
* Plugin Name: Block: Page teaser
* Description: Display a preview teaser for a selected sub-page.
* Requires at least: 5.8
* Requires PHP: 7.0
* Version: 1.0.1
* Author: Say Hello GmbH
* Author URI: https://sayhello.ch/
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: shb-pageteaser
* Domain Path: /languages
*/
function shb_pageteaser_block_init()
{
register_block_type(__DIR__ . '/build', [
'render_callback' => 'render_block_shb_pageteaser',
'attributes' => [
'post' => [
'type' => 'string',
'default' => ''
],
'imageSize' => [
'type' => 'string',
'default' => 'medium'
]
]
]);
}
add_action('init', 'shb_pageteaser_block_init');
function render_block_shb_pageteaser(array $attributes, string $content, WP_Block $block)
{
$blockWrapperAttributes = get_block_wrapper_attributes();
preg_match_all('/class="(.*?)"/s', $blockWrapperAttributes, $match);
$classNameBase = $match[1][0] ?? 'UNDEFINED_CLASS_NAME_BASE';
// Allow modification via filter
$classNameBase = apply_filters('shb-pageteaser/classnamebase', $classNameBase, $block);
if (!(int) ($attributes['post'] ?? 0)) {
return sprintf('<div class="c-editormessage c-editormessage--error"><p>%s</p></div>', __('Please select a page', 'shb-pageteaser'));
}
$post = get_post($attributes['post']);
if (!$post instanceof WP_Post) {
return sprintf('<div class="c-editormessage c-editormessage--error"><p>%s</p></div>', __('The selected entry is not a page.', 'shb-pageteaser'));
}
$image = sprintf('<figure class="%s"></figure>', "{$classNameBase}__figure {$classNameBase}__figure--empty");
if (has_post_thumbnail($post->ID)) {
$image = sprintf('<figure class="%s"><a class="%s" href="%s">%s</a></figure>', "{$classNameBase}__figure", "{$classNameBase}__figurelink", get_permalink($post->ID), wp_get_attachment_image(get_post_thumbnail_id($post->ID), $attributes['imageSize']), false, ['class' => "{$classNameBase}__image"]);
}
// Allow modification via filter
$image = apply_filters('shb-pageteaser/image', $image, $post->ID, $block);
ob_start();
?>
<article <?php echo $blockWrapperAttributes; ?>>
<?php echo $image; ?>
<header class="<?php echo "{$classNameBase}__header"; ?>">
<h3 class="<?php echo "{$classNameBase}__title"; ?>"><a class="<?php echo "{$classNameBase}__titlelink"; ?>" href="<?php the_permalink($post->ID); ?>"><?php echo get_the_title($post->ID); ?></a></h3>
</header>
<?php if (!empty($excerpt = get_the_excerpt($post->ID))) { ?>
<div class="<?php echo "{$classNameBase}__excerpt"; ?>"><?php echo $excerpt; ?></div>
<?php }
?>
<footer class="<?php echo "{$classNameBase}__footer"; ?>">
<a class="<?php echo "{$classNameBase}__footerlink"; ?>" href="<?php the_permalink($post->ID); ?>"><?php _e('Read more', 'shb-pageteaser'); ?></a>
</footer>
</article>
<?php
$html = ob_get_contents();
// Allow modification via filter
$html = apply_filters('shb-pageteaser/html', $html, $block);
ob_end_clean();
return $html;
}