Browse files

Added URL path caching to reduce queries.

  • Loading branch information...
1 parent 3082257 commit 929141f21643dc672d0ccf014d9fb77c5677a68f @mvied committed Mar 30, 2013
View
1 css/settings.css
@@ -88,6 +88,7 @@
float: left;
}
#wphttps-main #settings-reset,
+#wphttps-main #settings-cache-reset,
#wphttps-main #network-settings-reset,
#wphttps-main #filters-reset,
#wphttps-main #domain-mapping-reset {
View
2 lib/WordPressHTTPS.php
@@ -45,6 +45,8 @@ class WordPressHTTPS extends Mvied_Plugin_Modular {
'secure_filter' => array(), // Expressions to secure URL's against
'ssl_host_mapping' => array(), // External SSL Hosts whose HTTPS content is on another domain
'network_defaults' => array(), // Default settings for new blogs on a multisite network
+ 'path_cache' => array(), // Cache of URL paths to Post IDs
+ 'blog_cache' => array(), // Cache of URL paths to Blog IDs
'version' => '', // Version of the plugin this blog has installed
);
View
59 lib/WordPressHTTPS/Module/Parser.php
@@ -291,6 +291,9 @@ public function fixLinksAndForms() {
// Update anchor and form tags to appropriate URL's
preg_match_all('/\<(a|form)[^>]+[\'"]((http|https):\/\/[^\'"]+)[\'"][^>]*>/im', $this->_html, $matches);
+ $path_cache = $this->getPlugin()->getSetting('path_cache');
+ $blog_cache = $this->getPlugin()->getSetting('blog_cache');
+
for ($i = 0; $i < sizeof($matches[0]); $i++) {
$html = $matches[0][$i];
$type = $matches[1][$i];
@@ -300,7 +303,9 @@ public function fixLinksAndForms() {
$post_id = null;
$blog_id = null;
$force_ssl = null;
- $url_path = '/';
+ $url_path = null;
+ $blog_path = null;
+ $blog_url_path = '/';
if ( !$this->getPlugin()->isUrlLocal($url) ) {
continue;
@@ -332,23 +337,40 @@ public function fixLinksAndForms() {
if ( get_option('show_on_front') == 'page' ) {
$post_id = get_option('page_on_front');
}
- } else if ( isset($url_parts['path']) && ($post = get_page_by_path($url_parts['path'])) ) {
- $post_id = $post->ID;
+ } else if ( isset($url_parts['path']) ) {
+ $url_path = $url_parts['path'];
+ if ( !array_key_exists($url_path, $path_cache) ) {
+ if ( $post = get_page_by_path($url_path) ) {
+ $post_id = $post->ID;
+ $path_cache[$url_path] = $post_id;
+ } else {
+ $path_cache[$url_path] = 0;
+ }
+ } else {
+ $post_id = $path_cache[$url_path];
+ }
}
if ( is_multisite() && isset($url_parts['host']) ) {
if ( is_subdomain_install() ) {
- $blog_id = get_blog_id_from_url( $url_parts['host'], '/');
+ $blog_path = $url_parts['host'] . '/';
+ if ( array_key_exists($blog_path, $blog_cache) ) {
+ $blog_id = $blog_cache[$blog_path];
+ } else {
+ $blog_id = get_blog_id_from_url( $url_parts['host'], '/');
+ }
} else {
$url_path_segments = explode('/', $url_parts['path']);
if ( sizeof($url_path_segments) > 1 ) {
foreach( $url_path_segments as $url_path_segment ) {
if ( is_null($blog_id) && $url_path_segment != '' ) {
- $url_path .= $url_path_segment . '/';
- if ( ($blog_id = get_blog_id_from_url( $url_parts['host'], $url_path)) > 0 ) {
- break;
+ $blog_url_path .= $url_path_segment . '/';
+ $blog_path = $url_parts['host'] . $blog_url_path;
+ if ( array_key_exists($blog_path, $blog_cache) ) {
+ $blog_id = $blog_cache[$blog_path];
} else {
- $blog_id = null;
+ $blog_id = $blog_cache[$blog_path] = get_blog_id_from_url( $url_parts['host'], $blog_url_path);
+ break;
}
}
}
@@ -383,30 +405,11 @@ public function fixLinksAndForms() {
if ( $force_ssl == true ) {
if ( is_null($blog_id) ) {
$updated = $this->getPlugin()->makeUrlHttps($url);
- } else {
- if ( $this->getPlugin()->getSetting('ssl_host', $blog_id) ) {
- $ssl_host = $this->getPlugin()->getSetting('ssl_host', $blog_id);
- } else {
- $ssl_host = parse_url(get_home_url($blog_id, '/'), PHP_URL_HOST);
- }
- if ( is_subdomain_install() ) {
- $host = $url_parts['host'] . '/';
- } else {
- $host = $url_parts['host'] . '/' . $url_path;
- }
- $updated = str_replace($url_parts['scheme'] . '://' . $host, $ssl_host, $url);
}
$this->_html = str_replace($html, str_replace($url, $updated, $html), $this->_html);
} else if ( !is_null($force_ssl) && !$force_ssl ) {
if ( is_null($blog_id) ) {
$updated = $this->getPlugin()->makeUrlHttp($url);
- } else {
- if ( is_subdomain_install() ) {
- $host = $url_parts['host'] . '/';
- } else {
- $host = $url_parts['host'] . '/' . $url_path;
- }
- $updated = str_replace($url_parts['scheme'] . '://' . $host, get_home_url($blog_id, '/'), $url);
}
$this->_html = str_replace($html, str_replace($url, $updated, $html), $this->_html);
}
@@ -419,6 +422,8 @@ public function fixLinksAndForms() {
}
}
}
+ $this->getPlugin()->setSetting('path_cache', $path_cache);
+ $this->getPlugin()->setSetting('blog_cache', $blog_cache);
}
/**
View
24 lib/WordPressHTTPS/Module/Settings.php
@@ -21,6 +21,7 @@ public function init() {
if ( is_admin() ) {
add_action('wp_ajax_' . $this->getPlugin()->getSlug() . '_settings_save', array(&$this, 'save'));
add_action('wp_ajax_' . $this->getPlugin()->getSlug() . '_settings_reset', array(&$this, 'reset'));
+ add_action('wp_ajax_' . $this->getPlugin()->getSlug() . '_settings_cache_reset', array(&$this, 'cache_reset'));
add_action('wp_ajax_' . $this->getPlugin()->getSlug() . '_ajax_metabox', array(&$this, 'ajax_metabox'));
if ( isset($_GET['page']) && strpos($_GET['page'], $this->getPlugin()->getSlug()) !== false ) {
// Add meta boxes
@@ -185,6 +186,29 @@ public function render() {
}
/**
+ * Reset Cache
+ *
+ * @param none
+ * @return void
+ */
+ public function cache_reset() {
+ if ( !wp_verify_nonce($_POST['_wpnonce'], $this->getPlugin()->getSlug()) ) {
+ return false;
+ }
+
+ $message = _e('Cache reset.','wordpress-https');
+ $errors = array();
+ $reload = false;
+
+ $this->getPlugin()->setSetting('secure_external_urls', array());
+ $this->getPlugin()->setSetting('unsecure_external_urls', array());
+ $this->getPlugin()->setSetting('path_cache', array());
+ $this->getPlugin()->setSetting('blog_cache', array());
+
+ $this->getPlugin()->renderView('ajax_message', array('message' => $message, 'errors' => $errors, 'reload' => $reload));
+ }
+
+ /**
* Reset Settings
*
* @param none
View
4 uninstall.php
@@ -23,7 +23,9 @@
'wordpress-https_debug',
'wordpress-https_admin_menu',
'wordpress-https_secure_filter',
- 'wordpress-https_ssl_host_mapping'
+ 'wordpress-https_ssl_host_mapping',
+ 'wordpress-https_path_cache',
+ 'wordpress-https_blog_cache',
);
global $wpdb;
View
4 view/metabox/settings.php
@@ -107,6 +107,7 @@
<p class="button-controls">
<input type="submit" name="settings-save" value="<?php _e('Save Changes','wordpress-https'); ?>" class="button-primary" id="settings-save" />
<input type="submit" name="settings-reset" value="<?php _e('Reset','wordpress-https'); ?>" class="button-secondary" id="settings-reset" />
+ <input type="submit" name="settings-cache-reset" value="<?php _e('Cache Reset','wordpress-https'); ?>" class="button-secondary" id="settings-cache-reset" />
<img alt="<?php _e('Waiting...','wordpress-https'); ?>" src="<?php echo admin_url('/images/wpspin_light.gif'); ?>" class="waiting submit-waiting" />
</p>
</form>
@@ -116,6 +117,9 @@
$('#settings-save').click(function() {
$(form).find('input[name="action"]').val('<?php echo $this->getSlug(); ?>_settings_save');
});
+ $('#settings-cache-reset').click(function() {
+ $(form).find('input[name="action"]').val('<?php echo $this->getSlug(); ?>_settings_cache_reset');
+ });
$('#settings-reset').click(function() {
$(form).find('input[name="action"]').val('<?php echo $this->getSlug(); ?>_settings_reset');
});

0 comments on commit 929141f

Please sign in to comment.