| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Functions to register client-side assets (scripts and stylesheets) for the |
| 5 |
* Gutenberg block. |
| 6 |
* |
| 7 |
* @package betterdocs |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Registers all block assets so that they can be enqueued through Gutenberg in |
| 12 |
* the corresponding context. |
| 13 |
* |
| 14 |
* @see https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/block-tutorial/applying-styles-with-stylesheets/ |
| 15 |
*/ |
| 16 |
function betterdocs_searchbox_block_init() |
| 17 |
{ |
| 18 |
// Skip block registration if Gutenberg is not enabled/merged. |
| 19 |
if (!function_exists('register_block_type')) { |
| 20 |
return; |
| 21 |
} |
| 22 |
$dir = dirname(__FILE__); |
| 23 |
|
| 24 |
$index_js = 'searchbox/index.js'; |
| 25 |
wp_register_script( |
| 26 |
'betterdocs-searchbox-block-editor', |
| 27 |
plugins_url($index_js, __FILE__), |
| 28 |
array( |
| 29 |
'wp-blocks', |
| 30 |
'wp-i18n', |
| 31 |
'wp-element', |
| 32 |
'wp-editor', |
| 33 |
'wp-block-editor', |
| 34 |
'betterdocs-blocks-edit-post' |
| 35 |
), |
| 36 |
filemtime("$dir/$index_js") |
| 37 |
); |
| 38 |
|
| 39 |
$editor_style = 'searchbox/style.css'; |
| 40 |
wp_register_style( |
| 41 |
'betterdocs-searchbox-block-editor', |
| 42 |
plugins_url($editor_style, __FILE__), |
| 43 |
array(), |
| 44 |
filemtime("$dir/$editor_style"), |
| 45 |
'all' |
| 46 |
); |
| 47 |
|
| 48 |
register_block_type(__DIR__ . '/searchbox', array( |
| 49 |
'editor_script' => 'betterdocs-searchbox-block-editor', |
| 50 |
'editor_style' => 'betterdocs-searchbox-block-editor', |
| 51 |
'render_callback' => 'betterdocs_searchbox_server_side_render' |
| 52 |
)); |
| 53 |
} |
| 54 |
add_action('init', 'betterdocs_searchbox_block_init'); |
| 55 |
|
| 56 |
/** |
| 57 |
* Search Box Server Side Render |
| 58 |
*/ |
| 59 |
function betterdocs_searchbox_server_side_render($attributes) |
| 60 |
{ |
| 61 |
|
| 62 |
if (!is_admin()) { |
| 63 |
wp_enqueue_style('betterdocs-searchbox-block-editor'); |
| 64 |
} |
| 65 |
|
| 66 |
$attributes = wp_parse_args( |
| 67 |
$attributes, |
| 68 |
[ |
| 69 |
'blockId' => '', |
| 70 |
'placeholderText' => esc_html__('Search', 'betterdocs'), |
| 71 |
] |
| 72 |
); |
| 73 |
|
| 74 |
$blockId = $attributes['blockId']; |
| 75 |
$placeholderText = $attributes['placeholderText']; |
| 76 |
|
| 77 |
|
| 78 |
$html = ''; |
| 79 |
$html .= '<div class="' . $blockId . ' betterdocs-searchbox-wrapper">'; |
| 80 |
$shortcode = sprintf('[betterdocs_search_form placeholder="' . $placeholderText . '"]', apply_filters('betterdocs_search_form_atts', [])); |
| 81 |
$html .= do_shortcode(shortcode_unautop($shortcode)); |
| 82 |
$html .= '</div>'; |
| 83 |
|
| 84 |
return $html; |
| 85 |
} |
| 86 |
|