| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Register our block. |
| 5 |
*/ |
| 6 |
function st_tag_clouds_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-tag-clouds', |
| 21 |
STAGS_URL . '/blocks/src/tag-clouds.js', |
| 22 |
['wp-blocks', 'wp-element', 'wp-components', 'wp-editor'] |
| 23 |
); |
| 24 |
|
| 25 |
// Register our block, and explicitly define the attributes we accept. |
| 26 |
$tagcloud_data = taxopress_get_tagcloud_data(); |
| 27 |
|
| 28 |
$options = []; |
| 29 |
$default = ''; |
| 30 |
if (count($tagcloud_data) > 0) { |
| 31 |
$sn = 0; |
| 32 |
foreach ($tagcloud_data as $key => $value) { |
| 33 |
$sn++; |
| 34 |
if ($sn === 1) { |
| 35 |
$default = $key; |
| 36 |
} |
| 37 |
$options[] = ['label' => $value['title'], 'value' => $key]; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
register_block_type('taxopress/tag-clouds', [ |
| 42 |
'attributes' => [ |
| 43 |
'tagcloud_id' => [ |
| 44 |
'type' => 'string', |
| 45 |
'default' => $default, |
| 46 |
], |
| 47 |
'post_id' => [ |
| 48 |
'type' => 'string', |
| 49 |
], |
| 50 |
], |
| 51 |
'editor_script' => 'st-block-tag-clouds', |
| 52 |
'render_callback' => 'st_tag_clouds_render', |
| 53 |
]); |
| 54 |
|
| 55 |
$shortcode_page = sprintf( |
| 56 |
'<a href="%s">%s</a>', |
| 57 |
add_query_arg( |
| 58 |
[ |
| 59 |
'page' => 'st_terms_display', |
| 60 |
], |
| 61 |
admin_url('admin.php') |
| 62 |
), |
| 63 |
esc_html__('this page.', 'simple-tags') |
| 64 |
); |
| 65 |
|
| 66 |
$select_label = __('Select Terms Display', 'simple-tags'); |
| 67 |
$panel_title = __('Terms Display (TaxoPress)', 'simple-tags'); |
| 68 |
|
| 69 |
wp_localize_script('st-block-tag-clouds', 'ST_TAG_CLOUDS', [ |
| 70 |
'options' => $options, |
| 71 |
'panel_title' => $panel_title, |
| 72 |
'select_label' => $select_label, |
| 73 |
]); |
| 74 |
} |
| 75 |
|
| 76 |
add_action('init', 'st_tag_clouds_block_init'); |
| 77 |
|
| 78 |
/** |
| 79 |
* Our combined block and shortcode renderer. |
| 80 |
* |
| 81 |
* @param array $attributes The attributes that were set on the block or shortcode. |
| 82 |
*/ |
| 83 |
function st_tag_clouds_render($attributes) |
| 84 |
{ |
| 85 |
global $post; |
| 86 |
ob_start(); |
| 87 |
echo do_shortcode('[taxopress_termsdisplay id="' . $attributes['tagcloud_id'] . '"]'); |
| 88 |
|
| 89 |
return ob_get_clean(); |
| 90 |
} |
| 91 |
|