| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Complementary Products Module. |
| 5 |
* |
| 6 |
* @package Merchant |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Complementary Products Module. |
| 15 |
* |
| 16 |
*/ |
| 17 |
class Merchant_Complementary_Products extends Merchant_Add_Module { |
| 18 |
|
| 19 |
/** |
| 20 |
* Module ID. |
| 21 |
*/ |
| 22 |
const MODULE_ID = 'complementary-products'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Module template path. |
| 26 |
*/ |
| 27 |
const MODULE_TEMPLATES_PATH = 'modules/' . self::MODULE_ID; |
| 28 |
|
| 29 |
/** |
| 30 |
* Is module preview. |
| 31 |
* |
| 32 |
*/ |
| 33 |
public static $is_module_preview = false; |
| 34 |
|
| 35 |
/** |
| 36 |
* Set the module as having analytics. |
| 37 |
* |
| 38 |
* @var bool |
| 39 |
*/ |
| 40 |
protected $has_analytics = true; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor. |
| 44 |
* |
| 45 |
*/ |
| 46 |
public function __construct() { |
| 47 |
// Module id. |
| 48 |
$this->module_id = self::MODULE_ID; |
| 49 |
|
| 50 |
// WooCommerce only. |
| 51 |
$this->wc_only = true; |
| 52 |
|
| 53 |
// Parent construct. |
| 54 |
parent::__construct(); |
| 55 |
|
| 56 |
// Module data. |
| 57 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 58 |
|
| 59 |
// Module section. |
| 60 |
$this->module_section = $this->module_data['section']; |
| 61 |
|
| 62 |
// Module options path. |
| 63 |
$this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php'; |
| 64 |
|
| 65 |
// Is module preview page. |
| 66 |
if ( is_admin() && parent::is_module_settings_page() ) { |
| 67 |
self::$is_module_preview = true; |
| 68 |
|
| 69 |
// Enqueue admin styles. |
| 70 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); |
| 71 |
|
| 72 |
// Enqueue admin scripts. |
| 73 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_js' ) ); |
| 74 |
|
| 75 |
// Admin preview box. |
| 76 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 77 |
} |
| 78 |
|
| 79 |
if ( $this->is_module_active() && is_admin() ) { |
| 80 |
// Init translations. |
| 81 |
$this->init_translations(); |
| 82 |
} |
| 83 |
|
| 84 |
if ( ! $this->is_module_active() ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
// actions and filters goes here. |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Init translations. |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public function init_translations() { |
| 96 |
$settings = $this->get_module_settings(); |
| 97 |
if ( ! empty( $settings['offers'] ) ) { |
| 98 |
foreach ( $settings['offers'] as $offer ) { |
| 99 |
if ( ! empty( $offer['product_single_page']['offer-title'] ) ) { |
| 100 |
Merchant_Translator::register_string( $offer['product_single_page']['offer-title'] ); |
| 101 |
} |
| 102 |
|
| 103 |
if ( ! empty( $offer['product_single_page']['offer-description'] ) ) { |
| 104 |
Merchant_Translator::register_string( $offer['product_single_page']['offer-description'] ); |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! empty( $offer['cart_page']['title'] ) ) { |
| 108 |
Merchant_Translator::register_string( $offer['cart_page']['title'] ); |
| 109 |
} |
| 110 |
|
| 111 |
if ( ! empty( $offer['cart_page']['button_text'] ) ) { |
| 112 |
Merchant_Translator::register_string( $offer['cart_page']['button_text'] ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( ! empty( $offer['checkout_page']['title'] ) ) { |
| 116 |
Merchant_Translator::register_string( $offer['checkout_page']['title'] ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( ! empty( $offer['checkout_page']['offer_description'] ) ) { |
| 120 |
Merchant_Translator::register_string( $offer['checkout_page']['offer_description'] ); |
| 121 |
} |
| 122 |
|
| 123 |
if ( ! empty( $offer['checkout_page']['button_text'] ) ) { |
| 124 |
Merchant_Translator::register_string( $offer['checkout_page']['button_text'] ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( ! empty( $offer['thank_you_page']['title'] ) ) { |
| 128 |
Merchant_Translator::register_string( $offer['thank_you_page']['title'] ); |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! empty( $offer['thank_you_page']['discount_text'] ) ) { |
| 132 |
Merchant_Translator::register_string( $offer['thank_you_page']['discount_text'] ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( ! empty( $offer['thank_you_page']['button_text'] ) ) { |
| 136 |
Merchant_Translator::register_string( $offer['thank_you_page']['button_text'] ); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Admin enqueue CSS. |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function admin_enqueue_css() { |
| 148 |
if ( $this->is_module_settings_page() ) { |
| 149 |
wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Admin enqueue scripts. |
| 155 |
* |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
public function admin_enqueue_js() { |
| 159 |
if ( $this->is_module_settings_page() ) { |
| 160 |
wp_enqueue_script( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/admin/preview.min.js', array( 'jquery' ), |
| 161 |
MERCHANT_VERSION, true ); |
| 162 |
|
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Render admin preview |
| 168 |
* |
| 169 |
* @param Merchant_Admin_Preview $preview |
| 170 |
* @param string $module |
| 171 |
* |
| 172 |
* @return Merchant_Admin_Preview |
| 173 |
*/ |
| 174 |
public function render_admin_preview( $preview, $module ) { |
| 175 |
if ( $module === self::MODULE_ID ) { |
| 176 |
// HTML. |
| 177 |
$preview->set_html( array( $this, 'admin_preview_content' ), $this->get_module_settings() ); |
| 178 |
|
| 179 |
// Simple product add to cart text |
| 180 |
$preview->set_text( 'simple_product_shop_label', '.merchant-preview-add-to-cart-simple' ); |
| 181 |
|
| 182 |
// Variable product add to cart text |
| 183 |
$preview->set_text( 'variable_product_shop_label', '.merchant-preview-add-to-cart-variable' ); |
| 184 |
|
| 185 |
// Out of stock add to cart text |
| 186 |
$preview->set_text( 'out_of_stock_shop_label', '.merchant-preview-add-to-cart-out-of-stock' ); |
| 187 |
|
| 188 |
// Out of stock add to cart text |
| 189 |
$preview->set_class( 'out_of_stock_custom_label', '.merchant-preview-product-out-of-stock', array(), 'display' ); |
| 190 |
} |
| 191 |
|
| 192 |
return $preview; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Admin preview content. |
| 197 |
* |
| 198 |
* @param array $settings |
| 199 |
* |
| 200 |
* @return void |
| 201 |
*/ |
| 202 |
public function admin_preview_content( $settings ) { |
| 203 |
merchant_get_template_part( |
| 204 |
self::MODULE_TEMPLATES_PATH . '/admin-preview/', |
| 205 |
'single-product' |
| 206 |
); |
| 207 |
merchant_get_template_part( |
| 208 |
self::MODULE_TEMPLATES_PATH . '/admin-preview/', |
| 209 |
'cart' |
| 210 |
); |
| 211 |
merchant_get_template_part( |
| 212 |
self::MODULE_TEMPLATES_PATH . '/admin-preview/', |
| 213 |
'checkout' |
| 214 |
); |
| 215 |
merchant_get_template_part( |
| 216 |
self::MODULE_TEMPLATES_PATH . '/admin-preview/', |
| 217 |
'thank-you-page' |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
private function is_module_active() { |
| 222 |
return Merchant_Modules::is_module_active( self::MODULE_ID ); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// Initialize the module. |
| 227 |
add_action( 'init', function () { |
| 228 |
Merchant_Modules::create_module( new Merchant_Complementary_Products() ); |
| 229 |
} ); |
| 230 |
|