Skip to content

Commit 6f6b0fa

Browse files
committed
fix file format
1 parent 8584359 commit 6f6b0fa

File tree

1 file changed

+89
-89
lines changed

1 file changed

+89
-89
lines changed

inc/widgets/cc-org-news.php

Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

33
class WP_Widget_org_news extends WP_Widget {
4-
const CATEGORIES_URL = 'https://creativecommons.org/wp-json/wp/v2/categories';
5-
const TAGS_URL = 'https://creativecommons.org/wp-json/wp/v2/tags';
6-
const ENTRIES_URL = 'https://creativecommons.org/wp-json/wp/v2/posts';
7-
const MEDIA_URL = 'https://creativecommons.org/wp-json/wp/v2/media';
8-
const TRANSIENT_PREFIX = 'cc_widget_org_news_';
9-
const CC_ORG_BLOG_URL = 'https://creativecommons.org/blog';
4+
const CATEGORIES_URL = 'https://creativecommons.org/wp-json/wp/v2/categories';
5+
const TAGS_URL = 'https://creativecommons.org/wp-json/wp/v2/tags';
6+
const ENTRIES_URL = 'https://creativecommons.org/wp-json/wp/v2/posts';
7+
const MEDIA_URL = 'https://creativecommons.org/wp-json/wp/v2/media';
8+
const TRANSIENT_PREFIX = 'cc_widget_org_news_';
9+
const CC_ORG_BLOG_URL = 'https://creativecommons.org/blog';
1010

11-
public $per_page_categories = 50;
11+
public $per_page_categories = 50;
1212

1313
/** constructor */
1414
function __construct() {
@@ -19,107 +19,107 @@ function __construct() {
1919
$control_ops = array();
2020
parent::__construct( 'widget-org-news', 'CC Org Last news', $widget_ops, $control_ops );
2121
}
22-
public function query_api( $url ) {
23-
$response = wp_remote_get( $url );
24-
$http_code = wp_remote_retrieve_response_code( $response );
25-
if ( $http_code == 200 ) {
26-
$api_response = json_decode(wp_remote_retrieve_body( $response ));
27-
return $api_response;
28-
} else {
29-
return false;
30-
}
31-
}
22+
public function query_api( $url ) {
23+
$response = wp_remote_get( $url );
24+
$http_code = wp_remote_retrieve_response_code( $response );
25+
if ( $http_code == 200 ) {
26+
$api_response = json_decode( wp_remote_retrieve_body( $response ) );
27+
return $api_response;
28+
} else {
29+
return false;
30+
}
31+
}
3232
function get_last_news( $size, $category, $tag ) {
33-
if ( false === ( $get_entries = get_transient( self::TRANSIENT_PREFIX. $this->id . '_entries' ) ) ) {
34-
$entries_url = self::ENTRIES_URL.'?per_page='.$size;
35-
if ( !empty( $category ) ) {
36-
$entries_url .= '&categories='.$category;
37-
}
38-
if ( !empty( $tag ) ) {
39-
$entries_url .= '&tags='.$tag;
40-
}
33+
if ( false === ( $get_entries = get_transient( self::TRANSIENT_PREFIX . $this->id . '_entries' ) ) ) {
34+
$entries_url = self::ENTRIES_URL . '?per_page=' . $size;
35+
if ( ! empty( $category ) ) {
36+
$entries_url .= '&categories=' . $category;
37+
}
38+
if ( ! empty( $tag ) ) {
39+
$entries_url .= '&tags=' . $tag;
40+
}
4141

42-
$get_entries = $this->query_api( $entries_url );
43-
if ( !empty( $get_entries ) ) {
44-
$modified_entries = array();
45-
foreach ( $get_entries as $entry ) {
46-
if ( !empty( $entry->featured_media ) ) {
47-
$api_response = $this->query_api( self::MEDIA_URL.'/'. $entry->featured_media );
48-
if ( !empty( $api_response ) ) {
49-
$entry->featured_media_url = $api_response->media_details->sizes->cc_list_post_thumbnail->source_url;
50-
$entry->featured_media_url_full = $api_response->media_details->sizes->full->source_url;
51-
}
52-
}
53-
$modified_entries[] = $entry;
54-
}
55-
$get_entries = $modified_entries;
56-
set_transient( self::TRANSIENT_PREFIX. $this->id . '_entries', $get_entries, HOUR_IN_SECONDS );
57-
}
58-
}
59-
return $get_entries;
42+
$get_entries = $this->query_api( $entries_url );
43+
if ( ! empty( $get_entries ) ) {
44+
$modified_entries = array();
45+
foreach ( $get_entries as $entry ) {
46+
if ( ! empty( $entry->featured_media ) ) {
47+
$api_response = $this->query_api( self::MEDIA_URL . '/' . $entry->featured_media );
48+
if ( ! empty( $api_response ) ) {
49+
$entry->featured_media_url = $api_response->media_details->sizes->cc_list_post_thumbnail->source_url;
50+
$entry->featured_media_url_full = $api_response->media_details->sizes->full->source_url;
51+
}
52+
}
53+
$modified_entries[] = $entry;
54+
}
55+
$get_entries = $modified_entries;
56+
set_transient( self::TRANSIENT_PREFIX . $this->id . '_entries', $get_entries, HOUR_IN_SECONDS );
57+
}
58+
}
59+
return $get_entries;
60+
}
61+
function get_ccorg_categories() {
62+
if ( false === ( $get_categories = get_transient( self::TRANSIENT_PREFIX . $this->id . '_categories' ) ) ) {
63+
$api_response = $this->query_api( self::CATEGORIES_URL . '?per_page=' . $this->per_page_categories );
64+
foreach ( $api_response as $category ) {
65+
$get_categories[ $category->id ] = array(
66+
'name' => $category->name,
67+
'link' => $category->link,
68+
);
69+
}
70+
set_transient( self::TRANSIENT_PREFIX . 'categories', $get_categories, HOUR_IN_SECONDS );
71+
}
72+
return $get_categories;
73+
}
74+
function get_tag_id( $tag_slug ) {
75+
$api_response = $this->query_api( self::TAGS_URL . '?slug=' . $tag_slug );
76+
if ( ! empty( $api_response ) ) {
77+
return $api_response[0]->id;
78+
}
6079
}
61-
function get_ccorg_categories() {
62-
if ( false === ( $get_categories = get_transient( self::TRANSIENT_PREFIX. $this->id . '_categories' ) ) ) {
63-
$api_response = $this->query_api( self::CATEGORIES_URL.'?per_page='.$this->per_page_categories );
64-
foreach ( $api_response as $category ) {
65-
$get_categories[$category->id] = array(
66-
'name' => $category->name,
67-
'link' => $category->link
68-
);
69-
}
70-
set_transient( self::TRANSIENT_PREFIX . 'categories', $get_categories, HOUR_IN_SECONDS );
71-
}
72-
return $get_categories;
73-
}
74-
function get_tag_id( $tag_slug ) {
75-
$api_response = $this->query_api( self::TAGS_URL.'?slug='.$tag_slug );
76-
if ( !empty( $api_response ) ) {
77-
return $api_response[0]->id;
78-
}
79-
}
8080
function widget( $args, $instance ) {
8181
global $post;
8282
$size = ( ! empty( $instance['size'] ) ) ? $instance['size'] : 3;
8383
$the_category = ( ! empty( $instance['category'] ) ) ? $instance['category'] : null;
8484
$link_text = ( ! empty( $instance['link_text'] ) ) ? $instance['link_text'] : 'More news';
85-
$tag = ( ! empty( $instance['tag_id'] ) ) ? $instance['tag_id'] : false;
85+
$tag = ( ! empty( $instance['tag_id'] ) ) ? $instance['tag_id'] : false;
8686
$news = $this->get_last_news( $size, $the_category, $tag );
87-
$categories = $this->get_ccorg_categories();
87+
$categories = $this->get_ccorg_categories();
8888
if ( ! empty( $news ) ) {
8989
echo '<div class="widget news">';
9090
echo '<header class="widget-header">';
9191
if ( $instance['show_title'] ) {
9292
echo '<h2 class="widget-title">' . esc_attr( $instance['title'] ) . '</h2>';
9393
}
9494
if ( ! empty( $instance['is_link'] && ( ! empty( $instance['category'] ) ) ) ) {
95-
$link = !empty( $instance['category'] ) ? $categories[$instance['category']] : self::CC_ORG_BLOG_URL;
95+
$link = ! empty( $instance['category'] ) ? $categories[ $instance['category'] ] : self::CC_ORG_BLOG_URL;
9696
echo '<div class="more-news">';
9797
echo '<a href="' . $link['link'] . '" class="widget-more" target="_blank">' . $link_text . '<i class="icon chevron-right"></i></a>';
9898
echo '</div>';
9999
}
100100
echo '</header>';
101101
echo '<div class="widget-content">';
102102
foreach ( $news as $item ) {
103-
$thumb_url = ( !empty( $item->featured_media ) ) ? $item->featured_media_url : '';
103+
$thumb_url = ( ! empty( $item->featured_media ) ) ? $item->featured_media_url : '';
104104
echo Components::simple_entry( $item->ID, false, true, true, $item->title->rendered, $thumb_url, $item->date, $item->link, $item->excerpt->rendered );
105105
}
106106
echo '</div>';
107107
echo '</div>';
108108
}
109109
}
110-
110+
111111
function update( $new_instance, $old_instance ) {
112-
if ( !empty( $new_instance['tag'] ) ) {
113-
$new_instance['tag_id'] = $this->get_tag_id( $new_instance['tag'] );
114-
if ( !empty( $new_instance['tag_id'] ) ) {
115-
$new_instance['tag_remote_exists'] = 1;
116-
} else {
117-
$new_instance['tag_remote_exists'] = 0;
118-
}
119-
}
120-
delete_transient( self::TRANSIENT_PREFIX. $this->id . '_categories' );
121-
delete_transient( self::TRANSIENT_PREFIX. $this->id . '_entries' );
122-
delete_transient( self::TRANSIENT_PREFIX . '_thumbnails' );
112+
if ( ! empty( $new_instance['tag'] ) ) {
113+
$new_instance['tag_id'] = $this->get_tag_id( $new_instance['tag'] );
114+
if ( ! empty( $new_instance['tag_id'] ) ) {
115+
$new_instance['tag_remote_exists'] = 1;
116+
} else {
117+
$new_instance['tag_remote_exists'] = 0;
118+
}
119+
}
120+
delete_transient( self::TRANSIENT_PREFIX . $this->id . '_categories' );
121+
delete_transient( self::TRANSIENT_PREFIX . $this->id . '_entries' );
122+
delete_transient( self::TRANSIENT_PREFIX . '_thumbnails' );
123123
return $new_instance;
124124
}
125125

@@ -130,18 +130,18 @@ function form( $instance ) {
130130
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>';
131131
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>';
132132
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>';
133-
$tag_error = ( !$instance['tag_remote_exists'] ) ? '<small style="color:red;">The tag doesn\'t seems to exists in the source website</small>' : '';
134-
echo '<p><label for="' . $this->get_field_id( 'tag' ) . '">Tag slug: <input type="text" name="' . $this->get_field_name( 'tag' ) . '" id="' . $this->get_field_id( 'tag' ) . '" value="' . $instance['tag'] . '"/></label>'.$tag_error.'</p>';
135-
echo '<input type="hidden" name="' . $this->get_field_name( 'tag_id' ) . ' value="' . $instance['tag_id'] . '"/>';
136-
echo '<input type="hidden" name="' . $this->get_field_name( 'tag_remote_exists' ) . ' value="' . $instance['tag_remote_exists'] . '"/>';
133+
$tag_error = ( ! $instance['tag_remote_exists'] ) ? '<small style="color:red;">The tag doesn\'t seems to exists in the source website</small>' : '';
134+
echo '<p><label for="' . $this->get_field_id( 'tag' ) . '">Tag slug: <input type="text" name="' . $this->get_field_name( 'tag' ) . '" id="' . $this->get_field_id( 'tag' ) . '" value="' . $instance['tag'] . '"/></label>' . $tag_error . '</p>';
135+
echo '<input type="hidden" name="' . $this->get_field_name( 'tag_id' ) . ' value="' . $instance['tag_id'] . '"/>';
136+
echo '<input type="hidden" name="' . $this->get_field_name( 'tag_remote_exists' ) . ' value="' . $instance['tag_remote_exists'] . '"/>';
137137
echo '<p><label for="' . $this->get_field_id( 'category' ) . '">Category: ';
138-
$get_categories = $this->get_ccorg_categories();
139-
echo '<p><select name="'.$this->get_field_name( 'category' ).'" id="'.$this->get_field_id( 'category' ).'">';
140-
foreach ( $get_categories as $key => $category ) {
141-
$selected = ( $key == $instance['category'] ) ? 'selected = "selected" ' : '';
142-
echo '<option value="'.$key.'" '.$selected.'>'.$category['name'].'</option>';
143-
}
144-
echo '</select>';
138+
$get_categories = $this->get_ccorg_categories();
139+
echo '<p><select name="' . $this->get_field_name( 'category' ) . '" id="' . $this->get_field_id( 'category' ) . '">';
140+
foreach ( $get_categories as $key => $category ) {
141+
$selected = ( $key == $instance['category'] ) ? 'selected = "selected" ' : '';
142+
echo '<option value="' . $key . '" ' . $selected . '>' . $category['name'] . '</option>';
143+
}
144+
echo '</select>';
145145
echo '</label></p>';
146146
}
147147
}

0 commit comments

Comments
 (0)