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 / class-merchant-campaign-resolver.php

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

146 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Merchant Campaign Resolver.
4 *
5 * Resolves campaign identifiers (name or index) to actual campaign data
6 * from the module's flexible_content field.
7 *
8 * @package Merchant
9 * @since 2.3.0
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Merchant_Campaign_Resolver
18 *
19 * Locates a specific campaign within a module's flexible_content
20 * settings by index (integer) or by title (string).
21 *
22 * @since 2.3.0
23 */
24 class Merchant_Campaign_Resolver {
25
26 /**
27 * Resolve a campaign by name (string) or index (integer).
28 *
29 * @param string $module_id The module identifier.
30 * @param string|int $campaign_identifier Name or index.
31 * @param string $campaign_field_id The flexible_content field ID.
32 * @param string $title_field The field used as campaign title.
33 *
34 * @return array{index: int, campaign: array<string, mixed>}|array{error: array<string, mixed>}
35 */
36 public function resolve( $module_id, $campaign_identifier, $campaign_field_id, $title_field = 'offer-title' ) {
37 $module = Merchant_Modules::get_module( $module_id );
38
39 if ( ! $module ) {
40 return array(
41 'error' => array(
42 'code' => 'module_not_found',
43 'message' => sprintf( "Module '%s' not found.", $module_id ),
44 ),
45 );
46 }
47
48 $settings = $module->get_module_settings();
49 $campaigns = isset( $settings[ $campaign_field_id ] ) ? $settings[ $campaign_field_id ] : array();
50
51 if ( ! is_array( $campaigns ) || empty( $campaigns ) ) {
52 return array(
53 'error' => array(
54 'code' => 'campaign_not_found',
55 'message' => sprintf( "No campaigns found in '%s'.", $module_id ),
56 ),
57 );
58 }
59
60 // Resolve by flexible_id UUID (highest priority — guaranteed unique).
61 $uuid_result = $this->resolve_by_uuid( $campaigns, (string) $campaign_identifier );
62 if ( null !== $uuid_result ) {
63 return $uuid_result;
64 }
65
66 // Resolve by index.
67 if ( is_int( $campaign_identifier ) || ctype_digit( (string) $campaign_identifier ) ) {
68 $index = (int) $campaign_identifier;
69
70 if ( ! isset( $campaigns[ $index ] ) ) {
71 return array(
72 'error' => array(
73 'code' => 'campaign_not_found',
74 'message' => sprintf( "Campaign index %d not found. Total campaigns: %d.", $index, count( $campaigns ) ),
75 'available' => $this->get_available_names( $campaigns, $title_field ),
76 ),
77 );
78 }
79
80 return array(
81 'index' => $index,
82 'campaign' => $campaigns[ $index ],
83 );
84 }
85
86 // Resolve by name (case insensitive).
87 $search = strtolower( (string) $campaign_identifier );
88 foreach ( $campaigns as $index => $campaign ) {
89 $name = isset( $campaign[ $title_field ] ) ? $campaign[ $title_field ] : '';
90 if ( strtolower( $name ) === $search ) {
91 return array(
92 'index' => $index,
93 'campaign' => $campaign,
94 );
95 }
96 }
97
98 return array(
99 'error' => array(
100 'code' => 'campaign_not_found',
101 'message' => sprintf( "Campaign '%s' not found in '%s'.", $campaign_identifier, $module_id ),
102 'available' => $this->get_available_names( $campaigns, $title_field ),
103 ),
104 );
105 }
106
107 /**
108 * Resolve a campaign by its flexible_id UUID.
109 *
110 * @param array<int, array<string, mixed>> $campaigns The campaigns array.
111 * @param string $identifier The identifier to match against flexible_id.
112 *
113 * @return array{index: int, campaign: array<string, mixed>}|null
114 */
115 private function resolve_by_uuid( $campaigns, $identifier ) {
116 foreach ( $campaigns as $index => $campaign ) {
117 if ( isset( $campaign['flexible_id'] ) && $campaign['flexible_id'] === $identifier ) {
118 return array(
119 'index' => $index,
120 'campaign' => $campaign,
121 );
122 }
123 }
124
125 return null;
126 }
127
128 /**
129 * Get available campaign names for error messages.
130 *
131 * @param array<int, array<string, mixed>> $campaigns The campaigns array.
132 * @param string $title_field The title field key.
133 *
134 * @return array<int, string> Campaign names.
135 */
136 private function get_available_names( $campaigns, $title_field ) {
137 $names = array();
138 foreach ( $campaigns as $campaign ) {
139 if ( isset( $campaign[ $title_field ] ) && '' !== $campaign[ $title_field ] ) {
140 $names[] = $campaign[ $title_field ];
141 }
142 }
143 return $names;
144 }
145 }
146