| 1 |
<?php |
| 2 |
/** |
| 3 |
* Comments feature |
| 4 |
* |
| 5 |
* @since 3.6.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Feature\Comments; |
| 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; |
| 16 |
|
| 17 |
/** |
| 18 |
* Comments feature class |
| 19 |
*/ |
| 20 |
class Comments extends Feature { |
| 21 |
|
| 22 |
/** |
| 23 |
* Initialize feature, setting it's config |
| 24 |
* |
| 25 |
* @since 3.6.0 |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
$this->slug = 'comments'; |
| 29 |
|
| 30 |
$this->title = esc_html__( 'Comments', 'elasticpress' ); |
| 31 |
|
| 32 |
$this->summary = __( 'Improve comment search relevancy and query performance.', 'elasticpress' ); |
| 33 |
|
| 34 |
$this->docs_url = __( 'https://elasticpress.zendesk.com/hc/en-us/articles/360050447492-Configuring-ElasticPress-via-the-Plugin-Dashboard#comments', 'elasticpress' ); |
| 35 |
|
| 36 |
$this->requires_install_reindex = true; |
| 37 |
|
| 38 |
parent::__construct(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Setup search functionality |
| 43 |
* |
| 44 |
* @since 3.6.0 |
| 45 |
*/ |
| 46 |
public function setup() { |
| 47 |
Indexables::factory()->register( new Indexable\Comment\Comment() ); |
| 48 |
|
| 49 |
add_action( 'init', [ $this, 'search_setup' ] ); |
| 50 |
add_action( 'widgets_init', [ $this, 'register_widget' ] ); |
| 51 |
add_action( 'rest_api_init', [ $this, 'rest_api_init' ] ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Setup search integration |
| 56 |
* |
| 57 |
* @since 3.6.0 |
| 58 |
*/ |
| 59 |
public function search_setup() { |
| 60 |
$admin_integration = apply_filters( 'ep_admin_wp_query_integration', false ); |
| 61 |
|
| 62 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 63 |
/** |
| 64 |
* Filter to integrate with admin ajax queries |
| 65 |
* |
| 66 |
* @hook ep_ajax_wp_query_integration |
| 67 |
* @param {bool} $integrate True to integrate |
| 68 |
* @return {bool} New value |
| 69 |
*/ |
| 70 |
if ( ! apply_filters( 'ep_ajax_wp_query_integration', false ) ) { |
| 71 |
return; |
| 72 |
} else { |
| 73 |
$admin_integration = true; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if ( is_admin() && ! $admin_integration ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
add_filter( 'ep_elasticpress_enabled', [ $this, 'integrate_search_queries' ], 10, 2 ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 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 |
* Enable integration on search queries |
| 97 |
* |
| 98 |
* @param bool $enabled Whether EP is enabled |
| 99 |
* @param \WP_Comment_Query $query Current query object. |
| 100 |
* @since 3.6.0 |
| 101 |
* @return bool |
| 102 |
*/ |
| 103 |
public function integrate_search_queries( $enabled, $query ) { |
| 104 |
if ( ! is_a( $query, 'WP_Comment_Query' ) ) { |
| 105 |
return $enabled; |
| 106 |
} |
| 107 |
|
| 108 |
if ( isset( $query->query_vars['ep_integrate'] ) && ! filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOLEAN ) ) { |
| 109 |
$enabled = false; |
| 110 |
} elseif ( ! empty( $query->query_vars['search'] ) ) { |
| 111 |
$enabled = true; |
| 112 |
} |
| 113 |
|
| 114 |
return $enabled; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Determine feature reqs status |
| 119 |
* |
| 120 |
* @since 3.6.0 |
| 121 |
* @return FeatureRequirementsStatus |
| 122 |
*/ |
| 123 |
public function requirements_status() { |
| 124 |
$status = new FeatureRequirementsStatus( 1 ); |
| 125 |
|
| 126 |
return $status; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Register comments widget |
| 131 |
* |
| 132 |
* @since 3.6.0 |
| 133 |
*/ |
| 134 |
public function register_widget() { |
| 135 |
register_widget( __NAMESPACE__ . '\Widget' ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Registers the API endpoint to search for comments |
| 140 |
* |
| 141 |
* @since 3.6.0 |
| 142 |
*/ |
| 143 |
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 |
); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Handles the search for comments |
| 170 |
* |
| 171 |
* @since 3.6.0 |
| 172 |
* @param \WP_REST_Request $request Rest request |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
public function handle_comments_search( $request ) { |
| 176 |
$search = $request->get_param( 's' ); |
| 177 |
|
| 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(), |
| 185 |
function ( $post_type ) { |
| 186 |
return post_type_supports( $post_type, 'comments' ); |
| 187 |
} |
| 188 |
); |
| 189 |
|
| 190 |
if ( ! empty( $post_type_filter ) && is_array( $searchable_post_types ) ) { |
| 191 |
$post_type_filter = array_intersect( $post_type_filter, $searchable_post_types ); |
| 192 |
} |
| 193 |
|
| 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, |
| 201 |
]; |
| 202 |
|
| 203 |
/** |
| 204 |
* Filter to args used in WP_Comment_Query in Widget Search Comment |
| 205 |
* |
| 206 |
* @hook ep_comment_search_widget_args |
| 207 |
* @since 3.6.0 |
| 208 |
* @param {array} $default_args Defaults args |
| 209 |
* @return {array} New value |
| 210 |
*/ |
| 211 |
$args = apply_filters( 'ep_comment_search_widget_args', $default_args ); |
| 212 |
|
| 213 |
/** |
| 214 |
* Fires before the comment query is executed. |
| 215 |
* |
| 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. |
| 220 |
*/ |
| 221 |
do_action( 'ep_comment_pre_search_widget', $args, $request ); |
| 222 |
|
| 223 |
$comment_query = new \WP_Comment_Query( $args ); |
| 224 |
|
| 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 ); |
| 234 |
|
| 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 |
} |
| 243 |
|
| 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 ); |
| 253 |
} |
| 254 |
} |
| 255 |
|