Unbounce Landing Pages - Version 1.0.5

Version Description

Download this release

Release Info

Developer unbouncewordpress
Plugin Icon Unbounce Landing Pages
Version 1.0.5
Comparing to
See all releases

Code changes from version 1.0.4 to 1.0.5

Files changed (8) hide show
  1. UBConfig.php +88 -18
  2. UBDiagnostics.php +15 -9
  3. UBEvents.php +63 -0
  4. UBHTTP.php +45 -0
  5. UBLogger.php +2 -46
  6. Unbounce-Page.php +64 -19
  7. js/set-unbounce-domains.js +60 -27
  8. readme.txt +1 -1
UBConfig.php CHANGED
@@ -7,13 +7,17 @@ class UBConfig {
7
  const UB_REMOTE_DEBUG_KEY = 'ub-remote-debug';
8
  const UB_PAGE_SERVER_DOMAIN_KEY = 'ub-page-server-domain';
9
  const UB_REMOTE_LOG_URL_KEY = 'ub-remote-log-url';
 
10
  const UB_API_URL_KEY = 'ub-api-url';
11
  const UB_API_CLIENT_ID_KEY = 'ub-api-client-id';
12
  const UB_AUTHORIZED_DOMAINS_KEY = 'ub-authorized-domains';
13
  const UB_HAS_AUTHORIZED_KEY = 'ub-has-authorized';
 
 
 
14
  const UB_CACHE_TIMEOUT_ENV_KEY = 'UB_WP_ROUTES_CACHE_EXP';
15
- const UB_USER_AGENT = 'Unbounce WP Plugin 1.0.4';
16
- const UB_VERSION = '1.0.4';
17
 
18
  public static function default_page_server_domain() {
19
  $domain = getenv('UB_PAGE_SERVER_DOMAIN');
@@ -28,6 +32,14 @@ class UBConfig {
28
  return $url;
29
  }
30
 
 
 
 
 
 
 
 
 
31
  public static function default_api_url() {
32
  $url = getenv('UB_API_URL');
33
  return $url ? $url : 'https://api.unbounce.com';
@@ -51,6 +63,10 @@ class UBConfig {
51
  return get_option(UBConfig::UB_REMOTE_LOG_URL_KEY, UBConfig::default_remote_log_url());
52
  }
53
 
 
 
 
 
54
  public static function api_url() {
55
  return get_option(UBConfig::UB_API_URL_KEY, UBConfig::default_api_url());
56
  }
@@ -75,10 +91,43 @@ class UBConfig {
75
  return get_option(UBConfig::UB_REMOTE_DEBUG_KEY, 0) == 1;
76
  }
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  public static function fetch_proxyable_url_set($domain, $etag, $ps_domain) {
79
  if(!$domain) {
80
- UBLogger::warning('Domain not provided, not fetching sitemap.xml');
81
- return array('FAILURE', null, null, null);
 
82
  }
83
 
84
  try {
@@ -129,29 +178,33 @@ class UBConfig {
129
 
130
  if ($success) {
131
  UBLogger::debug("Retrieved new routes, HTTP code: '$http_code'");
132
- return array('NEW', $etag, $max_age, $result);
133
  }
134
  else {
135
  $errors = join(', ', $result);
136
- UBLogger::warning("An error occurred while processing routes, XML errors: '$errors'");
137
- return array('FAILURE', null, null, null);
 
138
  }
139
  }
140
  if ($http_code == 304) {
141
  UBLogger::debug("Routes have not changed, HTTP code: '$http_code'");
142
- return array('SAME', $etag, $max_age, null);
143
  }
144
  if ($http_code == 404) {
145
  UBLogger::debug("No routes to retrieve, HTTP code: '$http_code'");
146
- return array('NONE', null, null, null);
147
  }
148
  else {
149
- UBLogger::warning("An error occurred while retrieving routes; HTTP code: '$http_code'; Error: " . $curl_error);
150
- return array('FAILURE', null, null, null);
 
151
  }
 
152
  } catch (Exception $e) {
153
- UBLogger::warning("An error occurred while retrieving routes; Error: " . $e);
154
- return array('FAILURE', null, null, null);
 
155
  }
156
  }
157
 
@@ -223,28 +276,32 @@ class UBConfig {
223
 
224
  list($routes_status, $etag, $max_age, $proxyable_url_set_new) = $result_array;
225
 
226
- if ($routes_status == 'NEW') {
227
  $domain_info['proxyable_url_set'] = $proxyable_url_set_new;
228
  $domain_info['proxyable_url_set_etag'] = $etag;
229
  $domain_info['proxyable_url_set_cache_timeout'] = $max_age;
230
  }
231
- elseif ($routes_status == 'SAME') {
232
  // Just extend the cache
233
  $domain_info['proxyable_url_set_cache_timeout'] = $max_age;
234
  }
235
- elseif ($routes_status == 'NONE') {
236
  $domain_info['proxyable_url_set'] = array();
237
  $domain_info['proxyable_url_set_etag'] = null;
238
  }
239
- elseif ($routes_status == 'FAILURE') {
240
  UBLogger::warning('Route fetching failed');
241
  }
242
  else {
243
  UBLogger::warning("Unknown response from route fetcher: '$routes_status'");
244
  }
245
 
 
246
  $domain_info['proxyable_url_set_fetched_at'] = $current_time;
247
- $domain_info['last_status'] = $routes_status;
 
 
 
248
  $domains_info[$domain] = $domain_info;
249
  $options_setter(UBConfig::UB_ROUTES_CACHE_KEY, $domains_info);
250
  }
@@ -252,6 +309,7 @@ class UBConfig {
252
  return UBUtil::array_select_by_key($domain_info,
253
  array('proxyable_url_set',
254
  'proxyable_url_set_fetched_at',
 
255
  'last_status'));
256
  }
257
 
@@ -270,5 +328,17 @@ class UBConfig {
270
  return in_array($domain, UBConfig::authorized_domains());
271
  }
272
 
 
 
 
 
 
 
 
 
 
 
 
 
273
  }
274
  ?>
7
  const UB_REMOTE_DEBUG_KEY = 'ub-remote-debug';
8
  const UB_PAGE_SERVER_DOMAIN_KEY = 'ub-page-server-domain';
9
  const UB_REMOTE_LOG_URL_KEY = 'ub-remote-log-url';
10
+ const UB_REMOTE_EVENTS_URL_KEY = 'ub-remote-events-url';
11
  const UB_API_URL_KEY = 'ub-api-url';
12
  const UB_API_CLIENT_ID_KEY = 'ub-api-client-id';
13
  const UB_AUTHORIZED_DOMAINS_KEY = 'ub-authorized-domains';
14
  const UB_HAS_AUTHORIZED_KEY = 'ub-has-authorized';
15
+ const UB_USER_ID_KEY = 'ub-user-id';
16
+ const UB_DOMAIN_ID_KEY = 'ub-domain-id';
17
+ const UB_CLIENT_ID_KEY = 'ub-client-id';
18
  const UB_CACHE_TIMEOUT_ENV_KEY = 'UB_WP_ROUTES_CACHE_EXP';
19
+ const UB_USER_AGENT = 'Unbounce WP Plugin 1.0.5';
20
+ const UB_VERSION = '1.0.5';
21
 
22
  public static function default_page_server_domain() {
23
  $domain = getenv('UB_PAGE_SERVER_DOMAIN');
32
  return $url;
33
  }
34
 
35
+ public static function default_remote_events_url() {
36
+ $url = getenv('UB_REMOTE_EVENTS_URL');
37
+ if ($url == null) {
38
+ return 'https://events-gateway.unbounce.com/events/domains';
39
+ }
40
+ return $url;
41
+ }
42
+
43
  public static function default_api_url() {
44
  $url = getenv('UB_API_URL');
45
  return $url ? $url : 'https://api.unbounce.com';
63
  return get_option(UBConfig::UB_REMOTE_LOG_URL_KEY, UBConfig::default_remote_log_url());
64
  }
65
 
66
+ public static function remote_events_url() {
67
+ return get_option(UBConfig::UB_REMOTE_EVENTS_URL_KEY, UBConfig::default_remote_events_url());
68
+ }
69
+
70
  public static function api_url() {
71
  return get_option(UBConfig::UB_API_URL_KEY, UBConfig::default_api_url());
72
  }
91
  return get_option(UBConfig::UB_REMOTE_DEBUG_KEY, 0) == 1;
92
  }
93
 
94
+ public static function create_none_response() {
95
+ return array(array('status' => 'NONE'), null, null, null);
96
+ }
97
+
98
+ public static function create_same_response($etag, $max_age) {
99
+ return array(array('status' => 'SAME'), $etag, $max_age, null);
100
+ }
101
+
102
+ public static function create_new_response($etag, $max_age, $proxyable_url_set) {
103
+ return array(array('status' => 'NEW'), $etag, $max_age, $proxyable_url_set);
104
+ }
105
+
106
+ public static function create_failure_response($failure_message) {
107
+ return array(array('status' => 'FAILURE',
108
+ 'failure_message' => $failure_message),
109
+ null, null, null);
110
+ }
111
+
112
+ public static function domain() {
113
+ return parse_url(get_home_url(), PHP_URL_HOST);
114
+ }
115
+
116
+ public static function domain_with_port() {
117
+ $port = parse_url(get_home_url(), PHP_URL_PORT);
118
+ $host = parse_url(get_home_url(), PHP_URL_HOST);
119
+ if ($port) {
120
+ return $host . ':' . $port;
121
+ } else {
122
+ return $host;
123
+ }
124
+ }
125
+
126
  public static function fetch_proxyable_url_set($domain, $etag, $ps_domain) {
127
  if(!$domain) {
128
+ $failure_message = 'Domain not provided, not fetching sitemap.xml';
129
+ UBLogger::warning($failure_message);
130
+ return UBConfig::create_failure_response($failure_message);
131
  }
132
 
133
  try {
178
 
179
  if ($success) {
180
  UBLogger::debug("Retrieved new routes, HTTP code: '$http_code'");
181
+ return UBConfig::create_new_response($etag, $max_age, $result);
182
  }
183
  else {
184
  $errors = join(', ', $result);
185
+ $failure_message = "An error occurred while processing pages, XML errors: '$errors'";
186
+ UBLogger::warning($failure_message);
187
+ return UBConfig::create_failure_response($failure_message);
188
  }
189
  }
190
  if ($http_code == 304) {
191
  UBLogger::debug("Routes have not changed, HTTP code: '$http_code'");
192
+ return UBConfig::create_same_response($etag, $max_age);
193
  }
194
  if ($http_code == 404) {
195
  UBLogger::debug("No routes to retrieve, HTTP code: '$http_code'");
196
+ return UBConfig::create_none_response();
197
  }
198
  else {
199
+ $failure_message = "An error occurred while retrieving routes; HTTP code: '$http_code'; Error: " . $curl_error;
200
+ UBLogger::warning($failure_message);
201
+ return UBConfig::create_failure_response($failure_message);
202
  }
203
+
204
  } catch (Exception $e) {
205
+ $failure_message = "An error occurred while retrieving routes; Error: " . $e;
206
+ UBLogger::warning($failure_message);
207
+ return UBConfig::create_failure_response($failure_message);
208
  }
209
  }
