| 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 |
$this->data = $args; |
| 19 |
add_filter( 'plugin_action_links_' . $this->data["plugin"] , array( $this, 'add_action' ) ); |
| 20 |
add_action( 'wp_ajax_rednumber_check_purchase_code', array($this,'check_purchase_code_ajax') ); |
| 21 |
add_action( 'wp_ajax_rednumber_check_purchase_code_remove', array($this,'check_purchase_remove_code_ajax') ); |
| 22 |
add_action('admin_enqueue_scripts', array($this,'add_js')); |
| 23 |
add_action( 'admin_notices', array($this,"add_pro") ); |
| 24 |
add_action( 'admin_head', array($this,"add_head") ); |
| 25 |
} |
| 26 |
function add_head() { |
| 27 |
?> |
| 28 |
<style type="text/css"> |
| 29 |
.pro_disable::after { |
| 30 |
content: "Pro"; |
| 31 |
position: absolute; |
| 32 |
bottom: 0; |
| 33 |
right: 0; |
| 34 |
background: red; |
| 35 |
padding: 3px; |
| 36 |
font-size: 11px; |
| 37 |
color: #fff; |
| 38 |
border-radius: 5px 0 0 0; |
| 39 |
} |
| 40 |
.pro_disable { |
| 41 |
position: relative; |
| 42 |
} |
| 43 |
.pro_disable_padding{ |
| 44 |
padding: 10px !important; |
| 45 |
} |
| 46 |
.pro_text_style{ |
| 47 |
color:#9f9e9e; |
| 48 |
} |
| 49 |
body .pro_disable_fff { |
| 50 |
background: transparent; |
| 51 |
} |
| 52 |
</style> |
| 53 |
<?php |
| 54 |
} |
| 55 |
function add_pro(){ |
| 56 |
global $pagenow; |
| 57 |
$admin_pages = array('index.php', 'plugins.php'); |
| 58 |
$check = get_option( '_redmuber_item_'.$this->data["id"] ); |
| 59 |
if($check != "ok" ) { |
| 60 |
if ( in_array( $pagenow, $admin_pages )) { |
| 61 |
?> |
| 62 |
<div class="notice notice-warning is-dismissible"> |
| 63 |
<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> |
| 64 |
</div> |
| 65 |
<?php |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
function add_js(){ |
| 70 |
wp_enqueue_script('rednumber_check_purchase_code', plugins_url('rednumber_check_purchase_code.js', __FILE__),array("jquery")); |
| 71 |
} |
| 72 |
function add_action($links){ |
| 73 |
$check = get_option( '_redmuber_item_'.$this->data["id"] ); |
| 74 |
$class_1 = ""; |
| 75 |
$class_2 = ""; |
| 76 |
if( $check =="ok" ){ |
| 77 |
$class_1 = "hidden"; |
| 78 |
}else{ |
| 79 |
$class_2 = "hidden"; |
| 80 |
} |
| 81 |
$mylinks = array( |
| 82 |
'<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> |
| 83 |
<div class="rednumber-purchase-container rednumber-purchase-container_show '.$class_2.'">Purchased: <span>'.get_option( '_redmuber_item_'.$this->data["id"]."_code" ).'</span> <a data-code="'.get_option( '_redmuber_item_'.$this->data["id"]."_code" ).'" 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>', |
| 84 |
); |
| 85 |
$mylinks[] ='<a href="'.$this->data["document"] .'" target="_blank" />Document</a>'; |
| 86 |
return array_merge( $links, $mylinks ); |
| 87 |
} |
| 88 |
function check_purchase_code_ajax(){ |
| 89 |
$code = sanitize_text_field($_POST["code"]); |
| 90 |
$id = sanitize_text_field($_POST["id"]); |
| 91 |
$status = $this->check_purchase_code($code,$id); |
| 92 |
if( $status == "ok"){ |
| 93 |
update_option( '_redmuber_item_'.$id, "ok" ); |
| 94 |
update_option( '_redmuber_item_'.$id."_code", $code ); |
| 95 |
} |
| 96 |
echo esc_attr($status); |
| 97 |
die(); |
| 98 |
} |
| 99 |
function check_purchase_remove_code_ajax(){ |
| 100 |
$id = sanitize_text_field($_POST["id"]); |
| 101 |
$code = sanitize_text_field($_POST["code"]); |
| 102 |
delete_option('_redmuber_item_'.$id); |
| 103 |
delete_option('_redmuber_item_'.$id."_code"); |
| 104 |
$personalToken = "uzAMx8rZ3FRV0ecu8t1pXNWG0d0NA6qL"; |
| 105 |
$userAgent = "Purchase code verification"; |
| 106 |
$ch = curl_init(); |
| 107 |
$domain_name = preg_replace('/^www\./','',$_SERVER['SERVER_NAME']); |
| 108 |
curl_setopt_array($ch, array( |
| 109 |
CURLOPT_URL => "https://add-ons.org/wp-json/removepurchase_code/apiv2/token/{$code}/".htmlentities($domain_name), |
| 110 |
CURLOPT_RETURNTRANSFER => true, |
| 111 |
CURLOPT_TIMEOUT => 20, |
| 112 |
CURLOPT_HTTPHEADER => array( |
| 113 |
"Authorization: Bearer {$personalToken}", |
| 114 |
"User-Agent: {$userAgent}" |
| 115 |
) |
| 116 |
)); |
| 117 |
$response = curl_exec($ch); |
| 118 |
if (curl_errno($ch) > 0) { |
| 119 |
return "Error connecting to API: " . curl_error($ch); |
| 120 |
} |
| 121 |
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 122 |
if ($responseCode === 404) { |
| 123 |
return "The purchase code was invalid"; |
| 124 |
} |
| 125 |
if ($responseCode !== 200) { |
| 126 |
return "Failed to validate code due to an error: HTTP {$responseCode}"; |
| 127 |
} |
| 128 |
$body = json_decode($response,true); |
| 129 |
if ($body === false && json_last_error() !== JSON_ERROR_NONE) { |
| 130 |
return "Error parsing response"; |
| 131 |
} |
| 132 |
die(); |
| 133 |
} |
| 134 |
function resolve_root_domain($url, $max=6){ |
| 135 |
$domain = parse_url($url, PHP_URL_HOST); |
| 136 |
if (!strstr(substr($domain, 0, $max), '.')) |
| 137 |
return ($domain); |
| 138 |
else |
| 139 |
return (preg_replace("/^(.*?)\.(.*)$/", "$2", $domain)); |
| 140 |
} |
| 141 |
function check_purchase_code($code,$id_item){ |
| 142 |
$personalToken = "uzAMx8rZ3FRV0ecu8t1pXNWG0d0NA6qL"; |
| 143 |
$userAgent = "Purchase code verification"; |
| 144 |
$ch = curl_init(); |
| 145 |
$domain_name = preg_replace('/^www\./','',$_SERVER['SERVER_NAME']); |
| 146 |
curl_setopt_array($ch, array( |
| 147 |
CURLOPT_URL => "https://add-ons.org/wp-json/checkpurchase_code/apiv2/token/{$code}/".htmlentities($domain_name)."/".$id_item, |
| 148 |
CURLOPT_RETURNTRANSFER => true, |
| 149 |
CURLOPT_TIMEOUT => 20, |
| 150 |
CURLOPT_HTTPHEADER => array( |
| 151 |
"Authorization: Bearer {$personalToken}", |
| 152 |
"User-Agent: {$userAgent}" |
| 153 |
) |
| 154 |
)); |
| 155 |
$response = curl_exec($ch); |
| 156 |
if (curl_errno($ch) > 0) { |
| 157 |
return "Error connecting to API: " . curl_error($ch); |
| 158 |
} |
| 159 |
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 160 |
if ($responseCode === 404) { |
| 161 |
return "The purchase code was invalid"; |
| 162 |
} |
| 163 |
if ($responseCode !== 200) { |
| 164 |
return "Failed to validate code due to an error: HTTP {$responseCode}"; |
| 165 |
} |
| 166 |
$body = json_decode($response,true); |
| 167 |
if ($body === false && json_last_error() !== JSON_ERROR_NONE) { |
| 168 |
return "Error parsing response"; |
| 169 |
} |
| 170 |
if( isset($body["check"]) && $body["check"] == "ok" ){ |
| 171 |
return "ok"; |
| 172 |
}else{ |
| 173 |
if( isset($body["check"]) ){ |
| 174 |
return $body["check"]; |
| 175 |
}else{ |
| 176 |
return "Please choose other purchase."; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |