544 lines
27 KiB
PHP
544 lines
27 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Fulfils paid orders through the API, in the background (Action Scheduler):
|
|
* domain registration (POST /v1/domains/register), transfer (POST /v1/domains/transfer), plans (POST /v1/services/purchase),
|
|
* SMS credits (POST /v1/sms/credits/purchase) and renewals (see MOZOUT_RESELLER_Renew). Each request carries an Idempotency-Key made
|
|
* from the order item, so a retry never buys twice. Long operations are followed with GET /v1/actions/{id}.
|
|
*
|
|
* Item states (order item meta _mozout_reseller_state): new, queued, submitted, done, failed, waiting_credit, waiting_invoice.
|
|
*/
|
|
class MOZOUT_RESELLER_Fulfil
|
|
{
|
|
const GROUP = 'mozout-reseller';
|
|
|
|
public static function init()
|
|
{
|
|
add_action('woocommerce_order_status_processing', array(__CLASS__, 'on_paid'), 20);
|
|
add_action('woocommerce_order_status_completed', array(__CLASS__, 'on_paid'), 20);
|
|
add_action('mozout_reseller_run_order', array(__CLASS__, 'run_order'));
|
|
add_action('mozout_reseller_poll', array(__CLASS__, 'poll'), 10, 3);
|
|
add_action('mozout_reseller_daily', array(__CLASS__, 'daily'));
|
|
add_action('mozout_reseller_retry_waiting', array(__CLASS__, 'retry_waiting'));
|
|
add_action('init', array(__CLASS__, 'schedule'));
|
|
if (is_admin()) {
|
|
add_action('add_meta_boxes', array(__CLASS__, 'meta_box'), 30);
|
|
add_action('admin_post_mozout_reseller_item', array(__CLASS__, 'admin_item'));
|
|
}
|
|
}
|
|
|
|
public static function activate()
|
|
{
|
|
if (!wp_next_scheduled('mozout_reseller_daily')) {
|
|
wp_schedule_event(time() + 600, 'daily', 'mozout_reseller_daily');
|
|
}
|
|
if (!wp_next_scheduled('mozout_reseller_retry_waiting')) {
|
|
wp_schedule_event(time() + 900, 'hourly', 'mozout_reseller_retry_waiting');
|
|
}
|
|
}
|
|
|
|
public static function deactivate()
|
|
{
|
|
wp_clear_scheduled_hook('mozout_reseller_daily');
|
|
wp_clear_scheduled_hook('mozout_reseller_retry_waiting');
|
|
}
|
|
|
|
public static function schedule()
|
|
{
|
|
if (!wp_next_scheduled('mozout_reseller_daily') || !wp_next_scheduled('mozout_reseller_retry_waiting')) {
|
|
self::activate();
|
|
}
|
|
}
|
|
|
|
private static function later($hook, array $args, $seconds)
|
|
{
|
|
if (function_exists('as_schedule_single_action')) {
|
|
as_schedule_single_action(time() + $seconds, $hook, $args, self::GROUP);
|
|
} else {
|
|
wp_schedule_single_event(time() + $seconds, $hook, $args);
|
|
}
|
|
}
|
|
|
|
/* ---- Paid orders ---- */
|
|
|
|
public static function on_paid($order_id)
|
|
{
|
|
if ((int) MOZOUT_RESELLER_Settings::get('auto_run') !== 1) {
|
|
return;
|
|
}
|
|
$order = wc_get_order($order_id);
|
|
if (!$order || $order->get_meta('_mozout_reseller_has_items') !== 'yes') {
|
|
return;
|
|
}
|
|
$queued = false;
|
|
foreach ($order->get_items() as $item) {
|
|
if ($item->get_meta('_mozout_reseller_type') && $item->get_meta('_mozout_reseller_state') === 'new') {
|
|
$item->update_meta_data('_mozout_reseller_state', 'queued');
|
|
$item->save();
|
|
$queued = true;
|
|
}
|
|
}
|
|
if ($queued) {
|
|
self::later('mozout_reseller_run_order', array((int) $order_id), 5);
|
|
}
|
|
}
|
|
|
|
/** Runs the queued items of an order: domains first, so a plan can use a domain bought in the same order. */
|
|
public static function run_order($order_id)
|
|
{
|
|
$order = wc_get_order($order_id);
|
|
if (!$order) {
|
|
return;
|
|
}
|
|
$items = array();
|
|
foreach ($order->get_items() as $item) {
|
|
$type = (string) $item->get_meta('_mozout_reseller_type');
|
|
if ($type !== '' && in_array($item->get_meta('_mozout_reseller_state'), array('queued', 'waiting_credit'), true)) {
|
|
$items[] = $item;
|
|
}
|
|
}
|
|
usort($items, function ($a, $b) {
|
|
$rank = array('domain_register' => 0, 'domain_transfer' => 1, 'renew_domain' => 2, 'plan' => 3, 'renew_service' => 4, 'sms' => 5);
|
|
$ra = isset($rank[$a->get_meta('_mozout_reseller_type')]) ? $rank[$a->get_meta('_mozout_reseller_type')] : 9;
|
|
$rb = isset($rank[$b->get_meta('_mozout_reseller_type')]) ? $rank[$b->get_meta('_mozout_reseller_type')] : 9;
|
|
return $ra - $rb;
|
|
});
|
|
foreach ($items as $item) {
|
|
self::run_item($order, $item);
|
|
}
|
|
self::maybe_complete($order);
|
|
}
|
|
|
|
private static function key($order, $item, $what)
|
|
{
|
|
// A new round after a definite refusal (nothing was bought), so the API does not return the saved refusal again.
|
|
$round = (int) $item->get_meta('_mozout_reseller_round');
|
|
return 'mozout-reseller-' . substr(md5(home_url()), 0, 10) . '-' . $order->get_id() . '-' . $item->get_id() . '-' . $what . ($round ? '-r' . $round : '');
|
|
}
|
|
|
|
/** Sends one item to the API. */
|
|
public static function run_item($order, $item)
|
|
{
|
|
$type = (string) $item->get_meta('_mozout_reseller_type');
|
|
$product = $item->get_product();
|
|
$domain = (string) $item->get_meta('mozout_reseller_domain');
|
|
$qty = max(1, (int) $item->get_quantity());
|
|
$res = null;
|
|
$label = $item->get_name();
|
|
switch ($type) {
|
|
case 'domain_register':
|
|
$body = array('domain' => $domain, 'years' => max(1, (int) MOZOUT_RESELLER_Product::meta($product, 'years', '1')));
|
|
$ns = array_filter(array_map('trim', explode(',', (string) MOZOUT_RESELLER_Product::meta($product, 'ns'))));
|
|
if (count($ns) >= 2) {
|
|
$body['nameservers'] = array_values($ns);
|
|
}
|
|
$item->update_meta_data('_mozout_reseller_years', $body['years']);
|
|
$res = MOZOUT_RESELLER_Api::request('POST', '/v1/domains/register', $body, array('timeout' => 45, 'idempotency_key' => self::key($order, $item, 'register')));
|
|
break;
|
|
case 'domain_transfer':
|
|
$res = MOZOUT_RESELLER_Api::request('POST', '/v1/domains/transfer', array('domain' => $domain, 'transfer_code' => (string) $item->get_meta('_mozout_reseller_epp')), array('timeout' => 45, 'idempotency_key' => self::key($order, $item, 'transfer')));
|
|
break;
|
|
case 'plan':
|
|
$body = array(
|
|
'product_id' => (int) MOZOUT_RESELLER_Product::meta($product, 'plan'),
|
|
'billing_cycle' => (string) MOZOUT_RESELLER_Product::meta($product, 'cycle', 'annually'),
|
|
);
|
|
$opts = (array) json_decode((string) MOZOUT_RESELLER_Product::meta($product, 'options', '{}'), true);
|
|
if ($opts) {
|
|
$body['config_options'] = (object) $opts;
|
|
}
|
|
if ($domain !== '' && self::domain_in_account($domain)) {
|
|
$body['domain'] = $domain;
|
|
}
|
|
if (!$body['product_id']) {
|
|
self::fail($order, $item, __('The product has no plan chosen in its Fulfilment tab.', 'mozout-reseller'));
|
|
return;
|
|
}
|
|
$res = MOZOUT_RESELLER_Api::request('POST', '/v1/services/purchase', $body, array('timeout' => 60, 'idempotency_key' => self::key($order, $item, 'purchase')));
|
|
break;
|
|
case 'sms':
|
|
$credits = $qty * max(1, (int) MOZOUT_RESELLER_Product::meta($product, 'sms_qty', '1000'));
|
|
$res = MOZOUT_RESELLER_Api::request('POST', '/v1/sms/credits/purchase', array('quantity' => $credits), array('timeout' => 45, 'idempotency_key' => self::key($order, $item, 'sms')));
|
|
break;
|
|
case 'renew_domain':
|
|
case 'renew_service':
|
|
MOZOUT_RESELLER_Renew::run_item($order, $item);
|
|
return;
|
|
default:
|
|
return;
|
|
}
|
|
$item->update_meta_data('_mozout_reseller_tries', (int) $item->get_meta('_mozout_reseller_tries') + 1);
|
|
if (is_wp_error($res)) {
|
|
$code = $res->get_error_code();
|
|
if (in_array($code, array('insufficient_credit', 'spending_limit_exceeded'), true)) {
|
|
self::wait_credit($order, $item, $res->get_error_message());
|
|
return;
|
|
}
|
|
$status = (int) MOZOUT_RESELLER_Admin::error_status($res);
|
|
if ($status === 0 || $status === 429 || $status >= 500) {
|
|
// Network or a busy API: try again in a few minutes, up to 6 times.
|
|
if ((int) $item->get_meta('_mozout_reseller_tries') < 6) {
|
|
$item->update_meta_data('_mozout_reseller_state', 'queued');
|
|
$item->update_meta_data('_mozout_reseller_error', $res->get_error_message());
|
|
$item->save();
|
|
self::later('mozout_reseller_run_order', array($order->get_id()), 300);
|
|
return;
|
|
}
|
|
}
|
|
self::fail($order, $item, $res->get_error_message());
|
|
return;
|
|
}
|
|
$d = is_array($res['data']) ? $res['data'] : array();
|
|
$sim = !empty($d['simulated']);
|
|
$item->update_meta_data('_mozout_reseller_sim', $sim ? 'yes' : 'no');
|
|
$item->update_meta_data('_mozout_reseller_result', wp_json_encode(array_intersect_key($d, array_flip(array('service_id', 'order_id', 'invoice_id', 'amount', 'currency', 'domain', 'temporary_address', 'credits', 'name')))));
|
|
if (!empty($d['service_id'])) {
|
|
$item->update_meta_data('_mozout_reseller_service', (int) $d['service_id']);
|
|
}
|
|
if ($sim && isset($d['credit_sufficient']) && $d['credit_sufficient'] === false) {
|
|
$order->add_order_note(sprintf(
|
|
/* translators: %s: product name */
|
|
__('%s: in Sandbox the account credit would not be enough. In Production this order would wait for credit.', 'mozout-reseller'),
|
|
$label
|
|
));
|
|
}
|
|
if (!empty($d['action_id']) && !$sim) {
|
|
$item->update_meta_data('_mozout_reseller_state', 'submitted');
|
|
$item->update_meta_data('_mozout_reseller_action', (string) $d['action_id']);
|
|
$item->save();
|
|
$order->add_order_note(sprintf(
|
|
/* translators: %s: product name */
|
|
__('%s: sent to the API. Following the result.', 'mozout-reseller'),
|
|
$label
|
|
));
|
|
self::later('mozout_reseller_poll', array($order->get_id(), $item->get_id(), 1), 60);
|
|
return;
|
|
}
|
|
self::done($order, $item, $d);
|
|
}
|
|
|
|
/** True when the domain already belongs to the account (GET /v1/domains/{domain}). */
|
|
public static function domain_in_account($domain)
|
|
{
|
|
$res = MOZOUT_RESELLER_Api::request('GET', '/v1/domains/' . rawurlencode($domain), null, array('timeout' => 15));
|
|
return !is_wp_error($res) && !empty($res['data']['domain']);
|
|
}
|
|
|
|
/** Follows a long operation. */
|
|
public static function poll($order_id, $item_id, $attempt)
|
|
{
|
|
$order = wc_get_order($order_id);
|
|
if (!$order) {
|
|
return;
|
|
}
|
|
$item = $order->get_item($item_id);
|
|
if (!$item || $item->get_meta('_mozout_reseller_state') !== 'submitted') {
|
|
return;
|
|
}
|
|
$res = MOZOUT_RESELLER_Api::request('GET', '/v1/actions/' . rawurlencode((string) $item->get_meta('_mozout_reseller_action')), null, array('timeout' => 20));
|
|
$status = is_wp_error($res) ? 'unknown' : (isset($res['data']['status']) ? $res['data']['status'] : 'unknown');
|
|
if ($status === 'done') {
|
|
$result = isset($res['data']['result']) && is_array($res['data']['result']) ? $res['data']['result'] : array();
|
|
if (!empty($result['service_id'])) {
|
|
$item->update_meta_data('_mozout_reseller_service', (int) $result['service_id']);
|
|
}
|
|
self::done($order, $item, $result);
|
|
self::maybe_complete($order);
|
|
return;
|
|
}
|
|
if ($status === 'failed') {
|
|
$r = isset($res['data']['result']) ? $res['data']['result'] : null;
|
|
$why = is_array($r) && !empty($r['message']) ? (string) $r['message'] : (is_string($r) ? $r : __('The API could not finish this operation.', 'mozout-reseller'));
|
|
self::fail($order, $item, $why);
|
|
return;
|
|
}
|
|
// queued, running or not readable now: look again later, less often each time, for about 2 days.
|
|
$attempt = (int) $attempt + 1;
|
|
if ($attempt > 60) {
|
|
self::fail($order, $item, __('The API took too long to answer. Check the order in your account.', 'mozout-reseller'));
|
|
return;
|
|
}
|
|
self::later('mozout_reseller_poll', array($order_id, $item_id, $attempt), min(3600, 60 * $attempt));
|
|
}
|
|
|
|
public static function done($order, $item, array $d)
|
|
{
|
|
$type = (string) $item->get_meta('_mozout_reseller_type');
|
|
$sim = $item->get_meta('_mozout_reseller_sim') === 'yes';
|
|
$item->update_meta_data('_mozout_reseller_state', 'done');
|
|
$item->delete_meta_data('_mozout_reseller_error');
|
|
if (strpos($type, 'renew_') !== 0) {
|
|
$item->update_meta_data('_mozout_reseller_status', $type === 'domain_transfer' ? 'pending_transfer' : ($type === 'sms' ? 'done' : 'active'));
|
|
}
|
|
if ($type === 'domain_register') {
|
|
$years = max(1, (int) $item->get_meta('_mozout_reseller_years'));
|
|
$item->update_meta_data('_mozout_reseller_expires', gmdate('Y-m-d', strtotime('+' . $years . ' years')));
|
|
}
|
|
if ($type === 'sms') {
|
|
$credits = isset($d['credits']) ? (int) $d['credits'] : 0;
|
|
$uid = $order->get_customer_id();
|
|
if ($uid && $credits) {
|
|
update_user_meta($uid, '_mozout_reseller_sms_credits', (int) get_user_meta($uid, '_mozout_reseller_sms_credits', true) + $credits);
|
|
}
|
|
}
|
|
$item->save();
|
|
$note = $sim ? __('%s: done in Sandbox (simulated, nothing was bought).', 'mozout-reseller') : __('%s: done.', 'mozout-reseller');
|
|
$order->add_order_note(sprintf($note, $item->get_name()));
|
|
MOZOUT_RESELLER_Log::add('order', true, sprintf($note, $item->get_name()), array('order' => $order->get_id()));
|
|
if (strpos($type, 'renew_') === 0) {
|
|
MOZOUT_RESELLER_Renew::finished($order, $item);
|
|
} else {
|
|
MOZOUT_RESELLER_Emails::ready($order, $item);
|
|
}
|
|
}
|
|
|
|
public static function fail($order, $item, $why)
|
|
{
|
|
$item->update_meta_data('_mozout_reseller_state', 'failed');
|
|
$item->update_meta_data('_mozout_reseller_error', (string) $why);
|
|
$item->save();
|
|
/* translators: 1: product name, 2: reason */
|
|
$text = sprintf(__('%1$s: not done. %2$s', 'mozout-reseller'), $item->get_name(), $why);
|
|
$order->add_order_note($text);
|
|
MOZOUT_RESELLER_Log::add('order', false, $text, array('order' => $order->get_id()));
|
|
MOZOUT_RESELLER_Emails::admin_alert($order, $text);
|
|
}
|
|
|
|
private static function wait_credit($order, $item, $why)
|
|
{
|
|
$first = $item->get_meta('_mozout_reseller_state') !== 'waiting_credit';
|
|
$item->update_meta_data('_mozout_reseller_round', (int) $item->get_meta('_mozout_reseller_round') + 1);
|
|
$item->update_meta_data('_mozout_reseller_state', 'waiting_credit');
|
|
$item->update_meta_data('_mozout_reseller_error', (string) $why);
|
|
$item->save();
|
|
if ($first) {
|
|
/* translators: 1: product name, 2: reason */
|
|
$text = sprintf(__('%1$s: waiting for credit in your account. %2$s It runs again by itself every hour.', 'mozout-reseller'), $item->get_name(), $why);
|
|
$order->add_order_note($text);
|
|
MOZOUT_RESELLER_Log::add('order', false, $text, array('order' => $order->get_id()));
|
|
MOZOUT_RESELLER_Emails::admin_alert($order, $text);
|
|
}
|
|
}
|
|
|
|
/** Marks the order completed when every item is done (setting). */
|
|
public static function maybe_complete($order)
|
|
{
|
|
if ((int) MOZOUT_RESELLER_Settings::get('complete_order') !== 1 || $order->get_status() !== 'processing') {
|
|
return;
|
|
}
|
|
foreach ($order->get_items() as $item) {
|
|
if ($item->get_meta('_mozout_reseller_type') && $item->get_meta('_mozout_reseller_state') !== 'done') {
|
|
return;
|
|
}
|
|
if (!$item->get_meta('_mozout_reseller_type') && $item->get_product() && $item->get_product()->needs_shipping()) {
|
|
return;
|
|
}
|
|
}
|
|
$order->update_status('completed', __('Everything was fulfilled through the API.', 'mozout-reseller'));
|
|
}
|
|
|
|
/** Hourly: orders waiting for credit or for a renewal invoice try again. */
|
|
public static function retry_waiting()
|
|
{
|
|
foreach (self::orders_with_items(array('waiting_credit', 'waiting_invoice'), 50) as $order) {
|
|
$run = false;
|
|
foreach ($order->get_items() as $item) {
|
|
if ($item->get_meta('_mozout_reseller_state') === 'waiting_credit') {
|
|
$run = true;
|
|
} elseif ($item->get_meta('_mozout_reseller_state') === 'waiting_invoice') {
|
|
MOZOUT_RESELLER_Renew::run_item($order, $item);
|
|
}
|
|
}
|
|
if ($run) {
|
|
self::run_order($order->get_id());
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Orders with fulfilled items, newest first. With $states, only orders that have an item in one of them.
|
|
* @return WC_Order[]
|
|
*/
|
|
public static function orders_with_items($states = array(), $limit = 200)
|
|
{
|
|
$orders = wc_get_orders(array(
|
|
'limit' => $limit,
|
|
'orderby' => 'date',
|
|
'order' => 'DESC',
|
|
'meta_query' => array(array('key' => '_mozout_reseller_has_items', 'value' => 'yes')), // phpcs:ignore WordPress.DB.SlowDBQuery
|
|
'status' => array('wc-processing', 'wc-completed', 'wc-on-hold'),
|
|
));
|
|
if (!$states) {
|
|
return $orders;
|
|
}
|
|
return array_values(array_filter($orders, function ($o) use ($states) {
|
|
foreach ($o->get_items() as $it) {
|
|
if (in_array($it->get_meta('_mozout_reseller_state'), $states, true)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}));
|
|
}
|
|
|
|
/** Daily: reads the state and expiry of what was sold, tells the customer about changes and sends renewal reminders. */
|
|
public static function daily()
|
|
{
|
|
if (MOZOUT_RESELLER_Settings::api_key() === '') {
|
|
return;
|
|
}
|
|
foreach (self::orders_with_items(array('done'), 300) as $order) {
|
|
foreach ($order->get_items() as $item) {
|
|
if ($item->get_meta('_mozout_reseller_state') !== 'done' || $item->get_meta('_mozout_reseller_sim') === 'yes') {
|
|
continue;
|
|
}
|
|
self::sync_item($order, $item);
|
|
MOZOUT_RESELLER_Renew::maybe_remind($order, $item);
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Reads the current state of one item from the API and saves it. */
|
|
public static function sync_item($order, $item)
|
|
{
|
|
$type = (string) $item->get_meta('_mozout_reseller_type');
|
|
$old = (string) $item->get_meta('_mozout_reseller_status');
|
|
if (in_array($type, array('domain_register', 'domain_transfer'), true)) {
|
|
$res = MOZOUT_RESELLER_Api::request('GET', '/v1/domains/' . rawurlencode((string) $item->get_meta('mozout_reseller_domain')), null, array('timeout' => 15));
|
|
if (is_wp_error($res)) {
|
|
return;
|
|
}
|
|
$status = strtolower((string) $res['data']['status']);
|
|
$expires = !empty($res['data']['expires_at']) ? substr((string) $res['data']['expires_at'], 0, 10) : '';
|
|
} elseif ($type === 'plan' && (int) $item->get_meta('_mozout_reseller_service')) {
|
|
$res = MOZOUT_RESELLER_Api::request('GET', '/v1/services/' . (int) $item->get_meta('_mozout_reseller_service'), null, array('timeout' => 15));
|
|
if (is_wp_error($res)) {
|
|
return;
|
|
}
|
|
$status = strtolower((string) $res['data']['status']);
|
|
$expires = !empty($res['data']['next_due_date']) ? substr((string) $res['data']['next_due_date'], 0, 10) : '';
|
|
if (!$item->get_meta('mozout_reseller_domain') && !empty($res['data']['domain'])) {
|
|
$item->update_meta_data('_mozout_reseller_site', (string) $res['data']['domain']);
|
|
}
|
|
} else {
|
|
return;
|
|
}
|
|
$item->update_meta_data('_mozout_reseller_status', $status);
|
|
if ($expires !== '') {
|
|
$item->update_meta_data('_mozout_reseller_expires', $expires);
|
|
}
|
|
$item->save();
|
|
if ($old !== '' && $old !== $status) {
|
|
$order->add_order_note(sprintf(
|
|
/* translators: 1: product name, 2: old state, 3: new state */
|
|
__('%1$s: state changed from %2$s to %3$s.', 'mozout-reseller'),
|
|
$item->get_name(),
|
|
MOZOUT_RESELLER_Account::status_name($old),
|
|
MOZOUT_RESELLER_Account::status_name($status)
|
|
));
|
|
MOZOUT_RESELLER_Emails::status_changed($order, $item, $old, $status);
|
|
}
|
|
}
|
|
|
|
/* ---- Order screen ---- */
|
|
|
|
public static function meta_box()
|
|
{
|
|
$screens = array('shop_order');
|
|
if (function_exists('wc_get_page_screen_id')) {
|
|
$screens[] = wc_get_page_screen_id('shop-order');
|
|
}
|
|
foreach (array_unique($screens) as $screen) {
|
|
add_meta_box('mozout-reseller-order', __('Fulfilment by the API', 'mozout-reseller'), array(__CLASS__, 'meta_box_html'), $screen, 'normal', 'default');
|
|
}
|
|
}
|
|
|
|
public static function state_name($s)
|
|
{
|
|
$names = array(
|
|
'new' => __('Waiting for payment', 'mozout-reseller'),
|
|
'queued' => __('In the queue', 'mozout-reseller'),
|
|
'submitted' => __('Sent, waiting for the result', 'mozout-reseller'),
|
|
'done' => __('Done', 'mozout-reseller'),
|
|
'failed' => __('Not done', 'mozout-reseller'),
|
|
'waiting_credit' => __('Waiting for credit', 'mozout-reseller'),
|
|
'waiting_invoice' => __('Waiting for the renewal invoice', 'mozout-reseller'),
|
|
);
|
|
return isset($names[$s]) ? $names[$s] : $s;
|
|
}
|
|
|
|
public static function meta_box_html($post_or_order)
|
|
{
|
|
$order = ($post_or_order instanceof WP_Post) ? wc_get_order($post_or_order->ID) : $post_or_order;
|
|
if (!$order || !is_a($order, 'WC_Order')) {
|
|
return;
|
|
}
|
|
$rows = array();
|
|
foreach ($order->get_items() as $item) {
|
|
if ($item->get_meta('_mozout_reseller_type')) {
|
|
$rows[] = $item;
|
|
}
|
|
}
|
|
if (!$rows) {
|
|
echo '<p>' . esc_html__('Nothing in this order is fulfilled by the API.', 'mozout-reseller') . '</p>';
|
|
return;
|
|
}
|
|
wp_enqueue_style('mozout-reseller-admin', MOZOUT_RESELLER_URL . 'assets/admin.css', array(), MOZOUT_RESELLER_VERSION);
|
|
echo '<div class="mozout-reseller-wrap"><table class="widefat striped"><thead><tr><th>' . esc_html__('Item', 'mozout-reseller') . '</th><th>' . esc_html__('State', 'mozout-reseller') . '</th><th>' . esc_html__('In the account', 'mozout-reseller') . '</th><th></th></tr></thead><tbody>';
|
|
foreach ($rows as $item) {
|
|
$state = (string) $item->get_meta('_mozout_reseller_state');
|
|
$cls = $state === 'done' ? 'is-good' : ($state === 'failed' ? 'is-bad' : 'is-warn');
|
|
echo '<tr><td><strong>' . esc_html($item->get_name()) . '</strong>' . ($item->get_meta('mozout_reseller_domain') ? '<br>' . esc_html($item->get_meta('mozout_reseller_domain')) : '') . '</td>';
|
|
echo '<td><span class="mozout-reseller-badge ' . esc_attr($cls) . '">' . esc_html(self::state_name($state)) . '</span>' . ($item->get_meta('_mozout_reseller_sim') === 'yes' ? ' <span class="mozout-reseller-badge">Sandbox</span>' : '');
|
|
if ($item->get_meta('_mozout_reseller_error') && $state !== 'done') {
|
|
echo '<br><span class="mozout-reseller-muted">' . esc_html($item->get_meta('_mozout_reseller_error')) . '</span>';
|
|
}
|
|
echo '</td><td>' . esc_html(MOZOUT_RESELLER_Account::status_name((string) $item->get_meta('_mozout_reseller_status'))) . ($item->get_meta('_mozout_reseller_expires') ? '<br><span class="mozout-reseller-muted">' . esc_html($item->get_meta('_mozout_reseller_expires')) . '</span>' : '') . '</td><td class="mozout-reseller-actions">';
|
|
if (in_array($state, array('failed', 'waiting_credit', 'new', 'queued', 'waiting_invoice'), true) && $order->is_paid()) {
|
|
echo self::item_button($order, $item, 'run', $state === 'new' ? __('Run now', 'mozout-reseller') : __('Try again', 'mozout-reseller'));
|
|
}
|
|
if ($state === 'done' && $item->get_meta('_mozout_reseller_sim') !== 'yes') {
|
|
echo self::item_button($order, $item, 'sync', __('Check the state', 'mozout-reseller'));
|
|
}
|
|
echo '</td></tr>';
|
|
}
|
|
echo '</tbody></table></div>';
|
|
}
|
|
|
|
private static function item_button($order, $item, $op, $label)
|
|
{
|
|
$url = wp_nonce_url(add_query_arg(array('action' => 'mozout_reseller_item', 'order' => $order->get_id(), 'item' => $item->get_id(), 'op' => $op), admin_url('admin-post.php')), 'mozout_reseller_item_' . $item->get_id());
|
|
return '<a class="button button-small" href="' . esc_url($url) . '">' . esc_html($label) . '</a> ';
|
|
}
|
|
|
|
public static function admin_item()
|
|
{
|
|
$item_id = isset($_GET['item']) ? (int) $_GET['item'] : 0;
|
|
$order = wc_get_order(isset($_GET['order']) ? (int) $_GET['order'] : 0);
|
|
if (!$order || !current_user_can('edit_shop_orders') || !check_admin_referer('mozout_reseller_item_' . $item_id)) {
|
|
wp_die(esc_html__('You do not have permission to do this.', 'mozout-reseller'), 403);
|
|
}
|
|
$item = $order->get_item($item_id);
|
|
$op = isset($_GET['op']) ? sanitize_key(wp_unslash($_GET['op'])) : '';
|
|
if ($item && $op === 'run') {
|
|
if ($item->get_meta('_mozout_reseller_state') === 'failed') {
|
|
$item->update_meta_data('_mozout_reseller_round', (int) $item->get_meta('_mozout_reseller_round') + 1);
|
|
}
|
|
$item->update_meta_data('_mozout_reseller_state', $item->get_meta('_mozout_reseller_state') === 'waiting_invoice' ? 'waiting_invoice' : 'queued');
|
|
$item->update_meta_data('_mozout_reseller_tries', 0);
|
|
$item->save();
|
|
if ($item->get_meta('_mozout_reseller_state') === 'waiting_invoice') {
|
|
MOZOUT_RESELLER_Renew::run_item($order, $item);
|
|
} else {
|
|
self::run_order($order->get_id());
|
|
}
|
|
} elseif ($item && $op === 'sync') {
|
|
self::sync_item($order, $item);
|
|
}
|
|
wp_safe_redirect(wp_get_referer() ? wp_get_referer() : $order->get_edit_order_url());
|
|
exit;
|
|
}
|
|
}
|