module_id = self::MODULE_ID; // WooCommerce only. $this->wc_only = true; // Parent construct. parent::__construct(); $this->main_func = $main_func; // Module section. $this->module_section = 'boost-revenue'; // Module default settings. $this->module_default_settings = array( 'button_text' => __( 'Pre Order Now!', 'merchant' ), 'additional_text' => __( 'Ships on {date}.', 'merchant' ), ); // Module data. $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; // Module options path. $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php'; // Is module preview page. if ( is_admin() && parent::is_module_settings_page() ) { self::$is_module_preview = true; // Enqueue admin styles. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_js' ) ); // Localize Script. add_filter( 'merchant_admin_localize_script', array( $this, 'localize_script' ) ); // Admin preview box. add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); // Custom CSS. // The custom CSS should be added here as well due to ensure preview box works properly. add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) ); } add_action( 'merchant_admin_before_include_modules_options', array( $this, 'help_banner' ) ); if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) { // Init translations. $this->init_translations(); // Include product metabox for per-product settings. require_once MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/class-pre-orders-metabox.php'; } if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) { return; } // TODO: Refactor the 'Merchant_Pre_Orders_Main_Functionality' class to load admin things separated from frontend things. $main_func->init(); // Return early if it's on admin but not in the respective module settings page. if ( is_admin() && ! wp_doing_ajax() && ! parent::is_module_settings_page() ) { return; } // Enqueue styles. add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) ); // Enqueue scripts. add_action( 'merchant_enqueue_after_main_css_js', array( $this, 'enqueue_scripts' ) ); // Localize script. add_filter( 'merchant_localize_script', array( $this, 'localize_script' ) ); // Custom CSS. add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) ); $this->supply_missing_id( 'rules' ); } /** * Init translations for module settings. * * Registers strings for the Merchant translator tool. * * @return void */ public function init_translations() { $settings = $this->get_module_settings(); if ( ! empty( $settings['rules'] ) ) { foreach ( $settings['rules'] as $rule ) { if ( ! empty( $rule['button_text'] ) ) { Merchant_Translator::register_string( $rule['button_text'], 'Pre order button text' ); } if ( ! empty( $rule['additional_text'] ) ) { Merchant_Translator::register_string( $rule['additional_text'], 'Pre order additional information' ); } if ( ! empty( $rule['cart_label_text'] ) ) { Merchant_Translator::register_string( $rule['cart_label_text'], 'Label text on cart' ); } } } } /** * Enqueue admin-specific CSS. * * @return void */ public function admin_enqueue_css() { if ( parent::is_module_settings_page() ) { wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/pre-orders.min.css', array(), MERCHANT_VERSION ); wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION ); } } /** * Enqueue admin-specific JavaScript. * * @return void */ public function admin_enqueue_js() { if ( parent::is_module_settings_page() ) { wp_enqueue_script( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/admin/preview.min.js', array( 'jquery' ), MERCHANT_VERSION, true ); } } /** * Enqueue frontend-specific CSS. * * @return void */ public function enqueue_css() { // Specific module styles. wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/pre-orders.min.css', array(), MERCHANT_VERSION ); // add inline style if ( is_singular( 'product' ) ) { wp_add_inline_style( 'merchant-' . self::MODULE_ID, $this->add_inline_style() ); } } /** * Add inline CSS styles based on the active rule. * * @return string The generated inline CSS. */ public function add_inline_style() { ob_start(); $rule = $this->current_rule(); if ( ! empty( $rule ) ) { ?> .woocommerce .merchant-pre-ordered-product{ --mrc-po-text-color: ; --mrc-po-text-hover-color: ; --mrc-po-border-color: ; --mrc-po-border-hover-color: ; --mrc-po-border-width: ; --mrc-po-border-radius: ; --mrc-po-background-color: ; --mrc-po-background-hover-color: ; } current_rule(); if ( ! empty( $rule ) && ! empty( $rule['button_text'] ) ) { $setting['pre_orders_add_button_title'] = Merchant_Translator::translate( $rule['button_text'] ); } else { $setting['pre_orders_add_button_title'] = esc_html__( 'Pre Order Now!', 'merchant' ); } $setting['shipping_date_missing_text'] = esc_html__( 'Please set a shipping date first', 'merchant' ); return $setting; } /** * Render the admin preview for the module. * * @param Merchant_Admin_Preview $preview The preview object. * @param string $module The module ID. * * @return Merchant_Admin_Preview The updated preview object. */ public function render_admin_preview( $preview, $module ) { if ( self::MODULE_ID === $module ) { $settings = $this->get_module_settings(); // Additional text. $additional_text = $settings['additional_text']; $time_format = date_i18n( get_option( 'date_format' ), strtotime( gmdate( 'Y-m-d', strtotime( '+2 days' ) ) ) ); $text = $this->main_func->replace_date_text( $additional_text, $time_format ); ob_start(); self::admin_preview_content( $settings, $text ); $content = ob_get_clean(); // HTML. $preview->set_html( $content ); // Button Text. $preview->set_text( 'button_text', '.add_to_cart_button' ); // Additional Text. $preview->set_text( 'additional_text', '.merchant-pre-orders-date', array( array( '{date}', ), array( $time_format, ), ) ); } return $preview; } /** * Admin preview HTML content. * * @param array $settings The module settings. * @param string $text The formatted additional text. * * @return void */ public function admin_preview_content( $settings, $text ) { ?>
get_module_custom_css(); return $css; } /** * Filter for frontend custom CSS. * * @param string $css The custom CSS. * * @return string The updated custom CSS. */ public function frontend_custom_css( $css ) { $css .= $this->get_module_custom_css(); return $css; } /** * Get the currently applicable product rule. * * @return array The rule array if found, empty array otherwise. */ private function current_rule() { if ( is_singular( 'product' ) ) { $product_id = get_queried_object_id(); $product = wc_get_product( $product_id ); if ( ! $product ) { return array(); } $rule = Merchant_Pre_Orders_Main_Functionality::available_product_rule( $product->get_id() ); if ( empty( $rule ) && $product instanceof \WC_Product_Variable ) { $available_variations = $product->get_available_variations(); foreach ( $available_variations as $variation ) { $rule = Merchant_Pre_Orders_Main_Functionality::available_product_rule( $variation['variation_id'] ); if ( ! empty( $rule ) ) { break; } } } return $rule; } return array(); } /** * Display a help banner in the module settings page. * * @param string $module_id The module ID. * * @return void */ public function help_banner( $module_id ) { if ( $module_id === self::MODULE_ID ) { ?>%2s', esc_url( admin_url( 'edit.php?post_type=shop_order' ) ), esc_html__( 'View Pre-Orders', 'merchant' ) ); ?>