Completed
Pull Request — master (#174)
by Stephanie
02:25
created

FrmStylesHelper::jquery_themes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
class FrmStylesHelper {
4
5
	public static function get_upload_base() {
6
		$uploads = wp_upload_dir();
7
		if ( is_ssl() && ! preg_match( '/^https:\/\/.*\..*$/', $uploads['baseurl'] ) ) {
8
			$uploads['baseurl'] = str_replace( 'http://', 'https://', $uploads['baseurl'] );
9
		}
10
11
		return $uploads;
12
	}
13
14
	/**
15
	 * @since 4.0
16
	 */
17
	public static function get_style_menu( $active = '' ) {
18
		ob_start();
19
		self::style_menu( $active );
20
		$menu = ob_get_contents();
21
		ob_end_clean();
22
23
		return $menu;
24
	}
25
26
	public static function style_menu( $active = '' ) {
27
		?>
28
		<ul class="frm_form_nav">
29
			<li>
30
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles' ) ); ?>"
31
					class="<?php echo ( '' == $active ) ? 'current_page' : ''; ?>">
32
					<?php esc_html_e( 'Edit Styles', 'formidable' ); ?>
33
				</a>
34
			</li>
35
			<li>
36
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles&frm_action=manage' ) ); ?>"
37
					class="<?php echo ( 'manage' == $active ) ? 'current_page' : ''; ?>">
38
					<?php esc_html_e( 'Manage Styles', 'formidable' ); ?>
39
				</a>
40
			</li>
41
			<li>
42
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles&frm_action=custom_css' ) ); ?>"
43
					class="<?php echo ( 'custom_css' == $active ) ? 'current_page' : ''; ?>">
44
					<?php esc_html_e( 'Custom CSS', 'formidable' ); ?>
45
				</a>
46
			</li>
47
		</ul>
48
		<?php
49
	}
50
51
	/**
52
	 * @since 4.0
53
	 */
54
	public static function styler_save_button( $atts ) {
55
		$style = $atts['style'];
56
		if ( ! empty( $style->ID ) && empty( $style->menu_order ) ) {
57
			$delete_link = admin_url( 'admin.php?page=formidable-styles&frm_action=destroy&id=' . $style->ID );
58
		}
59
60
		include( FrmAppHelper::plugin_path() . '/classes/views/styles/header-buttons.php' );
61
	}
62
63
	/**
64
	 * Called from the admin header.
65
	 *
66
	 * @since 4.0
67
	 */
68
	public static function save_button() {
69
		?>
70
		<input type="submit" name="submit" class="button button-primary frm-button-primary" value="<?php esc_attr_e( 'Update', 'formidable' ); ?>" />
71
		<?php
72
	}
73
74
	/**
75
	 * Either get the heading or the style switcher.
76
	 *
77
	 * @since 4.0
78
	 */
79
	public static function styler_switcher( $atts ) {
80
		if ( has_action( 'frm_style_switcher_heading' ) ) {
81
			do_action( 'frm_style_switcher_heading', $atts );
82
		} else {
83
			?>
84
			<div class="frm_top_left">
85
				<h1>
86
					<?php echo esc_html( $atts['style']->post_title ); ?>
87
				</h1>
88
			</div>
89
			<?php
90
		}
91
	}
92
93
	/**
94
	 * @since 2.05
95
	 */
96
	public static function get_css_label_positions() {
97
		return array(
98
			'none'     => __( 'top', 'formidable' ),
99
			'left'     => __( 'left', 'formidable' ),
100
			'right'    => __( 'right', 'formidable' ),
101
			'no_label' => __( 'none', 'formidable' ),
102
			'inside'   => __( 'inside', 'formidable' ),
103
		);
104
	}
105
106
	public static function get_single_label_positions() {
107
		return array(
108
			'top'    => __( 'Top', 'formidable' ),
109
			'left'   => __( 'Left', 'formidable' ),
110
			'right'  => __( 'Right', 'formidable' ),
111
			'inline' => __( 'Inline (left without a set width)', 'formidable' ),
112
			'none'   => __( 'None', 'formidable' ),
113
			'hidden' => __( 'Hidden (but leave the space)', 'formidable' ),
114
			'inside' => __( 'Placeholder inside the field', 'formidable' ),
115
		);
116
	}
117
118
	public static function minus_icons() {
119
		return array(
120
			0 => array(
121
				'-' => '62e',
122
				'+' => '62f',
123
			),
124
			1 => array(
125
				'-' => '600',
126
				'+' => '602',
127
			),
128
			2 => array(
129
				'-' => '604',
130
				'+' => '603',
131
			),
132
			3 => array(
133
				'-' => '633',
134
				'+' => '632',
135
			),
136
			4 => array(
137
				'-' => '613',
138
				'+' => '60f',
139
			),
140
		);
141
	}
142
143
	public static function arrow_icons() {
144
		$minus_icons = self::minus_icons();
145
146
		return array(
147
			6    => array(
148
				'-' => '62d',
149
				'+' => '62a',
150
			),
151
			0    => array(
152
				'-' => '60d',
153
				'+' => '609',
154
			),
155
			1    => array(
156
				'-' => '60e',
157
				'+' => '60c',
158
			),
159
			2    => array(
160
				'-' => '630',
161
				'+' => '631',
162
			),
163
			3    => array(
164
				'-' => '62b',
165
				'+' => '628',
166
			),
167
			4    => array(
168
				'-' => '62c',
169
				'+' => '629',
170
			),
171
			5    => array(
172
				'-' => '635',
173
				'+' => '634',
174
			),
175
			'p0' => $minus_icons[0],
176
			'p1' => $minus_icons[1],
177
			'p2' => $minus_icons[2],
178
			'p3' => $minus_icons[3],
179
			'p4' => $minus_icons[4],
180
		);
181
	}
182
183
	/**
184
	 * @since 2.0
185
	 * @return The class for this icon
186
	 */
187
	public static function icon_key_to_class( $key, $icon = '+', $type = 'arrow' ) {
188
		if ( 'arrow' == $type && is_numeric( $key ) ) {
189
			//frm_arrowup6_icon
190
			$arrow = array(
191
				'-' => 'down',
192
				'+' => 'up',
193
			);
194
			$class = 'frm_arrow' . $arrow[ $icon ];
195
		} else {
196
			//frm_minus1_icon
197
			$key   = str_replace( 'p', '', $key );
198
			$plus  = array(
199
				'-' => 'minus',
200
				'+' => 'plus',
201
			);
202
			$class = 'frm_' . $plus[ $icon ];
203
		}
204
205
		if ( $key ) {
206
			$class .= $key;
207
		}
208
		$class .= '_icon';
209
210
		return $class;
211
	}
212
213
	public static function bs_icon_select( $style, $frm_style, $type = 'arrow' ) {
214
		$function_name = $type . '_icons';
215
		$icons         = self::$function_name();
216
		unset( $function_name );
217
218
		$name = ( 'arrow' == $type ) ? 'collapse_icon' : 'repeat_icon';
219
		?>
220
		<div class="btn-group" id="frm_<?php echo esc_attr( $name ); ?>_select">
221
			<button class="multiselect dropdown-toggle btn btn-default" data-toggle="dropdown" type="button">
222
				<?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $style->post_content[ $name ], '+', $type ) ); ?>
223
				<?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $style->post_content[ $name ], '-', $type ) ); ?>
