Skip to content

Commit 8584359

Browse files
committed
add tag support for widget
1 parent 2de00cf commit 8584359

2 files changed

Lines changed: 93 additions & 70 deletions

File tree

functions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ public function enqueue_styles() {
201201
function admin_enqueue_scripts() {
202202
// admin scripts
203203
global $pagenow;
204-
if ( is_admin() && ( $pagenow == 'widgets.php' ) ) {
204+
$current_screen = get_current_screen();
205+
if ( is_admin() && ( $pagenow == 'widgets.php' ) || $current_screen->id == 'dashboard_page_cc-main-site-settings' ) {
205206
wp_enqueue_media();
206207
wp_enqueue_script( 'script-admin', THEME_JS . '/admin_scripts.js', array( 'jquery' ), self::theme_ver );
207208
}

inc/widgets/cc-org-news.php

Lines changed: 91 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +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 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';
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';
910

10-
public $per_page_categories = 50;
11+
public $per_page_categories = 50;
1112

1213
/** constructor */
1314
function __construct() {
@@ -18,90 +19,107 @@ function __construct() {
1819
$control_ops = array();
1920
parent::__construct( 'widget-org-news', 'CC Org Last news', $widget_ops, $control_ops );
2021
}
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-
}
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+
}
32+
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+
}
3741

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;
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;
5660
}
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-
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+
}
7180
function widget( $args, $instance ) {
7281
global $post;
7382
$size = ( ! empty( $instance['size'] ) ) ? $instance['size'] : 3;
7483
$the_category = ( ! empty( $instance['category'] ) ) ? $instance['category'] : null;
7584
$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();
85+
$tag = ( ! empty( $instance['tag_id'] ) ) ? $instance['tag_id'] : false;
86+
$news = $this->get_last_news( $size, $the_category, $tag );
87+
$categories = $this->get_ccorg_categories();
7888
if ( ! empty( $news ) ) {
7989
echo '<div class="widget news">';
8090
echo '<header class="widget-header">';
8191
if ( $instance['show_title'] ) {
8292
echo '<h2 class="widget-title">' . esc_attr( $instance['title'] ) . '</h2>';
8393
}
8494
if ( ! empty( $instance['is_link'] && ( ! empty( $instance['category'] ) ) ) ) {
85-
$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;
8696
echo '<div class="more-news">';
8797
echo '<a href="' . $link['link'] . '" class="widget-more" target="_blank">' . $link_text . '<i class="icon chevron-right"></i></a>';
8898
echo '</div>';
8999
}
90100
echo '</header>';
91101
echo '<div class="widget-content">';
92102
foreach ( $news as $item ) {
93-
$thumb_url = ( ! empty( $item->featured_media ) ) ? $item->featured_media_url : '';
103+
$thumb_url = ( !empty( $item->featured_media ) ) ? $item->featured_media_url : '';
94104
echo Components::simple_entry( $item->ID, false, true, true, $item->title->rendered, $thumb_url, $item->date, $item->link, $item->excerpt->rendered );
95105
}
96106
echo '</div>';
97107
echo '</div>';
98108
}
99109
}
100-
110+
101111
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' );
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' );
105123
return $new_instance;
106124
}
107125

@@ -112,14 +130,18 @@ function form( $instance ) {
112130
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>';
113131
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>';
114132
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'] . '"/>';
115137
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>';
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>';
123145
echo '</label></p>';
124146
}
125147
}

0 commit comments

Comments
 (0)