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

247 lines 4.8 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['will_change'] ) ) {
137 return $class . ' frm-style-dependent-updater-component';
138 }
139 return $class;
140 }
141
142 /**
143 * Get the wrapper classnames.
144 *
145 * @since 6.14
146 *
147 * @return string
148 */
149 protected function get_wrapper_class_name() {
150 return $this->get_default_wrapper_class_names();
151 }
152
153 /**
154 * Get the wrapper's attributes.
155 *
156 * @since 6.14
157 *
158 * @return string
159 */
160 private function get_component_attributes() {
161 $attributes = '';
162 if ( ! empty( $this->data['will_change'] ) ) {
163 $attributes .= 'data-will-change=' . wp_json_encode( $this->data['will_change'] );
164 }
165 return $attributes;
166 }
167
168 /**
169 * Get the input's name that will handle the form value.
170 * 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
171 *
172 * @since 6.14
173 *
174 * @return string
175 */
176 private function get_field_name() {
177 return ! empty( $this->field_name ) ? 'name=' . $this->field_name : '';
178 }
179
180 /**
181 * Init the full view path.
182 *
183 * @since 6.14
184 *
185 * @return void
186 */
187 private function set_view_path() {
188 $this->view_folder = FrmAppHelper::plugin_path() . '/classes/views/styles/components/templates/';
189 }
190
191 /**
192 * Load the view file.
193 *
194 * @since 6.14
195 *
196 * @return void
197 */
198 protected function load_view() {
199 if ( empty( $this->view_name ) ) {
200 return;
201 }
202
203 $this->set_view_path();
204 $component_attr = $this->get_component_attributes();
205 $component_class = $this->get_wrapper_class_name();
206 $component = $this->data;
207 $field_name = $this->get_field_name();
208 $field_value = $this->field_value;
209
210 include $this->view_folder . $this->view_name . '.php';
211 }
212
213 protected function hide_component() {
214 if ( empty( $this->data['not_show_in'] ) ) {
215 return false;
216 }
217
218 if ( FrmAppHelper::get_param( 'section', '', 'get', 'sanitize_text_field' ) === $this->data['not_show_in'] ) {
219 return true;
220 }
221
222 return false;
223 }
224
225 /**
226 * Load component CSS file.
227 *
228 * @since 6.14
229 *
230 * @return void
231 */
232 private function load_css() {
233 wp_enqueue_style( self::ASSETS_SLUG );
234 }
235
236 /**
237 * Load component JS file.
238 *
239 * @since 6.14
240 *
241 * @return void
242 */
243 private function load_js() {
244 wp_enqueue_script( self::ASSETS_SLUG );
245 }
246 }
247