224
				<b class="caret"></b>
225
			</button>
226
			<ul class="multiselect-container frm-dropdown-menu">
227
				<?php foreach ( $icons as $key => $icon ) { ?>
228
					<li <?php echo ( $style->post_content['collapse_icon'] == $key ) ? 'class="active"' : ''; ?>>
229
						<a href="javascript:void(0);">
230
							<label>
231
								<input type="radio" value="<?php echo esc_attr( $key ); ?>" name="<?php echo esc_attr( $frm_style->get_field_name( $name ) ); ?>" <?php checked( $style->post_content[ $name ], $key ); ?>/>
232
								<span>
233
									<?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $key, '+', $type ) ); ?>
234
									<?php FrmAppHelper::icon_by_class( 'frmfont ' . self::icon_key_to_class( $key, '-', $type ) ); ?>
235
								</span>
236
							</label>
237
						</a>
238
					</li>
239
				<?php } ?>
240
			</ul>
241
		</div>
242
		<?php
243
	}
244
245
	public static function hex2rgb( $hex ) {
246
		$hex = str_replace( '#', '', $hex );
247
248
		list( $r, $g, $b ) = sscanf( $hex, '%02x%02x%02x' );
249
250
		$rgb = array( $r, $g, $b );
251
252
		return implode( ',', $rgb );
253
	}
254
255
	/**
256
	 * @since 4.0
257
	 */
258
	public static function hex2rgba( $hex, $a ) {
259
		$rgb = self::hex2rgb( $hex );
260
261
		return 'rgba(' . $rgb . ',' . $a . ')';
262
	}
