Router works for POST requests.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace MailPoet\Config;
|
||||
use MailPoet\Models;
|
||||
use MailPoet\Router;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
@@ -18,6 +19,7 @@ class Initializer {
|
||||
$this->setupRenderer();
|
||||
$this->setupLocalizer();
|
||||
$this->setupMenu();
|
||||
$this->setupRouter();
|
||||
}
|
||||
|
||||
function setupDB() {
|
||||
@@ -54,4 +56,9 @@ class Initializer {
|
||||
);
|
||||
$menu->init();
|
||||
}
|
||||
|
||||
function setupRouter() {
|
||||
$router = new Router\Router();
|
||||
$router->init();
|
||||
}
|
||||
}
|
||||
|
@@ -8,29 +8,36 @@ class Router {
|
||||
}
|
||||
|
||||
function init() {
|
||||
add_action(
|
||||
'admin_head',
|
||||
array($this, 'setToken')
|
||||
);
|
||||
add_action(
|
||||
'wp_ajax_mailpoet',
|
||||
array($this, 'setup')
|
||||
);
|
||||
$this->setToken();
|
||||
}
|
||||
|
||||
function setup() {
|
||||
$this->securityCheck();
|
||||
$endpoint = ucfirst($_POST['endpoint']);
|
||||
$action = $_POST['action'];
|
||||
$class = ucfirst($_POST['endpoint']);
|
||||
$endpoint = __NAMESPACE__ . "\\" . $class;
|
||||
$method = $_POST['method'];
|
||||
$args = $_POST['args'];
|
||||
$route = new $endpoint();
|
||||
$route->$action($args);
|
||||
$endpoint = new $endpoint();
|
||||
$endpoint->$method(json_encode($args));
|
||||
}
|
||||
|
||||
function setToken() {
|
||||
$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() {
|
||||
if (!current_user_can('manage_options')) {die();}
|
||||
if (!wp_verify_nonce($_POST['token'])) {die();}
|
||||
if (!wp_verify_nonce($_POST['token'], 'mailpoet_token')) {die();}
|
||||
}
|
||||
}
|
||||
|
@@ -13,6 +13,6 @@ class Settings {
|
||||
'last_name' => 'Mailer',
|
||||
'email' => 'john@mailpoet.com'
|
||||
);
|
||||
echo json_encode($data);
|
||||
wp_send_json($params);
|
||||
}
|
||||
}
|
||||
|
@@ -32,34 +32,24 @@
|
||||
</p>
|
||||
</form>
|
||||
|
||||
<script type="text/javascript">
|
||||
jQuery(function($) {
|
||||
|
||||
function loadSettings() {
|
||||
var data = {
|
||||
'endpoint': 'settings',
|
||||
'action': 'get',
|
||||
'args': ''
|
||||
};
|
||||
|
||||
$.post(ajaxurl, data, function(response) {
|
||||
alert('Server response from the AJAX URL ' + response);
|
||||
});
|
||||
<script type="text/javascript">
|
||||
jQuery(document).ready(function($) {
|
||||
$.ajax({
|
||||
url: ajaxurl,
|
||||
type: 'post',
|
||||
data: {
|
||||
action: 'mailpoet',
|
||||
token: mailpoet_token,
|
||||
endpoint: 'settings',
|
||||
method: 'get',
|
||||
args: {
|
||||
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 %>
|
||||
|
Reference in New Issue
Block a user