'https'))); exit; } // ADLY is another site: allowed for this one redirect add_filter('allowed_redirect_hosts', function ($hosts) { $hosts[] = wp_parse_url(ADLY_SHOP_CONNECT_URL, PHP_URL_HOST); return $hosts; }); wp_safe_redirect(self::connect_url()); exit; } /** Step 3: back from ADLY with ?adly_code=…&state=… on the plugin's page. */ // The one-time state this site stored for the admin who pressed Connect is the check here (it does what a // nonce does, and must survive the trip to ADLY and back), so no WordPress nonce is read. // phpcs:disable WordPress.Security.NonceVerification.Recommended public static function maybe_finish() { if (empty($_GET['page']) || $_GET['page'] !== 'adly-shop' || empty($_GET['adly_code']) || empty($_GET['state'])) { return; } if (!current_user_can('manage_woocommerce')) { return; } $code = preg_replace('/[^a-f0-9]/', '', strtolower(sanitize_text_field(wp_unslash($_GET['adly_code'])))); $state = preg_replace('/[^a-f0-9]/', '', strtolower(sanitize_text_field(wp_unslash($_GET['state'])))); $error = self::finish($code, $state); wp_safe_redirect(self::page_url($error ? array('adly_error' => $error) : array('adly_connected' => 1))); exit; } // phpcs:enable WordPress.Security.NonceVerification.Recommended /** The code for the keys, then the test. Null when connected, else what went wrong (adly_error). */ public static function finish($code, $state) { $owner = get_transient(self::STATE_PREFIX . $state); delete_transient(self::STATE_PREFIX . $state); // Only the state this site sent, and only for the admin who pressed Connect if (!$owner || (int) $owner !== get_current_user_id()) { return 'state'; } $res = Adly_Shop_Api::post('/plugin/exchange', array('code' => $code, 'state' => $state), 'none', 20); if (!$res['ok'] || empty($res['body']['secret_key']) || empty($res['body']['publishable_key'])) { Adly_Shop_Settings::merge(array('last_error' => self::error_text($res))); return isset($res['body']['error']['code']) && $res['body']['error']['code'] === 'code_expired' ? 'expired' : 'exchange'; } $b = $res['body']; $before = Adly_Shop_Settings::get(); Adly_Shop_Settings::save(array( 'pk' => (string) $b['publishable_key'], 'sk' => (string) $b['secret_key'], 'api_base' => !empty($b['api_base']) ? esc_url_raw($b['api_base']) : ADLY_SHOP_API_BASE, 'store_id' => isset($b['store_id']) ? (string) $b['store_id'] : '', 'store_name' => isset($b['store_name']) ? (string) $b['store_name'] : '', 'brand_name' => isset($b['brand_name']) ? (string) $b['brand_name'] : '', 'currency' => isset($b['currency']) ? (string) $b['currency'] : get_woocommerce_currency(), // Reconnecting keeps the first date: orders between the two are still reported 'connected_at' => !empty($before['connected_at']) && !empty($before['store_id']) && $before['store_id'] === (isset($b['store_id']) ? $b['store_id'] : '') ? (int) $before['connected_at'] : time(), )); self::finish_setup(); return null; } /** Step 1 without the redirect: the address at ADLY, with a fresh state for the current admin. */ public static function connect_url() { $state = bin2hex(random_bytes(20)); set_transient(self::STATE_PREFIX . $state, get_current_user_id(), 20 * MINUTE_IN_SECONDS); return add_query_arg(array( 'site' => rawurlencode(home_url('/')), 'return' => rawurlencode(self::page_url()), 'state' => $state, 'name' => rawurlencode(wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES)), 'currency' => get_woocommerce_currency(), ), ADLY_SHOP_CONNECT_URL); } /** The test, the products and the approval: right after connecting, and again from "Check again". */ public static function finish_setup() { Adly_Shop_Products::sync(); // An approved store is not tested again: ADLY answers ADLYTEST as an unknown code once it approved the store $ping = Adly_Shop_Api::get_ping(); if ($ping['ok'] && !empty($ping['body']['store']['approved'])) { $test = array('passed' => true, 'steps' => array('validate' => true, 'order' => true, 'beacon' => true, 'refund' => true), 'at' => time()); Adly_Shop_Settings::merge(array('test' => $test, 'approved' => true, 'checked_at' => time())); return $test; } $test = self::self_test(); $approved = false; if ($test['passed']) { $res = Adly_Shop_Api::post('/plugin/submit', array()); $approved = $res['ok'] && !empty($res['body']['approved']); if (!$res['ok']) { $test['error'] = self::error_text($res); } } Adly_Shop_Settings::merge(array('test' => $test, 'approved' => $approved, 'checked_at' => time())); return $test; } /** * The connection test with made-up data, nothing in the store changes: ADLY checks the test code ADLYTEST, * hears of an order that used it, sees its thank-you page, then hears of its refund. */ public static function self_test() { $s = Adly_Shop_Settings::get(); $currency = !empty($s['currency']) ? $s['currency'] : get_woocommerce_currency(); $steps = array('validate' => false, 'order' => false, 'beacon' => false, 'refund' => false); $fail = function ($step, $res) use (&$steps) { return array('passed' => false, 'steps' => $steps, 'failed' => $step, 'error' => Adly_Shop_Connect::error_text($res), 'at' => time()); }; $check = Adly_Shop_Api::post('/validate', array('code' => 'ADLYTEST', 'cart_total' => 100), 'secret', 15); if (!$check['ok'] || empty($check['body']['valid']) || empty($check['body']['check_id'])) { return $fail('validate', $check); } $steps['validate'] = true; $order_id = 'ADLYTEST-' . time(); $order = array( 'order_id' => $order_id, 'created_at' => gmdate('Y-m-d\TH:i:s\Z', time() - 120), 'updated_at' => gmdate('Y-m-d\TH:i:s\Z', time() - 60), 'status' => 'paid', 'currency' => $currency, 'total' => 90, 'discount_total' => 10, 'codes' => array('ADLYTEST'), 'check_id' => $check['body']['check_id'], 'lines' => array(array('id' => '1', 'product_id' => 'adly-test', 'title' => 'ADLY connection test', 'quantity' => 1, 'total' => 90)), 'refunds' => array(), ); $res = Adly_Shop_Api::post('/orders', $order, 'secret', 15); if (!$res['ok']) { return $fail('order', $res); } $steps['order'] = true; // The thank-you page's call, with the publishable key as the buyer's browser sends it $res = Adly_Shop_Api::post('/beacon', array('order_id' => $order_id, 'total' => 90, 'code' => 'ADLYTEST', 'check_id' => $check['body']['check_id']), 'publishable', 15); if (!$res['ok']) { return $fail('beacon', $res); } $steps['beacon'] = true; $order['updated_at'] = gmdate('Y-m-d\TH:i:s\Z'); $order['refunds'] = array(array('id' => '1', 'amount' => 90, 'reason' => 'ADLY connection test', 'created_at' => gmdate('Y-m-d\TH:i:s\Z'))); $res = Adly_Shop_Api::post('/orders', $order, 'secret', 15); if (!$res['ok']) { return $fail('refund', $res); } $steps['refund'] = true; return array('passed' => true, 'steps' => $steps, 'at' => time()); } public static function error_text($res) { if (!empty($res['body']['error']['message'])) { $m = $res['body']['error']['message']; if (!empty($res['body']['error']['details'])) { $m .= ' ' . implode(' ', (array) $res['body']['error']['details']); } return $m; } if (!empty($res['error'])) { return $res['error']; } return 'HTTP ' . (int) $res['status']; } public static function retest() { self::guard('adly_shop_retest'); if (Adly_Shop_Settings::connected()) { self::finish_setup(); } wp_safe_redirect(self::page_url(array('adly_checked' => 1))); exit; } public static function resync() { self::guard('adly_shop_products'); $res = Adly_Shop_Products::sync(); wp_safe_redirect(self::page_url(array('adly_products' => $res['ok'] ? 1 : 0))); exit; } /** ADLY hears it (the store stops taking new creators), then the keys are forgotten here. */ public static function disconnect() { self::guard('adly_shop_disconnect'); if (Adly_Shop_Settings::connected()) { Adly_Shop_Api::post('/plugin/disconnect', array(), 'secret', 10); } Adly_Shop_Settings::clear(); wp_safe_redirect(self::page_url(array('adly_disconnected' => 1))); exit; } }