PluginProbe
Constant Contact Forms / trunk
Constant Contact Forms vtrunk
2.21.0 2.20.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.10.0 1.10.1 1.11.0 1.12.0 1.13.0 1.14.0 1.14.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 All 91 releases
constant-contact-forms / includes / class-display-shortcode.php

class-display-shortcode.php in Constant Contact Forms trunk, at includes/class-display-shortcode.php

315 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles the output of the shortcode.
4 *
5 * @package ConstantContact
6 *
7 * @subpackage DisplayShortcode
8 * @author Constant Contact
9 * @since 1.0.0
10 *
11 * phpcs:disable WebDevStudios.All.RequireAuthor -- Don't require author tag in docblocks.
12 */
13
14 /**
15 * Helper class that gets called to display our stuff on the front-end via a shortcode.
16 *
17 * @since 1.0.0
18 */
19 class ConstantContact_Display_Shortcode {
20
21 /**
22 * Parent plugin class.
23 *
24 * @since 1.0.0
25 *
26 * @var object
27 */
28 protected $plugin;
29
30 /**
31 * Track form instances on page.
32 *
33 * @since 1.0.0
34 *
35 * @var array
36 */
37 protected static $form_instance = 0;
38
39 /**
40 * Constructor.
41 *
42 * @since 1.0.0
43 *
44 * @param object $plugin Parent plugin class.
45 */
46 public function __construct( $plugin ) {
47 $this->plugin = $plugin;
48 $this->hooks();
49 }
50
51 /**
52 * Hooks.
53 *
54 * @since 1.0.0
55 */
56 public function hooks() {
57 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_display_styles' ] );
58 }
59
60 /**
61 * Renders [ctct] shortcodes.
62 *
63 * @since 1.0.0
64 *
65 * @param array $atts Shortcode attributes.
66 * @return string
67 */
68 public function render_shortcode( $atts ) {
69
70 $atts = shortcode_atts(
71 $this->plugin->get_shortcode()->get_atts(),
72 $atts,
73 $this->plugin->get_shortcode()->tag
74 );
75
76 if ( ! isset( $atts['form'] ) ) {
77 return '';
78 }
79
80 $show_title = isset( $atts['show_title'] ) && 'true' === $atts['show_title'];
81
82 return $this->get_form( absint( $atts['form'] ), $show_title );
83 }
84
85 /**
86 * Get a form from ID.
87 *
88 * @since 1.0.0
89 *
90 * @param int $form_id Form ID.
91 * @param bool $show_title If true, show the form title.
92 * @return string
93 */
94 public function get_form( int $form_id, bool $show_title = false ) {
95
96 if ( ! $form_id ) {
97 return '';
98 }
99
100 $meta = get_post_meta( $form_id );
101
102 if ( ! $meta ) {
103 return '';
104 }
105
106 $form_data = $this->get_field_meta( $meta, $form_id );
107 $classes = [ 'ctct-form-wrapper' ];
108 $custom = $this->wrapper_classes();
109 $all_classes = array_merge( $classes, $custom );
110 $form = sprintf(
111 '<div data-form-id="%1$s" id="ctct-form-wrapper-%2$s" class="%3$s">%4$s</div>',
112 esc_attr( $form_id ),
113 esc_attr( self::$form_instance ),
114 esc_attr( implode( ' ', $all_classes ) ),
115 constant_contact()->get_display()->form( $form_data, $form_id, $show_title, self::$form_instance )
116 );
117
118 ++self::$form_instance;
119
120 return $form;
121 }
122
123 /**
124 * Display a form to the screen.
125 *
126 * @since 1.0.0
127 *
128 * @param int $form_id Form ID to display.
129 * @param bool $show_title If true, show the title.
130 */
131 public function display_form( int $form_id, bool $show_title = false ) {
132 echo $this->get_form( $form_id, $show_title ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- XSS OK.
133 }
134
135 /**
136 * Proccess cmb2 options into form data array.
137 *
138 * @since 1.0.0
139 *
140 * @param array $form_meta Post meta.
141 * @param int $form_id Form ID.
142 * @return array|string Form field data.
143 */
144 public function get_field_meta( array $form_meta, int $form_id ) {
145
146 if ( empty( $form_meta ) ) {
147 return '';
148 }
149
150 if (
151 isset( $form_meta['custom_fields_group'][0] ) && $form_meta['custom_fields_group']
152 ) {
153 return $this->get_field_values( $form_meta['custom_fields_group'][0], $form_meta, $form_id );
154 }
155
156 return '';
157 }
158
159 /**
160 * Get custom field values from post meta data from form CPT post.
161 *
162 * @since 1.0.0
163 *
164 * @param string $custom_fields Custom fields to parse through.
165 * @param array $full_data Array of full data.
166 * @param int $form_id Form ID.
167 * @return array Form field markup.
168 */
169 public function get_field_values( $custom_fields, $full_data, $form_id ) {
170
171 $fields = $this->generate_field_values_for_fields( maybe_unserialize( $custom_fields ) );
172
173 if ( $form_id ) {
174 $fields['options']['form_id'] = $form_id;
175 }
176
177 $fields['options']['description'] = $this->get_nested_value_from_data( '_ctct_description', $full_data );
178
179 $fields['options']['description_visibility'] = $this->get_nested_value_from_data( '_ctct_description_visibility', $full_data );
180
181 $fields['options']['optin'] = $this->generate_optin_data( $full_data );
182
183 return $fields;
184 }
185
186 /**
187 * Get all our data from our fields.
188 *
189 * @since 1.0.0
190 *
191 * @param array $custom_fields All custom fields data.
192 * @return array Fields array of converted data.
193 */
194 public function generate_field_values_for_fields( $custom_fields ) {
195
196 $fields = [];
197
198 if ( ! is_array( $custom_fields ) ) {
199 return $fields;
200 }
201
202 foreach ( $custom_fields as $key => $value ) {
203 if ( ! isset( $custom_fields[ $key ] ) ) {
204 continue;
205 }
206
207 $fields = $this->set_field( '_ctct_field_label', 'name', $key, $fields, $custom_fields );
208
209 $fields = $this->set_field( '_ctct_map_select', 'map_to', $key, $fields, $custom_fields );
210 $fields = $this->set_field( '_ctct_map_select', 'type', $key, $fields, $custom_fields );
211
212 $fields = $this->set_field( '_ctct_field_desc', 'description', $key, $fields, $custom_fields );
213
214 $fields['fields'][ $key ]['required'] = (
215 isset( $custom_fields[ $key ]['_ctct_required_field'] ) &&
216 'on' === $custom_fields[ $key ]['_ctct_required_field']
217 );
218 }
219
220 return $fields;
221 }
222
223 /**
224 * Helper method to set our $fields array keys.
225 *
226 * @since 1.0.0
227 *
228 * @param string $from_key Key to grab from $custom_fields.
229 * @param string $to_key Key to use for return $fields.
230 * @param string $key Field key.
231 * @param array $fields Current $fields array.
232 * @param array $custom_fields All $custom_fields.
233 * @return array
234 */
235 public function set_field( $from_key, $to_key, $key, $fields, $custom_fields ) {
236
237 if (
238 is_array( $custom_fields ) &&
239 isset( $custom_fields[ $key ] ) &&
240 $custom_fields[ $key ] &&
241 isset( $custom_fields[ $key ][ $from_key ] ) &&
242 $custom_fields[ $key ][ $from_key ]
243 ) {
244 $fields['fields'][ $key ][ $to_key ] = $custom_fields[ $key ][ $from_key ];
245 }
246
247 return $fields;
248 }
249
250 /**
251 * Helper method to get our optin data.
252 *
253 * @since 1.0.0
254 *
255 * @param array $form_data Form data array.
256 * @return array Array of opt-in data.
257 */
258 public function generate_optin_data( $form_data ) {
259 return [
260 'list' => $this->get_nested_value_from_data( '_ctct_list', $form_data ),
261 'show' => $this->get_nested_value_from_data( '_ctct_opt_in', $form_data ),
262 'instructions' => $this->get_nested_value_from_data( '_ctct_opt_in_instructions', $form_data ),
263 ];
264 }
265
266 /**
267 * Helper method to get opt in instructions or other text from form data.
268 *
269 * @since 1.0.0
270 *
271 * @param string $key Field key.
272 * @param array $form_data Form data.
273 * @return string Instructions.
274 */
275 public function get_nested_value_from_data( $key, $form_data ) {
276 if (
277 isset( $form_data[ $key ] ) &&
278 $form_data[ $key ] &&
279 isset( $form_data[ $key ][0] ) &&
280 $form_data[ $key ][0]
281 ) {
282 return $form_data[ $key ][0];
283 }
284
285 return '';
286 }
287
288 /**
289 * Call the method to enqueue styles for display.
290 *
291 * @since 1.0.0
292 */
293 public function enqueue_display_styles() {
294 constant_contact()->get_display()->styles( true );
295 }
296
297 /**
298 * Helper method for custom wrapper classes for popular theme compatibility.
299 *
300 * @since 2.13.0
301 *
302 * @return array
303 */
304 private function wrapper_classes() {
305 $classes = [];
306
307 // Divi
308 if ( defined( 'ET_CORE_VERSION' ) ) {
309 $classes[] = 'et_pb_contact';
310 }
311
312 return $classes;
313 }
314 }
315