| @@ -7,19 +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 | 16 | use ElasticPress\Utils; |
| 17 | +use ElasticPress\REST; | |
| 17 | 18 | |
| 18 | 19 | /** |
| 19 | 20 | * Comments feature class |
| 20 | 21 | */ |
| 21 | 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; | |
| 22 | 30 | |
| 23 | 31 | /** |
| 24 | 32 | * Initialize feature, setting it's config |
| 25 | 33 | * |
| @@ -27,17 +35,27 @@ | ||
| 27 | 35 | */ |
| 28 | 36 | public function __construct() { |
| 29 | 37 | $this->slug = 'comments'; |
| 30 | 38 | |
| 31 | - $this->title = esc_html__( 'Comments', 'elasticpress' ); | |
| 39 | + $this->requires_install_reindex = true; | |
| 32 | 40 | |
| 33 | - $this->summary = __( 'Improve comment search relevancy and query performance.', 'elasticpress' ); | |
| 41 | + Indexables::factory()->register( new Indexable\Comment\Comment(), false ); | |
| 34 | 42 | |
| 35 | - $this->docs_url = __( 'https://elasticpress.zendesk.com/hc/en-us/articles/360050447492-Configuring-ElasticPress-via-the-Plugin-Dashboard#comments', 'elasticpress' ); | |
| 43 | + parent::__construct(); | |
| 44 | + } | |
| 36 | 45 | |
| 37 | - $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' ); | |
| 38 | 54 | |
| 39 | - 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' ); | |
| 40 | 58 | } |
| 41 | 59 | |
| 42 | 60 | /** |
| 43 | 61 | * Setup search functionality |
| @@ -44,9 +62,9 @@ | ||
| 44 | 62 | * |
| 45 | 63 | * @since 3.6.0 |
| 46 | 64 | */ |
| 47 | 65 | public function setup() { |
| 48 | - Indexables::factory()->register( new Indexable\Comment\Comment() ); | |
| 66 | + Indexables::factory()->activate( 'comment' ); | |
| 49 | 67 | |
| 50 | 68 | add_action( 'init', [ $this, 'register_block' ] ); |
| 51 | 69 | add_action( 'init', [ $this, 'search_setup' ] ); |
| 52 | 70 | add_action( 'widgets_init', [ $this, 'register_widget' ] ); |
| @@ -85,19 +103,8 @@ | ||
| 85 | 103 | add_filter( 'ep_elasticpress_enabled', [ $this, 'integrate_search_queries' ], 10, 2 ); |
| 86 | 104 | } |
| 87 | 105 | |
| 88 | 106 | /** |
| 89 | - * Output feature box long text | |
| 90 | - * | |
| 91 | - * @since 3.6.0 | |
| 92 | - */ | |
| 93 | - public function output_feature_box_long() { | |
| 94 | - ?> | |
| 95 | - <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> | |
| 96 | - <?php | |
| 97 | - } | |
| 98 | - | |
| 99 | - /** | |
| 100 | 107 | * Enable integration on search queries |
| 101 | 108 | * |
| 102 | 109 | * @param bool $enabled Whether EP is enabled |
| 103 | 110 | * @param \WP_Comment_Query $query Current query object. |
| @@ -124,9 +131,9 @@ | ||
| 124 | 131 | * @since 3.6.0 |
| 125 | 132 | * @return FeatureRequirementsStatus |
| 126 | 133 | */ |
| 127 | 134 | public function requirements_status() { |
| 128 | - $status = new FeatureRequirementsStatus( 1 ); | |
| 135 | + $status = new FeatureRequirementsStatus( 1, null, $this ); | |
| 129 | 136 | |
| 130 | 137 | return $status; |
| 131 | 138 | } |
| 132 | 139 | |
| @@ -144,120 +151,13 @@ | ||
| 144 | 151 | * |
| 145 | 152 | * @since 3.6.0 |
| 146 | 153 | */ |
| 147 | 154 | public function rest_api_init() { |
| 148 | - register_rest_route( | |
| 149 | - 'elasticpress/v1', | |
| 150 | - 'comments', | |
| 151 | - [ | |
| 152 | - 'methods' => 'GET', | |
| 153 | - 'callback' => [ $this, 'handle_comments_search' ], | |
| 154 | - 'permission_callback' => '__return_true', | |
| 155 | - 'args' => [ | |
| 156 | - 's' => [ | |
| 157 | - 'validate_callback' => function ( $param ) { | |
| 158 | - return ! empty( $param ); | |
| 159 | - }, | |
| 160 | - 'required' => true, | |
| 161 | - ], | |
| 162 | - 'post_type' => [ | |
| 163 | - 'validate_callback' => function ( $param ) { | |
| 164 | - return ! empty( $param ); | |
| 165 | - }, | |
| 166 | - ], | |
| 167 | - ], | |
| 168 | - ] | |
| 169 | - ); | |
| 155 | + $controller = new REST\Comments(); | |
| 156 | + $controller->register_routes(); | |
| 170 | 157 | } |
| 171 | 158 | |
| 172 | 159 | /** |
| 173 | - * Handles the search for comments | |
| 174 | - * | |
| 175 | - * @since 3.6.0 | |
| 176 | - * @param \WP_REST_Request $request Rest request | |
| 177 | - * @return array | |
| 178 | - */ | |
| 179 | - public function handle_comments_search( $request ) { | |
| 180 | - $search = $request->get_param( 's' ); | |
| 181 | - | |
| 182 | - if ( empty( $search ) ) { | |
| 183 | - return new \WP_Error( 400 ); | |
| 184 | - } | |
| 185 | - | |
| 186 | - $post_type_filter = explode( ',', $request->get_param( 'post_type' ) ); | |
| 187 | - $searchable_post_types = array_filter( | |
| 188 | - Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types(), | |
| 189 | - function ( $post_type ) { | |
| 190 | - return post_type_supports( $post_type, 'comments' ); | |
| 191 | - } | |
| 192 | - ); | |
| 193 | - | |
| 194 | - if ( ! empty( $post_type_filter ) && is_array( $searchable_post_types ) ) { | |
| 195 | - $post_type_filter = array_intersect( $post_type_filter, $searchable_post_types ); | |
| 196 | - } | |
| 197 | - | |
| 198 | - $default_args = [ | |
| 199 | - 'status' => 'approve', | |
| 200 | - 'search' => $search, | |
| 201 | - 'type' => Indexables::factory()->get( 'comment' )->get_indexable_comment_types(), | |
| 202 | - 'post_type' => empty( $post_type_filter ) ? $searchable_post_types : $post_type_filter, | |
| 203 | - 'post_status' => 'publish', | |
| 204 | - 'number' => 5, | |
| 205 | - ]; | |
| 206 | - | |
| 207 | - /** | |
| 208 | - * Filter to args used in WP_Comment_Query in Widget Search Comment | |
| 209 | - * | |
| 210 | - * @hook ep_comment_search_widget_args | |
| 211 | - * @since 3.6.0 | |
| 212 | - * @param {array} $default_args Defaults args | |
| 213 | - * @return {array} New value | |
| 214 | - */ | |
| 215 | - $args = apply_filters( 'ep_comment_search_widget_args', $default_args ); | |
| 216 | - | |
| 217 | - /** | |
| 218 | - * Fires before the comment query is executed. | |
| 219 | - * | |
| 220 | - * @hook ep_comment_pre_search_widget | |
| 221 | - * @since 3.6.0 | |
| 222 | - * @param {array} $args Args passed to `WP_Comment_Query`. | |
| 223 | - * @param {WP_REST_Request} $request Rest request. | |
| 224 | - */ | |
| 225 | - do_action( 'ep_comment_pre_search_widget', $args, $request ); | |
| 226 | - | |
| 227 | - $comment_query = new \WP_Comment_Query( $args ); | |
| 228 | - | |
| 229 | - /** | |
| 230 | - * Fires after the comment query is executed. | |
| 231 | - * | |
| 232 | - * @hook ep_comment_after_search_widget | |
| 233 | - * @since 3.6.0 | |
| 234 | - * @param {WP_Comment_Query} $comment_query WP_Comment_Query object. | |
| 235 | - * @param {WP_REST_Request} $request Rest request. | |
| 236 | - */ | |
| 237 | - do_action( 'ep_comment_after_search_widget', $comment_query, $request ); | |
| 238 | - | |
| 239 | - $return = []; | |
| 240 | - foreach ( $comment_query->comments as $comment ) { | |
| 241 | - $return[ $comment->comment_ID ] = [ | |
| 242 | - 'id' => $comment->comment_ID, | |
| 243 | - 'content' => $comment->comment_content, | |
| 244 | - 'link' => get_comment_link( $comment ), | |
| 245 | - ]; | |
| 246 | - } | |
| 247 | - | |
| 248 | - /** | |
| 249 | - * Filters the comments response | |
| 250 | - * | |
| 251 | - * @hook ep_comment_search_widget_response | |
| 252 | - * @since 3.6.0 | |
| 253 | - * @param {array} $return The result of fetched comments. | |
| 254 | - * @return {array} New value | |
| 255 | - */ | |
| 256 | - return apply_filters( 'ep_comment_search_widget_response', $return ); | |
| 257 | - } | |
| 258 | - | |
| 259 | - /** | |
| 260 | 160 | * Get a list of searchable post types that support comments. |
| 261 | 161 | * |
| 262 | 162 | * @return array Array of post type labels keyed by post type. |
| 263 | 163 | * @since 4.4.0 |
| @@ -267,9 +167,9 @@ | ||
| 267 | 167 | |
| 268 | 168 | $post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types(); |
| 269 | 169 | $post_types = array_filter( |
| 270 | 170 | $post_types, |
| 271 | - function( $post_type ) { | |
| 171 | + function ( $post_type ) { | |
| 272 | 172 | return post_type_supports( $post_type, 'comments' ); |
| 273 | 173 | } |
| 274 | 174 | ); |
| 275 | 175 | |