| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailerLite\Includes\Classes\Settings; |
| 4 |
|
| 5 |
use MailerLite\Includes\Classes\Singleton; |
| 6 |
|
| 7 |
class MailerLiteSettings extends Singleton |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Class instance |
| 11 |
* @var $instance |
| 12 |
*/ |
| 13 |
protected static $instance; |
| 14 |
|
| 15 |
/** |
| 16 |
* Check if checkout action is active |
| 17 |
* woo_ml_is_active |
| 18 |
* @return mixed |
| 19 |
*/ |
| 20 |
public function isActive() |
| 21 |
{ |
| 22 |
$api_status = $this->getMlOption('api_status', false); |
| 23 |
|
| 24 |
return $api_status; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get settings option |
| 29 |
* woo_ml_get_option |
| 30 |
* |
| 31 |
* @param $key |
| 32 |
* @param null $default |
| 33 |
* |
| 34 |
* @return null |
| 35 |
*/ |
| 36 |
public function getMlOption($key, $default = null) |
| 37 |
{ |
| 38 |
$settings = get_option('woocommerce_mailerlite_settings'); |
| 39 |
|
| 40 |
return (isset($settings[$key])) ? $settings[$key] : $default; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* |
| 45 |
* woo_ml_sync_failed |
| 46 |
* @return bool |
| 47 |
*/ |
| 48 |
public function syncFailed() |
| 49 |
{ |
| 50 |
return get_option('woo_ml_resource_sync_failed') || (get_option('woo_ml_sync_active') && ! get_transient('woo_ml_resource_sync_in_progress')); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get settings group options |
| 55 |
* woo_ml_settings_get_group_options |
| 56 |
* @return array |
| 57 |
*/ |
| 58 |
public function getGroupOptions() |
| 59 |
{ |
| 60 |
|
| 61 |
if ( ! is_admin()) { |
| 62 |
return []; |
| 63 |
} |
| 64 |
|
| 65 |
$options = array(); |
| 66 |
|
| 67 |
$groups = mailerlite_wp_get_groups(); |
| 68 |
|
| 69 |
if (is_array($groups) && sizeof($groups) > 0) { |
| 70 |
$options[''] = __('Please select...', 'woo-mailerlite'); |
| 71 |
foreach ($groups as $group) { |
| 72 |
if (isset($group['id']) && isset($group['name'])) { |
| 73 |
$options[$group['id']] = $group['name']; |
| 74 |
} |
| 75 |
} |
| 76 |
} else { |
| 77 |
$options[''] = __('No groups found', 'woo-mailerlite'); |
| 78 |
} |
| 79 |
|
| 80 |
return $options; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Map array in correct structure for WooCommerce Integration |
| 85 |
* woo_ml_remap_list |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
public function remapList($products) |
| 89 |
{ |
| 90 |
|
| 91 |
return array_map('strval', array_keys($products)); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Creates the ml_data table and sets the ml_data_table option flag |
| 96 |
* woo_create_mailer_data_table |
| 97 |
*/ |
| 98 |
public function createMailerDataTable() |
| 99 |
{ |
| 100 |
|
| 101 |
global $wpdb; |
| 102 |
|
| 103 |
$charset_collate = $wpdb->get_charset_collate(); |
| 104 |
|
| 105 |
$table = $wpdb->prefix . 'ml_data'; |
| 106 |
|
| 107 |
$sql = "CREATE TABLE $table ( |
| 108 |
data_name varchar(45) NOT NULL, |
| 109 |
data_value text NOT NULL, |
| 110 |
PRIMARY KEY (data_name) |
| 111 |
) $charset_collate;"; |
| 112 |
|
| 113 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 114 |
dbDelta($sql); |
| 115 |
|
| 116 |
update_option('ml_data_table', 1); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Set WooCommerce MailerLite settings options |
| 122 |
* woo_ml_set_option |
| 123 |
* |
| 124 |
* @param $key |
| 125 |
* @param $value |
| 126 |
* |
| 127 |
* @return bool |
| 128 |
*/ |
| 129 |
public function setOption($key, $value) |
| 130 |
{ |
| 131 |
$settings = get_option('woocommerce_mailerlite_settings'); |
| 132 |
|
| 133 |
if (isset($settings[$key])) { |
| 134 |
$settings[$key] = $value; |
| 135 |
} |
| 136 |
|
| 137 |
update_option('woocommerce_mailerlite_settings', $settings); |
| 138 |
|
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Remove product from ignored |
| 144 |
* woo_ml_remove_ignore_product |
| 145 |
* |
| 146 |
* @param $product_id |
| 147 |
*/ |
| 148 |
public function removeIgnoreProduct($product_id) |
| 149 |
{ |
| 150 |
|
| 151 |
delete_post_meta($product_id, '_woo_ml_product_ignored'); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Mark product as ignored for automation |
| 156 |
* woo_ml_ignore_product |
| 157 |
* |
| 158 |
* @param $product_id |
| 159 |
*/ |
| 160 |
public function ignoreProduct($product_id) |
| 161 |
{ |
| 162 |
|
| 163 |
add_post_meta($product_id, '_woo_ml_product_ignored', true, true); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Mark category as being tracked |
| 168 |
* woo_ml_complete_category_tracking |
| 169 |
* |
| 170 |
* @param $category_id |
| 171 |
* @param $ecommerce_id |
| 172 |
*/ |
| 173 |
public function completeCategoryTracking($category_id, $ecommerce_id) |
| 174 |
{ |
| 175 |
|
| 176 |
add_term_meta($category_id, '_woo_ml_category_tracked', $ecommerce_id, true); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Mark product as being tracked |
| 181 |
* woo_ml_complete_product_tracking |
| 182 |
* |
| 183 |
* @param $product_id |
| 184 |
*/ |
| 185 |
public function completeProductTracking($product_id) |
| 186 |
{ |
| 187 |
|
| 188 |
add_post_meta($product_id, '_woo_ml_product_tracked', true, true); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Check whether product was already tracked or not |
| 193 |
* woo_ml_product_tracking_completed |
| 194 |
* |
| 195 |
* @param $product_id |
| 196 |
* |
| 197 |
* @return bool |
| 198 |
*/ |
| 199 |
public function checkProductTracking($product_id) |
| 200 |
{ |
| 201 |
|
| 202 |
$product_tracked = get_post_meta($product_id, '_woo_ml_product_tracked', true); |
| 203 |
|
| 204 |
return ('1' == $product_tracked) ? true : false; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Get subscriber fields from customer data |
| 209 |
* woo_ml_get_subscriber_fields_from_customer_data |
| 210 |
* |
| 211 |
* @param $customer_data |
| 212 |
* |
| 213 |
* @return array |
| 214 |
*/ |
| 215 |
public function getSubscriberFieldsFromCustomerData($customer_data) |
| 216 |
{ |
| 217 |
|
| 218 |
$subscriber_fields = array(); |
| 219 |
|
| 220 |
if ( ! empty($customer_data['first_name'])) { |
| 221 |
$subscriber_fields['name'] = $customer_data['first_name']; |
| 222 |
} |
| 223 |
|
| 224 |
if ( ! empty($customer_data['last_name'])) { |
| 225 |
$subscriber_fields['last_name'] = $customer_data['last_name']; |
| 226 |
} |
| 227 |
|
| 228 |
if ( ! empty($customer_data['company'])) { |
| 229 |
$subscriber_fields['company'] = $customer_data['company']; |
| 230 |
} |
| 231 |
|
| 232 |
if ( ! empty($customer_data['city'])) { |
| 233 |
$subscriber_fields['city'] = $customer_data['city']; |
| 234 |
} |
| 235 |
|
| 236 |
if ( ! empty($customer_data['postcode'])) { |
| 237 |
$subscriber_fields['zip'] = $customer_data['postcode']; |
| 238 |
} |
| 239 |
|
| 240 |
if ( ! empty($customer_data['state'])) { |
| 241 |
$subscriber_fields['state'] = $customer_data['state']; |
| 242 |
} |
| 243 |
|
| 244 |
if ( ! empty($customer_data['country'])) { |
| 245 |
$subscriber_fields['country'] = $customer_data['country']; |
| 246 |
} |
| 247 |
|
| 248 |
if ( ! empty($customer_data['phone'])) { |
| 249 |
$subscriber_fields['phone'] = $customer_data['phone']; |
| 250 |
} |
| 251 |
|
| 252 |
return $subscriber_fields; |
| 253 |
} |
| 254 |
} |