Skip to content

Commit 385c5da

Browse files
authored
Merge pull request #147 from rtCamp/fix/68
Use WP Ajax to get latest news
2 parents 9bb17c4 + d9e57cf commit 385c5da

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
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: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -401,44 +401,58 @@ 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
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+
407417
require_once( ABSPATH . WPINC . '/feed.php' );
418+
408419
$maxitems = 0;
420+
409421
// Get a SimplePie feed object from the specified feed source.
410-
$rss = fetch_feed( $feed_url );
411-
if ( !is_wp_error( $rss ) ) { // Checks that the object is created correctly
422+
$rss = fetch_feed( 'http://rtcamp.com/blog/feed/' );
423+
424+
// Checks that the object is created correctly.
425+
if ( ! is_wp_error( $rss ) ) {
426+
412427
// Figure out how many total items there are, but limit it to 5.
413428
$maxitems = $rss->get_item_quantity( 5 );
414429

415430
// Build an array of all the items, starting with element 0 (first element).
416431
$rss_items = $rss->get_items( 0, $maxitems );
417432
}
418-
?>
419-
<ul role="list"><?php
420-
if ( $maxitems == 0 ) {
421-
echo '<li role="listitem">' . __( 'No items', 'nginx-helper' ) . '.</li>';
433+
?>
434+
<ul role="list">
435+
<?php
436+
if ( 0 === $maxitems ) {
437+
echo '<li role="listitem">' . esc_html__( 'No items', 'nginx-helper' ) . '.</li>';
422438
} else {
423439
// Loop through each feed item and display each item as a hyperlink.
424440
foreach ( $rss_items as $item ) {
425-
?>
441+
?>
426442
<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
443+
<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' ) ); ?>'>
444+
<?php echo esc_html( $item->get_title() ); ?>
445+
</a>
446+
</li>
447+
<?php
429448
}
430449
}
431450
?>
432-
</ul><?php
451+
</ul>
452+
<?php
453+
die();
433454
}
434455

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

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

0 commit comments

Comments
 (0)