← All changes
|
includes/classes/Feature/RelatedPosts/RelatedPosts.php
+25
-88
4.7.2
→
5.3.5
View file →
| @@ -7,11 +7,12 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ElasticPress\Feature\RelatedPosts; |
| 10 | 10 | |
| 11 | -use \WP_Query; | |
| 11 | +use WP_Query; | |
| 12 | 12 | use ElasticPress\Elasticsearch; |
| 13 | 13 | use ElasticPress\Feature; |
| 14 | +use ElasticPress\REST; | |
| 14 | 15 | use ElasticPress\Utils; |
| 15 | 16 | |
| 16 | 17 | /** |
| 17 | 18 | * Related posts feature class |
| @@ -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 |
| @@ -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,76 +185,13 @@ | ||
| 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 | |
| 202 | - * | |
| 203 | - * @param \WP_REST_Request $request REST request | |
| 204 | - * @since 3.2 | |
| 205 | - * @return \WP_REST_Response | |
| 206 | - */ | |
| 207 | - public function output_endpoint( $request ) { | |
| 208 | - $id = $request['id']; | |
| 209 | - | |
| 210 | - $posts = $this->find_related( $id, (int) $request['number'] ); | |
| 211 | - $prepared_posts = []; | |
| 212 | - | |
| 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 | 194 | * Register gutenberg block |
| 247 | 195 | * |
| 248 | 196 | * @since 3.2 |
| 249 | 197 | */ |
| @@ -335,17 +283,6 @@ | ||
| 335 | 283 | public function hide_legacy_widget( $widgets ) { |
| 336 | 284 | $widgets[] = 'ep-related-posts'; |
| 337 | 285 | |
| 338 | 286 | return $widgets; |
| 339 | - } | |
| 340 | - | |
| 341 | - /** | |
| 342 | - * Output feature box long | |
| 343 | - * | |
| 344 | - * @since 2.1 | |
| 345 | - */ | |
| 346 | - public function output_feature_box_long() { | |
| 347 | - ?> | |
| 348 | - <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> | |
| 349 | - <?php | |
| 350 | 287 | } |
| 351 | 288 | } |