Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function calcolo(array $data)
- {
- $math = array();
- // ... make math.
- return $math;
- }
- function retrieveData()
- {
- // Prevent tainted data.
- $data = array();
- // Data doesn't exists? Just return empty array.
- // Nothing else to do.
- if(!isset($_POST['data']) {
- return array();
- }
- // Parse Data.
- parse_str($_POST['data'], $data);
- // See http://php.net/manual/it/function.filter-var.php
- // See http://php.net/manual/it/function.filter-var-array.php
- // See http://php.net/manual/it/filter.filters.sanitize.php
- $data = filter_var_array($data[$key], array(
- // Sanitize Costo, but I'll use https://github.com/moneyphp/money
- // We are talking about money afterall.
- 'costo' => FILTER_SANITIZE_NUMBER_FLOAT,
- 'ore' => FILTER_SANITIZE_NUMBER_INT,
- 'giorni' => FILTER_SANITIZE_NUMBER_INT,
- 'potenza' => FILTER_SANITIZE_STRING,
- 'luce' => FILTER_SANITIZE_STRING,
- ));
- return $data;
- }
- function makeHTML(array $data)
- {
- $html = '';
- // ... create the HTML output to send to `htmlSend()`
- return $html;
- }
- function htmlSend($html)
- {
- $data = array('html' => $html);
- // If empty html
- if('' === $html) {
- // @see https://codex.wordpress.org/Function_Reference/wp_send_json_error
- wp_send_json_error($data);
- }
- // @see https://codex.wordpress.org/Function_Reference/wp_send_json_success
- wp_send_json_success($data);
- }
- function checkAjaxRequest()
- {
- // Verify request.
- // See https://codex.wordpress.org/Function_Reference/check_ajax_referer
- // Will die if not verified.
- check_ajax_referer('my-special-string', 'security', true);
- }
- /**
- * Controller
- *
- * This manage the request and send the output via json.
- */
- function controller()
- {
- // Do nothing if not an ajax request.
- if(!wp_doing_ajax()) {
- return;
- }
- // Will die if the nonce has not been verified.
- checkAjaxRequest();
- $output = '';
- $data = retrieveData();
- if($data) {
- $output = makeHTML(calcolo($data));
- }
- // Then send the output to `htmlSend` that will send it wrapped into a json.
- htmlSend($output);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement