| 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; |
| 12 |
use ElasticPress\FeatureRequirementsStatus; |
| 13 |
use ElasticPress\Features; |
| 14 |
use ElasticPress\Indexable; |
| 15 |
use ElasticPress\Indexables; |
| 16 |
use ElasticPress\Utils; |
| 17 |
use ElasticPress\REST; |
| 18 |
|
| 19 |
/** |
| 20 |
* Comments feature class |
| 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; |
| 30 |
|
| 31 |
/** |
| 32 |
* Initialize feature, setting it's config |
| 33 |
* |
| 34 |
* @since 3.6.0 |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
$this->slug = 'comments'; |
| 38 |
|
| 39 |
$this->requires_install_reindex = true; |
| 40 |
|
| 41 |
Indexables::factory()->register( new Indexable\Comment\Comment(), false ); |
| 42 |
|
| 43 |
parent::__construct(); |
| 44 |
} |
| 45 |
|
| 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' ); |
| 54 |
|
| 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' ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Setup search functionality |
| 62 |
* |
| 63 |
* @since 3.6.0 |
| 64 |
*/ |
| 65 |
public function setup() { |
| 66 |
Indexables::factory()->activate( 'comment' ); |
| 67 |
|
| 68 |
add_action( 'init', [ $this, 'register_block' ] ); |
| 69 |
add_action( 'init', [ $this, 'search_setup' ] ); |
| 70 |
add_action( 'widgets_init', [ $this, 'register_widget' ] ); |
| 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' ] ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Setup search integration |
| 78 |
* |
| 79 |
* @since 3.6.0 |
| 80 |
*/ |
| 81 |
public function search_setup() { |
| 82 |
$admin_integration = apply_filters( 'ep_admin_wp_query_integration', false ); |
| 83 |
|
| 84 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 85 |
/** |
| 86 |
* Filter to integrate with admin ajax queries |
| 87 |
* |
| 88 |
* @hook ep_ajax_wp_query_integration |
| 89 |
* @param {bool} $integrate True to integrate |
| 90 |
* @return {bool} New value |
| 91 |
*/ |
| 92 |
if ( ! apply_filters( 'ep_ajax_wp_query_integration', false ) ) { |
| 93 |
return; |
| 94 |
} else { |
| 95 |
$admin_integration = true; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if ( is_admin() && ! $admin_integration ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
add_filter( 'ep_elasticpress_enabled', [ $this, 'integrate_search_queries' ], 10, 2 ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Enable integration on search queries |
| 108 |
* |
| 109 |
* @param bool $enabled Whether EP is enabled |
| 110 |
* @param \WP_Comment_Query $query Current query object. |
| 111 |
* @since 3.6.0 |
| 112 |
* @return bool |
| 113 |
*/ |
| 114 |
public function integrate_search_queries( $enabled, $query ) { |
| 115 |
if ( ! is_a( $query, 'WP_Comment_Query' ) ) { |
| 116 |
return $enabled; |
| 117 |
} |
| 118 |
|
| 119 |
if ( isset( $query->query_vars['ep_integrate'] ) && ! filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOLEAN ) ) { |
| 120 |
$enabled = false; |
| 121 |
} elseif ( ! empty( $query->query_vars['search'] ) ) { |
| 122 |
$enabled = true; |
| 123 |
} |
| 124 |
|
| 125 |
return $enabled; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Determine feature reqs status |
| 130 |
* |
| 131 |
* @since 3.6.0 |
| 132 |
* @return FeatureRequirementsStatus |
| 133 |
*/ |
| 134 |
public function requirements_status() { |
| 135 |
$status = new FeatureRequirementsStatus( 1, null, $this ); |
| 136 |
|
| 137 |
return $status; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Register comments widget |
| 142 |
* |
| 143 |
* @since 3.6.0 |
| 144 |
*/ |
| 145 |
public function register_widget() { |
| 146 |
register_widget( __NAMESPACE__ . '\Widget' ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Registers the API endpoint to search for comments |
| 151 |
* |
| 152 |
* @since 3.6.0 |
| 153 |
*/ |
| 154 |
public function rest_api_init() { |
| 155 |
$controller = new REST\Comments(); |
| 156 |
$controller->register_routes(); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Get a list of searchable post types that support comments. |
| 161 |
* |
| 162 |
* @return array Array of post type labels keyed by post type. |
| 163 |
* @since 4.4.0 |
| 164 |
*/ |
| 165 |
public static function get_searchable_post_types() { |
| 166 |
$searchable_post_types = array(); |
| 167 |
|
| 168 |
$post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types(); |
| 169 |
$post_types = array_filter( |
| 170 |
$post_types, |
| 171 |
function ( $post_type ) { |
| 172 |
return post_type_supports( $post_type, 'comments' ); |
| 173 |
} |
| 174 |
); |
| 175 |
|
| 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; |
| 181 |
} |
| 182 |
|
| 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' ), |
| 213 |
]; |
| 214 |
|
| 215 |
/** |
| 216 |
* Filter the l10n data attached to the Widget Search Comments script |
| 217 |
* |
| 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 |
| 222 |
*/ |
| 223 |
$script_data = apply_filters( 'ep_comment_search_widget_l10n_data_script', $default_script_data ); |
| 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() { |
| 238 |
/** |
| 239 |
* Registering it here so translation works |
| 240 |
* |
| 241 |
* @see https://core.trac.wordpress.org/ticket/54797#comment:20 |
| 242 |
*/ |
| 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 |
); |
| 250 |
|
| 251 |
wp_set_script_translations( 'elasticpress-comments-editor-script', 'elasticpress' ); |
| 252 |
|
| 253 |
wp_localize_script( |
| 254 |
'elasticpress-comments-editor-script', |
| 255 |
'epComments', |
| 256 |
[ |
| 257 |
'searchablePostTypes' => self::get_searchable_post_types(), |
| 258 |
] |
| 259 |
); |
| 260 |
|
| 261 |
register_block_type_from_metadata( |
| 262 |
EP_PATH . 'assets/js/blocks/comments', |
| 263 |
[ |
| 264 |
'render_callback' => [ $this, 'render_block' ], |
| 265 |
] |
| 266 |
); |
| 267 |
} |
| 268 |
|
| 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; |
| 325 |
} |
| 326 |
} |
| 327 |
|