Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
14.56% covered (danger)
14.56%
123 / 845
11.76% covered (danger)
11.76%
6 / 51
CRAP
0.00% covered (danger)
0.00%
0 / 2
Jetpack_RelatedPosts
14.84% covered (danger)
14.84%
123 / 829
13.04% covered (danger)
13.04%
6 / 46
41692.35
0.00% covered (danger)
0.00%
0 / 1
 init
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
 init_raw
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 __construct
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 get_blog_id
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 action_admin_init
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 action_frontend_init
60.00% covered (warning)
60.00%
6 / 10
0.00% covered (danger)
0.00%
0 / 1
8.30
 get_headline
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
2.01
 filter_add_target_to_dom
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
11.19
 get_server_rendered_html
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
 test_for_shortcode
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_client_rendered_html
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
3.01
 get_client_rendered_html_unsupported
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 render_block_item
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
132
 render_post_list
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 render_block
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 1
552
 parse_numeric_get_arg
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 get_options
95.45% covered (success)
95.45%
21 / 22
0.00% covered (danger)
0.00%
0 / 1
12
 get_option
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 parse_options
90.91% covered (success)
90.91%
20 / 22
0.00% covered (danger)
0.00%
0 / 1
17.22
 print_setting_html
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
6
 print_setting_head
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
20
 get_for_post_id
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
30
 get_es_filters_from_args
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
600
 get_coalesced_range
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 action_frontend_init_ajax
0.00% covered (danger)
0.00%
0 / 128
0.00% covered (danger)
0.00%
0 / 1
72
 get_related_post_data_for_post
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
2
 get_title
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 get_excerpt
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 generate_related_post_image_params
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 1
156
 to_utf8
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_related_posts
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 get_related_post_ids
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 1
462
 filter_non_public_posts
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 generate_related_post_display_author
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 generate_related_post_context_block
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
132
 generate_related_post_context
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
132
 log_click
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 enabled_for_request
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
7.07
 action_frontend_init_page
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 requires_scripts
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 enqueue_assets
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
5
 render_amp_reader_mode_css
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setup_shortcode
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 allow_feature_toggle
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 rest_register_related_posts
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 rest_get_related_posts
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
Jetpack_RelatedPosts_Raw
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 5
30
0.00% covered (danger)
0.00%
0 / 1
 set_query_name
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 action_admin_init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 action_frontend_init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_options
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 get_related_posts
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
1<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * The Jetpack_RelatedPosts class.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Assets;
9use Automattic\Jetpack\Blocks;
10use Automattic\Jetpack\Status\Request;
11use Automattic\Jetpack\Sync\Settings;
12
13/**
14 * The Jetpack_RelatedPosts class.
15 */
16class Jetpack_RelatedPosts {
17    const VERSION   = '20240116';
18    const SHORTCODE = 'jetpack-related-posts';
19
20    /**
21     * Instance of the class.
22     *
23     * @var Jetpack_RelatedPosts
24     */
25    private static $instance = null;
26
27    /**
28     * Instance of the raw class (?).
29     *
30     * @var Jetpack_RelatedPosts
31     */
32    private static $instance_raw = null;
33
34    /**
35     * Creates and returns a static instance of Jetpack_RelatedPosts.
36     *
37     * @return Jetpack_RelatedPosts
38     */
39    public static function init() {
40        if ( ! self::$instance ) {
41            if ( class_exists( 'WPCOM_RelatedPosts' ) && method_exists( 'WPCOM_RelatedPosts', 'init' ) ) {
42                self::$instance = WPCOM_RelatedPosts::init();
43            } else {
44                self::$instance = new Jetpack_RelatedPosts();
45            }
46        }
47
48        return self::$instance;
49    }
50
51    /**
52     * Creates and returns a static instance of Jetpack_RelatedPosts_Raw.
53     *
54     * @return Jetpack_RelatedPosts
55     */
56    public static function init_raw() {
57        if ( ! self::$instance_raw ) {
58            if ( class_exists( 'WPCOM_RelatedPosts' ) && method_exists( 'WPCOM_RelatedPosts', 'init_raw' ) ) {
59                self::$instance_raw = WPCOM_RelatedPosts::init_raw();
60            } else {
61                self::$instance_raw = new Jetpack_RelatedPosts_Raw();
62            }
63        }
64
65        return self::$instance_raw;
66    }
67
68    /**
69     * Options.
70     *
71     * @var array $options
72     */
73    protected $options;
74
75    /**
76     * Allow feature toggle variable.
77     *
78     * @var bool
79     */
80    protected $allow_feature_toggle;
81
82    /**
83     * Blog character set.
84     *
85     * @var mixed
86     */
87    protected $blog_charset;
88
89    /**
90     * Convert character set.
91     *
92     * @var bool
93     */
94    protected $convert_charset;
95
96    /**
97     * Previous Post ID
98     *
99     * @var int
100     */
101    protected $previous_post_id;
102
103    /**
104     * Shortcode usage.
105     *
106     * @var bool
107     */
108    protected $found_shortcode = false;
109
110    /**
111     * Constructor for Jetpack_RelatedPosts.
112     *
113     * @uses get_option, add_action, apply_filters
114     */
115    public function __construct() {
116        $this->blog_charset    = get_option( 'blog_charset' );
117        $this->convert_charset = ( function_exists( 'iconv' ) && ! preg_match( '/^utf\-?8$/i', $this->blog_charset ) );
118        add_action( 'admin_init', array( $this, 'action_admin_init' ) );
119        add_action( 'wp', array( $this, 'action_frontend_init' ) );
120
121        if ( ! class_exists( 'Jetpack_Media_Summary' ) ) {
122            require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.media-summary.php';
123        }
124
125        // Add Related Posts to the REST API Post response.
126        add_action( 'rest_api_init', array( $this, 'rest_register_related_posts' ) );
127    }
128
129    /**
130     * Get the blog ID.
131     *
132     * @return mixed current blog id.
133     */
134    protected function get_blog_id() {
135        return Jetpack_Options::get_option( 'id' );
136    }
137
138    /**
139     * =================
140     * ACTIONS & FILTERS
141     * =================
142     */
143
144    /**
145     * Add a checkbox field to Settings > Reading for enabling related posts.
146     *
147     * @action admin_init
148     * @uses add_settings_field, __, register_setting, add_action
149     */
150    public function action_admin_init() {
151
152        // Add the setting field [jetpack_relatedposts] and place it in Settings > Reading.
153        add_settings_field( 'jetpack_relatedposts', '<span id="jetpack_relatedposts">' . __( 'Related posts', 'jetpack' ) . '</span>', array( $this, 'print_setting_html' ), 'reading' );
154        register_setting( 'reading', 'jetpack_relatedposts', array( $this, 'parse_options' ) );
155        add_action( 'admin_head', array( $this, 'print_setting_head' ) );
156
157        if ( 'options-reading.php' === $GLOBALS['pagenow'] ) {
158            // Enqueue style for live preview on the reading settings page.
159            $this->enqueue_assets( false, true );
160        }
161    }
162
163    /**
164     * Load related posts assets if it's an eligible front end page or execute search and return JSON if it's an endpoint request.
165     *
166     * @global $_GET
167     * @action wp
168     * @uses add_shortcode, get_the_ID
169     */
170    public function action_frontend_init() {
171        // Add a shortcode handler that outputs nothing, this gets overridden later if we can display related content.
172        add_shortcode( self::SHORTCODE, array( $this, 'get_client_rendered_html_unsupported' ) );
173
174        if ( ! $this->enabled_for_request() ) {
175            return;
176        }
177
178        if ( isset( $_GET['relatedposts'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading and checking if we need to generate a list of excuded posts, does not update anything on the site.
179            $excludes = $this->parse_numeric_get_arg( 'relatedposts_exclude' );
180            $this->action_frontend_init_ajax( $excludes );
181        } else {
182            if ( isset( $_GET['relatedposts_hit'] ) && isset( $_GET['relatedposts_origin'] ) && isset( $_GET['relatedposts_position'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- checking if fields are set to setup tracking, nothing is changing on the site.
183                $this->previous_post_id = (int) $_GET['relatedposts_origin']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- fetching a previous post ID for tracking, nothing is changing on the site.
184                $this->log_click( $this->previous_post_id, get_the_ID(), sanitize_text_field( wp_unslash( $_GET['relatedposts_position'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- logging the click for tracking, nothing is changing on the site.
185            }
186
187            $this->action_frontend_init_page();
188        }
189    }
190
191    /**
192     * Render insertion point.
193     *
194     * @since 4.2.0
195     *
196     * @return string
197     */
198    public function get_headline() {
199        $options = $this->get_options();
200
201        if ( ! empty( $options['show_headline'] ) ) {
202            $headline = sprintf(
203                /** This filter is already documented in modules/sharedaddy/sharing-service.php */
204                apply_filters( 'jetpack_sharing_headline_html', '<h3 class="jp-relatedposts-headline"><em>%s</em></h3>', esc_html( $options['headline'] ), 'related-posts' ),
205                esc_html( $options['headline'] )
206            );
207        } else {
208            $headline = '';
209        }
210        return $headline;
211    }
212
213    /**
214     * Adds a target to the post content to load related posts into if a shortcode for it did not already exist.
215     * Will skip adding the target if the post content contains a Related Posts block, if the 'get_the_excerpt'
216     * hook is in the current filter list, or if the site is running an FSE/Site Editor theme.
217     *
218     * @filter the_content
219     *
220     * @param string $content Post content.
221     *
222     * @return string
223     */
224    public function filter_add_target_to_dom( $content ) {
225        // Do not output related posts for ActivityPub requests.
226        if (
227            function_exists( '\Activitypub\is_activitypub_request' )
228            && \Activitypub\is_activitypub_request()
229        ) {
230            return $content;
231        }
232
233        if ( has_block( 'jetpack/related-posts' ) || Blocks::is_fse_theme() ) {
234            return $content;
235        }
236
237        if ( ! $this->found_shortcode && ! doing_filter( 'get_the_excerpt' ) ) {
238            if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
239                $content .= "\n" . $this->get_server_rendered_html();
240            } else {
241                $content .= "\n" . $this->get_client_rendered_html();
242            }
243        }
244
245        return $content;
246    }
247
248    /**
249     * Render static markup based on the Gutenberg block code
250     *
251     * @return string Rendered related posts HTML.
252     */
253    public function get_server_rendered_html() {
254        $rp_settings       = $this->get_options();
255        $block_rp_settings = array(
256            'displayThumbnails' => $rp_settings['show_thumbnails'],
257            'showHeadline'      => $rp_settings['show_headline'],
258            'displayDate'       => isset( $rp_settings['show_date'] ) ? (bool) $rp_settings['show_date'] : true,
259            'displayContext'    => isset( $rp_settings['show_context'] ) && $rp_settings['show_context'],
260            'postLayout'        => isset( $rp_settings['layout'] ) ? $rp_settings['layout'] : 'grid',
261            'postsToShow'       => isset( $rp_settings['size'] ) ? $rp_settings['size'] : 3,
262            /** This filter is already documented in modules/related-posts/jetpack-related-posts.php */
263            'headline'          => apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() ),
264            'isServerRendered'  => true,
265        );
266
267        return $this->render_block( $block_rp_settings, '' );
268    }
269
270    /**
271     * Looks for our shortcode on the unfiltered content, this has to execute early.
272     *
273     * @filter the_content
274     * @param string $content - content of the post.
275     * @uses has_shortcode
276     * @return string $content
277     */
278    public function test_for_shortcode( $content ) {
279        $this->found_shortcode = has_shortcode( $content, self::SHORTCODE );
280
281        return $content;
282    }
283
284    /**
285     * Returns the HTML for the related posts section.
286     *
287     * @uses esc_html__, apply_filters
288     * @return string
289     */
290    public function get_client_rendered_html() {
291        if ( Settings::is_syncing() ) {
292            return '';
293        }
294
295        /**
296         * Filter the Related Posts headline.
297         *
298         * @module related-posts
299         *
300         * @since 3.0.0
301         *
302         * @param string $headline Related Posts heading.
303         */
304        $headline = apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() );
305
306        if ( $this->previous_post_id ) {
307            $exclude = "data-exclude='{$this->previous_post_id}'";
308        } else {
309            $exclude = '';
310        }
311
312        return <<<EOT
313<div id='jp-relatedposts' class='jp-relatedposts' $exclude>
314    $headline
315</div>
316EOT;
317    }
318
319    /**
320     * Returns the HTML for the related posts section if it's running in the loop or other instances where we don't support related posts.
321     *
322     * @return string
323     */
324    public function get_client_rendered_html_unsupported() {
325        if ( Settings::is_syncing() ) {
326            return '';
327        }
328        return "\n\n<!-- Jetpack Related Posts is not supported in this context. -->\n\n";
329    }
330
331    /**
332     * ===============
333     * GUTENBERG BLOCK
334     * ===============
335     */
336
337    /**
338     * Echoes out items for the Gutenberg block
339     *
340     * @param array $related_post The post object.
341     * @param array $block_attributes The block attributes.
342     */
343    public function render_block_item( $related_post, $block_attributes ) {
344        $instance_id = 'related-posts-item-' . uniqid();
345        $label_id    = $instance_id . '-label';
346        $title       = $related_post['title'];
347        $url         = $related_post['url'];
348        $rel         = $related_post['rel'];
349        $img         = '';
350        $list        = '';
351
352        $item_markup = sprintf(
353            '<li id="%1$s" class="jp-related-posts-i2__post">',
354            esc_attr( $instance_id )
355        );
356
357        // Thumbnail
358        if ( ! empty( $block_attributes['show_thumbnails'] ) && ! empty( $related_post['img']['src'] ) ) {
359            $img = sprintf(
360                '<img loading="lazy" class="jp-related-posts-i2__post-img" src="%1$s" alt="%2$s" %3$s/>',
361                esc_url( $related_post['img']['src'] ),
362                esc_attr( $related_post['img']['alt_text'] ),
363                ( ! empty( $related_post['img']['srcset'] ) ? 'srcset="' . esc_attr( $related_post['img']['srcset'] ) . '"' : '' )
364            );
365        }
366
367        // Link
368        $item_markup .= sprintf(
369            '<a id="%1$s" href="%2$s" class="jp-related-posts-i2__post-link" %3$s>%4$s%5$s</a>',
370            esc_attr( $label_id ),
371            esc_url( $url ),
372            ( ! empty( $rel ) ? 'rel="' . esc_attr( $rel ) . '"' : '' ),
373            esc_html( $title ),
374            $img
375        );
376
377        // Date
378        if ( $block_attributes['show_date'] ) {
379            $list .= '<dt>' . __( 'Date', 'jetpack' ) . '</dt>';
380            $list .= '<dd class="jp-related-posts-i2__post-date">';
381            $list .= esc_html( $related_post['date'] );
382            $list .= '</dd>';
383        }
384
385        // Author
386        if ( $block_attributes['show_author'] ) {
387            $list .= '<dt>' . __( 'Author', 'jetpack' ) . '</dt>';
388            $list .= '<dd class="jp-related-posts-i2__post-author">';
389            $list .= esc_html( $related_post['author'] );
390            $list .= '</dd>';
391        }
392
393        // Context
394        if ( ( $block_attributes['show_context'] ) && ! empty( $related_post['block_context'] ) ) {
395            // translators: this is followed by the reason why the item is related to the current post
396            $list .= '<dt>' . __( 'In relation to', 'jetpack' ) . '</dt>';
397            $list .= '<dd class="jp-related-posts-i2__post-context">';
398
399            // Note: The original 'context' value is not used when rendering the block.
400            // It is still generated and available for the legacy rendering code path though.
401            // See './related-posts.js' for that usage.
402            $block_context = $related_post['block_context'];
403
404            if ( ! empty( $block_context['link'] ) ) {
405                $list .= sprintf(
406                    '<a href="%1$s">%2$s</a>',
407                    esc_url( $block_context['link'] ),
408                    esc_html( $block_context['text'] )
409                );
410            } else {
411                $list .= esc_html( $block_context['text'] );
412            }
413
414            $list .= '</dd>';
415        }
416
417        // Metadata
418        if ( ! empty( $list ) ) {
419            $item_markup .= '<dl class="jp-related-posts-i2__post-defs">' . $list . '</dl>';
420        }
421
422        $item_markup .= '</li>';
423
424        return $item_markup;
425    }
426
427    /**
428     * Render the list of related posts.
429     *
430     * @param array $posts The posts to render into the list.
431     * @param array $block_attributes Block attributes.
432     * @return string
433     */
434    public function render_post_list( $posts, $block_attributes ) {
435        $markup = '';
436
437        foreach ( $posts as $post ) {
438            $markup .= $this->render_block_item( $post, $block_attributes );
439        }
440
441        return sprintf(
442            // role="list" is required for accessibility as VoiceOver ignores unstyled lists.
443            '<ul class="jp-related-posts-i2__list" role="list" data-post-count="%1$s">%2$s</ul>',
444            count( $posts ),
445            $markup
446        );
447    }
448
449    /**
450     * Render the related posts markup.
451     *
452     * @param array    $attributes Block attributes.
453     * @param string   $content    String containing the related Posts block content.
454     * @param WP_Block $block    The block object.
455     * @return string
456     */
457    public function render_block( $attributes, $content, $block = null ) {
458        if ( ! Request::is_frontend() ) {
459            return $content;
460        }
461
462        $wrapper_attributes = array();
463        $post_id            = get_the_ID();
464        $block_attributes   = array(
465            'headline'        => isset( $attributes['headline'] ) ? $attributes['headline'] : null,
466            'show_thumbnails' => isset( $attributes['displayThumbnails'] ) && $attributes['displayThumbnails'],
467            'show_author'     => isset( $attributes['displayAuthor'] ) ? (bool) $attributes['displayAuthor'] : false,
468            'show_headline'   => isset( $attributes['displayHeadline'] ) ? (bool) $attributes['displayHeadline'] : false,
469            'show_date'       => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true,
470            'show_context'    => isset( $attributes['displayContext'] ) && $attributes['displayContext'],
471            'layout'          => isset( $attributes['postLayout'] ) && 'list' === $attributes['postLayout'] ? $attributes['postLayout'] : 'grid',
472            'size'            => ! empty( $attributes['postsToShow'] ) ? absint( $attributes['postsToShow'] ) : 3,
473        );
474
475        $excludes = $this->parse_numeric_get_arg( 'relatedposts_origin' );
476
477        $related_posts = $this->get_for_post_id(
478            $post_id,
479            array(
480                'size'             => $block_attributes['size'],
481                'exclude_post_ids' => $excludes,
482            )
483        );
484
485        if ( empty( $related_posts ) ) {
486            return '';
487        }
488
489        $list_markup = $this->render_post_list( $related_posts, $block_attributes );
490
491        if ( empty( $attributes['isServerRendered'] ) ) {
492            // The get_server_rendered_html() path won't register a block,
493            // so only apply block supports when not server rendered.
494            $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
495        }
496
497        $headline_markup = '';
498
499        if ( isset( $block ) ) {
500            foreach ( $block->inner_blocks as $inner_block ) {
501                if ( 'core/heading' === $inner_block->name && ! empty( wp_strip_all_tags( $inner_block->inner_html ) ) ) {
502                    $headline_markup = trim( $inner_block->inner_html );
503                    break;
504                }
505            }
506        }
507
508        if ( empty( $headline_markup ) && $block_attributes['show_headline'] === true ) {
509            $headline = $block_attributes['headline'];
510            if ( strlen( trim( $headline ) ) !== 0 ) {
511                $headline_markup = sprintf(
512                    '<h3 class="jp-relatedposts-headline">%1$s</h3>',
513                    esc_html( $headline )
514                );
515            }
516        }
517
518        $display_markup = sprintf(
519            '<nav class="jp-relatedposts-i2%1$s"%2$s data-layout="%3$s" aria-label="%6$s">%4$s%5$s</nav>',
520            ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
521            ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
522            esc_attr( $block_attributes['layout'] ),
523            $headline_markup,
524            $list_markup,
525            empty( $headline_markup ) ? esc_attr__( 'Related Posts', 'jetpack' ) : esc_attr( wp_strip_all_tags( $headline_markup ) )
526        );
527
528        /**
529         * Filter the output HTML of Related Posts.
530         *
531         * @module related-posts
532         *
533         * @since 10.7
534         *
535         * @param string $display_markup HTML output of Related Posts.
536         * @param int|false get_the_ID() Post ID of the post for which we are retrieving Related Posts.
537         * @param array $related_posts Array of related posts.
538         * @param array $block_attributes Array of Block attributes.
539         */
540        return (string) apply_filters( 'jetpack_related_posts_display_markup', $display_markup, $post_id, $related_posts, $block_attributes );
541    }
542
543    /**
544     * ========================
545     * PUBLIC UTILITY FUNCTIONS
546     * ========================
547     */
548
549    /**
550     * Parse a numeric GET variable to an array of values.
551     *
552     * @since 6.9.0
553     *
554     * @uses absint
555     *
556     * @param string $arg Name of the GET variable.
557     * @return array $result Parsed value(s)
558     */
559    public function parse_numeric_get_arg( $arg ) {
560        $result = array();
561
562        if ( isset( $_GET[ $arg ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- requests are used to generate a list of related posts we want to exclude.
563            if ( is_string( $_GET[ $arg ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
564                $result = explode( ',', sanitize_text_field( wp_unslash( $_GET[ $arg ] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
565            } elseif ( is_array( $_GET[ $arg ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
566                $args   = array_map( 'sanitize_text_field', wp_unslash( $_GET[ $arg ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
567                $result = array_values( $args );
568            }
569
570            $result = array_unique( array_filter( array_map( 'absint', $result ) ) );
571        }
572
573        return $result;
574    }
575
576    /**
577     * Gets options set for Jetpack_RelatedPosts and merge with defaults.
578     *
579     * @uses Jetpack_Options::get_option, apply_filters
580     * @return array
581     */
582    public function get_options() {
583        if ( null === $this->options ) {
584            $this->options = Jetpack_Options::get_option( 'relatedposts', array() );
585            if ( ! is_array( $this->options ) ) {
586                $this->options = array();
587            }
588            if ( ! isset( $this->options['enabled'] ) ) {
589                $this->options['enabled'] = true;
590            }
591            if ( ! isset( $this->options['show_headline'] ) ) {
592                $this->options['show_headline'] = true;
593            }
594            if ( ! isset( $this->options['show_thumbnails'] ) ) {
595                $this->options['show_thumbnails'] = false;
596            }
597            if ( ! isset( $this->options['show_date'] ) ) {
598                $this->options['show_date'] = true;
599            }
600            if ( ! isset( $this->options['show_context'] ) ) {
601                $this->options['show_context'] = true;
602            }
603            if ( ! isset( $this->options['layout'] ) ) {
604                $this->options['layout'] = 'grid';
605            }
606            if ( ! isset( $this->options['headline'] ) ) {
607                $this->options['headline'] = esc_html__( 'Related', 'jetpack' );
608            }
609            if ( empty( $this->options['size'] ) || (int) $this->options['size'] < 1 ) {
610                $this->options['size'] = 3;
611            }
612
613            /**
614             * Filter Related Posts basic options.
615             *
616             * @module related-posts
617             *
618             * @since 2.8.0
619             *
620             * @param array $this->_options Array of basic Related Posts options.
621             */
622            $this->options = apply_filters( 'jetpack_relatedposts_filter_options', $this->options );
623        }
624
625        return $this->options;
626    }
627
628    /**
629     * Gets options.
630     *
631     * @param string $option_name - option we want to get.
632     */
633    public function get_option( $option_name ) {
634        $options = $this->get_options();
635
636        if ( isset( $options[ $option_name ] ) ) {
637            return $options[ $option_name ];
638        }
639
640        return false;
641    }
642
643    /**
644     * Parses input and returns normalized options array.
645     *
646     * @param array $input - input we're parsing.
647     * @uses self::get_options
648     * @return array
649     */
650    public function parse_options( $input ) {
651        $current = $this->get_options();
652
653        if ( ! is_array( $input ) ) {
654            $input = array();
655        }
656
657        if (
658            ! isset( $input['enabled'] )
659            || isset( $input['show_date'] )
660            || isset( $input['show_context'] )
661            || isset( $input['layout'] )
662            || isset( $input['headline'] )
663            ) {
664            $input['enabled'] = '1';
665        }
666
667        if ( '1' == $input['enabled'] ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- expecting string, but may return bools.
668            $current['enabled']         = true;
669            $current['show_headline']   = ( isset( $input['show_headline'] ) && '1' == $input['show_headline'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
670            $current['show_thumbnails'] = ( isset( $input['show_thumbnails'] ) && '1' == $input['show_thumbnails'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
671            $current['show_date']       = ( isset( $input['show_date'] ) && '1' == $input['show_date'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
672            $current['show_context']    = ( isset( $input['show_context'] ) && '1' == $input['show_context'] ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
673            $current['layout']          = isset( $input['layout'] ) && in_array( $input['layout'], array( 'grid', 'list' ), true ) ? $input['layout'] : 'grid';
674            $current['headline']        = isset( $input['headline'] ) ? $input['headline'] : esc_html__( 'Related', 'jetpack' );
675        } else {
676            $current['enabled'] = false;
677        }
678
679        if ( isset( $input['size'] ) && (int) $input['size'] > 0 ) {
680            $current['size'] = (int) $input['size'];
681        } else {
682            $current['size'] = null;
683        }
684        return $current;
685    }
686
687    /**
688     * HTML for admin settings page.
689     *
690     * @uses self::get_options, checked, esc_html__
691     */
692    public function print_setting_html() {
693        $options = $this->get_options();
694
695        $ui_settings_template = <<<'EOT'
696<p class="description">%s</p>
697<ul id="settings-reading-relatedposts-customize">
698    <li>
699        <label><input name="jetpack_relatedposts[show_headline]" type="checkbox" value="1" %s /> %s</label>
700    </li>
701    <li>
702        <label><input name="jetpack_relatedposts[show_thumbnails]" type="checkbox" value="1" %s /> %s</label>
703    </li>
704    <li>
705        <label><input name="jetpack_relatedposts[show_date]" type="checkbox" value="1" %s /> %s</label>
706    </li>
707    <li>
708        <label><input name="jetpack_relatedposts[show_context]" type="checkbox" value="1" %s /> %s</label>
709    </li>
710</ul>
711<div id='settings-reading-relatedposts-preview'>
712    %s
713    <div id="jp-relatedposts" class="jp-relatedposts"></div>
714</div>
715EOT;
716        $ui_settings          = sprintf(
717            $ui_settings_template,
718            esc_html__( 'The following settings will impact all related posts on your site, except for those you created via the block editor:', 'jetpack' ),
719            checked( $options['show_headline'], true, false ),
720            esc_html__( 'Highlight related content with a heading', 'jetpack' ),
721            checked( $options['show_thumbnails'], true, false ),
722            esc_html__( 'Show a thumbnail image where available', 'jetpack' ),
723            checked( $options['show_date'], true, false ),
724            esc_html__( 'Show entry date', 'jetpack' ),
725            checked( $options['show_context'], true, false ),
726            esc_html__( 'Show context (category or tag)', 'jetpack' ),
727            esc_html__( 'Preview:', 'jetpack' )
728        );
729
730        if ( ! $this->allow_feature_toggle() ) {
731            $template = <<<'EOT'
732<input type="hidden" name="jetpack_relatedposts[enabled]" value="1" />
733%s
734EOT;
735            printf(
736                $template, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
737                $ui_settings // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- data is escaped when variable is set.
738            );
739        } else {
740            $template = <<<'EOT'
741<ul id="settings-reading-relatedposts">
742    <li>
743        <label><input type="radio" name="jetpack_relatedposts[enabled]" value="0" class="tog" %s /> %s</label>
744    </li>
745    <li>
746        <label><input type="radio" name="jetpack_relatedposts[enabled]" value="1" class="tog" %s /> %s</label>
747        %s
748    </li>
749</ul>
750EOT;
751            printf(
752                $template, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
753                checked( $options['enabled'], false, false ),
754                esc_html__( 'Hide related content after posts', 'jetpack' ),
755                checked( $options['enabled'], true, false ),
756                esc_html__( 'Show related content after posts', 'jetpack' ),
757                $ui_settings // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- data is escaped when variable is set.
758            );
759        }
760    }
761
762    /**
763     * Head JS/CSS for admin settings page.
764     *
765     * @uses esc_html__
766     * @return null
767     */
768    public function print_setting_head() {
769
770        // only dislay the Related Posts JavaScript on the Reading Settings Admin Page.
771        $current_screen = get_current_screen();
772
773        if ( $current_screen === null ) {
774            return;
775        }
776
777        if ( 'options-reading' !== $current_screen->id ) {
778            return;
779        }
780
781        $related_headline = sprintf(
782            '<h3 class="jp-relatedposts-headline"><em>%s</em></h3>',
783            esc_html__( 'Related', 'jetpack' )
784        );
785
786        $href_params            = 'class="jp-relatedposts-post-a" href="#jetpack_relatedposts" rel="nofollow" data-origin="0" data-position="0"';
787        $related_with_images    = <<<EOT
788<div class="jp-relatedposts-items jp-relatedposts-items-visual">
789    <div class="jp-relatedposts-post jp-relatedposts-post0 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image">
790        <a $href_params>
791            <img class="jp-relatedposts-post-img" src="https://jetpackme.files.wordpress.com/2019/03/cat-blog.png" width="350" alt="Big iPhone/iPad Update Now Available" scale="0">
792        </a>
793        <h4 class="jp-relatedposts-post-title">
794            <a $href_params>Big iPhone/iPad Update Now Available</a>
795        </h4>
796        <p class="jp-relatedposts-post-excerpt">Big iPhone/iPad Update Now Available</p>
797        <p class="jp-relatedposts-post-context">In "Mobile"</p>
798    </div>
799    <div class="jp-relatedposts-post jp-relatedposts-post1 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image">
800        <a $href_params>
801            <img class="jp-relatedposts-post-img" src="https://jetpackme.files.wordpress.com/2019/03/devices.jpg" width="350" alt="The WordPress for Android App Gets a Big Facelift" scale="0">
802        </a>
803        <h4 class="jp-relatedposts-post-title">
804            <a $href_params>The WordPress for Android App Gets a Big Facelift</a>
805        </h4>
806        <p class="jp-relatedposts-post-excerpt">The WordPress for Android App Gets a Big Facelift</p>
807        <p class="jp-relatedposts-post-context">In "Mobile"</p>
808    </div>
809    <div class="jp-relatedposts-post jp-relatedposts-post2 jp-relatedposts-post-thumbs" data-post-id="0" data-post-format="image">
810        <a $href_params>
811            <img class="jp-relatedposts-post-img" src="https://jetpackme.files.wordpress.com/2019/03/mobile-wedding.jpg" width="350" alt="Upgrade Focus: VideoPress For Weddings" scale="0">
812        </a>
813        <h4 class="jp-relatedposts-post-title">
814            <a $href_params>Upgrade Focus: VideoPress For Weddings</a>
815        </h4>
816        <p class="jp-relatedposts-post-excerpt">Upgrade Focus: VideoPress For Weddings</p>
817        <p class="jp-relatedposts-post-context">In "Upgrade"</p>
818    </div>
819</div>
820EOT;
821        $related_with_images    = str_replace( "\n", '', $related_with_images );
822        $related_without_images = <<<EOT
823<div class="jp-relatedposts-items jp-relatedposts-items-minimal">
824    <p class="jp-relatedposts-post jp-relatedposts-post0" data-post-id="0" data-post-format="image">
825        <span class="jp-relatedposts-post-title"><a $href_params>Big iPhone/iPad Update Now Available</a></span>
826        <span class="jp-relatedposts-post-context">In "Mobile"</span>
827    </p>
828    <p class="jp-relatedposts-post jp-relatedposts-post1" data-post-id="0" data-post-format="image">
829        <span class="jp-relatedposts-post-title"><a $href_params>The WordPress for Android App Gets a Big Facelift</a></span>
830        <span class="jp-relatedposts-post-context">In "Mobile"</span>
831    </p>
832    <p class="jp-relatedposts-post jp-relatedposts-post2" data-post-id="0" data-post-format="image">
833        <span class="jp-relatedposts-post-title"><a $href_params>Upgrade Focus: VideoPress For Weddings</a></span>
834        <span class="jp-relatedposts-post-context">In "Upgrade"</span>
835    </p>
836</div>
837EOT;
838        $related_without_images = str_replace( "\n", '', $related_without_images );
839
840        if ( $this->allow_feature_toggle() ) {
841            $extra_css = '#settings-reading-relatedposts-customize { padding-left:2em; margin-top:.5em; }';
842        } else {
843            $extra_css = '';
844        }
845        // phpcs:disable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped -- Escaped above where needed.
846        echo <<<EOT
847<style type="text/css">
848    #settings-reading-relatedposts .disabled { opacity:.5; filter:Alpha(opacity=50); }
849    #settings-reading-relatedposts-preview .jp-relatedposts { background:#fff; padding:.5em; width:75%; }
850    $extra_css
851</style>
852<script type="text/javascript">
853    jQuery( document ).ready( function($) {
854        var update_ui = function() {
855            var is_enabled = true;
856            if ( 'radio' == $( 'input[name="jetpack_relatedposts[enabled]"]' ).attr('type') ) {
857                if ( '0' == $( 'input[name="jetpack_relatedposts[enabled]"]:checked' ).val() ) {
858                    is_enabled = false;
859                }
860            }
861            if ( is_enabled ) {
862                $( '#settings-reading-relatedposts-customize' )
863                    .removeClass( 'disabled' )
864                    .find( 'input' )
865                    .attr( 'disabled', false );
866                $( '#settings-reading-relatedposts-preview' )
867                    .removeClass( 'disabled' );
868            } else {
869                $( '#settings-reading-relatedposts-customize' )
870                    .addClass( 'disabled' )
871                    .find( 'input' )
872                    .attr( 'disabled', true );
873                $( '#settings-reading-relatedposts-preview' )
874                    .addClass( 'disabled' );
875            }
876        };
877
878        var update_preview = function() {
879            var html = '';
880            if ( $( 'input[name="jetpack_relatedposts[show_headline]"]:checked' ).length ) {
881                html += '$related_headline';
882            }
883            if ( $( 'input[name="jetpack_relatedposts[show_thumbnails]"]:checked' ).length ) {
884                html += '$related_with_images';
885            } else {
886                html += '$related_without_images';
887            }
888            $( '#settings-reading-relatedposts-preview .jp-relatedposts' ).html( html );
889            if ( $( 'input[name="jetpack_relatedposts[show_date]"]:checked' ).length ) {
890                $( '.jp-relatedposts-post-title' ).each( function() {
891                    $( this ).after( $( '<span>August 8, 2005</span>' ) );
892                } );
893            }
894            if ( $( 'input[name="jetpack_relatedposts[show_context]"]:checked' ).length ) {
895                $( '.jp-relatedposts-post-context' ).show();
896            } else {
897                $( '.jp-relatedposts-post-context' ).hide();
898            }
899            $( '#settings-reading-relatedposts-preview .jp-relatedposts' ).show();
900        };
901
902        // Update on load
903        update_preview();
904        update_ui();
905
906        // Update on change
907        $( '#settings-reading-relatedposts-customize input' )
908            .change( update_preview );
909        $( '#settings-reading-relatedposts' )
910            .find( 'input.tog' )
911            .change( update_ui );
912    });
913</script>
914EOT;
915        // phpcs:enable WordPress.Security.EscapeOutput.HeredocOutputNotEscaped
916    }
917
918    /**
919     * Gets an array of related posts that match the given post_id.
920     *
921     * @param int   $post_id Post which we want to find related posts for.
922     * @param array $args - params to use when building Elasticsearch filters to narrow down the search domain.
923     * @uses self::get_options, get_post_type, wp_parse_args, apply_filters
924     * @return array
925     */
926    public function get_for_post_id( $post_id, array $args ) {
927        $options = $this->get_options();
928
929        if ( ! empty( $args['size'] ) ) {
930            $options['size'] = $args['size'];
931        }
932
933        if (
934            empty( $options['enabled'] )
935            || 0 === (int) $post_id
936            || empty( $options['size'] )
937        ) {
938            return array();
939        }
940
941        $defaults = array(
942            'size'             => (int) $options['size'],
943            'post_type'        => get_post_type( $post_id ),
944            'post_formats'     => array(),
945            'has_terms'        => array(),
946            'date_range'       => array(),
947            'exclude_post_ids' => array(),
948        );
949        $args     = wp_parse_args( $args, $defaults );
950        /**
951         * Filter the arguments used to retrieve a list of Related Posts.
952         *
953         * @module related-posts
954         *
955         * @since 2.8.0
956         *
957         * @param array $args Array of options to retrieve Related Posts.
958         * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
959         */
960        $args = apply_filters( 'jetpack_relatedposts_filter_args', $args, $post_id );
961
962        $filters = $this->get_es_filters_from_args( $post_id, $args );
963        /**
964         * Filter Elasticsearch options used to calculate Related Posts.
965         *
966         * @module related-posts
967         *
968         * @since 2.8.0
969         *
970         * @param array $filters Array of Elasticsearch filters based on the post_id and args.
971         * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
972         */
973        $filters = apply_filters( 'jetpack_relatedposts_filter_filters', $filters, $post_id );
974
975        $results = $this->get_related_posts( $post_id, $args['size'], $filters );
976        /**
977         * Filter the array of related posts matched by Elasticsearch.
978         *
979         * @module related-posts
980         *
981         * @since 2.8.0
982         *
983         * @param array $results Array of related posts matched by Elasticsearch.
984         * @param int $post_id Post ID of the post for which we are retrieving Related Posts.
985         */
986        return apply_filters( 'jetpack_relatedposts_returned_results', $results, $post_id );
987    }
988
989    /**
990     * =========================
991     * PRIVATE UTILITY FUNCTIONS
992     * =========================
993     */
994
995    /**
996     * Creates an array of Elasticsearch filters based on the post_id and args.
997     *
998     * @param int   $post_id - the post ID.
999     * @param array $args - the arguments.
1000     * @uses apply_filters, get_post_types, get_post_format_strings
1001     * @return array
1002     */
1003    protected function get_es_filters_from_args( $post_id, array $args ) {
1004        $filters = array();
1005
1006        /**
1007         * Filter the terms used to search for Related Posts.
1008         *
1009         * @module related-posts
1010         *
1011         * @since 2.8.0
1012         *
1013         * @param array $args['has_terms'] Array of terms associated to the Related Posts.
1014         * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
1015         */
1016        $args['has_terms'] = apply_filters( 'jetpack_relatedposts_filter_has_terms', $args['has_terms'], $post_id );
1017        if ( ! empty( $args['has_terms'] ) ) {
1018            foreach ( (array) $args['has_terms'] as $term ) {
1019                if ( mb_strlen( $term->taxonomy ) ) {
1020                    switch ( $term->taxonomy ) {
1021                        case 'post_tag':
1022                            $tax_fld = 'tag.slug';
1023                            break;
1024                        case 'category':
1025                            $tax_fld = 'category.slug';
1026                            break;
1027                        default:
1028                            $tax_fld = 'taxonomy.' . $term->taxonomy . '.slug';
1029                            break;
1030                    }
1031                    $filters[] = array( 'term' => array( $tax_fld => $term->slug ) );
1032                }
1033            }
1034        }
1035
1036        /**
1037         * Filter the Post Types where we search Related Posts.
1038         *
1039         * @module related-posts
1040         *
1041         * @since 2.8.0
1042         *
1043         * @param array $args['post_type'] Array of Post Types.
1044         * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
1045         */
1046        $args['post_type'] = apply_filters( 'jetpack_relatedposts_filter_post_type', $args['post_type'], $post_id );
1047        $valid_post_types  = get_post_types();
1048        if ( is_array( $args['post_type'] ) ) {
1049            $sanitized_post_types = array();
1050            foreach ( $args['post_type'] as $pt ) {
1051                if ( in_array( $pt, $valid_post_types, true ) ) {
1052                    $sanitized_post_types[] = $pt;
1053                }
1054            }
1055            if ( ! empty( $sanitized_post_types ) ) {
1056                $filters[] = array( 'terms' => array( 'post_type' => $sanitized_post_types ) );
1057            }
1058        } elseif ( in_array( $args['post_type'], $valid_post_types, true ) && 'all' !== $args['post_type'] ) {
1059                $filters[] = array( 'term' => array( 'post_type' => $args['post_type'] ) );
1060        }
1061
1062        /**
1063         * Filter the Post Formats where we search Related Posts.
1064         *
1065         * @module related-posts
1066         *
1067         * @since 3.3.0
1068         *
1069         * @param array $args['post_formats'] Array of Post Formats.
1070         * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
1071         */
1072        $args['post_formats']   = apply_filters( 'jetpack_relatedposts_filter_post_formats', $args['post_formats'], $post_id );
1073        $valid_post_formats     = get_post_format_strings();
1074        $sanitized_post_formats = array();
1075        foreach ( $args['post_formats'] as $pf ) {
1076            if ( array_key_exists( $pf, $valid_post_formats ) ) {
1077                $sanitized_post_formats[] = $pf;
1078            }
1079        }
1080        if ( ! empty( $sanitized_post_formats ) ) {
1081            $filters[] = array( 'terms' => array( 'post_format' => $sanitized_post_formats ) );
1082        }
1083
1084        /**
1085         * Filter the date range used to search Related Posts.
1086         *
1087         * @module related-posts
1088         *
1089         * @since 2.8.0
1090         *
1091         * @param array $args['date_range'] Array of a month interval where we search Related Posts.
1092         * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
1093         */
1094        $args['date_range'] = apply_filters( 'jetpack_relatedposts_filter_date_range', $args['date_range'], $post_id );
1095        if ( is_array( $args['date_range'] ) && ! empty( $args['date_range'] ) ) {
1096            $args['date_range'] = array_map( 'intval', $args['date_range'] );
1097            if ( ! empty( $args['date_range']['from'] ) && ! empty( $args['date_range']['to'] ) ) {
1098                $filters[] = array(
1099                    'range' => array(
1100                        'date_gmt' => $this->get_coalesced_range( $args['date_range'] ),
1101                    ),
1102                );
1103            }
1104        }
1105
1106        /**
1107         * Filter the Post IDs excluded from appearing in Related Posts.
1108         *
1109         * @module related-posts
1110         *
1111         * @since 2.9.0
1112         *
1113         * @param array $args['exclude_post_ids'] Array of Post IDs.
1114         * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
1115         */
1116        $args['exclude_post_ids'] = apply_filters( 'jetpack_relatedposts_filter_exclude_post_ids', $args['exclude_post_ids'], $post_id );
1117        if ( ! empty( $args['exclude_post_ids'] ) && is_array( $args['exclude_post_ids'] ) ) {
1118            $excluded_post_ids = array();
1119            foreach ( $args['exclude_post_ids'] as $exclude_post_id ) {
1120                $exclude_post_id = (int) $exclude_post_id;
1121                if ( $exclude_post_id > 0 ) {
1122                    $excluded_post_ids[] = $exclude_post_id;
1123                }
1124            }
1125            $filters[] = array( 'not' => array( 'terms' => array( 'post_id' => $excluded_post_ids ) ) );
1126        }
1127
1128        return $filters;
1129    }
1130
1131    /**
1132     * Takes a range and coalesces it into a month interval bracketed by a time as determined by the blog_id to enhance caching.
1133     *
1134     * @todo Rewrite this function with proper date handling rather than `strtotime()` and `date()`.
1135     *
1136     * @param array $date_range - the date range.
1137     * @return array
1138     */
1139    protected function get_coalesced_range( array $date_range ) {
1140        $now           = time();
1141        $coalesce_time = $this->get_blog_id() % 86400;
1142        $current_time  = $now - strtotime( 'today', $now );
1143
1144        if ( $current_time < $coalesce_time && '01' === date( 'd', $now ) ) { // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
1145            // Move back 1 period.
1146            return array(
1147                'from' => date( 'Y-m-01', strtotime( '-1 month', $date_range['from'] ) ) . ' ' . date( 'H:i:s', $coalesce_time ), //phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
1148                'to'   => date( 'Y-m-01', $date_range['to'] ) . ' ' . date( 'H:i:s', $coalesce_time ), //phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
1149            );
1150        } else {
1151            // Use current period.
1152            return array(
1153                'from' => date( 'Y-m-01', $date_range['from'] ) . ' ' . date( 'H:i:s', $coalesce_time ), //phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
1154                'to'   => date( 'Y-m-01', strtotime( '+1 month', $date_range['to'] ) ) . ' ' . date( 'H:i:s', $coalesce_time ), //phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
1155            );
1156        }
1157    }
1158
1159    /**
1160     * Generate and output ajax response for related posts API call.
1161     * NOTE: Calls exit() to end all further processing after payload has been outputed.
1162     *
1163     * @param array $excludes array of post_ids to exclude.
1164     * @uses send_nosniff_header, self::get_for_post_id, get_the_ID
1165     * @return never
1166     */
1167    protected function action_frontend_init_ajax( array $excludes ) {
1168        define( 'DOING_AJAX', true );
1169
1170        header( 'Content-type: application/json; charset=utf-8' ); // JSON can only be UTF-8.
1171        send_nosniff_header();
1172
1173        $options = $this->get_options();
1174
1175        if ( isset( $_GET['jetpackrpcustomize'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- adds dummy content if we're in the customizer.
1176
1177            // If we're in the customizer, add dummy content.
1178            $date_now      = current_time( get_option( 'date_format' ) );
1179            $related_posts = array(
1180                array(
1181                    'id'       => - 1,
1182                    'url'      => 'https://jetpackme.files.wordpress.com/2019/03/cat-blog.png',
1183                    'url_meta' => array(
1184                        'origin'   => 0,
1185                        'position' => 0,
1186                    ),
1187                    'title'    => esc_html__( 'Big iPhone/iPad Update Now Available', 'jetpack' ),
1188                    'date'     => $date_now,
1189                    'format'   => false,
1190                    'excerpt'  => esc_html__( 'It is that time of the year when devices are shiny again.', 'jetpack' ),
1191                    'rel'      => 'nofollow',
1192                    'context'  => esc_html__( 'In "Mobile"', 'jetpack' ),
1193                    'img'      => array(
1194                        'src'    => 'https://jetpackme.files.wordpress.com/2019/03/cat-blog.png',
1195                        'width'  => 350,
1196                        'height' => 200,
1197                    ),
1198                    'classes'  => array(),
1199                ),
1200                array(
1201                    'id'       => - 1,
1202                    'url'      => 'https://jetpackme.files.wordpress.com/2019/03/devices.jpg',
1203                    'url_meta' => array(
1204                        'origin'   => 0,
1205                        'position' => 0,
1206                    ),
1207                    'title'    => esc_html__( 'The WordPress for Android App Gets a Big Facelift', 'jetpack' ),
1208                    'date'     => $date_now,
1209                    'format'   => false,
1210                    'excerpt'  => esc_html__( 'Writing is new again in Android with the new WordPress app.', 'jetpack' ),
1211                    'rel'      => 'nofollow',
1212                    'context'  => esc_html__( 'In "Mobile"', 'jetpack' ),
1213                    'img'      => array(
1214                        'src'    => 'https://jetpackme.files.wordpress.com/2019/03/devices.jpg',
1215                        'width'  => 350,
1216                        'height' => 200,
1217                    ),
1218                    'classes'  => array(),
1219                ),
1220                array(
1221                    'id'       => - 1,
1222                    'url'      => 'https://jetpackme.files.wordpress.com/2019/03/mobile-wedding.jpg',
1223                    'url_meta' => array(
1224                        'origin'   => 0,
1225                        'position' => 0,
1226                    ),
1227                    'title'    => esc_html__( 'Upgrade Focus, VideoPress for weddings', 'jetpack' ),
1228                    'date'     => $date_now,
1229                    'format'   => false,
1230                    'excerpt'  => esc_html__( 'Weddings are in the spotlight now with VideoPress for weddings.', 'jetpack' ),
1231                    'rel'      => 'nofollow',
1232                    'context'  => esc_html__( 'In "Mobile"', 'jetpack' ),
1233                    'img'      => array(
1234                        'src'    => 'https://jetpackme.files.wordpress.com/2019/03/mobile-wedding.jpg',
1235                        'width'  => 350,
1236                        'height' => 200,
1237                    ),
1238                    'classes'  => array(),
1239                ),
1240            );
1241
1242            for ( $total = 0; $total < $options['size'] - 3; $total++ ) {
1243                $related_posts[] = $related_posts[ $total ];
1244            }
1245
1246            $current_post = get_post();
1247
1248            // Exclude current post after filtering to make sure it's excluded and not lost during filtering.
1249            $excluded_posts = array_merge(
1250                /** This filter is already documented in modules/related-posts/jetpack-related-posts.php */
1251                apply_filters( 'jetpack_relatedposts_filter_exclude_post_ids', array() ),
1252                array( $current_post->ID )
1253            );
1254
1255            // Fetch posts with featured image.
1256            $with_post_thumbnails = get_posts(
1257                array(
1258                    'posts_per_page'   => $options['size'],
1259                    'post__not_in'     => $excluded_posts,
1260                    'post_type'        => $current_post->post_type,
1261                    'meta_key'         => '_thumbnail_id',
1262                    'suppress_filters' => false,
1263                )
1264            );
1265
1266            // If we don't have enough, fetch posts without featured image.
1267            $count_post_with_thumbnails = is_countable( $with_post_thumbnails ) ? count( $with_post_thumbnails ) : 0;
1268            $more                       = $options['size'] - $count_post_with_thumbnails;
1269            if ( 0 < $more ) {
1270                $no_post_thumbnails = get_posts(
1271                    array(
1272                        'posts_per_page'   => $more,
1273                        'post__not_in'     => $excluded_posts,
1274                        'post_type'        => $current_post->post_type,
1275                        'meta_query'       => array(
1276                            array(
1277                                'key'     => '_thumbnail_id',
1278                                'compare' => 'NOT EXISTS',
1279                            ),
1280                        ),
1281                        'suppress_filters' => false,
1282                    )
1283                );
1284            } else {
1285                $no_post_thumbnails = array();
1286            }
1287
1288            foreach ( array_merge( $with_post_thumbnails, $no_post_thumbnails ) as $index => $real_post ) {
1289                $related_posts[ $index ]['id']      = $real_post->ID;
1290                $related_posts[ $index ]['url']     = esc_url( get_permalink( $real_post ) );
1291                $related_posts[ $index ]['title']   = $this->to_utf8( $this->get_title( $real_post->post_title, $real_post->post_content, $real_post->ID ) );
1292                $related_posts[ $index ]['date']    = get_the_date( '', $real_post );
1293                $related_posts[ $index ]['excerpt'] = html_entity_decode( $this->to_utf8( $this->get_excerpt( $real_post->post_excerpt, $real_post->post_content ) ), ENT_QUOTES, 'UTF-8' );
1294                $related_posts[ $index ]['img']     = $this->generate_related_post_image_params( $real_post->ID );
1295                $related_posts[ $index ]['context'] = $this->generate_related_post_context( $real_post->ID );
1296            }
1297        } else {
1298            $related_posts = $this->get_for_post_id(
1299                get_the_ID(),
1300                array(
1301                    'exclude_post_ids' => $excludes,
1302                )
1303            );
1304        }
1305
1306        $response = array(
1307            'version'         => self::VERSION,
1308            'show_thumbnails' => (bool) ( $options['show_thumbnails'] ?? false ),
1309            'show_date'       => (bool) ( $options['show_date'] ?? true ),
1310            'show_context'    => (bool) ( $options['show_context'] ?? true ),
1311            'layout'          => (string) ( $options['layout'] ?? 'grid' ),
1312            'headline'        => (string) ( $options['headline'] ?? '' ),
1313            'items'           => array(),
1314        );
1315
1316        if ( ! empty( $options['size'] ) && count( $related_posts ) === $options['size'] ) {
1317            $response['items'] = $related_posts;
1318        }
1319
1320        // @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal -- It takes null, but its phpdoc only says int.
1321        wp_send_json( $response, null, JSON_UNESCAPED_SLASHES );
1322    }
1323
1324    /**
1325     * Returns a UTF-8 encoded array of post information for the given post_id
1326     *
1327     * @param int $post_id - the post ID.
1328     * @param int $position - position of the post.
1329     * @param int $origin - The post id that this is related to.
1330     * @uses get_post, get_permalink, remove_query_arg, get_post_format, apply_filters
1331     * @return array
1332     */
1333    public function get_related_post_data_for_post( $post_id, $position, $origin ) {
1334        $post = get_post( $post_id );
1335        return array(
1336            'id'            => $post->ID,
1337            'url'           => get_permalink( $post->ID ),
1338            'url_meta'      => array(
1339                'origin'   => $origin,
1340                'position' => $position,
1341            ),
1342            'title'         => $this->to_utf8( $this->get_title( $post->post_title, $post->post_content, $post->ID ) ),
1343            'author'        => $this->generate_related_post_display_author( $post->ID ),
1344            'date'          => get_the_date( '', $post->ID ),
1345            'format'        => get_post_format( $post->ID ),
1346            'excerpt'       => html_entity_decode( $this->to_utf8( $this->get_excerpt( $post->post_excerpt, $post->post_content ) ), ENT_QUOTES, 'UTF-8' ),
1347            /**
1348             * Filters the rel attribute for the Related Posts' links.
1349             *
1350             * @module related-posts
1351             *
1352             * @since 3.7.0
1353             * @since 7.9.0 - Change Default value to empty.
1354             *
1355             * @param string $link_rel Link rel attribute for Related Posts' link. Default is empty.
1356             * @param int    $post->ID Post ID.
1357             */
1358            'rel'           => apply_filters( 'jetpack_relatedposts_filter_post_link_rel', '', $post->ID ),
1359            /**
1360             * Filter the context displayed below each Related Post.
1361             *
1362             * This context is used when rendering the legacy 'widget' version of Related Posts.
1363             * It is not used when rendering the block-based version. See 'block_context' below for that.
1364             *
1365             * @module related-posts
1366             *
1367             * @since 3.0.0
1368             *
1369             * @param string $this->to_utf8( $this->generate_related_post_context( $post->ID ) ) Context displayed below each related post.
1370             * @param int $post_id Post ID of the post for which we are retrieving Related Posts.
1371             */
1372            'context'       => apply_filters(
1373                'jetpack_relatedposts_filter_post_context',
1374                $this->to_utf8( $this->generate_related_post_context( $post->ID ) ),
1375                $post->ID
1376            ),
1377            // The context used when rendering as a Block. No filtering applied.
1378            'block_context' => $this->generate_related_post_context_block( $post->ID ),
1379            'img'           => $this->generate_related_post_image_params( $post->ID ),
1380            /**
1381             * Filter the post css classes added on HTML markup.
1382             *
1383             * @module related-posts
1384             *
1385             * @since 3.8.0
1386             *
1387             * @param array array() CSS classes added on post HTML markup.
1388             * @param string $post_id Post ID.
1389             */
1390            'classes'       => apply_filters(
1391                'jetpack_relatedposts_filter_post_css_classes',
1392                array(),
1393                $post->ID
1394            ),
1395        );
1396    }
1397
1398    /**
1399     * Returns either the title or a small excerpt to use as title for post.
1400     *
1401     * @uses strip_shortcodes, wp_trim_words, __, apply_filters
1402     *
1403     * @param string $post_title   Post title.
1404     * @param string $post_content Post content.
1405     * @param int    $post_id Post ID.
1406     *
1407     * @return string
1408     */
1409    protected function get_title( $post_title, $post_content, $post_id ) {
1410        if ( ! empty( $post_title ) ) {
1411            return wp_strip_all_tags(
1412                /** This filter is documented in core/src/wp-includes/post-template.php */
1413                apply_filters( 'the_title', $post_title, $post_id )
1414            );
1415        }
1416
1417        $post_title = wp_trim_words( wp_strip_all_tags( strip_shortcodes( $post_content ) ), 5, '…' );
1418        if ( ! empty( $post_title ) ) {
1419            return $post_title;
1420        }
1421
1422        return __( 'Untitled Post', 'jetpack' );
1423    }
1424
1425    /**
1426     * Returns a plain text post excerpt for title attribute of links.
1427     *
1428     * @param string $post_excerpt - the post excerpt.
1429     * @param string $post_content - the post content.
1430     * @uses strip_shortcodes, wp_strip_all_tags, wp_trim_words
1431     * @return string
1432     */
1433    protected function get_excerpt( $post_excerpt, $post_content ) {
1434        if ( empty( $post_excerpt ) ) {
1435            $excerpt = $post_content;
1436        } else {
1437            $excerpt = $post_excerpt;
1438        }
1439
1440        return wp_trim_words( wp_strip_all_tags( strip_shortcodes( $excerpt ) ), 50, '…' );
1441    }
1442
1443    /**
1444     * Generates the thumbnail image to be used for the post. Uses the
1445     * image as returned by Jetpack_PostImages::get_image()
1446     *
1447     * @param int $post_id - the post ID.
1448     * @uses self::get_options, apply_filters, Jetpack_PostImages::get_image, Jetpack_PostImages::fit_image_url
1449     * @return string
1450     */
1451    protected function generate_related_post_image_params( $post_id ) {
1452        $image_params = array(
1453            'alt_text' => '',
1454            'src'      => '',
1455            'width'    => 0,
1456            'height'   => 0,
1457        );
1458
1459        /**
1460         * Filter the size of the Related Posts images.
1461         *
1462         * @module related-posts
1463         *
1464         * @since 2.8.0
1465         *
1466         * @param array array( 'width' => 350, 'height' => 200 ) Size of the images displayed below each Related Post.
1467         */
1468        $thumbnail_size = apply_filters(
1469            'jetpack_relatedposts_filter_thumbnail_size',
1470            array(
1471                'width'  => 350,
1472                'height' => 200,
1473            )
1474        );
1475        if ( ! is_array( $thumbnail_size ) ) {
1476            $thumbnail_size = array(
1477                'width'  => (int) $thumbnail_size,
1478                'height' => (int) $thumbnail_size,
1479            );
1480        }
1481
1482        // Try to get post image.
1483        if ( class_exists( 'Jetpack_PostImages' ) ) {
1484            $img_url    = '';
1485            $post_image = Jetpack_PostImages::get_image(
1486                $post_id,
1487                $thumbnail_size
1488            );
1489
1490            if ( is_array( $post_image ) ) {
1491                $img_url = $post_image['src'];
1492            } elseif ( class_exists( 'Jetpack_Media_Summary' ) ) {
1493                $media = Jetpack_Media_Summary::get( $post_id );
1494
1495                if ( is_array( $media ) && ! empty( $media['image'] ) ) {
1496                    $img_url = $media['image'];
1497                }
1498            }
1499
1500            if ( ! empty( $img_url ) ) {
1501                if ( ! empty( $post_image['alt_text'] ) ) {
1502                    $image_params['alt_text'] = $post_image['alt_text'];
1503                } else {
1504                    $image_params['alt_text'] = '';
1505                }
1506
1507                $thumbnail_width  = 0;
1508                $thumbnail_height = 0;
1509
1510                if ( ! empty( $thumbnail_size['width'] ) ) {
1511                    $thumbnail_width       = $thumbnail_size['width'];
1512                    $image_params['width'] = $thumbnail_width;
1513                }
1514
1515                if ( ! empty( $thumbnail_size['height'] ) ) {
1516                    $thumbnail_height       = $thumbnail_size['height'];
1517                    $image_params['height'] = $thumbnail_height;
1518                }
1519
1520                $image_params['src'] = Jetpack_PostImages::fit_image_url(
1521                    $img_url,
1522                    $thumbnail_width,
1523                    $thumbnail_height
1524                );
1525
1526                // Add a srcset to handle zoomed views and high-density screens.
1527                $srcset = Jetpack_PostImages::generate_cropped_srcset(
1528                    $post_image,
1529                    $thumbnail_width,
1530                    $thumbnail_height
1531                );
1532                if ( ! empty( $srcset ) ) {
1533                    $image_params['srcset'] = $srcset;
1534                }
1535            }
1536        }
1537
1538        return $image_params;
1539    }
1540
1541    /**
1542     * Returns the string UTF-8 encoded
1543     *
1544     * @param string $text - the text we want to convert.
1545     * @return string
1546     */
1547    protected function to_utf8( $text ) {
1548        if ( $this->convert_charset ) {
1549            return iconv( $this->blog_charset, 'UTF-8', $text );
1550        } else {
1551            return $text;
1552        }
1553    }
1554
1555    /**
1556     * =============================================
1557     * PROTECTED UTILITY FUNCTIONS EXTENDED BY WPCOM
1558     * =============================================
1559     */
1560
1561    /**
1562     * Workhorse method to return array of related posts matched by Elasticsearch.
1563     *
1564     * @param int   $post_id - the ID of the post.
1565     * @param int   $size - the size of the post.
1566     * @param array $filters - filters.
1567     * @uses wp_remote_post, is_wp_error, get_option, wp_remote_retrieve_body, get_post, add_query_arg, remove_query_arg, get_permalink, get_post_format, apply_filters
1568     * @return array
1569     */
1570    protected function get_related_posts( $post_id, $size, array $filters ) {
1571        $hits = $this->filter_non_public_posts(
1572            $this->get_related_post_ids(
1573                $post_id,
1574                $size,
1575                $filters
1576            )
1577        );
1578
1579        /**
1580         * Filter the Related Posts matched by Elasticsearch.
1581         *
1582         * @module related-posts
1583         *
1584         * @since 2.9.0
1585         *
1586         * @param array $hits Array of Post IDs matched by Elasticsearch.
1587         * @param string $post_id Post ID of the post for which we are retrieving Related Posts.
1588         */
1589        $hits = apply_filters( 'jetpack_relatedposts_filter_hits', $hits, $post_id );
1590
1591        $related_posts = array();
1592        foreach ( $hits as $i => $hit ) {
1593            $related_posts[] = $this->get_related_post_data_for_post( $hit['id'], $i, $post_id );
1594        }
1595        return $related_posts;
1596    }
1597
1598    /**
1599     * Get array of related posts matched by Elasticsearch.
1600     *
1601     * @param int   $post_id - the post ID.
1602     * @param int   $size - the size.
1603     * @param array $filters - some filters.
1604     * @uses wp_remote_post, is_wp_error, wp_remote_retrieve_body, get_post_meta, update_post_meta
1605     * @return array
1606     */
1607    protected function get_related_post_ids( $post_id, $size, array $filters ) {
1608        $transient_name = null;
1609        $now_ts         = time();
1610        $cache_meta_key = '_jetpack_related_posts_cache';
1611
1612        $body = array(
1613            'size' => (int) $size,
1614        );
1615
1616        if ( ! empty( $filters ) ) {
1617            $body['filter'] = array( 'and' => $filters );
1618        }
1619
1620        // Build cache key.
1621        $cache_key = md5( serialize( $body ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- this is used for caching.
1622
1623        // Load all cached values.
1624        if ( wp_using_ext_object_cache() ) {
1625            $transient_name = "{$cache_meta_key}_{$cache_key}_{$post_id}";
1626            $cache          = get_transient( $transient_name );
1627            if ( false !== $cache ) {
1628                return $cache;
1629            }
1630        } else {
1631            $cache = get_post_meta( $post_id, $cache_meta_key, true );
1632
1633            if ( empty( $cache ) ) {
1634                $cache = array();
1635            }
1636
1637            // Cache is valid! Return cached value.
1638            if ( isset( $cache[ $cache_key ] ) && is_array( $cache[ $cache_key ] ) && $cache[ $cache_key ]['expires'] > $now_ts ) {
1639                return $cache[ $cache_key ]['payload'];
1640            }
1641        }
1642
1643        $user_agent = '';
1644        if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
1645            $user_agent = strtolower( filter_var( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) );
1646        }
1647
1648        $response = wp_remote_post(
1649            "https://public-api.wordpress.com/rest/v1/sites/{$this->get_blog_id()}/posts/$post_id/related/",
1650            array(
1651                'timeout'    => 10,
1652                'user-agent' => "jetpack_related_posts, $user_agent",
1653                'sslverify'  => true,
1654                'body'       => $body,
1655            )
1656        );
1657
1658        // Oh no... return nothing don't cache errors. Also, don't cache HTTP 409 conflict responses.
1659        if ( is_wp_error( $response ) || WP_Http::CONFLICT === wp_remote_retrieve_response_code( $response ) ) {
1660            if ( isset( $cache[ $cache_key ] ) && is_array( $cache[ $cache_key ] ) ) {
1661                return $cache[ $cache_key ]['payload']; // return stale.
1662            } else {
1663                return array();
1664            }
1665        }
1666
1667        $results       = json_decode( wp_remote_retrieve_body( $response ), true );
1668        $related_posts = array();
1669        if ( is_array( $results ) && ! empty( $results['hits'] ) ) {
1670            foreach ( $results['hits'] as $hit ) {
1671                $related_posts[] = array(
1672                    'id' => $hit['fields']['post_id'],
1673                );
1674            }
1675        }
1676
1677        // An empty array might indicate no related posts or that posts
1678        // are not yet synced to WordPress.com, so we cache for only 1
1679        // minute in this case.
1680        if ( empty( $related_posts ) ) {
1681            $cache_ttl = 60;
1682        } else {
1683            $cache_ttl = 12 * HOUR_IN_SECONDS;
1684        }
1685
1686        // Update cache.
1687        if ( wp_using_ext_object_cache() ) {
1688            set_transient( $transient_name, $related_posts, $cache_ttl );
1689        } else {
1690            // Copy all valid cache values.
1691            $new_cache = array();
1692            foreach ( $cache as $k => $v ) {
1693                if ( is_array( $v ) && $v['expires'] > $now_ts ) {
1694                    $new_cache[ $k ] = $v;
1695                }
1696            }
1697
1698            // Set new cache value.
1699            $cache_expires           = $cache_ttl + $now_ts;
1700            $new_cache[ $cache_key ] = array(
1701                'expires' => $cache_expires,
1702                'payload' => $related_posts,
1703            );
1704            update_post_meta( $post_id, $cache_meta_key, $new_cache );
1705        }
1706
1707        return $related_posts;
1708    }
1709
1710    /**
1711     * Filter out any hits that are not public anymore.
1712     *
1713     * @param array $related_posts - the related posts.
1714     * @uses get_post_stati, get_post_status
1715     * @return array
1716     */
1717    protected function filter_non_public_posts( array $related_posts ) {
1718        $public_stati = get_post_stati( array( 'public' => true ) );
1719
1720        $filtered = array();
1721        foreach ( $related_posts as $hit ) {
1722            if ( in_array( get_post_status( $hit['id'] ), $public_stati, true ) ) {
1723                $filtered[] = $hit;
1724            }
1725        }
1726        return $filtered;
1727    }
1728
1729    /**
1730     * Generates the author byline for the related post.
1731     *
1732     * @param int $post_id - the post ID.
1733     * @uses get_post_field, get_the_author_meta
1734     * @return string
1735     */
1736    protected function generate_related_post_display_author( $post_id ) {
1737        $post_author         = get_post_field( 'post_author', $post_id );
1738        $author_display_name = get_the_author_meta( 'display_name', $post_author );
1739        if ( ! empty( $author_display_name ) ) {
1740            return $author_display_name;
1741        }
1742        return '';
1743    }
1744
1745    /**
1746     * Generates a context for the related content (second line in related post output).
1747     * Order of importance:
1748     *   - First category (Not 'Uncategorized')
1749     *   - First post tag
1750     *   - Number of comments
1751     *
1752     * @param int $post_id - the post ID.
1753     * @uses get_the_category, get_the_terms, get_comments_number, number_format_i18n, __, _n
1754     * @return string
1755     */
1756    protected function generate_related_post_context_block( $post_id ) {
1757        $categories = get_the_category( $post_id );
1758        if ( is_array( $categories ) ) {
1759            foreach ( $categories as $category ) {
1760                if ( $category instanceof WP_Term && 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1761                    $cat_link = get_category_link( $category );
1762                    return array(
1763                        'text' => trim( $category->name ),
1764                        'link' => $cat_link,
1765                    );
1766                }
1767            }
1768        }
1769        $tags = get_the_terms( $post_id, 'post_tag' );
1770        if ( is_array( $tags ) ) {
1771            foreach ( $tags as $tag ) {
1772                if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) {
1773                    $tag_link = get_tag_link( $tag );
1774                    return array(
1775                        'text' => trim( $tag->name ),
1776                        'link' => $tag_link,
1777                    );
1778                }
1779            }
1780        }
1781        $comment_count = get_comments_number( $post_id );
1782        if ( $comment_count > 0 ) {
1783            $comments_string = sprintf(
1784                // Translators: amount of comments.
1785                _n( 'With %s comment', 'With %s comments', $comment_count, 'jetpack' ),
1786                number_format_i18n( $comment_count )
1787            );
1788            $comments_link = get_comments_link( $post_id );
1789            return array(
1790                'text' => $comments_string,
1791                'link' => $comments_link,
1792            );
1793        }
1794        $fallback_string = __( 'Similar post', 'jetpack' );
1795        return array(
1796            'text' => $fallback_string,
1797            'link' => '',
1798        );
1799    }
1800
1801    /**
1802     * Generates a context for the related content (second line in related post output).
1803     * Order of importance:
1804     *   - First category (Not 'Uncategorized')
1805     *   - First post tag
1806     *   - Number of comments
1807     *
1808     * @param int $post_id - the post ID.
1809     * @uses get_the_category, get_the_terms, get_comments_number, number_format_i18n, __, _n
1810     * @return string
1811     */
1812    protected function generate_related_post_context( $post_id ) {
1813        $categories = get_the_category( $post_id );
1814        if ( is_array( $categories ) ) {
1815            foreach ( $categories as $category ) {
1816                if ( $category instanceof WP_Term && 'uncategorized' !== $category->slug && '' !== trim( $category->name ) ) {
1817                    $post_cat_context = sprintf(
1818                        // Translators: The category or tag name.
1819                        esc_html_x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1820                        $category->name
1821                    );
1822                    /**
1823                     * Filter the "In Category" line displayed in the post context below each Related Post.
1824                     *
1825                     * @module related-posts
1826                     *
1827                     * @since 3.2.0
1828                     *
1829                     * @param string $post_cat_context "In Category" line displayed in the post context below each Related Post.
1830                     * @param array $category Array containing information about the category.
1831                     */
1832                    return apply_filters( 'jetpack_relatedposts_post_category_context', $post_cat_context, $category );
1833                }
1834            }
1835        }
1836
1837        $tags = get_the_terms( $post_id, 'post_tag' );
1838        if ( is_array( $tags ) ) {
1839            foreach ( $tags as $tag ) {
1840                if ( $tag instanceof WP_Term && '' !== trim( $tag->name ) ) {
1841                    $post_tag_context = sprintf(
1842                        // Translators: the category or tag name.
1843                        _x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
1844                        $tag->name
1845                    );
1846                    /**
1847                     * Filter the "In Tag" line displayed in the post context below each Related Post.
1848                     *
1849                     * @module related-posts
1850                     *
1851                     * @since 3.2.0
1852                     *
1853                     * @param string $post_tag_context "In Tag" line displayed in the post context below each Related Post.
1854                     * @param array $tag Array containing information about the tag.
1855                     */
1856                    return apply_filters( 'jetpack_relatedposts_post_tag_context', $post_tag_context, $tag );
1857                }
1858            }
1859        }
1860
1861        $comment_count = get_comments_number( $post_id );
1862        if ( $comment_count > 0 ) {
1863            return sprintf(
1864                // Translators: amount of comments.
1865                _n( 'With %s comment', 'With %s comments', $comment_count, 'jetpack' ),
1866                number_format_i18n( $comment_count )
1867            );
1868        }
1869
1870        return __( 'Similar post', 'jetpack' );
1871    }
1872
1873    /**
1874     * Logs clicks for clickthrough analysis and related result tuning.
1875     *
1876     * @param int $post_id - the post ID.
1877     * @param int $to_post_id - the to post ID.
1878     * @param int $link_position - the link position.
1879     */
1880    protected function log_click( $post_id, $to_post_id, $link_position ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
1881    }
1882
1883    /**
1884     * Determines if the current post is able to use related posts.
1885     *
1886     * @since 14.0 Checks for singular instead of single to allow usage on non-posts CPTs in block themes.
1887     * @uses self::get_options, is_admin, is_singular, apply_filters
1888     * @return bool
1889     */
1890    protected function enabled_for_request() {
1891        /*
1892         * On block themes, allow usage on any singular view (post, page, CPT).
1893         * On classic themes, only allow usage on single posts by default.
1894         */
1895        $enabled_on_singular_views = wp_is_block_theme()
1896            ? is_singular()
1897            : is_single();
1898
1899        $enabled = $enabled_on_singular_views
1900            && ! is_attachment()
1901            && ! is_admin()
1902            && ! is_embed()
1903            && ( ! $this->allow_feature_toggle() || $this->get_option( 'enabled' ) );
1904
1905        /**
1906         * Filter the Enabled value to allow related posts to be selectively enabled/disabled.
1907         *
1908         * @module related-posts
1909         *
1910         * @since 3.3.0
1911         *
1912         * @param bool $enabled Should Related Posts be enabled on the current page.
1913         */
1914        return apply_filters( 'jetpack_relatedposts_filter_enabled_for_request', $enabled );
1915    }
1916
1917    /**
1918     * Adds filters.
1919     *
1920     * @uses self::enqueue_assets, self::setup_shortcode, add_filter
1921     */
1922    protected function action_frontend_init_page() {
1923        $this->enqueue_assets( true, true );
1924        $this->setup_shortcode();
1925
1926        add_filter( 'the_content', array( $this, 'filter_add_target_to_dom' ), 40 );
1927    }
1928
1929    /**
1930     * Determines if the scripts need be enqueued.
1931     *
1932     * @return bool
1933     */
1934    protected function requires_scripts() {
1935        return (
1936            ! ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) &&
1937            ! has_block( 'jetpack/related-posts' ) &&
1938            ! Blocks::is_fse_theme()
1939        );
1940    }
1941
1942    /**
1943     * Enqueues assets needed to do async loading of related posts.
1944     *
1945     * @param string $script - the script we're enqueing.
1946     * @param string $style - the style we're enqueing.
1947     *
1948     * @uses wp_enqueue_script, wp_enqueue_style, plugins_url
1949     */
1950    protected function enqueue_assets( $script, $style ) {
1951        $dependencies = is_customize_preview() ? array( 'customize-base' ) : array();
1952        // Do not enqueue scripts unless they are required.
1953        if ( $script && $this->requires_scripts() ) {
1954            wp_enqueue_script(
1955                'jetpack_related-posts',
1956                Assets::get_file_url_for_environment(
1957                    '_inc/build/related-posts/related-posts.min.js',
1958                    'modules/related-posts/related-posts.js'
1959                ),
1960                $dependencies,
1961                self::VERSION,
1962                false
1963            );
1964            $related_posts_js_options = array(
1965                /**
1966                 * Filter each Related Post Heading structure.
1967                 *
1968                 * @since 4.0.0
1969                 *
1970                 * @param string $str Related Post Heading structure. Default to h4.
1971                 */
1972                'post_heading' => apply_filters( 'jetpack_relatedposts_filter_post_heading', esc_attr( 'h4' ) ),
1973            );
1974            wp_localize_script( 'jetpack_related-posts', 'related_posts_js_options', $related_posts_js_options );
1975        }
1976        if ( $style ) {
1977            wp_enqueue_style( 'jetpack_related-posts', plugins_url( 'related-posts.css', __FILE__ ), array(), self::VERSION );
1978            wp_style_add_data( 'jetpack_related-posts', 'rtl', 'replace' );
1979            add_action( 'amp_post_template_css', array( $this, 'render_amp_reader_mode_css' ) );
1980        }
1981    }
1982
1983    /**
1984     * Render AMP's reader mode CSS.
1985     */
1986    public function render_amp_reader_mode_css() {
1987        echo file_get_contents( __DIR__ . '/related-posts.css' );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- this is loading a CSS file.
1988    }
1989
1990    /**
1991     * Sets up the shortcode processing.
1992     *
1993     * @uses add_filter, add_shortcode
1994     */
1995    protected function setup_shortcode() {
1996        add_filter( 'the_content', array( $this, 'test_for_shortcode' ), 0 );
1997
1998        add_shortcode( self::SHORTCODE, array( $this, 'get_client_rendered_html' ) );
1999    }
2000
2001    /**
2002     * Return status of related posts toggle.
2003     */
2004    protected function allow_feature_toggle() {
2005        if ( null === $this->allow_feature_toggle ) {
2006            /**
2007             * Filter the display of the Related Posts toggle in Settings > Reading.
2008             *
2009             * @module related-posts
2010             *
2011             * @since 2.8.0
2012             *
2013             * @param bool $allow_feature_toggle Display a feature toggle. Default to false.
2014             */
2015            $this->allow_feature_toggle = apply_filters( 'jetpack_relatedposts_filter_allow_feature_toggle', false );
2016        }
2017        return $this->allow_feature_toggle;
2018    }
2019
2020    /**
2021     * ===================================================
2022     * FUNCTIONS EXPOSING RELATED POSTS IN THE WP REST API
2023     * ===================================================
2024     */
2025
2026    /**
2027     * Add Related Posts to the REST API Post response.
2028     *
2029     * @since 4.4.0
2030     *
2031     * @action rest_api_init
2032     * @uses register_rest_field, self::rest_get_related_posts
2033     */
2034    public function rest_register_related_posts() {
2035        /** This filter is already documented in class.json-api-endpoints.php */
2036        $post_types = apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) );
2037
2038        /**
2039         * Filter the post types that are allowed to have related posts.
2040         *
2041         * @since 15.3
2042         *
2043         * @param array $post_types The post types that are allowed to have related posts.
2044         */
2045        $post_types = apply_filters( 'jetpack_related_posts_rest_api_allowed_post_types', $post_types );
2046
2047        foreach ( $post_types as $post_type ) {
2048            register_rest_field(
2049                $post_type,
2050                'jetpack-related-posts',
2051                array(
2052                    'get_callback'    => array( $this, 'rest_get_related_posts' ),
2053                    'update_callback' => null,
2054                    'schema'          => null,
2055                )
2056            );
2057        }
2058    }
2059
2060    /**
2061     * Build an array of Related Posts.
2062     * By default returns cached results that are stored for up to 12 hours.
2063     *
2064     * @since 4.4.0
2065     *
2066     * @param array $object Details of current post.
2067     *
2068     * @uses self::get_for_post_id
2069     *
2070     * @return array
2071     */
2072    public function rest_get_related_posts( $object ) {
2073        if ( ! isset( $object['id'] ) ) {
2074            return array();
2075        }
2076
2077        // If the Related Posts option is turned off, don't get the related posts.
2078        $options = \Jetpack_Options::get_option( 'relatedposts', array() );
2079        if ( empty( $options['enabled'] ) || ! $options['enabled'] ) {
2080            return array();
2081        }
2082
2083        // If the current post doesn't contain a Related Posts block, and we're also on an admin page, don't get the related posts.
2084        // This will ensure that if the feature is enabled, we can still retrieve Related Posts via the REST API.
2085        if ( ! has_block( 'jetpack/related-posts' ) && is_admin() ) {
2086            return array();
2087        }
2088
2089        return $this->get_for_post_id( $object['id'], array( 'size' => 6 ) );
2090    }
2091}
2092
2093/**
2094 * The raw related posts class can be used by other plugins or themes
2095 * to get related content. This class wraps the existing RelatedPosts
2096 * logic thus we never want to add anything to the DOM or do anything
2097 * for event hooks. We will also not present any settings for this
2098 * class and keep it enabled as calls to this class are done
2099 * programmatically.
2100 */
2101class Jetpack_RelatedPosts_Raw extends Jetpack_RelatedPosts { //phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace, Generic.Files.OneObjectStructurePerFile.MultipleFound
2102
2103    /**
2104     * The query name we want to look up.
2105     *
2106     * @var string
2107     */
2108    protected $query_name;
2109
2110    /**
2111     * Allows callers of this class to tag each query with a unique name for tracking purposes.
2112     *
2113     * @param string $name - the name of the query.
2114     * @return Jetpack_RelatedPosts_Raw
2115     */
2116    public function set_query_name( $name ) {
2117        $this->query_name = (string) $name;
2118        return $this;
2119    }
2120
2121    /**
2122     * Initialize admin.
2123     */
2124    public function action_admin_init() {}
2125
2126    /**
2127     * Initialize front end.
2128     */
2129    public function action_frontend_init() {}
2130
2131    /**
2132     * Get options.
2133     */
2134    public function get_options() {
2135        return array(
2136            'enabled' => true,
2137        );
2138    }
2139
2140    /**
2141     * Workhorse method to return array of related posts ids matched by Elasticsearch.
2142     *
2143     * @param int   $post_id - the post ID.
2144     * @param int   $size - size of the post.
2145     * @param array $filters - filters we're using.
2146     * @uses wp_remote_post, is_wp_error, wp_remote_retrieve_body
2147     * @return array
2148     */
2149    protected function get_related_posts( $post_id, $size, array $filters ) {
2150        $hits = $this->filter_non_public_posts(
2151            $this->get_related_post_ids(
2152                $post_id,
2153                $size,
2154                $filters
2155            )
2156        );
2157
2158        /** This filter is already documented in modules/related-posts/related-posts.php */
2159        $hits = apply_filters( 'jetpack_relatedposts_filter_hits', $hits, $post_id );
2160
2161        return $hits;
2162    }
2163}