Software : _blog_charset = get_option( 'blog_charset' );
$this->_convert_charset = ( function_exists( 'iconv' ) && ! preg_match( '/^utf\-?8$/i', $this->_blog_charset ) );
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
add_action( 'wp', array( $this, 'action_frontend_init' ) );
if ( ! class_exists( 'Jetpack_Media_Summary' ) ) {
jetpack_require_lib( 'class.media-summary' );
}
// Add Related Posts to the REST API Post response.
add_action( 'rest_api_init', array( $this, 'rest_register_related_posts' ) );
jetpack_register_block(
'jetpack/related-posts',
array(
'render_callback' => array( $this, 'render_block' ),
)
);
}
protected function get_blog_id() {
return Jetpack_Options::get_option( 'id' );
}
/**
* =================
* ACTIONS & FILTERS
* =================
*/
/**
* Add a checkbox field to Settings > Reading for enabling related posts.
*
* @action admin_init
* @uses add_settings_field, __, register_setting, add_action
* @return null
*/
public function action_admin_init() {
// Add the setting field [jetpack_relatedposts] and place it in Settings > Reading
add_settings_field( 'jetpack_relatedposts', '' . __( 'Related posts', 'jetpack' ) . '', array( $this, 'print_setting_html' ), 'reading' );
register_setting( 'reading', 'jetpack_relatedposts', array( $this, 'parse_options' ) );
add_action('admin_head', array( $this, 'print_setting_head' ) );
if( 'options-reading.php' == $GLOBALS['pagenow'] ) {
// Enqueue style for live preview on the reading settings page
$this->_enqueue_assets( false, true );
}
}
/**
* Load related posts assets if it's a elegiable front end page or execute search and return JSON if it's an endpoint request.
*
* @global $_GET
* @action wp
* @uses add_shortcode, get_the_ID
* @returns null
*/
public function action_frontend_init() {
// Add a shortcode handler that outputs nothing, this gets overridden later if we can display related content
add_shortcode( self::SHORTCODE, array( $this, 'get_client_rendered_html_unsupported' ) );
if ( ! $this->_enabled_for_request() )
return;
if ( isset( $_GET['relatedposts'] ) ) {
$excludes = $this->parse_numeric_get_arg( 'relatedposts_exclude' );
$this->_action_frontend_init_ajax( $excludes );
} else {
if ( isset( $_GET['relatedposts_hit'], $_GET['relatedposts_origin'], $_GET['relatedposts_position'] ) ) {
$this->_log_click( $_GET['relatedposts_origin'], get_the_ID(), $_GET['relatedposts_position'] );
$this->_previous_post_id = (int) $_GET['relatedposts_origin'];
}
$this->_action_frontend_init_page();
}
}
/**
* Render insertion point.
*
* @since 4.2.0
*
* @return string
*/
public function get_headline() {
$options = $this->get_options();
if ( $options['show_headline'] ) {
$headline = sprintf(
/** This filter is already documented in modules/sharedaddy/sharing-service.php */
apply_filters( 'jetpack_sharing_headline_html', '
%s
', esc_html( $options['headline'] ), 'related-posts' ),
esc_html( $options['headline'] )
);
} else {
$headline = '';
}
return $headline;
}
/**
* Adds a target to the post content to load related posts into if a shortcode for it did not already exist.
* Will skip adding the target if the post content contains a Related Posts block or if the 'get_the_excerpt'
* hook is in the current filter list.
*
* @filter the_content
*
* @param string $content Post content.
*
* @returns string
*/
public function filter_add_target_to_dom( $content ) {
if ( has_block( 'jetpack/related-posts' ) ) {
return $content;
}
if ( ! $this->_found_shortcode && ! doing_filter( 'get_the_excerpt' ) ) {
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
$content .= "\n" . $this->get_server_rendered_html();
} else {
$content .= "\n" . $this->get_client_rendered_html();
}
}
return $content;
}
/**
* Render static markup based on the Gutenberg block code
*
* @return string Rendered related posts HTML.
*/
public function get_server_rendered_html() {
$rp_settings = Jetpack_Options::get_option( 'relatedposts', array() );
$block_rp_settings = array(
'displayThumbnails' => $rp_settings['show_thumbnails'],
'showHeadline' => $rp_settings['show_headline'],
'displayDate' => isset( $rp_settings['show_date'] ) ? (bool) $rp_settings['show_date'] : true,
'displayContext' => isset( $rp_settings['show_context'] ) && $rp_settings['show_context'],
'postLayout' => isset( $rp_settings['layout'] ) ? $rp_settings['layout'] : 'grid',
'postsToShow' => isset( $rp_settings['size'] ) ? $rp_settings['size'] : 3,
/** This filter is already documented in modules/related-posts/jetpack-related-posts.php */
'headline' => apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() ),
);
return $this->render_block( $block_rp_settings );
}
/**
* Looks for our shortcode on the unfiltered content, this has to execute early.
*
* @filter the_content
* @param string $content
* @uses has_shortcode
* @returns string
*/
public function test_for_shortcode( $content ) {
$this->_found_shortcode = has_shortcode( $content, self::SHORTCODE );
return $content;
}
/**
* Returns the HTML for the related posts section.
*
* @uses esc_html__, apply_filters
* @returns string
*/
public function get_client_rendered_html() {
if ( Settings::is_syncing() ) {
return '';
}
/**
* Filter the Related Posts headline.
*
* @module related-posts
*
* @since 3.0.0
*
* @param string $headline Related Posts heading.
*/
$headline = apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() );
if ( $this->_previous_post_id ) {
$exclude = "data-exclude='{$this->_previous_post_id}'";
} else {
$exclude = "";
}
return <<
$headline
EOT;
}
/**
* 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.
*
* @returns string
*/
public function get_client_rendered_html_unsupported() {
if ( Settings::is_syncing() ) {
return '';
}
return "\n\n\n\n";
}
/**
* ===============
* GUTENBERG BLOCK
* ===============
*/
/**
* Echoes out items for the Gutenberg block
*
* @param array $related_post The post oject.
* @param array $block_attributes The block attributes.
*/
public function render_block_item( $related_post, $block_attributes ) {
$instance_id = 'related-posts-item-' . uniqid();
$label_id = $instance_id . '-label';
$item_markup = sprintf(
'