← All changes
|
includes/classes/Feature/RelatedPosts/RelatedPosts.php
+31
-97
4.6.0
→
5.3.5
View file →
| @@ -7,13 +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; | |
| 11 | +use WP_Query; | |
| 12 | +use ElasticPress\Elasticsearch; | |
| 13 | +use ElasticPress\Feature; | |
| 14 | +use ElasticPress\REST; | |
| 13 | 15 | use ElasticPress\Utils; |
| 14 | -use ElasticPress\Post\Post as Post; | |
| 15 | -use \WP_Query as WP_Query; | |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Related posts feature class |
| 19 | 19 | */ |
| @@ -25,17 +25,27 @@ | ||
| 25 | 25 | */ |
| 26 | 26 | public function __construct() { |
| 27 | 27 | $this->slug = 'related_posts'; |
| 28 | 28 | |
| 29 | - $this->title = esc_html__( 'Related Posts', 'elasticpress' ); | |
| 29 | + $this->group = 'core-search'; | |
| 30 | 30 | |
| 31 | - $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; | |
| 32 | 32 | |
| 33 | - $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 | + } | |
| 34 | 35 | |
| 35 | - $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' ); | |
| 36 | 44 | |
| 37 | - 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' ); | |
| 38 | 48 | } |
| 39 | 49 | |
| 40 | 50 | /** |
| 41 | 51 | * Format args for related posts |
| @@ -46,9 +56,9 @@ | ||
| 46 | 56 | */ |
| 47 | 57 | public function formatted_args( $formatted_args, $args ) { |
| 48 | 58 | if ( ! empty( $args['more_like'] ) ) { |
| 49 | 59 | // lets compare ES version to see if new MLT structure applies |
| 50 | - $new_mlt = version_compare( Elasticsearch::factory()->get_elasticsearch_version(), 6.0, '>=' ); | |
| 60 | + $new_mlt = version_compare( (string) Elasticsearch::factory()->get_elasticsearch_version(), 6.0, '>=' ); | |
| 51 | 61 | |
| 52 | 62 | if ( $new_mlt && is_array( $args['more_like'] ) ) { |
| 53 | 63 | foreach ( $args['more_like'] as $id ) { |
| 54 | 64 | $ids[] = array( '_id' => $id ); |
| @@ -113,16 +123,16 @@ | ||
| 113 | 123 | /** |
| 114 | 124 | * Search Elasticsearch for related content |
| 115 | 125 | * |
| 116 | 126 | * @param int $post_id Post ID |
| 117 | - * @param int $return Return code | |
| 127 | + * @param int $post_return Number of posts to return | |
| 118 | 128 | * @since 4.1.0 |
| 119 | 129 | * @return WP_Query |
| 120 | 130 | */ |
| 121 | - public function get_related_query( $post_id, $return = 5 ) { | |
| 131 | + public function get_related_query( $post_id, $post_return = 5 ) { | |
| 122 | 132 | $args = array( |
| 123 | 133 | 'more_like' => $post_id, |
| 124 | - 'posts_per_page' => $return, | |
| 134 | + 'posts_per_page' => $post_return, | |
| 125 | 135 | 'ep_integrate' => true, |
| 126 | 136 | 'ignore_sticky_posts' => true, |
| 127 | 137 | ); |
| 128 | 138 | |
| @@ -140,9 +150,9 @@ | ||
| 140 | 150 | /** |
| 141 | 151 | * Search Elasticsearch for related content |
| 142 | 152 | * |
| 143 | 153 | * @param int $post_id Post ID |
| 144 | - * @param int $return Return code | |
| 154 | + * @param int $post_return Number of posts to return | |
| 145 | 155 | * |
| 146 | 156 | * @since 2.1 |
| 147 | 157 | * @uses get_related_query |
| 148 | 158 | * |
| @@ -147,10 +157,10 @@ | ||
| 147 | 157 | * @uses get_related_query |
| 148 | 158 | * |
| 149 | 159 | * @return array|bool |
| 150 | 160 | */ |
| 151 | - public function find_related( $post_id, $return = 5 ) { | |
| 152 | - $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 ); | |
| 153 | 163 | |
| 154 | 164 | if ( ! $query->have_posts() ) { |
| 155 | 165 | return false; |
| 156 | 166 | } |
| @@ -175,76 +185,13 @@ | ||
| 175 | 185 | * |
| 176 | 186 | * @since 3.2 |
| 177 | 187 | */ |
| 178 | 188 | public function setup_endpoint() { |
| 179 | - register_rest_route( | |
| 180 | - 'wp/v2', | |
| 181 | - '/posts/(?P<id>[0-9]+)/related', | |
| 182 | - [ | |
| 183 | - 'methods' => 'GET', | |
| 184 | - 'callback' => [ $this, 'output_endpoint' ], | |
| 185 | - 'permission_callback' => '__return_true', | |
| 186 | - 'args' => [ | |
| 187 | - 'id' => [ | |
| 188 | - 'description' => 'Post ID.', | |
| 189 | - 'type' => 'numeric', | |
| 190 | - ], | |
| 191 | - 'number' => [ | |
| 192 | - 'description' => 'Number of posts', | |
| 193 | - 'type' => 'numeric', | |
| 194 | - 'default' => 5, | |
| 195 | - ], | |
| 196 | - ], | |
| 197 | - ] | |
| 198 | - ); | |
| 189 | + $controller = new REST\RelatedPosts(); | |
| 190 | + $controller->register_routes(); | |
| 199 | 191 | } |
| 200 | 192 | |
| 201 | 193 | /** |
| 202 | - * Output related posts endpoint | |
| 203 | - * | |
| 204 | - * @param \WP_REST_Request $request REST request | |
| 205 | - * @since 3.2 | |
| 206 | - * @return \WP_REST_Response | |
| 207 | - */ | |
| 208 | - public function output_endpoint( $request ) { | |
| 209 | - $id = $request['id']; | |
| 210 | - | |
| 211 | - $posts = $this->find_related( $id, (int) $request['number'] ); | |
| 212 | - $prepared_posts = []; | |
| 213 | - | |
| 214 | - if ( ! empty( $posts ) ) { | |
| 215 | - foreach ( $posts as $post ) { | |
| 216 | - $prepared_post = []; | |
| 217 | - | |
| 218 | - $prepared_post['id'] = $post->ID; | |
| 219 | - $prepared_post['link'] = get_permalink( $post->ID ); | |
| 220 | - $prepared_post['status'] = $post->post_status; | |
| 221 | - $prepared_post['title'] = [ | |
| 222 | - 'raw' => $post->post_title, | |
| 223 | - 'rendered' => get_the_title( $post->ID ), | |
| 224 | - ]; | |
| 225 | - $prepared_post['author'] = (int) $post->post_author; | |
| 226 | - $prepared_post['parent'] = (int) $post->post_parent; | |
| 227 | - $prepared_post['menu_order'] = (int) $post->menu_order; | |
| 228 | - $prepared_post['content'] = [ | |
| 229 | - 'rendered' => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ), | |
| 230 | - ]; | |
| 231 | - $prepared_post['date'] = $post->post_date; | |
| 232 | - $prepared_post['date_gmt'] = $post->post_date_gmt; | |
| 233 | - $prepared_post['modified'] = $post->post_modified; | |
| 234 | - $prepared_post['modified_gmt'] = $post->post_modified_gmt; | |
| 235 | - | |
| 236 | - $prepared_posts[] = $prepared_post; | |
| 237 | - } | |
| 238 | - } | |
| 239 | - | |
| 240 | - $response = new \WP_REST_Response(); | |
| 241 | - $response->set_data( $prepared_posts ); | |
| 242 | - | |
| 243 | - return $response; | |
| 244 | - } | |
| 245 | - | |
| 246 | - /** | |
| 247 | 194 | * Register gutenberg block |
| 248 | 195 | * |
| 249 | 196 | * @since 3.2 |
| 250 | 197 | */ |
| @@ -291,15 +238,13 @@ | ||
| 291 | 238 | if ( ! empty( $attributes['align'] ) ) { |
| 292 | 239 | $class .= ' align' . $attributes['align']; |
| 293 | 240 | } |
| 294 | 241 | |
| 295 | - if ( ! empty( $attributes['className'] ) ) { | |
| 296 | - $class .= ' ' . $attributes['className']; | |
| 297 | - } | |
| 242 | + ob_start(); | |
| 298 | 243 | |
| 299 | - ob_start(); | |
| 244 | + $wrapper_attributes = get_block_wrapper_attributes( [ 'class' => $class ] ); | |
| 300 | 245 | ?> |
| 301 | - <section class="<?php echo esc_attr( $class ); ?>"> | |
| 246 | + <section <?php echo wp_kses_data( $wrapper_attributes ); ?>"> | |
| 302 | 247 | <ul> |
| 303 | 248 | <?php foreach ( $posts as $related_post ) : ?> |
| 304 | 249 | <li> |
| 305 | 250 | <a href="<?php echo esc_url( get_permalink( $related_post->ID ) ); ?>"> |
| @@ -338,17 +283,6 @@ | ||
| 338 | 283 | public function hide_legacy_widget( $widgets ) { |
| 339 | 284 | $widgets[] = 'ep-related-posts'; |
| 340 | 285 | |
| 341 | 286 | return $widgets; |
| 342 | - } | |
| 343 | - | |
| 344 | - /** | |
| 345 | - * Output feature box long | |
| 346 | - * | |
| 347 | - * @since 2.1 | |
| 348 | - */ | |
| 349 | - public function output_feature_box_long() { | |
| 350 | - ?> | |
| 351 | - <p><?php echo wp_kses_post( __( 'Output related content using our Widget or directly in your theme using our <a href="https://elasticpress.zendesk.com/hc/en-us/articles/16671825423501-Features#related-posts">API functions.</a>', 'elasticpress' ) ); ?></p> | |
| 352 | - <?php | |
| 353 | 287 | } |
| 354 | 288 | } |