|
| 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