210
 
276
 
277
  list($routes_status, $etag, $max_age, $proxyable_url_set_new) = $result_array;
278
 
279
+ if ($routes_status['status'] == 'NEW') {
280
  $domain_info['proxyable_url_set'] = $proxyable_url_set_new;
281
  $domain_info['proxyable_url_set_etag'] = $etag;
282
  $domain_info['proxyable_url_set_cache_timeout'] = $max_age;
283
  }
284
+ elseif ($routes_status['status'] == 'SAME') {
285
  // Just extend the cache
286
  $domain_info['proxyable_url_set_cache_timeout'] = $max_age;
287
  }
288
+ elseif ($routes_status['status'] == 'NONE') {
289
  $domain_info['proxyable_url_set'] = array();
290
  $domain_info['proxyable_url_set_etag'] = null;
291
  }
292
+ elseif ($routes_status['status'] == 'FAILURE') {
293
  UBLogger::warning('Route fetching failed');
294
  }
295
  else {
296
  UBLogger::warning("Unknown response from route fetcher: '$routes_status'");
297
  }
298
 
299
+ // Creation of domain_info entry
300
  $domain_info['proxyable_url_set_fetched_at'] = $current_time;
301
+ $domain_info['last_status'] = $routes_status['status'];
302
+ if ($routes_status['status'] == 'FAILURE') {
303
+ $domain_info['failure_message'] = $routes_status['failure_message'];
304
+ }
305
  $domains_info[$domain] = $domain_info;
306
  $options_setter(UBConfig::UB_ROUTES_CACHE_KEY, $domains_info);
307
  }
309
  return UBUtil::array_select_by_key($domain_info,
310
  array('proxyable_url_set',
311
  'proxyable_url_set_fetched_at',
312
+ 'failure_message',
313
  'last_status'));
