check_purchase_code.php
118 lines
| 1 | <?php |
| 2 | if ( !function_exists( 'add_action' ) ) { |
| 3 | echo 'Hi there! I\'m just a plugin, not much I can do when called directly.'; |
| 4 | exit; |
| 5 | } |
| 6 | if(!class_exists('Superaddons_Check_Purchase_Code')){ |
| 7 | class Superaddons_Check_Purchase_Code { |
| 8 | protected $data; |
| 9 | public function __construct( $data ) { |
| 10 | $defaults = array( |
| 11 | "plugin"=>false, |
| 12 | "id"=>false, |
| 13 | "bundle"=>false, |
| 14 | "pro" => "", |
| 15 | "document"=>"#" |
| 16 | ); |
| 17 | $args = wp_parse_args( $data, $defaults ); |
| 18 | |
| 19 | $this->data = $args; |
| 20 | add_filter( 'plugin_action_links_' . $this->data["plugin"] , array( $this, 'add_action' ) ); |
| 21 | add_action( 'wp_ajax_rednumber_check_purchase_code', array($this,'check_purchase_code_ajax') ); |
| 22 | add_action( 'wp_ajax_rednumber_check_purchase_code_remove', array($this,'check_purchase_remove_code_ajax') ); |
| 23 | add_action('admin_enqueue_scripts', array($this,'add_js')); |
| 24 | add_action( 'admin_notices', array($this,"add_pro") ); |
| 25 | } |
| 26 | function add_pro(){ |
| 27 | global $pagenow; |
| 28 | $admin_pages = array('index.php', 'plugins.php'); |
| 29 | $check = get_option( '_redmuber_item_'.$this->data["id"] ); |
| 30 | if($check != "ok" ) { |
| 31 | if ( in_array( $pagenow, $admin_pages )) { |
| 32 | ?> |
| 33 | <div class="notice notice-warning is-dismissible"> |
| 34 | <p><strong><?php echo esc_attr($this->data["plugin_name"]) ?>: </strong><?php esc_html_e( 'Enter Purchase Code below the plugin or Upgrade to pro version: ', 'rednumber' ); ?> <a href="<?php echo esc_url( $this->data["pro"] ) ?>" target="_blank" ><?php echo esc_url( $this->data["pro"] ) ?></a></p> |
| 35 | </div> |
| 36 | <?php |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | function add_js(){ |
| 41 | wp_enqueue_script('rednumber_check_purchase_code', plugins_url('rednumber_check_purchase_code.js', __FILE__),array("jquery")); |
| 42 | } |
| 43 | function add_action($links){ |
| 44 | $check = get_option( '_redmuber_item_'.$this->data["id"] ); |
| 45 | $class_1 = ""; |
| 46 | $class_2 = ""; |
| 47 | if( $check =="ok" ){ |
| 48 | $class_1 = "hidden"; |
| 49 | }else{ |
| 50 | $class_2 = "hidden"; |
| 51 | } |
| 52 | $mylinks = array( |
| 53 | '<div class="rednumber-purchase-container rednumber-purchase-container_form '.$class_1.'">'.esc_html__("Purchase Code:","rednumber").' <input data-id="'.$this->data["id"].'" type="text"><a href="#" class="button button-primary rednumber-active">'.esc_html__("Active","rednumber").'</a></div> |
| 54 | <div class="rednumber-purchase-container rednumber-purchase-container_show '.$class_2.'">Purchased: '.get_option( '_redmuber_item_'.$this->data["id"]."_code" ).' <a data-id="'.$this->data["id"].'" href="#" class="rednumber-remove">'.esc_html__("Remove","rednumber").'</a></div><a target="_blank" class="'.$class_1.'" href="'.$this->data["pro"].'" >'.esc_html__("Get pro version","rednumber").'</a>', |
| 55 | ); |
| 56 | $mylinks[] ='<a href="'.$this->data["document"] .'" target="_blank" />Document</a>'; |
| 57 | return array_merge( $links, $mylinks ); |
| 58 | } |
| 59 | function check_purchase_code_ajax(){ |
| 60 | $code = sanitize_text_field($_POST["code"]); |
| 61 | $id = sanitize_text_field($_POST["id"]); |
| 62 | $status = $this->check_purchase_code($code,$id); |
| 63 | if( $status == "ok"){ |
| 64 | update_option( '_redmuber_item_'.$id, "ok" ); |
| 65 | update_option( '_redmuber_item_'.$id."_code", $code ); |
| 66 | } |
| 67 | echo esc_attr($status); |
| 68 | die(); |
| 69 | } |
| 70 | function check_purchase_remove_code_ajax(){ |
| 71 | $id = sanitize_text_field($_POST["id"]); |
| 72 | delete_option('_redmuber_item_'.$id); |
| 73 | delete_option('_redmuber_item_'.$id."_code"); |
| 74 | die(); |
| 75 | } |
| 76 | function check_purchase_code($code,$id_item){ |
| 77 | $personalToken = "uzAMx8rZ3FRV0ecu8t1pXNWG0d0NA6qL"; |
| 78 | $userAgent = "Purchase code verification"; |
| 79 | $ch = curl_init(); |
| 80 | curl_setopt_array($ch, array( |
| 81 | CURLOPT_URL => "https://add-ons.org/wp-json/checkpurchase_code/api/token/{$code}", |
| 82 | CURLOPT_RETURNTRANSFER => true, |
| 83 | CURLOPT_TIMEOUT => 20, |
| 84 | |
| 85 | CURLOPT_HTTPHEADER => array( |
| 86 | "Authorization: Bearer {$personalToken}", |
| 87 | "User-Agent: {$userAgent}" |
| 88 | ) |
| 89 | )); |
| 90 | $response = curl_exec($ch); |
| 91 | |
| 92 | if (curl_errno($ch) > 0) { |
| 93 | return "Error connecting to API: " . curl_error($ch); |
| 94 | } |
| 95 | $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 96 | if ($responseCode === 404) { |
| 97 | return "The purchase code was invalid"; |
| 98 | } |
| 99 | if ($responseCode !== 200) { |
| 100 | return "Failed to validate code due to an error: HTTP {$responseCode}"; |
| 101 | } |
| 102 | $body = json_decode($response,true); |
| 103 | |
| 104 | if ($body === false && json_last_error() !== JSON_ERROR_NONE) { |
| 105 | return "Error parsing response"; |
| 106 | } |
| 107 | if( isset($body["check"]) && $body["check"] == "ok" ){ |
| 108 | return "ok"; |
| 109 | }else{ |
| 110 | if( isset($body["check"]) ){ |
| 111 | return $body["check"]; |
| 112 | }else{ |
| 113 | return "Please choose other purchase."; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |