← All changes
|
includes/classes/Feature/RelatedPosts/RelatedPosts.php
+56
-130
4.2.0
→
5.3.5
View file →
| @@ -7,12 +7,13 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ElasticPress\Feature\RelatedPosts; |
| 10 | 10 | |
| 11 | -use ElasticPress\Feature as Feature; | |
| 12 | -use ElasticPress\Elasticsearch as Elasticsearch; | |
| 13 | -use ElasticPress\Post\Post as Post; | |
| 14 | -use \WP_Query as WP_Query; | |
| 11 | +use WP_Query; | |
| 12 | +use ElasticPress\Elasticsearch; | |
| 13 | +use ElasticPress\Feature; | |
| 14 | +use ElasticPress\REST; | |
| 15 | +use ElasticPress\Utils; | |
| 15 | 16 | |
| 16 | 17 | /** |
| 17 | 18 | * Related posts feature class |
| 18 | 19 | */ |
| @@ -24,17 +25,27 @@ | ||
| 24 | 25 | */ |
| 25 | 26 | public function __construct() { |
| 26 | 27 | $this->slug = 'related_posts'; |
| 27 | 28 | |
| 28 | - $this->title = esc_html__( 'Related Posts', 'elasticpress' ); | |
| 29 | + $this->group = 'core-search'; | |
| 29 | 30 | |
| 30 | - $this->summary = __( 'ElasticPress understands data in real time, so it can instantly deliver engaging and precise related content with no impact on site performance.', 'elasticpress' ); | |
| 31 | + $this->requires_install_reindex = false; | |
| 31 | 32 | |
| 32 | - $this->docs_url = __( 'https://elasticpress.zendesk.com/hc/en-us/articles/360050447492-Configuring-ElasticPress-via-the-Plugin-Dashboard#related-posts', 'elasticpress' ); | |
| 33 | + parent::__construct(); | |
| 34 | + } | |
| 33 | 35 | |
| 34 | - $this->requires_install_reindex = false; | |
| 36 | + /** | |
| 37 | + * Sets i18n strings. | |
| 38 | + * | |
| 39 | + * @return void | |
| 40 | + * @since 5.2.0 | |
| 41 | + */ | |
| 42 | + public function set_i18n_strings(): void { | |
| 43 | + $this->title = esc_html__( 'Related Posts', 'elasticpress' ); | |
| 35 | 44 | |
| 36 | - parent::__construct(); | |
| 45 | + $this->summary = '<p>' . __( 'Instantly deliver engaging and precise related content with no impact on site performance. Output related content using our block or directly in your theme using our <a href="https://www.elasticpress.io/resources/articles/related-posts-api/">API functions</a>.', 'elasticpress' ) . '</p>'; | |
| 46 | + | |
| 47 | + $this->docs_url = __( 'https://www.elasticpress.io/resources/articles/configuring-elasticpress-via-the-plugin-dashboard/#related-posts', 'elasticpress' ); | |
| 37 | 48 | } |
| 38 | 49 | |
| 39 | 50 | /** |
| 40 | 51 | * Format args for related posts |
| @@ -45,9 +56,9 @@ | ||
| 45 | 56 | */ |
| 46 | 57 | public function formatted_args( $formatted_args, $args ) { |
| 47 | 58 | if ( ! empty( $args['more_like'] ) ) { |
| 48 | 59 | // lets compare ES version to see if new MLT structure applies |
| 49 | - $new_mlt = version_compare( Elasticsearch::factory()->get_elasticsearch_version(), 6.0, '>=' ); | |
| 60 | + $new_mlt = version_compare( (string) Elasticsearch::factory()->get_elasticsearch_version(), 6.0, '>=' ); | |
| 50 | 61 | |
| 51 | 62 | if ( $new_mlt && is_array( $args['more_like'] ) ) { |
| 52 | 63 | foreach ( $args['more_like'] as $id ) { |
| 53 | 64 | $ids[] = array( '_id' => $id ); |
| @@ -112,16 +123,16 @@ | ||
| 112 | 123 | /** |
| 113 | 124 | * Search Elasticsearch for related content |
| 114 | 125 | * |
| 115 | 126 | * @param int $post_id Post ID |
| 116 | - * @param int $return Return code | |
| 127 | + * @param int $post_return Number of posts to return | |
| 117 | 128 | * @since 4.1.0 |
| 118 | 129 | * @return WP_Query |
| 119 | 130 | */ |
| 120 | - public function get_related_query( $post_id, $return = 5 ) { | |
| 131 | + public function get_related_query( $post_id, $post_return = 5 ) { | |
| 121 | 132 | $args = array( |
| 122 | 133 | 'more_like' => $post_id, |
| 123 | - 'posts_per_page' => $return, | |
| 134 | + 'posts_per_page' => $post_return, | |
| 124 | 135 | 'ep_integrate' => true, |
| 125 | 136 | 'ignore_sticky_posts' => true, |
| 126 | 137 | ); |
| 127 | 138 | |
| @@ -139,9 +150,9 @@ | ||
| 139 | 150 | /** |
| 140 | 151 | * Search Elasticsearch for related content |
| 141 | 152 | * |
| 142 | 153 | * @param int $post_id Post ID |
| 143 | - * @param int $return Return code | |
| 154 | + * @param int $post_return Number of posts to return | |
| 144 | 155 | * |
| 145 | 156 | * @since 2.1 |
| 146 | 157 | * @uses get_related_query |
| 147 | 158 | * |
| @@ -146,10 +157,10 @@ | ||
| 146 | 157 | * @uses get_related_query |
| 147 | 158 | * |
| 148 | 159 | * @return array|bool |
| 149 | 160 | */ |
| 150 | - public function find_related( $post_id, $return = 5 ) { | |
| 151 | - $query = $this->get_related_query( $post_id, $return ); | |
| 161 | + public function find_related( $post_id, $post_return = 5 ) { | |
| 162 | + $query = $this->get_related_query( $post_id, $post_return ); | |
| 152 | 163 | |
| 153 | 164 | if ( ! $query->have_posts() ) { |
| 154 | 165 | return false; |
| 155 | 166 | } |
| @@ -162,8 +173,9 @@ | ||
| 162 | 173 | * @since 2.1 |
| 163 | 174 | */ |
| 164 | 175 | public function setup() { |
| 165 | 176 | add_action( 'widgets_init', [ $this, 'register_widget' ] ); |
| 177 | + add_filter( 'widget_types_to_hide_from_legacy_widget_block', [ $this, 'hide_legacy_widget' ] ); | |
| 166 | 178 | add_filter( 'ep_formatted_args', [ $this, 'formatted_args' ], 10, 2 ); |
| 167 | 179 | add_action( 'init', [ $this, 'register_block' ] ); |
| 168 | 180 | add_action( 'rest_api_init', [ $this, 'setup_endpoint' ] ); |
| 169 | 181 | } |
| @@ -173,125 +185,36 @@ | ||
| 173 | 185 | * |
| 174 | 186 | * @since 3.2 |
| 175 | 187 | */ |
| 176 | 188 | public function setup_endpoint() { |
| 177 | - register_rest_route( | |
| 178 | - 'wp/v2', | |
| 179 | - '/posts/(?P<id>[0-9]+)/related', | |
| 180 | - [ | |
| 181 | - 'methods' => 'GET', | |
| 182 | - 'callback' => [ $this, 'output_endpoint' ], | |
| 183 | - 'permission_callback' => '__return_true', | |
| 184 | - 'args' => [ | |
| 185 | - 'id' => [ | |
| 186 | - 'description' => 'Post ID.', | |
| 187 | - 'type' => 'numeric', | |
| 188 | - ], | |
| 189 | - 'number' => [ | |
| 190 | - 'description' => 'Number of posts', | |
| 191 | - 'type' => 'numeric', | |
| 192 | - 'default' => 5, | |
| 193 | - ], | |
| 194 | - ], | |
| 195 | - ] | |
| 196 | - ); | |
| 189 | + $controller = new REST\RelatedPosts(); | |
| 190 | + $controller->register_routes(); | |
| 197 | 191 | } |
| 198 | 192 | |
| 199 | 193 | /** |
| 200 | - * Output related posts endpoint | |
| 201 | - * | |
| 202 | - * @param \WP_REST_Request $request REST request | |
| 203 | - * @since 3.2 | |
| 204 | - * @return \WP_REST_Response | |
| 205 | - */ | |
| 206 | - public function output_endpoint( $request ) { | |
| 207 | - $id = $request['id']; | |
| 208 | - | |
| 209 | - $posts = $this->find_related( $id, (int) $request['number'] ); | |
| 210 | - $prepared_posts = []; | |
| 211 | - | |
| 212 | - if ( ! empty( $posts ) ) { | |
| 213 | - foreach ( $posts as $post ) { | |
| 214 | - $prepared_post = []; | |
| 215 | - | |
| 216 | - $prepared_post['id'] = $post->ID; | |
| 217 | - $prepared_post['link'] = get_permalink( $post->ID ); | |
| 218 | - $prepared_post['status'] = $post->post_status; | |
| 219 | - $prepared_post['title'] = [ | |
| 220 | - 'raw' => $post->post_title, | |
| 221 | - 'rendered' => get_the_title( $post->ID ), | |
| 222 | - ]; | |
| 223 | - $prepared_post['author'] = (int) $post->post_author; | |
| 224 | - $prepared_post['parent'] = (int) $post->post_parent; | |
| 225 | - $prepared_post['menu_order'] = (int) $post->menu_order; | |
| 226 | - $prepared_post['content'] = [ | |
| 227 | - 'rendered' => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ), | |
| 228 | - ]; | |
| 229 | - $prepared_post['date'] = $post->post_date; | |
| 230 | - $prepared_post['date_gmt'] = $post->post_date_gmt; | |
| 231 | - $prepared_post['modified'] = $post->post_modified; | |
| 232 | - $prepared_post['modified_gmt'] = $post->post_modified_gmt; | |
| 233 | - | |
| 234 | - $prepared_posts[] = $prepared_post; | |
| 235 | - } | |
| 236 | - } | |
| 237 | - | |
| 238 | - $response = new \WP_REST_Response(); | |
| 239 | - $response->set_data( $prepared_posts ); | |
| 240 | - | |
| 241 | - return $response; | |
| 242 | - } | |
| 243 | - | |
| 244 | - /** | |
| 245 | 194 | * Register gutenberg block |
| 246 | 195 | * |
| 247 | 196 | * @since 3.2 |
| 248 | 197 | */ |
| 249 | 198 | public function register_block() { |
| 250 | - // Must be WP 5.0+ | |
| 251 | - if ( ! function_exists( 'register_block_type' ) ) { | |
| 252 | - return; | |
| 253 | - } | |
| 254 | - | |
| 199 | + /** | |
| 200 | + * Registering it here so translation works | |
| 201 | + * | |
| 202 | + * @see https://core.trac.wordpress.org/ticket/54797#comment:20 | |
| 203 | + */ | |
| 255 | 204 | wp_register_script( |
| 256 | - 'elasticpress-related-posts-block', | |
| 257 | - EP_URL . 'dist/js/related-posts-block-script.min.js', | |
| 258 | - [ | |
| 259 | - 'wp-blocks', | |
| 260 | - 'wp-element', | |
| 261 | - 'wp-editor', | |
| 262 | - 'wp-api-fetch', | |
| 263 | - ], | |
| 264 | - EP_VERSION, | |
| 205 | + 'ep-related-posts-block-script', | |
| 206 | + EP_URL . 'dist/js/related-posts-block-script.js', | |
| 207 | + Utils\get_asset_info( 'related-posts-block-script.js', 'dependencies' ), | |
| 208 | + Utils\get_asset_info( 'related-posts-block-script.js', 'version' ), | |
| 265 | 209 | true |
| 266 | 210 | ); |
| 267 | 211 | |
| 268 | - // The wp-edit-blocks style dependency is not needed on the front end of the site. | |
| 269 | - $style_dependencies = is_admin() ? [ 'wp-edit-blocks' ] : []; | |
| 212 | + wp_set_script_translations( 'ep-related-posts-block-script', 'elasticpress' ); | |
| 270 | 213 | |
| 271 | - wp_register_style( | |
| 272 | - 'elasticpress-related-posts-block', | |
| 273 | - EP_URL . 'dist/css/related-posts-block-styles.min.css', | |
| 274 | - $style_dependencies, | |
| 275 | - EP_VERSION | |
| 276 | - ); | |
| 277 | - | |
| 278 | - register_block_type( | |
| 279 | - 'elasticpress/related-posts', | |
| 214 | + register_block_type_from_metadata( | |
| 215 | + EP_PATH . 'assets/js/blocks/related-posts', | |
| 280 | 216 | [ |
| 281 | - 'attributes' => [ | |
| 282 | - 'number' => [ | |
| 283 | - 'type' => 'number', | |
| 284 | - 'default' => 5, | |
| 285 | - ], | |
| 286 | - 'align' => [ | |
| 287 | - 'type' => 'string', | |
| 288 | - 'enum' => [ 'left', 'center', 'right', 'wide', 'full' ], | |
| 289 | - ], | |
| 290 | - ], | |
| 291 | - 'editor_script' => 'elasticpress-related-posts-block', | |
| 292 | - 'editor_style' => 'elasticpress-related-posts-block', | |
| 293 | - 'style' => 'elasticpress-related-posts-block', | |
| 294 | 217 | 'render_callback' => [ $this, 'render_block' ], |
| 295 | 218 | ] |
| 296 | 219 | ); |
| 297 | 220 | } |
| @@ -315,15 +238,13 @@ | ||
| 315 | 238 | if ( ! empty( $attributes['align'] ) ) { |
| 316 | 239 | $class .= ' align' . $attributes['align']; |
| 317 | 240 | } |
| 318 | 241 | |
| 319 | - if ( ! empty( $attributes['className'] ) ) { | |
| 320 | - $class .= ' ' . $attributes['className']; | |
| 321 | - } | |
| 242 | + ob_start(); | |
| 322 | 243 | |
| 323 | - ob_start(); | |
| 244 | + $wrapper_attributes = get_block_wrapper_attributes( [ 'class' => $class ] ); | |
| 324 | 245 | ?> |
| 325 | - <section class="<?php echo esc_attr( $class ); ?>"> | |
| 246 | + <section <?php echo wp_kses_data( $wrapper_attributes ); ?>"> | |
| 326 | 247 | <ul> |
| 327 | 248 | <?php foreach ( $posts as $related_post ) : ?> |
| 328 | 249 | <li> |
| 329 | 250 | <a href="<?php echo esc_url( get_permalink( $related_post->ID ) ); ?>"> |
| @@ -349,14 +270,19 @@ | ||
| 349 | 270 | register_widget( __NAMESPACE__ . '\Widget' ); |
| 350 | 271 | } |
| 351 | 272 | |
| 352 | 273 | /** |
| 353 | - * Output feature box long | |
| 274 | + * Hide the legacy widget. | |
| 354 | 275 | * |
| 355 | - * @since 2.1 | |
| 276 | + * Hides the legacy widget in favor of the Block when the block editor | |
| 277 | + * is in use and the legacy widget has not been used. | |
| 278 | + * | |
| 279 | + * @since 4.3 | |
| 280 | + * @param array $widgets An array of excluded widget-type IDs. | |
| 281 | + * @return array array of excluded widget-type IDs to hide. | |
| 356 | 282 | */ |
| 357 | - public function output_feature_box_long() { | |
| 358 | - ?> | |
| 359 | - <p><?php echo wp_kses_post( __( 'Output related content using our Widget or directly in your theme using our <a href="https://10up.github.io/ElasticPress/tutorial-features.html#related-posts">API functions.</a>', 'elasticpress' ) ); ?></p> | |
| 360 | - <?php | |
| 283 | + public function hide_legacy_widget( $widgets ) { | |
| 284 | + $widgets[] = 'ep-related-posts'; | |
| 285 | + | |
| 286 | + return $widgets; | |
| 361 | 287 | } |
| 362 | 288 | } |