settings = $settings; $this->catalog = $catalog; add_action( 'admin_menu', array( $this, 'register_menu' ) ); add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_init', array( $this, 'maybe_redirect_after_activation' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_filter( 'admin_body_class', array( $this, 'body_class' ) ); } /** * Register the settings screen, nested under WooCommerce when available. * * @return void */ public function register_menu() { $title = __( 'Bulkora', 'bulkora-multiple-variation-order' ); if ( class_exists( 'WooCommerce' ) ) { add_submenu_page( 'woocommerce', __( 'Bulkora', 'bulkora-multiple-variation-order' ), $title, self::CAPABILITY, self::PAGE_SETTINGS, array( $this, 'render_settings' ) ); return; } add_menu_page( __( 'Bulkora', 'bulkora-multiple-variation-order' ), $title, self::CAPABILITY, self::PAGE_SETTINGS, array( $this, 'render_settings' ), 'dashicons-cart', 58.9 ); } /** * Register the options with the Settings API. * * @return void */ public function register_settings() { register_setting( self::OPTION_GROUP, 'bulkora_general', array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_general' ), 'default' => $this->settings->defaults( Settings::GROUP_GENERAL ), ) ); register_setting( self::OPTION_GROUP, 'bulkora_advanced', array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_advanced' ), 'default' => $this->settings->defaults( Settings::GROUP_ADVANCED ), ) ); } /** * Sanitise the general group. * * @param mixed $value Raw submitted value. * @return array */ public function sanitize_general( $value ) { return $this->settings->sanitize_group( Settings::GROUP_GENERAL, (array) $value ); } /** * Sanitise the advanced group. * * @param mixed $value Raw submitted value. * @return array */ public function sanitize_advanced( $value ) { return $this->settings->sanitize_group( Settings::GROUP_ADVANCED, (array) $value ); } /** * Load the settings assets, and only on the plugin's own screen. * * @param string $hook_suffix Current admin screen hook. * @return void */ public function enqueue_assets( $hook_suffix ) { if ( ! $this->is_plugin_screen( $hook_suffix ) ) { return; } wp_enqueue_style( 'bulkora-admin', BULKORA_ASSETS_URL . 'admin/css/admin.css', array(), \Bulkora\Plugin::asset_version( 'admin/css/admin.css' ) ); wp_enqueue_script( 'bulkora-admin', BULKORA_ASSETS_URL . 'admin/js/admin.js', array(), \Bulkora\Plugin::asset_version( 'admin/js/admin.js' ), true ); wp_localize_script( 'bulkora-admin', 'bulkoraAdmin', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( self::NONCE_ACTION ), 'i18n' => array( 'saved' => __( 'Changes saved.', 'bulkora-multiple-variation-order' ), 'error' => __( 'Something went wrong. Please try again.', 'bulkora-multiple-variation-order' ), ), ) ); } /** * Whether the current screen belongs to this plugin. * * @param string $hook_suffix Screen hook, or a screen id. * @return bool */ private function is_plugin_screen( $hook_suffix ) { return false !== strpos( (string) $hook_suffix, self::PAGE_SETTINGS ); } /** * Tag the plugin screen so styles can be scoped safely. * * @param string $classes Existing body classes. * @return string */ public function body_class( $classes ) { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && $this->is_plugin_screen( $screen->id ) ) { $classes .= ' bulkora-screen'; } return $classes; } /** * Send a freshly activated user straight to the settings screen. * * @return void */ public function maybe_redirect_after_activation() { if ( ! get_transient( 'bulkora_activation_redirect' ) ) { return; } delete_transient( 'bulkora_activation_redirect' ); if ( wp_doing_ajax() || is_network_admin() || ! current_user_can( self::CAPABILITY ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- presence check only. if ( isset( $_GET['activate-multi'] ) ) { return; } wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE_SETTINGS ) ); exit; } /** * Render the settings screen. * * @return void */ public function render_settings() { $this->render_view( 'settings' ); } /** * Include a view with the shared template context. * * @param string $view View file name, without extension. * @return void */ private function render_view( $view ) { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'bulkora-multiple-variation-order' ) ); } $settings = $this->settings; $catalog = $this->catalog; $admin = $this; $path = __DIR__ . '/views/' . $view . '.php'; if ( ! is_readable( $path ) ) { return; } include $path; } /** * Admin URL for the settings screen. * * @return string */ public function url() { return admin_url( 'admin.php?page=' . self::PAGE_SETTINGS ); } }