| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
| 3 | +defined( 'ABSPATH' ) || exit; | |
| 4 | 4 | |
| 5 | 5 | if ( ! class_exists( 'Dfrapi_Env' ) ) { |
| 6 | 6 | |
| 7 | 7 | /** |
| @@ -8,315 +8,40 @@ | ||
| 8 | 8 | * Check environment and print errors.. |
| 9 | 9 | */ |
| 10 | 10 | class Dfrapi_Env { |
| 11 | 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 | - } | |
| 12 | + public function __construct() { | |
| 13 | + } | |
| 35 | 14 | |
| 36 | - if ( $msg = self::unapproved_ph_merchants_exist() ) { | |
| 37 | - dfrapi_admin_messages( 'unapproved_ph_merchants', $msg ); | |
| 38 | - } | |
| 15 | + public static function api_keys_exist(): bool { | |
| 16 | + _deprecated_function( __METHOD__, '1.3.0', 'dfrapi_datafeedr_api_keys_exist()' ); | |
| 39 | 17 | |
| 40 | - if ( $msg = self::unapproved_effiliation_merchants_exist() ) { | |
| 41 | - dfrapi_admin_messages( 'unapproved_effiliation_merchants', $msg ); | |
| 42 | - } | |
| 18 | + return dfrapi_datafeedr_api_keys_exist(); | |
| 43 | 19 | } |
| 44 | 20 | |
| 45 | - static function api_keys_exist() { | |
| 46 | - | |
| 47 | - $configuration = (array) get_option( 'dfrapi_configuration' ); | |
| 48 | - $access_id = false; | |
| 49 | - $secret_key = false; | |
| 50 | - | |
| 51 | - if ( isset( $configuration['access_id'] ) && ( $configuration['access_id'] != '' ) ) { | |
| 52 | - $access_id = $configuration['access_id']; | |
| 53 | - } | |
| 54 | - | |
| 55 | - if ( isset( $configuration['secret_key'] ) && ( $configuration['secret_key'] != '' ) ) { | |
| 56 | - $secret_key = $configuration['secret_key']; | |
| 57 | - } | |
| 58 | - | |
| 59 | - if ( $access_id && $secret_key ) { | |
| 60 | - return true; | |
| 61 | - } | |
| 62 | - | |
| 63 | - return false; | |
| 21 | + public static function network_is_selected(): bool { | |
| 22 | + _deprecated_function( __METHOD__, '1.3.0', 'dfrapi_user_has_selected_networks()' ); | |
| 23 | + | |
| 24 | + return dfrapi_user_has_selected_networks(); | |
| 64 | 25 | } |
| 65 | - | |
| 66 | - static function network_is_selected() { | |
| 67 | - $networks = (array) get_option( 'dfrapi_networks' ); | |
| 68 | - if ( !empty( $networks['ids'] ) ) { | |
| 69 | - return true; | |
| 70 | - } | |
| 71 | - return false; | |
| 72 | - } | |
| 73 | - | |
| 74 | - static function merchant_is_selected() { | |
| 75 | - $merchants = (array) get_option( 'dfrapi_merchants' ); | |
| 76 | - if ( !empty( $merchants['ids'] ) ) { | |
| 77 | - return true; | |
| 78 | - } | |
| 79 | - return false; | |
| 80 | - } | |
| 81 | - | |
| 82 | - static function usage_over_90_percent() { | |
| 83 | - $percentage = dfrapi_get_api_usage_percentage(); | |
| 84 | - if ( $percentage >= 90 ) { | |
| 85 | - return true; | |
| 86 | - } | |
| 87 | - return false; | |
| 88 | - } | |
| 89 | 26 | |
| 90 | - static function missing_affiliate_ids() { | |
| 91 | - $networks = get_option( 'dfrapi_networks', array() ); | |
| 92 | - if ( ! empty( $networks ) ) { | |
| 93 | - foreach ( $networks['ids'] as $network ) { | |
| 27 | + public static function merchant_is_selected(): bool { | |
| 28 | + _deprecated_function( __METHOD__, '1.3.0', 'dfrapi_user_has_selected_merchants()' ); | |
| 94 | 29 | |
| 95 | - if ( | |
| 96 | - '801' == $network['nid'] || | |
| 97 | - '811' == $network['nid'] || | |
| 98 | - '812' == $network['nid'] || | |
| 99 | - '813' == $network['nid'] || | |
| 100 | - '814' == $network['nid'] || | |
| 101 | - '815' == $network['nid'] || | |
| 102 | - '816' == $network['nid'] || | |
| 103 | - '817' == $network['nid'] || | |
| 104 | - '818' == $network['nid'] | |
| 105 | - ) { | |
| 106 | - continue; // Partnerize does not have affiliate IDs. | |
| 107 | - } | |
| 108 | - | |
| 109 | - if ( '805' == $network['nid'] || '806' == $network['nid'] || '807' == $network['nid'] ) { | |
| 110 | - continue; // Effiliation does not have affiliate IDs. | |
| 111 | - } | |
| 112 | - | |
| 113 | - if ( empty( $network['aid'] ) ) { | |
| 114 | - return true; | |
| 115 | - } | |
| 116 | - } | |
| 117 | - } | |
| 118 | - | |
| 119 | - return false; | |
| 30 | + return dfrapi_user_has_selected_merchants(); | |
| 120 | 31 | } |
| 121 | - | |
| 122 | - static function unapproved_zanox_merchants_exist() { | |
| 123 | 32 | |
| 124 | - return false; | |
| 33 | + public static function usage_over_90_percent(): bool { | |
| 34 | + _deprecated_function( __METHOD__, '1.3.0', 'dfrapi_api_usage_over_90_percent()' ); | |
| 125 | 35 | |
| 126 | - global $wpdb; | |
| 127 | - | |
| 128 | - // Get range of Zanox Network IDs | |
| 129 | - $zanox_network_ids = range( 401, 430 ); | |
| 130 | - | |
| 131 | - // Get user's currently selected networks and convert into simple array of IDs. | |
| 132 | - $selected_networks = get_option( 'dfrapi_networks', array( 'ids' => array() ) ); | |
| 133 | - $selected_network_ids = array_keys( $selected_networks['ids'] ); | |
| 134 | - | |
| 135 | - // See if any Zanox Network IDs are found in the user's selected network IDs. | |
| 136 | - $selected_zanox_ids = array_intersect( $zanox_network_ids, $selected_network_ids ); | |
| 137 | - | |
| 138 | - // If there are no selected Zanox IDs, return. | |
| 139 | - if ( empty( $selected_zanox_ids ) ) { | |
| 140 | - return false; | |
| 141 | - } | |
| 142 | - | |
| 143 | - $results = $wpdb->get_results( | |
| 144 | - "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '%_transient_zmid_%' ", | |
| 145 | - OBJECT | |
| 146 | - ); | |
| 147 | - | |
| 148 | - // No results so nothing to worry about. Return false. | |
| 149 | - if ( empty( $results ) ) { | |
| 150 | - return false; | |
| 151 | - } | |
| 152 | - | |
| 153 | - $unapproved_merchants = array(); | |
| 154 | - | |
| 155 | - foreach ( $results as $row ) { | |
| 156 | - | |
| 157 | - if ( 'dfrapi_unapproved_zanox_merchant' != $row->option_value ) { | |
| 158 | - continue; | |
| 159 | - } | |
| 160 | - | |
| 161 | - $name = $row->option_name; // Example: _transient_zmid_46627_1264955 | |
| 162 | - $name = str_replace( '_transient_zmid_', '', $name ); | |
| 163 | - $name = explode( '_', $name ); | |
| 164 | - $name = array_filter( $name ); | |
| 165 | - | |
| 166 | - $merchant_id = absint( $name[0] ); | |
| 167 | - $adspace_id = ( isset( $name[1] ) ) ? absint( $name[1] ) : '_empty_'; | |
| 168 | - | |
| 169 | - $unapproved_merchants[ $merchant_id ] = $adspace_id; | |
| 170 | - } | |
| 171 | - | |
| 172 | - if ( empty( $unapproved_merchants ) ) { | |
| 173 | - return false; | |
| 174 | - } | |
| 175 | - | |
| 176 | - $unapproved_merchant_ids = array_keys( $unapproved_merchants ); | |
| 177 | - | |
| 178 | - $merchants = dfrapi_api_get_merchants_by_id( $unapproved_merchant_ids ); | |
| 179 | - | |
| 180 | - $msg = ''; | |
| 181 | - | |
| 182 | - foreach ( $merchants as $merchant ) { | |
| 183 | - $mid = absint( $merchant['_id'] ); | |
| 184 | - $msg .= '<br>- <strong>' . $merchant['name'] . '</strong> has not approved the Adspace ID: <strong>' . $unapproved_merchants[ $mid ] . '</strong>'; | |
| 185 | - } | |
| 186 | - | |
| 187 | - return $msg; | |
| 36 | + return dfrapi_api_usage_over_90_percent(); | |
| 188 | 37 | } |
| 189 | 38 | |
| 190 | - static function unapproved_ph_merchants_exist() { | |
| 39 | + public static function missing_affiliate_ids(): bool { | |
| 40 | + _deprecated_function( __METHOD__, '1.3.0', 'dfrapi_user_is_missing_affiliate_ids()' ); | |
| 191 | 41 | |
| 192 | - global $wpdb; | |
| 193 | - | |
| 194 | - // Get range of Partnerize Network IDs | |
| 195 | - $ph_network_ids = range( 801, 802 ); | |
| 196 | - | |
| 197 | - // Get user's currently selected networks and convert into simple array of IDs. | |
| 198 | - $selected_networks = get_option( 'dfrapi_networks', array( 'ids' => array() ) ); | |
| 199 | - $selected_network_ids = array_keys( $selected_networks['ids'] ); | |
| 200 | - | |
| 201 | - // See if any Partnerize Network IDs are found in the user's selected network IDs. | |
| 202 | - $selected_ph_ids = array_intersect( $ph_network_ids, $selected_network_ids ); | |
| 203 | - | |
| 204 | - // If there are no selected Partnerize IDs, return. | |
| 205 | - if ( empty( $selected_ph_ids ) ) { | |
| 206 | - return false; | |
| 207 | - } | |
| 208 | - | |
| 209 | - $results = $wpdb->get_results( | |
| 210 | - "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '%_transient_camref_%' ", | |
| 211 | - OBJECT | |
| 212 | - ); | |
| 213 | - | |
| 214 | - // No results so nothing to worry about. Return false. | |
| 215 | - if ( empty( $results ) ) { | |
| 216 | - return false; | |
| 217 | - } | |
| 218 | - | |
| 219 | - $unapproved_merchants = array(); | |
| 220 | - | |
| 221 | - foreach ( $results as $row ) { | |
| 222 | - | |
| 223 | - if ( 'dfrapi_unapproved_ph_merchant' != $row->option_value ) { | |
| 224 | - continue; | |
| 225 | - } | |
| 226 | - | |
| 227 | - $name = $row->option_name; // Example: _transient_camref_46627 | |
| 228 | - $name = str_replace( '_transient_camref_', '', $name ); | |
| 229 | - $name = explode( '_', $name ); | |
| 230 | - $name = array_filter( $name ); | |
| 231 | - | |
| 232 | - $merchant_id = absint( $name[0] ); | |
| 233 | - | |
| 234 | - $unapproved_merchants[ $merchant_id ] = $merchant_id; | |
| 235 | - } | |
| 236 | - | |
| 237 | - if ( empty( $unapproved_merchants ) ) { | |
| 238 | - return false; | |
| 239 | - } | |
| 240 | - | |
| 241 | - $unapproved_merchant_ids = array_keys( $unapproved_merchants ); | |
| 242 | - | |
| 243 | - $merchants = dfrapi_api_get_merchants_by_id( $unapproved_merchant_ids ); | |
| 244 | - | |
| 245 | - $msg = ''; | |
| 246 | - | |
| 247 | - foreach ( $merchants as $merchant ) { | |
| 248 | - $mid = absint( $merchant['_id'] ); | |
| 249 | - $msg .= '<br>- <strong>' . $merchant['name'] . '</strong>'; | |
| 250 | - } | |
| 251 | - | |
| 252 | - return $msg; | |
| 42 | + return dfrapi_user_is_missing_affiliate_ids(); | |
| 253 | 43 | } |
| 254 | 44 | |
| 255 | - static function unapproved_effiliation_merchants_exist() { | |
| 256 | - | |
| 257 | - global $wpdb; | |
| 258 | - | |
| 259 | - // Get range of Effiliation Network IDs | |
| 260 | - $effiliation_network_ids = range( 805, 807 ); | |
| 261 | - | |
| 262 | - // Get user's currently selected networks and convert into simple array of IDs. | |
| 263 | - $selected_networks = get_option( 'dfrapi_networks', array( 'ids' => array() ) ); | |
| 264 | - $selected_network_ids = array_keys( $selected_networks['ids'] ); | |
| 265 | - | |
| 266 | - // See if any Effiliation Network IDs are found in the user's selected network IDs. | |
| 267 | - $selected_effiliation_ids = array_intersect( $effiliation_network_ids, $selected_network_ids ); | |
| 268 | - | |
| 269 | - // If there are no selected Effiliation IDs, return. | |
| 270 | - if ( empty( $selected_effiliation_ids ) ) { | |
| 271 | - return false; | |
| 272 | - } | |
| 273 | - | |
| 274 | - $results = $wpdb->get_results( | |
| 275 | - "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '%_transient_effiliation_%' AND option_name != '_transient_effiliation_affiliate_ids' ", | |
| 276 | - OBJECT | |
| 277 | - ); | |
| 278 | - | |
| 279 | - // No results so nothing to worry about. Return false. | |
| 280 | - if ( empty( $results ) ) { | |
| 281 | - return false; | |
| 282 | - } | |
| 283 | - | |
| 284 | - $unapproved_merchants = array(); | |
| 285 | - | |
| 286 | - foreach ( $results as $row ) { | |
| 287 | - | |
| 288 | - if ( 'dfrapi_unapproved_effiliation_merchant' != $row->option_value ) { | |
| 289 | - continue; | |
| 290 | - } | |
| 291 | - | |
| 292 | - $name = $row->option_name; // Example: _transient_effiliation_46627 | |
| 293 | - $name = str_replace( '_transient_effiliation_', '', $name ); | |
| 294 | - $name = explode( '_', $name ); | |
| 295 | - $name = array_filter( $name ); | |
| 296 | - | |
| 297 | - $merchant_id = absint( $name[0] ); | |
| 298 | - | |
| 299 | - $unapproved_merchants[ $merchant_id ] = $merchant_id; | |
| 300 | - } | |
| 301 | - | |
| 302 | - if ( empty( $unapproved_merchants ) ) { | |
| 303 | - return false; | |
| 304 | - } | |
| 305 | - | |
| 306 | - $unapproved_merchant_ids = array_keys( $unapproved_merchants ); | |
| 307 | - | |
| 308 | - $merchants = dfrapi_api_get_merchants_by_id( $unapproved_merchant_ids ); | |
| 309 | - | |
| 310 | - $msg = ''; | |
| 311 | - | |
| 312 | - foreach ( $merchants as $merchant ) { | |
| 313 | - $mid = absint( $merchant['_id'] ); | |
| 314 | - $msg .= '<br>- <strong>' . $merchant['name'] . '</strong>'; | |
| 315 | - } | |
| 316 | - | |
| 317 | - return $msg; | |
| 318 | - } | |
| 319 | - | |
| 320 | 45 | } // class Dfrapi_Env |
| 321 | 46 | |
| 322 | 47 | } // class_exists check |