| 1 |
<?php |
| 2 |
|
| 3 |
class GiftUp_Settings |
| 4 |
{ |
| 5 |
private static $plugin; |
| 6 |
private static $plugin_directory; |
| 7 |
|
| 8 |
public function __construct() |
| 9 |
{ |
| 10 |
self::$plugin = GiftUp()->get_plugin_basename(); |
| 11 |
self::$plugin_directory = GiftUp()->get_plugin_basedirectory(); |
| 12 |
|
| 13 |
add_action( 'init', array( __CLASS__, 'set_up_menu' ) ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Method that is called to set up the settings menu |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
public static function set_up_menu() |
| 22 |
{ |
| 23 |
// Add Gift Up settings page in the menu |
| 24 |
add_action( 'admin_menu', array( __CLASS__, 'add_settings_menu' ) ); |
| 25 |
|
| 26 |
// Add Gift Up settings page in the plugin list |
| 27 |
add_filter( 'plugin_action_links_' . self::$plugin, array( __CLASS__, 'add_settings_link' ) ); |
| 28 |
|
| 29 |
// Add Gift Up notification globally |
| 30 |
add_action( 'admin_notices', array( __CLASS__, 'show_nag_messages' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Add Gift Up settings page in the menu |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public static function add_settings_menu() { |
| 39 |
add_options_page( 'Gift Up', 'Gift Up', 'manage_options', 'giftup-settings', array( __CLASS__, 'show_settings_page' )); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Add Gift Up settings page in the plugin list |
| 44 |
* |
| 45 |
* @param mixed $links links |
| 46 |
* |
| 47 |
* @return mixed links |
| 48 |
*/ |
| 49 |
public static function add_settings_link( $links ) |
| 50 |
{ |
| 51 |
$settings_link = '<a href="options-general.php?page=giftup-settings">Settings</a>'; |
| 52 |
array_unshift( $links, $settings_link ); |
| 53 |
|
| 54 |
return $links; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Method that is called to warn if gift up is not connected |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public static function show_nag_messages() { |
| 63 |
if (GiftUp()->options->get_company_id() == false) { |
| 64 |
echo '<div class="notice notice-warning is-dismissible" id="giftup-nag"><p>' . __( 'Please <a href="/wp-admin/options-general.php?page=giftup-settings">connect/create your Gift Up account</a> to your WordPress account to sell gift cards online' ) . '</p></div>'; |
| 65 |
} |
| 66 |
elseif (GiftUp()->options->get_woocommerce_enabled() |
| 67 |
&& GiftUp()->options->get_woocommerce_operating_mode() == GIFTUP_WOO_MODE_DISCOUNT_COUPONS |
| 68 |
&& GiftUp()->diagnostics->woocommerce_installed_version() != null) { |
| 69 |
echo '<div class="notice notice-warning is-dismissible" id="giftup-nag"><p>' . __( 'Please <a href="/wp-admin/options-general.php?page=giftup-settings">upgrade your Gift Up + WooCommerce connection</a> to improve the customer redemption experience in your cart' ) . '</p></div>'; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Display Gift Up settings page content |
| 75 |
* |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public static function show_settings_page() |
| 79 |
{ |
| 80 |
if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) { |
| 81 |
$response = self::consume_post( $_POST ); |
| 82 |
|
| 83 |
if (null !== $response) { |
| 84 |
$message = $response['message']; |
| 85 |
$status = $response['status']; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$giftup_dashboard_root = GiftUp()->api->dashboard_root(); |
| 90 |
$giftup_api_key = GiftUp()->options->get_api_key(); |
| 91 |
$giftup_company_id = GiftUp()->options->get_company_id(); |
| 92 |
|
| 93 |
if ( $giftup_api_key ) { |
| 94 |
$giftup_company = GiftUp()->api->get_company(); |
| 95 |
} |
| 96 |
|
| 97 |
$current_user = wp_get_current_user(); |
| 98 |
$giftup_email_address = $current_user->user_email; |
| 99 |
|
| 100 |
$woocommerce_version = GiftUp()->diagnostics->woocommerce_installed_version(); |
| 101 |
$woocommerce_activated = GiftUp()->diagnostics->is_woocommerce_activated(); |
| 102 |
$woocommerce_installed = $woocommerce_version != null; |
| 103 |
|
| 104 |
if (strlen( $giftup_company_id ) > 0 && $woocommerce_installed ) { |
| 105 |
$woocommerce_version_compatible = version_compare( $woocommerce_version, '3.0', '>=' ); |
| 106 |
$woocommerce_enabled = $woocommerce_version_compatible && GiftUp()->options->get_woocommerce_enabled() == true; |
| 107 |
|
| 108 |
$woocommerce_apply_to_shipping = GiftUp()->options->get_woocommerce_apply_to_shipping(); |
| 109 |
$woocommerce_apply_to_taxes = GiftUp()->options->get_woocommerce_apply_to_taxes(); |
| 110 |
|
| 111 |
$woocommerce_can_enable_test_mode = current_user_can('administrator'); |
| 112 |
$woocommerce_enabled_test_mode = self::is_test_mode(); |
| 113 |
$woocommerce_enabled_diagnostics_mode = self::is_diagnostics_on(); |
| 114 |
|
| 115 |
if ( $woocommerce_enabled_diagnostics_mode ) { |
| 116 |
$instaled_plugins_list = GiftUp()->diagnostics->get_plugins_list(); |
| 117 |
} |
| 118 |
|
| 119 |
$mode = GiftUp()->options->get_woocommerce_operating_mode(); |
| 120 |
|
| 121 |
$woocommerce_connection_status = GiftUp()->api->get_woocommerce_connection_status(); |
| 122 |
$woocommerce_is_connected = $woocommerce_connection_status != null && $woocommerce_connection_status["isConnected"] == true; |
| 123 |
$woocommerce_uses_api_direct = $woocommerce_is_connected && $woocommerce_connection_status["usesApiDirect"] == true; |
| 124 |
|
| 125 |
$woocommerce_currency = function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : "unknown"; |
| 126 |
$giftup_currency = $giftup_company ? $giftup_company["currency"] : "unknown"; |
| 127 |
$woocommerce_can_enable = strtolower($woocommerce_currency) == strtolower($giftup_currency); |
| 128 |
|
| 129 |
$woocommerce_upgrade_required = $woocommerce_is_connected && $woocommerce_uses_api_direct == false; |
| 130 |
|
| 131 |
if ( $woocommerce_enabled ) { |
| 132 |
if ( $woocommerce_is_connected == false ) { |
| 133 |
if ( GiftUp()->api->notify_connect_woocommerce() ) { |
| 134 |
self::delete_woocommerce_webhook(); |
| 135 |
$woocommerce_upgrade_required = false; |
| 136 |
} |
| 137 |
} |
| 138 |
else if ( $mode == GIFTUP_WOO_MODE_API && $woocommerce_uses_api_direct == false ) { |
| 139 |
if ( GiftUp()->api->notify_connect_woocommerce() ) { |
| 140 |
self::delete_woocommerce_webhook(); |
| 141 |
$woocommerce_upgrade_required = false; |
| 142 |
} |
| 143 |
} |
| 144 |
else if ( $mode == GIFTUP_WOO_MODE_DISCOUNT_COUPONS && $woocommerce_uses_api_direct ) { |
| 145 |
self::upgrade_woocommerce_operating_mode(); |
| 146 |
$woocommerce_upgrade_required = false; |
| 147 |
} |
| 148 |
} elseif ( $woocommerce_is_connected ) { |
| 149 |
GiftUp()->api->notify_disconnect_woocommerce(); |
| 150 |
} |
| 151 |
|
| 152 |
if ( strtolower($woocommerce_currency) != strtolower($giftup_currency) ) { |
| 153 |
GiftUp()->options->set_woocommerce_enabled( false ); |
| 154 |
$woocommerce_enabled = false; |
| 155 |
$woocommerce_can_enable = false; |
| 156 |
} |
| 157 |
|
| 158 |
$mode = GiftUp()->options->get_woocommerce_operating_mode(); |
| 159 |
$woocommerce_legacy_method = $mode == GIFTUP_WOO_MODE_DISCOUNT_COUPONS; |
| 160 |
} |
| 161 |
|
| 162 |
require_once self::$plugin_directory . 'view/giftup-settings.php'; |
| 163 |
} |
| 164 |
|
| 165 |
private static function is_test_mode() { |
| 166 |
$val = isset($_COOKIE["giftup_test_mode"]) ? $_COOKIE["giftup_test_mode"] : "live"; |
| 167 |
|
| 168 |
if ('POST' == $_SERVER['REQUEST_METHOD'] && isset( $_POST['giftup_woocommerce_settings'] )) { |
| 169 |
$val = isset( $_POST['woocommerce_test_mode'] ) ? $_POST['woocommerce_test_mode'] : "live"; |
| 170 |
} |
| 171 |
|
| 172 |
return $val == "test"; |
| 173 |
} |
| 174 |
|
| 175 |
private static function is_diagnostics_on() { |
| 176 |
$val = isset($_COOKIE["giftup_diagnostics_mode"]) ? true : false; |
| 177 |
|
| 178 |
if ('POST' == $_SERVER['REQUEST_METHOD'] && isset( $_POST['giftup_woocommerce_settings'] )) { |
| 179 |
$val = isset( $_POST['woocommerce_diagnostics_mode'] ) ? true : false; |
| 180 |
} |
| 181 |
|
| 182 |
return $val; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Routes processing of request parameters depending on the source section of the settings page |
| 187 |
* |
| 188 |
* @param mixed $params array of parameters from $_POST |
| 189 |
* |
| 190 |
* @return mixed response array from the save or send functions |
| 191 |
*/ |
| 192 |
private static function consume_post( $params ) { |
| 193 |
if ( isset( $params['giftup_api_key'] )) { |
| 194 |
if (!current_user_can('administrator')) { |
| 195 |
return array( |
| 196 |
'message' => 'You need to be a WP admin to set the Gift Up API key', |
| 197 |
'status' => 'error' |
| 198 |
); |
| 199 |
|
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $params[ 'giftup_api_key_nonce' ] ) ), 'giftup_api_key' ) ) { |
| 204 |
return array( |
| 205 |
'message' => 'We could not verify that you are in fact you when attempting to set the Gift Up API key, please try again...', |
| 206 |
'status' => 'error' |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
$api_key = $params['giftup_api_key']; |
| 211 |
|
| 212 |
if ( strlen($api_key) == 0 ) { |
| 213 |
GiftUp()->options->disconnect(); |
| 214 |
|
| 215 |
return array( |
| 216 |
'message' => 'Gift Up account disconnected', |
| 217 |
'status' => 'error' |
| 218 |
); |
| 219 |
} else { |
| 220 |
$company = GiftUp()->api->get_company( $api_key ); |
| 221 |
|
| 222 |
if ( NULL !== $company ) { |
| 223 |
GiftUp()->options->set_api_key( $api_key ); |
| 224 |
GiftUp()->options->set_company_id( $company['id'] ); |
| 225 |
|
| 226 |
if ( GiftUp()->diagnostics->woocommerce_installed_version() != null ) { |
| 227 |
GiftUp()->api->notify_connect_woocommerce(); |
| 228 |
} |
| 229 |
|
| 230 |
return; |
| 231 |
} else { |
| 232 |
return GiftUp()->diagnostics->test_curl( $api_key ); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if ( isset( $params['giftup_update_woocommerce_operating_mode'] )) { |
| 238 |
if (!current_user_can('administrator')) { |
| 239 |
return array( |
| 240 |
'message' => 'You need to be a WP admin to update Gift Up + WooCommerce settings', |
| 241 |
'status' => 'error' |
| 242 |
); |
| 243 |
|
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $params[ 'giftup_update_woocommerce_operating_mode_nonce' ] ) ), 'giftup_update_woocommerce_operating_mode' ) ) { |
| 248 |
return array( |
| 249 |
'message' => 'We could not verify that you are in fact you when attempting to updatate the WooCommerce + Gift Up operating mode, please try again...', |
| 250 |
'status' => 'error' |
| 251 |
); |
| 252 |
} |
| 253 |
|
| 254 |
self::upgrade_woocommerce_operating_mode(); |
| 255 |
} |
| 256 |
|
| 257 |
if ( isset( $params['giftup_woocommerce_settings'] )) { |
| 258 |
if (!current_user_can('administrator')) { |
| 259 |
return array( |
| 260 |
'message' => 'You need to be a WP admin to update Gift Up settings', |
| 261 |
'status' => 'error' |
| 262 |
); |
| 263 |
|
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $params[ 'giftup_woocommerce_settings_nonce' ] ) ), 'giftup_woocommerce_settings' ) ) { |
| 268 |
return array( |
| 269 |
'message' => 'We could not verify that you are in fact you when attempting to updatate Gift Up settings, please try again...', |
| 270 |
'status' => 'error' |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
GiftUp()->options->set_woocommerce_enabled( isset($params['giftup_woocommerce_enabled']) && $params['giftup_woocommerce_enabled'] == "on" ); |
| 275 |
GiftUp()->options->set_woocommerce_apply_to_shipping( isset($params['woocommerce_apply_to_shipping']) && $params['woocommerce_apply_to_shipping'] == "on" ); |
| 276 |
GiftUp()->options->set_woocommerce_apply_to_taxes( isset($params['woocommerce_apply_to_taxes']) && $params['woocommerce_apply_to_taxes'] == "on" ); |
| 277 |
|
| 278 |
// Drop a 1 hour cookie for test & diagnostics mode |
| 279 |
$val = isset($params['woocommerce_test_mode']) ? $params['woocommerce_test_mode'] : "live"; |
| 280 |
setcookie("giftup_test_mode", $val, time()+(60*60), "/"); |
| 281 |
|
| 282 |
if ( isset($params['woocommerce_diagnostics_mode']) && $params['woocommerce_diagnostics_mode'] == 'on' ) { |
| 283 |
setcookie("giftup_diagnostics_mode", "on", time()+(60*60), "/"); |
| 284 |
} else { |
| 285 |
setcookie("giftup_diagnostics_mode", "", time()-1, "/"); |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
public static function upgrade_woocommerce_operating_mode() { |
| 291 |
if ( GiftUp()->diagnostics->woocommerce_installed_version() == null ) { |
| 292 |
GiftUp()->api->notify_disconnect_woocommerce(); |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
if (GiftUp()->options->get_woocommerce_operating_mode() == GIFTUP_WOO_MODEAPI) { |
| 297 |
return; |
| 298 |
} |
| 299 |
|
| 300 |
self::delete_all_woocommerce_giftcardcoupons(); |
| 301 |
self::delete_woocommerce_webhook(); |
| 302 |
|
| 303 |
GiftUp()->api->notify_connect_woocommerce(); |
| 304 |
} |
| 305 |
|
| 306 |
public static function delete_woocommerce_webhook() { |
| 307 |
if ( GiftUp()->diagnostics->woocommerce_installed_version() == null |
| 308 |
|| GiftUp()->diagnostics->is_woocommerce_activated() == false ) { |
| 309 |
return; |
| 310 |
} |
| 311 |
|
| 312 |
$args = array(); |
| 313 |
|
| 314 |
$data_store = WC_Data_Store::load( 'webhook' ); |
| 315 |
$webhook_ids = $data_store->search_webhooks(); |
| 316 |
|
| 317 |
foreach( $webhook_ids as $webhook_id ) { |
| 318 |
$webhook = new WC_Webhook($webhook_id); |
| 319 |
if ( strpos( $webhook->get_delivery_url(), 'giftup.app') > 0 ){ |
| 320 |
$webhook->delete(true); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
private static function delete_all_woocommerce_giftcardcoupons() { |
| 326 |
if ( GiftUp()->diagnostics->woocommerce_installed_version() == null |
| 327 |
|| GiftUp()->diagnostics->is_woocommerce_activated() == false ) { |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
$coupons_store = WC_Data_Store::load( 'coupon' ); |
| 332 |
|
| 333 |
$limit = 20; |
| 334 |
$offset = 0; |
| 335 |
$gift_cards = null; |
| 336 |
|
| 337 |
do { |
| 338 |
$api_response = GiftUp()->api->get_gift_cards($offset, $limit); |
| 339 |
|
| 340 |
if ($api_response != null) { |
| 341 |
$gift_cards = $api_response['giftCards']; |
| 342 |
|
| 343 |
if ( $api_response['total'] <= 0 ) |
| 344 |
{ |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
foreach ( $gift_cards as $gift_card ) { |
| 349 |
$coupon_ids = $coupons_store->get_ids_by_code($gift_card['code']); |
| 350 |
|
| 351 |
foreach( $coupon_ids as $coupon_id ) { |
| 352 |
$coupon = new WC_Coupon($coupon_id); |
| 353 |
$coupon->delete(true); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
$offset = $offset + $limit; |
| 358 |
} |
| 359 |
} while ($api_response != null && $api_response['hasMore'] == true); |
| 360 |
} |
| 361 |
} |
| 362 |
|