Router works for POST requests.

This commit is contained in:
marco
2015-08-16 20:21:34 +02:00
parent d8fbdd82bc
commit 60919e6171
4 changed files with 40 additions and 36 deletions

View File

@@ -1,6 +1,7 @@
<?php <?php
namespace MailPoet\Config; namespace MailPoet\Config;
use MailPoet\Models; use MailPoet\Models;
use MailPoet\Router;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
@@ -18,6 +19,7 @@ class Initializer {
$this->setupRenderer(); $this->setupRenderer();
$this->setupLocalizer(); $this->setupLocalizer();
$this->setupMenu(); $this->setupMenu();
$this->setupRouter();
} }
function setupDB() { function setupDB() {
@@ -54,4 +56,9 @@ class Initializer {
); );
$menu->init(); $menu->init();
} }
function setupRouter() {
$router = new Router\Router();
$router->init();
}
} }

View File

@@ -8,29 +8,36 @@ class Router {
} }
function init() { function init() {
add_action(
'admin_head',
array($this, 'setToken')
);
add_action( add_action(
'wp_ajax_mailpoet', 'wp_ajax_mailpoet',
array($this, 'setup') array($this, 'setup')
); );
$this->setToken();
} }
function setup() { function setup() {
$this->securityCheck(); $this->securityCheck();
$endpoint = ucfirst($_POST['endpoint']); $class = ucfirst($_POST['endpoint']);
$action = $_POST['action']; $endpoint = __NAMESPACE__ . "\\" . $class;
$method = $_POST['method'];
$args = $_POST['args']; $args = $_POST['args'];
$route = new $endpoint(); $endpoint = new $endpoint();
$route->$action($args); $endpoint->$method(json_encode($args));
} }
function setToken() { function setToken() {
$token = wp_create_nonce('mailpoet_token'); $token = wp_create_nonce('mailpoet_token');
wp_localize_script($token); $global = '<script type="text/javascript">';
$global .= 'var mailpoet_token = "' . $token . '";';
$global .= "</script>/n";
echo $global;
} }
function securityCheck() { function securityCheck() {
if (!current_user_can('manage_options')) {die();} if (!current_user_can('manage_options')) {die();}
if (!wp_verify_nonce($_POST['token'])) {die();} if (!wp_verify_nonce($_POST['token'], 'mailpoet_token')) {die();}
} }
} }

View File

@@ -13,6 +13,6 @@ class Settings {
'last_name' => 'Mailer', 'last_name' => 'Mailer',
'email' => 'john@mailpoet.com' 'email' => 'john@mailpoet.com'
); );
echo json_encode($data); wp_send_json($params);
} }
} }

View File

@@ -32,34 +32,24 @@
</p> </p>
</form> </form>
<script type="text/javascript"> <script type="text/javascript">
jQuery(function($) { jQuery(document).ready(function($) {
$.ajax({
function loadSettings() { url: ajaxurl,
var data = { type: 'post',
'endpoint': 'settings', data: {
'action': 'get', action: 'mailpoet',
'args': '' token: mailpoet_token,
}; endpoint: 'settings',
method: 'get',
$.post(ajaxurl, data, function(response) { args: {
alert('Server response from the AJAX URL ' + response); first_name: 'John'
}); }
},
success : function(response) {
console.log(response);
} }
function saveSettings() {
console.log('save settings...');
return false;
}
// save settings on form submission
$(document).on('submit', '#mailpoet_settings', saveSettings);
// on dom loaded
$(function() {
loadSettings();
});
}); });
</script> });
</script>
<% endblock %> <% endblock %>