| @@ -36,8 +36,18 @@ | ||
| 36 | 36 | */ |
| 37 | 37 | public $module_data = array(); |
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | + * AI prompt examples shown in the "Ask AI" popup for this module. | |
| 41 | + * | |
| 42 | + * Subclasses set this in their constructor with a 'blurb' string | |
| 43 | + * and a 'prompts' array of example prompts. | |
| 44 | + * | |
| 45 | + * @var array | |
| 46 | + */ | |
| 47 | + protected $ai_examples = array(); | |
| 48 | + | |
| 49 | + /** | |
| 40 | 50 | * Module options. |
| 41 | 51 | * |
| 42 | 52 | */ |
| 43 | 53 | public $module_options_path = ''; |
| @@ -48,9 +58,66 @@ | ||
| 48 | 58 | * @var bool |
| 49 | 59 | */ |
| 50 | 60 | public $has_shortcode = false; |
| 51 | 61 | |
| 62 | + protected $has_analytics = false; | |
| 63 | + | |
| 52 | 64 | /** |
| 65 | + * Option group definitions cache. | |
| 66 | + * | |
| 67 | + * @var array<int, array<string, mixed>>|null | |
| 68 | + */ | |
| 69 | + protected $option_groups = null; | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Initialize the module's option group definitions. | |
| 73 | + * | |
| 74 | + * Subclasses override this to define their settings panels. | |
| 75 | + * Each element in the returned array corresponds to one settings | |
| 76 | + * section in the admin UI. | |
| 77 | + * | |
| 78 | + * @return array<int, array<string, mixed>> Array of option group arrays, each with 'module', 'title', and 'fields' keys. | |
| 79 | + */ | |
| 80 | + protected function init_option_groups() { | |
| 81 | + return array(); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Get the module's option group definitions. | |
| 86 | + * | |
| 87 | + * Returns cached definitions if available. On first call, | |
| 88 | + * initializes from the subclass and applies the filter for | |
| 89 | + * third-party extensibility. | |
| 90 | + * | |
| 91 | + * @return array<int, array<string, mixed>> Filtered option group definitions. | |
| 92 | + */ | |
| 93 | + public function get_option_groups() { | |
| 94 | + if ( $this->option_groups !== null ) { | |
| 95 | + return $this->option_groups; | |
| 96 | + } | |
| 97 | + | |
| 98 | + $groups = $this->init_option_groups(); | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Filter a module's option group definitions. | |
| 102 | + * | |
| 103 | + * Allows third-party plugins to add, remove, or modify | |
| 104 | + * the field definitions for any module. | |
| 105 | + * | |
| 106 | + * @param array<int, array<string, mixed>> $groups Array of option group arrays. | |
| 107 | + * | |
| 108 | + * @since 2.3.0 | |
| 109 | + */ | |
| 110 | + $this->option_groups = apply_filters( | |
| 111 | + "merchant_{$this->module_id}_option_groups", | |
| 112 | + $groups | |
| 113 | + ); | |
| 114 | + | |
| 115 | + return $this->option_groups; | |
| 116 | + } | |
| 117 | + | |
| 118 | + | |
| 119 | + /** | |
| 53 | 120 | * Constructor. |
| 54 | 121 | * |
| 55 | 122 | */ |
| 56 | 123 | public function __construct() { |
| @@ -66,13 +133,82 @@ | ||
| 66 | 133 | // Handle modules list item class. |
| 67 | 134 | add_filter( "merchant_admin_module_{$this->module_id}_list_item_class", array( $this, 'modules_list_item_class' ) ); |
| 68 | 135 | |
| 69 | 136 | if ( $this->has_shortcode ) { |
| 137 | + add_action( 'wp', array( $this, 'setup_product_object' ) ); | |
| 70 | 138 | add_shortcode( 'merchant_module_' . str_replace( '-', '_', $this->module_id ), array( $this, 'shortcode_handler' ) ); |
| 71 | 139 | } |
| 140 | + | |
| 141 | + // Remove merchant shortcodes from Botiga product card short description | |
| 142 | + add_filter( 'botiga_loop_product_elements', function( $elements ) { | |
| 143 | + add_filter( 'get_the_excerpt', function( $excerpt ) { | |
| 144 | + return preg_replace( '/\[merchant_[^]]+]/', '', $excerpt ); | |
| 145 | + } ); | |
| 146 | + | |
| 147 | + return $elements; | |
| 148 | + } ); | |
| 72 | 149 | } |
| 73 | 150 | |
| 74 | 151 | /** |
| 152 | + * Check if the module has analytics. | |
| 153 | + * | |
| 154 | + * @return bool | |
| 155 | + */ | |
| 156 | + public function has_analytics() { | |
| 157 | + return $this->has_analytics; | |
| 158 | + } | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * Get the module's AI prompt examples. | |
| 162 | + * | |
| 163 | + * @return array Array with 'blurb' and 'prompts' keys, or an empty array if the module has none. | |
| 164 | + */ | |
| 165 | + public function get_ai_examples() { | |
| 166 | + /** | |
| 167 | + * Filter a module's AI prompt examples. | |
| 168 | + * | |
| 169 | + * @param array $ai_examples Array with 'blurb' and 'prompts' keys. | |
| 170 | + * | |
| 171 | + * @since 2.3.0 | |
| 172 | + */ | |
| 173 | + return apply_filters( "merchant_{$this->module_id}_ai_examples", $this->ai_examples ); | |
| 174 | + } | |
| 175 | + | |
| 176 | + /** | |
| 177 | + * Get all analytics metrics and allow modules to filter them. | |
| 178 | + * | |
| 179 | + * @return array List of available metrics. | |
| 180 | + */ | |
| 181 | + public function analytics_metrics() { | |
| 182 | + /** | |
| 183 | + * Hook: merchant_analytics_module_metrics | |
| 184 | + * | |
| 185 | + * @param array $metrics List of available metrics. | |
| 186 | + * @param string $module_id Module ID. | |
| 187 | + * | |
| 188 | + * @since 2.0 | |
| 189 | + */ | |
| 190 | + return apply_filters( 'merchant_analytics_module_metrics', $this->default_analytics_metrics(), $this->module_id, $this ); | |
| 191 | + } | |
| 192 | + | |
| 193 | + /** | |
| 194 | + * Get analytics metrics. | |
| 195 | + * | |
| 196 | + * @return array List of available metrics. | |
| 197 | + */ | |
| 198 | + protected function default_analytics_metrics() { | |
| 199 | + return array( | |
| 200 | + 'campaigns' => true, | |
| 201 | + 'impressions' => true, | |
| 202 | + 'clicks' => true, | |
| 203 | + 'ctr' => true, | |
| 204 | + 'revenue' => true, | |
| 205 | + 'orders_count' => true, | |
| 206 | + 'aov' => true, | |
| 207 | + ); | |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 75 | 211 | * Active modules class handler. |
| 76 | 212 | * |
| 77 | 213 | */ |
| 78 | 214 | public function add_module_activation_status_class( $classes ) { |
| @@ -118,9 +254,9 @@ | ||
| 118 | 254 | * Get module settings. |
| 119 | 255 | * |
| 120 | 256 | */ |
| 121 | 257 | public function get_module_settings() { |
| 122 | - $settings = get_option( 'merchant' ) ? get_option( 'merchant' ) : array(); | |
| 258 | + $settings = get_option( 'merchant', array() ); | |
| 123 | 259 | |
| 124 | 260 | // Default settings. |
| 125 | 261 | $defaults = $this->module_default_settings; |
| 126 | 262 | |
| @@ -131,58 +267,44 @@ | ||
| 131 | 267 | // Parse settings with defaults. |
| 132 | 268 | // Todo: check if recursive_parse_args() works for all modules and remove the condition. |
| 133 | 269 | $settings = $this->module_id === 'product-labels' ? $this->recursive_parse_args( $settings[ $this->module_id ], $defaults ) : wp_parse_args( $settings[ $this->module_id ], $defaults ); |
| 134 | 270 | |
| 271 | + /** | |
| 272 | + * Hook: merchant_module_settings | |
| 273 | + * | |
| 274 | + * @param array $settings Module settings. | |
| 275 | + * @param string $module_id Module ID. | |
| 276 | + * | |
| 277 | + * @since 1.9.16 | |
| 278 | + */ | |
| 279 | + $settings = apply_filters( 'merchant_module_settings', $settings, $this->module_id ); | |
| 280 | + | |
| 135 | 281 | return $settings; |
| 136 | 282 | } |
| 137 | 283 | |
| 138 | 284 | /** |
| 139 | - * Get preview URL | |
| 285 | + * Update module settings. | |
| 140 | 286 | * |
| 141 | - * @param array $args | |
| 287 | + * @param array $module_settings | |
| 142 | 288 | * |
| 143 | - * @return string | |
| 289 | + * @return void | |
| 144 | 290 | */ |
| 145 | - public function set_module_preview_url( $args = array() ) { | |
| 146 | - // Mount preview url. | |
| 147 | - $preview_url = site_url( '/' ); | |
| 291 | + public function update_module_settings( $module_settings ) { | |
| 292 | + $settings = get_option( 'merchant', array() ); | |
| 148 | 293 | |
| 149 | - // Type based preview url | |
| 150 | - if ( isset( $args['type'] ) ) { | |
| 151 | - switch ( $args['type'] ) { | |
| 152 | - case 'shop': | |
| 153 | - if ( function_exists( 'wc_get_page_id' ) ) { | |
| 154 | - $preview_url = get_permalink( wc_get_page_id( 'shop' ) ); | |
| 155 | - } | |
| 156 | - break; | |
| 294 | + $settings[ $this->module_id ] = $module_settings; | |
| 157 | 295 | |
| 158 | - case 'product': | |
| 159 | - $query_args = array( | |
| 160 | - 'post_type' => 'product', | |
| 161 | - 'posts_per_page' => 1, | |
| 162 | - ); | |
| 296 | + /** | |
| 297 | + * Hook: merchant_module_settings_update | |
| 298 | + * | |
| 299 | + * @param array $settings Module settings. | |
| 300 | + * @param string $module_id Module ID. | |
| 301 | + * | |
| 302 | + * @since 2.0.0 | |
| 303 | + */ | |
| 304 | + $settings = apply_filters( 'merchant_module_settings_update', $settings, $this->module_id ); | |
| 163 | 305 | |
| 164 | - if ( isset( $args['query'] ) ) { | |
| 165 | - $products = ( new WP_Query( wp_parse_args( $args['query'], $query_args ) ) )->get_posts(); | |
| 166 | - | |
| 167 | - // If no results can be found with the custom query, | |
| 168 | - // then use the default args | |
| 169 | - if ( empty( $products ) || ! isset( $products[0] ) ) { | |
| 170 | - $products = ( new WP_Query( $query_args ) )->get_posts(); | |
| 171 | - } | |
| 172 | - } else { | |
| 173 | - $products = ( new WP_Query( $query_args ) )->get_posts(); | |
| 174 | - } | |
| 175 | - | |
| 176 | - if ( ! empty( $products ) && isset( $products[0] ) ) { | |
| 177 | - $preview_url = get_permalink( $products[0] ); | |
| 178 | - } | |
| 179 | - | |
| 180 | - break; | |
| 181 | - } | |
| 182 | - } | |
| 183 | - | |
| 184 | - return $preview_url; | |
| 306 | + update_option( 'merchant', $settings ); | |
| 185 | 307 | } |
| 186 | 308 | |
| 187 | 309 | /** |
| 188 | 310 | * Add module. |
| @@ -196,18 +318,60 @@ | ||
| 196 | 318 | |
| 197 | 319 | /** |
| 198 | 320 | * Add module options. |
| 199 | 321 | * |
| 322 | + * For migrated modules (those with option groups defined), | |
| 323 | + * renders settings directly from get_option_groups(). | |
| 324 | + * Unmigrated modules fall back to including $module_options_path. | |
| 325 | + * | |
| 326 | + * @param string $module_path Default file path. | |
| 327 | + * @param string $merchant_module Module ID being rendered. | |
| 328 | + * | |
| 329 | + * @return string File path to include, or empty string if already rendered. | |
| 200 | 330 | */ |
| 201 | 331 | public function add_module_options( $module_path, $merchant_module ) { |
| 202 | - if ( $this->module_id === $merchant_module ) { | |
| 203 | - return $this->module_options_path; | |
| 332 | + if ( $this->module_id !== $merchant_module ) { | |
| 333 | + return $module_path; | |
| 204 | 334 | } |
| 205 | 335 | |
| 206 | - return $module_path; | |
| 336 | + $groups = $this->get_option_groups(); | |
| 337 | + | |
| 338 | + if ( ! empty( $groups ) ) { | |
| 339 | + $this->before_render_option_groups(); | |
| 340 | + | |
| 341 | + foreach ( $groups as $group ) { | |
| 342 | + Merchant_Admin_Options::create( $group ); | |
| 343 | + } | |
| 344 | + | |
| 345 | + $this->after_render_option_groups(); | |
| 346 | + | |
| 347 | + return ''; | |
| 348 | + } | |
| 349 | + | |
| 350 | + return $this->module_options_path; | |
| 207 | 351 | } |
| 208 | 352 | |
| 209 | 353 | /** |
| 354 | + * Called before option groups are rendered in the admin. | |
| 355 | + * | |
| 356 | + * Override in subclasses that need to fire actions | |
| 357 | + * before settings fields (e.g., help banners). | |
| 358 | + * | |
| 359 | + * @return void | |
| 360 | + */ | |
| 361 | + protected function before_render_option_groups() {} | |
| 362 | + | |
| 363 | + /** | |
| 364 | + * Called after option groups are rendered in the admin. | |
| 365 | + * | |
| 366 | + * Override in subclasses that need to fire actions | |
| 367 | + * after settings fields. | |
| 368 | + * | |
| 369 | + * @return void | |
| 370 | + */ | |
| 371 | + protected function after_render_option_groups() {} | |
| 372 | + | |
| 373 | + /** | |
| 210 | 374 | * Display error message if the shortcode is placed in the wrong place. |
| 211 | 375 | * |
| 212 | 376 | * @return mixed|null |
| 213 | 377 | */ |
| @@ -232,9 +396,9 @@ | ||
| 232 | 396 | return apply_filters( 'merchant_module_shortcode_error_message_html', |
| 233 | 397 | '<div class="merchant-shortcode-wrong-placement">' . |
| 234 | 398 | $message |
| 235 | 399 | . '</div>', |
| 236 | - $this->module->module_id ); | |
| 400 | + $this->module_id ); | |
| 237 | 401 | } |
| 238 | 402 | |
| 239 | 403 | /** |
| 240 | 404 | * Check if shortcode is enabled. |
| @@ -271,6 +435,35 @@ | ||
| 271 | 435 | } |
| 272 | 436 | } |
| 273 | 437 | |
| 274 | 438 | return $result; |
| 439 | + } | |
| 440 | + | |
| 441 | + /** | |
| 442 | + * Ensure $product is an object in the Breakdance builder editor. | |
| 443 | + * If $product is a string, convert it to a WooCommerce product object. | |
| 444 | + * | |
| 445 | + * @return void | |
| 446 | + */ | |
| 447 | + public function setup_product_object() { | |
| 448 | + if ( ! class_exists( 'WooCommerce' ) ) { | |
| 449 | + return; | |
| 450 | + } | |
| 451 | + | |
| 452 | + if ( ! is_product() ) { | |
| 453 | + return; | |
| 454 | + } | |
| 455 | + | |
| 456 | + global $product; | |
| 457 | + | |
| 458 | + // Check if $product is a string and not already an object | |
| 459 | + if ( ! is_object( $product ) && is_string( $product ) ) { | |
| 460 | + // Retrieve the product object by slug | |
| 461 | + $product_object = wc_get_product( get_page_by_path( $product, OBJECT, 'product' ) ); | |
| 462 | + | |
| 463 | + // Update global $product with the retrieved product object if found | |
| 464 | + if ( $product_object ) { | |
| 465 | + $product = $product_object; | |
| 466 | + } | |
| 467 | + } | |
| 275 | 468 | } |
| 276 | 469 | } |