| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace RenzoJohnson\Blocks\Settings; |
| 6 |
|
| 7 |
use RenzoJohnson\Blocks\Security\SecretStore; |
| 8 |
|
| 9 |
\defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
final class SettingsSchemaBuilder { |
| 12 |
public function __construct( |
| 13 |
private readonly \Blocks_Settings_Repository $settings, |
| 14 |
private readonly SecretStore $secrets, |
| 15 |
) { |
| 16 |
} |
| 17 |
|
| 18 |
/** @return list<array<string, mixed>> */ |
| 19 |
public function build( SettingsRegistry $registry ): array { |
| 20 |
$tabs = array(); |
| 21 |
|
| 22 |
foreach ( $registry->tabs() as $tab ) { |
| 23 |
$sections = array(); |
| 24 |
|
| 25 |
foreach ( $tab->sections as $section ) { |
| 26 |
$fields = array(); |
| 27 |
|
| 28 |
foreach ( $section->fields as $field ) { |
| 29 |
$fields[] = $this->field( $tab, $field ); |
| 30 |
} |
| 31 |
|
| 32 |
$sections[] = array( |
| 33 |
'id' => $section->id, |
| 34 |
'label' => $section->label, |
| 35 |
'description' => $this->section_description( $section ), |
| 36 |
'fields' => $fields, |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
$tabs[] = array( |
| 41 |
'slug' => $tab->slug, |
| 42 |
'label' => $tab->label, |
| 43 |
'requirement' => $this->requirement_key( $tab->requirement ), |
| 44 |
'available' => $tab->requirement->satisfied(), |
| 45 |
'sections' => $sections, |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
return $tabs; |
| 50 |
} |
| 51 |
|
| 52 |
/** @return array<string, mixed> */ |
| 53 |
private function field( TabDefinition $tab, FieldDefinition $field ): array { |
| 54 |
$settings = array(); |
| 55 |
|
| 56 |
foreach ( $field->settings as $setting ) { |
| 57 |
$settings[] = array( |
| 58 |
'name' => $setting->option_name, |
| 59 |
'value' => $setting->sensitive ? '' : $this->value( $setting ), |
| 60 |
'hasValue' => $setting->sensitive && $this->secrets->has( $setting->option_name ), |
| 61 |
'publicToken' => 'general' === $tab->slug && null !== $setting->public_token ? '{{blocks:' . $setting->public_token . '}}' : null, |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
$primary = $settings[0]; |
| 66 |
$requirement = Requirement::Always === $field->requirement ? $tab->requirement : $field->requirement; |
| 67 |
|
| 68 |
return array( |
| 69 |
'id' => $field->id, |
| 70 |
'label' => $field->label, |
| 71 |
'description' => $field->description, |
| 72 |
'type' => $field->type->value, |
| 73 |
'renderArgs' => $field->render_args, |
| 74 |
'requirement' => $this->requirement_key( $requirement ), |
| 75 |
'available' => $tab->requirement->satisfied() && $field->requirement->satisfied(), |
| 76 |
'docUrl' => $field->doc_url, |
| 77 |
'docLabel' => $field->doc_label, |
| 78 |
'settings' => $settings, |
| 79 |
'imageUrl' => FieldType::Image === $field->type ? $this->attachment_url( $primary['value'] ) : '', |
| 80 |
'productLabel' => FieldType::Product === $field->type ? $this->product_label( $primary['value'] ) : '', |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
private function value( SettingDefinition $setting ): bool|int|string { |
| 85 |
return match ( $setting->wp_type ) { |
| 86 |
'boolean' => $this->settings->get_bool( $setting->option_name, (bool) $setting->default_value ), |
| 87 |
'integer' => $this->settings->get_int( $setting->option_name, (int) $setting->default_value ), |
| 88 |
default => $this->settings->get_string( $setting->option_name, (string) $setting->default_value ), |
| 89 |
}; |
| 90 |
} |
| 91 |
|
| 92 |
/** @return list<string> */ |
| 93 |
private function section_description( SectionDefinition $section ): array { |
| 94 |
if ( $section->honeypot_description ) { |
| 95 |
return array( |
| 96 |
\__( 'When enabled, Blocks automatically injects the hidden honeypot and optional timing fields into every Contact Form 7 form. No form shortcode is required.', 'blocks' ), |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
return $section->description_paragraphs; |
| 101 |
} |
| 102 |
|
| 103 |
private function requirement_key( Requirement $requirement ): string { |
| 104 |
return match ( $requirement ) { |
| 105 |
Requirement::Always => 'always', |
| 106 |
Requirement::WooCommerce => 'woo', |
| 107 |
Requirement::WooCommerceSubscriptions => 'wcs', |
| 108 |
}; |
| 109 |
} |
| 110 |
|
| 111 |
private function attachment_url( mixed $value ): string { |
| 112 |
if ( ! \is_int( $value ) || 0 >= $value ) { |
| 113 |
return ''; |
| 114 |
} |
| 115 |
|
| 116 |
$url = \wp_get_attachment_image_url( $value, 'medium' ); |
| 117 |
|
| 118 |
return \is_string( $url ) ? $url : ''; |
| 119 |
} |
| 120 |
|
| 121 |
private function product_label( mixed $value ): string { |
| 122 |
if ( ! \is_int( $value ) || 0 >= $value || ! \function_exists( 'wc_get_product' ) ) { |
| 123 |
return ''; |
| 124 |
} |
| 125 |
|
| 126 |
$product = \wc_get_product( $value ); |
| 127 |
if ( ! $product instanceof \WC_Product ) { |
| 128 |
return ''; |
| 129 |
} |
| 130 |
|
| 131 |
return \wp_strip_all_tags( $product->get_formatted_name() ); |
| 132 |
} |
| 133 |
} |
| 134 |
|