Skip to content

Commit 355d2d6

Browse files
author
Chandra Patel
committed
Use WP ajax to get latest news from rtcamp.com
See: #68
1 parent dfc2af1 commit 355d2d6

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

admin/admin.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ function nginx_admin_enqueue_assets($hook) {
133133
wp_enqueue_style('rt-nginx-admin-css', plugin_dir_url(__FILE__) . 'assets/style.css');
134134

135135
/* Load Plugin Scripts */
136-
$admin_js = trailingslashit( site_url() ) . '?get_feeds=1';
137136
wp_enqueue_script( 'nginx-js', plugin_dir_url( __FILE__ ) . 'assets/nginx.js', '', '', true );
138-
wp_localize_script( 'nginx-js', 'news_url', $admin_js );
139137
}
140138
}
141139
}

admin/assets/nginx.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
jQuery( document ).ready( function() {
22
var news_section = jQuery( '#latest_news' );
33
if ( news_section.length > 0 ) {
4-
jQuery.get( news_url, function( data ) {
4+
jQuery.get( ajaxurl, { action: 'rt_nginx_get_news' }, function( data ) {
55
news_section.find( '.inside' ).html( data );
66
} );
77
}

nginx-helper.php

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -401,44 +401,61 @@ function nginx_settings_link( $links )
401401
add_filter( "plugin_action_links_" . plugin_basename( __FILE__ ), 'nginx_settings_link' );
402402
}
403403

404-
function get_feeds( $feed_url = 'http://rtcamp.com/blog/feed/' )
405-
{
406-
// Get RSS Feed(s)
404+
/**
405+
* Get latest news from https://rtcamp.com
406+
*
407+
* @return void
408+
*/
409+
function rt_nginx_get_news() {
410+
411+
$is_rt_news_request = sanitize_text_field( filter_input( INPUT_GET, 'action' ) );
412+
413+
if ( empty( $is_rt_news_request ) || 'rt_nginx_get_news' !== $is_rt_news_request ) {
414+
return;
415+
}
416+
417+
$feed_url = 'http://rtcamp.com/blog/feed/';
418+
419+
// Get RSS Feed.
407420
require_once( ABSPATH . WPINC . '/feed.php' );
421+
408422
$maxitems = 0;
423+
409424
// Get a SimplePie feed object from the specified feed source.
410425
$rss = fetch_feed( $feed_url );
411-
if ( !is_wp_error( $rss ) ) { // Checks that the object is created correctly
426+
427+
// Checks that the object is created correctly.
428+
if ( ! is_wp_error( $rss ) ) {
429+
412430
// Figure out how many total items there are, but limit it to 5.
413431
$maxitems = $rss->get_item_quantity( 5 );
414432

415433
// Build an array of all the items, starting with element 0 (first element).
416434
$rss_items = $rss->get_items( 0, $maxitems );
417435
}
418-
?>
419-
<ul role="list"><?php
420-
if ( $maxitems == 0 ) {
421-
echo '<li role="listitem">' . __( 'No items', 'nginx-helper' ) . '.</li>';
436+
?>
437+
<ul role="list">
438+
<?php
439+
if ( 0 === $maxitems ) {
440+
echo '<li role="listitem">' . esc_html__( 'No items', 'nginx-helper' ) . '.</li>';
422441
} else {
423442
// Loop through each feed item and display each item as a hyperlink.
424443
foreach ( $rss_items as $item ) {
425-
?>
444+
?>
426445
<li role="listitem">
427-
<a href='<?php echo $item->get_permalink(); ?>' title='<?php echo __( 'Posted ', 'nginx-helper' ) . $item->get_date( 'j F Y | g:i a' ); ?>'><?php echo $item->get_title(); ?></a>
428-
</li><?php
446+
<a href='<?php echo esc_url( $item->get_permalink() ); ?>' title='<?php echo esc_attr__( 'Posted ', 'nginx-helper' ) . esc_attr( $item->get_date( 'j F Y | g:i a' ) ); ?>'>
447+
<?php echo esc_html( $item->get_title() ); ?>
448+
</a>
449+
</li>
450+
<?php
429451
}
430452
}
431453
?>
432-
</ul><?php
454+
</ul>
455+
<?php
456+
die();
433457
}
434458

435-
function fetch_feeds()
436-
{
437-
if ( isset( $_GET['get_feeds'] ) && $_GET['get_feeds'] == '1' ) {
438-
get_feeds();
439-
die();
440-
}
441-
}
459+
add_action( 'wp_ajax_rt_nginx_get_news', 'rt_nginx_get_news' );
442460

443-
add_action( 'init', 'fetch_feeds' );
444461
}

0 commit comments

Comments
 (0)