PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.28
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.28
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / views / styles / components / FrmStyleComponent.php

FrmStyleComponent.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.28, at classes/views/styles/components/FrmStyleComponent.php

263 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $plugin_url = FrmAppHelper::plugin_url();
84 $version = FrmAppHelper::plugin_version();
85
86 wp_register_script( self::ASSETS_SLUG, $plugin_url . '/js/formidable_styles.js', array( 'formidable_admin' ), $version, true );
87 }
88
89 /**
90 * Get the singleton instance.
91 *
92 * @since 6.14
93 *
94 * @return FrmStyleComponent|null
95 */
96 protected static function get_instance() {
97 if ( self::$instance ) {
98 return null;
99 }
100
101 self::$instance = new FrmStyleComponent();
102 return self::$instance;
103 }
104
105 /**
106 * @param array $data
107 * @param string $field_name
108 * @param mixed $field_value
109 *
110 * @return void
111 */
112 protected function init( $data, $field_name, $field_value ) {
113 $this->init_field_data( $data, $field_name, $field_value );
114 self::get_instance();
115 $this->load_view();
116 }
117
118 /**
119 * Init the field component data, field name and field value.
120 *
121 * @since 6.14
122 *
123 * @param array $data The field extra options.
124 * @param string $field_name The field input's name.
125 * @param mixed $field_value The field value.
126 *
127 * @return void
128 */
129 protected function init_field_data( $data, $field_name, $field_value ) {
130 $this->data = $data;
131 $this->field_name = $field_name;
132 $this->field_value = $field_value;
133 }
134
135 /**
136 * Get the default wrapper classnames.
137 *
138 * @since 6.14
139 *
140 * @return string
141 */
142 protected function get_default_wrapper_class_names() {
143 $class = 'frm-style-component';
144
145 if ( ! empty( $this->data['classname'] ) ) {
146 $class .= ' ' . $this->data['classname'];
147 }
148
149 if ( ! empty( $this->data['will_change'] ) ) {
150 return $class . ' frm-style-dependent-updater-component';
151 }
152
153 return $class;
154 }
155
156 /**
157 * Get the wrapper classnames.
158 *
159 * @since 6.14
160 *
161 * @return string
162 */
163 protected function get_wrapper_class_name() {
164 return $this->get_default_wrapper_class_names();
165 }
166
167 /**
168 * Get the wrapper's attributes.
169 *
170 * @since 6.14
171 *
172 * @return string
173 */
174 private function get_component_attributes() {
175 $attributes = '';
176
177 if ( ! empty( $this->data['will_change'] ) ) {
178 $attributes .= 'data-will-change=' . wp_json_encode( $this->data['will_change'] );
179 }
180
181 return $attributes;
182 }
183
184 /**
185 * Get the input's name that will handle the form value.
186 * 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
187 *
188 * @since 6.14
189 *
190 * @return string
191 */
192 private function get_field_name() {
193 return ! empty( $this->field_name ) ? 'name=' . $this->field_name : '';
194 }
195
196 /**
197 * Init the full view path.
198 *
199 * @since 6.14
200 *
201 * @return void
202 */
203 private function set_view_path() {
204 $this->view_folder = FrmAppHelper::plugin_path() . '/classes/views/styles/components/templates/';
205 }
206
207 /**
208 * Load the view file.
209 *
210 * @since 6.14
211 *
212 * @return void
213 */
214 protected function load_view() {
215 if ( empty( $this->view_name ) ) {
216 return;
217 }
218
219 $this->set_view_path();
220 $component_attr = $this->get_component_attributes();
221 $component_class = $this->get_wrapper_class_name();
222 $component = $this->data;
223 $field_name = $this->get_field_name();
224 $field_value = $this->field_value;
225
226 include $this->view_folder . $this->view_name . '.php';
227 }
228
229 /**
230 * @return bool
231 */
232 protected function hide_component() {
233 if ( empty( $this->data['not_show_in'] ) ) {
234 return false;
235 }
236 return FrmAppHelper::get_param( 'section', '', 'get', 'sanitize_text_field' ) === $this->data['not_show_in'];
237 }
238
239 /**
240 * Load component CSS file.
241 *
242 * @since 6.14
243 *
244 * @return void
245 */
246 private function load_css() {
247 wp_enqueue_style( self::ASSETS_SLUG );
248 wp_enqueue_style( 'formidable-settings-components' );
249 }
250
251 /**
252 * Load component JS file.
253 *
254 * @since 6.14
255 *
256 * @return void
257 */
258 private function load_js() {
259 wp_enqueue_script( self::ASSETS_SLUG );
260 wp_enqueue_script( 'formidable-settings-components' );
261 }
262 }
263