508 linhas
26 KiB
PHP
508 linhas
26 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* WooCommerce products that the API fulfils: a "Fulfilment" tab in the product data box, the domain field
|
|
* on the product page, checks before the product goes to the cart, and the order item data.
|
|
*/
|
|
class MOZOUT_RESELLER_Product
|
|
{
|
|
const TYPES = array('domain_register', 'domain_transfer', 'plan', 'sms');
|
|
|
|
public static function type_names()
|
|
{
|
|
return array(
|
|
'' => __('Not fulfilled by the API', 'mozout-reseller'),
|
|
'domain_register' => __('Domain registration', 'mozout-reseller'),
|
|
'domain_transfer' => __('Domain transfer', 'mozout-reseller'),
|
|
'plan' => __('Hosting or server plan', 'mozout-reseller'),
|
|
'sms' => __('SMS credits', 'mozout-reseller'),
|
|
);
|
|
}
|
|
|
|
public static function init()
|
|
{
|
|
add_filter('woocommerce_product_data_tabs', array(__CLASS__, 'tab'));
|
|
add_action('woocommerce_product_data_panels', array(__CLASS__, 'panel'));
|
|
add_action('woocommerce_admin_process_product_object', array(__CLASS__, 'save'));
|
|
add_action('admin_enqueue_scripts', array(__CLASS__, 'admin_assets'));
|
|
|
|
add_action('woocommerce_before_add_to_cart_button', array(__CLASS__, 'fields'));
|
|
add_filter('woocommerce_add_to_cart_validation', array(__CLASS__, 'validate'), 10, 3);
|
|
add_filter('woocommerce_add_cart_item_data', array(__CLASS__, 'cart_item_data'), 10, 2);
|
|
add_filter('woocommerce_get_item_data', array(__CLASS__, 'show_in_cart'), 10, 2);
|
|
add_action('woocommerce_checkout_create_order_line_item', array(__CLASS__, 'order_item'), 10, 4);
|
|
add_filter('woocommerce_is_sold_individually', array(__CLASS__, 'individually'), 10, 2);
|
|
add_filter('woocommerce_order_item_display_meta_key', array(__CLASS__, 'meta_label'), 10, 3);
|
|
add_filter('woocommerce_hidden_order_itemmeta', array(__CLASS__, 'hidden_meta'));
|
|
add_filter('woocommerce_product_add_to_cart_url', array(__CLASS__, 'loop_url'), 10, 2);
|
|
add_filter('woocommerce_product_add_to_cart_text', array(__CLASS__, 'loop_text'), 10, 2);
|
|
add_filter('woocommerce_product_supports', array(__CLASS__, 'no_ajax'), 10, 3);
|
|
|
|
// Domain search plugin of the same brand: its Register button can put the name in this cart.
|
|
add_filter('mozout_domain_buy_url', array(__CLASS__, 'search_url'), 10, 3);
|
|
}
|
|
|
|
/* ---- Product meta ---- */
|
|
|
|
public static function type($product)
|
|
{
|
|
$product = is_numeric($product) ? wc_get_product($product) : $product;
|
|
if (!$product) {
|
|
return '';
|
|
}
|
|
$id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
|
|
$t = (string) get_post_meta($id, '_mozout_reseller_type', true);
|
|
return in_array($t, self::TYPES, true) ? $t : '';
|
|
}
|
|
|
|
public static function meta($product, $key, $default = '')
|
|
{
|
|
$product = is_numeric($product) ? wc_get_product($product) : $product;
|
|
if (!$product) {
|
|
return $default;
|
|
}
|
|
$id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
|
|
$v = get_post_meta($id, '_mozout_reseller_' . $key, true);
|
|
return $v === '' ? $default : $v;
|
|
}
|
|
|
|
public static function needs_domain($type)
|
|
{
|
|
return in_array($type, array('domain_register', 'domain_transfer'), true);
|
|
}
|
|
|
|
/* ---- Admin: Fulfilment tab ---- */
|
|
|
|
public static function tab($tabs)
|
|
{
|
|
$tabs['acrsl'] = array(
|
|
'label' => __('Fulfilment', 'mozout-reseller'),
|
|
'target' => 'mozout_reseller_product_data',
|
|
'class' => array(),
|
|
'priority' => 75,
|
|
);
|
|
return $tabs;
|
|
}
|
|
|
|
public static function admin_assets($hook)
|
|
{
|
|
$screen = function_exists('get_current_screen') ? get_current_screen() : null;
|
|
if (!$screen || $screen->post_type !== 'product') {
|
|
return;
|
|
}
|
|
wp_enqueue_style('mozout-reseller-admin', MOZOUT_RESELLER_URL . 'assets/admin.css', array(), MOZOUT_RESELLER_VERSION);
|
|
wp_enqueue_script('mozout-reseller-select', MOZOUT_RESELLER_URL . 'assets/select.js', array(), MOZOUT_RESELLER_VERSION, true);
|
|
wp_enqueue_script('mozout-reseller-product', MOZOUT_RESELLER_URL . 'assets/product.js', array(), MOZOUT_RESELLER_VERSION, true);
|
|
}
|
|
|
|
public static function panel()
|
|
{
|
|
global $post;
|
|
$product = wc_get_product($post->ID);
|
|
$type = self::type($product);
|
|
echo '<div id="mozout_reseller_product_data" class="panel woocommerce_options_panel mozout-reseller-wrap mozout-reseller-panel">';
|
|
if (MOZOUT_RESELLER_Settings::api_key() === '') {
|
|
echo '<p class="mozout-reseller-error">' . esc_html__('Save the API key in Settings > MozOut Reseller first.', 'mozout-reseller') . '</p>';
|
|
}
|
|
echo '<p class="form-field"><label for="mozout_reseller_type">' . esc_html__('The API fulfils it as', 'mozout-reseller') . '</label>'
|
|
. MOZOUT_RESELLER_Admin::select('mozout_reseller_type', self::type_names(), $type, 'mozout_reseller_type') . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
|
|
|
|
echo '<div class="mozout-reseller-only" data-for="domain_register domain_transfer">';
|
|
woocommerce_wp_text_input(array(
|
|
'id' => 'mozout_reseller_tlds',
|
|
'label' => __('Allowed extensions', 'mozout-reseller'),
|
|
'value' => (string) self::meta($product, 'tlds'),
|
|
'placeholder' => __('All', 'mozout-reseller'),
|
|
'desc_tip' => true,
|
|
'description' => __('For example: .net, .com. Empty accepts every extension the API sells.', 'mozout-reseller'),
|
|
));
|
|
echo '</div>';
|
|
echo '<div class="mozout-reseller-only" data-for="domain_register">';
|
|
woocommerce_wp_text_input(array(
|
|
'id' => 'mozout_reseller_years',
|
|
'label' => __('Years', 'mozout-reseller'),
|
|
'value' => (string) self::meta($product, 'years', '1'),
|
|
'type' => 'number',
|
|
'custom_attributes' => array('min' => 1, 'max' => 10),
|
|
));
|
|
woocommerce_wp_text_input(array(
|
|
'id' => 'mozout_reseller_ns',
|
|
'label' => __('Nameservers', 'mozout-reseller'),
|
|
'value' => (string) self::meta($product, 'ns'),
|
|
'placeholder' => __('Ours', 'mozout-reseller'),
|
|
'desc_tip' => true,
|
|
'description' => __('Optional, 2 to 5, separated by commas. Empty uses the nameservers of the brand.', 'mozout-reseller'),
|
|
));
|
|
echo '</div>';
|
|
|
|
echo '<div class="mozout-reseller-only" data-for="plan">';
|
|
$plans = MOZOUT_RESELLER_Settings::api_key() !== '' ? MOZOUT_RESELLER_Catalog::plans() : array();
|
|
$plan_id = (int) self::meta($product, 'plan');
|
|
if (is_wp_error($plans)) {
|
|
echo '<p class="mozout-reseller-error">' . esc_html($plans->get_error_message()) . '</p>';
|
|
$plans = array();
|
|
}
|
|
$opts = array('0' => __('Choose a plan', 'mozout-reseller'));
|
|
$kinds = MOZOUT_RESELLER_Catalog::kinds();
|
|
foreach ($plans as $p) {
|
|
$opts[(string) $p['id']] = $kinds[$p['kind']][0] . ': ' . $p['name'];
|
|
}
|
|
echo '<p class="form-field"><label for="mozout_reseller_plan">' . esc_html__('Plan', 'mozout-reseller') . '</label>' . MOZOUT_RESELLER_Admin::select('mozout_reseller_plan', $opts, (string) $plan_id, 'mozout_reseller_plan') . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
|
|
$cycle = (string) self::meta($product, 'cycle', 'annually');
|
|
$cycles = MOZOUT_RESELLER_Catalog::cycles();
|
|
if ($plan_id && isset($plans[$plan_id])) {
|
|
$cycles = array_intersect_key($cycles, $plans[$plan_id]['prices']);
|
|
}
|
|
echo '<p class="form-field"><label for="mozout_reseller_cycle">' . esc_html__('Billing cycle', 'mozout-reseller') . '</label>' . MOZOUT_RESELLER_Admin::select('mozout_reseller_cycle', $cycles, $cycle, 'mozout_reseller_cycle') . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
|
|
if ($plan_id) {
|
|
$detail = MOZOUT_RESELLER_Catalog::plan($plan_id);
|
|
$chosen = (array) json_decode((string) self::meta($product, 'options', '{}'), true);
|
|
if (!is_wp_error($detail) && !empty($detail['config_options'])) {
|
|
foreach ($detail['config_options'] as $o) {
|
|
$c = array();
|
|
foreach ($o['choices'] as $ch) {
|
|
$c[(string) $ch['id']] = $ch['name'];
|
|
}
|
|
$cur = isset($chosen[$o['id']]) ? (string) $chosen[$o['id']] : (string) $o['default_choice_id'];
|
|
echo '<p class="form-field"><label for="mozout_reseller_opt_' . (int) $o['id'] . '">' . esc_html($o['name']) . '</label>' . MOZOUT_RESELLER_Admin::select('mozout_reseller_opt[' . (int) $o['id'] . ']', $c, $cur, 'mozout_reseller_opt_' . (int) $o['id']) . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built only from escaped parts (plugin helpers escape every value).
|
|
}
|
|
} elseif (is_wp_error($detail)) {
|
|
echo '<p class="mozout-reseller-error">' . esc_html($detail->get_error_message()) . '</p>';
|
|
}
|
|
if (!empty($detail['accepts_domain'])) {
|
|
woocommerce_wp_checkbox(array(
|
|
'id' => 'mozout_reseller_ask_domain',
|
|
'label' => __('Ask for the domain', 'mozout-reseller'),
|
|
'value' => self::meta($product, 'ask_domain', 'yes'),
|
|
'description' => __('The customer writes the site domain. It is used when the domain is in your account; otherwise the plan starts with a temporary address.', 'mozout-reseller'),
|
|
));
|
|
}
|
|
} else {
|
|
echo '<p class="description mozout-reseller-note">' . esc_html__('Choose the plan and update the product to see its options.', 'mozout-reseller') . '</p>';
|
|
}
|
|
echo '</div>';
|
|
|
|
echo '<div class="mozout-reseller-only" data-for="sms">';
|
|
woocommerce_wp_text_input(array(
|
|
'id' => 'mozout_reseller_sms_qty',
|
|
'label' => __('Credits per unit', 'mozout-reseller'),
|
|
'value' => (string) self::meta($product, 'sms_qty', '1000'),
|
|
'type' => 'number',
|
|
'custom_attributes' => array('min' => 1),
|
|
'desc_tip' => true,
|
|
'description' => __('Credits bought for each unit in the order. They go to your SMS balance; the plugin keeps the count for each customer.', 'mozout-reseller'),
|
|
));
|
|
echo '</div>';
|
|
|
|
echo '<div class="mozout-reseller-only" data-for="domain_register domain_transfer plan">';
|
|
woocommerce_wp_text_input(array(
|
|
'id' => 'mozout_reseller_renew_price',
|
|
'label' => __('Renewal price', 'mozout-reseller') . ' (' . get_woocommerce_currency_symbol() . ')',
|
|
'value' => (string) self::meta($product, 'renew_price'),
|
|
'data_type' => 'price',
|
|
'placeholder' => __('Same as the product price', 'mozout-reseller'),
|
|
'desc_tip' => true,
|
|
'description' => __('What the customer pays for each renewal.', 'mozout-reseller'),
|
|
));
|
|
echo '</div>';
|
|
|
|
$cost = self::cost_line($product);
|
|
if ($cost !== '') {
|
|
echo '<p class="mozout-reseller-cost">' . wp_kses($cost, array('strong' => array(), 'span' => array('class' => array()))) . '</p>';
|
|
}
|
|
echo '</div>';
|
|
}
|
|
|
|
/** Your API price next to the WooCommerce price, with a warning when you would sell below it. */
|
|
public static function cost_line($product)
|
|
{
|
|
if (!$product || MOZOUT_RESELLER_Settings::api_key() === '') {
|
|
return '';
|
|
}
|
|
$type = self::type($product);
|
|
$c = null;
|
|
if ($type === 'plan' && (int) self::meta($product, 'plan')) {
|
|
$c = MOZOUT_RESELLER_Catalog::plan_cost((int) self::meta($product, 'plan'), (string) self::meta($product, 'cycle', 'annually'), (array) json_decode((string) self::meta($product, 'options', '{}'), true));
|
|
} elseif ($type === 'sms') {
|
|
$c = MOZOUT_RESELLER_Catalog::sms_cost((int) self::meta($product, 'sms_qty', '1000'));
|
|
} elseif ($type === 'domain_register') {
|
|
$exts = self::allowed_tlds($product);
|
|
if (count($exts) === 1) {
|
|
$c = MOZOUT_RESELLER_Catalog::tld_cost($exts[0], 'register', max(1, (int) self::meta($product, 'years', '1')));
|
|
}
|
|
}
|
|
if (!$c) {
|
|
return '';
|
|
}
|
|
$line = sprintf(
|
|
/* translators: %s: amount */
|
|
__('Your API price: %s.', 'mozout-reseller'),
|
|
'<strong>' . esc_html(MOZOUT_RESELLER_Admin::money($c['cost'], $c['currency'])) . '</strong>'
|
|
);
|
|
$price = (float) $product->get_price();
|
|
if ($price > 0 && $c['currency'] === get_woocommerce_currency() && $price < $c['cost']) {
|
|
$line .= ' <span class="mozout-reseller-error">' . esc_html__('The product price is lower than your API price.', 'mozout-reseller') . '</span>';
|
|
} elseif ($c['currency'] !== get_woocommerce_currency()) {
|
|
$line .= ' <span class="description">' . esc_html(sprintf(
|
|
/* translators: 1: API currency, 2: WooCommerce currency */
|
|
__('The API uses %1$s and WooCommerce uses %2$s: compare them yourself.', 'mozout-reseller'),
|
|
$c['currency'],
|
|
get_woocommerce_currency()
|
|
)) . '</span>';
|
|
}
|
|
return $line;
|
|
}
|
|
|
|
public static function save($product)
|
|
{
|
|
$p = wp_unslash($_POST);
|
|
$type = isset($p['mozout_reseller_type']) && in_array($p['mozout_reseller_type'], self::TYPES, true) ? $p['mozout_reseller_type'] : '';
|
|
$product->update_meta_data('_mozout_reseller_type', $type);
|
|
if ($type === '') {
|
|
return;
|
|
}
|
|
$tlds = array();
|
|
foreach (preg_split('/[\s,]+/', strtolower(isset($p['mozout_reseller_tlds']) ? (string) $p['mozout_reseller_tlds'] : '')) as $e) {
|
|
$e = '.' . ltrim(preg_replace('/[^a-z0-9.\-]/', '', $e), '.');
|
|
if (strlen($e) > 2) {
|
|
$tlds[] = $e;
|
|
}
|
|
}
|
|
$product->update_meta_data('_mozout_reseller_tlds', implode(', ', array_unique($tlds)));
|
|
$product->update_meta_data('_mozout_reseller_years', (string) min(10, max(1, (int) (isset($p['mozout_reseller_years']) ? $p['mozout_reseller_years'] : 1))));
|
|
$ns = array();
|
|
foreach (preg_split('/[\s,]+/', strtolower(isset($p['mozout_reseller_ns']) ? (string) $p['mozout_reseller_ns'] : '')) as $n) {
|
|
$n = preg_replace('/[^a-z0-9.\-]/', '', $n);
|
|
if ($n !== '' && strpos($n, '.') !== false) {
|
|
$ns[] = $n;
|
|
}
|
|
}
|
|
$product->update_meta_data('_mozout_reseller_ns', implode(', ', array_slice($ns, 0, 5)));
|
|
$product->update_meta_data('_mozout_reseller_plan', (string) (int) (isset($p['mozout_reseller_plan']) ? $p['mozout_reseller_plan'] : 0));
|
|
$product->update_meta_data('_mozout_reseller_cycle', isset($p['mozout_reseller_cycle']) && array_key_exists($p['mozout_reseller_cycle'], MOZOUT_RESELLER_Catalog::cycles()) ? $p['mozout_reseller_cycle'] : 'annually');
|
|
$opts = array();
|
|
foreach ((array) (isset($p['mozout_reseller_opt']) ? $p['mozout_reseller_opt'] : array()) as $k => $v) {
|
|
$opts[(int) $k] = (int) $v;
|
|
}
|
|
$product->update_meta_data('_mozout_reseller_options', wp_json_encode((object) $opts));
|
|
$product->update_meta_data('_mozout_reseller_ask_domain', empty($p['mozout_reseller_ask_domain']) ? 'no' : 'yes');
|
|
$product->update_meta_data('_mozout_reseller_sms_qty', (string) max(1, (int) (isset($p['mozout_reseller_sms_qty']) ? $p['mozout_reseller_sms_qty'] : 1000)));
|
|
$rp = isset($p['mozout_reseller_renew_price']) ? wc_format_decimal($p['mozout_reseller_renew_price']) : '';
|
|
$product->update_meta_data('_mozout_reseller_renew_price', $rp === '' ? '' : (string) $rp);
|
|
// Products fulfilled by the API are services: no shipping.
|
|
$product->set_virtual(true);
|
|
}
|
|
|
|
/* ---- Product page ---- */
|
|
|
|
public static function allowed_tlds($product)
|
|
{
|
|
$csv = (string) self::meta($product, 'tlds');
|
|
return $csv === '' ? array() : array_values(array_filter(array_map('trim', explode(',', $csv))));
|
|
}
|
|
|
|
public static function asks_domain($product)
|
|
{
|
|
$type = self::type($product);
|
|
return self::needs_domain($type) || ($type === 'plan' && self::meta($product, 'ask_domain', 'no') === 'yes');
|
|
}
|
|
|
|
public static function fields()
|
|
{
|
|
global $product;
|
|
if (!$product || !self::asks_domain($product)) {
|
|
return;
|
|
}
|
|
$type = self::type($product);
|
|
$value = isset($_REQUEST['mozout_reseller_domain']) ? self::clean_domain(wp_unslash($_REQUEST['mozout_reseller_domain'])) : '';
|
|
$exts = self::allowed_tlds($product);
|
|
wp_enqueue_style('mozout-reseller-front', MOZOUT_RESELLER_URL . 'assets/front.css', array(), MOZOUT_RESELLER_VERSION);
|
|
echo '<div class="mozout-reseller-fields">';
|
|
echo '<p class="mozout-reseller-field"><label for="mozout_reseller_domain">' . esc_html($type === 'plan' ? __('Site domain (optional)', 'mozout-reseller') : __('Domain name', 'mozout-reseller')) . '</label>';
|
|
echo '<input type="text" id="mozout_reseller_domain" name="mozout_reseller_domain" value="' . esc_attr($value) . '" maxlength="120" autocomplete="off" autocapitalize="none" spellcheck="false" placeholder="' . esc_attr('mycompany' . ($exts ? $exts[0] : '.com')) . '"' . ($type === 'plan' ? '' : ' required') . '>';
|
|
if ($exts) {
|
|
echo '<small>' . esc_html(sprintf(
|
|
/* translators: %s: list of extensions */
|
|
__('Extensions: %s', 'mozout-reseller'),
|
|
implode(' ', $exts)
|
|
)) . '</small>';
|
|
}
|
|
echo '</p>';
|
|
if ($type === 'domain_transfer') {
|
|
echo '<p class="mozout-reseller-field"><label for="mozout_reseller_epp">' . esc_html__('Transfer code (EPP)', 'mozout-reseller') . '</label>';
|
|
echo '<input type="text" id="mozout_reseller_epp" name="mozout_reseller_epp" maxlength="120" autocomplete="off" spellcheck="false" required>';
|
|
echo '<small>' . esc_html__('Ask the company where the domain is now. The domain must be unlocked.', 'mozout-reseller') . '</small></p>';
|
|
}
|
|
echo '</div>';
|
|
}
|
|
|
|
public static function clean_domain($q)
|
|
{
|
|
$q = strtolower(trim((string) $q));
|
|
$q = preg_replace('#^[a-z]+://#', '', $q);
|
|
$q = preg_replace('#^www\.#', '', $q);
|
|
$q = preg_replace('#[/?\#].*$#', '', $q);
|
|
$q = preg_replace('/[^a-z0-9.\-]/', '', $q);
|
|
return trim($q, '.-');
|
|
}
|
|
|
|
public static function validate($passed, $product_id, $qty)
|
|
{
|
|
$product = wc_get_product(isset($_REQUEST['variation_id']) && (int) $_REQUEST['variation_id'] ? (int) $_REQUEST['variation_id'] : $product_id);
|
|
if (!$product || !self::asks_domain($product)) {
|
|
return $passed;
|
|
}
|
|
$type = self::type($product);
|
|
$d = isset($_REQUEST['mozout_reseller_domain']) ? self::clean_domain(wp_unslash($_REQUEST['mozout_reseller_domain'])) : '';
|
|
if ($d === '' && $type === 'plan') {
|
|
return $passed;
|
|
}
|
|
if (!preg_match('/^[a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?(\.[a-z0-9\-]{2,})+$/', $d)) {
|
|
wc_add_notice(__('Write a valid domain name, for example mycompany.com.', 'mozout-reseller'), 'error');
|
|
return false;
|
|
}
|
|
$exts = self::allowed_tlds($product);
|
|
if ($exts && self::needs_domain($type)) {
|
|
$ok = false;
|
|
foreach ($exts as $e) {
|
|
if (substr($d, -strlen($e)) === $e && strlen($d) > strlen($e)) {
|
|
$ok = true;
|
|
}
|
|
}
|
|
if (!$ok) {
|
|
wc_add_notice(sprintf(
|
|
/* translators: %s: list of extensions */
|
|
__('This product only takes these extensions: %s', 'mozout-reseller'),
|
|
implode(' ', $exts)
|
|
), 'error');
|
|
return false;
|
|
}
|
|
}
|
|
if ($type === 'domain_transfer') {
|
|
$epp = isset($_REQUEST['mozout_reseller_epp']) ? trim((string) wp_unslash($_REQUEST['mozout_reseller_epp'])) : '';
|
|
if ($epp === '') {
|
|
wc_add_notice(__('Write the transfer code (EPP) of the domain.', 'mozout-reseller'), 'error');
|
|
return false;
|
|
}
|
|
}
|
|
if ($type === 'domain_register') {
|
|
$res = MOZOUT_RESELLER_Api::request('GET', '/v1/domains/availability', null, array('query' => array('domain' => $d), 'timeout' => 20));
|
|
if (is_wp_error($res)) {
|
|
MOZOUT_RESELLER_Log::add('cart', false, $res->get_error_message(), array('domain' => $d));
|
|
wc_add_notice(__('We could not confirm that this name is free right now. Please try again in a moment.', 'mozout-reseller'), 'error');
|
|
return false;
|
|
}
|
|
$a = $res['data'];
|
|
if (!empty($a['portal_only'])) {
|
|
wc_add_notice(__('This name needs extra steps and cannot be ordered here. Please contact us.', 'mozout-reseller'), 'error');
|
|
return false;
|
|
}
|
|
if (empty($a['available'])) {
|
|
wc_add_notice(sprintf(
|
|
/* translators: %s: domain name */
|
|
__('%s is not available for registration.', 'mozout-reseller'),
|
|
$d
|
|
), 'error');
|
|
return false;
|
|
}
|
|
}
|
|
return $passed;
|
|
}
|
|
|
|
public static function cart_item_data($data, $product_id)
|
|
{
|
|
if (isset($_REQUEST['mozout_reseller_domain'])) {
|
|
$d = self::clean_domain(wp_unslash($_REQUEST['mozout_reseller_domain']));
|
|
if ($d !== '') {
|
|
$data['mozout_reseller_domain'] = $d;
|
|
}
|
|
}
|
|
if (isset($_REQUEST['mozout_reseller_epp'])) {
|
|
$epp = sanitize_text_field(wp_unslash($_REQUEST['mozout_reseller_epp']));
|
|
if ($epp !== '') {
|
|
$data['mozout_reseller_epp'] = $epp;
|
|
}
|
|
}
|
|
if (!empty($data['mozout_reseller_domain'])) {
|
|
$data['mozout_reseller_key'] = md5($data['mozout_reseller_domain'] . microtime());
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public static function show_in_cart($item_data, $cart_item)
|
|
{
|
|
if (!empty($cart_item['mozout_reseller_domain'])) {
|
|
$item_data[] = array('key' => __('Domain', 'mozout-reseller'), 'value' => esc_html($cart_item['mozout_reseller_domain']));
|
|
}
|
|
return $item_data;
|
|
}
|
|
|
|
public static function order_item($item, $cart_key, $values, $order)
|
|
{
|
|
$product = $item->get_product();
|
|
$type = self::type($product);
|
|
if ($type === '') {
|
|
return;
|
|
}
|
|
$item->add_meta_data('_mozout_reseller_type', $type, true);
|
|
if (!empty($values['mozout_reseller_domain'])) {
|
|
$item->add_meta_data('mozout_reseller_domain', $values['mozout_reseller_domain'], true);
|
|
}
|
|
if (!empty($values['mozout_reseller_epp'])) {
|
|
$item->add_meta_data('_mozout_reseller_epp', $values['mozout_reseller_epp'], true);
|
|
}
|
|
$item->add_meta_data('_mozout_reseller_state', 'new', true);
|
|
$order->update_meta_data('_mozout_reseller_has_items', 'yes');
|
|
}
|
|
|
|
public static function meta_label($display_key, $meta, $item)
|
|
{
|
|
return $meta->key === 'mozout_reseller_domain' ? __('Domain', 'mozout-reseller') : $display_key;
|
|
}
|
|
|
|
public static function hidden_meta($keys)
|
|
{
|
|
return array_merge($keys, array('_mozout_reseller_type', '_mozout_reseller_epp', '_mozout_reseller_state', '_mozout_reseller_action', '_mozout_reseller_result', '_mozout_reseller_tries', '_mozout_reseller_service', '_mozout_reseller_status', '_mozout_reseller_expires', '_mozout_reseller_renew_of', '_mozout_reseller_reminded', '_mozout_reseller_error', '_mozout_reseller_sim', '_mozout_reseller_years', '_mozout_reseller_site', '_mozout_reseller_invoice', '_mozout_reseller_renewals', '_mozout_reseller_round', '_mozout_reseller_renew_open'));
|
|
}
|
|
|
|
public static function individually($yes, $product)
|
|
{
|
|
$type = self::type($product);
|
|
return ($type !== '' && $type !== 'sms') ? true : $yes;
|
|
}
|
|
|
|
/** Products that need a domain cannot go to the cart from the product list: the button opens the product page. */
|
|
public static function loop_url($url, $product)
|
|
{
|
|
return self::asks_domain($product) && self::needs_domain(self::type($product)) ? get_permalink($product->get_id()) : $url;
|
|
}
|
|
|
|
public static function loop_text($text, $product)
|
|
{
|
|
return self::asks_domain($product) && self::needs_domain(self::type($product)) ? __('Choose the domain', 'mozout-reseller') : $text;
|
|
}
|
|
|
|
public static function no_ajax($supports, $feature, $product)
|
|
{
|
|
if ($feature === 'ajax_add_to_cart' && self::needs_domain(self::type($product))) {
|
|
return false;
|
|
}
|
|
return $supports;
|
|
}
|
|
|
|
/** Register button of the domain search: straight to the cart; transfer: the product page, for the code. */
|
|
public static function search_url($url, $domain, $action)
|
|
{
|
|
$pid = (int) MOZOUT_RESELLER_Settings::get($action === 'transfer' ? 'transfer_product' : 'register_product');
|
|
if (!$pid || !wc_get_product($pid)) {
|
|
return $url;
|
|
}
|
|
if ($action === 'transfer') {
|
|
return add_query_arg('mozout_reseller_domain', rawurlencode($domain), get_permalink($pid));
|
|
}
|
|
return add_query_arg(array('add-to-cart' => $pid, 'mozout_reseller_domain' => rawurlencode($domain)), wc_get_cart_url());
|
|
}
|
|
}
|