PluginProbe
Flexible Subscriptions / trunk
Flexible Subscriptions vtrunk
1.8.5 1.8.4 1.8.3 1.8.2 1.8.1 1.8.0 1.7.19 1.7.18 1.7.17 1.7.16 1.7.15 1.7.14 1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 All 62 releases
flexible-subscriptions / src / Admin / SettingsForm.php

SettingsForm.php in Flexible Subscriptions trunk, at src/Admin/SettingsForm.php

119 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3
4 namespace WPDesk\FlexibleSubscriptions\Admin;
5
6 use WPDesk\FlexibleSubscriptions\Admin\Settings\AccountTab;
7 use WPDesk\FlexibleSubscriptions\Admin\Settings\PaymentsTab;
8 use WPDesk\FlexibleSubscriptions\Admin\Settings\ProductsTab;
9 use WPDesk\FlexibleSubscriptions\Form\ExtendedForm;
10 use WPDesk\FlexibleSubscriptions\Form\Fields\ActionField;
11 use WPDesk\FlexibleSubscriptions\Form\Fields\NoOnceField;
12 use WPDesk\FlexibleSubscriptions\Form\Fields\Section;
13 use WPDesk\FlexibleSubscriptions\Form\Fields\Tab;
14 use WPDesk\FlexibleSubscriptions\Vendor\WPDesk\Forms\Field\InputTextField;
15 use WPDesk\FlexibleSubscriptions\Vendor\WPDesk\Forms\Field\SubmitField;
16 use WPDesk\FlexibleSubscriptions\Vendor\WPDesk\View\Renderer\Renderer;
17
18 /**
19 * This is inspired by wp-forms, but changes the internals for better feeling.
20 */
21 class SettingsForm extends ExtendedForm {
22
23 private string $active_tab = 'account';
24
25 public function __construct( string $active_tab = 'account' ) {
26 $this->active_tab = $active_tab;
27 parent::__construct( $this->fields(), 'fsb-settings' );
28 $this->set_action( add_query_arg( [ 'tab' => $this->active_tab ], admin_url( 'admin-post.php' ) ) );
29 }
30
31 private function fields(): array {
32 $tabs = apply_filters(
33 'fsub/settings/tabs',
34 [
35 ( new ProductsTab() )
36 ->set_name( 'product' )
37 ->set_label( esc_html__( 'Products', 'flexible-subscriptions' ) ),
38 ( new PaymentsTab() )
39 ->set_name( 'payment' )
40 ->set_label( esc_html__( 'Payments', 'flexible-subscriptions' ) ),
41 ( new AccountTab() )
42 ->set_name( 'account' )
43 ->set_label( esc_html__( 'Account', 'flexible-subscriptions' ) ),
44 ]
45 );
46
47 return [
48 ( new NoOnceField( 'fsb_save_settings' ) )->set_name( '_wpnonce' ),
49 ( new ActionField() )
50 ->set_name( 'action' )
51 ->set_default_value( 'fsb_save_settings' ),
52 ...$tabs,
53 ( new SubmitField() )
54 ->set_label( esc_html__( 'Save changes', 'flexible-subscriptions' ) )
55 ->add_class( 'button-primary' )
56 ->set_name( 'save' ),
57 ];
58 }
59
60 public function tabs(): array {
61 return array_filter(
62 $this->get_fields(),
63 static fn ( $field ) => $field instanceof Tab
64 );
65 }
66
67 public function set_active_tab( string $tab ): void {
68 $this->active_tab = $tab;
69 }
70
71 #[\Override]
72 public function render_fields( Renderer $renderer ): string {
73 $content = '';
74 $fields_data = $this->get_data();
75 foreach ( $this->get_fields() as $field ) {
76 if ( $field instanceof Tab && $this->active_tab !== $field->get_name() ) {
77 continue;
78 }
79 $content .= $renderer->render(
80 $field->should_override_form_template() ? $field->get_template_name() : 'form-field',
81 [
82 'field' => $field,
83 'renderer' => $renderer,
84 'name_prefix' => $this->get_form_id(),
85 'value' => $this->get_field_value( $field, $fields_data ),
86 'template_name' => $field->get_template_name(),
87 ]
88 );
89 }
90
91 return $content;
92 }
93
94 public function get_current_tab_data(): array {
95 foreach ( $this->get_fields() as $field ) {
96 if ( $field instanceof Tab && $this->active_tab === $field->get_name() ) {
97 return $this->get_data_by_field( $field, $this->updated_data );
98 }
99 }
100
101 return [];
102 }
103
104 #[\Override]
105 public function render_form( Renderer $renderer ): string {
106 $content = $renderer->render(
107 'form-start',
108 [
109 'form' => $this,
110 'active_tab' => $this->active_tab,
111 ]
112 );
113 $content .= $this->render_fields( $renderer );
114 $content .= $renderer->render( 'form-end' );
115
116 return $content;
117 }
118 }
119