210 linhas
11 KiB
PHP
210 linhas
11 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* "My services" in the WooCommerce customer account: what the customer bought, its state and expiry,
|
|
* a Renew button, the control panel link for hosting (POST /v1/services/{id}/cpanel-login) and the SMS credits.
|
|
*/
|
|
class MOZOUT_RESELLER_Account
|
|
{
|
|
public static function endpoint()
|
|
{
|
|
$e = sanitize_title((string) MOZOUT_RESELLER_Settings::get('endpoint'));
|
|
return $e !== '' ? $e : 'services';
|
|
}
|
|
|
|
public static function init()
|
|
{
|
|
add_action('init', array(__CLASS__, 'rewrite'));
|
|
add_filter('woocommerce_get_query_vars', array(__CLASS__, 'query_vars'));
|
|
add_filter('woocommerce_account_menu_items', array(__CLASS__, 'menu'));
|
|
add_action('woocommerce_account_' . self::endpoint() . '_endpoint', array(__CLASS__, 'page'));
|
|
add_filter('woocommerce_endpoint_' . self::endpoint() . '_title', array(__CLASS__, 'title'));
|
|
add_action('template_redirect', array(__CLASS__, 'actions'));
|
|
}
|
|
|
|
public static function rewrite()
|
|
{
|
|
add_rewrite_endpoint(self::endpoint(), EP_ROOT | EP_PAGES);
|
|
if (get_option('mozout_reseller_rewrite') !== self::endpoint()) {
|
|
flush_rewrite_rules(false);
|
|
update_option('mozout_reseller_rewrite', self::endpoint(), false);
|
|
}
|
|
}
|
|
|
|
public static function query_vars($vars)
|
|
{
|
|
$vars[self::endpoint()] = self::endpoint();
|
|
return $vars;
|
|
}
|
|
|
|
public static function title()
|
|
{
|
|
return __('My services', 'mozout-reseller');
|
|
}
|
|
|
|
public static function menu($items)
|
|
{
|
|
$out = array();
|
|
foreach ($items as $k => $v) {
|
|
$out[$k] = $v;
|
|
if ($k === 'orders') {
|
|
$out[self::endpoint()] = __('My services', 'mozout-reseller');
|
|
}
|
|
}
|
|
if (!isset($out[self::endpoint()])) {
|
|
$out[self::endpoint()] = __('My services', 'mozout-reseller');
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
/** Words of the API states, in the reader's language or in $lang. */
|
|
public static function status_name($s, $lang = null)
|
|
{
|
|
$t = $lang ? function ($x) use ($lang) {
|
|
return MOZOUT_RESELLER_I18n::in($lang, $x);
|
|
} : function ($x) {
|
|
return __($x, 'mozout-reseller'); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
|
|
};
|
|
$names = array(
|
|
'active' => $t('Active'), 'pending' => $t('Pending'), 'suspended' => $t('Suspended'), 'terminated' => $t('Closed'),
|
|
'cancelled' => $t('Cancelled'), 'expired' => $t('Expired'), 'grace' => $t('Expired, can still be renewed'),
|
|
'redemption' => $t('Expired, in redemption'), 'pending_registration' => $t('Being registered'),
|
|
'pending_transfer' => $t('Being transferred'), 'transferred_away' => $t('Transferred away'), 'done' => $t('Done'),
|
|
'fraud' => $t('Cancelled'), 'completed' => $t('Completed'),
|
|
);
|
|
return isset($names[$s]) ? $names[$s] : ($s === '' ? '-' : ucfirst(str_replace('_', ' ', $s)));
|
|
}
|
|
|
|
/** The items fulfilled for a customer. @return array of array(order, item) */
|
|
public static function items_of($user_id)
|
|
{
|
|
$orders = wc_get_orders(array(
|
|
'customer_id' => (int) $user_id,
|
|
'limit' => 200,
|
|
'orderby' => 'date',
|
|
'order' => 'DESC',
|
|
'meta_query' => array(array('key' => '_mozout_reseller_has_items', 'value' => 'yes')), // phpcs:ignore WordPress.DB.SlowDBQuery
|
|
));
|
|
$out = array();
|
|
foreach ($orders as $o) {
|
|
foreach ($o->get_items() as $item) {
|
|
$type = (string) $item->get_meta('_mozout_reseller_type');
|
|
if ($type === '' || strpos($type, 'renew_') === 0) {
|
|
continue;
|
|
}
|
|
$out[] = array($o, $item);
|
|
}
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
public static function renew_url($order, $item)
|
|
{
|
|
return wp_nonce_url(add_query_arg('mozout_reseller_renew', $order->get_id() . ':' . $item->get_id(), wc_get_account_endpoint_url(self::endpoint())), 'mozout_reseller_renew_' . $item->get_id());
|
|
}
|
|
|
|
private static function panel_url($order, $item)
|
|
{
|
|
return wp_nonce_url(add_query_arg('mozout_reseller_panel', $order->get_id() . ':' . $item->get_id(), wc_get_account_endpoint_url(self::endpoint())), 'mozout_reseller_panel_' . $item->get_id());
|
|
}
|
|
|
|
/** Finds an item of the logged-in customer from "order:item". @return array(order, item)|null */
|
|
private static function own($ref)
|
|
{
|
|
$p = explode(':', (string) $ref);
|
|
if (count($p) !== 2 || !is_user_logged_in()) {
|
|
return null;
|
|
}
|
|
$o = wc_get_order((int) $p[0]);
|
|
if (!$o || (int) $o->get_customer_id() !== get_current_user_id()) {
|
|
return null;
|
|
}
|
|
$i = $o->get_item((int) $p[1]);
|
|
return $i ? array($o, $i) : null;
|
|
}
|
|
|
|
public static function actions()
|
|
{
|
|
if (!empty($_GET['mozout_reseller_renew'])) {
|
|
$own = self::own(wp_unslash($_GET['mozout_reseller_renew']));
|
|
if (!$own || !wp_verify_nonce(isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : '', 'mozout_reseller_renew_' . $own[1]->get_id())) {
|
|
if (!is_user_logged_in()) {
|
|
wp_safe_redirect(add_query_arg('redirect_to', rawurlencode(add_query_arg(null, null)), wc_get_page_permalink('myaccount')));
|
|
exit;
|
|
}
|
|
wc_add_notice(__('This link is not valid. Please open it again from your email or from My services.', 'mozout-reseller'), 'error');
|
|
wp_safe_redirect(wc_get_account_endpoint_url(self::endpoint()));
|
|
exit;
|
|
}
|
|
$renewal = MOZOUT_RESELLER_Renew::create_order($own[0], $own[1]);
|
|
if (is_wp_error($renewal)) {
|
|
wc_add_notice($renewal->get_error_message(), 'error');
|
|
wp_safe_redirect(wc_get_account_endpoint_url(self::endpoint()));
|
|
exit;
|
|
}
|
|
wp_safe_redirect($renewal->get_checkout_payment_url());
|
|
exit;
|
|
}
|
|
if (!empty($_GET['mozout_reseller_panel'])) {
|
|
$own = self::own(wp_unslash($_GET['mozout_reseller_panel']));
|
|
if (!$own || !wp_verify_nonce(isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : '', 'mozout_reseller_panel_' . $own[1]->get_id())) {
|
|
wp_safe_redirect(wc_get_account_endpoint_url(self::endpoint()));
|
|
exit;
|
|
}
|
|
$sid = (int) $own[1]->get_meta('_mozout_reseller_service');
|
|
$res = $sid ? MOZOUT_RESELLER_Api::request('POST', '/v1/services/' . $sid . '/cpanel-login', null, array('timeout' => 30)) : new WP_Error('mozout_reseller_no_service', '');
|
|
$url = !is_wp_error($res) && !empty($res['data']['url']) ? (string) $res['data']['url'] : '';
|
|
if ($url === '' || strpos($url, 'https://') !== 0) {
|
|
if (is_wp_error($res) && $res->get_error_message() !== '') {
|
|
MOZOUT_RESELLER_Log::add('account', false, $res->get_error_message(), array('order' => $own[0]->get_id()));
|
|
}
|
|
wc_add_notice(__('The control panel cannot be opened right now. Please try again in a moment or contact us.', 'mozout-reseller'), 'error');
|
|
wp_safe_redirect(wc_get_account_endpoint_url(self::endpoint()));
|
|
exit;
|
|
}
|
|
wp_redirect($url);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public static function page()
|
|
{
|
|
$rows = self::items_of(get_current_user_id());
|
|
wp_enqueue_style('mozout-reseller-front', MOZOUT_RESELLER_URL . 'assets/front.css', array(), MOZOUT_RESELLER_VERSION);
|
|
$credits = (int) get_user_meta(get_current_user_id(), '_mozout_reseller_sms_credits', true);
|
|
if ($credits > 0) {
|
|
echo '<p class="mozout-reseller-credits">' . esc_html(sprintf(
|
|
/* translators: %s: number of credits */
|
|
__('SMS credits bought: %s', 'mozout-reseller'),
|
|
number_format_i18n($credits)
|
|
)) . '</p>';
|
|
}
|
|
if (!$rows) {
|
|
echo '<p>' . esc_html__('You have no services yet.', 'mozout-reseller') . '</p>';
|
|
return;
|
|
}
|
|
echo '<table class="woocommerce-table shop_table mozout-reseller-services"><thead><tr><th>' . esc_html__('Service', 'mozout-reseller') . '</th><th>' . esc_html__('State', 'mozout-reseller') . '</th><th>' . esc_html__('Valid until', 'mozout-reseller') . '</th><th></th></tr></thead><tbody>';
|
|
foreach ($rows as $r) {
|
|
list($o, $item) = $r;
|
|
$state = (string) $item->get_meta('_mozout_reseller_state');
|
|
$status = $state === 'done' ? self::status_name((string) $item->get_meta('_mozout_reseller_status')) : ($state === 'new' ? __('Waiting for payment', 'mozout-reseller') : ($state === 'failed' ? __('We are working on it', 'mozout-reseller') : __('Being set up', 'mozout-reseller')));
|
|
$domain = (string) $item->get_meta('mozout_reseller_domain');
|
|
$site = (string) $item->get_meta('_mozout_reseller_site');
|
|
echo '<tr><td data-title="' . esc_attr__('Service', 'mozout-reseller') . '"><strong>' . esc_html($item->get_name()) . '</strong>' . ($domain !== '' || $site !== '' ? '<br><small>' . esc_html($domain !== '' ? $domain : $site) . '</small>' : '') . '</td>';
|
|
echo '<td data-title="' . esc_attr__('State', 'mozout-reseller') . '">' . esc_html($status) . '</td>';
|
|
$exp = (string) $item->get_meta('_mozout_reseller_expires');
|
|
echo '<td data-title="' . esc_attr__('Valid until', 'mozout-reseller') . '">' . esc_html($exp !== '' ? date_i18n(get_option('date_format'), strtotime($exp)) : '-') . '</td><td class="mozout-reseller-services__actions">';
|
|
if (MOZOUT_RESELLER_Renew::renewable($item) && (int) MOZOUT_RESELLER_Settings::get('renewals') === 1) {
|
|
echo '<a class="button" href="' . esc_url(self::renew_url($o, $item)) . '">' . esc_html__('Renew', 'mozout-reseller') . '</a> ';
|
|
}
|
|
if ($state === 'done' && $item->get_meta('_mozout_reseller_type') === 'plan' && (int) $item->get_meta('_mozout_reseller_service') && (int) MOZOUT_RESELLER_Settings::get('panel_link') === 1 && $item->get_meta('_mozout_reseller_status') === 'active') {
|
|
echo '<a class="button" href="' . esc_url(self::panel_url($o, $item)) . '" target="_blank" rel="noopener">' . esc_html__('Control panel', 'mozout-reseller') . '</a>';
|
|
}
|
|
echo '</td></tr>';
|
|
}
|
|
echo '</tbody></table>';
|
|
}
|
|
}
|