Skip to content

Commit 9af8734

Browse files
committed
add widget to get remote news from CC.org
1 parent bc5c097 commit 9af8734

3 files changed

Lines changed: 153 additions & 11 deletions

File tree

inc/class-components.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -348,33 +348,43 @@ public static function notification( $type = 'warning', $url, $title, $content,
348348

349349
return $out;
350350
}
351-
/**
352-
* Notification
351+
/**
352+
* Simple entry
353353
*
354354
* @param int $post_id : post or entry ID.
355355
* @param boolean $has_content : whether to show or not the content excerpt.
356356
* @param boolean $has_image : whether to show or not the entry thumbnail.
357+
* @param boolean $use_remote_data : whether to use or not remote data providing each element separatedly
358+
* @param string $title : the title of the entry
359+
* @param string $image_url : the url of the entry featured image
360+
* @param string $date : the date of the entry
361+
* @param string $permalink : the permalink of the entry
362+
* @param string $excerpt : the excerpt of the entry
357363
* @return string component layout
358364
*/
359-
public static function simple_entry( $post_id, $has_content = true, $has_image = true ) {
360-
$has_thumb = has_post_thumbnail( $post_id );
361-
$has_thumb_class = ( $has_thumb ) ? ' has-image' : '';
365+
public static function simple_entry( $post_id, $has_content = true, $has_image = true, $use_remote_data = null, $title = null, $image_url = null, $date = null, $permalink = null, $excerpt= null ) {
366+
$has_thumb = ( !$use_remote_data ) ? has_post_thumbnail( $post_id ) : !empty( $image_url );
367+
$has_thumb_class = ( !empty( $has_thumb ) ) ? ' has-image' : '';
368+
$external = ( $use_remote_data ) ? ' target="_blank" ' : '';
362369
$out = '<article class="entry-simple-post' . $has_thumb_class . '">';
363370
$out .= '<div class="columns is-gapless">';
364371
if ( $has_thumb && $has_image ) {
372+
$thumb_image = ( !$use_remote_data ) ? get_the_post_thumbnail( $post_id, 'landscape-small' ) : '<img src="'.$image_url.'" alt="'.$title.'" />';
365373
$out .= '<figure class="entry-image column is-4">';
366-
$out .= get_the_post_thumbnail( $post_id, 'landscape-small' );
374+
$out .= $thumb_image;
367375
$out .= '</figure>';
368376
}
377+
$the_title = ( !$use_remote_data ) ? get_the_title( $post_id ) : $title;
378+
$the_permalink = ( !$use_remote_data ) ? get_permalink( $post_id ) : $permalink;
379+
$the_date = ( !$use_remote_data ) ? get_the_date( 'd F Y' ) : mysql2date( 'd F Y', $date );
369380
$out .= '<div class="entry-content column">';
370-
$out .= '<h4 class="b-header"><a href="' . get_permalink( $post_id ) . '">' . get_the_title( $post_id ) . '</a></h4>';
371-
$out .= '<span class="entry-date">' . get_the_date( CC_Site::get_date_format() ) . '</span>';
381+
$out .= '<h4 class="b-header"><a href="' . $the_permalink . '"'.$external.'>' . $the_title . '</a></h4>';
382+
$out .= '<span class="entry-date">' . $the_date . '</span>';
372383
if ( $has_content ) {
373384
$the_post = get_post( $post_id );
374-
$the_content = do_excerpt( $the_post );
375-
$filtered_content = apply_filters( 'cc_theme_base_filter_card_link_content', $the_content, $post_id );
385+
$the_excerpt = ( !$use_remote_data ) ? do_excerpt( $the_post ) : $excerpt;
376386
$out .= '<div class="entry-description">';
377-
$out .= $filtered_content;
387+
$out .= $the_excerpt;
378388
$out .= '</div>';
379389
}
380390
$out .= '</div>';

inc/widgets.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
require TEMPLATEPATH . '/inc/widgets/cc-card.php';
1212
require TEMPLATEPATH . '/inc/widgets/cc-title.php';
1313
require TEMPLATEPATH . '/inc/widgets/cc-donate.php';
14+
require TEMPLATEPATH . '/inc/widgets/cc-org-news.php';

inc/widgets/cc-org-news.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
class WP_Widget_org_news extends WP_Widget {
4+
const CATEGORIES_URL = 'https://creativecommons.org/wp-json/wp/v2/categories';
5+
const ENTRIES_URL = 'https://creativecommons.org/wp-json/wp/v2/posts';
6+
const MEDIA_URL = 'https://creativecommons.org/wp-json/wp/v2/media';
7+
const TRANSIENT_PREFIX = 'cc_widget_org_news_';
8+
const CC_ORG_BLOG_URL = 'https://creativecommons.org/blog';
9+
10+
public $per_page_categories = 50;
11+
12+
/** constructor */
13+
function __construct() {
14+
$widget_ops = array(
15+
'classname' => 'widget-org-news',
16+
'description' => 'Show the last categorized news from the main CC website',
17+
);
18+
$control_ops = array();
19+
parent::__construct( 'widget-org-news', 'CC Org Last news', $widget_ops, $control_ops );
20+
}
21+
public function query_api( $url ) {
22+
$response = wp_remote_get( $url );
23+
$http_code = wp_remote_retrieve_response_code( $response );
24+
if ( $http_code == 200 ) {
25+
$api_response = json_decode(wp_remote_retrieve_body( $response ));
26+
return $api_response;
27+
} else {
28+
return false;
29+
}
30+
}
31+
function get_last_news( $size, $category ) {
32+
if ( false === ( $get_entries = get_transient( self::TRANSIENT_PREFIX. $this->id . '_entries' ) ) ) {
33+
$entries_url = self::ENTRIES_URL.'?per_page='.$size;
34+
if ( !empty( $category ) ) {
35+
$entries_url .= '&categories='.$category;
36+
}
37+
38+
$get_entries = $this->query_api( $entries_url );
39+
if ( !empty( $get_entries ) ) {
40+
$modified_entries = array();
41+
foreach ( $get_entries as $entry ) {
42+
if ( !empty( $entry->featured_media ) ) {
43+
$api_response = $this->query_api( self::MEDIA_URL.'/'. $entry->featured_media );
44+
if ( !empty( $api_response ) ) {
45+
$entry->featured_media_url = $api_response->media_details->sizes->cc_list_post_thumbnail->source_url;
46+
$entry->featured_media_url_full = $api_response->media_details->sizes->full->source_url;
47+
}
48+
}
49+
$modified_entries[] = $entry;
50+
}
51+
$get_entries = $modified_entries;
52+
set_transient( self::TRANSIENT_PREFIX. $this->id . '_entries', $get_entries, HOUR_IN_SECONDS );
53+
}
54+
}
55+
return $get_entries;
56+
}
57+
function get_ccorg_categories() {
58+
if ( false === ( $get_categories = get_transient( self::TRANSIENT_PREFIX. $this->id . '_categories' ) ) ) {
59+
$api_response = $this->query_api( self::CATEGORIES_URL.'?per_page='.$this->per_page_categories );
60+
foreach ( $api_response as $category ) {
61+
$get_categories[$category->id] = array(
62+
'name' => $category->name,
63+
'link' => $category->link
64+
);
65+
}
66+
set_transient( self::TRANSIENT_PREFIX . 'categories', $get_categories, HOUR_IN_SECONDS );
67+
}
68+
return $get_categories;
69+
}
70+
71+
function widget( $args, $instance ) {
72+
global $post;
73+
$size = ( ! empty( $instance['size'] ) ) ? $instance['size'] : 3;
74+
$the_category = ( ! empty( $instance['category'] ) ) ? $instance['category'] : null;
75+
$link_text = ( ! empty( $instance['link_text'] ) ) ? $instance['link_text'] : 'More news';
76+
$news = $this->get_last_news( $size, $the_category );
77+
$categories = $this->get_ccorg_categories();
78+
if ( ! empty( $news ) ) {
79+
echo '<div class="widget news">';
80+
echo '<header class="widget-header">';
81+
if ( $instance['show_title'] ) {
82+
echo '<h2 class="widget-title">' . esc_attr( $instance['title'] ) . '</h2>';
83+
}
84+
if ( ! empty( $instance['is_link'] && ( ! empty( $instance['category'] ) ) ) ) {
85+
$link = !empty( $instance['category'] ) ? $categories[$instance['category']] : self::CC_ORG_BLOG_URL;
86+
echo '<div class="more-news">';
87+
echo '<a href="' . $link['link'] . '" class="widget-more" target="_blank">' . $link_text . '<i class="icon chevron-right"></i></a>';
88+
echo '</div>';
89+
}
90+
echo '</header>';
91+
echo '<div class="widget-content">';
92+
foreach ( $news as $item ) {
93+
$thumb_url = ( !empty( $item->featured_media ) ) ? $item->featured_media_url : '';
94+
echo Components::simple_entry( $item->ID, false, true, true, $item->title->rendered, $thumb_url, $item->date, $item->link, $item->excerpt->rendered );
95+
}
96+
echo '</div>';
97+
echo '</div>';
98+
}
99+
}
100+
101+
function update( $new_instance, $old_instance ) {
102+
delete_transient( self::TRANSIENT_PREFIX. $this->id . '_categories' );
103+
delete_transient( self::TRANSIENT_PREFIX. $this->id . '_entries' );
104+
delete_transient( self::TRANSIENT_PREFIX . '_thumbnails' );
105+
return $new_instance;
106+
}
107+
108+
function form( $instance ) {
109+
extract( $instance );
110+
echo '<p><label for="' . $this->get_field_id( 'title' ) . '">Title: <input type="text" name="' . $this->get_field_name( 'title' ) . '" id="' . $this->get_field_id( 'title' ) . '" value="' . $instance['title'] . '" class="widefat" /></label></p>';
111+
echo '<p><label for="' . $this->get_field_name( 'show_title' ) . '">Show title? </label><input type="checkbox" id="' . $this->get_field_id( 'show_title' ) . '"' . ( ( ! empty( $show_title ) ) ? ' checked="checked" ' : '' ) . ' name="' . $this->get_field_name( 'show_title' ) . '" value="1"></p>';
112+
echo '<p><label for="' . $this->get_field_name( 'is_link' ) . '">Link to news archive? </label><input type="checkbox" id="' . $this->get_field_id( 'is_link' ) . '"' . ( ( ! empty( $is_link ) ) ? ' checked="checked" ' : '' ) . ' name="' . $this->get_field_name( 'is_link' ) . '" value="1"></p>';
113+
echo '<p><label for="' . $this->get_field_id( 'link_text' ) . '">Link text: <input type="text" name="' . $this->get_field_name( 'link_text' ) . '" id="' . $this->get_field_id( 'link_text' ) . '" value="' . $instance['link_text'] . '" class="widefat"/></label></p>';
114+
echo '<p><label for="' . $this->get_field_id( 'size' ) . '">Entries number: <input type="number" name="' . $this->get_field_name( 'size' ) . '" id="' . $this->get_field_id( 'size' ) . '" value="' . $instance['size'] . '"/></label></p>';
115+
echo '<p><label for="' . $this->get_field_id( 'category' ) . '">Category: ';
116+
$get_categories = $this->get_ccorg_categories();
117+
echo '<p><select name="'.$this->get_field_name( 'category' ).'" id="'.$this->get_field_id( 'category' ).'">';
118+
foreach ( $get_categories as $key => $category ) {
119+
$selected = ( $key == $instance['category'] ) ? 'selected = "selected" ' : '';
120+
echo '<option value="'.$key.'" '.$selected.'>'.$category['name'].'</option>';
121+
}
122+
echo '</select>';
123+
echo '</label></p>';
124+
}
125+
}
126+
127+
function cc_text_news_org_widget_init() {
128+
register_widget( 'WP_Widget_org_news' );
129+
}
130+
131+
add_action( 'widgets_init', 'cc_text_news_org_widget_init' );

0 commit comments

Comments
 (0)