PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.19
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.19
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.19, at classes/views/styles/components/FrmStyleComponent.php

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