← All changes
|
includes/classes/Feature/RelatedPosts/RelatedPosts.php
+47
-97
4.3.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 | } |
| @@ -174,81 +185,33 @@ | ||
| 174 | 185 | * |
| 175 | 186 | * @since 3.2 |
| 176 | 187 | */ |
| 177 | 188 | public function setup_endpoint() { |
| 178 | - register_rest_route( | |
| 179 | - 'wp/v2', | |
| 180 | - '/posts/(?P<id>[0-9]+)/related', | |
| 181 | - [ | |
| 182 | - 'methods' => 'GET', | |
| 183 | - 'callback' => [ $this, 'output_endpoint' ], | |
| 184 | - 'permission_callback' => '__return_true', | |
| 185 | - 'args' => [ | |
| 186 | - 'id' => [ | |
| 187 | - 'description' => 'Post ID.', | |
| 188 | - 'type' => 'numeric', | |
| 189 | - ], | |
| 190 | - 'number' => [ | |
| 191 | - 'description' => 'Number of posts', | |
| 192 | - 'type' => 'numeric', | |
| 193 | - 'default' => 5, | |
| 194 | - ], | |
| 195 | - ], | |
| 196 | - ] | |
| 197 | - ); | |
| 189 | + $controller = new REST\RelatedPosts(); | |
| 190 | + $controller->register_routes(); | |
| 198 | 191 | } |
| 199 | 192 | |
| 200 | 193 | /** |
| 201 | - * Output related posts endpoint | |
| 194 | + * Register gutenberg block | |
| 202 | 195 | * |
| 203 | - * @param \WP_REST_Request $request REST request | |
| 204 | 196 | * @since 3.2 |
| 205 | - * @return \WP_REST_Response | |
| 206 | 197 | */ |
| 207 | - public function output_endpoint( $request ) { | |
| 208 | - $id = $request['id']; | |
| 198 | + public function register_block() { | |
| 199 | + /** | |
| 200 | + * Registering it here so translation works | |
| 201 | + * | |
| 202 | + * @see https://core.trac.wordpress.org/ticket/54797#comment:20 | |
| 203 | + */ | |
| 204 | + wp_register_script( | |
| 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' ), | |
| 209 | + true | |
| 210 | + ); | |
| 209 | 211 | |
| 210 | - $posts = $this->find_related( $id, (int) $request['number'] ); | |
| 211 | - $prepared_posts = []; | |
| 212 | + wp_set_script_translations( 'ep-related-posts-block-script', 'elasticpress' ); | |
| 212 | 213 | |
| 213 | - if ( ! empty( $posts ) ) { | |
| 214 | - foreach ( $posts as $post ) { | |
| 215 | - $prepared_post = []; | |
| 216 | - | |
| 217 | - $prepared_post['id'] = $post->ID; | |
| 218 | - $prepared_post['link'] = get_permalink( $post->ID ); | |
| 219 | - $prepared_post['status'] = $post->post_status; | |
| 220 | - $prepared_post['title'] = [ | |
| 221 | - 'raw' => $post->post_title, | |
| 222 | - 'rendered' => get_the_title( $post->ID ), | |
| 223 | - ]; | |
| 224 | - $prepared_post['author'] = (int) $post->post_author; | |
| 225 | - $prepared_post['parent'] = (int) $post->post_parent; | |
| 226 | - $prepared_post['menu_order'] = (int) $post->menu_order; | |
| 227 | - $prepared_post['content'] = [ | |
| 228 | - 'rendered' => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ), | |
| 229 | - ]; | |
| 230 | - $prepared_post['date'] = $post->post_date; | |
| 231 | - $prepared_post['date_gmt'] = $post->post_date_gmt; | |
| 232 | - $prepared_post['modified'] = $post->post_modified; | |
| 233 | - $prepared_post['modified_gmt'] = $post->post_modified_gmt; | |
| 234 | - | |
| 235 | - $prepared_posts[] = $prepared_post; | |
| 236 | - } | |
| 237 | - } | |
| 238 | - | |
| 239 | - $response = new \WP_REST_Response(); | |
| 240 | - $response->set_data( $prepared_posts ); | |
| 241 | - | |
| 242 | - return $response; | |
| 243 | - } | |
| 244 | - | |
| 245 | - /** | |
| 246 | - * Register gutenberg block | |
| 247 | - * | |
| 248 | - * @since 3.2 | |
| 249 | - */ | |
| 250 | - public function register_block() { | |
| 251 | 214 | register_block_type_from_metadata( |
| 252 | 215 | EP_PATH . 'assets/js/blocks/related-posts', |
| 253 | 216 | [ |
| 254 | 217 | 'render_callback' => [ $this, 'render_block' ], |
| @@ -275,15 +238,13 @@ | ||
| 275 | 238 | if ( ! empty( $attributes['align'] ) ) { |
| 276 | 239 | $class .= ' align' . $attributes['align']; |
| 277 | 240 | } |
| 278 | 241 | |
| 279 | - if ( ! empty( $attributes['className'] ) ) { | |
| 280 | - $class .= ' ' . $attributes['className']; | |
| 281 | - } | |
| 242 | + ob_start(); | |
| 282 | 243 | |
| 283 | - ob_start(); | |
| 244 | + $wrapper_attributes = get_block_wrapper_attributes( [ 'class' => $class ] ); | |
| 284 | 245 | ?> |
| 285 | - <section class="<?php echo esc_attr( $class ); ?>"> | |
| 246 | + <section <?php echo wp_kses_data( $wrapper_attributes ); ?>"> | |
| 286 | 247 | <ul> |
| 287 | 248 | <?php foreach ( $posts as $related_post ) : ?> |
| 288 | 249 | <li> |
| 289 | 250 | <a href="<?php echo esc_url( get_permalink( $related_post->ID ) ); ?>"> |
| @@ -322,17 +283,6 @@ | ||
| 322 | 283 | public function hide_legacy_widget( $widgets ) { |
| 323 | 284 | $widgets[] = 'ep-related-posts'; |
| 324 | 285 | |
| 325 | 286 | return $widgets; |
| 326 | - } | |
| 327 | - | |
| 328 | - /** | |
| 329 | - * Output feature box long | |
| 330 | - * | |
| 331 | - * @since 2.1 | |
| 332 | - */ | |
| 333 | - public function output_feature_box_long() { | |
| 334 | - ?> | |
| 335 | - <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> | |
| 336 | - <?php | |
| 337 | 287 | } |
| 338 | 288 | } |