| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes\Integrations\Base; |
| 4 |
|
| 5 |
use OPTN\Includes\Db; |
| 6 |
use OPTN\Includes\Utils\Cache; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Parent MailService class. |
| 12 |
*/ |
| 13 |
abstract class BaseMailIntegration implements MailIntegration { |
| 14 |
|
| 15 |
/** |
| 16 |
* Db instance |
| 17 |
* |
| 18 |
* @var Db $db Db variable |
| 19 |
*/ |
| 20 |
protected $db; |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Account id |
| 25 |
* |
| 26 |
* @var int $account_id account id. |
| 27 |
*/ |
| 28 |
protected $account_id; |
| 29 |
|
| 30 |
/** |
| 31 |
* Settings for current account |
| 32 |
* |
| 33 |
* @var array $settings |
| 34 |
*/ |
| 35 |
protected $settings; |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
* |
| 40 |
* @param int|null $account_id account id. |
| 41 |
*/ |
| 42 |
public function __construct( $account_id ) { |
| 43 |
$this->db = Db::get_instance(); |
| 44 |
$this->account_id = $account_id; |
| 45 |
$this->settings = null; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get name |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
abstract public function get_name(); |
| 54 |
|
| 55 |
/** |
| 56 |
* Adds subscriber |
| 57 |
* |
| 58 |
* @param array $data data. |
| 59 |
* @return boolean |
| 60 |
*/ |
| 61 |
abstract public function add_subscriber( $data ): bool; |
| 62 |
|
| 63 |
/** |
| 64 |
* Add leads to lead table |
| 65 |
* |
| 66 |
* @param array $data user form input data. |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
abstract public function add_lead( $data ); |
| 70 |
|
| 71 |
/** |
| 72 |
* Gets all the fields |
| 73 |
* |
| 74 |
* @param array $args receive data as array. |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
abstract public function get_fields( $args = array() ): array; |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Gets all the lists |
| 82 |
* |
| 83 |
* @param array $args receive data as array. |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
abstract public function get_lists( $args = array() ): array; |
| 87 |
|
| 88 |
/** |
| 89 |
* Gets user settings |
| 90 |
* |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
protected function get_settings() { |
| 94 |
|
| 95 |
if ( is_null( $this->settings ) && $this->account_id ) { |
| 96 |
|
| 97 |
$res = $this->db->get_integration( $this->account_id, false ); |
| 98 |
$this->settings = $res['data']; |
| 99 |
} |
| 100 |
|
| 101 |
return $this->settings; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Set lists cache |
| 106 |
* |
| 107 |
* @param array $value value. |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
protected function set_lists_cache( $value ) { |
| 111 |
$key = 'optn_int_' . $this->get_name() . '_lists_' . $this->account_id; |
| 112 |
Cache::set( $key, wp_json_encode( $value ) ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Set fields cache |
| 117 |
* |
| 118 |
* @param array $value value. |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
protected function set_fields_cache( $value ) { |
| 122 |
$key = 'optn_int_' . $this->get_name() . '_fields_' . $this->account_id; |
| 123 |
Cache::set( $key, wp_json_encode( $value ) ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get lists cache |
| 128 |
* |
| 129 |
* @return mixed |
| 130 |
*/ |
| 131 |
protected function get_cached_lists() { |
| 132 |
$key = 'optn_int_' . $this->get_name() . '_lists_' . $this->account_id; |
| 133 |
$value = Cache::get( $key ); |
| 134 |
return $value ? json_decode( $value, true ) : null; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get lists cache |
| 139 |
* |
| 140 |
* @return mixed |
| 141 |
*/ |
| 142 |
protected function get_cached_fields() { |
| 143 |
$key = 'optn_int_' . $this->get_name() . '_fields_' . $this->account_id; |
| 144 |
$value = Cache::get( $key ); |
| 145 |
return $value ? json_decode( $value, true ) : null; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Gets headers with authorization token |
| 150 |
* |
| 151 |
* @return array |
| 152 |
*/ |
| 153 |
protected function get_headers() { |
| 154 |
|
| 155 |
$settings = $this->get_settings(); |
| 156 |
|
| 157 |
return array( |
| 158 |
'User-Agent' => 'WowOptin/' . OPTN_VERSION, |
| 159 |
'Content-Type' => 'application/json', |
| 160 |
'Accept' => 'application/json', |
| 161 |
'Authorization' => 'Bearer ' . $settings['apiKey'], |
| 162 |
); |
| 163 |
} |
| 164 |
} |
| 165 |
|