Commit 93f96a7c authored by Jordan Michaels's avatar Jordan Michaels

alpha release 2018011901

parent 54078ef3
......@@ -2,7 +2,6 @@
abstract class Vivio_Swift_Admin_Messages
{
function show_msg_settings_updated()
{
echo '<div id="message" class="updated fade"><p><strong>';
......@@ -44,16 +43,4 @@ abstract class Vivio_Swift_Admin_Messages
echo $error_msg;
echo '</strong></p></div>';
}
function start_buffer()
{
ob_start();
}
function end_buffer_and_collect()
{
$output = ob_get_contents();
ob_end_clean();
return $output;
}
}
\ No newline at end of file
......@@ -14,10 +14,10 @@ class Vivio_Swift_Admin {
add_action('admin_menu', array(&$this, 'create_admin_menus'));
//init scripts/styles if user is on vivio_swift plugin pages
//if (isset($_GET['page']) && strpos($_GET['page'], VIVIO_SWIFT_MENU_SLUG_PREFIX) !== false) {
if (isset($_GET['page']) && strpos($_GET['page'], VIVIO_SWIFT_MENU_SLUG_PREFIX) !== false) {
add_action('admin_print_scripts', array(&$this, 'admin_menu_page_scripts'));
add_action('admin_print_styles', array(&$this, 'admin_menu_page_styles'));
//}
}
}
function admin_menu_page_scripts()
......
......@@ -8,7 +8,9 @@ class Vivio_Swift_Cache_Menu extends Vivio_Swift_Admin_Messages
var $menu_tabs_handler = array(
'tab1' => 'render_tab1',
'tab2' => 'render_tab2'
'tab2' => 'render_tab2',
'tab3' => 'render_tab3',
'tab4' => 'render_tab4'
);
function __construct()
......@@ -19,8 +21,10 @@ class Vivio_Swift_Cache_Menu extends Vivio_Swift_Admin_Messages
function set_menu_tabs()
{
$this->menu_tabs = array(
'tab1' => __('Settings', 'vivio-swift'),
'tab2' => __('Reports', 'vivio-swift'),
'tab1' => __('Simple Settings', 'vivio-swift'),
'tab2' => __('Advanced Settings', 'vivio-swift'),
'tab3' => __('Exclusions', 'vivio-swift'),
'tab4' => __('Reports', 'vivio-swift'),
);
}
......@@ -69,10 +73,6 @@ class Vivio_Swift_Cache_Menu extends Vivio_Swift_Admin_Messages
function render_tab1()
{
echo '<div class="message_standard">';
echo '<p>' . __("By caching PHP to it's fully rendered HTML, you reduce processing load and help all supplimentary caching services like memcache, CDN's, and even browsers, run faster.", 'vivio-swift') . '</p>';
echo '</div>';
global $vivio_swift_global;
// process cache settings
......@@ -82,14 +82,21 @@ class Vivio_Swift_Cache_Menu extends Vivio_Swift_Admin_Messages
// verify nonce
$nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($nonce, 'vivio_swift_remove_query_strings_nonce')) {
if (!wp_verify_nonce($nonce, 'vivio_swift_simple_settings_nonce')) {
$vivio_swift_global->debug_logger->log_debug("Nonce check failed on cache settings.", 4);
wp_die("Error: Nonce check failed on cache settings.");
}
if($error)
{
$this->show_msg_error(__('Attention!','vivio-swift').$error);
}
// set and save debug settings
$vivio_swift_global->configs->set_value('vivio_swift_remove_query_strings',isset($_POST["vivio_swift_remove_query_strings"])?'1':'');
$vivio_swift_global->configs->set_value('vivio_swift_cache_enabled',isset($_POST["vivio_swift_cache_enabled"])?'1':'');
$vivio_swift_global->configs->save_config();
$this->show_msg_settings_updated();
}
?>
......@@ -97,32 +104,163 @@ class Vivio_Swift_Cache_Menu extends Vivio_Swift_Admin_Messages
<div class="postbox">
<h3 class="hndle">
<label for="title"><?php _e('Settings', 'vivio-swift');?></label>
<label for="title"><?php _e('Simple Settings', 'vivio-swift');?></label>
</h3>
<div class="inside">
<form action="" method="POST">
<?php wp_nonce_field('vivio_swift_remove_query_strings_nonce'); ?>
<form action=""
id="vivio_swift_cache_simple_settings"
method="POST">
<?php wp_nonce_field('vivio_swift_simple_settings_nonce'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('Remove Query Strings', 'vivio-swift')?>:</th>
<th scope="row"><?php _e('Enable Cache', 'vivio-swift')?>:</th>
<td>
<input name="vivio_swift_remove_query_strings" type="checkbox"<?php if($vivio_swift_global->configs->get_value('vivio_swift_remove_query_strings')=='1') echo ' checked="checked"'; ?> value="1"/>
<span class="description"><?php _e('Check this to remove version number query strings from static assets so they can be cached by more systems.', 'vivio-swift'); ?></span>
<label class="switch">
<input name="vivio_swift_cache_enabled" type="checkbox"<?php if($vivio_swift_global->configs->get_value('vivio_swift_cache_enabled')=='1') echo ' checked="checked"'; ?> value="1"/>
<span class="slider round"></span>
</label>
<span class="description"><?php _e('Enable caching using Vivio Swift.', 'vivio-swift'); ?></span>
</td>
</tr>
</table>
<input type="submit" name="vivio_swift_save_cache_settings" value="<?php _e('Save Settings', 'vivio-swift')?>" class="button-primary" />
<input type="submit" name="vivio_swift_save_cache_settings" value="<?php _e('Save Settings', 'vivio-swift')?>" class="button-secondary" />
</form>
</div>
</div>
</div><!-- End of Site Info -->
</div><!-- End of Cache Settings -->
<?php
}
function render_tab2()
{
global $vivio_swift_global;
// process advanced settings
if (isset($_POST['vivio_swift_save_advanced_cache_settings']))
{
$error = '';
// verify nonce
$nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($nonce, 'vivio_swift_advanced_settings_nonce')) {
$vivio_swift_global->debug_logger->log_debug("Nonce check failed in advanced settings.", 4);
wp_die("Error: Nonce check failed in advanced settings.");
}
$vivio_swift_preload_cache_expire_minutes_val = sanitize_text_field($_POST['vivio_swift_preload_cache_expire_minutes']);
if(!is_numeric($vivio_swift_preload_cache_expire_minutes_val))
{
$error .= '<br />'.__('You entered a non numeric value for the cache expires field. It has been set to the default value.','vivio-swift');
$vivio_swift_preload_cache_expire_minutes_val = '60';//Set it to the default value for this field
} elseif ($vivio_swift_preload_cache_expire_minutes_val < 60)
{
$error .= '<br />'.__('The Cache Expires field cannot have a value of less than 60. It has been set to the default value.','vivio-swift');
$vivio_swift_preload_cache_expire_minutes_val = '60';//Set it to the default value for this field
}
if($error)
{
$this->show_msg_error(__('Attention!','vivio-swift').$error);
}
// set and save submitted settings
$vivio_swift_global->configs->set_value('vivio_swift_remove_query_strings',isset($_POST["vivio_swift_remove_query_strings"])?'1':'');
$vivio_swift_global->configs->set_value('vivio_swift_preload_cache_enabled',isset($_POST["vivio_swift_preload_cache_enabled"])?'1':'');
$vivio_swift_global->configs->set_value('vivio_swift_preload_cache_expire_minutes',absint($vivio_swift_preload_cache_expire_minutes_val));
$vivio_swift_global->configs->save_config();
$this->show_msg_settings_updated();
}
?>
<div id="vivio-swift-container">
<form action=""
id="vivio_swift_cache_advanced_settings"
method="POST">
<?php wp_nonce_field('vivio_swift_advanced_settings_nonce'); ?>
<div class="postbox">
<h3 class="hndle">
<label for="title"><?php _e('Preload Cache', 'vivio-swift');?></label>
</h3>
<div class="inside">
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('Enable Preload Cache', 'vivio-swift')?>:</th>
<td>
<label class="switch">
<input name="vivio_swift_preload_cache_enabled"
id="vivio_swift_preload_cache_enabled"
type="checkbox"<?php if($vivio_swift_global->configs->get_value('vivio_swift_preload_cache_enabled')=='1') echo ' checked="checked"'; ?> value="1"/>
<span class="slider round"></span>
</label>
<span class="description"><?php _e("Enable a process that will cache your site at regular intervals (based on when they're set to expire).", 'vivio-swift'); ?></span>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Cache Expires', 'vivio-swift')?>:</th>
<td>
<input type="text" size="6"
id="vivio_swift_preload_cache_expire_minutes"
name="vivio_swift_preload_cache_expire_minutes"
value="<?php echo $vivio_swift_global->configs->get_value('vivio_swift_preload_cache_expire_minutes'); ?>"
/>
<span class="description"><?php _e('Number of minutes until the Preload Cache expires and is rebuilt.', 'vivio-swift'); ?></span>
</td>
</tr>
</table>
<input type="submit" name="vivio_swift_save_advanced_cache_settings" value="<?php _e('Save Settings', 'vivio-swift')?>" class="button-secondary" />
</div>
</div>
<div class="postbox">
<h3 class="hndle">
<label for="title"><?php _e('Advanced Settings', 'vivio-swift');?></label>
</h3>
<div class="inside">
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('Remove Query Strings', 'vivio-swift')?>:</th>
<td>
<label class="switch">
<input name="vivio_swift_remove_query_strings"
id="vivio_swift_remove_query_strings"
type="checkbox"<?php if($vivio_swift_global->configs->get_value('vivio_swift_remove_query_strings')=='1') echo ' checked="checked"'; ?> value="1"/>
<span class="slider round"></span>
</label>
<span class="description"><?php _e('Remove version number query strings from static assets so they can be cached more easily.', 'vivio-swift'); ?></span>
</td>
</tr>
</table>
<input type="submit" name="vivio_swift_save_advanced_cache_settings" value="<?php _e('Save Settings', 'vivio-swift')?>" class="button-secondary" />
</div>
</div>
</form>
</div><!-- End of Extra Settings -->
<?php
}
function render_tab3()
{
global $vivio_swift_global;
?>
<div id="vivio-swift-container">
function render_tab2()
<div class="postbox">
<h3 class="hndle">
<label for="title"><?php _e('Exclusions', 'vivio-swift');?></label>
</h3>
<div class="inside">
</div>
</div>
</div><!-- End of Exclusions -->
<?php
}
function render_tab4()
{
global $vivio_swift_global;
......@@ -137,7 +275,7 @@ class Vivio_Swift_Cache_Menu extends Vivio_Swift_Admin_Messages
<div class="inside">
</div>
</div>
</div><!-- End of Site Info -->
</div><!-- End of Reports -->
<?php
}
......
......@@ -328,6 +328,7 @@ class Vivio_Swift_Dashboard_Menu extends Vivio_Swift_Admin_Messages
// set and save debug settings
$vivio_swift_global->configs->set_value('vivio_swift_enable_debug',isset($_POST["vivio_swift_enable_debug"])?'1':'');
$vivio_swift_global->configs->save_config();
}
?>
......@@ -339,18 +340,25 @@ class Vivio_Swift_Dashboard_Menu extends Vivio_Swift_Admin_Messages
</h3>
<div class="inside">
<form action="" method="POST">
<form action=""
id="vivio_swift_dashboard_debug_settings"
method="POST">
<?php wp_nonce_field('vivio-swift-debug-settings-nonce'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('Enable Debug Logging', 'vivio-swift')?>:</th>
<td>
<input name="vivio_swift_enable_debug" type="checkbox"<?php if($vivio_swift_global->configs->get_value('vivio_swift_enable_debug')=='1') echo ' checked="checked"'; ?> value="1"/>
<span class="description"><?php _e('Check this to enable certain plugin actions to be logged and viewable in the Vivio Swift Log Viewer.', 'vivio-swift'); ?></span>
<label class="switch">
<input name="vivio_swift_enable_debug"
id="vivio_swift_enable_debug"
type="checkbox"<?php if($vivio_swift_global->configs->get_value('vivio_swift_enable_debug')=='1') echo ' checked="checked"'; ?> value="1"/>
<span class="slider round"></span>
</label>
<span class="description"><?php _e('Enable certain actions to be logged and viewable in the Vivio Swift Log Viewer.', 'vivio-swift'); ?></span>
</td>
</tr>
</table>
<input type="submit" name="vivio_swift_save_debug_settings" value="<?php _e('Save Settings', 'vivio-swift')?>" class="button-primary" />
<input type="submit" name="vivio_swift_save_debug_settings" value="<?php _e('Save Settings', 'vivio-swift')?>" class="button-secondary" />
</form>
</div>
......@@ -361,7 +369,9 @@ class Vivio_Swift_Dashboard_Menu extends Vivio_Swift_Admin_Messages
</h3>
<div class="inside">
<form action="" method="POST">
<form action=""
id="vivio_swift_dashboard_logs"
method="POST">
<?php wp_nonce_field('vivio-swift-logs-nonce'); ?>
<table class="form-table">
<tr valign="top">
......@@ -386,7 +396,7 @@ class Vivio_Swift_Dashboard_Menu extends Vivio_Swift_Admin_Messages
</table>
<input type="submit" name="vivio_swift_view_logs"
value="<?php _e('View Logs', 'vivio-swift')?>"
class="button-primary"/>
class="button-secondary"/>
</form>
</div>
</div>
......
......@@ -11,9 +11,8 @@ class Vivio_Swift_Activator{
// hold-off on multi-site support for now.
}
// run activation processes
// set default configs
Vivio_Swift_Config_Settings::add_value_configs();
error_log("[vivio-swift] options saved.", 0);
}
}
......@@ -7,24 +7,12 @@ class Vivio_Swift_Cache
$this->remove_query_strings();
}
/**
* filters query strings with versions starting with a question mark
*
* @param $src - script loader source path
* @return $rqs - source path with query strings removed
*/
function remove_query_strings_qmark($src)
{
$rqs = explode('?ver', $src);
return $rqs[0];
}
/**
* filters query strings with versions starting with an ampersand
*
* @param $src - script loader source path
* @return $rqs - source path with query strings removed
*/
function remove_query_strings_amp($src)
{
$rqs = explode('&ver', $src);
......@@ -35,12 +23,257 @@ class Vivio_Swift_Cache
{
global $vivio_swift_global;
if( ($vivio_swift_global->configs->get_value('vivio_swift_remove_query_strings')=='1') && (! is_admin()) )
{
if( ($vivio_swift_global->configs->get_value('vivio_swift_remove_query_strings')=='1') && (! is_admin()) ){
add_filter( 'script_loader_src', array(&$this,'remove_query_strings_qmark'), 15, 1 );
add_filter( 'style_loader_src', array(&$this,'remove_query_strings_qmark'), 15, 1 );
add_filter( 'script_loader_src', array(&$this,'remove_query_strings_amp'), 15, 1 );
add_filter( 'style_loader_src', array(&$this,'remove_query_strings_amp'), 15, 1 );
}
}
function create_preload_cache()
{
global $vivio_swift_global;
global $wpdb;
$urls = array();
if ($vivio_swift_global->configs->get_value('vivio_swift_cache_enabled')=='1'){
// TODO Add option to select mobile cache as well
// TODO Add options to select what we want to cache. For now, CACHE ALL THE THINGS
// home page URI
array_push($urls, array("url" => get_option("home")));
// log it
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::create_preload_cache() - Added HOME URI: ".get_option("home"), 0);
}
// post URI's
$posts = get_posts(array('post_type'=>'post','numberposts'=>'-1','post_status'=>'publish'));
if (count($posts) > 0){
foreach ($posts as $p){
if ($post_permalink = get_permalink($p)){
// if we're using permalinks, use the permalink uri
array_push($urls, $post_permalink);
// log it
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::create_preload_cache() - Added POST URI: ".$post_permalink, 0);
}
} else {
// otherwise just log it as incompatible
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::create_preload_cache() - Failed to get Permalink for Post ID:".$p->ID."[SKIPPING]", 4);
}
}
} // end foreach
}
// attachment URI's
$posts = get_posts(array('post_type'=>'attachment','numberposts'=>'-1','post_status'=>'publish','post_parent' => null));
if (count($posts) > 0){
foreach ($posts as $p){
if ($post_permalink = get_permalink($p)){
// if we're using permalinks, use the permalink uri
array_push($urls, $post_permalink);
// log it
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::create_preload_cache() - Added ATTACHMENT URI: ".$post_permalink, 0);
}
} else {
// otherwise just log it as incompatible
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::create_preload_cache() - Failed to get Permalink for Post ID:".$p->ID."[SKIPPING]", 4);
}
}
} // end foreach
}
// page URI's
$posts = get_posts(array('post_type'=>'page','numberposts'=>'-1','post_status'=>'publish'));
if (count($posts) > 0){
foreach ($posts as $p){
if ($post_permalink = get_permalink($p)){
// if we're using permalinks, use the permalink uri
array_push($urls, $post_permalink);
// log it
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::create_preload_cache() - Added PAGE URI: ".$post_permalink, 0);
}
} else {
// otherwise just log it as incompatible
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::create_preload_cache() - Failed to get Permalink for Post ID:".$p->ID."[SKIPPING]", 4);
}
}
} // end foreach
}
// Category URI's
$categories = get_categories( array(
'orderby' => 'id',
'order' => 'ASC'
) );
foreach ($categories as $cat){
$cat_link = get_category_link( $cat->term_id );
// save the link
array_push($urls, $cat_link);
// log it
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::create_preload_cache() - Added PAGE URI: ".$cat_link, 0);
}
}
// Tag URI's
$tags = get_tags();
foreach ($tags as $tag){
$tag_link = get_tag_link( $tag->term_id );
// save the link
array_push($urls, $tag_link);
// log it
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::create_preload_cache() - Added PAGE URI: ".$tag_link, 0);
}
}
// Send URLs off to caching process
foreach ($urls as $url){
error_log("[vivio-swift] hitting cache_url_with_agent(".$url.", ".VIVIO_SWIFT_CACHE_BOT_NAME.")...", 0);
$this->cache_url_with_agent($url, VIVIO_SWIFT_CACHE_BOT_NAME);
}
}
} // end create_preload_cache
function cache_url_with_agent($url, $agent)
{
$args = array(
'timeout' => 10,
'sslverify' => false,
'user-agent' => $agent,
'headers' => array(
'cache-control' => array(
'no-store, no-cache, must-revalidate',
'post-check=0, pre-check=0'
)
)
);
// can't do local url's
// $response = wp_remote_get($url, $args);
// pull the uri with wp_remote_get
error_log("[Vivio_Swift] is error? ".is_wp_error($response));
//$response = wp_remote_get($url, array('timeout' => 10, 'sslverify' => false, 'headers' => array("cache-control" => array("no-store, no-cache, must-revalidate", "post-check=0, pre-check=0"),'user-agent' => $agent)));
/*
$response = wp_remote_get($url, array(
'timeout' => 10,
'sslverify' => false,
'headers' => array(
'cache-control' => array(
'no-store, no-cache, must-revalidate',
'post-check=0, pre-check=0'
)
),
'user-agent' => $agent
));
*/
/*
// check the response
if (!$response || is_wp_error($response)){
// no response from wp_remote_get()
// log the problem
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::cache_url_with_agent() - No response when attempting to cache URL:".$url."[SKIPPING]", 4);
}
} elseif ( is_wp_error($response) ){
// received error object from wp_remote_get()
// log the problem
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::cache_url_with_agent() - Received error response object when attempting to cache URL:".$url."[SKIPPING]", 4);
$vivio_swift_global->debug_logger->log_debug("Error codes:".$response->get_error_codes(), 4);
$vivio_swift_global->debug_logger->log_debug("Error message:".$response->get_error_message(), 4);
}
} elseif ( wp_remote_retrieve_response_code($response) != 200 ){
// received bad response code
// log the problem
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::cache_url_with_agent() - Received bad response code when attempting to cache URL:".$url."[SKIPPING]", 4);
$vivio_swift_global->debug_logger->log_debug("Response code:".wp_remote_retrieve_response_code($response), 4);
}
} else {
// all good cache the page
if ( $uri = $this->parse_uri($url) ){
// failed to parse URI
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::cache_url_with_agent() - Failed to parse:".$url."[SKIPPING]", 4);
}
} else {
// send page off to save util
$this->save_cached_page($uri, wp_remote_retrieve_body($response));
}
// for now, dump a URL list in the logs
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::cache_url_with_agent() - Successfully cached:".$url, 0);
}
}
*/
}
function parse_uri($url)
{
// parse the URI (the directories) out of a URL (full URL with FQD)
if ( $arr_parsed_url = parse_url($url) ){
return $arr_parsed_url;
}
else {
return false;
}
}
function save_cached_page($uri, $body)
{
$fullpath = VIVIO_SWIFT_CACHE_PATH.'/'.$uri;
// create directory if it doesn't already exist
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::save_cached_page() - attempting to create directory:".$fullpath, 1);
}
if ( Vivio_Swift_Utility_File::create_dir($fullpath) ){
if ($debug){
$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache::save_cached_page() - failed to create directory:".$fullpath, 4);
}
} else {