314
  }
315
 
328
  return in_array($domain, UBConfig::authorized_domains());
329
  }
330
 
331
+ public static function update_authorization_options($domains, $data) {
332
+ update_option(UBConfig::UB_USER_ID_KEY, $data['user_id']);
333
+ update_option(UBConfig::UB_DOMAIN_ID_KEY, $data['domain_id']);
334
+ update_option(UBConfig::UB_CLIENT_ID_KEY, $data['client_id']);
335
+ update_option(UBConfig::UB_AUTHORIZED_DOMAINS_KEY, $domains);
336
+ update_option(UBConfig::UB_HAS_AUTHORIZED_KEY, true);
337
+ }
338
+
339
+ public static function int_min() {
340
+ return (PHP_INT_MAX * -1) - 1;
341
+ }
342
+
343
  }
344
  ?>
UBDiagnostics.php CHANGED
@@ -28,16 +28,10 @@ class UBDiagnostics {
28
  return array(
29
  'PHP Version' => phpversion(),
30
  'WordPress Version' => UBDiagnostics::wordpress_version(),
31
- 'Unbounce Plugin Version' => "1.0.4",
32
  'Permalink Structure' => get_option('permalink_structure', ''),
33
  'Domain' => $domain,
34
  'Domain Authorized' => print_r(UBConfig::is_authorized_domain($domain), true),
35
- 'Domain Information' => print_r($domain_info, true),
36
- 'Page Server Domain' => UBConfig::page_server_domain(),
37
- 'Remote Log URL' => UBConfig::remote_log_url(),
38
- 'API URL' => UBConfig::api_url(),
39
- 'API Client ID' => UBConfig::api_client_id(),
40
- 'Authorized Domains' => join(', ', UBConfig::authorized_domains()),
41
  'Has Authorized' => print_r(UBConfig::has_authorized(), true),
42
  'Active Plugins' => print_r(get_option('active_plugins'), true),
43
  'Plugin Details' => print_r(get_plugins(), true),
@@ -45,10 +39,22 @@ class UBDiagnostics {
45
  'Configuration Options' => print_r(ini_get_all(), true),
46
  'Extensions' => print_r(get_loaded_extensions(), true),
47
  'Operating System' => php_uname(),
48
- 'Checks' => print_r(UBDiagnostics::checks($domain, $domain_info), true)
 
49
  );
50
  }
51
 
 
 
 
 
 
 
 
 
 
 
 
52
  private static function curl_version() {
53
  if(function_exists('curl_version')) {
54
  return print_r(curl_version(), true);
@@ -65,7 +71,7 @@ class UBDiagnostics {
65
 
66
  private static function last_status_success($domain_info) {
67
  $last_status = UBUtil::array_fetch($domain_info, 'last_status');
68
- return $last_status !== 'FAILURE' && $last_status !== null;
69
  }
70
 
71
  }
28
  return array(
29
  'PHP Version' => phpversion(),
30
  'WordPress Version' => UBDiagnostics::wordpress_version(),
31
+ 'Unbounce Plugin Version' => "1.0.5",
32
  'Permalink Structure' => get_option('permalink_structure', ''),
33
  'Domain' => $domain,
34
  'Domain Authorized' => print_r(UBConfig::is_authorized_domain($domain), true),
 
 
 
 
 
 
35
  'Has Authorized' => print_r(UBConfig::has_authorized(), true),
36
  'Active Plugins' => print_r(get_option('active_plugins'), true),
37
  'Plugin Details' => print_r(get_plugins(), true),
39
  'Configuration Options' => print_r(ini_get_all(), true),
40
  'Extensions' => print_r(get_loaded_extensions(), true),
41
  'Operating System' => php_uname(),
42
+ 'Checks' => print_r(UBDiagnostics::checks($domain, $domain_info), true),
43
+ 'Options' => print_r(UBDiagnostics::ub_options(), true)
44
  );
45
  }
46
 
47
+ private static function ub_options() {
48
+ $ub_options = array_filter(wp_load_alloptions(), function($key) {
49
+ return strpos($key, 'ub-') === 0;
50
+ }, ARRAY_FILTER_USE_KEY);
51
+
52
+ return array_map(function($value) {
53
+ $unserialized = @unserialize($value);
54
+ return $unserialized ? $unserialized : $value;
55
+ }, $ub_options);
56
+ }
57
+
58
  private static function curl_version() {
59
  if(function_exists('curl_version')) {
60
  return print_r(curl_version(), true);
71
 
72
  private static function last_status_success($domain_info) {
73
  $last_status = UBUtil::array_fetch($domain_info, 'last_status');
74
+ return $last_status !== null && $last_status !== 'FAILURE';
75
  }
76
 
77
  }
UBEvents.php ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class UBEvents {
4
+
5
+ public static function successful_authorization_event($data) {
6
+ return UBEvents::event('WordPressSuccessfulAuthorizationEventV1.0',
7
+ UBEvents::authorization_event($data));
8
+ }
9
+
10
+ public static function failed_authorization_event($data) {
11
+ return UBEvents::event('WordPressFailedAuthorizationEventV1.0',
12
+ UBEvents::authorization_event($data));
13
+ }
14
+
15
+ public static function log_event($data) {
16
+ return UBEvents::event('WordpressLogV1.0', $data);
17
+ }
18
+
19
+ private static function authorization_event($data) {
20
+ $event = array(
21
+ 'domain_name' => $data['domain_name'],
22
+ 'first_authorization' => (boolean) $data['first_authorization'],
23
+ 'metadata' => array()
24
+ );
25
+
26
+ if ($data['domain_id']) {
27
+ $event['domain_id'] = UBEvents::maybe_convert_to_int($data['domain_id']);
28
+ }
29
+
30
+ if ($data['user_id']) {
31
+ $event['metadata']['user_id'] = UBEvents::maybe_convert_to_int($data['user_id']);
32
+ }
33
+
34
+ if ($data['client_id']) {
35
+ $event['metadata']['client_id'] = UBEvents::maybe_convert_to_int($data['client_id']);
36
+ }
37
+
38
+ return $event;
39
+ }
40
+
41
+ private static function maybe_convert_to_int($str) {
42
+ if(is_numeric($str)) {
43
+ return intval($str);
44
+ } else {
45
+ return $str;
46
+ }
47
+ }
48
+
49
+ private static function event($type, $data) {
50
+ $event = array_merge(array('type' => $type),
51
+ UBEvents::default_attributes(),
52
+ $data);
53
+ $json_unescaped = json_encode($event);
54
+ return str_replace('\\/', '/', $json_unescaped);
55
+ }
56
+
57
+ private static function default_attributes() {
58
+ $datetime = new DateTime('NOW', new DateTimeZone('UTC'));
59
+ return array('id' => uniqid(),
60
+ 'time_sent' => $datetime->format('Y-m-d\TH:i:s\Z'),
61
+ 'source' => UBConfig::UB_USER_AGENT . ' ' . gethostname());
62
+ }
63
+ }
UBHTTP.php CHANGED
@@ -227,6 +227,51 @@ class UBHTTP {
227
  }
228
  }
229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  }
231
 
232
  ?>
227
  }
