PluginProbe
Same Category Posts / trunk
Same Category Posts vtrunk
1.2.0 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 trunk 1.0 1.0.1 1.0.10 1.0.11 1.0.12 All 36 releases
same-category-posts / same-posts-block.php

same-posts-block.php in Same Category Posts trunk, at same-posts-block.php

100 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Gutenberg Block implementation.
4 *
5 * @package samePosts.
6 *
7 * @since 4.9
8 */
9
10 namespace samePosts;
11
12 require_once __DIR__ . '/includes/block-attributes.php';
13
14 /**
15 * Markup wrapping the title in the block.
16 *
17 * The classic widget gets before_title / after_title from the sidebar it sits
18 * in. A block has no such args, so it brings its own wrapper.
19 */
20 const BLOCK_BEFORE_TITLE = '<h2 class="widget-title">';
21 const BLOCK_AFTER_TITLE = '</h2>';
22
23 /**
24 * Renders the `tiptip/same-posts-block` on the server.
25 *
26 * Uses the same render core as the classic widget, so both produce the same
27 * list. The attributes are translated into a widget instance first -- see
28 * includes/block-attributes.php for why that step cannot be skipped.
29 *
30 * @param array $attributes The block attributes.
31 * @param string $content The block's inner content, unused.
32 * @param \WP_Block $block The block instance, carries the postId context.
33 *
34 * @return string The rendered block content.
35 */
36 function render_same_posts_block( $attributes, $content = '', $block = null ) {
37 $current_post_id = current_post_id( $block );
38
39 // Nothing to relate to: no post, and not a term archive either.
40 if ( ! $current_post_id && ! is_archive() ) {
41 return '';
42 }
43
44 $widget = new Widget();
45 $instance = block_attributes_to_instance( $attributes );
46
47 $html = $widget->render_html( $instance, $current_post_id, BLOCK_BEFORE_TITLE, BLOCK_AFTER_TITLE );
48
49 if ( '' === $html ) {
50 return '';
51 }
52
53 return sprintf( '<div %1$s>%2$s</div>', get_block_wrapper_attributes(), $html );
54 }
55
56 /**
57 * Determines the post the block relates to.
58 *
59 * Three contexts have to be served:
60 *
61 * - a term archive, where the queried term rather than a post is the basis of
62 * the query -- 0 tells the render core to use the queried object;
63 * - a single post or a post template, where the postId context is set;
64 * - the editor's preview, which arrives through the block-renderer REST route
65 * without any query context. WordPress sets up the post from the post_id
66 * that ServerSideRender sends along, so get_the_ID() answers there too.
67 *
68 * @param \WP_Block|null $block The block instance.
69 *
70 * @return int The post id, or 0 when there is none.
71 */
72 function current_post_id( $block ) {
73 if ( is_archive() ) {
74 return 0;
75 }
76
77 if ( $block instanceof \WP_Block && isset( $block->context['postId'] ) ) {
78 return (int) $block->context['postId'];
79 }
80
81 return (int) get_the_ID();
82 }
83
84 /**
85 * Registers the block using the metadata loaded from the `block.json` file.
86 * Behind the scenes, it registers also all assets so they can be enqueued
87 * through the block editor in the corresponding context.
88 *
89 * @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/writing-your-first-block-type/
90 */
91 function same_posts_block_init() {
92 register_block_type(
93 __DIR__,
94 array(
95 'render_callback' => __NAMESPACE__ . '\render_same_posts_block',
96 )
97 );
98 }
99 add_action( 'init', __NAMESPACE__ . '\same_posts_block_init' );
100