1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148:
<?php
class Inbound_Pro_Admin_Ajax_Listeners {
public function __construct() {
self::load_hooks();
}
public static function load_hooks() {
add_action('wp_ajax_inbound_update_download_filter_preferences', array(__CLASS__, 'update_download_filter_preferences'));
add_action('wp_ajax_inbound_validate_api_key', array(__CLASS__, 'validate_api_key'));
}
public static function update_download_filter_preferences() {
global $wpdb;
if (!isset($_POST)) {
return;
}
$memory = Inbound_Options_API::get_option('inbound-pro', 'memory', array());
$memory['meta_filter'] = $_POST['meta_filter'];
Inbound_Options_API::update_option('inbound-pro', 'memory', $memory);
header('HTTP/1.1 200 OK');
exit;
}
public static function validate_api_key() {
global $inbound_settings;
if (!trim($_REQUEST['api_key'])) {
echo "{\"error\":\"missing-api-key\",\"message\":\"You must specify an API key to access this endpoint!\"}";
exit;
}
$customer = Inbound_Options_API::get_option('inbound-pro', 'customer', array());
$cache = get_transient('inbound_api_key_cache');
$clear_cache = (isset($_REQUEST['clear_cache'])) ? $_REQUEST['clear_cache'] : false;
if (
( trim($_REQUEST['api_key']) == $inbound_settings['api-key']['api-key'] )
&&
$cache
&&
!$clear_cache
) {
echo json_encode($cache);
exit;
}
$inbound_settings['api-key']['api-key'] = trim($_REQUEST['api_key']);
Inbound_Options_API::update_option('inbound-pro', 'settings', $inbound_settings);
$response = wp_remote_post(Inbound_API_Wrapper::get_api_url() . 'key/check', array(
'body' => array(
'api-key' => trim($_REQUEST['api_key']),
'site' => $_REQUEST['site']
),
'timeout' => 5
));
if (is_wp_error($response)) {
echo json_encode($response);
exit;
}
$decoded = json_decode($response['body'], true);
if (strstr($response['body'], '403 Forbidden')) {
echo "{\"error\":\"403\",\"message\":\"403 error. The connection is being blocked by a server security setting. Please contact your host for further assistance!\"}";
exit;
}
if (isset($decoded['customer'])) {
$customer['is_pro'] = self::get_highest_price_id($decoded['customer']);
Inbound_Options_API::update_option('inbound-pro', 'customer', $decoded['customer']);
update_option('inbound_activate_pro_components', true);
set_transient('inbound_api_key_cache', $decoded, WEEK_IN_SECONDS);
} else {
if ($decoded){
$customer['is_pro'] = 9;
Inbound_Options_API::update_option('inbound-pro', 'customer', $customer);
}
delete_transient('inbound_api_key_cache');
}
echo wp_remote_retrieve_body($response);
exit;
}
public static function get_highest_price_id($customer) {
$price_id = $customer['is_pro'];
if (isset($customer['payments']) && $customer['payments']) {
foreach ($customer['payments'] as $payment_id => $payment) {
foreach ($payment as $k => $download) {
if ($download['id'] != '119326') {
continue;
}
if ($download['options']['price_id'] > $price_id) {
$price_id = $download['options']['price_id'];
}
}
}
}
return $price_id;
}
}
$Inbound_Pro_Admin_Ajax_Listeners = new Inbound_Pro_Admin_Ajax_Listeners();