Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter for WordPress Posts widget content #5883

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions modules/widgets/wordpress-post-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,26 +734,28 @@ public function widget( $args, $instance ) {
// Enqueue front end assets.
$this->enqueue_scripts();

echo $args['before_widget'];
$content = $args['before_widget'];

if ( empty( $instance['url'] ) ) {
if ( current_user_can( 'manage_options' ) ) {
echo '<p>';
$content .= '<p>';
/* Translators: the "Blog URL" field mentioned is the input field labeled as such in the widget form. */
esc_html_e( 'The Blog URL is not properly setup in the widget.', 'jetpack' );
echo '</p>';
$content .= esc_html__( 'The Blog URL is not properly setup in the widget.', 'jetpack' );
$content .= '</p>';
}
echo $args['after_widget'];
$content .= $args['after_widget'];

echo $content;
return;
}

$data = $this->get_blog_data( $instance['url'] );

// check for errors
if ( is_wp_error( $data ) || empty( $data['site_info']['data'] ) ) {
echo '<p>' . __( 'Cannot load blog information at this time.', 'jetpack' ) . '</p>';
echo $args['after_widget'];
$content .= '<p>' . __( 'Cannot load blog information at this time.', 'jetpack' ) . '</p>';
$content .= $args['after_widget'];

echo $content;
return;
}

Expand All @@ -762,19 +764,20 @@ public function widget( $args, $instance ) {
if ( ! empty( $instance['title'] ) ) {
/** This filter is documented in core/src/wp-includes/default-widgets.php */
$instance['title'] = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_title'] . esc_html( $instance['title'] . ': ' . $site_info->name ) . $args['after_title'];
$content .= $args['before_title'] . esc_html( $instance['title'] . ': ' . $site_info->name ) . $args['after_title'];
}
else {
echo $args['before_title'] . esc_html( $site_info->name ) . $args['after_title'];
$content .= $args['before_title'] . esc_html( $site_info->name ) . $args['after_title'];
}

echo '<div class="jetpack-display-remote-posts">';
$content .= '<div class="jetpack-display-remote-posts">';

if ( is_wp_error( $data['posts']['data'] ) || empty( $data['posts']['data'] ) ) {
echo '<p>' . __( 'Cannot load blog posts at this time.', 'jetpack' ) . '</p>';
echo '</div><!-- .jetpack-display-remote-posts -->';
echo $args['after_widget'];
$content .= '<p>' . __( 'Cannot load blog posts at this time.', 'jetpack' ) . '</p>';
$content .= '</div><!-- .jetpack-display-remote-posts -->';
$content .= $args['after_widget'];

echo $content;
return;
}

Expand All @@ -794,7 +797,7 @@ public function widget( $args, $instance ) {
if ( isset( $instance['open_in_new_window'] ) && $instance['open_in_new_window'] == true ) {
$target = ' target="_blank"';
}
echo '<h4><a href="' . esc_url( $single_post['url'] ) . '"' . $target . '>' . esc_html( $post_title ) . '</a></h4>' . "\n";
$content .= '<h4><a href="' . esc_url( $single_post['url'] ) . '"' . $target . '>' . esc_html( $post_title ) . '</a></h4>' . "\n";
if ( ( $instance['featured_image'] == true ) && ( ! empty ( $single_post['featured_image'] ) ) ) {
$featured_image = $single_post['featured_image'];
/**
Expand All @@ -809,16 +812,27 @@ public function widget( $args, $instance ) {
* @param array $args Array of Photon Parameters.
*/
$image_params = apply_filters( 'jetpack_display_posts_widget_image_params', array() );
echo '<a title="' . esc_attr( $post_title ) . '" href="' . esc_url( $single_post['url'] ) . '"' . $target . '><img src="' . jetpack_photon_url( $featured_image, $image_params ) . '" alt="' . esc_attr( $post_title ) . '"/></a>';
$content .= '<a title="' . esc_attr( $post_title ) . '" href="' . esc_url( $single_post['url'] ) . '"' . $target . '><img src="' . jetpack_photon_url( $featured_image, $image_params ) . '" alt="' . esc_attr( $post_title ) . '"/></a>';
}

if ( $instance['show_excerpts'] == true ) {
echo $single_post['excerpt'];
$content .= $single_post['excerpt'];
}
}

echo '</div><!-- .jetpack-display-remote-posts -->';
echo $args['after_widget'];
$content .= '</div><!-- .jetpack-display-remote-posts -->';
$content .= $args['after_widget'];

/**
* Filter the WordPress Posts widget content.
*
* @module widgets
*
* @since 4.6.0
*
* @param string $content Widget content.
*/
echo apply_filters( 'jetpack_display_posts_widget_content', $content );
}

/**
Expand Down