263
264
	/**
265
	 * @param $hex string - The original color in hex format #ffffff
266
	 * @param $steps integer - should be between -255 and 255. Negative = darker, positive = lighter
267
	 *
268
	 * @since 2.3
269
	 */
270
	public static function adjust_brightness( $hex, $steps ) {
271
		$steps = max( - 255, min( 255, $steps ) );
272
273
		// Normalize into a six character long hex string
274
		$hex = str_replace( '#', '', $hex );
275
		if ( strlen( $hex ) == 3 ) {
276
			$hex = str_repeat( substr( $hex, 0, 1 ), 2 );
277
			$hex .= str_repeat( substr( $hex, 1, 1 ), 2 );
278
			$hex .= str_repeat( substr( $hex, 2, 1 ), 2 );
279
		}
280
281
		// Split into three parts: R, G and B
282
		$color_parts = str_split( $hex, 2 );
283
		$return      = '#';
284
285
		foreach ( $color_parts as $color ) {
286
			$color  = hexdec( $color ); // Convert to decimal
287
			$color  = max( 0, min( 255, $color + $steps ) ); // Adjust color
288
			$return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code
289
		}
290
291
		return $return;
292
	}
293
294
	/**
295
	 * @since 4.05.02
296
	 */
297
	public static function get_css_vars( $vars = array() ) {
298
		$vars = apply_filters( 'frm_css_vars', $vars );
299
		return array_unique( $vars );
300
	}
301
302
	/**
303
	 * @since 4.05.02
304
	 */
305
	public static function output_vars( $settings, $defaults = array(), $vars = array() ) {
306
		if ( empty( $vars ) ) {
307
			$vars = self::get_css_vars( array_keys( $settings ) );
308
		}
309
		$remove = array( 'remove_box_shadow', 'remove_box_shadow_active', 'theme_css', 'theme_name', 'theme_selector', 'important_style', 'submit_style', 'collapse_icon', 'center_form', 'custom_css', 'style_class', 'submit_bg_img', 'change_margin', 'repeat_icon' );
310
		$vars   = array_diff( $vars, $remove );
311
312
		foreach ( $vars as $var ) {
313
			if ( ! isset( $settings[ $var ] ) ) {
314
				continue;
315
			}
316
			$show = empty( $defaults ) || ( $settings[ $var ] !== '' && $settings[ $var ] !== $defaults[ $var ] );
317
			if ( $show ) {
318
				echo '--' . esc_html( str_replace( '_', '-', $var ) ) . ':' . ( $var === 'font' ? FrmAppHelper::kses( $settings[ $var ] ) : esc_html( $settings[ $var ] ) ) . ';'; // WPCS: XSS ok.
319
			}
320
		}
321
	}
322
323
	/**
324
	 * @since 2.3
325
	 */
326
	public static function get_settings_for_output( $style ) {
327
		if ( self::previewing_style() ) {
328
329
			if ( isset( $_POST['frm_style_setting'] ) ) {
330
				// Sanitizing is done later.
331
				// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
332
				$posted = wp_unslash( $_POST['frm_style_setting'] );
333
				if ( ! is_array( $posted ) ) {
334
					$posted = json_decode( $posted, true );
335
					FrmAppHelper::format_form_data( $posted );
336
					$settings = $posted['frm_style_setting']['post_content'];
337
					$style_name = sanitize_title( $posted['style_name'] );
338
				} else {
339
					$settings = $posted['post_content'];
340
					$style_name = FrmAppHelper::get_post_param( 'style_name', '', 'sanitize_title' );
341
				}
342
			} else {
343
				$settings = $_GET;
344
				$style_name = FrmAppHelper::get_param( 'style_name', '', 'get', 'sanitize_title' );
345
			}
346
347
			FrmAppHelper::sanitize_value( 'sanitize_text_field', $settings );
348
349
			$settings['style_class'] = '';
350
			if ( ! empty( $style_name ) ) {
351
				$settings['style_class'] = $style_name . '.';
352
			}
353
		} else {
354
			$settings                = $style->post_content;
355
			$settings['style_class'] = 'frm_style_' . $style->post_name . '.';
356
		}
357
358
		$settings['style_class']   .= 'with_frm_style';
359
		$settings['font']          = stripslashes( $settings['font'] );
360
		$settings['change_margin'] = self::description_margin_for_screensize( $settings['width'] );
361
362
		$checkbox_opts = array( 'important_style', 'auto_width', 'submit_style', 'collapse_icon', 'center_form' );
363
		foreach ( $checkbox_opts as $opt ) {
364
			if ( ! isset( $settings[ $opt ] ) ) {
365
				$settings[ $opt ] = 0;
366
			}
367
		}
368
369
		self::prepare_color_output( $settings );
370
371
		$settings['field_height'] = $settings['field_height'] === '' ? 'auto' : $settings['field_height'];
372
		$settings['field_width']  = $settings['field_width'] === '' ? 'auto' : $settings['field_width'];
373
		$settings['auto_width']   = $settings['auto_width'] ? 'auto' : $settings['field_width'];
374
		$settings['box_shadow']   = ( isset( $settings['remove_box_shadow'] ) && $settings['remove_box_shadow'] ) ? 'none' : '0 1px 1px rgba(0, 0, 0, 0.075) inset';
375
376
		return $settings;
377
	}
