COD Limits.
*/
class Shaban_COD_Limits_Admin {
const SLUG = 'shaban-cod-limits';
/**
* Screen hook suffix.
*
* @var string
*/
private $hook = '';
/**
* Hook everything up.
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'menu' ), 99 );
add_action( 'admin_init', array( $this, 'register' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) );
add_filter( 'plugin_action_links_' . plugin_basename( SHABAN_COD_LIMITS_FILE ), array( $this, 'action_links' ) );
// Let shop managers save the settings too, not just administrators.
add_filter( 'option_page_capability_shaban_cod_limits_group', array( $this, 'capability' ) );
}
/**
* Capability needed to save the settings.
*/
public function capability() {
return 'manage_woocommerce';
}
/**
* Add the WooCommerce submenu page.
*/
public function menu() {
$this->hook = add_submenu_page(
'woocommerce',
__( 'Shaban COD Limits', 'shaban-cod-limits' ),
__( 'COD Limits', 'shaban-cod-limits' ),
'manage_woocommerce',
self::SLUG,
array( $this, 'render' )
);
}
/**
* Register the setting with the Settings API.
*/
public function register() {
register_setting(
'shaban_cod_limits_group',
SHABAN_COD_LIMITS_OPTION,
array(
'type' => 'array',
'sanitize_callback' => array( 'Shaban_COD_Limits_Settings', 'sanitize' ),
'default' => Shaban_COD_Limits_Settings::defaults(),
)
);
}
/**
* Add a Settings link on the Plugins screen.
*
* @param string[] $links Existing action links.
* @return string[]
*/
public function action_links( $links ) {
$url = admin_url( 'admin.php?page=' . self::SLUG );
array_unshift(
$links,
'' . esc_html__( 'Settings', 'shaban-cod-limits' ) . '',
'' . esc_html__( 'Support', 'shaban-cod-limits' ) . ''
);
return $links;
}
/**
* Load the dashboard CSS and JS on our own screen only.
*
* @param string $hook Current admin screen hook suffix.
*/
public function assets( $hook ) {
if ( ! $this->hook || $hook !== $this->hook ) {
return;
}
// WooCommerce's own product search + enhanced select.
wp_enqueue_style( 'woocommerce_admin_styles' );
wp_enqueue_script( 'selectWoo' );
wp_enqueue_script( 'wc-enhanced-select' );
wp_enqueue_style( 'shaban-cod-limits-admin', SHABAN_COD_LIMITS_URL . 'assets/css/admin.css', array( 'woocommerce_admin_styles' ), SHABAN_COD_LIMITS_VERSION );
wp_enqueue_script( 'shaban-cod-limits-admin', SHABAN_COD_LIMITS_URL . 'assets/js/admin.js', array( 'jquery', 'wc-enhanced-select' ), SHABAN_COD_LIMITS_VERSION, true );
wp_localize_script(
'shaban-cod-limits-admin',
'shabanCodLimitsData',
array(
'symbol' => html_entity_decode( get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8' ),
'locale' => str_replace( '_', '-', get_user_locale() ),
'i18n' => array(
'active' => __( 'Active', 'shaban-cod-limits' ),
'paused' => __( 'Paused', 'shaban-cod-limits' ),
'cod' => __( 'cash on delivery', 'shaban-cod-limits' ),
'codName' => __( 'Cash on delivery', 'shaban-cod-limits' ),
'selectedMethods' => __( 'the selected payment methods', 'shaban-cod-limits' ),
'nothing' => __( 'nothing', 'shaban-cod-limits' ),
'chooseMethod' => __( 'Choose at least one payment method to hide.', 'shaban-cod-limits' ),
'pausedSummary' => __( 'The rule is paused, so {gw} works as usual.', 'shaban-cod-limits' ),
'above' => __( 'above', 'shaban-cod-limits' ),
'atLeast' => __( 'at or above', 'shaban-cod-limits' ),
'basisTotal' => __( 'cart total (with shipping, tax and fees)', 'shaban-cod-limits' ),
'basisSub' => __( 'products subtotal (after discounts)', 'shaban-cod-limits' ),
'sumAll' => __( 'Hides {gw} when the {basis} is {cmp} {amount}.', 'shaban-cod-limits' ),
'sumAnyInclude' => __( 'Hides {gw} whenever the cart has a selected product.', 'shaban-cod-limits' ),
'sumAnyExclude' => __( 'Hides {gw} whenever the cart has a product other than the selected ones.', 'shaban-cod-limits' ),
'whoInclude' => __( 'selected products', 'shaban-cod-limits' ),
'whoExclude' => __( 'products other than the selected ones', 'shaban-cod-limits' ),
'sumWho' => __( 'Hides {gw} when the {who} add up to {cmp} {amount}.', 'shaban-cod-limits' ),
'warnEmpty' => __( 'Pick at least one product or category, otherwise this rule does nothing.', 'shaban-cod-limits' ),
'lblOrder' => __( 'Order value', 'shaban-cod-limits' ),
'lblInclude' => __( 'Value of the selected products', 'shaban-cod-limits' ),
'lblExclude' => __( 'Value of the other products', 'shaban-cod-limits' ),
'setLimit' => __( 'Enter a limit amount to turn this rule on. Until then, nothing is restricted.', 'shaban-cod-limits' ),
'sending' => __( 'Sending…', 'shaban-cod-limits' ),
'unsaved' => __( 'You have unsaved changes', 'shaban-cod-limits' ),
'hiddenFor' => __( '{gw} is hidden for this order.', 'shaban-cod-limits' ),
'availFor' => __( '{gw} stays available for this order.', 'shaban-cod-limits' ),
),
)
);
}
/**
* Output the settings page.
*/
public function render() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
$s = Shaban_COD_Limits_Settings::get();
$symbol = html_entity_decode( get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8' );
$gateways = WC()->payment_gateways() ? WC()->payment_gateways()->payment_gateways() : array();
// Categories, labelled with their parents ("Parent › Child").
$terms = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
)
);
$terms = is_wp_error( $terms ) ? array() : $terms;
$by_id = array();
foreach ( $terms as $term ) {
$by_id[ $term->term_id ] = $term;
}
$label_for = function ( $term ) use ( &$label_for, $by_id ) {
$prefix = '';
if ( $term->parent && isset( $by_id[ $term->parent ] ) ) {
$prefix = $label_for( $by_id[ $term->parent ] ) . ' › ';
}
return $prefix . $term->name;
};
$categories = array();
foreach ( $terms as $term ) {
$categories[ $term->term_id ] = $label_for( $term );
}
asort( $categories, SORT_NATURAL | SORT_FLAG_CASE );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$saved = isset( $_GET['settings-updated'] ) && 'true' === sanitize_text_field( wp_unslash( $_GET['settings-updated'] ) );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$support_status = isset( $_GET['scl_support'] ) ? sanitize_key( wp_unslash( $_GET['scl_support'] ) ) : '';
$current_user = wp_get_current_user();
require_once SHABAN_COD_LIMITS_PATH . 'views/settings-page.php';
shaban_cod_limits_render_settings_page(
array(
'settings' => $s,
'symbol' => $symbol,
'gateways' => $gateways,
'categories' => $categories,
'saved' => $saved,
'support' => array(
'name' => $current_user->display_name,
'email' => $current_user->user_email,
'topics' => Shaban_COD_Limits_Support::topics(),
'details' => Shaban_COD_Limits_Support::diagnostics_labels(),
'flash' => Shaban_COD_Limits_Support::flash( $support_status ),
'contact' => Shaban_COD_Limits_Support::EMAIL,
),
)
);
}
}