| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
if ( ! class_exists( 'Dfrapi_Env' ) ) { |
| 6 |
|
| 7 |
/** |
| 8 |
* Check environment and print errors.. |
| 9 |
*/ |
| 10 |
class Dfrapi_Env { |
| 11 |
|
| 12 |
function __construct() { |
| 13 |
|
| 14 |
// Cascading Errors |
| 15 |
if ( !self::api_keys_exist() ) { |
| 16 |
dfrapi_admin_messages( 'missing_api_keys' ); |
| 17 |
} elseif ( !self::network_is_selected() ) { |
| 18 |
dfrapi_admin_messages( 'missing_network_ids' ); |
| 19 |
} elseif ( !self::merchant_is_selected() ) { |
| 20 |
dfrapi_admin_messages( 'missing_merchant_ids' ); |
| 21 |
} |
| 22 |
|
| 23 |
// Non-cascading Errors |
| 24 |
if ( self::usage_over_90_percent() ) { |
| 25 |
dfrapi_admin_messages( 'usage_over_90_percent' ); |
| 26 |
} |
| 27 |
|
| 28 |
if ( self::missing_affiliate_ids() ) { |
| 29 |
dfrapi_admin_messages( 'missing_affiliate_ids' ); |
| 30 |
} |
| 31 |
|
| 32 |
if ( $msg = self::unapproved_zanox_merchants_exist() ) { |
| 33 |
dfrapi_admin_messages( 'unapproved_zanox_merchants', $msg ); |
| 34 |
} |
| 35 |
|
| 36 |
if ( $msg = self::unapproved_ph_merchants_exist() ) { |
| 37 |
dfrapi_admin_messages( 'unapproved_ph_merchants', $msg ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
static function api_keys_exist() { |
| 42 |
|
| 43 |
$configuration = (array) get_option( 'dfrapi_configuration' ); |
| 44 |
$access_id = false; |
| 45 |
$secret_key = false; |
| 46 |
|
| 47 |
if ( isset( $configuration['access_id'] ) && ( $configuration['access_id'] != '' ) ) { |
| 48 |
$access_id = $configuration['access_id']; |
| 49 |
} |
| 50 |
|
| 51 |
if ( isset( $configuration['secret_key'] ) && ( $configuration['secret_key'] != '' ) ) { |
| 52 |
$secret_key = $configuration['secret_key']; |
| 53 |
} |
| 54 |
|
| 55 |
if ( $access_id && $secret_key ) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
static function network_is_selected() { |
| 63 |
$networks = (array) get_option( 'dfrapi_networks' ); |
| 64 |
if ( !empty( $networks['ids'] ) ) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
static function merchant_is_selected() { |
| 71 |
$merchants = (array) get_option( 'dfrapi_merchants' ); |
| 72 |
if ( !empty( $merchants['ids'] ) ) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
static function usage_over_90_percent() { |
| 79 |
$percentage = dfrapi_get_api_usage_percentage(); |
| 80 |
if ( $percentage >= 90 ) { |
| 81 |
return true; |
| 82 |
} |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
static function missing_affiliate_ids() { |
| 87 |
$networks = get_option( 'dfrapi_networks', array() ); |
| 88 |
if ( ! empty( $networks ) ) { |
| 89 |
foreach ( $networks['ids'] as $network ) { |
| 90 |
|
| 91 |
if ( '801' == $network['nid'] ) { |
| 92 |
continue; // Performance Horizon does not have affiliate IDs. |
| 93 |
} |
| 94 |
|
| 95 |
if ( empty( $network['aid'] ) ) { |
| 96 |
return true; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
static function unapproved_zanox_merchants_exist() { |
| 105 |
|
| 106 |
global $wpdb; |
| 107 |
|
| 108 |
// Get range of Zanox Network IDs |
| 109 |
$zanox_network_ids = range( 401, 430 ); |
| 110 |
|
| 111 |
// Get user's currently selected networks and convert into simple array of IDs. |
| 112 |
$selected_networks = get_option( 'dfrapi_networks', array( 'ids' => array() ) ); |
| 113 |
$selected_network_ids = array_keys( $selected_networks['ids'] ); |
| 114 |
|
| 115 |
// See if any Zanox Network IDs are found in the user's selected network IDs. |
| 116 |
$selected_zanox_ids = array_intersect( $zanox_network_ids, $selected_network_ids ); |
| 117 |
|
| 118 |
// If there are no selected Zanox IDs, return. |
| 119 |
if ( empty( $selected_zanox_ids ) ) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
$results = $wpdb->get_results( |
| 124 |
"SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '%_transient_zmid_%' ", |
| 125 |
OBJECT |
| 126 |
); |
| 127 |
|
| 128 |
// No results so nothing to worry about. Return false. |
| 129 |
if ( empty( $results ) ) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
$unapproved_merchants = array(); |
| 134 |
|
| 135 |
foreach ( $results as $row ) { |
| 136 |
|
| 137 |
if ( 'dfrapi_unapproved_zanox_merchant' != $row->option_value ) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
$name = $row->option_name; // Example: _transient_zmid_46627_1264955 |
| 142 |
$name = str_replace( '_transient_zmid_', '', $name ); |
| 143 |
$name = explode( '_', $name ); |
| 144 |
$name = array_filter( $name ); |
| 145 |
|
| 146 |
$merchant_id = absint( $name[0] ); |
| 147 |
$adspace_id = ( isset( $name[1] ) ) ? absint( $name[1] ) : '_empty_'; |
| 148 |
|
| 149 |
$unapproved_merchants[ $merchant_id ] = $adspace_id; |
| 150 |
} |
| 151 |
|
| 152 |
if ( empty( $unapproved_merchants ) ) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
$unapproved_merchant_ids = array_keys( $unapproved_merchants ); |
| 157 |
|
| 158 |
$merchants = dfrapi_api_get_merchants_by_id( $unapproved_merchant_ids ); |
| 159 |
|
| 160 |
$msg = ''; |
| 161 |
|
| 162 |
foreach ( $merchants as $merchant ) { |
| 163 |
$mid = absint( $merchant['_id'] ); |
| 164 |
$msg .= '<br>- <strong>' . $merchant['name'] . '</strong> has not approved the Adspace ID: <strong>' . $unapproved_merchants[ $mid ] . '</strong>'; |
| 165 |
} |
| 166 |
|
| 167 |
return $msg; |
| 168 |
} |
| 169 |
|
| 170 |
static function unapproved_ph_merchants_exist() { |
| 171 |
|
| 172 |
global $wpdb; |
| 173 |
|
| 174 |
// Get range of Performance Horizon Network IDs |
| 175 |
$ph_network_ids = range( 801, 810 ); |
| 176 |
|
| 177 |
// Get user's currently selected networks and convert into simple array of IDs. |
| 178 |
$selected_networks = get_option( 'dfrapi_networks', array( 'ids' => array() ) ); |
| 179 |
$selected_network_ids = array_keys( $selected_networks['ids'] ); |
| 180 |
|
| 181 |
// See if any Performance Horizon Network IDs are found in the user's selected network IDs. |
| 182 |
$selected_ph_ids = array_intersect( $ph_network_ids, $selected_network_ids ); |
| 183 |
|
| 184 |
// If there are no selected Performance Horizon IDs, return. |
| 185 |
if ( empty( $selected_ph_ids ) ) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
$results = $wpdb->get_results( |
| 190 |
"SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '%_transient_camref_%' ", |
| 191 |
OBJECT |
| 192 |
); |
| 193 |
|
| 194 |
// No results so nothing to worry about. Return false. |
| 195 |
if ( empty( $results ) ) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
$unapproved_merchants = array(); |
| 200 |
|
| 201 |
foreach ( $results as $row ) { |
| 202 |
|
| 203 |
if ( 'dfrapi_unapproved_ph_merchant' != $row->option_value ) { |
| 204 |
continue; |
| 205 |
} |
| 206 |
|
| 207 |
$name = $row->option_name; // Example: _transient_camref_46627 |
| 208 |
$name = str_replace( '_transient_camref_', '', $name ); |
| 209 |
$name = explode( '_', $name ); |
| 210 |
$name = array_filter( $name ); |
| 211 |
|
| 212 |
$merchant_id = absint( $name[0] ); |
| 213 |
|
| 214 |
$unapproved_merchants[ $merchant_id ] = $merchant_id; |
| 215 |
} |
| 216 |
|
| 217 |
if ( empty( $unapproved_merchants ) ) { |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
$unapproved_merchant_ids = array_keys( $unapproved_merchants ); |
| 222 |
|
| 223 |
$merchants = dfrapi_api_get_merchants_by_id( $unapproved_merchant_ids ); |
| 224 |
|
| 225 |
$msg = ''; |
| 226 |
|
| 227 |
foreach ( $merchants as $merchant ) { |
| 228 |
$mid = absint( $merchant['_id'] ); |
| 229 |
$msg .= '<br>- <strong>' . $merchant['name'] . '</strong>'; |
| 230 |
} |
| 231 |
|
| 232 |
return $msg; |
| 233 |
} |
| 234 |
|
| 235 |
} // class Dfrapi_Env |
| 236 |
|
| 237 |
} // class_exists check |
| 238 |
|