Advertisement
Guest User

Ajax Request

a guest
Nov 11th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.08 KB | None | 0 0
  1. <?php
  2. function calcolo(array $data)
  3. {
  4.     $math = array();
  5.  
  6.     // ... make math.
  7.  
  8.     return $math;
  9. }
  10.  
  11. function retrieveData()
  12. {
  13.     // Prevent tainted data.
  14.     $data = array();
  15.  
  16.     // Data doesn't exists? Just return empty array.
  17.     // Nothing else to do.
  18.     if(!isset($_POST['data']) {
  19.         return array();
  20.     }
  21.  
  22.     // Parse Data.
  23.     parse_str($_POST['data'], $data);
  24.  
  25.     // See http://php.net/manual/it/function.filter-var.php
  26.     // See http://php.net/manual/it/function.filter-var-array.php
  27.     // See http://php.net/manual/it/filter.filters.sanitize.php
  28.     $data = filter_var_array($data[$key], array(
  29.         // Sanitize Costo, but I'll use https://github.com/moneyphp/money
  30.         // We are talking about money afterall.
  31.         'costo'   => FILTER_SANITIZE_NUMBER_FLOAT,
  32.         'ore'     => FILTER_SANITIZE_NUMBER_INT,
  33.         'giorni'  => FILTER_SANITIZE_NUMBER_INT,
  34.         'potenza' => FILTER_SANITIZE_STRING,
  35.         'luce'    => FILTER_SANITIZE_STRING,
  36.     ));
  37.  
  38.     return $data;
  39. }
  40.  
  41. function makeHTML(array $data)
  42. {
  43.     $html = '';
  44.  
  45.     // ... create the HTML output to send to `htmlSend()`
  46.  
  47.     return $html;
  48. }
  49.  
  50. function htmlSend($html)
  51. {
  52.     $data = array('html' => $html);
  53.  
  54.     // If empty html
  55.     if('' === $html) {
  56.         // @see https://codex.wordpress.org/Function_Reference/wp_send_json_error
  57.         wp_send_json_error($data);
  58.     }
  59.  
  60.     // @see https://codex.wordpress.org/Function_Reference/wp_send_json_success
  61.     wp_send_json_success($data);
  62. }  
  63.  
  64. function checkAjaxRequest()
  65. {
  66.     // Verify request.
  67.     // See https://codex.wordpress.org/Function_Reference/check_ajax_referer
  68.     // Will die if not verified.
  69.     check_ajax_referer('my-special-string', 'security', true);
  70. }
  71.  
  72. /**
  73.  * Controller
  74.  *
  75.  * This manage the request and send the output via json.
  76.  */
  77. function controller()
  78. {
  79.     // Do nothing if not an ajax request.
  80.     if(!wp_doing_ajax()) {
  81.         return;
  82.     }
  83.  
  84.     // Will die if the nonce has not been verified.
  85.     checkAjaxRequest();
  86.  
  87.     $output = '';
  88.     $data   = retrieveData();
  89.  
  90.     if($data) {
  91.         $output = makeHTML(calcolo($data));
  92.     }
  93.  
  94.     // Then send the output to `htmlSend` that will send it wrapped into a json.
  95.     htmlSend($output);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement