<?php
/**
 * Funciones diversas
 *
 */
 
 // funcion que se ejecuta despues del proceso e actualizacion del plugin para actualizar tablas
      function gamasms_update_plugin (){
         global $wpdb;
		 
          // se crea la tabla reporte detallado de mensajes
          $sql6 = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}gamasms_msg_details(
                id INT NOT NULL AUTO_INCREMENT,
                creationdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                msg_from VARCHAR(256) NULL,
                msg_to VARCHAR(256) NULL,
                msg_text text DEFAULT '',
                status VARCHAR(8) DEFAULT '0',
                description text DEFAULT '',
				medio VARCHAR(64) NULL,
                PRIMARY KEY(id)
          )";
          $wpdb->query($sql6);		

          // se crea la tabla de mensajes recibidos
          $sql7 = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}gamasms_msg_incoming(
                id INT NOT NULL AUTO_INCREMENT,
                creationdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                msg_from VARCHAR(256) NULL,
                msg_text text DEFAULT '',
				msg_date VARCHAR(256),
                msg_viewed VARCHAR(8) DEFAULT '0',
                PRIMARY KEY(id)
          )";
          $wpdb->query($sql7);
	  
	  }
 
 
 // Acciones cuando el plugin se activa o desactiva
     function gamasms_activar(){
         global $wpdb;

          //Agregando las opciones de configuracion en la tabla wp_options de wordpress
          $wp_options_data = array('token'=>'','smstype'=>'0','medio'=>'sms','admin_mobile_number'=>'','cf7_metabox'=>'0','wpforms_process_complete'=>'0','wpforms_process_complete_send_to'=>'admin','wpforms_process_complete_send_to_subscribes_group_id'=>'-1','wc_notif_created_customer'=>'0','wc_notif_created_customer_self'=>'0','wc_notif_created_customer_add_db'=>'0','wc_notif_new_order'=>'0','wc_notif_new_order_self'=>'0','edd_notif_new_order'=>'0','gamasms_publish_post'=>'0','gamasms_publish_post_send_to'=>'admin','gamasms_publish_post_send_to_subscribes_group_id'=>'-1','gamasms_comment_post'=>'0','gamasms_comment_post_send_to'=>'admin','gamasms_comment_post_send_to_subscribes_group_id'=>'-1','gform_after_submission'=>'0','gform_after_submission_send_to'=>'admin','gform_after_submission_send_to_subscribes_group_id'=>'-1','wpforms_process_complete_template'=>'','wc_notif_created_customer_template'=>'','wc_notif_created_customer_self_template'=>'','wc_notif_new_order_template'=>'','wc_notif_new_order_self_template'=>'','wc_notif_created_customer_add_db_group_id'=>'','edd_notif_new_order_template'=>'','gamasms_publish_post_template'=>'','gamasms_comment_post_template'=>'','gform_after_submission_template'=>'','gform_after_submission_forms'=>'0','gform_after_submission_forms_ids'=>'','wc_notif_processing_order'=>'0','wc_notif_cancelled_order'=>'0','wc_notif_completed_order'=>'0','wc_notif_pending_order'=>'0','wc_notif_failed_order'=>'0','wc_notif_refunded_order'=>'0','wc_notif_note_order'=>'0','wc_notif_processing_order_template'=>'','wc_notif_cancelled_order_template'=>'','wc_notif_completed_order_template'=>'','wc_notif_processing_order_template'=>'','wc_notif_pending_order_template'=>'','wc_notif_failed_order_template'=>'','wc_notif_refunded_order_template'=>'','wc_notif_note_order_template'=>''); 
          add_option('wpgamasms_settings',$wp_options_data);

          // se crea la tabla de formularios
          $sql2 = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}gamasms_forms(
                id INT NOT NULL AUTO_INCREMENT,
                creationdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                name VARCHAR(128) NULL,
                shortcode VARCHAR(256) DEFAULT '',
                smsnumber VARCHAR(256) NULL,
                smstype VARCHAR(8) DEFAULT '0',
                captcha_act VARCHAR(8) DEFAULT '0',
                form_title VARCHAR(256) DEFAULT '',
                form_strlen_text VARCHAR(256) DEFAULT '480',
                form_btn_submit_name VARCHAR(8) DEFAULT 'Enviar',
                PRIMARY KEY(id)
          )";
          $wpdb->query($sql2);

          // se crea la tabla de subscritores
          $sql3 = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}gamasms_subscribes(
                id INT NOT NULL AUTO_INCREMENT,
                creationdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                name VARCHAR(250) NULL,
                mobile VARCHAR(20) DEFAULT '',
                status VARCHAR(8) NULL,
                activate_key VARCHAR(11) DEFAULT '',
                group_id int(11) DEFAULT '-1',
                PRIMARY KEY(id)
          )";
          $wpdb->query($sql3);

          // se crea la tabla de grupos de subscritores
          $sql4 = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}gamasms_subscribes_group(
                id INT NOT NULL AUTO_INCREMENT,
                creationdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                name VARCHAR(128) NULL,
                PRIMARY KEY(id)
          )";
          $wpdb->query($sql4);

          // se crea la tabla de lista negra
          $sql5 = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}gamasms_not_call(
                id INT NOT NULL AUTO_INCREMENT,
                creationdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                phone VARCHAR(64) NULL,
                PRIMARY KEY(id)
          )";
          $wpdb->query($sql5);
		  
          // se crea la tabla reporte detallado de mensajes
          $sql6 = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}gamasms_msg_details(
                id INT NOT NULL AUTO_INCREMENT,
                creationdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                msg_from VARCHAR(256) NULL,
                msg_to VARCHAR(256) NULL,
                msg_text text DEFAULT '',
                status VARCHAR(8) DEFAULT '0',
                description text DEFAULT '',
				medio VARCHAR(64) NULL,
                PRIMARY KEY(id)
          )";
          $wpdb->query($sql6);
		  
          // se crea la tabla de mensajes recibidos
          $sql7 = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}gamasms_msg_incoming(
                id INT NOT NULL AUTO_INCREMENT,
                creationdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                msg_from VARCHAR(256) NULL,
                msg_text text DEFAULT '',
				msg_date VARCHAR(256),
                msg_viewed VARCHAR(8) DEFAULT '0',
                PRIMARY KEY(id)
          )";
          $wpdb->query($sql7);

     }

     function gamasms_desactivar(){

         global $wpdb;
		 //delete_option( 'wpgamasms_settings' );
         flush_rewrite_rules();         

     }
	 
     function gamasms_uninstall(){
         global $wpdb;
         
         $sql2 = "DROP TABLE IF EXISTS {$wpdb->prefix}gamasms_forms";
         $wpdb->query($sql2);

         $sql3 = "DROP TABLE IF EXISTS {$wpdb->prefix}gamasms_subscribes";
         $wpdb->query($sql3);

         $sql4 = "DROP TABLE IF EXISTS {$wpdb->prefix}gamasms_subscribes_group";
         $wpdb->query($sql4);

         $sql5 = "DROP TABLE IF EXISTS {$wpdb->prefix}gamasms_not_call";
         $wpdb->query($sql5);
		 
         $sql6 = "DROP TABLE IF EXISTS {$wpdb->prefix}gamasms_msg_details";
         $wpdb->query($sql6);
		 
         $sql7 = "DROP TABLE IF EXISTS {$wpdb->prefix}gamasms_msg_incoming";
         $wpdb->query($sql7);

         delete_option( 'wpgamasms_settings' );

     }
	 
// Ajax de editar y borrar los forms
    function OptionsForms(){
       global $wpdb;

       $nonce = sanitize_text_field($_POST['nonce']);

       if(!wp_verify_nonce($nonce,'seg')){
          wp_die("No tiene permisos para eliminar.");
       }

        $table = "{$wpdb->prefix}gamasms_forms";
        $id = sanitize_text_field($_POST['id']);
        $option = sanitize_text_field($_POST['option']);

        if($option == "delete"){
            $wpdb->delete($table,array('id'=>$id));   
        }

        if($option == "edit"){
            $sql = "SELECT * FROM $table WHERE id='$id'";
            $result = $wpdb->get_results($sql,ARRAY_A);
            wp_send_json($result); // devuelve el resultado en json
        }
    
        return true;

    }	 

// Ajax de editar y borrar los Groups
    function OptionsGroups(){
       global $wpdb;

       $nonce = sanitize_text_field($_POST['nonce']);

       if(!wp_verify_nonce($nonce,'seg')){
          wp_die("No tiene permisos para eliminar.");
       }

        $table = "{$wpdb->prefix}gamasms_subscribes_group";
        $id = $_POST['id'];
        $option = $_POST['option'];

        if($option == "delete"){
            $wpdb->delete($table,array('id'=>$id));   
        }

        if($option == "edit"){
            $sql = "SELECT * FROM $table WHERE id='$id'";
            $result = $wpdb->get_results($sql,ARRAY_A);
            wp_send_json($result); // devuelve el resultado en json
        }
    
        return true;

    }
	
// Shortcode forms
     function showform($atts){
        global $gama_sms;
        $html ="";
        $_gama_form =  new gama_form;
        $id = $atts['id'];
        if(isset($_POST['btnsend'])){
			
			// Verifica el token nonce para asegurar que no sea un ataque
            if(wp_verify_nonce($_POST["noncekey_gama_form"],'noncesendsms')){				 

               $data_form = $_gama_form->getForm($id);
               $captcha_act = $data_form['captcha_act'];

               
               $gama_sms->smsnumber = $data_form['smsnumber'];
               $gama_sms->smstext = sanitize_text_field($_POST["smstext"]);
               $gama_sms->smstype = $data_form['smstype'];
               $gama_sms->shorturl = "0";// acortador URL

               if($captcha_act == "t"){
                   $captcha_answer = strtoupper($_POST["captcha_answer"]);
                   if($_POST["captcha_orig"] == $captcha_answer){
                          $result = $gama_sms->SendSMS();
                   }else{
                          $result = "Caracteres ingresados no coincidieron!";
                   }
               }else{
                   // honeybox contra spam
                   $mygamabox = sanitize_text_field($_POST["mygamabox"]);
                   if(strlen($mygamabox)<=0){
                      $result = $gama_sms->SendSMS();
                   }else{
                      $result = ""; // No hace nada
                   }
               }

            }else{
                 $result = ""; // No hace nada
			}				
               
			//echo $result;
            $html = "<p>".$result."</p>";
         }

        $html .= $_gama_form->formGenerator($id);
        return $html;
      }
	
// Ajax de editar y borrar los Subscribes
    function OptionsSubscribes(){
       global $wpdb;

       $nonce = sanitize_text_field($_POST['nonce']);

       if(!wp_verify_nonce($nonce,'seg')){
          wp_die("No tiene permisos para eliminar.");
       }

        $table = "{$wpdb->prefix}gamasms_subscribes";
        $id = sanitize_text_field($_POST['id']);
        $option = sanitize_text_field($_POST['option']);

        if($option == "delete"){
            $wpdb->delete($table,array('id'=>$id));   
        }

        if($option == "edit"){
            $sql = "SELECT * FROM $table WHERE id='$id'";
            $result = $wpdb->get_results($sql,ARRAY_A);
            wp_send_json($result); // devuelve el resultado en json
        }
    
        return true;

    }
	
// Ajax de editar y borrar los Groups
    function OptionsBlackList(){
       global $wpdb;

       $nonce = sanitize_text_field($_POST['nonce']);

       if(!wp_verify_nonce($nonce,'seg')){
          wp_die("No tiene permisos para eliminar.");
       }

        $table = "{$wpdb->prefix}gamasms_not_call";
        $id = $_POST['id'];
        $option = $_POST['option'];

        if($option == "delete"){
            $wpdb->delete($table,array('id'=>$id));   
        }

        if($option == "edit"){
            $sql = "SELECT * FROM $table WHERE id='$id'";
            $result = $wpdb->get_results($sql,ARRAY_A);
            wp_send_json($result); // devuelve el resultado en json
        }
    
        return true;

    }
	
// Ajax de borrar los detalles de mensajes enviados o editarlos
    function OptionsMsgDetails(){
       global $wpdb;

       $nonce = sanitize_text_field($_POST['nonce']);
	   

       if(!wp_verify_nonce($nonce,'seg')){
          wp_die("No tiene permisos para eliminar.");
       }

        $table = "{$wpdb->prefix}gamasms_msg_details";
        $id = sanitize_text_field($_POST['id']);
        $option = sanitize_text_field($_POST['option']);

        if($option == "delete"){
            $wpdb->delete($table,array('id'=>$id));   
        }
    
	    if($option == "deleteall"){
			$delete = $wpdb->query("TRUNCATE TABLE $table");
        }
	
        return true;

    }
	
	
// Ajax de borrar los detalles de mensajes enviados o editarlos
    function OptionsMsgIncoming(){
       global $wpdb;
	   global $gama_sms;

       $nonce = sanitize_text_field($_POST['nonce']);
	   

       if(!wp_verify_nonce($nonce,'seg')){
          wp_die("No tiene permisos para eliminar.");
       }

        $table = "{$wpdb->prefix}gamasms_msg_incoming";
        $id = sanitize_text_field($_POST['id']);
        $option = sanitize_text_field($_POST['option']);

        if($option == "delete"){
            $wpdb->delete($table,array('id'=>$id));   
        }
    
	    if($option == "deleteall"){
			$delete = $wpdb->query("TRUNCATE TABLE $table");
        }
		
	    if($option == "sendmsg"){
			$gama_sms->smsnumber = sanitize_text_field($_POST['msgphone']);
            $gama_sms->smstext = sanitize_text_field($_POST['msgtext']);
			$result = $gama_sms->SendSMS(); // smstype dependera de la configuracion por defecto
        }
	
        return true;

    }
	
	 
    // Graba las configuraciones de integracion
    function save_config_integrations($hook){
        global $wpgamasms_option;
        check_ajax_referer('my_secure','nonce');

		if(isset($_POST['action'])){
			  //print_r($_POST);
			   $cf7_metabox = $_POST['cf7_metabox']; 
			   $wpforms_process_complete = $_POST['wpforms_process_complete']; 
			   $wpforms_process_complete_template = $_POST['wpforms_process_complete_template']; 
			   $wc_notif_new_order = $_POST['wc_notif_new_order']; 
			   $wc_notif_new_order_template = $_POST['wc_notif_new_order_template']; 
			   $wc_notif_new_order_self = $_POST['wc_notif_new_order_self'];
			   $wc_notif_new_order_self_template = $_POST['wc_notif_new_order_self_template'];
			   $wc_notif_created_customer = $_POST['wc_notif_created_customer'];
			   $wc_notif_created_customer_template = $_POST['wc_notif_created_customer_template'];
			   $wc_notif_created_customer_self = $_POST['wc_notif_created_customer_self'];
			   $wc_notif_created_customer_self_template = $_POST['wc_notif_created_customer_self_template'];
			   $wc_notif_created_customer_add_db = $_POST['wc_notif_created_customer_add_db'];
			   $wc_notif_created_customer_add_db_group_id = $_POST['wc_notif_created_customer_add_db_group_id'];
			   $edd_notif_new_order = $_POST['edd_notif_new_order'];
			   $edd_notif_new_order_template = $_POST['edd_notif_new_order_template'];
			   $gamasms_publish_post = $_POST['gamasms_publish_post'];
			   $gamasms_publish_post_template = $_POST['gamasms_publish_post_template'];
			   $gamasms_comment_post = $_POST['gamasms_comment_post']; 
			   $gamasms_comment_post_template = $_POST['gamasms_comment_post_template'];
			   $wpforms_process_complete_send_to = $_POST['wpforms_process_complete_send_to'];
			   $wpforms_process_complete_send_to_subscribes_group_id = $_POST['wpforms_process_complete_send_to_subscribes_group_id'];   
			   $gamasms_publish_post_send_to = $_POST['gamasms_publish_post_send_to'];
			   $gamasms_publish_post_send_to_subscribes_group_id = $_POST['gamasms_publish_post_send_to_subscribes_group_id'];
			   $gamasms_comment_post_send_to = $_POST['gamasms_comment_post_send_to'];
			   $gamasms_comment_post_send_to_subscribes_group_id = $_POST['gamasms_comment_post_send_to_subscribes_group_id'];  
               $gform_after_submission = $_POST['gform_after_submission'];
               $gform_after_submission_send_to = $_POST['gform_after_submission_send_to'];
               $gform_after_submission_send_to_subscribes_group_id = $_POST['gform_after_submission_send_to_subscribes_group_id'];
               $gform_after_submission_template = $_POST['gform_after_submission_template'];			   
			   $gform_after_submission_forms = $_POST['gform_after_submission_forms'];
			   $gform_after_submission_forms_ids = $_POST['gform_after_submission_forms_ids'];
               $wc_notif_processing_order = $_POST['wc_notif_processing_order'];
               $wc_notif_processing_order_template = $_POST['wc_notif_processing_order_template'];
               $wc_notif_cancelled_order = $_POST['wc_notif_cancelled_order'];
               $wc_notif_cancelled_order_template = $_POST['wc_notif_cancelled_order_template'];
               $wc_notif_completed_order = $_POST['wc_notif_completed_order'];
               $wc_notif_completed_order_template = $_POST['wc_notif_completed_order_template'];
               $wc_notif_pending_order = $_POST['wc_notif_pending_order'];
               $wc_notif_pending_order_template = $_POST['wc_notif_pending_order_template'];
               $wc_notif_failed_order = $_POST['wc_notif_failed_order'];
               $wc_notif_failed_order_template = $_POST['wc_notif_failed_order_template'];
               $wc_notif_refunded_order = $_POST['wc_notif_refunded_order'];
               $wc_notif_refunded_order_template = $_POST['wc_notif_refunded_order_template'];
               $wc_notif_note_order = $_POST['wc_notif_note_order'];
               $wc_notif_note_order_template = $_POST['wc_notif_note_order_template'];

			   if($gamasms_publish_post == "1"){
				   if($gamasms_publish_post_template == ""){
					   	   echo json_encode(['result'=>'Error. Mensaje vacio, Wordpress Publish Post','error'=>'1']);
			               wp_die();
				   }
			   }

			   if($gamasms_comment_post == "1"){
				   if($gamasms_comment_post_template == ""){
					   	   echo json_encode(['result'=>'Error. Mensaje vacio, Wordpress Comentario Post','error'=>'1']);
			               wp_die();
				   }
			   }

			   if($wpforms_process_complete == "1"){
				   if($wpforms_process_complete_template == ""){
					   	   echo json_encode(['result'=>'Error. Mensaje vacio, WPforms.','error'=>'1']);
			               wp_die();
				   }
			   }


			   if($wc_notif_processing_order == "1"){
				   if($wc_notif_processing_order_template == ""){
					   	   echo json_encode(['result'=>'Error. Mensaje vacio, en procesando orden de woocomerce.','error'=>'1']);
			               wp_die();
				   }
			   }

			   if($wc_notif_cancelled_order == "1"){
				   if($wc_notif_cancelled_order_template == ""){
					   	   echo json_encode(['result'=>'Error. Mensaje vacio, en cancelada orden de woocomerce.','error'=>'1']);
			               wp_die();
				   }
			   }


			   if($wc_notif_completed_order == "1"){
				   if($wc_notif_completed_order_template == ""){
					   	   echo json_encode(['result'=>'Error. Mensaje vacio, en completada orden de woocomerce.','error'=>'1']);
			               wp_die();
				   }
			   }


			   if($wc_notif_pending_order == "1"){
				   if($wc_notif_pending_order_template == ""){
					   	   echo json_encode(['result'=>'Error. Mensaje vacio, en pendiente orden de woocomerce.','error'=>'1']);
			               wp_die();
				   }
			   }

			   
			   if($wc_notif_failed_order == "1"){
				   if($wc_notif_failed_order_template == ""){
					   	   echo json_encode(['result'=>'Error. Mensaje vacio, en fallo orden de woocomerce.','error'=>'1']);
			               wp_die();
				   }
			   }

			   if($wc_notif_refunded_order == "1"){
				   if($wc_notif_refunded_order_template == ""){
					   	   echo json_encode(['result'=>'Error. Mensaje vacio, en reembolso orden de woocomerce.','error'=>'1']);
			               wp_die();
				   }
			   }
			   
			   if($wc_notif_note_order == "1"){
				   if($wc_notif_note_order_template == ""){
					   	   echo json_encode(['result'=>'Error. Mensaje vacio, en nota agregada a la orden de woocomerce.','error'=>'1']);
			               wp_die();
				   }
			   }
			   
			   
		   			   
			   $data = array('cf7_metabox'=>$cf7_metabox,
							 'wpforms_process_complete'=>$wpforms_process_complete,
							 'wpforms_process_complete_template'=>$wpforms_process_complete_template,
							 'wc_notif_new_order'=>$wc_notif_new_order,
							 'wc_notif_new_order_template'=>$wc_notif_new_order_template,
							 'wc_notif_new_order_self'=>$wc_notif_new_order_self,
							 'wc_notif_new_order_self_template'=>$wc_notif_new_order_self_template,
							 'wc_notif_created_customer'=>$wc_notif_created_customer,
							 'wc_notif_created_customer_template'=>$wc_notif_created_customer_template,
							 'wc_notif_created_customer_self'=>$wc_notif_created_customer_self,
							 'wc_notif_created_customer_self_template'=>$wc_notif_created_customer_self_template,
							 'wc_notif_created_customer_add_db'=>$wc_notif_created_customer_add_db,
							 'wc_notif_created_customer_add_db_group_id'=>$wc_notif_created_customer_add_db_group_id,
							 'edd_notif_new_order'=>$edd_notif_new_order,
							 'edd_notif_new_order_template'=>$edd_notif_new_order_template,
							 'gamasms_publish_post'=>$gamasms_publish_post,
							 'gamasms_publish_post_template'=>$gamasms_publish_post_template,
							 'gamasms_comment_post'=>$gamasms_comment_post,
							 'gamasms_comment_post_template'=>$gamasms_comment_post_template,
							 'wpforms_process_complete_send_to'=>$wpforms_process_complete_send_to,
							 'wpforms_process_complete_send_to_subscribes_group_id'=>$wpforms_process_complete_send_to_subscribes_group_id,
							 'gamasms_publish_post_send_to'=>$gamasms_publish_post_send_to,
							 'gamasms_publish_post_send_to_subscribes_group_id'=>$gamasms_publish_post_send_to_subscribes_group_id,
							 'gamasms_comment_post_send_to'=>$gamasms_comment_post_send_to,
							 'gamasms_comment_post_send_to_subscribes_group_id'=>$gamasms_comment_post_send_to_subscribes_group_id,
							 'gform_after_submission'=>$gform_after_submission,
                             'gform_after_submission_send_to'=>$gform_after_submission_send_to,
                             'gform_after_submission_send_to_subscribes_group_id'=> $gform_after_submission_send_to_subscribes_group_id,
                             'gform_after_submission_template'=>$gform_after_submission_template,
							 'gform_after_submission_forms'=>$gform_after_submission_forms,
							 'gform_after_submission_forms_ids'=>$gform_after_submission_forms_ids,
							 'wc_notif_processing_order'=>$wc_notif_processing_order,
							 'wc_notif_processing_order_template'=>$wc_notif_processing_order_template,
							 'wc_notif_cancelled_order'=>$wc_notif_cancelled_order,
							 'wc_notif_cancelled_order_template'=>$wc_notif_cancelled_order_template,
							 'wc_notif_completed_order'=>$wc_notif_completed_order,
							 'wc_notif_completed_order_template'=>$wc_notif_completed_order_template,
							 'wc_notif_pending_order'=>$wc_notif_pending_order,
							 'wc_notif_pending_order_template'=>$wc_notif_pending_order_template,
							 'wc_notif_failed_order'=>$wc_notif_failed_order,
							 'wc_notif_failed_order_template'=>$wc_notif_failed_order_template,
							 'wc_notif_refunded_order'=>$wc_notif_refunded_order,
							 'wc_notif_refunded_order_template'=>$wc_notif_refunded_order_template,
							 'wc_notif_note_order'=>$wc_notif_note_order,
							 'wc_notif_note_order_template'=>$wc_notif_note_order_template);
						 
			   update_option('wpgamasms_settings',array_merge($wpgamasms_option,$data));
			   echo json_encode(['result'=>'Se grabaron los datos de integracion.','error'=>null]);
			   wp_die();

		}else{
		   echo json_encode(['result'=>'No se grabaron los datos de integracion.']);
		   wp_die();				
			
		}
	
    }

    function save_config_general($hook){
        global $wpgamasms_option;
        check_ajax_referer('my_secure','nonce');

		if(isset($_POST['action'])){
			  //print_r($_POST);
			   $token = $_POST['token']; 
			   $medio = $_POST['medio'];
			   $smstype = $_POST['smstype']; 
			   $adminmobilenumber = $_POST['adminmobilenumber']; 

			   $data = array('token'=>$token,
			                 'medio'=>$medio,
							 'smstype'=>$smstype,
							 'admin_mobile_number'=>$adminmobilenumber);
							 
			   update_option('wpgamasms_settings',array_merge($wpgamasms_option,$data)); 
			   
			   echo json_encode(['result'=>'Se grabaron los datos generales.']);
			   wp_die();

		}else{
			   echo json_encode(['result'=>'No se grabaron los datos generales.']);
			   wp_die();				
			
		}
	
    }
	
	
   //Normalizar texto
   function gamasms_normaliza_mensaje( $mensaje ) {
	 $reemplazo = [ 
		'Š'			=> 'S', 
		'š'			=> 's', 
		'Đ'			=> 'Dj', 
		'đ'			=> 'dj', 
		'Ž'			=> 'Z', 
		'ž'			=> 'z', 
		'Č'			=> 'C', 
		'č'			=> 'c', 
		'Ć'			=> 'C', 
		'ć'			=> 'c', 
		'À'			=> 'A', 
		'Á'			=> 'A', 
		'Â'			=> 'A', 
		'Ã'			=> 'A', 
		'Ä'			=> 'A', 
		'Å'			=> 'A', 
		'Æ'			=> 'A', 
		'Ç'			=> 'C', 
		'È'			=> 'E', 
		'É'			=> 'E', 
		'Ê'			=> 'E', 
		'Ë'			=> 'E', 
		'Ì'			=> 'I', 
		'Í'			=> 'I', 
		'Î'			=> 'I', 
		'Ï'			=> 'I', 
		'Ñ'			=> 'N', 
		'Ò'			=> 'O', 
		'Ó'			=> 'O', 
		'Ô'			=> 'O', 
		'Õ'			=> 'O', 
		'Ö'			=> 'O', 
		'Ø'			=> 'O', 
		'Ù'			=> 'U', 
		'Ú'			=> 'U', 
		'Û'			=> 'U', 
		'Ü'			=> 'U', 
		'Ý'			=> 'Y', 
		'Þ'			=> 'B', 
		'ß'			=> 'Ss', 
		'à'			=> 'a', 
		'á'			=> 'a', 
		'â'			=> 'a', 
		'ã'			=> 'a', 
		'ä'			=> 'a', 
		'å'			=> 'a', 
		'æ'			=> 'a', 
		'ç'			=> 'c', 
		'è'			=> 'e', 
		'é'			=> 'e', 
		'ê'			=> 'e',  
		'ë'			=> 'e', 
		'ì'			=> 'i', 
		'í'			=> 'i', 
		'î'			=> 'i', 
		'ï'			=> 'i', 
		'ð'			=> 'o', 
		'ñ'			=> 'n', 
		'ò'			=> 'o', 
		'ó'			=> 'o', 
		'ô'			=> 'o', 
		'õ'			=> 'o', 
		'ö'			=> 'o', 
		'ø'			=> 'o', 
		'ù'			=> 'u', 
		'ú'			=> 'u', 
		'û'			=> 'u', 
		'ý'			=> 'y',  
		'ý'			=> 'y', 
		'þ'			=> 'b', 
		'ÿ'			=> 'y', 
		'Ŕ'			=> 'R', 
		'ŕ'			=> 'r', 
		"`"			=> "'", 
		"´"			=> "'", 
		"„"			=> ",", 
		"`"			=> "'",
		"´"			=> "'", 
		"“"			=> "\"", 
		"”"			=> "\"", 
		"´"			=> "'", 
		"&acirc;€™"	=> "'", 
		"{"			=> "", 
		"~"			=> "", 
		"–"			=> "-", 
		"’"			=> "'", 
		"!"			=> ".", 
		"¡"			=> "", 
		"?"			=> ".", 
		"¿"			=> "" 
     ];

	 $mensaje = str_replace( array_keys( $reemplazo ), array_values( $reemplazo ), $mensaje );

	 return $mensaje;
   }
	


?>