228
  }
229
 
230
+ public static function send_event_to_events_gateway($url, $data_string) {
231
+ try {
232
+ $stream_function = function($curl, $str) { return strlen($str); };
233
+
234
+ $curl = curl_init();
235
+ $curl_options = array(
236
+ CURLOPT_URL => $url,
237
+ CURLOPT_CUSTOMREQUEST => 'POST',
238
+ CURLOPT_USERAGENT => UBConfig::UB_USER_AGENT,
239
+ CURLOPT_FOLLOWLOCATION => false,
240
+ CURLOPT_HTTPHEADER => array(
241
+ 'Content-Type: application/json',
242
+ 'Content-Length: ' . strlen($data_string)
243
+ ),
244
+ CURLOPT_HEADERFUNCTION => $stream_function,
245
+ CURLOPT_WRITEFUNCTION => $stream_function,
246
+ CURLOPT_POSTFIELDS => $data_string,
247
+ CURLOPT_TIMEOUT => 2
248
+ );
249
+ curl_setopt_array($curl, $curl_options);
250
+ $success = curl_exec($curl);
251
+ $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
252
+
253
+ if(!$success) {
254
+ $message = 'Unable to send log messages to ' . $url . ': "'
255
+ . curl_error($curl) . '" - HTTP status: ' . curl_errno($curl);
256
+ UBLogger::warning($message);
257
+ } elseif($http_code >= 200 && $http_code < 300) {
258
+ $message = 'Successfully sent log messsages to ' . $url
259
+ . ' - HTTP status: ' . $http_code;
260
+ UBLogger::debug($message);
261
+ } else {
262
+ $message = 'Unable to send log messages to ' . $url
263
+ . ' - HTTP status: ' . $http_code;
264
+ UBLogger::warning($message);
265
+ }
266
+
267
+ curl_close($curl);
268
+ } catch (Exception $e) {
269
+ $message = 'Unable to send log messages to ' . $url
270
+ . ' - Error: ' . $e;
271
+ UBLogger::warning($message);
272
+ }
273
+ }
274
+
275
  }
276
 
277
  ?>
UBLogger.php CHANGED
@@ -1,6 +1,7 @@
1
  <?php
2
 
3
  require_once dirname(__FILE__) . '/UBConfig.php';
 
4
 
