| 1 |
<?php |
| 2 |
|
| 3 |
class SGPMApi |
| 4 |
{ |
| 5 |
/** |
| 6 |
* Reference to base plugin class. |
| 7 |
* |
| 8 |
* @var SGPMBase |
| 9 |
*/ |
| 10 |
protected $base; |
| 11 |
|
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
$this->set(); |
| 15 |
$this->loadOption(); |
| 16 |
add_action('admin_post_sgpm_connect', array($this, 'connect')); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Sets our object instance and base class instance. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
public function set() |
| 25 |
{ |
| 26 |
$this->base = SGPMBase::getInstance(); |
| 27 |
} |
| 28 |
|
| 29 |
public function connect($reload = true) |
| 30 |
{ |
| 31 |
//CSRF check |
| 32 |
if (!check_admin_referer('sgpm_connect', 'wp-nonce-token')) { |
| 33 |
wp_die('Security check fail'); |
| 34 |
} |
| 35 |
|
| 36 |
$options = get_option('sgpm_popup_maker_api_option'); |
| 37 |
$connectUrl = SGPM_SERVICE_URL.'app/connect'; |
| 38 |
$apiKey = $this->sanitize('sgpm-api-key'); |
| 39 |
|
| 40 |
if (!$apiKey && $options['oldUser'] && $options['isAuthenticate'] && $options['apiKey']) { |
| 41 |
$apiKey = $options['apiKey']; |
| 42 |
} |
| 43 |
|
| 44 |
$data = array( |
| 45 |
'apiKey' => $apiKey, |
| 46 |
'appname' => 'Wordpress' |
| 47 |
); |
| 48 |
|
| 49 |
$argData = array( |
| 50 |
'method' => 'POST', |
| 51 |
'timeout' => 45, |
| 52 |
'redirection' => 5, |
| 53 |
'httpversion' => '1.0', |
| 54 |
'blocking' => true, |
| 55 |
'headers' => array(), |
| 56 |
'body' => $data, |
| 57 |
'cookies' => array() |
| 58 |
); |
| 59 |
|
| 60 |
// connect call to service |
| 61 |
$response = wp_remote_post($connectUrl, $argData); |
| 62 |
|
| 63 |
if (is_wp_error($response)) { |
| 64 |
wp_die("Something went wrong after connection!"); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
global $SGPM_DATA_CONFIG_ARRAY; |
| 69 |
|
| 70 |
$data = json_decode($response['body'], true); |
| 71 |
|
| 72 |
$options['apiKey'] = ''; |
| 73 |
$options['isAuthenticate'] = false; |
| 74 |
|
| 75 |
if (isset($data['isAuthenticate']) && $data['isAuthenticate']) { |
| 76 |
$options['oldUser'] = true; |
| 77 |
$options['isAuthenticate'] = true; |
| 78 |
$options['apiKey'] = $data['apiKey']; |
| 79 |
$options['popups'] = array_reverse($data['popups'], true); |
| 80 |
$options['user'] = $data['user']; |
| 81 |
} |
| 82 |
|
| 83 |
$newestPopup = true; |
| 84 |
if (empty($options['popupsSettings'])) { |
| 85 |
foreach ($options['popups'] as $popupId => $popup) { |
| 86 |
if ($newestPopup) { |
| 87 |
$options['popupsSettings'][$popupId]['status'] = 'enabled'; |
| 88 |
$options['popupsSettings'][$popupId]['displayTarget'] = $SGPM_DATA_CONFIG_ARRAY['displayTarget']['initialData']; |
| 89 |
|
| 90 |
$newestPopup = false; |
| 91 |
} |
| 92 |
else { |
| 93 |
$options['popupsSettings'][$popupId]['status'] = 'disabled'; |
| 94 |
$options['popupsSettings'][$popupId]['displayTarget'] = $SGPM_DATA_CONFIG_ARRAY['displayTarget']['initialData']; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
else { |
| 99 |
// disable popups from refreshed list |
| 100 |
foreach ($options['popups'] as $popupId => $popup) { |
| 101 |
if (isset($options['popupsSettings'][$popupId])) continue; |
| 102 |
|
| 103 |
$options['popupsSettings'][$popupId]['status'] = 'disabled'; |
| 104 |
$options['popupsSettings'][$popupId]['displayTarget'] = $SGPM_DATA_CONFIG_ARRAY['displayTarget']['initialData']; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
update_option('sgpm_popup_maker_api_option', $options); |
| 109 |
wp_redirect(SGPM_ADMIN_URL."admin.php?page=popup-maker-api-settings&tryconnect=1"); |
| 110 |
exit(); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Sets our global option if it is not found in the DB. |
| 115 |
* |
| 116 |
* @since 1.0.0 |
| 117 |
*/ |
| 118 |
public function loadOption() |
| 119 |
{ |
| 120 |
$option = get_option('sgpm_popup_maker_api_option'); |
| 121 |
if (!$option || empty($option)) { |
| 122 |
$option = self::defaultOptions(); |
| 123 |
update_option('sgpm_popup_maker_api_option', $option); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public static function defaultOptions() |
| 128 |
{ |
| 129 |
$options = array( |
| 130 |
'isAuthenticate' => false, |
| 131 |
'apiKey' => '', |
| 132 |
'popups' => array(), |
| 133 |
'popupsSettings' => array(), |
| 134 |
'user' => array( |
| 135 |
'isActive' => false, |
| 136 |
'isExpired' => false, |
| 137 |
'isDisabled' => false, |
| 138 |
'email' => '', |
| 139 |
'firstname' => '', |
| 140 |
'lastname' => '' |
| 141 |
), |
| 142 |
'oldUser' => false, |
| 143 |
'pluginVersion' => SGPM_VERSION |
| 144 |
); |
| 145 |
|
| 146 |
return $options; |
| 147 |
} |
| 148 |
|
| 149 |
public function sanitize($optionsKey) |
| 150 |
{ |
| 151 |
if ( ! isset($_POST[$optionsKey]) ) { |
| 152 |
return ''; // Or handle the error appropriately |
| 153 |
} |
| 154 |
return sanitize_text_field($_POST[$optionsKey]); |
| 155 |
} |
| 156 |
} |
| 157 |
|