PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / trunk
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms vtrunk
3.53.0 3.52.0 3.51.0 3.50.0 3.45.0 3.38.0 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.40.0 3.40.1 3.41.0 3.42.0 3.43.0 3.44.0 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 All 177 releases
simple-tags / blocks / related-posts.php

related-posts.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms trunk, at blocks/related-posts.php

114 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Register our block.
5 */
6 function st_related_posts_block_init()
7 {
8 global $post, $pagenow;
9
10 if (!function_exists('register_block_type')) {
11 return;
12 }
13
14 if ($pagenow === 'widgets.php') {
15 return;
16 }
17
18 // Register our block editor script.
19 wp_register_script(
20 'st-block-related-posts',
21 STAGS_URL . '/blocks/src/related-posts.js',
22 ['wp-blocks', 'wp-element', 'wp-components', 'wp-editor']
23 );
24
25 // Register and enqueue frontend styles for block editor
26 wp_register_style(
27 'taxopress-frontend-css',
28 STAGS_URL . '/assets/frontend/css/frontend.css',
29 [],
30 STAGS_VERSION,
31 'all'
32 );
33 add_action('enqueue_block_editor_assets', function () {
34 if (is_admin() && function_exists('get_current_screen')) {
35 $screen = get_current_screen();
36 if ($screen && $screen->is_block_editor()) {
37 wp_enqueue_style('taxopress-frontend-css');
38 }
39 }
40 });
41
42 // Register our block, and explicitly define the attributes we accept.
43 $relatedposts_data = taxopress_get_relatedpost_data();
44
45 $options = [];
46 $default = '';
47 if (count($relatedposts_data) > 0) {
48 $sn = 0;
49 foreach ($relatedposts_data as $key => $value) {
50 $sn++;
51 if ($sn === 1) {
52 $default = $key;
53 }
54 $options[] = ['label' => $value['title'], 'value' => $key];
55 }
56 }
57
58 register_block_type('taxopress/related-posts', [
59 'attributes' => [
60 'relatedpost_id' => [
61 'type' => 'string',
62 'default' => $default,
63 ],
64 'post_id' => [
65 'type' => 'string',
66 ],
67 ],
68 'editor_script' => 'st-block-related-posts',
69 'render_callback' => 'st_related_posts_render',
70 ]);
71
72 $shortcode_page = sprintf(
73 '<a href="%s">%s</a>',
74 add_query_arg(
75 [
76 'page' => 'st_related_posts',
77 ],
78 admin_url('admin.php')
79 ),
80 __('this page.', 'simple-tags')
81 );
82
83 $select_label = __('Select related post', 'simple-tags');
84 $panel_title = __('Related Posts (TaxoPress)', 'simple-tags');
85
86 wp_localize_script('st-block-related-posts', 'ST_RELATED_POST', [
87 'options' => $options,
88 'panel_title' => $panel_title,
89 'select_label' => $select_label,
90 ]);
91 }
92
93 add_action('init', 'st_related_posts_block_init');
94
95 /**
96 * Our combined block and shortcode renderer.
97 *
98 * @param array $attributes The attributes that were set on the block or shortcode.
99 */
100 function st_related_posts_render($attributes)
101 {
102 global $post;
103 ob_start();
104
105 $relatedpost_id = '';
106 if (is_array($attributes) && isset($attributes['relatedpost_id'])) {
107 $relatedpost_id = sanitize_text_field($attributes['relatedpost_id']);
108 }
109
110 echo do_shortcode('[taxopress_relatedposts id="' . $relatedpost_id . '"]');
111
112 return ob_get_clean();
113 }
114