Skip to content

Commit

Permalink
Merge pull request #1 from SparkartGroupInc/initial-development
Browse files Browse the repository at this point in the history
#0rQ4rIV6 Initial Development
  • Loading branch information
localjo committed Aug 7, 2015
2 parents 02316de + ddeef89 commit eead61c
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 1 deletion.
61 changes: 61 additions & 0 deletions admin/storyteller-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
function storyteller_settings_init() {
add_settings_section(
'storyteller_settings',
'Storyteller',
'storyteller_settings_section_setup',
'general'
);

add_settings_field(
'storyteller_project',
'Project',
'storyteller_project_setup',
'general',
'storyteller_settings'
);
register_setting( 'general', 'storyteller_project' );

add_settings_field(
'storyteller_apikey',
'API Key',
'storyteller_apikey_setup',
'general',
'storyteller_settings'
);
register_setting( 'general', 'storyteller_apikey' );

add_settings_field(
'storyteller_proxyurl',
'Proxy URL',
'storyteller_proxyurl_setup',
'general',
'storyteller_settings'
);
register_setting( 'general', 'storyteller_proxyurl' );
}
add_action( 'admin_init', 'storyteller_settings_init' );

function storyteller_project_setup() {
$storyteller_project = get_option('storyteller_project');
$storyteller_project_name = isset($storyteller_project) ? $storyteller_project : '';
echo '<input name="storyteller_project" id="storytellerProject" type="text" value="' . $storyteller_project_name . '" />';
}

function storyteller_apikey_setup() {
$storyteller_apikey = get_option('storyteller_apikey');
$storyteller_key = isset($storyteller_apikey) ? $storyteller_apikey : '';
echo '<input name="storyteller_apikey" id="storytellerApiKey" type="text" value="' . $storyteller_key . '" />';
}

function storyteller_proxyurl_setup() {
$storyteller_proxyurl = get_option('storyteller_proxyurl');
$storyteller_url = isset($storyteller_proxyurl) ? $storyteller_proxyurl : 'http://proxy.storyteller.io/wordpress-rest-api/';
echo '<input name="storyteller_proxyurl" id="storytellerProxyUrl" type="text" value="' . $storyteller_url . '" />';
}

function storyteller_settings_section_setup() {

// Render settings html
echo '<p>Enter Storyteller.io credentials to authenticate WordPress with Storyteller.</p>';
}
119 changes: 118 additions & 1 deletion storyteller-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,128 @@
* Plugin Name: Storyteller Helper
* Plugin URI: https://github.com/SparkartGroupInc/storyteller-helper
* Description: Adds useful features for use with Storyteller.io such as triggering webhooks to clear the Storyteller cache on post updates.
* Version: 0.0.0
* Version: 0.1.0
* Author: Sparkart Group, Inc.
* Author URI: http://sparkart.com
* License: MIT
*/

// Blocks direct access to plugin PHP files
defined( 'ABSPATH' ) or die( 'Access denied!' );

$site_subdomain = str_replace(array('http://', 'https://'), '', site_url());

include 'admin/storyteller-settings.php';

$storyteller_project = get_option('storyteller_project');
$storyteller_apikey = get_option('storyteller_apikey');
$storyteller_proxyurl = get_option('storyteller_proxyurl');

if ($storyteller_project && $storyteller_apikey && $storyteller_proxyurl) {
add_action( 'post_updated', 'clear_storyteller_post_cache');

add_action('add_attachment', 'clear_storyteller_attachment_cache');
add_action('edit_attachment', 'clear_storyteller_attachment_cache');
add_action('delete_attachment', 'clear_storyteller_attachment_cache');

add_action('add_category', 'clear_storyteller_category_cache');
add_action('edit_category', 'clear_storyteller_category_cache');
add_action('delete_category', 'clear_storyteller_category_cache');

add_filter('redirect_post_location', 'pass_storyteller_confirmation');
//add_filter('post_updated_messages', 'add_storyteller_confirmation');

if($_GET['storyteller_routes_cleared'] || $_GET['storyteller_routes_errored']){
add_action('admin_notices', 'display_storyteller_messages');
}

}

function clear_storyteller_cache($routes_to_clear) {
global $site_subdomain, $storyteller_project, $storyteller_apikey, $storyteller_proxyurl;
$routes_cleared = array();
$routes_errored = array();
foreach ($routes_to_clear as $route) {
$url = $storyteller_proxyurl . $site_subdomain . $route;
$args = array(
'method' => 'PUT',
'headers' => array(
'Project' => $storyteller_project,
'Api-Key' => $storyteller_apikey
)
);
$response = wp_remote_request( $url, $args );
$response_body = json_decode($response['body']);
if ($response_body->status == 'ok') {
array_push($routes_cleared, $route);
} elseif ($response_body->status == 'error') {
array_push($routes_errored, $route);
}
}
$_POST['storyteller_routes_cleared'] = $routes_cleared;
$_POST['storyteller_routes_errored'] = $routes_errored;
}

function pass_storyteller_confirmation($location){
if (isset($_POST['storyteller_routes_cleared'])) {
$routes_param = array('storyteller_routes_cleared' => $_POST['storyteller_routes_cleared']);
$location = esc_url_raw(add_query_arg($routes_param, $location));
}
if (isset($_POST['storyteller_routes_errored'])) {
$routes_param_e = array('storyteller_routes_errored' => $_POST['storyteller_routes_errored']);
$location = esc_url_raw(add_query_arg($routes_param_e, $location));
}
return $location;
}

function display_storyteller_messages(){
if ($_GET['storyteller_routes_cleared']) {
echo '<div id="message" class="notice notice-info is-dismissible">
<p>Storyteller caches cleared: <code>' . implode('</code>, <code>', $_GET['storyteller_routes_cleared']) . '</code></p>
</div>';
}
if ($_GET['storyteller_routes_errored']) {
echo '<div id="message" class="notice notice-warning is-dismissible">
<p>Failed to clear Storyteller caches: <code>' . implode('</code>, <code>', $_GET['storyteller_routes_errored']) . '</code></p>
</div>';
}
remove_action('admin_notices', 'display_storyteller_messages');
}

function clear_storyteller_post_cache($post_id) {
$routes_to_clear = array();
$updated_post = get_post($post_id);
if ($updated_post->post_type == 'page') {
$parent_post = $updated_post->post_parent? get_post($updated_post->post_parent) : null;
$parent_slug = $parent_post->post_name?$parent_post->post_name.'/':null;
array_push($routes_to_clear,
'/pages',
'/pages/'.$post_id,
'/pages/'.$parent_slug.$updated_post->post_name
);
} else {
array_push($routes_to_clear,
'/posts',
'/posts/'.$post_id
);
}
clear_storyteller_cache($routes_to_clear);
}

function clear_storyteller_attachment_cache($attachment_id) {
$routes_to_clear = array();
array_push($routes_to_clear,
'/media',
'/media/'.$attachment_id
);
clear_storyteller_cache($routes_to_clear);
}

function clear_storyteller_category_cache($category_id) {
$routes_to_clear = array();
array_push($routes_to_clear,
'/posts/types/post/taxonomies/category/terms',
'/posts/types/post/taxonomies/category/terms/'.$category_id
);
clear_storyteller_cache($routes_to_clear);
}

0 comments on commit eead61c

Please sign in to comment.