| @@ -7,18 +7,27 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ElasticPress\Feature\Comments; |
| 10 | 10 | |
| 11 | -use ElasticPress\Feature as Feature; | |
| 12 | -use ElasticPress\Indexables as Indexables; | |
| 13 | -use ElasticPress\Indexable as Indexable; | |
| 14 | -use ElasticPress\Features as Features; | |
| 15 | -use ElasticPress\FeatureRequirementsStatus as FeatureRequirementsStatus; | |
| 11 | +use ElasticPress\Feature; | |
| 12 | +use ElasticPress\FeatureRequirementsStatus; | |
| 13 | +use ElasticPress\Features; | |
| 14 | +use ElasticPress\Indexable; | |
| 15 | +use ElasticPress\Indexables; | |
| 16 | +use ElasticPress\Utils; | |
| 17 | +use ElasticPress\REST; | |
| 16 | 18 | |
| 17 | 19 | /** |
| 18 | 20 | * Comments feature class |
| 19 | 21 | */ |
| 20 | 22 | class Comments extends Feature { |
| 23 | + /** | |
| 24 | + * Whether the feature should be always visible in the dashboard | |
| 25 | + * | |
| 26 | + * @since 5.0.0 | |
| 27 | + * @var boolean | |
| 28 | + */ | |
| 29 | + protected $is_visible = false; | |
| 21 | 30 | |
| 22 | 31 | /** |
| 23 | 32 | * Initialize feature, setting it's config |
| 24 | 33 | * |
| @@ -26,17 +35,27 @@ | ||
| 26 | 35 | */ |
| 27 | 36 | public function __construct() { |
| 28 | 37 | $this->slug = 'comments'; |
| 29 | 38 | |
| 30 | - $this->title = esc_html__( 'Comments', 'elasticpress' ); | |
| 39 | + $this->requires_install_reindex = true; | |
| 31 | 40 | |
| 32 | - $this->summary = __( 'Improve comment search relevancy and query performance.', 'elasticpress' ); | |
| 41 | + Indexables::factory()->register( new Indexable\Comment\Comment(), false ); | |
| 33 | 42 | |
| 34 | - $this->docs_url = __( 'https://elasticpress.zendesk.com/hc/en-us/articles/360050447492-Configuring-ElasticPress-via-the-Plugin-Dashboard#comments', 'elasticpress' ); | |
| 43 | + parent::__construct(); | |
| 44 | + } | |
| 35 | 45 | |
| 36 | - $this->requires_install_reindex = true; | |
| 46 | + /** | |
| 47 | + * Sets i18n strings. | |
| 48 | + * | |
| 49 | + * @return void | |
| 50 | + * @since 5.2.0 | |
| 51 | + */ | |
| 52 | + public function set_i18n_strings(): void { | |
| 53 | + $this->title = esc_html__( 'Comments', 'elasticpress' ); | |
| 37 | 54 | |
| 38 | - parent::__construct(); | |
| 55 | + $this->summary = '<p>' . __( 'This feature will empower your website to overcome traditional WordPress comment search and query limitations that can present themselves at scale. This feature is only needed if you are using <code>WP_Comment_Query</code> directly.', 'elasticpress' ) . '</p>'; | |
| 56 | + | |
| 57 | + $this->docs_url = __( 'https://www.elasticpress.io/resources/articles/configuring-elasticpress-via-the-plugin-dashboard/#comments', 'elasticpress' ); | |
| 39 | 58 | } |
| 40 | 59 | |
| 41 | 60 | /** |
| 42 | 61 | * Setup search functionality |
| @@ -43,13 +62,16 @@ | ||
| 43 | 62 | * |
| 44 | 63 | * @since 3.6.0 |
| 45 | 64 | */ |
| 46 | 65 | public function setup() { |
| 47 | - Indexables::factory()->register( new Indexable\Comment\Comment() ); | |
| 66 | + Indexables::factory()->activate( 'comment' ); | |
| 48 | 67 | |
| 68 | + add_action( 'init', [ $this, 'register_block' ] ); | |
| 49 | 69 | add_action( 'init', [ $this, 'search_setup' ] ); |
| 50 | 70 | add_action( 'widgets_init', [ $this, 'register_widget' ] ); |
| 51 | 71 | add_action( 'rest_api_init', [ $this, 'rest_api_init' ] ); |
| 72 | + add_action( 'wp_enqueue_scripts', [ $this, 'frontend_scripts' ] ); | |
| 73 | + add_filter( 'widget_types_to_hide_from_legacy_widget_block', [ $this, 'hide_legacy_widget' ] ); | |
| 52 | 74 | } |
| 53 | 75 | |
| 54 | 76 | /** |
| 55 | 77 | * Setup search integration |
| @@ -81,19 +103,8 @@ | ||
| 81 | 103 | add_filter( 'ep_elasticpress_enabled', [ $this, 'integrate_search_queries' ], 10, 2 ); |
| 82 | 104 | } |
| 83 | 105 | |
| 84 | 106 | /** |
| 85 | - * Output feature box long text | |
| 86 | - * | |
| 87 | - * @since 3.6.0 | |
| 88 | - */ | |
| 89 | - public function output_feature_box_long() { | |
| 90 | - ?> | |
| 91 | - <p><?php esc_html_e( 'This feature will empower your website to overcome traditional WordPress comment search and query limitations that can present themselves at scale.', 'elasticpress' ); ?></p> | |
| 92 | - <?php | |
| 93 | - } | |
| 94 | - | |
| 95 | - /** | |
| 96 | 107 | * Enable integration on search queries |
| 97 | 108 | * |
| 98 | 109 | * @param bool $enabled Whether EP is enabled |
| 99 | 110 | * @param \WP_Comment_Query $query Current query object. |
| @@ -120,9 +131,9 @@ | ||
| 120 | 131 | * @since 3.6.0 |
| 121 | 132 | * @return FeatureRequirementsStatus |
| 122 | 133 | */ |
| 123 | 134 | public function requirements_status() { |
| 124 | - $status = new FeatureRequirementsStatus( 1 ); | |
| 135 | + $status = new FeatureRequirementsStatus( 1, null, $this ); | |
| 125 | 136 | |
| 126 | 137 | return $status; |
| 127 | 138 | } |
| 128 | 139 | |
| @@ -140,115 +151,176 @@ | ||
| 140 | 151 | * |
| 141 | 152 | * @since 3.6.0 |
| 142 | 153 | */ |
| 143 | 154 | public function rest_api_init() { |
| 144 | - register_rest_route( | |
| 145 | - 'elasticpress/v1', | |
| 146 | - 'comments', | |
| 147 | - [ | |
| 148 | - 'methods' => 'GET', | |
| 149 | - 'callback' => [ $this, 'handle_comments_search' ], | |
| 150 | - 'permission_callback' => '__return_true', | |
| 151 | - 'args' => [ | |
| 152 | - 's' => [ | |
| 153 | - 'validate_callback' => function ( $param ) { | |
| 154 | - return ! empty( $param ); | |
| 155 | - }, | |
| 156 | - 'required' => true, | |
| 157 | - ], | |
| 158 | - 'post_type' => [ | |
| 159 | - 'validate_callback' => function ( $param ) { | |
| 160 | - return ! empty( $param ); | |
| 161 | - }, | |
| 162 | - ], | |
| 163 | - ], | |
| 164 | - ] | |
| 165 | - ); | |
| 155 | + $controller = new REST\Comments(); | |
| 156 | + $controller->register_routes(); | |
| 166 | 157 | } |
| 167 | 158 | |
| 168 | 159 | /** |
| 169 | - * Handles the search for comments | |
| 160 | + * Get a list of searchable post types that support comments. | |
| 170 | 161 | * |
| 171 | - * @since 3.6.0 | |
| 172 | - * @param \WP_REST_Request $request Rest request | |
| 173 | - * @return array | |
| 162 | + * @return array Array of post type labels keyed by post type. | |
| 163 | + * @since 4.4.0 | |
| 174 | 164 | */ |
| 175 | - public function handle_comments_search( $request ) { | |
| 176 | - $search = $request->get_param( 's' ); | |
| 165 | + public static function get_searchable_post_types() { | |
| 166 | + $searchable_post_types = array(); | |
| 177 | 167 | |
| 178 | - if ( empty( $search ) ) { | |
| 179 | - return new \WP_Error( 400 ); | |
| 180 | - } | |
| 181 | - | |
| 182 | - $post_type_filter = explode( ',', $request->get_param( 'post_type' ) ); | |
| 183 | - $searchable_post_types = array_filter( | |
| 184 | - Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types(), | |
| 168 | + $post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types(); | |
| 169 | + $post_types = array_filter( | |
| 170 | + $post_types, | |
| 185 | 171 | function ( $post_type ) { |
| 186 | 172 | return post_type_supports( $post_type, 'comments' ); |
| 187 | 173 | } |
| 188 | 174 | ); |
| 189 | 175 | |
| 190 | - if ( ! empty( $post_type_filter ) && is_array( $searchable_post_types ) ) { | |
| 191 | - $post_type_filter = array_intersect( $post_type_filter, $searchable_post_types ); | |
| 176 | + foreach ( $post_types as $post_type ) { | |
| 177 | + $post_type_object = get_post_type_object( $post_type ); | |
| 178 | + $post_type_labels = get_post_type_labels( $post_type_object ); | |
| 179 | + | |
| 180 | + $searchable_post_types[ $post_type ] = $post_type_labels->name; | |
| 192 | 181 | } |
| 193 | 182 | |
| 194 | - $default_args = [ | |
| 195 | - 'status' => 'approve', | |
| 196 | - 'search' => $search, | |
| 197 | - 'type' => Indexables::factory()->get( 'comment' )->get_indexable_comment_types(), | |
| 198 | - 'post_type' => empty( $post_type_filter ) ? $searchable_post_types : $post_type_filter, | |
| 199 | - 'post_status' => 'publish', | |
| 200 | - 'number' => 5, | |
| 183 | + return $searchable_post_types; | |
| 184 | + } | |
| 185 | + | |
| 186 | + /** | |
| 187 | + * Enqueue frontend scripts. | |
| 188 | + * | |
| 189 | + * @since 4.4.0 | |
| 190 | + */ | |
| 191 | + public function frontend_scripts() { | |
| 192 | + wp_register_script( | |
| 193 | + 'elasticpress-comments', | |
| 194 | + EP_URL . 'dist/js/comments-script.js', | |
| 195 | + Utils\get_asset_info( 'comments-script', 'dependencies' ), | |
| 196 | + Utils\get_asset_info( 'comments-script', 'version' ), | |
| 197 | + true | |
| 198 | + ); | |
| 199 | + | |
| 200 | + wp_set_script_translations( 'elasticpress-comments', 'elasticpress' ); | |
| 201 | + | |
| 202 | + wp_register_style( | |
| 203 | + 'elasticpress-comments', | |
| 204 | + EP_URL . 'dist/css/comments-styles.css', | |
| 205 | + Utils\get_asset_info( 'comments-styles', 'dependencies' ), | |
| 206 | + Utils\get_asset_info( 'comments-styles', 'version' ) | |
| 207 | + ); | |
| 208 | + | |
| 209 | + $default_script_data = [ | |
| 210 | + 'noResultsFoundText' => esc_html__( 'We could not find any results', 'elasticpress' ), | |
| 211 | + 'minimumLengthToSearch' => 2, | |
| 212 | + 'restApiEndpoint' => get_rest_url( null, 'elasticpress/v1/comments' ), | |
| 201 | 213 | ]; |
| 202 | 214 | |
| 203 | 215 | /** |
| 204 | - * Filter to args used in WP_Comment_Query in Widget Search Comment | |
| 216 | + * Filter the l10n data attached to the Widget Search Comments script | |
| 205 | 217 | * |
| 206 | - * @hook ep_comment_search_widget_args | |
| 207 | - * @since 3.6.0 | |
| 208 | - * @param {array} $default_args Defaults args | |
| 209 | - * @return {array} New value | |
| 218 | + * @since 3.6.0 | |
| 219 | + * @hook ep_comment_search_widget_l10n_data_script | |
| 220 | + * @param {array} $default_script_data Default data attached to the script | |
| 221 | + * @return {array} New l10n data to be attached | |
| 210 | 222 | */ |
| 211 | - $args = apply_filters( 'ep_comment_search_widget_args', $default_args ); | |
| 223 | + $script_data = apply_filters( 'ep_comment_search_widget_l10n_data_script', $default_script_data ); | |
| 212 | 224 | |
| 225 | + wp_localize_script( | |
| 226 | + 'elasticpress-comments', | |
| 227 | + 'epc', | |
| 228 | + $script_data | |
| 229 | + ); | |
| 230 | + } | |
| 231 | + | |
| 232 | + /** | |
| 233 | + * Register block. | |
| 234 | + * | |
| 235 | + * @since 4.4.0 | |
| 236 | + */ | |
| 237 | + public function register_block() { | |
| 213 | 238 | /** |
| 214 | - * Fires before the comment query is executed. | |
| 239 | + * Registering it here so translation works | |
| 215 | 240 | * |
| 216 | - * @hook ep_comment_pre_search_widget | |
| 217 | - * @since 3.6.0 | |
| 218 | - * @param {array} $args Args passed to `WP_Comment_Query`. | |
| 219 | - * @param {WP_REST_Request} $request Rest request. | |
| 241 | + * @see https://core.trac.wordpress.org/ticket/54797#comment:20 | |
| 220 | 242 | */ |
| 221 | - do_action( 'ep_comment_pre_search_widget', $args, $request ); | |
| 243 | + wp_register_script( | |
| 244 | + 'elasticpress-comments-editor-script', | |
| 245 | + EP_URL . 'dist/js/comments-block-script.js', | |
| 246 | + Utils\get_asset_info( 'comments-block-script', 'dependencies' ), | |
| 247 | + Utils\get_asset_info( 'comments-block-script', 'version' ), | |
| 248 | + true | |
| 249 | + ); | |
| 222 | 250 | |
| 223 | - $comment_query = new \WP_Comment_Query( $args ); | |
| 251 | + wp_set_script_translations( 'elasticpress-comments-editor-script', 'elasticpress' ); | |
| 224 | 252 | |
| 225 | - /** | |
| 226 | - * Fires after the comment query is executed. | |
| 227 | - * | |
| 228 | - * @hook ep_comment_after_search_widget | |
| 229 | - * @since 3.6.0 | |
| 230 | - * @param {WP_Comment_Query} $comment_query WP_Comment_Query object. | |
| 231 | - * @param {WP_REST_Request} $request Rest request. | |
| 232 | - */ | |
| 233 | - do_action( 'ep_comment_after_search_widget', $comment_query, $request ); | |
| 253 | + wp_localize_script( | |
| 254 | + 'elasticpress-comments-editor-script', | |
| 255 | + 'epComments', | |
| 256 | + [ | |
| 257 | + 'searchablePostTypes' => self::get_searchable_post_types(), | |
| 258 | + ] | |
| 259 | + ); | |
| 234 | 260 | |
| 235 | - $return = []; | |
| 236 | - foreach ( $comment_query->comments as $comment ) { | |
| 237 | - $return[ $comment->comment_ID ] = [ | |
| 238 | - 'id' => $comment->comment_ID, | |
| 239 | - 'content' => $comment->comment_content, | |
| 240 | - 'link' => get_comment_link( $comment ), | |
| 241 | - ]; | |
| 242 | - } | |
| 261 | + register_block_type_from_metadata( | |
| 262 | + EP_PATH . 'assets/js/blocks/comments', | |
| 263 | + [ | |
| 264 | + 'render_callback' => [ $this, 'render_block' ], | |
| 265 | + ] | |
| 266 | + ); | |
| 267 | + } | |
| 243 | 268 | |
| 244 | - /** | |
| 245 | - * Filters the comments response | |
| 246 | - * | |
| 247 | - * @hook ep_comment_search_widget_response | |
| 248 | - * @since 3.6.0 | |
| 249 | - * @param {array} $return The result of fetched comments. | |
| 250 | - * @return {array} New value | |
| 251 | - */ | |
| 252 | - return apply_filters( 'ep_comment_search_widget_response', $return ); | |
| 269 | + /** | |
| 270 | + * Render block. | |
| 271 | + * | |
| 272 | + * @param array $attributes Block attributes | |
| 273 | + * @since 4.4.0 | |
| 274 | + * @return string | |
| 275 | + */ | |
| 276 | + public function render_block( $attributes ) { | |
| 277 | + $wrapper_id = 'ep-search-comments-' . uniqid(); | |
| 278 | + $wrapper_attributes = get_block_wrapper_attributes( | |
| 279 | + array( | |
| 280 | + 'id' => $wrapper_id, | |
| 281 | + 'class' => 'ep-widget-search-comments', | |
| 282 | + ) | |
| 283 | + ); | |
| 284 | + | |
| 285 | + $label = ! empty( $attributes['label'] ) | |
| 286 | + ? sprintf( | |
| 287 | + '<label for="%1$s-s">%2$s</label>', | |
| 288 | + esc_attr( $wrapper_id ), | |
| 289 | + wp_kses_post( $attributes['label'] ) | |
| 290 | + ) | |
| 291 | + : ''; | |
| 292 | + | |
| 293 | + $post_types_input = ! empty( $attributes['postTypes'] ) | |
| 294 | + ? sprintf( | |
| 295 | + '<input class="ep-widget-search-comments-post-type" type="hidden" id="%1$s-post-type" value="%2$s">', | |
| 296 | + esc_attr( $wrapper_id ), | |
| 297 | + esc_attr( implode( ',', $attributes['postTypes'] ) ) | |
| 298 | + ) | |
| 299 | + : ''; | |
| 300 | + | |
| 301 | + $block_html = sprintf( | |
| 302 | + '<div %1$s>%2$s%3$s</div>', | |
| 303 | + $wrapper_attributes, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 304 | + $label, | |
| 305 | + $post_types_input | |
| 306 | + ); | |
| 307 | + | |
| 308 | + return $block_html; | |
| 309 | + } | |
| 310 | + | |
| 311 | + /** | |
| 312 | + * Hide the legacy widget. | |
| 313 | + * | |
| 314 | + * Hides the legacy widget in favor of the Block when the block editor | |
| 315 | + * is in use and the legacy widget has not been used. | |
| 316 | + * | |
| 317 | + * @since 4.4.0 | |
| 318 | + * @param array $widgets An array of excluded widget-type IDs. | |
| 319 | + * @return array array of excluded widget-type IDs to hide. | |
| 320 | + */ | |
| 321 | + public function hide_legacy_widget( $widgets ) { | |
| 322 | + $widgets[] = 'ep-comments'; | |
| 323 | + | |
| 324 | + return $widgets; | |
| 253 | 325 | } |
| 254 | 326 | } |