| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
class Merchant_Add_Module { |
| 8 |
|
| 9 |
/** |
| 10 |
* WooCommerce only. |
| 11 |
* |
| 12 |
*/ |
| 13 |
public $wc_only = false; |
| 14 |
|
| 15 |
/** |
| 16 |
* Module section. |
| 17 |
* |
| 18 |
*/ |
| 19 |
public $module_section = ''; |
| 20 |
|
| 21 |
/** |
| 22 |
* Module id. |
| 23 |
* |
| 24 |
*/ |
| 25 |
public $module_id = ''; |
| 26 |
|
| 27 |
/** |
| 28 |
* Module default settings. |
| 29 |
* |
| 30 |
*/ |
| 31 |
public $module_default_settings = array(); |
| 32 |
|
| 33 |
/** |
| 34 |
* Module data. |
| 35 |
* |
| 36 |
*/ |
| 37 |
public $module_data = array(); |
| 38 |
|
| 39 |
/** |
| 40 |
* Module options. |
| 41 |
* |
| 42 |
*/ |
| 43 |
public $module_options_path = ''; |
| 44 |
|
| 45 |
/** |
| 46 |
* Whether the module has a shortcode or not. |
| 47 |
* |
| 48 |
* @var bool |
| 49 |
*/ |
| 50 |
public $has_shortcode = false; |
| 51 |
|
| 52 |
/** |
| 53 |
* Constructor. |
| 54 |
* |
| 55 |
*/ |
| 56 |
public function __construct() { |
| 57 |
// Add and expose the module into the plugin dashboard. |
| 58 |
add_filter( 'merchant_modules', array( $this, 'add_module' ) ); |
| 59 |
|
| 60 |
// Add module options. |
| 61 |
add_filter( 'merchant_module_file_path', array( $this, 'add_module_options' ), 10, 2 ); |
| 62 |
|
| 63 |
// Add class to body to identify if module is active or not. |
| 64 |
add_filter( 'admin_body_class', array( $this, 'add_module_activation_status_class' ), 10, 2 ); |
| 65 |
|
| 66 |
// Handle modules list item class. |
| 67 |
add_filter( "merchant_admin_module_{$this->module_id}_list_item_class", array( $this, 'modules_list_item_class' ) ); |
| 68 |
|
| 69 |
if ( $this->has_shortcode ) { |
| 70 |
add_action( 'wp', array( $this, 'setup_product_object' ) ); |
| 71 |
add_shortcode( 'merchant_module_' . str_replace( '-', '_', $this->module_id ), array( $this, 'shortcode_handler' ) ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Active modules class handler. |
| 77 |
* |
| 78 |
*/ |
| 79 |
public function add_module_activation_status_class( $classes ) { |
| 80 |
if ( ! $this->is_module_settings_page() ) { |
| 81 |
return $classes; |
| 82 |
} |
| 83 |
|
| 84 |
if ( Merchant_Modules::is_module_active( $this->module_id ) ) { |
| 85 |
$classes = $classes . ' merchant-module-enabled'; |
| 86 |
} else { |
| 87 |
$classes = $classes . ' merchant-module-disabled'; |
| 88 |
} |
| 89 |
|
| 90 |
return $classes; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Modules list item class. |
| 95 |
* |
| 96 |
* @param string $module_class |
| 97 |
* |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
public function modules_list_item_class( $module_class ) { |
| 101 |
if ( $this->wc_only && ! class_exists( 'Woocommerce' ) ) { |
| 102 |
$module_class = $module_class . ' merchant-module-wc-only'; |
| 103 |
} |
| 104 |
|
| 105 |
return $module_class; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Is module settings page. |
| 110 |
* |
| 111 |
* @return bool |
| 112 |
*/ |
| 113 |
public function is_module_settings_page() { |
| 114 |
return isset( $_GET['page'] ) && 'merchant' === $_GET['page'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 115 |
&& isset( $_GET['module'] ) && $this->module_id === $_GET['module']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get module settings. |
| 120 |
* |
| 121 |
*/ |
| 122 |
public function get_module_settings() { |
| 123 |
$settings = get_option( 'merchant' ) ? get_option( 'merchant' ) : array(); |
| 124 |
|
| 125 |
// Default settings. |
| 126 |
$defaults = $this->module_default_settings; |
| 127 |
|
| 128 |
if ( empty( $settings[ $this->module_id ] ) ) { |
| 129 |
$settings[ $this->module_id ] = $defaults; |
| 130 |
} |
| 131 |
|
| 132 |
// Parse settings with defaults. |
| 133 |
// Todo: check if recursive_parse_args() works for all modules and remove the condition. |
| 134 |
$settings = $this->module_id === 'product-labels' ? $this->recursive_parse_args( $settings[ $this->module_id ], $defaults ) : wp_parse_args( $settings[ $this->module_id ], $defaults ); |
| 135 |
|
| 136 |
/** |
| 137 |
* Hook: merchant_module_settings |
| 138 |
* |
| 139 |
* @param array $settings Module settings. |
| 140 |
* @param string $module_id Module ID. |
| 141 |
* |
| 142 |
* @since 1.9.16 |
| 143 |
*/ |
| 144 |
$settings = apply_filters( 'merchant_module_settings', $settings, $this->module_id ); |
| 145 |
|
| 146 |
return $settings; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get preview URL |
| 151 |
* |
| 152 |
* @param array $args |
| 153 |
* |
| 154 |
* @return string |
| 155 |
*/ |
| 156 |
public function set_module_preview_url( $args = array() ) { |
| 157 |
// Mount preview url. |
| 158 |
$preview_url = site_url( '/' ); |
| 159 |
|
| 160 |
// Type based preview url |
| 161 |
if ( isset( $args['type'] ) ) { |
| 162 |
switch ( $args['type'] ) { |
| 163 |
case 'shop': |
| 164 |
if ( function_exists( 'wc_get_page_id' ) ) { |
| 165 |
$preview_url = get_permalink( wc_get_page_id( 'shop' ) ); |
| 166 |
} |
| 167 |
break; |
| 168 |
|
| 169 |
case 'product': |
| 170 |
$query_args = array( |
| 171 |
'post_type' => 'product', |
| 172 |
'posts_per_page' => 1, |
| 173 |
); |
| 174 |
|
| 175 |
if ( isset( $args['query'] ) ) { |
| 176 |
$products = ( new WP_Query( wp_parse_args( $args['query'], $query_args ) ) )->get_posts(); |
| 177 |
|
| 178 |
// If no results can be found with the custom query, |
| 179 |
// then use the default args |
| 180 |
if ( empty( $products ) || ! isset( $products[0] ) ) { |
| 181 |
$products = ( new WP_Query( $query_args ) )->get_posts(); |
| 182 |
} |
| 183 |
} else { |
| 184 |
$products = ( new WP_Query( $query_args ) )->get_posts(); |
| 185 |
} |
| 186 |
|
| 187 |
if ( ! empty( $products ) && isset( $products[0] ) ) { |
| 188 |
$preview_url = get_permalink( $products[0] ); |
| 189 |
} |
| 190 |
|
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return $preview_url; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Add module. |
| 200 |
* |
| 201 |
*/ |
| 202 |
public function add_module( $modules ) { |
| 203 |
$modules[ $this->module_section ]['modules'][ $this->module_id ] = $this->module_data; |
| 204 |
|
| 205 |
return $modules; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Add module options. |
| 210 |
* |
| 211 |
*/ |
| 212 |
public function add_module_options( $module_path, $merchant_module ) { |
| 213 |
if ( $this->module_id === $merchant_module ) { |
| 214 |
return $this->module_options_path; |
| 215 |
} |
| 216 |
|
| 217 |
return $module_path; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Display error message if the shortcode is placed in the wrong place. |
| 222 |
* |
| 223 |
* @return mixed|null |
| 224 |
*/ |
| 225 |
public function shortcode_placement_error() { |
| 226 |
/* |
| 227 |
* translators: %s: module id |
| 228 |
*/ |
| 229 |
$message = __( 'The shortcode <strong>[merchant_module_%s]</strong> can only be used on single product pages.', 'merchant' ); |
| 230 |
$message = sprintf( $message, str_replace( '-', '_', $this->module_id ) ); |
| 231 |
$message = wp_kses( $message, array( |
| 232 |
'strong' => array(), |
| 233 |
) ); |
| 234 |
|
| 235 |
/** |
| 236 |
* Filter the shortcode error message html content. |
| 237 |
* |
| 238 |
* @param string $message_content |
| 239 |
* @param string $module_id |
| 240 |
* |
| 241 |
* @since 1.8 |
| 242 |
*/ |
| 243 |
return apply_filters( 'merchant_module_shortcode_error_message_html', |
| 244 |
'<div class="merchant-shortcode-wrong-placement">' . |
| 245 |
$message |
| 246 |
. '</div>', |
| 247 |
$this->module_id ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Check if shortcode is enabled. |
| 252 |
* |
| 253 |
* @return bool |
| 254 |
*/ |
| 255 |
public function is_shortcode_enabled() { |
| 256 |
|
| 257 |
/** |
| 258 |
* Hook 'merchant_{$this->module_id}_is_shortcode_enabled' |
| 259 |
* |
| 260 |
* @since 1.9.3 |
| 261 |
*/ |
| 262 |
return apply_filters( "merchant_{$this->module_id}_is_shortcode_enabled", Merchant_Admin_Options::get( $this->module_id, 'use_shortcode', false ) ); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Recursively merges user-defined arguments into default arguments. |
| 267 |
* |
| 268 |
* @param $args |
| 269 |
* @param $defaults |
| 270 |
* |
| 271 |
* @return mixed |
| 272 |
*/ |
| 273 |
private function recursive_parse_args( $args, $defaults ) { |
| 274 |
$result = $defaults; |
| 275 |
|
| 276 |
foreach ( $args as $key => $value ) { |
| 277 |
// If the value is an array and the corresponding default is also an array, merge them recursively. |
| 278 |
if ( is_array( $value ) && isset( $result[ $key ] ) && is_array( $result[ $key ] ) ) { |
| 279 |
$result[ $key ] = $this->recursive_parse_args( $value, $result[ $key ] ); |
| 280 |
} else { |
| 281 |
$result[ $key ] = $value; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
return $result; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Ensure $product is an object in the Breakdance builder editor. |
| 290 |
* If $product is a string, convert it to a WooCommerce product object. |
| 291 |
* |
| 292 |
* @return void |
| 293 |
*/ |
| 294 |
public function setup_product_object() { |
| 295 |
if ( ! class_exists( 'WooCommerce' ) ) { |
| 296 |
return; |
| 297 |
} |
| 298 |
|
| 299 |
if ( ! is_product() ) { |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
global $product; |
| 304 |
|
| 305 |
// Check if $product is a string and not already an object |
| 306 |
if ( ! is_object( $product ) && is_string( $product ) ) { |
| 307 |
// Retrieve the product object by slug |
| 308 |
$product_object = wc_get_product( get_page_by_path( $product, OBJECT, 'product' ) ); |
| 309 |
|
| 310 |
// Update global $product with the retrieved product object if found |
| 311 |
if ( $product_object ) { |
| 312 |
$product = $product_object; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|