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