| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace RenzoJohnson\Blocks\Signls; |
| 6 |
|
| 7 |
use RenzoJohnson\Blocks\LiveChat\ProviderIdValidator; |
| 8 |
|
| 9 |
\defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Supplies the closed, aggregate Blocks product snapshot. |
| 13 |
* |
| 14 |
* Every emitted dimension is defined below. Option values are reduced to |
| 15 |
* booleans at the boundary, and no content, identifiers, secrets, errors, |
| 16 |
* commerce records, or visitor activity can enter the snapshot. |
| 17 |
*/ |
| 18 |
final class Adapter implements \Signls\Sdk\V1\ProductAdapterInterface { |
| 19 |
|
| 20 |
private const INTEGRATIONS = array( |
| 21 |
'woocommerce', |
| 22 |
'woocommerce-subscriptions', |
| 23 |
'contact-form-7', |
| 24 |
'google-analytics-4', |
| 25 |
'olark', |
| 26 |
'crisp', |
| 27 |
'tawk', |
| 28 |
); |
| 29 |
|
| 30 |
private const FEATURES = array( |
| 31 |
'reusable_content', |
| 32 |
'site_variables', |
| 33 |
'content_transfer', |
| 34 |
'faq_content', |
| 35 |
'feature_content', |
| 36 |
'glossary_content', |
| 37 |
'release_content', |
| 38 |
'page_builders', |
| 39 |
'pricing', |
| 40 |
'coupons', |
| 41 |
'woo_checkout', |
| 42 |
'woo_orders', |
| 43 |
'woo_subscriptions', |
| 44 |
'ga4_configuration', |
| 45 |
'ga4_delivery', |
| 46 |
'cf7_anti_spam', |
| 47 |
'live_chat', |
| 48 |
'performance', |
| 49 |
'security', |
| 50 |
'hide_login', |
| 51 |
'svg_dependency', |
| 52 |
'compatibility', |
| 53 |
'lifecycle', |
| 54 |
'reliability', |
| 55 |
); |
| 56 |
|
| 57 |
private const COUPON_OPTIONS = array( |
| 58 |
'blocks_woo_coupon_category_enabled', |
| 59 |
'blocks_coupon_shortcode_enabled', |
| 60 |
'blocks_woo_checkout_coupon_enabled', |
| 61 |
); |
| 62 |
|
| 63 |
private const WOO_CHECKOUT_OPTIONS = array( |
| 64 |
'blocks_woo_checkout_message_before_enabled', |
| 65 |
'blocks_woo_checkout_message_after_enabled', |
| 66 |
'blocks_woo_checkout_remove_added_message_enabled', |
| 67 |
'blocks_woo_checkout_redirect_to_checkout_enabled', |
| 68 |
'blocks_woo_checkout_single_item_cart_enabled', |
| 69 |
'blocks_woo_checkout_redirect_to_account_enabled', |
| 70 |
'blocks_woo_checkout_coupon_enabled', |
| 71 |
); |
| 72 |
|
| 73 |
private const WOO_ORDER_OPTIONS = array( |
| 74 |
'blocks_woo_auto_complete_enabled', |
| 75 |
'blocks_woo_thank_you_enabled', |
| 76 |
'blocks_woo_email_bcc_enabled', |
| 77 |
'blocks_woo_email_subject_completed_enabled', |
| 78 |
'blocks_woo_email_subject_renewal_enabled', |
| 79 |
'blocks_woo_email_subject_cancelled_enabled', |
| 80 |
); |
| 81 |
|
| 82 |
private const WOO_SUBSCRIPTION_OPTIONS = array( |
| 83 |
'blocks_woo_upgrade_button_enabled', |
| 84 |
'blocks_woo_subscription_upcoming_enabled', |
| 85 |
'blocks_woo_subscription_fixer_enabled', |
| 86 |
'blocks_woo_subscription_email_enabled', |
| 87 |
); |
| 88 |
|
| 89 |
private const GA4_EVENT_OPTIONS = array( |
| 90 |
'blocks_ga4_404_enabled', |
| 91 |
'blocks_ga4_zaraz_enabled', |
| 92 |
'blocks_ga4_purchase_enabled', |
| 93 |
'blocks_ga4_add_to_cart_enabled', |
| 94 |
'blocks_ga4_remove_from_cart_enabled', |
| 95 |
'blocks_ga4_view_item_list_enabled', |
| 96 |
'blocks_ga4_select_content_enabled', |
| 97 |
'blocks_ga4_view_item_enabled', |
| 98 |
'blocks_ga4_begin_checkout_enabled', |
| 99 |
); |
| 100 |
|
| 101 |
private const PERFORMANCE_OPTIONS = array( |
| 102 |
'blocks_perf_disable_gutenberg', |
| 103 |
'blocks_perf_remove_global_styles', |
| 104 |
'blocks_perf_remove_jquery_migrate', |
| 105 |
'blocks_perf_disable_emoji', |
| 106 |
'blocks_perf_disable_speculation_rules', |
| 107 |
'blocks_perf_dequeue_img_auto_sizes', |
| 108 |
'blocks_perf_dequeue_block_css', |
| 109 |
'blocks_perf_dequeue_woo_css', |
| 110 |
'blocks_perf_dequeue_woo_block_css', |
| 111 |
'blocks_perf_dequeue_stripe_css', |
| 112 |
'blocks_perf_dequeue_classic_theme_styles', |
| 113 |
'blocks_perf_dequeue_woo_inline_css', |
| 114 |
'blocks_perf_dequeue_cart_fragments', |
| 115 |
'blocks_perf_dequeue_woo_noscript', |
| 116 |
'blocks_perf_dequeue_apfs', |
| 117 |
'blocks_perf_delay_analytics', |
| 118 |
); |
| 119 |
|
| 120 |
public function __construct( private readonly \Blocks_Settings_Repository $settings ) { |
| 121 |
} |
| 122 |
|
| 123 |
public function product_slug(): string { |
| 124 |
return Runtime::PRODUCT; |
| 125 |
} |
| 126 |
|
| 127 |
public function product_version(): string { |
| 128 |
return \defined( 'BLOCKS_VERSION' ) ? (string) BLOCKS_VERSION : 'unknown'; |
| 129 |
} |
| 130 |
|
| 131 |
public function signal_sharing_enabled(): bool { |
| 132 |
return Runtime::enabled(); |
| 133 |
} |
| 134 |
|
| 135 |
public function install_id(): string { |
| 136 |
return Runtime::install_id(); |
| 137 |
} |
| 138 |
|
| 139 |
/** @return array<string, mixed> */ |
| 140 |
public function contract(): array { |
| 141 |
return array( |
| 142 |
'snapshot_schema_version' => 1, |
| 143 |
'snapshot_payload_revision' => 1, |
| 144 |
'integrations' => self::INTEGRATIONS, |
| 145 |
'features' => self::FEATURES, |
| 146 |
'operations' => array(), |
| 147 |
'companions' => array(), |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
/** @return array<string, mixed> */ |
| 152 |
public function snapshot(): array { |
| 153 |
if ( ! $this->signal_sharing_enabled() ) { |
| 154 |
return array(); |
| 155 |
} |
| 156 |
|
| 157 |
$states = $this->feature_states(); |
| 158 |
$active = \count( \array_filter( $states ) ); |
| 159 |
|
| 160 |
return array( |
| 161 |
'versions' => array( 'wordpress' => \get_bloginfo( 'version' ) ), |
| 162 |
'is_multisite' => \is_multisite(), |
| 163 |
'configured_units' => $active, |
| 164 |
'active_units' => $active, |
| 165 |
'integrations' => $this->integration_rows( $states ), |
| 166 |
'features' => $this->feature_rows( $states ), |
| 167 |
'operation_health' => array(), |
| 168 |
'companions' => array(), |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
/** @return list<string> */ |
| 173 |
public function observed_options(): array { |
| 174 |
return \array_values( |
| 175 |
\array_unique( |
| 176 |
\array_merge( |
| 177 |
array( |
| 178 |
'blocks_faq_enabled', |
| 179 |
'blocks_feature_enabled', |
| 180 |
'blocks_glossary_enabled', |
| 181 |
'blocks_release_enabled', |
| 182 |
'blocks_pricing_enabled', |
| 183 |
'blocks_ga4_ecommerce_enabled', |
| 184 |
'blocks_ga4_measurement_id', |
| 185 |
'blocks_honeypot_enabled', |
| 186 |
'blocks_cf7_keyword_filter_enabled', |
| 187 |
'blocks_livechat_enabled', |
| 188 |
'blocks_livechat_olark', |
| 189 |
'blocks_livechat_olark_id', |
| 190 |
'blocks_livechat_crisp', |
| 191 |
'blocks_livechat_crisp_id', |
| 192 |
'blocks_livechat_tawk', |
| 193 |
'blocks_livechat_tawk_id', |
| 194 |
'blocks_hide_login_enabled', |
| 195 |
'blocks_login_slug', |
| 196 |
'blocks_svg_upload_enabled', |
| 197 |
), |
| 198 |
self::COUPON_OPTIONS, |
| 199 |
self::WOO_CHECKOUT_OPTIONS, |
| 200 |
self::WOO_ORDER_OPTIONS, |
| 201 |
self::WOO_SUBSCRIPTION_OPTIONS, |
| 202 |
self::GA4_EVENT_OPTIONS, |
| 203 |
self::PERFORMANCE_OPTIONS |
| 204 |
) |
| 205 |
) |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* @param array<string, bool> $features Feature states. |
| 211 |
* @return list<array<string, int|string>> |
| 212 |
*/ |
| 213 |
private function integration_rows( array $features ): array { |
| 214 |
$woocommerce = \class_exists( 'WooCommerce' ); |
| 215 |
$subscriptions = $woocommerce && \class_exists( 'WC_Subscriptions' ); |
| 216 |
$cf7 = \class_exists( 'WPCF7_ContactForm' ) || \defined( 'WPCF7_VERSION' ); |
| 217 |
$ga4 = $features['ga4_configuration']; |
| 218 |
$providers = $this->provider_states(); |
| 219 |
|
| 220 |
return array( |
| 221 |
$this->integration( 'woocommerce', $woocommerce, $features['coupons'] || $features['woo_checkout'] || $features['woo_orders'] ), |
| 222 |
$this->integration( 'woocommerce-subscriptions', $subscriptions, $features['woo_subscriptions'] ), |
| 223 |
$this->integration( 'contact-form-7', $cf7, $features['cf7_anti_spam'] ), |
| 224 |
$this->integration( 'google-analytics-4', $ga4, $features['ga4_delivery'] ), |
| 225 |
$this->integration( 'olark', $providers['olark'], $providers['olark'] ), |
| 226 |
$this->integration( 'crisp', $providers['crisp'], $providers['crisp'] ), |
| 227 |
$this->integration( 'tawk', $providers['tawk'], $providers['tawk'] ), |
| 228 |
); |
| 229 |
} |
| 230 |
|
| 231 |
/** @return array<string, int|string> */ |
| 232 |
private function integration( string $slug, bool $configured, bool $active ): array { |
| 233 |
return array( |
| 234 |
'slug' => $slug, |
| 235 |
'configured_units' => $configured ? 1 : 0, |
| 236 |
'credential_units' => 0, |
| 237 |
'oauth_units' => 0, |
| 238 |
'api_key_units' => 0, |
| 239 |
'destination_units' => $active ? 1 : 0, |
| 240 |
'mapping_count' => 0, |
| 241 |
'attempts' => 0, |
| 242 |
'successes' => 0, |
| 243 |
'failures' => 0, |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* @param array<string, bool> $states Feature states. |
| 249 |
* @return list<array{slug: string, configured_units: int, source: string}> |
| 250 |
*/ |
| 251 |
private function feature_rows( array $states ): array { |
| 252 |
$rows = array(); |
| 253 |
foreach ( self::FEATURES as $slug ) { |
| 254 |
$rows[] = array( |
| 255 |
'slug' => $slug, |
| 256 |
'configured_units' => ! empty( $states[ $slug ] ) ? 1 : 0, |
| 257 |
'source' => 'lite_config', |
| 258 |
); |
| 259 |
} |
| 260 |
return $rows; |
| 261 |
} |
| 262 |
|
| 263 |
/** @return array<string, bool> */ |
| 264 |
private function feature_states(): array { |
| 265 |
$woocommerce = \class_exists( 'WooCommerce' ); |
| 266 |
$subscriptions = $woocommerce && \class_exists( 'WC_Subscriptions' ); |
| 267 |
$cf7 = \class_exists( 'WPCF7_ContactForm' ) || \defined( 'WPCF7_VERSION' ); |
| 268 |
$ga4 = $this->settings->get_bool( 'blocks_ga4_ecommerce_enabled' ) |
| 269 |
&& 1 === \preg_match( '/^G-[A-Z0-9]+$/D', \strtoupper( $this->settings->get_string( 'blocks_ga4_measurement_id' ) ) ); |
| 270 |
$providers = $this->provider_states(); |
| 271 |
|
| 272 |
return array( |
| 273 |
'reusable_content' => true, |
| 274 |
'site_variables' => true, |
| 275 |
'content_transfer' => true, |
| 276 |
'faq_content' => $this->settings->get_bool( 'blocks_faq_enabled' ), |
| 277 |
'feature_content' => $this->settings->get_bool( 'blocks_feature_enabled' ), |
| 278 |
'glossary_content' => $this->settings->get_bool( 'blocks_glossary_enabled' ), |
| 279 |
'release_content' => $this->settings->get_bool( 'blocks_release_enabled' ), |
| 280 |
'page_builders' => true, |
| 281 |
'pricing' => $woocommerce && $this->settings->get_bool( 'blocks_pricing_enabled' ), |
| 282 |
'coupons' => $woocommerce && $this->any_enabled( self::COUPON_OPTIONS ), |
| 283 |
'woo_checkout' => $woocommerce && $this->any_enabled( self::WOO_CHECKOUT_OPTIONS ), |
| 284 |
'woo_orders' => $woocommerce && $this->any_enabled( self::WOO_ORDER_OPTIONS ), |
| 285 |
'woo_subscriptions' => $subscriptions && $this->any_enabled( self::WOO_SUBSCRIPTION_OPTIONS ), |
| 286 |
'ga4_configuration' => $ga4, |
| 287 |
'ga4_delivery' => $ga4 && $this->any_enabled( self::GA4_EVENT_OPTIONS ), |
| 288 |
'cf7_anti_spam' => $cf7 && ( $this->settings->get_bool( 'blocks_honeypot_enabled' ) || $this->settings->get_bool( 'blocks_cf7_keyword_filter_enabled' ) ), |
| 289 |
'live_chat' => \in_array( true, $providers, true ), |
| 290 |
'performance' => $this->any_enabled( self::PERFORMANCE_OPTIONS ), |
| 291 |
'security' => true, |
| 292 |
'hide_login' => $this->settings->get_bool( 'blocks_hide_login_enabled' ) && '' !== $this->settings->get_string( 'blocks_login_slug' ), |
| 293 |
'svg_dependency' => $this->settings->get_bool( 'blocks_svg_upload_enabled' ) && \Blocks_Svg_Upload::has_safe_svg_upload_pipeline(), |
| 294 |
'compatibility' => true, |
| 295 |
'lifecycle' => true, |
| 296 |
'reliability' => true, |
| 297 |
); |
| 298 |
} |
| 299 |
|
| 300 |
/** @return array{olark: bool, crisp: bool, tawk: bool} */ |
| 301 |
private function provider_states(): array { |
| 302 |
$master = $this->settings->get_bool( 'blocks_livechat_enabled' ); |
| 303 |
$states = array(); |
| 304 |
foreach ( array( 'olark', 'crisp', 'tawk' ) as $provider ) { |
| 305 |
$identifier = $this->settings->get_string( 'blocks_livechat_' . $provider . '_id' ); |
| 306 |
$states[ $provider ] = $master |
| 307 |
&& $this->settings->get_bool( 'blocks_livechat_' . $provider ) |
| 308 |
&& ProviderIdValidator::is_valid( $provider, $identifier ); |
| 309 |
} |
| 310 |
|
| 311 |
/** @var array{olark: bool, crisp: bool, tawk: bool} $states */ |
| 312 |
return $states; |
| 313 |
} |
| 314 |
|
| 315 |
/** @param list<string> $options */ |
| 316 |
private function any_enabled( array $options ): bool { |
| 317 |
foreach ( $options as $option ) { |
| 318 |
if ( $this->settings->get_bool( $option ) ) { |
| 319 |
return true; |
| 320 |
} |
| 321 |
} |
| 322 |
return false; |
| 323 |
} |
| 324 |
} |
| 325 |
|