Advertisement
stremblaym

Untitled

Sep 17th, 2023
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1. <?php
  2.    
  3. class helloChamp{
  4.    
  5.     function __construct(){
  6.        
  7.        
  8.         /*
  9.         * Ajax Requests
  10.         */
  11.         $this->ajax_requests();
  12.        
  13.     }
  14.    
  15.    
  16.     protected function ajax_requests(){
  17.        
  18.        
  19.         /*
  20.         * Send somethings
  21.         */
  22.         add_action('wp_ajax_save-something', 'save_something');
  23.         //add_action('wp_ajax_nopriv_save-something', 'save_something');
  24.         function save_something(){
  25.            
  26.             /*
  27.             * Prevent data you don't want
  28.             */
  29.             $data = $_POST;
  30.  
  31.             $keys = [
  32.                 'action',
  33.                 'key',
  34.                 'value',
  35.             ];
  36.  
  37.  
  38.             if(count($data) !== count($keys)) return;
  39.  
  40.             foreach($keys as $key){
  41.  
  42.                 if(!isset($data[$key]) || !$data[$key] || empty($data[$key])){
  43.                    
  44.                     echo wp_json_encode([
  45.                         'status' => 'error',
  46.                         'message' => 'Your data doesn\'t respect the requirements.'
  47.                     ]);
  48.                    
  49.                     exit;
  50.                 }
  51.  
  52.             }
  53.            
  54.             /*
  55.             * Now you sure you have the data you need, make sure the data is like your JS verifications here
  56.             */
  57.             $k = $data['key'];
  58.             $v = $data['value'];
  59.            
  60.             /*
  61.             * Do your verifications here...
  62.             */
  63.             $error = false;
  64.            
  65.            
  66.             /*
  67.             * Ok, verifications are good, save
  68.             */
  69.             if(!$error){
  70.                
  71.                 update_field('field_key', wp_json_encode(
  72.                     [
  73.                         'key' => $k,
  74.                         'value' => $v
  75.                     ]
  76.                 ), $post_id);
  77.                
  78.                 echo wp_json_encode([
  79.                     'status' => 'success',
  80.                     'message' => 'Your datas has been saved.'
  81.                 ]);
  82.                    
  83.             } else {
  84.                
  85.                 echo wp_json_encode([
  86.                     'status' => 'error',
  87.                     'message' => 'Your can\t be save.'
  88.                 ]);
  89.                
  90.             }
  91.            
  92.            
  93.             exit;
  94.            
  95.         }
  96.     }
  97.    
  98. }
  99.  
  100. new helloChamp();
  101.  
  102. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement