|
8 | 8 |
|
9 | 9 | namespace ElasticPress\Feature\DidYouMean; |
10 | 10 |
|
11 | | -use ElasticPress\{Elasticsearch, Feature, FeatureRequirementsStatus, Features }; |
| 11 | +use ElasticPress\{Elasticsearch, Feature, FeatureRequirementsStatus, Utils }; |
12 | 12 |
|
13 | 13 | /** |
14 | 14 | * Did You Mean feature class. |
@@ -61,6 +61,91 @@ public function setup() { |
61 | 61 | add_filter( 'ep_integrate_search_queries', [ $this, 'set_ep_suggestion' ], 10, 2 ); |
62 | 62 | add_action( 'template_redirect', [ $this, 'automatically_redirect_user' ] ); |
63 | 63 | add_action( 'ep_suggestions', [ $this, 'the_output' ] ); |
| 64 | + |
| 65 | + if ( $this->is_block_enabled_in_editor() ) { |
| 66 | + add_action( 'init', [ $this, 'register_block' ] ); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Check if Did You Mean block should be enabled in the editor. |
| 72 | + * |
| 73 | + * @since 5.4.0 |
| 74 | + * @return bool |
| 75 | + */ |
| 76 | + protected function is_block_enabled_in_editor(): bool { |
| 77 | + global $pagenow; |
| 78 | + |
| 79 | + $in_editor = in_array( $pagenow, [ 'post-new.php', 'post.php' ], true ); |
| 80 | + |
| 81 | + /** |
| 82 | + * Filter if Did You Mean block should be enabled in the post editor. |
| 83 | + * |
| 84 | + * @since 5.4.0 |
| 85 | + * @hook ep_did_you_mean_enabled_in_editor |
| 86 | + * @param {bool} $enabled If enabled or not. |
| 87 | + * @return {bool} If enabled or not. |
| 88 | + */ |
| 89 | + return ! ( $in_editor && ! apply_filters( 'ep_did_you_mean_enabled_in_editor', false ) ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Register Did You Mean block. |
| 94 | + * |
| 95 | + * @return void |
| 96 | + * @since 5.4.0 |
| 97 | + */ |
| 98 | + public function register_block(): void { |
| 99 | + /** |
| 100 | + * Registering it here so translation works |
| 101 | + * |
| 102 | + * @see https://core.trac.wordpress.org/ticket/54797#comment:20 |
| 103 | + */ |
| 104 | + wp_register_script( |
| 105 | + 'ep-did-you-mean-block-script', |
| 106 | + EP_URL . 'dist/js/did-you-mean-block-script.js', |
| 107 | + Utils\get_asset_info( 'did-you-mean-block-script', 'dependencies' ), |
| 108 | + Utils\get_asset_info( 'did-you-mean-block-script', 'version' ), |
| 109 | + true |
| 110 | + ); |
| 111 | + |
| 112 | + wp_set_script_translations( 'ep-did-you-mean-block-script', 'elasticpress' ); |
| 113 | + |
| 114 | + register_block_type_from_metadata( |
| 115 | + EP_PATH . 'assets/js/blocks/did-you-mean', |
| 116 | + [ |
| 117 | + 'render_callback' => [ $this, 'render_block' ], |
| 118 | + ] |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Render Did You Mean block. |
| 124 | + * |
| 125 | + * @param array $attributes Block attributes. |
| 126 | + * @return string |
| 127 | + * @since 5.4.0 |
| 128 | + */ |
| 129 | + public function render_block( array $attributes = [] ): string { |
| 130 | + ob_start(); |
| 131 | + do_action( 'ep_suggestions' ); |
| 132 | + $output = ob_get_clean(); |
| 133 | + |
| 134 | + if ( empty( $output ) ) { |
| 135 | + return ''; |
| 136 | + } |
| 137 | + |
| 138 | + $wrapper_attributes = get_block_wrapper_attributes( |
| 139 | + [ |
| 140 | + 'class' => 'wp-block-elasticpress-did-you-mean', |
| 141 | + ] |
| 142 | + ); |
| 143 | + |
| 144 | + return sprintf( |
| 145 | + '<div %1$s>%2$s</div>', |
| 146 | + wp_kses_data( $wrapper_attributes ), |
| 147 | + wp_kses_post( $output ) |
| 148 | + ); |
64 | 149 | } |
65 | 150 |
|
66 | 151 | /** |
|
0 commit comments