Permalink
Browse files

Updated plugin to use admin-ajax.php for Ajax calls.

  • Loading branch information...
1 parent e5de7f8 commit e5f4594d4d932711ebf3d5a3d5a9d8dac3c17315 @mvied committed Feb 3, 2013
View
@@ -1,26 +0,0 @@
-<?php
-
-require_once(realpath(dirname(__FILE__) . '/../../../../..') . '/wp-load.php');
-
-// 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;
-}
-?>
@@ -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>
@@ -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); ?>" />
@@ -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'); ?>') ) {
@@ -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'));
}
/**
@@ -127,6 +127,36 @@ public function dispatch() {
}
/**
+ * 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
*

0 comments on commit e5f4594

Please sign in to comment.