base-vui-button.php
197 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Admin\Buttons; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Arrayable\Arrayable; |
| 7 | use Jet_Form_Builder\Generators\Base; |
| 8 | use Jet_Form_Builder\Rest_Api\Rest_Api_Endpoint_Base; |
| 9 | |
| 10 | class Base_Vui_Button implements Arrayable { |
| 11 | |
| 12 | const STYLE_ACCENT = 'accent'; |
| 13 | const STYLE_ACCENT_ERROR = 'accent-error'; |
| 14 | const STYLE_ACCENT_ERROR_BORDER = 'accent-error-border'; |
| 15 | const STYLE_DEFAULT = 'default'; |
| 16 | const STYLE_LINK_ACCENT = 'link-accent'; |
| 17 | const STYLE_LINK_ERROR = 'link-error'; |
| 18 | const STYLE_ACCENT_BORDER = 'accent-border'; |
| 19 | const STYLE_DEFAULT_BORDER = 'default-border'; |
| 20 | |
| 21 | const TYPE_BUTTON = 'button'; |
| 22 | const TYPE_SUBMIT = 'submit'; |
| 23 | const TYPE_RESET = 'reset'; |
| 24 | |
| 25 | const SIZE_DEFAULT = 'default'; |
| 26 | const SIZE_MINI = 'mini'; |
| 27 | const SIZE_MINI_X2 = 'mini-x2'; |
| 28 | const SIZE_LINK = 'link'; |
| 29 | |
| 30 | const PRESET_PAGE_ACTION = 'page_action'; |
| 31 | |
| 32 | protected $slug; |
| 33 | protected $url = ''; |
| 34 | protected $rest_url = ''; |
| 35 | protected $rest_methods = ''; |
| 36 | protected $style = self::STYLE_ACCENT; |
| 37 | protected $type = self::TYPE_BUTTON; |
| 38 | protected $size = self::SIZE_DEFAULT; |
| 39 | protected $label = ''; |
| 40 | protected $disabled = false; |
| 41 | protected $classes = array(); |
| 42 | |
| 43 | public function __construct( string $slug = 'default' ) { |
| 44 | $this->slug = $slug; |
| 45 | } |
| 46 | |
| 47 | /** Getters */ |
| 48 | |
| 49 | protected function get_presets(): array { |
| 50 | return array( |
| 51 | self::PRESET_PAGE_ACTION => array( $this, 'set_preset_page_action' ), |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | public function get_slug(): string { |
| 56 | return $this->slug; |
| 57 | } |
| 58 | |
| 59 | public function get_style(): string { |
| 60 | return $this->style; |
| 61 | } |
| 62 | |
| 63 | public function get_type(): string { |
| 64 | return $this->type; |
| 65 | } |
| 66 | |
| 67 | public function get_size(): string { |
| 68 | return $this->size; |
| 69 | } |
| 70 | |
| 71 | public function get_url(): string { |
| 72 | return $this->url; |
| 73 | } |
| 74 | |
| 75 | public function get_rest_url(): string { |
| 76 | return $this->rest_url; |
| 77 | } |
| 78 | |
| 79 | public function get_rest_methods(): string { |
| 80 | return $this->rest_methods; |
| 81 | } |
| 82 | |
| 83 | public function get_label(): string { |
| 84 | return $this->label; |
| 85 | } |
| 86 | |
| 87 | public function get_classes(): array { |
| 88 | return $this->classes; |
| 89 | } |
| 90 | |
| 91 | public function is_disabled(): bool { |
| 92 | return $this->disabled; |
| 93 | } |
| 94 | |
| 95 | /** Setters */ |
| 96 | |
| 97 | protected function set_preset_page_action() { |
| 98 | $this->add_classes( |
| 99 | array( 'unset-box-shadow' ) |
| 100 | ); |
| 101 | $this->set_size( self::SIZE_MINI_X2 ); |
| 102 | } |
| 103 | |
| 104 | public function set_preset( string $preset ): Base_Vui_Button { |
| 105 | $presets = $this->get_presets(); |
| 106 | |
| 107 | if ( ! isset( $presets[ $preset ] ) || ! is_callable( $presets[ $preset ] ) ) { |
| 108 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 109 | wp_die( 'Undefined style preset in ' . static::class ); |
| 110 | } |
| 111 | call_user_func( $presets[ $preset ] ); |
| 112 | |
| 113 | return $this; |
| 114 | } |
| 115 | |
| 116 | public function set_style( string $style ): Base_Vui_Button { |
| 117 | $this->style = $style; |
| 118 | |
| 119 | return $this; |
| 120 | } |
| 121 | |
| 122 | public function set_type( string $type ): Base_Vui_Button { |
| 123 | $this->type = $type; |
| 124 | |
| 125 | return $this; |
| 126 | } |
| 127 | |
| 128 | public function set_size( string $size ): Base_Vui_Button { |
| 129 | $this->size = $size; |
| 130 | |
| 131 | return $this; |
| 132 | } |
| 133 | |
| 134 | public function set_rest( Rest_Api_Endpoint_Base $endpoint_base ): Base_Vui_Button { |
| 135 | return $this->set_rest_url( $endpoint_base::rest_url() ) |
| 136 | ->set_rest_methods( $endpoint_base::get_methods() ); |
| 137 | } |
| 138 | |
| 139 | public function set_rest_url( string $url ): Base_Vui_Button { |
| 140 | $this->rest_url = $url; |
| 141 | |
| 142 | return $this->set_url( 'javascript:void(0)' ); |
| 143 | } |
| 144 | |
| 145 | public function set_rest_methods( string $rest_methods ): Base_Vui_Button { |
| 146 | $this->rest_methods = $rest_methods; |
| 147 | |
| 148 | return $this; |
| 149 | } |
| 150 | |
| 151 | public function set_url( string $url ): Base_Vui_Button { |
| 152 | $this->url = $url; |
| 153 | |
| 154 | return $this; |
| 155 | } |
| 156 | |
| 157 | public function set_label( string $label ): Base_Vui_Button { |
| 158 | $this->label = $label; |
| 159 | |
| 160 | return $this; |
| 161 | } |
| 162 | |
| 163 | public function add_classes( array $classes ): Base_Vui_Button { |
| 164 | $this->classes += $classes; |
| 165 | |
| 166 | return $this; |
| 167 | } |
| 168 | |
| 169 | public function set_disabled( bool $disabled ): Base_Vui_Button { |
| 170 | $this->disabled = $disabled; |
| 171 | |
| 172 | return $this; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the instance as an array. |
| 177 | * |
| 178 | * @return array |
| 179 | */ |
| 180 | public function to_array(): array { |
| 181 | return array( |
| 182 | 'slug' => $this->get_slug(), |
| 183 | 'size' => $this->get_size(), |
| 184 | 'style' => $this->get_style(), |
| 185 | 'type' => $this->get_type(), |
| 186 | 'url' => $this->get_url(), |
| 187 | 'rest' => array( |
| 188 | 'url' => $this->get_rest_url(), |
| 189 | 'method' => $this->get_rest_methods(), |
| 190 | ), |
| 191 | 'label' => $this->get_label(), |
| 192 | 'classes' => $this->get_classes(), |
| 193 | 'disabled' => $this->is_disabled(), |
| 194 | ); |
| 195 | } |
| 196 | } |
| 197 |