5
  class UBLogger {
6
 
@@ -15,56 +16,11 @@ class UBLogger {
15
 
16
  public static function upload_logs_to_unbounce($url) {
17
  if(UBConfig::remote_debug_logging_enabled()) {
18
- $datetime = new DateTime('NOW', new DateTimeZone('UTC'));
19
  $data = array(
20
- 'type' => 'WordpressLogV1.0',
21
  'messages' => $GLOBALS['wp_log'][UBConfig::UB_PLUGIN_NAME],
22
  'vars' => $GLOBALS['wp_log'][UBConfig::UB_PLUGIN_NAME . '-vars'],
23
- 'id' => uniqid(),
24
- 'time_sent' => $datetime->format('Y-m-d\TH:i:s.000\Z'),
25
- 'source' => UBConfig::UB_USER_AGENT . ' ' . gethostname()
26
  );
27
- $json_unescaped = json_encode($data);
28
- $data_string = str_replace('\\/', '/', $json_unescaped);
29
-
30
- try {
31
- $curl = curl_init();
32
- $curl_options = array(
33
- CURLOPT_URL => $url,
34
- CURLOPT_CUSTOMREQUEST => 'POST',
35
- CURLOPT_USERAGENT => UBConfig::UB_USER_AGENT,
36
- CURLOPT_FOLLOWLOCATION => false,
37
- CURLOPT_HTTPHEADER => array(
38
- 'Content-Type: application/json',
39
- 'Content-Length: ' . strlen($data_string)
40
- ),
41
- CURLOPT_POSTFIELDS => $data_string,
42
- CURLOPT_TIMEOUT => 2
43
- );
44
- curl_setopt_array($curl, $curl_options);
45
- $success = curl_exec($curl);
46
- $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
47
-
48
- if(!$success) {
49
- $message = 'Unable to send log messages to ' . $url . ': "'
50
- . curl_error($curl) . '" - HTTP status: ' . curl_errno($curl);
51
- UBLogger::warning($message);
52
- } elseif($http_code >= 200 && $http_code < 300) {
53
- $message = 'Successfully sent log messsages to ' . $url
54
- . ' - HTTP status: ' . $http_code;
55
- UBLogger::debug($message);
56
- } else {
57
- $message = 'Unable to send log messages to ' . $url
58
- . ' - HTTP status: ' . $http_code;
59
- UBLogger::warning($message);
60
- }
61
-
62
- curl_close($curl);
63
- } catch (Exception $e) {
64
- $message = 'Unable to send log messages to ' . $url
65
- . ' - Error: ' . $e;
66
- UBLogger::warning($message);
67
- }
68
  }
69
  }
70
 
1
  <?php
2
 
3
  require_once dirname(__FILE__) . '/UBConfig.php';
4
+ require_once dirname(__FILE__) . '/UBEvents.php';
5
 
6
  class UBLogger {
7
 
16
 
17
  public static function upload_logs_to_unbounce($url) {
18
  if(UBConfig::remote_debug_logging_enabled()) {
 
19
  $data = array(
 
20
  'messages' => $GLOBALS['wp_log'][UBConfig::UB_PLUGIN_NAME],
21
  'vars' => $GLOBALS['wp_log'][UBConfig::UB_PLUGIN_NAME . '-vars'],
 
 
 
22
  );
23
+ UBHTTP::send_event_to_events_gateway($url, UBEvents::log_event($data));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  }
25
  }
26
 
Unbounce-Page.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Unbounce
4
  Plugin URI: http://unbounce.com
5
  Description: Publish Unbounce Landing Pages to your Wordpress Domain.
6
- Version: 1.0.4
7
  Author: Unbounce
8
  Author URI: http://unbounce.com
9
  License: GPLv2
@@ -17,6 +17,7 @@ require_once dirname(__FILE__) . '/UBLogger.php';
17
  require_once dirname(__FILE__) . '/UBHTTP.php';
18
  require_once dirname(__FILE__) . '/UBIcon.php';
19
  require_once dirname(__FILE__) . '/UBPageTable.php';
 
20
 
21
  register_activation_hook(__FILE__, function() {
22
  add_option(UBConfig::UB_ROUTES_CACHE_KEY, array());
@@ -32,6 +33,11 @@ register_activation_hook(__FILE__, function() {
32
  add_option(UBConfig::UB_AUTHORIZED_DOMAINS_KEY,
33
  UBConfig::default_authorized_domains());
34
  add_option(UBConfig::UB_HAS_AUTHORIZED_KEY);
 
 
 
 
 
35
  });
36
 
37
  register_deactivation_hook(__FILE__, function() {
@@ -43,12 +49,16 @@ register_deactivation_hook(__FILE__, function() {
43
  delete_option(UBConfig::UB_API_CLIENT_ID_KEY);
44
  delete_option(UBConfig::UB_AUTHORIZED_DOMAINS_KEY);
45
  delete_option(UBConfig::UB_HAS_AUTHORIZED_KEY);
 
 
 
 
46
  });
47
 
48
  add_action('init', function() {
49
  UBLogger::setup_logger();
50
 
51
- $domain = parse_url(get_home_url(), PHP_URL_HOST);
52
 
53
  if(!UBConfig::is_authorized_domain($domain)) {
54
  UBLogger::info("Domain: $domain has not been authorized");
@@ -89,6 +99,10 @@ add_action('init', function() {
89
  UBLogger::debug("ignoring request to URL " . $current_url);
90
  }
91
  elseif ($url_purpose == 'HealthCheck') {
 
 
 
 
92
  header('Content-Type: application/json');
93
  $version = UBConfig::UB_VERSION;
94
  echo "{\"ub_wordpress\":{\"version\":\"$version\"}}";
@@ -104,11 +118,22 @@ add_action('init', function() {
104
  define('DONOTCACHEPAGE', true);
105
  }
106
 
107
- // Disable CDN for W3 Total Cache
108
  if(!defined('DONOTCDN')) {
109
  define('DONOTCDN', true);
110
  }
111
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  UBLogger::debug("perform ''" . $url_purpose . "'' on received URL " . $current_url);
113
 
114
  $cookies_to_forward = UBUtil::array_select_by_key($_COOKIE,
@@ -136,7 +161,7 @@ add_action('init', function() {
136
 
137
  exit(0);
138
  }
139
- });
140
 
141
  add_action('admin_init', function() {
142
  UBUtil::clear_flash();
@@ -164,10 +189,13 @@ add_action('admin_init', function() {
164
  plugins_url('css/unbounce-pages.css', __FILE__));
165
  }, 0);
166
 
167
- function authorization_button($text, $wrap_in_p = false, $is_primary = true, $outer_text = '') {
168
  $set_domains_url = admin_url('admin-post.php?action=set_unbounce_domains');
169
  echo "<form method='post' action='$set_domains_url'>";
170
  echo "<input type='hidden' name='domains' />";
 
 
 
171
  echo $outer_text;
172
  $style = $outer_text ? 'vertical-align: baseline' : '';
173
  echo get_submit_button($text,
@@ -178,6 +206,7 @@ function authorization_button($text, $wrap_in_p = false, $is_primary = true, $ou
178
  'data-redirect-uri' => admin_url('admin.php?page=unbounce-pages'),
179
  'data-api-url' => UBConfig::api_url(),
180
  'data-api-client-id' => UBConfig::api_client_id(),
 
181
  'style' => $style));
182
  echo '</form>';
183
  }
@@ -216,7 +245,7 @@ function render_unbounce_pages($domain_info, $domain) {
216
 
217
  $proxyable_url_set_fetched_at = UBUtil::array_fetch($domain_info, 'proxyable_url_set_fetched_at');
218
  echo '<p>Last refreshed ' . UBUtil::time_ago($proxyable_url_set_fetched_at) . '.</p>';
219
- authorization_button('Update WordPress Enabled Domains', false, false);
220
  echo '</p>';
221
  echo '</div>';
222
 
@@ -233,7 +262,7 @@ function render_unbounce_pages($domain_info, $domain) {
233
  echo '<a href="http://documentation.unbounce.com/hc/en-us/articles/205069824-Integrating-with-WordPress" target="_blank">Check out our knowledge base.</a>';
234
  $diagnostics_url = admin_url('admin.php?page=unbounce-pages-diagnostics');
235
  echo "<br/><a class='ub-diagnostics-link' href='${diagnostics_url}'>Diagnostics</a>";
236
- echo '<p class="ub-version">Unbounce Version 1.0.4</p>';
237
  });
238
  } else {
239
  if (UBConfig::has_authorized()) {
@@ -248,11 +277,11 @@ function render_unbounce_pages($domain_info, $domain) {
248
  'onclick' => 'swap_primary_buttons("add-domain", "set-unbounce-domains");'));
249
  echo '</form>';
250
 
251
- authorization_button('Update WordPress Enabled Domains', false, false, 'After adding your domain in Unbounce, come back here and ');
252
  } else {
253
  echo '<div class="ub-authorize-message">Before you can publish your pages to WordPress you will have to authorize your Unbounce account.</div>';
254
 
255
- authorization_button('Authorize With Unbounce', true);
256
 
257
  $try_unbounce_url = "http://unbounce.com/landing-pages-for-wordpress/";
258
  echo "<form method='get' action='$try_unbounce_url' target='_blank'>";
@@ -270,7 +299,7 @@ function render_unbounce_pages($domain_info, $domain) {
270
  echo ' target="_blank">Check out our knowledge base.</a>';
271
  $diagnostics_url = admin_url('admin.php?page=unbounce-pages-diagnostics');
272
  echo "<br/><a class='ub-diagnostics-link' href='${diagnostics_url}'>Diagnostics</a>";
273
- echo '<p class="ub-version">Unbounce Version 1.0.4</p>';
274
  });
275
  }
276
 
@@ -306,7 +335,7 @@ function render_unbounce_pages_diagnostics($domain, $domain_info) {
306
  add_action('admin_menu', function() {
307
  // Main admin page
308
  $print_admin_panel = function() {
309
- $domain = parse_url(get_home_url(), PHP_URL_HOST);
310
  $domain_info = UBConfig::read_unbounce_domain_info($domain, false);
311
 
312
  render_unbounce_pages($domain_info, $domain);
@@ -321,7 +350,7 @@ add_action('admin_menu', function() {
321
 
322
  // Diagnostics page
323
  $print_diagnostics_panel = function() {
324
- $domain = parse_url(get_home_url(), PHP_URL_HOST);
325
  $domain_info = UBConfig::read_unbounce_domain_info($domain, false);
326
 
327
  render_unbounce_pages_diagnostics($domain, $domain_info);
@@ -336,13 +365,29 @@ add_action('admin_menu', function() {
336
  });
337
 
338
  add_action('admin_post_set_unbounce_domains', function() {
339
- $domains_json = UBUtil::array_fetch($_POST, 'domains', '');
340
- $domains = explode(',', $domains_json);
341
 
342
- if($domains_json && is_array($domains)) {
343
- update_option(UBConfig::UB_AUTHORIZED_DOMAINS_KEY, $domains);
344
- update_option(UBConfig::UB_HAS_AUTHORIZED_KEY, true);
345
  $authorization = 'success';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
  } else {
347
  $authorization = 'failure';
348
  }
@@ -355,7 +400,7 @@ add_action('admin_post_set_unbounce_domains', function() {
355
  });
356
 
357
  add_action('admin_post_flush_unbounce_pages', function() {
358
- $domain = parse_url(get_home_url(), PHP_URL_HOST);
359
  // Expire cache and redirect
360
  $_domain_info = UBConfig::read_unbounce_domain_info($domain, true);
361
  status_header(301);
@@ -364,7 +409,7 @@ add_action('admin_post_flush_unbounce_pages', function() {
364
  });
365
 
366
  add_action('shutdown', function() {
367
- UBLogger::upload_logs_to_unbounce(get_option(UBConfig::UB_REMOTE_LOG_URL_KEY));
368
  });
369
 
370
  ?>
3
  Plugin Name: Unbounce
4
  Plugin URI: http://unbounce.com
5
  Description: Publish Unbounce Landing Pages to your Wordpress Domain.
6
+ Version: 1.0.5
7
  Author: Unbounce
8
  Author URI: http://unbounce.com
9
  License: GPLv2
17
  require_once dirname(__FILE__) . '/UBHTTP.php';
18
  require_once dirname(__FILE__) . '/UBIcon.php';
19
  require_once dirname(__FILE__) . '/UBPageTable.php';
20
+ require_once dirname(__FILE__) . '/UBEvents.php';
21
 
22
  register_activation_hook(__FILE__, function() {
23
  add_option(UBConfig::UB_ROUTES_CACHE_KEY, array());
33
  add_option(UBConfig::UB_AUTHORIZED_DOMAINS_KEY,
34
  UBConfig::default_authorized_domains());
35
  add_option(UBConfig::UB_HAS_AUTHORIZED_KEY);
36
+ add_option(UBConfig::UB_REMOTE_EVENTS_URL_KEY,
37
+ UBConfig::default_remote_events_url());
38
+ add_option(UBConfig::UB_USER_ID_KEY);
39
+ add_option(UBConfig::UB_DOMAIN_ID_KEY);
40
+ add_option(UBConfig::UB_CLIENT_ID_KEY);
41
  });
42
 
43
  register_deactivation_hook(__FILE__, function() {
49
  delete_option(UBConfig::UB_API_CLIENT_ID_KEY);
50
  delete_option(UBConfig::UB_AUTHORIZED_DOMAINS_KEY);
51
  delete_option(UBConfig::UB_HAS_AUTHORIZED_KEY);
52
+ delete_option(UBConfig::UB_REMOTE_EVENTS_URL_KEY);
53
+ delete_option(UBConfig::UB_USER_ID_KEY);
54
+ delete_option(UBConfig::UB_DOMAIN_ID_KEY);
55
+ delete_option(UBConfig::UB_CLIENT_ID_KEY);
56
  });
57
 
58
  add_action('init', function() {
59
  UBLogger::setup_logger();
60
 
61
+ $domain = UBConfig::domain();
62
 
63
  if(!UBConfig::is_authorized_domain($domain)) {
64
  UBLogger::info("Domain: $domain has not been authorized");
99
  UBLogger::debug("ignoring request to URL " . $current_url);
100
  }
101
  elseif ($url_purpose == 'HealthCheck') {
102
+ if (UBConfig::domain_with_port() !== UBUtil::array_fetch($_SERVER, 'HTTP_HOST')) {
103
+ http_response_code(412);
104
+ }
105
+
106
  header('Content-Type: application/json');
107
  $version = UBConfig::UB_VERSION;
108
  echo "{\"ub_wordpress\":{\"version\":\"$version\"}}";
118
  define('DONOTCACHEPAGE', true);
119
  }
120
 
 
121
  if(!defined('DONOTCDN')) {
122
  define('DONOTCDN', true);
123
  }
124
 
125
+ if(!defined('DONOTCACHEDB')) {
126
+ define('DONOTCACHEDB', true);
127
+ }
128
+
129
+ if(!defined('DONOTMINIFY')) {
130
+ define('DONOTMINIFY', true);
131
+ }
132
+
133
+ if(!defined('DONOTCACHEOBJECT')) {
134
+ define('DONOTCACHEOBJECT', true);
135
+ }
136
+
137
  UBLogger::debug("perform ''" . $url_purpose . "'' on received URL " . $current_url);
138
 
139
  $cookies_to_forward = UBUtil::array_select_by_key($_COOKIE,
161
 
162
  exit(0);
163
  }
164
+ }, UBConfig::int_min());
165
 
166
  add_action('admin_init', function() {
167
  UBUtil::clear_flash();
189
  plugins_url('css/unbounce-pages.css', __FILE__));
190
  }, 0);
191
 
192
+ function authorization_button($text, $domain, $wrap_in_p = false, $is_primary = true, $outer_text = '') {
193
  $set_domains_url = admin_url('admin-post.php?action=set_unbounce_domains');
194
  echo "<form method='post' action='$set_domains_url'>";
195
  echo "<input type='hidden' name='domains' />";
196
+ echo "<input type='hidden' name='user_id' />";
197
+ echo "<input type='hidden' name='domain_id' />";
198
+ echo "<input type='hidden' name='client_id' />";
199
  echo $outer_text;
200
  $style = $outer_text ? 'vertical-align: baseline' : '';
201
  echo get_submit_button($text,
206
  'data-redirect-uri' => admin_url('admin.php?page=unbounce-pages'),
207
  'data-api-url' => UBConfig::api_url(),
208
  'data-api-client-id' => UBConfig::api_client_id(),
209
+ 'data-wordpress-domain-name' => $domain,
210
  'style' => $style));
211
  echo '</form>';
212
  }
245
 
246
  $proxyable_url_set_fetched_at = UBUtil::array_fetch($domain_info, 'proxyable_url_set_fetched_at');
247
  echo '<p>Last refreshed ' . UBUtil::time_ago($proxyable_url_set_fetched_at) . '.</p>';
248
+ authorization_button('Update WordPress Enabled Domains', $domain, false, false);
249
  echo '</p>';
250
  echo '</div>';
251
 
262
  echo '<a href="http://documentation.unbounce.com/hc/en-us/articles/205069824-Integrating-with-WordPress" target="_blank">Check out our knowledge base.</a>';
263
  $diagnostics_url = admin_url('admin.php?page=unbounce-pages-diagnostics');
264
  echo "<br/><a class='ub-diagnostics-link' href='${diagnostics_url}'>Diagnostics</a>";
265
+ echo '<p class="ub-version">Unbounce Version 1.0.5</p>';
266
  });
267
  } else {
268
  if (UBConfig::has_authorized()) {
277
  'onclick' => 'swap_primary_buttons("add-domain", "set-unbounce-domains");'));
278
  echo '</form>';
279
 
280
+ authorization_button('Update WordPress Enabled Domains', $domain, false, false, 'After adding your domain in Unbounce, come back here and ');
281
  } else {
282
  echo '<div class="ub-authorize-message">Before you can publish your pages to WordPress you will have to authorize your Unbounce account.</div>';
283
 
284
+ authorization_button('Authorize With Unbounce', $domain, true);
285
 
286
  $try_unbounce_url = "http://unbounce.com/landing-pages-for-wordpress/";
287
  echo "<form method='get' action='$try_unbounce_url' target='_blank'>";
299
  echo ' target="_blank">Check out our knowledge base.</a>';
300
  $diagnostics_url = admin_url('admin.php?page=unbounce-pages-diagnostics');
301
  echo "<br/><a class='ub-diagnostics-link' href='${diagnostics_url}'>Diagnostics</a>";
302
+ echo '<p class="ub-version">Unbounce Version 1.0.5</p>';
303
  });
304
  }
305
 
335
  add_action('admin_menu', function() {
336
  // Main admin page
337
  $print_admin_panel = function() {
338
+ $domain = UBConfig::domain();
339
  $domain_info = UBConfig::read_unbounce_domain_info($domain, false);
340
 
341
  render_unbounce_pages($domain_info, $domain);
350
 
351
  // Diagnostics page
352
  $print_diagnostics_panel = function() {
353
+ $domain = UBConfig::domain();
354
  $domain_info = UBConfig::read_unbounce_domain_info($domain, false);
355
 
356
  render_unbounce_pages_diagnostics($domain, $domain_info);
365
  });
366
 
367
  add_action('admin_post_set_unbounce_domains', function() {
368
+ $domains_list = UBUtil::array_fetch($_POST, 'domains', '');
369
+ $domains = explode(',', $domains_list);
370
 
371
+ if($domains && is_array($domains)) {
 
 
372
  $authorization = 'success';
373
+ $has_authorized = get_option(UBConfig::UB_HAS_AUTHORIZED_KEY, false);
374
+
375
+ $data = array(
376
+ 'domain_name' => UBConfig::domain(),
377
+ 'first_authorization' => !$has_authorized,
378
+ 'user_id' => UBUtil::array_fetch($_POST, 'user_id', ''),
379
+ 'client_id' => UBUtil::array_fetch($_POST, 'client_id', ''),
380
+ 'domain_id' => UBUtil::array_fetch($_POST, 'domain_id', ''),
381
+ );
382
+
383
+ UBConfig::update_authorization_options($domains, $data);
384
+
385
+ if(UBConfig::is_authorized_domain(UBConfig::domain())) {
386
+ $event = UBEvents::successful_authorization_event($data);
387
+ } else {
388
+ $event = UBEvents::failed_authorization_event($data);
389
+ }
390
+ UBHTTP::send_event_to_events_gateway(UBConfig::remote_events_url(), $event);
391
  } else {
392
  $authorization = 'failure';
393
  }
400
  });
401
 
402
  add_action('admin_post_flush_unbounce_pages', function() {
403
+ $domain = UBConfig::domain();
404
  // Expire cache and redirect
405
  $_domain_info = UBConfig::read_unbounce_domain_info($domain, true);
406
  status_header(301);
409
  });
410
 
411
  add_action('shutdown', function() {
412
+ UBLogger::upload_logs_to_unbounce(UBConfig::remote_log_url());
413
  });
414
 
415
  ?>
js/set-unbounce-domains.js CHANGED
@@ -1,22 +1,29 @@
1
  (function($) {
2
 
3
- function apiGet(url, token) {
4
- var request = $.ajax({
5
  url: url,
6
  method: 'get',
7
  headers: { 'Authorization': 'Bearer ' + token,
8
  'Accept': 'application/vnd.unbounce.api.v0.4+json' },
9
  dataType: 'json'
10
- });
11
- return Rx.Observable.fromPromise(request.promise());
 
 
 
12
  }
13
 
14
- function getDomainNames(result) {
15
- if($.isArray(result.domains)) {
16
  return Rx.Observable.fromArray(
17
- $.map(result.domains, function(domain) {
18
  if(domain && domain.name) {
19
- return domain.name;
 
 
 
 
20
  } else {
21
  throw 'Unable to fetch domain name';
22
  }
@@ -26,20 +33,37 @@
26
  }
27
  }
28
 
29
- function getUserSubAccountIds(user) {
30
  if(user.metadata && user.metadata.related && $.isArray(user.metadata.related.sub_accounts)) {
31
- return Rx.Observable.fromArray(
32
- $.map(user.metadata.related.sub_accounts, function(sub_account_url) {
33
- var pieces = sub_account_url.split('/');
34
- return pieces[pieces.length - 1];
35
- }));
36
  } else {
37
  throw 'Unable to fetch user';
38
  }
39
  }
40
 
41
- function postDomainsToWordpress($form, domains) {
42
- $form.find('[name="domains"]').val(domains.join(','));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  $form.submit();
44
  }
45
 
@@ -63,6 +87,7 @@
63
  apiUrl = $submitButton.attr('data-api-url'),
64
  redirectUri = $submitButton.attr('data-redirect-uri'),
65
  apiClientId = $submitButton.attr('data-api-client-id'),
 
66
  getTokenUrl = apiUrl + '/oauth/authorize?response_type=token&client_id=' + apiClientId + '&redirect_uri=' + redirectUri,
67
  getUserUrl = apiUrl + '/users/self?limit=1000',
68
  getSubAccountsUrl = apiUrl + '/accounts/{accountId}/sub_accounts',
@@ -83,19 +108,25 @@
83
  if(accessToken) {
84
  loadingUI($submitButton, loadingText);
85
 
86
- var source = apiGet(getUserUrl, accessToken)
 
87
  .flatMap(function(user) {
88
- return getUserSubAccountIds(user);
89
- })
90
- .flatMap(function (subAccountId) {
91
- return apiGet(getDomainsUrl.replace('{subAccountId}', subAccountId), accessToken);
92
- })
93
- .flatMap(function(domains) {
94
- return getDomainNames(domains);
95
  }).toArray().publish(),
96
- subscription = source.subscribe(
 
 
 
 
 
 
97
  function (domains) {
98
- postDomainsToWordpress($form, domains);
99
  },
100
  function (error) {
101
  failureUI($form, $submitButton, originalText);
@@ -106,7 +137,9 @@
106
  // as we'll also have access to the list of domains
107
  });
108
 
109
- source.connect();
 
 
110
  }
111
  }
112
  });
1
  (function($) {
2
 
3
+ function apiGetPromise(url, token) {
4
+ return $.ajax({
5
  url: url,
6
  method: 'get',
7
  headers: { 'Authorization': 'Bearer ' + token,
8
  'Accept': 'application/vnd.unbounce.api.v0.4+json' },
9
  dataType: 'json'
10
+ }).promise();
11
+ }
12
+
13
+ function apiGet(url, token) {
14
+ return Rx.Observable.fromPromise(apiGetPromise(url, token));
15
  }
16
 
17
+ function getDomainData(subAccount, domains) {
18
+ if($.isArray(domains)) {
19
  return Rx.Observable.fromArray(
20
+ $.map(domains, function(domain) {
21
  if(domain && domain.name) {
22
+ return {
23
+ clientId: subAccount.id,
24
+ domainId: domain.id,
25
+ name: domain.name
26
+ };
27
  } else {
28
  throw 'Unable to fetch domain name';
29
  }
33
  }
34
  }
35
 
36
+ function getUserSubAccounts(user, token) {
37
  if(user.metadata && user.metadata.related && $.isArray(user.metadata.related.sub_accounts)) {
38
+ var promises = $.map(user.metadata.related.sub_accounts, function(subAccountUrl) {
39
+ return apiGetPromise(subAccountUrl, token);
40
+ });
41
+ return Rx.Observable.fromArray(promises).flatMap(Rx.Observable.fromPromise);
 
42
  } else {
43
  throw 'Unable to fetch user';
44
  }
45
  }
46
 
47
+ function postDomainsToWordpress($form, data, wordpressDomainName) {
48
+ if($.isArray(data.domains)) {
49
+ var wordpressDomains = data.domains.filter(function(domain) {
50
+ return domain.name === wordpressDomainName;
51
+ });
52
+
53
+ if(wordpressDomains[0]) {
54
+ var wordpressDomain = wordpressDomains[0];
55
+ $form.find('[name="domain_id"]').val(wordpressDomain.domainId);
56
+ $form.find('[name="client_id"]').val(wordpressDomain.clientId);
57
+ }
58
+
59
+ var domainNames = $.map(data.domains, function(domain) {
60
+ return domain.name;
61
+ });
62
+
63
+ $form.find('[name="user_id"]').val(data.userId);
64
+ $form.find('[name="domains"]').val(domainNames.join(','));
65
+ }
66
+
67
  $form.submit();
68
  }
69
 
87
  apiUrl = $submitButton.attr('data-api-url'),
88
  redirectUri = $submitButton.attr('data-redirect-uri'),
89
  apiClientId = $submitButton.attr('data-api-client-id'),
90
+ wordpressDomainName = $submitButton.attr('data-wordpress-domain-name'),
91
  getTokenUrl = apiUrl + '/oauth/authorize?response_type=token&client_id=' + apiClientId + '&redirect_uri=' + redirectUri,
92
  getUserUrl = apiUrl + '/users/self?limit=1000',
93
  getSubAccountsUrl = apiUrl + '/accounts/{accountId}/sub_accounts',
108
  if(accessToken) {
109
  loadingUI($submitButton, loadingText);
110
 
111
+ var userObservable = apiGet(getUserUrl, accessToken).publish(),
112
+ domainsObservable = userObservable
113
  .flatMap(function(user) {
114
+ return getUserSubAccounts(user, accessToken);
115
+ }).flatMap(function (subAccount) {
116
+ return apiGet(getDomainsUrl.replace('{subAccountId}', subAccount.id), accessToken)
117
+ .flatMap(function(response) {
118
+ return getDomainData(subAccount, response.domains);
119
+ });
 
120
  }).toArray().publish(),
121
+ userDomainsObservable = Rx.Observable.zip(
122
+ userObservable,
123
+ domainsObservable,
124
+ function(user, domains) {
125
+ return {userId: user.id, domains: domains};
126
+ }).publish(),
127
+ subscription = userDomainsObservable.subscribe(
128
  function (domains) {
129
+ postDomainsToWordpress($form, domains, wordpressDomainName);
130
  },
131
  function (error) {
132
  failureUI($form, $submitButton, originalText);
137
  // as we'll also have access to the list of domains
138
  });
139
 
140
+ userDomainsObservable.connect();
141
+ domainsObservable.connect();
142
+ userObservable.connect();
143
  }
144
  }
145
  });
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: unbouncewordpress
3
  Tags: Unbounce, AB testing, A/B testing, split testing, CRO, conversion optimization, wordpress landing page, wp landing pages, splash pages, landing pages, squeeze pages, lead gen, lead generation, email list, responsive landing pages, templates, inbound marketing, ppc, analytics
4
  Requires at least: 4.1.5
5
  Tested up to: 4.3
6
- Stable tag: 1.0.4
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
3
  Tags: Unbounce, AB testing, A/B testing, split testing, CRO, conversion optimization, wordpress landing page, wp landing pages, splash pages, landing pages, squeeze pages, lead gen, lead generation, email list, responsive landing pages, templates, inbound marketing, ppc, analytics
4
  Requires at least: 4.1.5
5
  Tested up to: 4.3
6
+ Stable tag: 1.0.5
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9