378
379
	/**
380
	 * @since 2.3
381
	 */
382
	public static function prepare_color_output( &$settings, $allow_transparent = true ) {
383
		$colors = self::allow_color_override();
384
		foreach ( $colors as $css => $opts ) {
385
			if ( $css === 'transparent' && ! $allow_transparent ) {
386
				$css = '';
387
			}
388
			foreach ( $opts as $opt ) {
389
				self::get_color_output( $css, $settings[ $opt ] );
390
			}
391
		}
392
	}
393
394
	/**
395
	 * @since 2.3
396
	 */
397
	private static function allow_color_override() {
398
		$frm_style = new FrmStyle();
399
		$colors    = $frm_style->get_color_settings();
400
401
		$transparent = array(
402
			'fieldset_color',
403
			'fieldset_bg_color',
404
			'bg_color',
405
			'section_bg_color',
406
			'error_bg',
407
			'success_bg_color',
408
			'progress_bg_color',
409
			'progress_active_bg_color',
410
		);
411
412
		return array(
413
			'transparent' => $transparent,
414
			''            => array_diff( $colors, $transparent ),
415
		);
416
	}
417
418
	/**
419
	 * @since 2.3
420
	 */
421
	private static function get_color_output( $default, &$color ) {
422
		$color = trim( $color );
423
		if ( empty( $color ) ) {
424
			$color = $default;
425
		} elseif ( strpos( $color, '#' ) === false ) {
426
			$color = '#' . $color;
427
		}
428
	}
429
430
	/**
431
	 * If left/right label is over a certain size,
432
	 * adjust the field description margin at a different screen size
433
	 *
434
	 * @since 2.3
435
	 */
436
	private static function description_margin_for_screensize( $width ) {
437
		$temp_label_width = str_replace( 'px', '', $width );
438
		$change_margin    = false;
439
		if ( $temp_label_width >= 230 ) {
440
			$change_margin = '800px';
441
		} elseif ( $width >= 215 ) {
442
			$change_margin = '700px';
443
		} elseif ( $width >= 180 ) {
444
			$change_margin = '650px';
445
		}
446
447
		return $change_margin;
448
	}
449
450
	/**
451
	 * @since 2.3
452
	 */
453
	public static function previewing_style() {
454
		$ajax_change = isset( $_POST['action'] ) && $_POST['action'] === 'frm_change_styling' && isset( $_POST['frm_style_setting'] );
455
456
		return $ajax_change || isset( $_GET['flat'] );
457
	}
458
459
	/**
460
	 * @deprecated 3.01
461
	 * @codeCoverageIgnore
462
	 */
463
	public static function get_sigle_label_postitions() {
464
		return FrmDeprecated::get_sigle_label_postitions();
465
	}
466
467
	/**
468
	 * @deprecated 3.02.03
469
	 * @codeCoverageIgnore
470
	 */
471
	public static function jquery_themes() {
472
		return FrmDeprecated::jquery_themes();
473
	}
474
475
	/**
476
	 * @deprecated 3.02.03
477
	 * @codeCoverageIgnore
478
	 */
479
	public static function jquery_css_url( $theme_css ) {
480
		return FrmDeprecated::jquery_css_url( $theme_css );
481
	}
482
483
	/**
484
	 * @deprecated 3.02.03
485
	 * @codeCoverageIgnore
486
	 */
487
	public static function enqueue_jquery_css() {
488
		FrmDeprecated::enqueue_jquery_css();
489
	}
490
491
	/**
492
	 * @deprecated 3.02.03
493
	 * @codeCoverageIgnore
494
	 */
495
	public static function get_form_for_page() {
496
		return FrmDeprecated::get_form_for_page();
497
	}
498
}
499