Skip to content
This repository has been archived by the owner on Nov 5, 2020. It is now read-only.

Commit

Permalink
souce code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wokamoto committed Apr 28, 2014
1 parent acf690e commit 961a6b7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 106 deletions.
113 changes: 29 additions & 84 deletions feed-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
Plugin URI: http://wordpress.org/extend/plugins/feed-json/
Description: Adds a new type of feed you can subscribe to. http://example.com/feed/json or http://example.com/?feed=json to anywhere you get a JSON form.
Author: wokamoto
Version: 1.0.8
Version: 1.0.9
Author URI: http://dogmap.jp/
License:
Released under the GPL license
http://www.gnu.org/copyleft/gpl.html
Copyright 2011-2013 (email : wokamoto1973@gmail.com)
Copyright 2011-2014 (email : wokamoto1973@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -28,37 +28,36 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

$feed_json = feed_json->get_instance();
$feed_json->init();

register_activation_hook(__FILE__, array($feed_json, 'add_feed_json_once');
register_deactivation_hook(__FILE__, array($feed_json, 'remove_feed_json');

class feed_json {
static $instance;
const JSON_TEMPLATE = 'feed-json.php';
const INCREMENTOR_KEY = 'fj_posts_inc';

public function __construct() {
self::$instance = $this;

add_action('init', array($this, 'add_feed_json'));
add_action('do_feed_json', array($this, 'do_feed_json'), 10, 1);
add_filter('template_include', array($this, 'template_json'));
add_filter('query_vars', array($this, 'add_query_vars'));
add_action( 'save_post', array( $this, 'action_save_post_flush_cache' ) );
add_action( 'delete_term', array( $this, 'action_term_flush_cache' ) );
add_action( 'create_term', array( $this, 'action_term_flush_cache' ) );
add_action( 'edit_terms', array( $this, 'action_term_flush_cache' ) );

if (function_exists('register_activation_hook')) {
register_activation_hook(__FILE__, array($this, 'add_feed_json_once'));
register_deactivation_hook(__FILE__, array($this, 'remove_feed_json'));
} else {
$plugin_basename = plugin_basename(__FILE__);
add_action('activate_' . $plugin_basename, array($this, 'add_feed_json_once'));
add_action('deactivate_' . $plugin_basename, array($this, 'remove_feed_json'));

private function __construct() {}

public static function get_instance() {
if( !isset( self::$instance ) ) {
$c = __CLASS__;
self::$instance = new $c();
}
return self::$instance;
}

public function init() {
add_action( 'init', array( $this, 'add_feed_json') );
add_action( 'do_feed_json', array( $this, 'do_feed_json'), 10, 1 );
add_filter( 'template_include', array( $this, 'template_json') );
add_filter( 'query_vars', array( $this, 'add_query_vars') );
}

public function add_feed_json_once() {
global $wp_rewrite;
$this->add_feed_json();
$wp_rewrite->flush_rules();
flush_rewrite_rules();
}

public function remove_feed_json() {
Expand All @@ -70,16 +69,16 @@ public function remove_feed_json() {
}
}
$wp_rewrite->feeds = $feeds;
$wp_rewrite->flush_rules();
flush_rewrite_rules();
}

public function add_query_vars($qvars) {
$qvars[] = 'callback';
$qvars[] = 'limit';
return $qvars;
$qvars[] = 'callback';
$qvars[] = 'limit';
return $qvars;
}

public function add_feed_json() {
static public function add_feed_json() {
add_feed('json', array($this, 'do_feed_json'));
}

Expand All @@ -102,58 +101,4 @@ public function template_json( $template ) {
$template_file = ($template_file !== false ? $template_file : $template);
return apply_filters( 'feed-json-template-file', $template_file );
}

/**
* Return current cache incrementor value.
*
* @return string
*/
public function get_incrementor_value() {

if ( $incrementor = get_transient( self::INCREMENTOR_KEY ) )
return $incrementor;
else
return $this->update_incrementor();
}

/**
* Flush cache on post save.
*
* @param int $post_id
* @return void
*/
public function action_save_post_flush_cache( $post_id ) {
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! current_user_can( 'edit_post', $post_id ) || 'revision' == get_post_type( $post_id ) )
return;

$this->update_incrementor();
}

/**
* Flush cache on term save/create/delete.
*
* @param int $post_id
* @return void
*/
public function action_term_flush_cache() {
if ( ! current_user_can( 'edit_post' ) )
return;

$this->update_incrementor();
}

/**
* Updates cache incrementor value.
*
* @return string
*/
public function update_incrementor() {
$time = time();
set_transient( self::INCREMENTOR_KEY, $time );
return $time;
}
}

global $fj_feed_json;
$fj_feed_json = new feed_json();

18 changes: 11 additions & 7 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Contributors: wokamoto
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=9S8AJCY7XB8F4&lc=JP&item_name=WordPress%20Plugins&item_number=wp%2dplugins&currency_code=JPY&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted
Tags: feed, feeds, json, jsonp
Requires at least: 3.0
Tested up to: 3.8
Stable tag: 1.0.8
Tested up to: 3.9
Stable tag: 1.0.9

Adds a new type of feed you can subscribe to. http://example.com/feed/json or http://example.com/?feed=json to anywhere you get a JSON form.

Expand All @@ -19,7 +19,11 @@ Adds a new type of feed you can subscribe to.
1. Upload the entire `feed-json` folder to the `/wp-content/plugins/` directory.
2. Activate the plugin through the 'Plugins' menu in WordPress.

== Changelog ==
== Changelog ==

**1.0.9 April 28, 2014**

Source code refactoring.

**1.0.7 November 13**

Expand All @@ -38,18 +42,18 @@ Bug fix.

source code refactoring

**1.0.3 June 10, 2013**
**1.0.3 June 10, 2013**

source code refactoring

**1.0.2 February 17, 2012**
**1.0.2 February 17, 2012**

Bug fix.

**1.0.1 July 28, 2011**
**1.0.1 July 28, 2011**

Bug fix.

**1.0.0 February 15, 2011**
**1.0.0 February 15, 2011**

Initial release.
18 changes: 3 additions & 15 deletions template/feed-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,10 @@
*
*/
$callback = trim(esc_html(get_query_var('callback')));
$charset = get_bloginfo('charset');

if (!function_exists('json_encode')) {
// For PHP < 5.2.0
function json_encode( $string ) {
if ( !class_exists('Services_JSON') ) {
require_once( 'class-json.php' );
}
$json = new Services_JSON();
return $json->encode( $string );
}
}
$charset = get_option('charset');

if ( have_posts() ) {

global $wp_query, $fj_feed_json;
global $wp_query;
$query_array = $wp_query->query;

// Make sure query args are always in the same order
Expand Down Expand Up @@ -75,6 +63,6 @@ function json_encode( $string ) {
}

} else {
header("HTTP/1.0 404 Not Found");
status_header('404');
wp_die("404 Not Found");
}

0 comments on commit 961a6b7

Please sign in to comment.