Completed
Push — master ( 105a2b...368c73 )
by Stephanie
02:12
created

FrmStylesHelper::arrow_icons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 39
rs 9.296
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 2.3
296
	 */
297
	public static function get_settings_for_output( $style ) {
298
		if ( self::previewing_style() ) {
299
300
			if ( isset( $_POST['frm_style_setting'] ) ) {
301
				$posted = json_decode( wp_unslash( $_POST['frm_style_setting'] ), true );
302
				FrmAppHelper::format_form_data( $posted );
0 ignored issues
show
Bug introduced by
The method format_form_data() does not seem to exist on object<FrmAppHelper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
303
304
				$settings = $posted['frm_style_setting']['post_content'];
305
				$style_name = sanitize_title( $posted['style_name'] );
306
			} else {
307
				$settings = $_GET;
308
				$style_name = FrmAppHelper::get_param( 'style_name', '', 'get', 'sanitize_title' );
309
			}
310
311
			FrmAppHelper::sanitize_value( 'sanitize_text_field', $settings );
312
313
			$settings['style_class'] = '';
314
			if ( ! empty( $style_name ) ) {
315
				$settings['style_class'] = $style_name . '.';
316
			}
317
		} else {
318
			$settings                = $style->post_content;
319
			$settings['style_class'] = 'frm_style_' . $style->post_name . '.';
320
		}
321
322
		$settings['style_class']   .= 'with_frm_style';
323
		$settings['font']          = stripslashes( $settings['font'] );
324
		$settings['change_margin'] = self::description_margin_for_screensize( $settings['width'] );
325
326
		$checkbox_opts = array( 'important_style', 'auto_width', 'submit_style', 'collapse_icon', 'center_form' );
327
		foreach ( $checkbox_opts as $opt ) {
328
			if ( ! isset( $settings[ $opt ] ) ) {
329
				$settings[ $opt ] = 0;
330
			}
331
		}
332
333
		self::prepare_color_output( $settings );
334
335
		return $settings;
336
	}
337
338
	/**
339
	 * @since 2.3
340
	 */
341
	public static function prepare_color_output( &$settings, $allow_transparent = true ) {
342
		$colors = self::allow_color_override();
343
		foreach ( $colors as $css => $opts ) {
344
			if ( $css === 'transparent' && ! $allow_transparent ) {
345
				$css = '';
346
			}
347
			foreach ( $opts as $opt ) {
348
				self::get_color_output( $css, $settings[ $opt ] );
349
			}
350
		}
351
	}
352
353
	/**
354
	 * @since 2.3
355
	 */
356
	private static function allow_color_override() {
357
		$frm_style = new FrmStyle();
358
		$colors    = $frm_style->get_color_settings();
359
360
		$transparent = array(
361
			'fieldset_color',
362
			'fieldset_bg_color',
363
			'bg_color',
364
			'section_bg_color',
365
			'error_bg',
366
			'success_bg_color',
367
			'progress_bg_color',
368
			'progress_active_bg_color',
369
		);
370
371
		return array(
372
			'transparent' => $transparent,
373
			''            => array_diff( $colors, $transparent ),
374
		);
375
	}
376
377
	/**
378
	 * @since 2.3
379
	 */
380
	private static function get_color_output( $default, &$color ) {
381
		$color = trim( $color );
382
		if ( empty( $color ) ) {
383
			$color = $default;
384
		} elseif ( strpos( $color, '#' ) === false ) {
385
			$color = '#' . $color;
386
		}
387
	}
388
389
	/**
390
	 * If left/right label is over a certain size,
391
	 * adjust the field description margin at a different screen size
392
	 *
393
	 * @since 2.3
394
	 */
395
	private static function description_margin_for_screensize( $width ) {
396
		$temp_label_width = str_replace( 'px', '', $width );
397
		$change_margin    = false;
398
		if ( $temp_label_width >= 230 ) {
399
			$change_margin = '800px';
400
		} elseif ( $width >= 215 ) {
401
			$change_margin = '700px';
402
		} elseif ( $width >= 180 ) {
403
			$change_margin = '650px';
404
		}
405
406
		return $change_margin;
407
	}
408
409
	/**
410
	 * @since 2.3
411
	 */
412
	public static function previewing_style() {
413
		$ajax_change = isset( $_POST['action'] ) && $_POST['action'] === 'frm_change_styling' && isset( $_POST['frm_style_setting'] );
414
415
		return $ajax_change || isset( $_GET['flat'] );
416
	}
417
418
	/**
419
	 * @deprecated 3.01
420
	 * @codeCoverageIgnore
421
	 */
422
	public static function get_sigle_label_postitions() {
423
		return FrmDeprecated::get_sigle_label_postitions();
424
	}
425
426
	/**
427
	 * @deprecated 3.02.03
428
	 * @codeCoverageIgnore
429
	 */
430
	public static function jquery_themes() {
431
		return FrmDeprecated::jquery_themes();
432
	}
433
434
	/**
435
	 * @deprecated 3.02.03
436
	 * @codeCoverageIgnore
437
	 */
438
	public static function jquery_css_url( $theme_css ) {
439
		return FrmDeprecated::jquery_css_url( $theme_css );
440
	}
441
442
	/**
443
	 * @deprecated 3.02.03
444
	 * @codeCoverageIgnore
445
	 */
446
	public static function enqueue_jquery_css() {
447
		FrmDeprecated::enqueue_jquery_css();
448
	}
449
450
	/**
451
	 * @deprecated 3.02.03
452
	 * @codeCoverageIgnore
453
	 */
454
	public static function get_form_for_page() {
455
		return FrmDeprecated::get_form_for_page();
456
	}
457
}
458