Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: Axilent/Axilent-Wordpress-Plugin
base: a9085f57f1
...
head fork: Axilent/Axilent-Wordpress-Plugin
compare: 445e7ee865
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
96 axilent.php
@@ -10,12 +10,13 @@
require_once dirname(__FILE__) . '/lib/Utility.php';
require_once dirname(__FILE__) . '/lib/View.php';
+require_once dirname(__FILE__) . '/lib/Model.php';
+require_once dirname(__FILE__) . '/vendor/Axilent.php';
add_action('admin_menu', array('Axilent_Core', 'registerAdmin'));
add_action('widgets_init', array('Axilent_Core', 'registerWidget'));
add_action('add_meta_boxes', array('Axilent_Core', 'addMetaBoxes'));
-add_action('publish_post', array('Axilent_Core', 'postCallback'));
-add_action('publish_page', array('Axilent_Core', 'pageCallback'));
+add_action('save_post', array('Axilent_Core', 'saveCallback'));
/**
* This class is the core of the github/bitbucket project lister.
@@ -27,6 +28,12 @@ class Axilent_Core
* @var int
*/
public static $_cacheExpiration = 3600;
+
+ /**
+ * An active axlient client, if one has been created
+ * @var Axilent
+ */
+ public static $_axilent = null;
/**
* Add the Axilent meta box below the post content
@@ -48,21 +55,57 @@ static function addMetaBoxes()
}
/* Prints the box content */
- function axilentAPIBox( $post ) {
+ function axilentAPIBox($post) {
// Use nonce for verification
wp_nonce_field(plugin_basename(__FILE__), 'axilent_noncename');
- $portlet_key = get_user_meta(get_current_user_id(), 'axilent_portlet_key', true);
+ $axilent = self::getAxilentClient();
+ $content_key = get_post_meta($post->ID, 'axilent_content_key', true);
- if($portlet_key)
- $markup = '<iframe style="width:100%; height: 300px;" src="http://wpdev.axilent.net/airtower/portlets/content/?key='.$portlet_key.'&content_type=Whiskey"></frame>';
+ if(!$axilent->hasPortletKey())
+ {
+ $markup = "You do not have a portlet key defined for your user account. Have an adminitrator set it on the Axilent plugin settings page.";
+ }
else
- $markup = '<p>You do not have a portlet key defined for your user account. Have an adminitrator set it on the Axilent plugin settings page.</p>';
+ {
+ if($content_key)
+ {
+ try
+ {
+ $markup = '<iframe style="width:100%; height: 300px;" src="'.$axilent->getPortletURL($content_key).'"></frame>';
+ }
+ catch(Exception $ex)
+ {
+ $markup = 'There was an error loading this section.';
+ }
+ }
+ else
+ {
+ $markup = '<p>When this post is saved, an Axilent portlet will appear here on subsequent views.</p>';
+ }
+ }
echo $markup;
}
/**
+ * @return Axilent
+ */
+ static function getAxilentClient()
+ {
+ if(self::$_axilent) return self::$_axilent;
+
+ $axilent_project_name = Axilent_Utility::getOption('axilent_project_name');
+ $axilent_subdomain = Axilent_Utility::getOption('axilent_subdomain');
+ $axilent_api_key = Axilent_Utility::getOption('axilent_api_key');
+ $portlet_key = get_user_meta(get_current_user_id(), 'axilent_portlet_key', true);
+
+ self::$_axilent = new Axilent($axilent_subdomain, $axilent_project_name, $axilent_api_key, $portlet_key);
+
+ return self::$_axilent;
+ }
+
+ /**
* Register the admin settings page
*/
static function registerAdmin()
@@ -87,8 +130,9 @@ static function adminMenuCallback()
update_user_meta($id, 'axilent_portlet_key', $value);
}
- Axilent_Utility::setOption('axilent_api_username', $_POST['username']);
- Axilent_Utility::setOption('axilent_api_key', $_POST['api_key']);
+ Axilent_Utility::setOption('axilent_subdomain', $_POST['axilent_subdomain']);
+ Axilent_Utility::setOption('axilent_project_name', $_POST['axilent_project_name']);
+ Axilent_Utility::setOption('axilent_api_key', $_POST['axilent_api_key']);
}
# Attach the API key to each user object
@@ -96,13 +140,16 @@ static function adminMenuCallback()
for($i = 0; $i < count($users); $i++) {
$users[$i]->portlet_key = get_user_meta($users[$i]->ID, 'axilent_portlet_key', true);
}
+
+ $axilent_project_name = Axilent_Utility::getOption('axilent_project_name');
+ $axilent_subdomain = Axilent_Utility::getOption('axilent_subdomain');
+ $axilent_api_key = Axilent_Utility::getOption('axilent_api_key');
$data = array (
- # 'axilent_opener' => self::getOpeningListTemplate(),
- # 'axilent_closer' => self::getClosingListTemplate(),
- # 'axilent_template' => self::getProjectTemplate(),
- # 'axilent_updated' => $updated
- 'axilent_users' => $users
+ 'axilent_project_name' => $axilent_project_name,
+ 'axilent_subdomain' => $axilent_subdomain,
+ 'axilent_api_key' => $axilent_api_key,
+ 'axilent_users' => $users
);
Axilent_View::load('admin', $data);
@@ -112,18 +159,23 @@ static function adminMenuCallback()
* A callback executed wheeever a post is posted
* @param int $postId
*/
- public function postCallback($postId)
+ public function saveCallback($postId)
{
+ $parents = get_post_ancestors($postId);
+ if(count($parents)) $postId = $parents[0];
- }
+ $post = get_post($postId);
+ $content_key = get_post_meta($postId, 'axilent_content_key', true);
- /**
- * A callback executed whenever a page is published
- * @param int $postId
- */
- public function pageCallback($postId)
- {
+ if(!$content_key) $content_key = false;
+ $content = array (
+ 'Content' => $post->post_content,
+ 'Title' => $post->post_title
+ );
+
+ $content_key = self::getAxilentClient()->postContent($content, $content_key);
+ update_post_meta($postId, 'axilent_content_key', $content_key);
}
/**
View
98 lib/Model.php
@@ -0,0 +1,98 @@
+<?php
+/*
+ *
+ *
+ * @author Kenny Katzgrau, Katzgrau LLC <kenny@katgrau.com> www.katzgau.com
+ */
+
+/**
+ * Description of Model
+ *
+ */
+class Axilent_Model
+{
+ public static function getPublishedDocument($document_id)
+ {
+ global $wpdb;
+
+ $table = self::getTableName('posts');
+
+ $sql = "SELECT *
+ FROM $table
+ WHERE ID = '$document_id'
+ AND post_status = 'publish'
+ LIMIT 1";
+
+ $posts = $wpdb->get_results($sql);
+
+ if(count($posts) == 0)
+ return false;
+ else
+ return $posts[0];
+ }
+
+ public static function getPublishedDocuments($page = 0, $per_page = 10)
+ {
+ global $wpdb;
+
+ $start = $page * $per_page;
+
+ $table = self::getTableName('posts');
+
+ $sql = "SELECT *
+ FROM $table
+ WHERE post_status = 'publish'
+ LIMIT $start, $per_page";
+
+ return $wpdb->get_results($sql);
+ }
+
+ public static function getPublishedPostCount()
+ {
+ global $wpdb;
+
+ $table = self::getTableName('posts');
+
+ $sql = "SELECT COUNT(*) AS 'count'
+ FROM $table
+ WHERE post_status = 'publish'";
+
+ $results = $wpdb->get_results($sql);
+
+ return $results[0]->count;
+ }
+
+ public static function getPostComments($post_id, $page = FALSE, $per_page = FALSE)
+ {
+ global $wpdb;
+
+ $table = self::getTableName('comments');
+
+ /* Process posts in batches of 20 from the db */
+ $sql = "SELECT comment_author, comment_content
+ FROM $table
+ WHERE comment_approved = '1'
+ AND comment_post_ID = '$post_id'";
+
+ if($page && $per_page)
+ {
+ $start = $page * $per_page;
+ $sql = "$sql LIMIT $start, $per_page";
+ }
+
+ return $wpdb->get_results($sql);
+ }
+
+ public static function getCategories()
+ {
+ global $wpdb;
+
+ $table = $this->getTableName('');
+ }
+
+ public static function getTableName($table_name)
+ {
+ global $wpdb;
+ return "{$wpdb->prefix}{$table_name}";
+ }
+}
View
102 lib/Net.php
@@ -1,102 +0,0 @@
-<?php
-/**
- * This file contains a class for making webservice calls. Pulled from
- * the WPSearch 2 project
- *
- * @author Kenny Katzgrau, Katzgrau LLC <kenny@katgrau.com> www.katzgau.com
- */
-
-/**
- * Facilitates HTTP GET, POST, PUT, and DELETE calls using cURL as a backend. For
- * GET, will fallback to file_get_contents
- */
-class Axilent_Net
-{
- /**
- * Fetch a web resource by URL
- * @param string $url The HTTP URL that the request is being made to
- * @param array $options Any PHP cURL options that are needed
- * @return object An object with properties of 'url', 'body', and 'status'
- */
- public static function fetch($url, $options = array())
- {
- if(!function_exists('curl_exec'))
- {
- if(!$options) return file_get_contents ($url);
- else return '';
- }
-
- $curl_handle = curl_init($url);
- $options += array(CURLOPT_RETURNTRANSFER => true);
-
- curl_setopt_array($curl_handle, $options);
-
- $timer = "Call to $url via HTTP";
-
- $body = curl_exec($curl_handle);
- $status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
-
- return $body;
- }
-
- /**
- * Issues an HTTP GET request to the specified URL
- * @param string $url
- * @return object An object with properties of 'url', 'body', and 'status'
- */
- public static function get($url)
- {
- return self::fetch($url);
- }
-
- /**
- * Issues an HTTP POST request to the specified URL with the supplied POST
- * body
- * @param string $url
- * @param string $data The raw POST body
- * @return object An object with properties of 'url', 'body', and 'status'
- */
- public static function post($url, $data)
- {
- return self::fetch($url, array (
- CURLOPT_POST => true,
- CURLOPT_POSTFIELDS => $post_data
- )
- );
- }
-
- /**
- * Issues an HTTP DELETE to the specified URL
- * @param string $url
- * @return object An object with properties of 'url', 'body', and 'status'
- */
- public static function delete($url)
- {
- return self::fetch($url, array (
- CURLOPT_CUSTOMREQUEST => 'DELETE'
- ));
- }
-
- /**
- * Issues an HTTP PUT to the specified URL
- * @param string $url
- * @param string $data Raw PUT data
- * @return object An object with properties of 'url', 'body', and 'status'
- */
- public static function put($url, $data)
- {
- $putData = tmpfile();
-
- fwrite($putData, $data);
- fseek($putData, 0);
-
- $result = self::fetch($url, array(
- CURLOPT_PUT => true,
- CURLOPT_INFILE => $putData,
- CURLOPT_INFILESIZE => strlen($putData)
- ));
- fclose($putData);
-
- return $result;
- }
-}
View
251 vendor/Axilent.php
@@ -1,29 +1,48 @@
<?php
/**
- * This file contains a PHP class for facilitating Axilent API calls.
+ * This file contains classes for facilitating Axilent API calls. The only one
+ * relevant to the average develper is class 'Axilent'. Axilent_Net is a
+ * utility class containing static methods for making various HTTP requests.
+ *
+ * Usage:
+ * $client = new Axilent('fooproj, 'Foo Proj', 'somemumbojumbo');
+ * $client->postContent(array('title' => 'Title Text', 'content' => 'Body Text'))
*/
/**
* This is the core Axilent PHP library. It is used to authenticate and make
* API calls to Axilent.
* @author Kenny Katzgrau, Katzgrau LLC <kenny@katgrau.com> www.katzgau.com
+ * @version Works with Axilent API beta1
*/
class Axilent
-{
+{
/**
- *
- * @var type
+ * The subdmain used for the API
+ * @var string
*/
- protected $_apiBase = null;
+ protected $_apiDomain = null;
/**
- *
- * @var type
+ * The API Key
+ * @var string
*/
protected $_apiKey = null;
/**
- *
+ * A template for API URIs
+ * @var string
+ */
+ protected $_apiPrototype = "http://%s.axilent.net/api/resource/%s/%s/";
+
+ /**
+ * The version of the API we're using
+ * @var string
+ */
+ protected $_apiVersion = "beta1";
+
+ /**
+ * A hash of active API instances
* @var Axilent
*/
protected static $_instances = array();
@@ -35,27 +54,28 @@ class Axilent
protected $_portletKey = null;
/**
- *
+ * The project name
* @var type
*/
- protected $_username = null;
+ protected $_project = null;
/**
* Create a new Axilent API client
- * @param type $api_base The base path of the API
- * @param type $username
+ * @param type $project The project name
* @param type $apiKey
*/
- public function __construct($api_base, $username, $apiKey, $portlet_key = null)
+ public function __construct($subdomain, $project, $apiKey, $portlet_key = null)
{
- $this->_apiBase = rtrim($api_base, '/') . '/';
- $this->_username = $username;
+ $this->_apiDomain = $subdomain;
$this->_apiKey = $apiKey;
$this->_portletKey = $portlet_key;
+ $this->_project = $project;
}
/**
* Make a request to the Axilent API
+ * @param type $resource The name fo the resource we're sending data to, like
+ * "axilent.airtower"
* @param type $path The URI to send data to
* @param type $arguments An associative array that will be encoded as JSON
* and posted
@@ -63,27 +83,27 @@ public function __construct($api_base, $username, $apiKey, $portlet_key = null)
* properties
* @throws Exception If curl is not found
*/
- protected function _makeRequest($path, $arguments)
+ protected function _makeRequest($method, $resource, $path, $arguments)
{
if(!function_exists('curl_exec'))
{
throw new Exception("The cURL module must be installed to use this class");
}
- $curl_handle = curl_init($url);
- $options = array (
- CURLOPT_USERPWD => "{$this->_username}:{$this->_apiKey}",
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_POST => true,
- CURLOPT_POSTFIELDS => json_encode($arguments)
- );
-
- curl_setopt_array($curl_handle, $options);
-
- $body = curl_exec($curl_handle);
- $status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
+ $url = $this->_getRequestURL($resource, $path);
+ $result = Axilent_Net::call($method, $url, json_encode($arguments), array(CURLOPT_USERPWD => $this->_apiKey));
- return (object)(array('url' => $url, 'body' => $body, 'status' => $status));
+ return json_decode($result);
+ }
+
+ /**
+ * Get the request url for a call
+ * @param type $resource The resource, such as "axilent.airtower"
+ * @param type $path The URI for the request and the given resource
+ */
+ protected function _getRequestURL($resource, $path)
+ {
+ return sprintf($this->_apiPrototype, $this->_apiDomain, $resource, $this->_apiVersion) . rtrim($path, '/') . '/';
}
/**
@@ -91,20 +111,185 @@ protected function _makeRequest($path, $arguments)
* object instantiation
* @throws Exception If the portlet key was not provided
*/
- public function getPortletURL()
+ public function getPortletURL($content_key = '')
{
if($this->_portletKey === null)
throw new Exception("Portlet key was not provided at initialization of " . __CLASS__);
+
+ return "http://{$this->_portletKey}@wpdev.axilent.net/airtower/portlets/content/?key={$content_key}&content_type=post";
}
-
+ /**
+ * Get and array of relevant content
+ * @param type $content_key
+ */
public function getRelevantContent($content_key)
{
}
- public function postContent($content, $content_key = false)
+
+ /**
+ * Does this class have a portlet key?
+ * @return bool
+ */
+ public function hasPortletKey()
+ {
+ return (bool)$this->_portletKey;
+ }
+
+ /**
+ * Import content (create or update)
+ * @param array $content An associative array of content fields to values
+ * @param type $content_key The content key. If this isn't provided, this
+ * will be treated as an update
+ * @return The content key of the post just sent
+ */
+ public function postContent($content, $content_key = false, $content_type = 'post')
{
+ $args = array (
+ 'project' => $this->_project,
+ 'content' => $content,
+ 'content_type' => $content_type
+ );
+
+ $method = 'post';
+
+ if($content_key)
+ {
+ $method = 'put';
+ $args['key'] = $content_key;
+ }
+ $response = $this->_makeRequest($method, 'axilent.airtower', 'content', $args);
+
+ if(!$content_key) {
+ list($type, $key) = explode(':', $response->created_content);
+ return $key;
+ }
+ else
+ {
+ return $content_key;
+ }
}
-}
+}
+
+/**
+ * Facilitates HTTP GET, POST, PUT, and DELETE calls using cURL as a backend. For
+ * GET, will fallback to file_get_contents
+ */
+class Axilent_Net
+{
+ /**
+ * Fetch a web resource by URL
+ * @param string $url The HTTP URL that the request is being made to
+ * @param array $options Any PHP cURL options that are needed
+ * @return object An object with properties of 'url', 'body', and 'status'
+ */
+ public static function fetch($url, $options = array())
+ {
+ if(!function_exists('curl_exec'))
+ {
+ if(!$options) return file_get_contents ($url);
+ else return '';
+ }
+
+ $curl_handle = curl_init($url);
+ $options = array(CURLOPT_RETURNTRANSFER => true) + $options;
+
+ curl_setopt_array($curl_handle, $options);
+
+ $timer = "Call to $url via HTTP";
+
+ $body = curl_exec($curl_handle);
+ $status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
+
+ if(!$status) throw new Axilent_HTTPException("Error making request to $url with ".print_r($options, true).". \nStatus: $status");
+
+ return $body;
+ }
+
+ /**
+ * Make an HTTP call
+ * @param type $method The HTP method to use
+ * @param type $url The URL to call
+ * @param type $data The data to pass (if applicable)
+ * @param type $options The options to pass
+ * @throws Axilent_ArgumentException
+ */
+ public static function call($method, $url, $data, $options)
+ {
+ if(method_exists(__CLASS__, $method)) {
+ return call_user_func_array(array(__CLASS__, $method), array($url, $data, $options));
+ } else {
+ throw new Axilent_ArgumentException("Method '$method' not allowed on " . __CLASS__);
+ }
+ }
+
+ /**
+ * Issues an HTTP GET request to the specified URL
+ * @param string $url
+ * @return object An object with properties of 'url', 'body', and 'status'
+ */
+ public static function get($url, $data, $options = array())
+ {
+ return self::fetch($url, $options);
+ }
+
+ /**
+ * Issues an HTTP POST request to the specified URL with the supplied POST
+ * body
+ * @param string $url
+ * @param string $data The raw POST body
+ * @return object An object with properties of 'url', 'body', and 'status'
+ */
+ public static function post($url, $data = false, $options = array())
+ {
+ $options = array (
+ CURLOPT_POST => true,
+ CURLOPT_POSTFIELDS => $data
+ ) + $options;
+
+ return self::fetch($url, $options);
+ }
+
+ /**
+ * Issues an HTTP DELETE to the specified URL
+ * @param string $url
+ * @return object An object with properties of 'url', 'body', and 'status'
+ */
+ public static function delete($url, $data = false, $options = array())
+ {
+ $options = array (CURLOPT_CUSTOMREQUEST => 'DELETE') + $options;
+ return self::fetch($url, $options);
+ }
+
+ /**
+ * Issues an HTTP PUT to the specified URL
+ * @param string $url
+ * @param string $data Raw PUT data
+ * @return object An object with properties of 'url', 'body', and 'status'
+ */
+ public static function put($url, $data = false, $options = array())
+ {
+ $putData = tmpfile();
+
+ fwrite($putData, $data);
+ fseek($putData, 0);
+
+ $options = array (
+ CURLOPT_PUT => true,
+ CURLOPT_INFILE => $putData,
+ CURLOPT_INFILESIZE => strlen($data)
+ ) + $options;
+
+ $result = self::fetch($url, $options);
+ fclose($putData);
+
+ return $result;
+ }
+}
+
+class Axilent_Exception extends Exception {}
+class Axilent_ArgumentException extends Axilent_Exception {}
+class Axilent_HTTPException extends Axilent_Exception {}
View
10 views/admin.php
@@ -48,16 +48,16 @@
</thead>
<tbody>
<tr>
- <td>Axilent Account Name</td>
- <td><input type="text" name="account_name" /></td>
+ <td>Axilent Project Name</td>
+ <td><input type="text" name="axilent_project_name" value="<?php echo $axilent_project_name; ?>" /></td>
</tr>
<tr>
- <td>API Username</td>
- <td><input type="text" name="username" /></td>
+ <td>API Subdomain</td>
+ <td><input type="text" name="axilent_subdomain" value="<?php echo $axilent_subdomain; ?>"/></td>
</tr>
<tr>
<td>API Key</td>
- <td><input type="text" name="api_key" /></td>
+ <td><input type="text" name="axilent_api_key" value="<?php echo $axilent_api_key ?>" /></td>
</tr>
</tbody>
</table>

No commit comments for this range

Something went wrong with that request. Please try again.