Permalink
Browse files

Change to OO style

Change to OO style. Taiwan offline works,
  • Loading branch information...
chen420 committed Apr 18, 2016
1 parent 7456feb commit 1375d94b3175334ef1a764c531ed57df71d81814
Showing with 161 additions and 109 deletions.
  1. +5 −2 menus.php
  2. +14 −12 readme.txt
  3. +18 −95 tinywebdb-api.php
  4. +124 −0 tinywebdb.php
View
@@ -1,5 +1,8 @@
<?php
class TinyWebDB_Admin {
}
//***** Options Menu *****
function wp_tinywebdb_api_optionsmenu() {
@@ -80,7 +83,7 @@ function wp_tinywebdb_api_optionsmenu() {
</table>
<div class="clear"></div>
</div>
<h3>Address</h3>
<table class="form-table">
@@ -97,7 +100,7 @@ function wp_tinywebdb_api_optionsmenu() {
<tr valign="top">
<th scope="row">Tag type</th>
<td>
<input type="radio" name="tagtype" value="id" <?php if ($setting_tagtype == 'id') { echo 'checked="checked"';} ?> /> Post ID
<input type="radio" name="tagtype" value="id" <?php if ($setting_tagtype == 'id') { echo 'checked="checked"';} ?> /> Post ID
<input type="radio" name="tagtype" value="slug" <?php if ($setting_tagtype == 'slug') { echo 'checked="checked"'; } ?> /> Slug
<br/>Select Tag mach to type <em>post_id</em> or <em>slug</em>.</td>
</tr>
View
@@ -1,12 +1,12 @@
=== TinyWebDB API ===
Contributors: chen420
Plugin URI: http://appinventor.in/side/tinywebdb-api/
Plugin URI: http://edu2web.com/tinywebdb-api/
Author URI: http://digilib.net/
Donate link: http://appinventor.in/donate/
Donate link: http://edu2web.com/tinywebdb-api/donate/
Tags: appinventor, tinywebdb, api
Requires at least: 3.4
Tested up to: 3.4.2
Stable tag: 0.2.0
Requires at least: 4.0
Tested up to: 4.4
Stable tag: 0.2.2
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -19,15 +19,15 @@ AppInventor is a easy way to creating an Android app from web browser.
TinyWebDB API is a AppInventor TinyWebDB API plugin, use you WordPress as a TinyWebDB web service.
TinyWebDB Protocol:
Action URL Post Parameters Response
Get Value {ServiceURL}/getvalue tag JSON: ["VALUE","{tag}", {value}]
Store A Value {ServiceURL}/storeavalue tag,value JSON: ["STORED", "{tag}", {value}]
Action URL Post Parameters Response
Get Value {ServiceURL}/getvalue tag JSON: ["VALUE","{tag}", {value}]
Store A Value {ServiceURL}/storeavalue tag,value JSON: ["STORED", "{tag}", {value}]
Roadmap:
TinyWebDB API 0.1.0 implemented Get Value Action.
TinyWebDB API 0.2.0 will implement Store A Value Action.
TinyWebDB API 0.3.0 will implement Authentication.
TinyWebDB API 1.0.0 Full release.
TinyWebDB API 0.3.0 will implement Authentication.
TinyWebDB API 1.0.0 Full release.
Visit Plugin URI for detail.
@@ -47,7 +47,7 @@ for how to use AppInventor to inventor your Android app with this plugin , visit
3. Reactivate plugin
Upgrade notes:
* You may use the autmoated plugin updater in WordPress 2.5+ with this plugin, but make sure you read the upgrade notes of the latest version after upgrading.
* You may use the automated plugin updater in WordPress 2.5+ with this plugin, but make sure you read the upgrade notes of the latest version after upgrading.
= 1.0 =
@@ -77,5 +77,7 @@ First alpha release which implemented Get Value Action.
Release which implemented Get Value Action with API Key.
Add test URL on admin menu.
= 1.0.0 =
= 0.2.2 =
Re-write code with oo-calss style.
= 1.0.0 =
View
@@ -1,21 +1,19 @@
<?php
/*
Plugin Name: Wp TinyWebDB API
Plugin URI: http://appinventor.in/side/tinywebdb-api/
Plugin URI: http://edu2web.com/tinywebdb-api/
Description: a AppInventor TinyWebDB API plugin, use you WordPress as a TinyWebDB web service.
Action URL Post Parameters Response
Get Value {ServiceURL}/getvalue tag JSON: ["VALUE","{tag}", {value}]
Store A Value {ServiceURL}/storeavalue tag,value JSON: ["STORED", "{tag}", {value}]
Action URL Post Parameters Response
Get Value {ServiceURL}/getvalue tag JSON: ["VALUE","{tag}", {value}]
Store A Value {ServiceURL}/storeavalue tag,value JSON: ["STORED", "{tag}", {value}]
Author: Hong Chen
Author URI: http://digilib.net/
Version: 0.2.1
Version: 0.2.2
*/
define("TINYWEBDB", "tools.php?page=tinywebdb-api/tinywebdb-api.php");
define("TINYWEBDB_VER", "0.2.1");
define("TINYWEBDB_VER", "0.2.2");
//***** Hooks *****
register_activation_hook(__FILE__,'wp_tinywebdb_api_install'); //Install
@@ -29,52 +27,6 @@
include "installer.php";
}
function wp_tinywebdb_api_get_postid($tagName){
$tagtype = get_option("wp_tinywebdb_api_tag_type");
if ($tagtype=='') {
$tagtype = 'id';
}
if ($tagtype == 'id') {
$postid = $tagName;
} else {
// get_page_by_path('slug')->ID;
$args=array(
'name' => $tagName,
'post_type' => 'post',
'post_status' => 'publish',
'showposts' => 1,
'caller_get_posts'=> 1
);
$my_posts = get_posts($args);
if( $my_posts ) {
$postid = $my_posts[0]->ID;
}
}
return $postid;
}
function wp_tinywebdb_api_get_tagName($postid){
$tagtype = get_option("wp_tinywebdb_api_tag_type");
if ($tagtype=='') {
$tagtype = 'id';
}
if ($tagtype == 'id') {
$tagName = $postid;
} else {
// get_page_by_path('slug')->ID;
$post_data = get_post($postid, ARRAY_A);
$slug = $post_data['post_name'];
$tagName = $slug;
}
return $tagName;
}
//***** get $request and get_post , then json_encode it *****
@@ -87,14 +39,13 @@ function add_fetch($public_query_vars) {
}
function wp_tinywebdb_api_query() {
global $wpdb, $table_prefix;
$bedtag = array("id" => "0", "post_author" => "0", "post_content" => "ERROR BAD tag SUPPLIED");
$request = $_SERVER['REQUEST_URI'];
if (!isset($_SERVER['REQUEST_URI'])) {
$request = substr($_SERVER['PHP_SELF'], 1);
if (isset($_SERVER['QUERY_STRING']) AND $_SERVER['QUERY_STRING'] != '') { $request.='?'.$_SERVER['QUERY_STRING']; }
}
$url_trigger = get_option("wp_tinywebdb_api_url_trigger");
if ($url_trigger=='') {
$url_trigger = 'api';
@@ -106,26 +57,14 @@ function wp_tinywebdb_api_query() {
if ( strpos('/'.$request, '/'.$url_trigger.'/') ) {
global $wp_query;
if ($wp_query->is_404) {
$wp_query->is_404 = false;
$wp_query->is_archive = true;
}
header("HTTP/1.1 200 OK");
$tinywebdb_key = explode($url_trigger.'/', $request);
$tinywebdb_key = $tinywebdb_key[1];
$tinywebdb_key = explode('/', $tinywebdb_key);
$action = $tinywebdb_key[0];
$action = $wpdb->escape($action);
switch ($action) {
case "getvalue": // this action enable from v 0.1.x
// JSON_API , Post Parameters : tag
include "tinywebdb.php";
$tinywebdb = TinyWebDB;
header("HTTP/1.1 200 OK");
switch (TinyWebDB::get_action()) {
case "getvalue":
$tagName = get_query_var('tag');
$postid = wp_tinywebdb_api_get_postid($tagName);
$tagValue = get_post($postid);
if (is_null($tagValue)) $tagValue = $bedtag; //reports a get_post failure
// $tagName = wp_tinywebdb_api_get_tagName($postid);
$tagValue = TinyWebDB::getvalue($tagName);
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
@@ -136,29 +75,13 @@ function wp_tinywebdb_api_query() {
case "storeavalue": // this action will enable from v 0.2.x
// JSON_API , Post Parameters : tag,value
$tagName = get_query_var('tag');
$tagValue = get_query_var('value'); // $_REQUEST['value']; //
$tagValue = get_query_var('value'); // $_REQUEST['value']; //
$apiKey = get_query_var('apikey');
error_log("Wp TinyWebDB API : storeavalue: " . __FILE__ . "/" . __LINE__ . " ($apiKey) $tagName -- $tagValue");
error_log("Wp TinyWebDB API : storeavalue: " . __FILE__ . "/" . __LINE__ . " ($apiKey) $tagName -- $tagValue");
$setting_apikey = get_option("wp_tinywebdb_api_key");
if ($apiKey == $setting_apikey){
// Create post object
$args = array(
'post_title' => wp_strip_all_tags( $tagName ),
'post_content' => $tagValue,
'post_status' => 'publish',
);
// Insert the post into the database
$postid = wp_insert_post( $args );
if ($postid == 0) {
$postid = wp_tinywebdb_api_get_postid($tagName);
$args = array(
'ID' => wp_strip_all_tags( $postid ),
'post_content' => $tagValue,
);
$postid = wp_update_post( $args );
}
$postid = TinyWebDB::getvalue($tagName, $tagValue);
$tagName = wp_tinywebdb_api_get_tagName($postid);
header('Cache-Control: no-cache, must-revalidate');
View
@@ -0,0 +1,124 @@
<?php
class TinyWebDB {
private static $initiated = false;
public static function init() {
if ( ! self::$initiated ) {
self::init_hooks();
}
}
public static function init_hooks() {
self::$initiated = true;
}
public function __construct {
global $wp_query;
if ($wp_query->is_404) {
$wp_query->is_404 = false;
$wp_query->is_archive = true;
}
}
public function __destruct() {
return true;
}
public static function get_action() {
global $wpdb;
$tinywebdb_key = explode($url_trigger.'/', $request);
$tinywebdb_key = $tinywebdb_key[1];
$tinywebdb_key = explode('/', $tinywebdb_key);
$action = $tinywebdb_key[0];
$action = $wpdb->escape($action);
return $action;
}
function wp_tinywebdb_api_get_postid($tagName){
$tagtype = get_option("wp_tinywebdb_api_tag_type");
if ($tagtype=='') {
$tagtype = 'id';
}
if ($tagtype == 'id') {
$postid = $tagName;
} else {
// get_page_by_path('slug')->ID;
$args=array(
'name' => $tagName,
'post_type' => 'post',
'post_status' => 'publish',
'showposts' => 1,
'caller_get_posts'=> 1
);
$my_posts = get_posts($args);
if( $my_posts ) {
$postid = $my_posts[0]->ID;
}
}
return $postid;
}
function wp_tinywebdb_api_get_tagName($postid){
$tagtype = get_option("wp_tinywebdb_api_tag_type");
if ($tagtype=='') {
$tagtype = 'id';
}
if ($tagtype == 'id') {
$tagName = $postid;
} else {
// get_page_by_path('slug')->ID;
$post_data = get_post($postid, ARRAY_A);
$slug = $post_data['post_name'];
$tagName = $slug;
}
return $tagName;
}
public static function getvalue($tagName) {
$bedtag = array("id" => "0", "post_author" => "0", "post_content" => "ERROR BAD tag SUPPLIED");
// this action enable from v 0.1.x
// JSON_API , Post Parameters : tag
$postid = wp_tinywebdb_api_get_postid($tagName);
$tagValue = get_post($postid);
if (is_null($tagValue)) $tagValue = $bedtag; //reports a get_post failure
// $tagName = wp_tinywebdb_api_get_tagName($postid);
return $tagValue;
}
public static function storeavalue($tagName, $tagValue) {
// Create post object
$args = array(
'post_title' => wp_strip_all_tags( $tagName ),
'post_content' => $tagValue,
'post_status' => 'publish',
);
// Insert the post into the database
$postid = wp_insert_post( $args );
if ($postid == 0) {
$postid = wp_tinywebdb_api_get_postid($tagName);
$args = array(
'ID' => wp_strip_all_tags( $postid ),
'post_content' => $tagValue,
);
$postid = wp_update_post( $args );
}
return $postid;
}
}

0 comments on commit 1375d94

Please sign in to comment.