| @@ -1,0 +1,303 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\MailerLite\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Helper function to get the MailerLite url | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * | |
| 16 | + * @return string | |
| 17 | + */ | |
| 18 | +function automatorwp_mailerlite_get_url() { | |
| 19 | + | |
| 20 | + return ' https://connect.mailerlite.com'; | |
| 21 | + | |
| 22 | +} | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * Helper function to get the MailerLite API parameters | |
| 26 | + * | |
| 27 | + * @since 1.0.0 | |
| 28 | + * | |
| 29 | + * @return array|false | |
| 30 | + */ | |
| 31 | +function automatorwp_mailerlite_get_api() { | |
| 32 | + | |
| 33 | + $url = automatorwp_mailerlite_get_url(); | |
| 34 | + $token = automatorwp_mailerlite_get_option( 'token', '' ); | |
| 35 | + | |
| 36 | + if( empty( $token ) ) { | |
| 37 | + return false; | |
| 38 | + } | |
| 39 | + | |
| 40 | + return array( | |
| 41 | + 'url' => $url, | |
| 42 | + 'token' => $token, | |
| 43 | + ); | |
| 44 | + | |
| 45 | +} | |
| 46 | + | |
| 47 | + | |
| 48 | +/** | |
| 49 | + * Add subscriber to MailerLite | |
| 50 | + * | |
| 51 | + * @since 1.0.0 | |
| 52 | + * | |
| 53 | + * @param array $subscriber The subscriber data | |
| 54 | + */ | |
| 55 | +function automatorwp_mailerlite_add_subscriber( $subscriber ) { | |
| 56 | + | |
| 57 | + $api = automatorwp_mailerlite_get_api(); | |
| 58 | + | |
| 59 | + if( ! $api ) { | |
| 60 | + return; | |
| 61 | + } | |
| 62 | + | |
| 63 | + $response = wp_remote_post( 'https://api.mailerlite.com/api/v2/subscribers', array( | |
| 64 | + 'headers' => array( | |
| 65 | + 'X-MailerLite-ApiKey' => $api['token'], | |
| 66 | + 'Accept' => 'application/json', | |
| 67 | + 'Content-Type' => 'application/json' | |
| 68 | + ), | |
| 69 | + 'body' => json_encode( array( | |
| 70 | + 'email' => $subscriber['email'], | |
| 71 | + 'fields' => array( | |
| 72 | + 'name' => $subscriber['first_name'], | |
| 73 | + 'last_name' => $subscriber['last_name'], | |
| 74 | + ) | |
| 75 | + ) ) | |
| 76 | + ) ); | |
| 77 | + | |
| 78 | + return $response['response']['code']; | |
| 79 | +} | |
| 80 | + | |
| 81 | +/** | |
| 82 | + * Get groups from MailerLite | |
| 83 | + * | |
| 84 | + * @since 1.0.0 | |
| 85 | + * | |
| 86 | + * @return array | |
| 87 | + */ | |
| 88 | +function automatorwp_mailerlite_get_groups( ) { | |
| 89 | + | |
| 90 | + $groups = array(); | |
| 91 | + | |
| 92 | + $api = automatorwp_mailerlite_get_api(); | |
| 93 | + | |
| 94 | + if( ! $api ) { | |
| 95 | + return $options; | |
| 96 | + } | |
| 97 | + | |
| 98 | + $response = wp_remote_get( 'https://api.mailerlite.com/api/v2/groups', array( | |
| 99 | + 'headers' => array( | |
| 100 | + 'X-MailerLite-ApiKey' => $api['token'], | |
| 101 | + ), | |
| 102 | + ) ); | |
| 103 | + | |
| 104 | + $response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 105 | + | |
| 106 | + foreach ( $response as $group ){ | |
| 107 | + | |
| 108 | + $groups[] = array( | |
| 109 | + 'id' => $group['id'], | |
| 110 | + 'name' => $group['name'], | |
| 111 | + ); | |
| 112 | + | |
| 113 | + } | |
| 114 | + | |
| 115 | + return $groups; | |
| 116 | + | |
| 117 | +} | |
| 118 | + | |
| 119 | +/** | |
| 120 | + * Get group from MailerLite | |
| 121 | + * | |
| 122 | + * @since 1.0.0 | |
| 123 | + * | |
| 124 | + * @param stdClass $field | |
| 125 | + * | |
| 126 | + * @return array | |
| 127 | + */ | |
| 128 | +function automatorwp_mailerlite_options_cb_group( $field ) { | |
| 129 | + | |
| 130 | + // Setup vars | |
| 131 | + $value = $field->escaped_value; | |
| 132 | + $none_value = 'any'; | |
| 133 | + $none_label = __( 'any group', 'automatorwp-pro' ); | |
| 134 | + $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); | |
| 135 | + | |
| 136 | + if( ! empty( $value ) ) { | |
| 137 | + if( ! is_array( $value ) ) { | |
| 138 | + $value = array( $value ); | |
| 139 | + } | |
| 140 | + | |
| 141 | + foreach( $value as $group_id ) { | |
| 142 | + | |
| 143 | + // Skip option none | |
| 144 | + if( $group_id === $none_value ) { | |
| 145 | + continue; | |
| 146 | + } | |
| 147 | + | |
| 148 | + $options[$group_id] = automatorwp_mailerlite_get_group_name( $group_id ); | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + return $options; | |
| 153 | + | |
| 154 | +} | |
| 155 | + | |
| 156 | +/** | |
| 157 | +* Get the group name | |
| 158 | +* | |
| 159 | +* @since 1.0.0 | |
| 160 | +* | |
| 161 | +* @param string $group_id | |
| 162 | +* | |
| 163 | +* @return array | |
| 164 | +*/ | |
| 165 | +function automatorwp_mailerlite_get_group_name( $group_id ) { | |
| 166 | + | |
| 167 | + $api = automatorwp_mailerlite_get_api(); | |
| 168 | + | |
| 169 | + if( ! $api ) { | |
| 170 | + return $options; | |
| 171 | + } | |
| 172 | + | |
| 173 | + $response = wp_remote_get( 'https://api.mailerlite.com/api/v2/groups/' . $group_id, array( | |
| 174 | + 'headers' => array( | |
| 175 | + 'X-MailerLite-ApiKey' => $api['token'], | |
| 176 | + ), | |
| 177 | + ) ); | |
| 178 | + | |
| 179 | + $response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 180 | + | |
| 181 | + if ( isset ( $response['error']['code'] ) === 404 || !isset ( $response['name'] ) ){ | |
| 182 | + return; | |
| 183 | + } | |
| 184 | + | |
| 185 | + return $response['name']; | |
| 186 | +} | |
| 187 | + | |
| 188 | + | |
| 189 | +/** | |
| 190 | + * Add subscriber to MailerLite | |
| 191 | + * | |
| 192 | + * @since 1.0.0 | |
| 193 | + * | |
| 194 | + * @param array $subscriber The subscriber data | |
| 195 | + * @param int $group The group ID | |
| 196 | + */ | |
| 197 | +function automatorwp_mailerlite_add_subscriber_group( $subscriber, $group ) { | |
| 198 | + | |
| 199 | + $api = automatorwp_mailerlite_get_api(); | |
| 200 | + | |
| 201 | + if( ! $api ) { | |
| 202 | + return; | |
| 203 | + } | |
| 204 | + | |
| 205 | + $response = wp_remote_post( 'https://api.mailerlite.com/api/v2/groups/' . $group . '/subscribers', array( | |
| 206 | + 'headers' => array( | |
| 207 | + 'X-MailerLite-ApiKey' => $api['token'], | |
| 208 | + 'Accept' => 'application/json', | |
| 209 | + 'Content-Type' => 'application/json' | |
| 210 | + ), | |
| 211 | + 'body' => json_encode( array( | |
| 212 | + 'email' => $subscriber['email'], | |
| 213 | + 'fields' => array( | |
| 214 | + 'name' => $subscriber['first_name'], | |
| 215 | + 'last_name' => $subscriber['last_name'], | |
| 216 | + ) | |
| 217 | + ) ) | |
| 218 | + ) ); | |
| 219 | + | |
| 220 | + return $response['response']['code']; | |
| 221 | +} | |
| 222 | + | |
| 223 | +/** | |
| 224 | + * Add subscriber to MailerLite | |
| 225 | + * | |
| 226 | + * @since 1.0.0 | |
| 227 | + * | |
| 228 | + * @param array $subscriber The subscriber data | |
| 229 | + * @param int $group The group ID | |
| 230 | + */ | |
| 231 | +function automatorwp_mailerlite_remove_subscriber_group( $subscriber, $group ) { | |
| 232 | + | |
| 233 | + $api = automatorwp_mailerlite_get_api(); | |
| 234 | + | |
| 235 | + if( ! $api ) { | |
| 236 | + return; | |
| 237 | + } | |
| 238 | + | |
| 239 | + $response = wp_remote_request( 'https://api.mailerlite.com/api/v2/groups/' . $group .'/subscribers/' . $subscriber['email'], array( | |
| 240 | + 'method' => 'DELETE', | |
| 241 | + 'headers' => array( | |
| 242 | + 'X-MailerLite-ApiKey' => $api['token'], | |
| 243 | + ), | |
| 244 | + ) ); | |
| 245 | + | |
| 246 | + return $response['response']['code']; | |
| 247 | + | |
| 248 | +} | |
| 249 | + | |
| 250 | +/** | |
| 251 | + * Create group in MailerLite | |
| 252 | + * | |
| 253 | + * @since 1.0.0 | |
| 254 | + * | |
| 255 | + * @param string $group The group name | |
| 256 | + */ | |
| 257 | +function automatorwp_mailerlite_create_group( $group ) { | |
| 258 | + | |
| 259 | + $api = automatorwp_mailerlite_get_api(); | |
| 260 | + | |
| 261 | + if( ! $api ) { | |
| 262 | + return; | |
| 263 | + } | |
| 264 | + | |
| 265 | + $response = wp_remote_post( 'https://api.mailerlite.com/api/v2/groups', array( | |
| 266 | + 'headers' => array( | |
| 267 | + 'X-MailerLite-ApiKey' => $api['token'], | |
| 268 | + 'Content-Type' => 'application/json' | |
| 269 | + ), | |
| 270 | + 'body' => json_encode( array( | |
| 271 | + 'name' => $group | |
| 272 | + )) | |
| 273 | + ) ); | |
| 274 | + | |
| 275 | + return $response['response']['code']; | |
| 276 | + | |
| 277 | +} | |
| 278 | + | |
| 279 | +/** | |
| 280 | + * Remove group from MailerLite | |
| 281 | + * | |
| 282 | + * @since 1.0.0 | |
| 283 | + * | |
| 284 | + * @param int $group_id The ID group | |
| 285 | + */ | |
| 286 | +function automatorwp_mailerlite_remove_group( $group_id ) { | |
| 287 | + | |
| 288 | + $api = automatorwp_mailerlite_get_api(); | |
| 289 | + | |
| 290 | + if( ! $api ) { | |
| 291 | + return; | |
| 292 | + } | |
| 293 | + | |
| 294 | + $response = wp_remote_request( 'https://api.mailerlite.com/api/v2/groups/' . $group_id, array( | |
| 295 | + 'method' => 'DELETE', | |
| 296 | + 'headers' => array( | |
| 297 | + 'X-MailerLite-ApiKey' => $api['token'], | |
| 298 | + ), | |
| 299 | + ) ); | |
| 300 | + | |
| 301 | + return $response['response']['code']; | |
| 302 | + | |
| 303 | +} | |