> */ public function build( SettingsRegistry $registry ): array { $tabs = array(); foreach ( $registry->tabs() as $tab ) { $sections = array(); foreach ( $tab->sections as $section ) { $fields = array(); foreach ( $section->fields as $field ) { $fields[] = $this->field( $tab, $field ); } $sections[] = array( 'id' => $section->id, 'label' => $section->label, 'description' => $this->section_description( $section ), 'fields' => $fields, ); } $tabs[] = array( 'slug' => $tab->slug, 'label' => $tab->label, 'requirement' => $this->requirement_key( $tab->requirement ), 'available' => $tab->requirement->satisfied(), 'sections' => $sections, ); } return $tabs; } /** @return array */ private function field( TabDefinition $tab, FieldDefinition $field ): array { $settings = array(); foreach ( $field->settings as $setting ) { $settings[] = array( 'name' => $setting->option_name, 'value' => $setting->sensitive ? '' : $this->value( $setting ), 'hasValue' => $setting->sensitive && $this->secrets->has( $setting->option_name ), 'publicToken' => 'general' === $tab->slug && null !== $setting->public_token ? '{{blocks:' . $setting->public_token . '}}' : null, ); } $primary = $settings[0]; $requirement = Requirement::Always === $field->requirement ? $tab->requirement : $field->requirement; return array( 'id' => $field->id, 'label' => $field->label, 'description' => $field->description, 'type' => $field->type->value, 'renderArgs' => $field->render_args, 'requirement' => $this->requirement_key( $requirement ), 'available' => $tab->requirement->satisfied() && $field->requirement->satisfied(), 'docUrl' => $field->doc_url, 'docLabel' => $field->doc_label, 'settings' => $settings, 'imageUrl' => FieldType::Image === $field->type ? $this->attachment_url( $primary['value'] ) : '', 'productLabel' => FieldType::Product === $field->type ? $this->product_label( $primary['value'] ) : '', ); } private function value( SettingDefinition $setting ): bool|int|string { return match ( $setting->wp_type ) { 'boolean' => $this->settings->get_bool( $setting->option_name, (bool) $setting->default_value ), 'integer' => $this->settings->get_int( $setting->option_name, (int) $setting->default_value ), default => $this->settings->get_string( $setting->option_name, (string) $setting->default_value ), }; } /** @return list */ private function section_description( SectionDefinition $section ): array { if ( $section->honeypot_description ) { return array( \__( 'When enabled, Blocks automatically injects the hidden honeypot and optional timing fields into every Contact Form 7 form. No form shortcode is required.', 'blocks' ), ); } return $section->description_paragraphs; } private function requirement_key( Requirement $requirement ): string { return match ( $requirement ) { Requirement::Always => 'always', Requirement::WooCommerce => 'woo', Requirement::WooCommerceSubscriptions => 'wcs', }; } private function attachment_url( mixed $value ): string { if ( ! \is_int( $value ) || 0 >= $value ) { return ''; } $url = \wp_get_attachment_image_url( $value, 'medium' ); return \is_string( $url ) ? $url : ''; } private function product_label( mixed $value ): string { if ( ! \is_int( $value ) || 0 >= $value || ! \function_exists( 'wc_get_product' ) ) { return ''; } $product = \wc_get_product( $value ); if ( ! $product instanceof \WC_Product ) { return ''; } return \wp_strip_all_tags( $product->get_formatted_name() ); } }