Skip to content

Commit

Permalink
Updated plugin to use admin-ajax.php for Ajax calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Ems committed Feb 3, 2013
1 parent e5de7f8 commit e5f4594
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 53 deletions.
26 changes: 0 additions & 26 deletions admin/js/metabox.php

This file was deleted.

21 changes: 9 additions & 12 deletions admin/templates/metabox/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@

$('#<?php echo $metabox['id']; ?> .handlediv').append( loading );
$('#<?php echo $metabox['id']; ?> .handlediv .loading').fadeIn('fast');
$.ajax({
type: 'post',
url: '<?php echo parse_url((( $this->getPlugin()->isSsl() ) ? $this->getPlugin()->makeUrlHttps($this->getPlugin()->getPluginUrl()) : $this->getPlugin()->getPluginUrl()), PHP_URL_PATH); ?>/admin/js/metabox.php',
data: {
id : '<?php echo $metabox['id']; ?>',
url : '<?php echo $metabox['args']['url']; ?>',
_nonce : '<?php echo $nonce; ?>'
},
success: function(response) {
$('#<?php echo $metabox['id']; ?> .inside').html(response);
$('#<?php echo $metabox['id']; ?> .handlediv .loading').fadeIn(0).fadeOut('fast');
}

$.post(ajaxurl, {
action : '<?php echo $this->getPlugin()->getSlug(); ?>_ajax_metabox',
id : '<?php echo $metabox['id']; ?>',
url : '<?php echo $metabox['args']['url']; ?>',
_nonce : '<?php echo $nonce; ?>'
}, function(response) {
$('#<?php echo $metabox['id']; ?> .inside').html(response);
$('#<?php echo $metabox['id']; ?> .handlediv .loading').fadeIn(0).fadeOut('fast');
});
});
</script>
15 changes: 11 additions & 4 deletions admin/templates/metabox/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
</tr>
</table>

<input type="hidden" name="action" value="wphttps-settings" />
<input type="hidden" name="ssl_host_subdomain" value="<?php echo (($this->getPlugin()->getSetting('ssl_host_subdomain') != 1) ? 0 : 1); ?>" />
<input type="hidden" name="ssl_host_diff" value="<?php echo (($this->getPlugin()->getSetting('ssl_host_diff') != 1) ? 0 : 1); ?>" />

Expand All @@ -112,14 +111,22 @@
</form>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#<?php echo $this->getPlugin()->getSlug(); ?>_settings_form').submit(function() {
$('#<?php echo $this->getPlugin()->getSlug(); ?>_settings_form').submit(function(e) {
e.preventDefault();
$('#<?php echo $this->getPlugin()->getSlug(); ?>_settings_form input[name="action"]').val('<?php echo $this->getPlugin()->getSlug(); ?>_settings');
$('#<?php echo $this->getPlugin()->getSlug(); ?>_settings_form .submit-waiting').show();
}).ajaxForm({
$.post(ajaxurl, $('#<?php echo $this->getPlugin()->getSlug(); ?>_settings_form').serialize(), function(response) {
$('#<?php echo $this->getPlugin()->getSlug(); ?>_settings_form .submit-waiting').hide();
$('#message-body').html(response).fadeOut(0).fadeIn().delay(5000).fadeOut();
});
});

/*.ajaxForm({
success: function(responseText, textStatus, XMLHttpRequest) {
$('#<?php echo $this->getPlugin()->getSlug(); ?>_settings_form .submit-waiting').hide();
$('#message-body').html(responseText).fadeOut(0).fadeIn().delay(5000).fadeOut();
}
});
});*/

$('#settings-reset').click(function(e, el) {
if ( ! confirm('<?php _e('Are you sure you want to reset all WordPress HTTPS settings?','wordpress-https'); ?>') ) {
Expand Down
52 changes: 41 additions & 11 deletions lib/WordPressHTTPS/Module/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ class WordPressHTTPS_Module_Settings extends Mvied_Plugin_Module {
* @return void
*/
public function init() {
if ( is_admin() && isset($_GET['page']) && strpos($_GET['page'], $this->getPlugin()->getSlug()) !== false ) {
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] == 'wphttps-settings' ) {
add_action('plugins_loaded', array(&$this, 'save'), 1);
}
if ( is_admin() ) {
add_action('wp_ajax_' . $this->getPlugin()->getSlug() . '_settings', array(&$this, 'save'));
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
add_action('admin_init', array(&$this, 'add_meta_boxes'));

// Add meta boxes
add_action('admin_init', array(&$this, 'add_meta_boxes'));
// Add scripts
add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts'));
}

// Add scripts
add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts'));
// Add admin menus
add_action('admin_menu', array(&$this, 'admin_menu'));
}

// Add admin menus
add_action('admin_menu', array(&$this, 'admin_menu'));
}

/**
Expand Down Expand Up @@ -126,6 +126,36 @@ public function dispatch() {
self::render();
}

/**
* Dispatch request for ajax metabox
*
* @param none
* @return void
*/
public function ajax_metabox() {
// Disable errors
error_reporting(0);

// Set headers
header("Status: 200");
header("HTTP/1.1 200 OK");
header('Content-Type: text/html');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
header("Vary: Accept-Encoding");

if ( ! wp_verify_nonce($_POST['_nonce'], 'wordpress-https') ) {
exit;
}

$content = WordPressHTTPS_Url::fromString( $_POST['url'] )->getContent();
if ( $content ) {
echo $content;
}
exit;
}

/**
* Adds javascript and stylesheets to settings page in the admin panel.
* WordPress Hook - enqueue_scripts
Expand Down

0 comments on commit e5f4594

Please sign in to comment.