| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Display Recent WordPress Posts Widget |
| 4 |
* Description: Displays recent posts from a WordPress.com or Jetpack-enabled self-hosted WordPress site. |
| 5 |
* Version: 1.0 |
| 6 |
* Author: Brad Angelcyk, Kathryn Presner, Justin Shreve, Carolyn Sonnek |
| 7 |
* Author URI: http://automattic.com |
| 8 |
* License: GPL2 |
| 9 |
*/ |
| 10 |
add_action( 'widgets_init', 'jetpack_display_posts_widget' ); |
| 11 |
function jetpack_display_posts_widget() { |
| 12 |
register_widget( 'Jetpack_Display_Posts_Widget' ); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Displays a list of recent posts from a WordPress.com or Jetpack-enabled blog. |
| 17 |
*/ |
| 18 |
class Jetpack_Display_Posts_Widget extends WP_Widget { |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
parent::__construct( |
| 22 |
'jetpack_display_posts_widget', // internal id |
| 23 |
__( 'Display WordPress Posts (Jetpack)', 'jetpack' ), // wp-admin title |
| 24 |
array( |
| 25 |
'description' => __( 'Displays a list of recent posts from another WordPress.com or Jetpack-enabled blog.', 'jetpack' ), // description |
| 26 |
) |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
/** Set up the widget display on the front end |
| 31 |
*/ |
| 32 |
public function widget( $args, $instance ) { |
| 33 |
$title = apply_filters( 'widget_title', $instance['title'] ); |
| 34 |
|
| 35 |
wp_enqueue_style( 'jetpack_display_posts_widget', plugins_url( 'wordpress-post-widget/style.css' ) ); |
| 36 |
|
| 37 |
$site = $instance['url']; |
| 38 |
$site = urlencode( $site ); |
| 39 |
$api_url = "https://public-api.wordpress.com/rest/v1/sites/" . $site; |
| 40 |
|
| 41 |
$data_from_cache = get_transient( 'wp-site-info-' . $instance['url'], 'display-posts-widget' ); |
| 42 |
if ( false === $data_from_cache ) { |
| 43 |
$response = wp_remote_get( $api_url ); |
| 44 |
set_transient( 'wp-site-info-' . $instance['url'], $response, 'display-posts-widget', ( 10 * MINUTE_IN_SECONDS ) ); |
| 45 |
} else { |
| 46 |
$response = $data_from_cache; |
| 47 |
} |
| 48 |
|
| 49 |
$site_info = json_decode( $response ['body'] ); |
| 50 |
|
| 51 |
if ( empty( $site_info->ID ) ) { |
| 52 |
echo "<p>" . __( 'We cannot load blog data at this time.', 'jetpack' ) . "</p>"; |
| 53 |
echo $args['after_widget']; |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
echo $args['before_widget']; |
| 58 |
if ( ! empty( $title ) ) { |
| 59 |
echo $args['before_title'] . esc_html( $title . ": " . $site_info->name ) . $args['after_title']; |
| 60 |
} |
| 61 |
else { |
| 62 |
echo $args['before_title'] . esc_html( $site_info->name ) . $args['after_title']; |
| 63 |
} |
| 64 |
|
| 65 |
$number_of_posts = $instance['number_of_posts']; |
| 66 |
|
| 67 |
$data_from_cache = get_transient( 'wp-post-info-' . $instance['url'], 'display-posts-widget' ); |
| 68 |
if ( false === $data_from_cache ) { |
| 69 |
$response = wp_remote_get( $api_url . '/posts' ); |
| 70 |
set_transient( 'wp-post-info-' . $instance['url'], $response, 'display-posts-widget', ( 10 * MINUTE_IN_SECONDS ) ); |
| 71 |
} else { |
| 72 |
$response = $data_from_cache; |
| 73 |
} |
| 74 |
|
| 75 |
if ( is_a( $response, 'WP_Error' ) ) { |
| 76 |
echo "<p>" . __( 'We cannot load blog data at this time.', 'jetpack' ) . "</p>"; |
| 77 |
echo $args['after_widget']; |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$posts_info = json_decode( $response['body'] ); |
| 82 |
|
| 83 |
echo "<div class='jetpack-display-remote-posts'>"; |
| 84 |
|
| 85 |
if ( isset( $posts_info->error ) && 'jetpack_error' == $posts_info->error ) { |
| 86 |
echo '<p>' . __( 'We cannot display posts for this blog.', 'jetpack' ) . '</p>'; |
| 87 |
echo $args['after_widget']; |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
for ( $i = 0; $i < $number_of_posts; $i++ ) { |
| 92 |
$single_post = $posts_info->posts[$i]; |
| 93 |
$post_title = ( $single_post->title ) ? esc_html( $single_post->title ) : '( No Title )'; |
| 94 |
|
| 95 |
echo "<h4><a href='" . esc_url( $single_post->URL ) . "'>". $post_title . "</a></h4>" . "\n"; |
| 96 |
if ( ( $instance['featured_image'] == true ) && ( ! empty ( $single_post->featured_image) ) ) { |
| 97 |
$featured_image = ( $single_post->featured_image ) ? $single_post->featured_image : ''; |
| 98 |
echo "<img src='" . $featured_image . "'>"; |
| 99 |
} |
| 100 |
|
| 101 |
if ( $instance['show_excerpts'] == true ) { |
| 102 |
$post_excerpt = ( $single_post->excerpt ) ? $single_post->excerpt : ''; |
| 103 |
echo $post_excerpt; |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
echo "</div>"; |
| 109 |
} |
| 110 |
|
| 111 |
public function form( $instance ) { |
| 112 |
if ( isset( $instance[ 'title' ] ) ) { |
| 113 |
$title = $instance[ 'title' ]; |
| 114 |
} else { |
| 115 |
$title = __( 'Recent Posts', 'jetpack' ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( isset( $instance[ 'url' ] ) ) { |
| 119 |
$url = $instance[ 'url' ]; |
| 120 |
} else { |
| 121 |
$url = ""; |
| 122 |
} |
| 123 |
|
| 124 |
if ( isset( $instance[ 'number_of_posts' ] ) ) { |
| 125 |
$number_of_posts = $instance[ 'number_of_posts' ]; |
| 126 |
} else { |
| 127 |
$number_of_posts = 5; |
| 128 |
} |
| 129 |
|
| 130 |
if ( isset( $instance[ 'featured_image'] ) ) { |
| 131 |
$featured_image = $instance[ 'featured_image']; |
| 132 |
} else { |
| 133 |
$featured_image = false; |
| 134 |
} |
| 135 |
|
| 136 |
if ( isset( $instance[ 'show_excerpts'] ) ) { |
| 137 |
$show_excerpts = $instance[ 'show_excerpts']; |
| 138 |
} else { |
| 139 |
$show_excerpts = false; |
| 140 |
} |
| 141 |
|
| 142 |
?> |
| 143 |
<p> |
| 144 |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'jetpack' ); ?></label> |
| 145 |
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
| 146 |
</p> |
| 147 |
|
| 148 |
<p> |
| 149 |
<label for="<?php echo $this->get_field_id( 'url' ); ?>"><?php _e( 'Blog URL:', 'jetpack' ); ?></label> |
| 150 |
<input class="widefat" id="<?php echo $this->get_field_id( 'url' ); ?>" name="<?php echo $this->get_field_name( 'url' ); ?>" type="text" value="<?php echo esc_attr( $url ); ?>" /> |
| 151 |
<p> |
| 152 |
<?php _e( "Enter a WordPress.com or Jetpack WordPress site URL.", 'jetpack' ); ?> |
| 153 |
</p> |
| 154 |
</p> |
| 155 |
<p> |
| 156 |
<label for="<?php echo $this->get_field_id( 'number_of_posts' ); ?>"><?php _e( 'Number of Posts to Display:', 'jetpack' ); ?></label> |
| 157 |
<select name="<?php echo $this->get_field_name( 'number_of_posts' ); ?>"> |
| 158 |
<?php |
| 159 |
for ($i = 1; $i <= 10; $i++) { |
| 160 |
echo '<option value="' . $i . '" '.selected( $number_of_posts, $i ).'>' . $i . '</option>'; |
| 161 |
} |
| 162 |
?> |
| 163 |
</select> |
| 164 |
</p> |
| 165 |
<p> |
| 166 |
<label for="<?php echo $this->get_field_id( 'featured_image' ); ?>"><?php _e( 'Show Featured Image:', 'jetpack' ); ?></label> |
| 167 |
<input type="checkbox" name="<?php echo $this->get_field_name( 'featured_image' ); ?>" <?php checked( $featured_image, 1 ); ?> /> |
| 168 |
</p> |
| 169 |
<p> |
| 170 |
<label for="<?php echo $this->get_field_id( 'show_excerpts' ); ?>"><?php _e( 'Show Excerpts:', 'jetpack' ); ?></label> |
| 171 |
<input type="checkbox" name="<?php echo $this->get_field_name( 'show_excerpts' ); ?>" <?php checked( $show_excerpts, 1 ); ?> /> |
| 172 |
</p> |
| 173 |
|
| 174 |
<?php |
| 175 |
} |
| 176 |
|
| 177 |
public function update( $new_instance, $old_instance ) { |
| 178 |
$instance = array(); |
| 179 |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
| 180 |
$instance['url'] = ( ! empty( $new_instance['url'] ) ) ? strip_tags( $new_instance['url'] ) : ''; |
| 181 |
$instance['url'] = str_replace( "http://", "", $instance['url'] ); |
| 182 |
$instance['url'] = untrailingslashit( $instance['url'] ); |
| 183 |
$instance['number_of_posts'] = ( ! empty( $new_instance['number_of_posts'] ) ) ? intval( $new_instance['number_of_posts'] ) : ''; |
| 184 |
$instance['featured_image'] = ( ! empty( $new_instance['featured_image'] ) ) ? true : ''; |
| 185 |
$instance['show_excerpts'] = ( ! empty( $new_instance['show_excerpts'] ) ) ? true : ''; |
| 186 |
return $instance; |
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
?> |