| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
class FrmStyleComponent { |
| 6 |
|
| 7 |
/** |
| 8 |
* The CSS and JS scripts slug. |
| 9 |
* |
| 10 |
* @since 6.14 |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
const ASSETS_SLUG = 'formidable-style-components'; |
| 15 |
|
| 16 |
/** |
| 17 |
* The folder name where views files are located. |
| 18 |
* |
| 19 |
* @since 6.14 |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $view_folder; |
| 24 |
|
| 25 |
/** |
| 26 |
* The view file name. |
| 27 |
* |
| 28 |
* @since 6.14 |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $view_name; |
| 33 |
|
| 34 |
/** |
| 35 |
* The FrmStyleComponent data. |
| 36 |
* |
| 37 |
* @since 6.14 |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
protected $data; |
| 42 |
|
| 43 |
/** |
| 44 |
* The name of the input field that will handle the form value |
| 45 |
* |
| 46 |
* @since 6.14 |
| 47 |
* |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
protected $field_name; |
| 51 |
|
| 52 |
/** |
| 53 |
* The field value |
| 54 |
* |
| 55 |
* @since 6.14 |
| 56 |
* |
| 57 |
* @var string |
| 58 |
*/ |
| 59 |
protected $field_value; |
| 60 |
|
| 61 |
/** |
| 62 |
* The FrmStyleComponent instance. |
| 63 |
* |
| 64 |
* @since 6.14 |
| 65 |
* |
| 66 |
* @var FrmStyleComponent|null |
| 67 |
*/ |
| 68 |
private static $instance; |
| 69 |
|
| 70 |
private function __construct() { |
| 71 |
$this->load_css(); |
| 72 |
$this->load_js(); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Register CSS & JS style components assets. |
| 77 |
* |
| 78 |
* @since 6.14 |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public static function register_assets() { |
| 83 |
$version = FrmAppHelper::plugin_version(); |
| 84 |
wp_register_script( self::ASSETS_SLUG, FrmAppHelper::plugin_url() . '/js/formidable_styles.js', array( 'formidable_admin' ), $version, true ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get the singleton instance. |
| 89 |
* |
| 90 |
* @since 6.14 |
| 91 |
* |
| 92 |
* @return FrmStyleComponent|null |
| 93 |
*/ |
| 94 |
protected static function get_instance() { |
| 95 |
if ( self::$instance ) { |
| 96 |
return null; |
| 97 |
} |
| 98 |
|
| 99 |
self::$instance = new FrmStyleComponent(); |
| 100 |
return self::$instance; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @param array $data |
| 105 |
* @param string $field_name |
| 106 |
* @param mixed $field_value |
| 107 |
* |
| 108 |
* @return void |
| 109 |
*/ |
| 110 |
protected function init( $data, $field_name, $field_value ) { |
| 111 |
$this->init_field_data( $data, $field_name, $field_value ); |
| 112 |
self::get_instance(); |
| 113 |
$this->load_view(); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Init the field component data, field name and field value. |
| 118 |
* |
| 119 |
* @since 6.14 |
| 120 |
* |
| 121 |
* @param array $data The field extra options. |
| 122 |
* @param string $field_name The field input's name. |
| 123 |
* @param mixed $field_value The field value. |
| 124 |
* |
| 125 |
* @return void |
| 126 |
*/ |
| 127 |
protected function init_field_data( $data, $field_name, $field_value ) { |
| 128 |
$this->data = $data; |
| 129 |
$this->field_name = $field_name; |
| 130 |
$this->field_value = $field_value; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get the default wrapper classnames. |
| 135 |
* |
| 136 |
* @since 6.14 |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
protected function get_default_wrapper_class_names() { |
| 141 |
$class = 'frm-style-component'; |
| 142 |
|
| 143 |
if ( ! empty( $this->data['classname'] ) ) { |
| 144 |
$class .= ' ' . $this->data['classname']; |
| 145 |
} |
| 146 |
|
| 147 |
if ( ! empty( $this->data['will_change'] ) ) { |
| 148 |
return $class . ' frm-style-dependent-updater-component'; |
| 149 |
} |
| 150 |
|
| 151 |
return $class; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get the wrapper classnames. |
| 156 |
* |
| 157 |
* @since 6.14 |
| 158 |
* |
| 159 |
* @return string |
| 160 |
*/ |
| 161 |
protected function get_wrapper_class_name() { |
| 162 |
return $this->get_default_wrapper_class_names(); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get the wrapper's attributes. |
| 167 |
* |
| 168 |
* @since 6.14 |
| 169 |
* |
| 170 |
* @return string |
| 171 |
*/ |
| 172 |
private function get_component_attributes() { |
| 173 |
$attributes = ''; |
| 174 |
|
| 175 |
if ( ! empty( $this->data['will_change'] ) ) { |
| 176 |
$attributes .= 'data-will-change=' . wp_json_encode( $this->data['will_change'] ); |
| 177 |
} |
| 178 |
|
| 179 |
return $attributes; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Get the input's name that will handle the form value. |
| 184 |
* It can have no name attribute for cases when the component is a dependency updater. It will update multiple fields from the same form. It will require "will-change" attribute to be set |
| 185 |
* |
| 186 |
* @since 6.14 |
| 187 |
* |
| 188 |
* @return string |
| 189 |
*/ |
| 190 |
private function get_field_name() { |
| 191 |
return $this->field_name ? 'name=' . $this->field_name : ''; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Init the full view path. |
| 196 |
* |
| 197 |
* @since 6.14 |
| 198 |
* |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
private function set_view_path() { |
| 202 |
$this->view_folder = FrmAppHelper::plugin_path() . '/classes/views/styles/components/templates/'; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Load the view file. |
| 207 |
* |
| 208 |
* @since 6.14 |
| 209 |
* |
| 210 |
* @return void |
| 211 |
*/ |
| 212 |
protected function load_view() { |
| 213 |
if ( ! $this->view_name ) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
$this->set_view_path(); |
| 218 |
$component_attr = $this->get_component_attributes(); |
| 219 |
$component_class = $this->get_wrapper_class_name(); |
| 220 |
$component = $this->data; |
| 221 |
$field_name = $this->get_field_name(); |
| 222 |
$field_value = $this->field_value; |
| 223 |
|
| 224 |
include $this->view_folder . $this->view_name . '.php'; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* @return bool |
| 229 |
*/ |
| 230 |
protected function hide_component() { |
| 231 |
if ( empty( $this->data['not_show_in'] ) ) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
return FrmAppHelper::get_param( 'section', '', 'get', 'sanitize_text_field' ) === $this->data['not_show_in']; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Load component CSS file. |
| 239 |
* |
| 240 |
* @since 6.14 |
| 241 |
* |
| 242 |
* @return void |
| 243 |
*/ |
| 244 |
private function load_css() { |
| 245 |
wp_enqueue_style( self::ASSETS_SLUG ); |
| 246 |
wp_enqueue_style( 'formidable-settings-components' ); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Load component JS file. |
| 251 |
* |
| 252 |
* @since 6.14 |
| 253 |
* |
| 254 |
* @return void |
| 255 |
*/ |
| 256 |
private function load_js() { |
| 257 |
wp_enqueue_script( self::ASSETS_SLUG ); |
| 258 |
wp_enqueue_script( 'formidable-settings-components' ); |
| 259 |
} |
| 260 |
} |
| 261 |
|