PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.44.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.44.0
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 3.44.0, at blocks/related-posts.php

109 lines 3.1 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', STAGS_URL . '/blocks/src/related-posts.js',
21 ['wp-blocks', 'wp-element', 'wp-components', 'wp-editor']
22 );
23
24 // Register and enqueue frontend styles for block editor
25 wp_register_style(
26 'taxopress-frontend-css',
27 STAGS_URL . '/assets/frontend/css/frontend.css',
28 [],
29 STAGS_VERSION,
30 'all'
31 );
32 add_action('enqueue_block_editor_assets', function () {
33 if (is_admin() && function_exists('get_current_screen')) {
34 $screen = get_current_screen();
35 if ($screen && $screen->is_block_editor()) {
36 wp_enqueue_style('taxopress-frontend-css');
37 }
38 }
39 });
40
41 // Register our block, and explicitly define the attributes we accept.
42 $relatedposts_data = taxopress_get_relatedpost_data();
43
44 $options = [];
45 $default = '';
46 if (count($relatedposts_data) > 0) {
47 $sn = 0;
48 foreach ($relatedposts_data as $key => $value) {
49 $sn++;
50 if ($sn === 1) {
51 $default = $key;
52 }
53 $options[] = ['label' => $value['title'], 'value' => $key];
54 }
55 }
56
57 register_block_type('taxopress/related-posts', [
58 'attributes' => [
59 'relatedpost_id' => [
60 'type' => 'string',
61 'default' => $default,
62 ],
63 'post_id' => [
64 'type' => 'string',
65 ],
66 ],
67 'editor_script' => 'st-block-related-posts',
68 'render_callback' => 'st_related_posts_render',
69 ]);
70
71 $shortcode_page = sprintf(
72 '<a href="%s">%s</a>',
73 add_query_arg(
74 [
75 'page' => 'st_related_posts',
76 ],
77 admin_url('admin.php')
78 ),
79 __('this page.', 'simple-tags')
80 );
81
82 //$select_desc = __('Related Posts shortcode are added on Related Post screen', 'simple-tags');
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 //'select_desc' => $select_desc
91 ]);
92
93 }
94
95 add_action('init', 'st_related_posts_block_init');
96
97 /**
98 * Our combined block and shortcode renderer.
99 *
100 * @param array $attributes The attributes that were set on the block or shortcode.
101 */
102 function st_related_posts_render($attributes)
103 {
104 global $post;
105 ob_start();
106 echo do_shortcode('[taxopress_relatedposts id="' . $attributes['relatedpost_id'] . '"]');
107
108 return ob_get_clean();
109 }