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