22
33class WP_Widget_org_news extends WP_Widget {
44 const CATEGORIES_URL = 'https://creativecommons.org/wp-json/wp/v2/categories ' ;
5+ const TAGS_URL = 'https://creativecommons.org/wp-json/wp/v2/tags ' ;
56 const ENTRIES_URL = 'https://creativecommons.org/wp-json/wp/v2/posts ' ;
67 const MEDIA_URL = 'https://creativecommons.org/wp-json/wp/v2/media ' ;
78 const TRANSIENT_PREFIX = 'cc_widget_org_news_ ' ;
@@ -28,12 +29,15 @@ public function query_api( $url ) {
2829 return false ;
2930 }
3031 }
31- function get_last_news ( $ size , $ category ) {
32+ function get_last_news ( $ size , $ category, $ tag ) {
3233 if ( false === ( $ get_entries = get_transient ( self ::TRANSIENT_PREFIX . $ this ->id . '_entries ' ) ) ) {
3334 $ entries_url = self ::ENTRIES_URL . '?per_page= ' . $ size ;
3435 if ( ! empty ( $ category ) ) {
3536 $ entries_url .= '&categories= ' . $ category ;
3637 }
38+ if ( ! empty ( $ tag ) ) {
39+ $ entries_url .= '&tags= ' . $ tag ;
40+ }
3741
3842 $ get_entries = $ this ->query_api ( $ entries_url );
3943 if ( ! empty ( $ get_entries ) ) {
@@ -42,8 +46,8 @@ function get_last_news( $size, $category ) {
4246 if ( ! empty ( $ entry ->featured_media ) ) {
4347 $ api_response = $ this ->query_api ( self ::MEDIA_URL . '/ ' . $ entry ->featured_media );
4448 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 ;
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 ;
4751 }
4852 }
4953 $ modified_entries [] = $ entry ;
@@ -67,13 +71,19 @@ function get_ccorg_categories() {
6771 }
6872 return $ get_categories ;
6973 }
70-
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 );
85+ $ tag = ( ! empty ( $ instance ['tag_id ' ] ) ) ? $ instance ['tag_id ' ] : false ;
86+ $ news = $ this ->get_last_news ( $ size , $ the_category , $ tag );
7787 $ categories = $ this ->get_ccorg_categories ();
7888 if ( ! empty ( $ news ) ) {
7989 echo '<div class="widget news"> ' ;
@@ -99,6 +109,14 @@ function widget( $args, $instance ) {
99109 }
100110
101111 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+ }
102120 delete_transient ( self ::TRANSIENT_PREFIX . $ this ->id . '_categories ' );
103121 delete_transient ( self ::TRANSIENT_PREFIX . $ this ->id . '_entries ' );
104122 delete_transient ( self ::TRANSIENT_PREFIX . '_thumbnails ' );
@@ -112,6 +130,10 @@ 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: ' ;
116138 $ get_categories = $ this ->get_ccorg_categories ();
117139 echo '<p><select name=" ' . $ this ->get_field_name ( 'category ' ) . '" id=" ' . $ this ->get_field_id ( 'category ' ) . '"> ' ;
0 commit comments