'Size Chart', * 'group' => 'cpod_sc_settings_group', // registered with Fields::register() * 'render' => callable printing the fields (inside the form), * ); * * @package CPODKit */ namespace CPODKit\Core\Admin; use CPODKit\Core\Modules; use CPODKit\Core\Regions; use CPODKit\Core\Settings; defined( 'ABSPATH' ) || exit; class SettingsPage { const PAGE = 'cpod-kit-settings'; const GROUP = 'cpod_settings_group'; /** Tab order; tabs not listed follow in the order they were added. */ const ORDER = array( 'general', 'size-chart', 'pod-profile', 'auto-terms', 'fulfillment-integrations', 'product-sections', 'estimated-delivery', 'quick-view', 'upsell', 'collections', 'best-selling', 'checkout-extras', 'store-variables', 'icons' ); /** Groups of the side menu: group id => tab ids. Tabs not listed go under "More". */ const GROUPS = array( 'general' => array( 'general' ), 'product' => array( 'size-chart', 'pod-profile', 'auto-terms', 'fulfillment-integrations', 'product-sections', 'estimated-delivery', 'quick-view' ), 'sales' => array( 'upsell', 'collections', 'best-selling', 'checkout-extras' ), 'store' => array( 'store-variables', 'icons' ), ); public function register_hooks() { add_action( 'admin_menu', array( $this, 'menu' ), 99 ); add_action( 'admin_init', array( $this, 'register' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); } /** * URL of the settings page, optionally on a tab. * * @param string $tab Tab id. * @return string */ public static function url( $tab = '' ) { $url = Menu::url( self::PAGE ); return $tab ? add_query_arg( 'tab', $tab, $url ) : $url; } public function menu() { add_submenu_page( Menu::SLUG, __( 'cPOD Kit settings', 'cpod-kit' ), __( 'Settings', 'cpod-kit' ), 'manage_woocommerce', self::PAGE, array( $this, 'render' ) ); } public function register() { Fields::register( self::GROUP, Settings::OPTION, array( Settings::class, 'sanitize' ) ); } public function enqueue() { if ( TopBar::current_page() !== self::PAGE ) { return; } wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'wp-color-picker' ); wp_add_inline_script( 'wp-color-picker', 'jQuery(function($){' . 'if($.fn.wpColorPicker){$(".cpod-color").wpColorPicker();}' . 'function sync(i){$(i).closest(".cpod-color-row").toggleClass("has-var",$.trim(i.value)!=="");}' . '$(".cpod-color-var").each(function(){sync(this);}).on("input",function(){sync(this);});' . 'function un(){$(".cpod-uninstall-list").prop("hidden",$("input[name=\'cpod_settings[uninstall]\']:checked").val()!=="delete");}' . '$("input[name=\'cpod_settings[uninstall]\']").on("change",un);un();' . '});' ); } /** Tabs: id => array( label, group, render ). */ public static function tabs() { $tabs = array( 'general' => array( 'label' => __( 'General', 'cpod-kit' ), 'group' => self::GROUP, 'render' => array( __CLASS__, 'render_general' ), ), ); $tabs = array_merge( $tabs, (array) apply_filters( 'cpod_settings_tabs', array() ) ); $order = array_flip( self::ORDER ); $index = array_flip( array_keys( $tabs ) ); uksort( $tabs, static function ( $a, $b ) use ( $order, $index ) { $pa = isset( $order[ $a ] ) ? $order[ $a ] : 100 + $index[ $a ]; $pb = isset( $order[ $b ] ) ? $order[ $b ] : 100 + $index[ $b ]; return $pa - $pb; } ); return $tabs; } /** Group labels of the side menu. */ public static function group_labels() { return array( 'general' => __( 'General', 'cpod-kit' ), 'product' => __( 'Product page', 'cpod-kit' ), 'sales' => __( 'Sales', 'cpod-kit' ), 'store' => __( 'Store', 'cpod-kit' ), 'more' => __( 'More', 'cpod-kit' ), ); } /** * Tabs sorted into the side menu groups. * * @param array $tabs Tabs from tabs(). * @return array group id => array( tab id => tab ). */ public static function grouped( $tabs ) { $grouped = array(); $placed = array(); foreach ( self::GROUPS as $group => $ids ) { foreach ( $tabs as $id => $tab ) { if ( in_array( $id, $ids, true ) ) { $grouped[ $group ][ $id ] = $tab; $placed[ $id ] = true; } } } foreach ( $tabs as $id => $tab ) { if ( empty( $placed[ $id ] ) ) { $grouped['more'][ $id ] = $tab; } } return $grouped; } public function render() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $tabs = self::tabs(); $active = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'general'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! isset( $tabs[ $active ] ) ) { $active = 'general'; } $tab = $tabs[ $active ]; $labels = self::group_labels(); ?>