PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.2
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.2
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / inc / abilities / module-adapters / class-merchant-default-module-adapter.php

class-merchant-default-module-adapter.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.3.2, at inc/abilities/module-adapters/class-merchant-default-module-adapter.php

265 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Merchant Default Module Adapter.
4 *
5 * Auto-detects module type and capabilities from get_option_groups().
6 * Provides generic campaign summaries for modules without custom adapters.
7 *
8 * @package Merchant
9 * @since 2.3.0
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Merchant_Default_Module_Adapter
18 *
19 * Falls back to auto-detected campaign handling when a module
20 * doesn't register a custom adapter. Scans get_option_groups()
21 * for flexible_content fields to determine module type.
22 *
23 * @since 2.3.0
24 */
25 class Merchant_Default_Module_Adapter implements Merchant_Module_Abilities_Interface {
26
27 /**
28 * The module identifier.
29 *
30 * @var string
31 */
32 private $module_id;
33
34 /**
35 * Cached campaign field ID (null = not scanned, false = not found).
36 *
37 * @var string|false|null
38 */
39 private $campaign_field_id_cache = null;
40
41 /**
42 * Constructor.
43 *
44 * @param string $module_id The module identifier.
45 */
46 public function __construct( $module_id ) {
47 $this->module_id = $module_id;
48 }
49
50 /**
51 * Get the flexible_content field ID used for campaigns.
52 *
53 * Scans get_option_groups() for a flexible_content field.
54 *
55 * @return string|null Campaign field ID, or null if not campaign-based.
56 */
57 public function get_campaign_field_id() {
58 if ( null !== $this->campaign_field_id_cache ) {
59 return false === $this->campaign_field_id_cache ? null : $this->campaign_field_id_cache;
60 }
61
62 $module = Merchant_Modules::get_module( $this->module_id );
63
64 if ( ! $module ) {
65 $this->campaign_field_id_cache = false;
66 return null;
67 }
68
69 $option_groups = $module->get_option_groups();
70
71 foreach ( $option_groups as $group ) {
72 if ( ! isset( $group['fields'] ) || ! is_array( $group['fields'] ) ) {
73 continue;
74 }
75
76 foreach ( $group['fields'] as $field ) {
77 if ( isset( $field['type'] ) && 'flexible_content' === $field['type'] && isset( $field['id'] ) ) {
78 $this->campaign_field_id_cache = $field['id'];
79 return $field['id'];
80 }
81 }
82 }
83
84 $this->campaign_field_id_cache = false;
85 return null;
86 }
87
88 /**
89 * Get the field used as the campaign title/name.
90 *
91 * Looks for 'title-field' in layout definition, falls back to 'offer-title'.
92 *
93 * @return string The campaign title field key.
94 */
95 public function get_campaign_title_field() {
96 $module = Merchant_Modules::get_module( $this->module_id );
97
98 if ( ! $module ) {
99 return 'offer-title';
100 }
101
102 $option_groups = $module->get_option_groups();
103
104 foreach ( $option_groups as $group ) {
105 if ( ! isset( $group['fields'] ) || ! is_array( $group['fields'] ) ) {
106 continue;
107 }
108
109 foreach ( $group['fields'] as $field ) {
110 if ( isset( $field['type'] ) && 'flexible_content' === $field['type'] ) {
111 if ( isset( $field['title-field'] ) ) {
112 return $field['title-field'];
113 }
114 break 2;
115 }
116 }
117 }
118
119 return 'offer-title';
120 }
121
122 /**
123 * Get a generic campaign summary.
124 *
125 * Extracts key field values: status, discount type/value.
126 *
127 * @param array<string, mixed> $campaign_data Raw campaign field values.
128 *
129 * @return array<string, mixed> Key highlights of this campaign.
130 */
131 public function summarize_campaign( $campaign_data ) {
132 $summary = array();
133
134 // Status detection.
135 if ( isset( $campaign_data['status'] ) ) {
136 $summary['status'] = $campaign_data['status'];
137 }
138
139 // Discount type/value if present.
140 if ( isset( $campaign_data['discount_type'] ) ) {
141 $summary['discount_type'] = $campaign_data['discount_type'];
142 }
143 if ( isset( $campaign_data['discount_value'] ) ) {
144 $summary['discount_value'] = $campaign_data['discount_value'];
145 }
146
147 $summary['field_count'] = count( $campaign_data );
148
149 return $summary;
150 }
151
152 /**
153 * Return schema overrides. Default adapter uses fully auto-generated schema.
154 *
155 * @return array<string, mixed>|null Always null for default adapter.
156 */
157 public function get_schema_overrides() {
158 return null;
159 }
160
161 /**
162 * Get the list of available operations for this module.
163 *
164 * Settings-only: get-settings, update-settings.
165 * Campaign-based: adds campaign CRUD.
166 * Product bundles: settings-only in Free; Pro adds bundle CRUD.
167 * Size chart: settings-only in Free; Pro adds size-chart CRUD + assign.
168 * Wait list: settings-only in Free; Pro adds subscriber list/update/delete.
169 *
170 * @return array<int, string> Available operation slugs.
171 */
172 public function get_available_operations() {
173 // Product bundles: bundle CRUD is a Pro-only capability.
174 if ( 'product-bundles' === $this->module_id ) {
175 $ops = array( 'get-settings', 'update-settings' );
176
177 if ( defined( 'MERCHANT_PRO_VERSION' ) ) {
178 $ops = array_merge( $ops, array(
179 'list-bundles',
180 'create-bundle',
181 'update-bundle',
182 'delete-bundle',
183 ) );
184 }
185
186 return $ops;
187 }
188
189 // Size chart: entity CRUD + assign is a Pro-only capability.
190 if ( 'size-chart' === $this->module_id ) {
191 $ops = array( 'get-settings', 'update-settings' );
192
193 if ( defined( 'MERCHANT_PRO_VERSION' ) ) {
194 $ops = array_merge( $ops, array(
195 'list-size-charts',
196 'create-size-chart',
197 'update-size-chart',
198 'delete-size-chart',
199 'assign-size-chart',
200 ) );
201 }
202
203 return $ops;
204 }
205
206 // Wait list: subscriber triage is a Pro-only capability.
207 if ( 'wait-list' === $this->module_id ) {
208 $ops = array( 'get-settings', 'update-settings' );
209
210 if ( defined( 'MERCHANT_PRO_VERSION' ) ) {
211 $ops = array_merge( $ops, array(
212 'list-waitlist-subscribers',
213 'update-waitlist-subscriber-status',
214 'delete-waitlist-subscriber',
215 ) );
216 }
217
218 return $ops;
219 }
220
221 $base = array( 'get-settings', 'update-settings' );
222
223 if ( null !== $this->get_campaign_field_id() ) {
224 $base = array_merge( $base, array(
225 'list-campaigns',
226 'create-campaign',
227 'update-campaign',
228 'delete-campaign',
229 ) );
230 }
231
232 return array_merge( $base, $this->product_meta_ops() );
233 }
234
235 /**
236 * Get the product-settings/attribute-swatches ops this module additionally advertises.
237 *
238 * pre-orders and add-to-cart-text are free modules served by the
239 * product-settings pair, so they always advertise it. product-audio,
240 * product-video, and product-brand-image are Pro-only, so the pair
241 * only appears when Pro is present. product-swatches mirrors that with
242 * the attribute-swatches pair.
243 *
244 * @return array<int, string>
245 */
246 private function product_meta_ops() {
247 $free_product_settings_modules = array( 'pre-orders', 'add-to-cart-text' );
248 $pro_product_settings_modules = array( 'product-audio', 'product-video', 'product-brand-image' );
249
250 if ( in_array( $this->module_id, $free_product_settings_modules, true ) ) {
251 return array( 'get-product-settings', 'update-product-settings' );
252 }
253
254 if ( in_array( $this->module_id, $pro_product_settings_modules, true ) && defined( 'MERCHANT_PRO_VERSION' ) ) {
255 return array( 'get-product-settings', 'update-product-settings' );
256 }
257
258 if ( 'product-swatches' === $this->module_id && defined( 'MERCHANT_PRO_VERSION' ) ) {
259 return array( 'get-attribute-swatches', 'update-attribute-swatches' );
260 }
261
262 return array();
263 }
264 }
265