Completed
Push — master ( ae12eb...603d32 )
by Stephanie
02:47
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
class FrmStylesHelper {
3
4
	public static function get_upload_base() {
5
		$uploads = wp_upload_dir();
6
		if ( is_ssl() && ! preg_match( '/^https:\/\/.*\..*$/', $uploads['baseurl'] ) ) {
7
			$uploads['baseurl'] = str_replace( 'http://', 'https://', $uploads['baseurl'] );
8
		}
9
10
		return $uploads;
11
	}
12
13
	public static function style_menu( $active = '' ) {
14
?>
15
        <h2 class="nav-tab-wrapper">
16
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles' ) ) ?>" class="nav-tab <?php echo ( '' == $active ) ? 'nav-tab-active' : '' ?>"><?php esc_html_e( 'Edit Styles', 'formidable' ) ?></a>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
17
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles&frm_action=manage' ) ) ?>" class="nav-tab <?php echo ( 'manage' == $active ) ? 'nav-tab-active' : '' ?>"><?php esc_html_e( 'Manage Form Styles', 'formidable' ) ?></a>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
18
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable-styles&frm_action=custom_css' ) ); ?>" class="nav-tab <?php echo ( 'custom_css' == $active ) ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Custom CSS', 'formidable' ); ?></a>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
19
        </h2>
20
<?php
21
    }
22
23
	/**
24
	 * @since 2.05
25
	 */
26
	public static function get_css_label_positions() {
27
		return array(
28
			'none'     => __( 'top', 'formidable' ),
29
			'left'     => __( 'left', 'formidable' ),
30
			'right'    => __( 'right', 'formidable' ),
31
			'no_label' => __( 'none', 'formidable' ),
32
			'inside'   => __( 'inside', 'formidable' ),
33
		);
34
	}
35
36
	public static function get_single_label_positions() {
37
		return array(
38
			'top'    => __( 'Top', 'formidable' ),
39
			'left'   => __( 'Left', 'formidable' ),
40
			'right'  => __( 'Right', 'formidable' ),
41
			'inline' => __( 'Inline (left without a set width)', 'formidable' ),
42
			'none'   => __( 'None', 'formidable' ),
43
			'hidden' => __( 'Hidden (but leave the space)', 'formidable' ),
44
			'inside' => __( 'Placeholder inside the field', 'formidable' ),
45
		);
46
	}
47
48
	public static function minus_icons() {
49
		return array(
50
			0 => array(
51
				'-' => '62e',
52
				'+' => '62f',
53
			),
54
			1 => array(
55
				'-' => '600',
56
				'+' => '602',
57
			),
58
			2 => array(
59
				'-' => '604',
60
				'+' => '603',
61
			),
62
			3 => array(
63
				'-' => '633',
64
				'+' => '632',
65
			),
66
			4 => array(
67
				'-' => '613',
68
				'+' => '60f',
69
			),
70
		);
71
	}
72
73
	public static function arrow_icons() {
74
		$minus_icons = self::minus_icons();
75
76
		return array(
77
			6 => array(
78
				'-' => '62d',
79
				'+' => '62a',
80
			),
81
			0 => array(
82
				'-' => '60d',
83
				'+' => '609',
84
			),
85
			1 => array(
86
				'-' => '60e',
87
				'+' => '60c',
88
			),
89
			2 => array(
90
				'-' => '630',
91
				'+' => '631',
92
			),
93
			3 => array(
94
				'-' => '62b',
95
				'+' => '628',
96
			),
97
			4 => array(
98
				'-' => '62c',
99
				'+' => '629',
100
			),
101
			5 => array(
102
				'-' => '635',
103
				'+' => '634',
104
			),
105
			'p0' => $minus_icons[0],
106
			'p1' => $minus_icons[1],
107
			'p2' => $minus_icons[2],
108
			'p3' => $minus_icons[3],
109
			'p4' => $minus_icons[4],
110
		);
111
	}
112
113
    /**
114
     * @since 2.0
115
     * @return The class for this icon
116
     */
117
	public static function icon_key_to_class( $key, $icon = '+', $type = 'arrow' ) {
118
		if ( 'arrow' == $type && is_numeric( $key ) ) {
119
			//frm_arrowup6_icon
120
			$arrow = array(
121
				'-' => 'down',
122
				'+' => 'up',
123
			);
124
			$class = 'frm_arrow' . $arrow[ $icon ];
125
		} else {
126
			//frm_minus1_icon
127
			$key = str_replace( 'p', '', $key );
128
			$plus = array(
129
				'-' => 'minus',
130
				'+' => 'plus',
131
			);
132
			$class = 'frm_' . $plus[ $icon ];
133
		}
134
135
        if ( $key ) {
136
            $class .= $key;
137
        }
138
        $class .= '_icon';
139
140
        return $class;
141
    }
142
143
	public static function bs_icon_select( $style, $frm_style, $type = 'arrow' ) {
144
		$function_name = $type . '_icons';
145
		$icons = self::$function_name();
146
		unset( $function_name );
147
148
        $name = ( 'arrow' == $type ) ? 'collapse_icon' : 'repeat_icon';
149
?>
150
    	<select name="<?php echo esc_attr( $frm_style->get_field_name( $name ) ); ?>" id="frm_<?php echo esc_attr( $name ); ?>" class="frm_icon_font frm_multiselect hide-if-js">
151
            <?php foreach ( $icons as $key => $icon ) { ?>
152
			<option value="<?php echo esc_attr( $key ) ?>" <?php selected( $style->post_content[ $name ], $key ) ?>>
153
				<?php echo '&#xe' . esc_html( $icon['+'] ) . '; &#xe' . esc_html( $icon['-'] ) . ';'; ?>
154
            </option>
155
            <?php } ?>
156
    	</select>
157
158
        <div class="btn-group hide-if-no-js" id="frm_<?php echo esc_attr( $name ) ?>_select">
159
            <button class="multiselect dropdown-toggle btn btn-default" data-toggle="dropdown" type="button">
160
				<i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $style->post_content[ $name ], '+', $type ) ) ?>"></i>
161
				<i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $style->post_content[ $name ], '-', $type ) ) ?>"></i>
162
                <b class="caret"></b>
163
            </button>
164
            <ul class="multiselect-container frm-dropdown-menu">
165
                <?php foreach ( $icons as $key => $icon ) { ?>
166
                <li <?php echo ( $style->post_content['collapse_icon'] == $key ) ? 'class="active"' : '' ?>>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
167
                    <a href="javascript:void(0);">
168
                        <label>
169
                            <input type="radio" value="<?php echo esc_attr( $key ) ?>"/>
170
                            <span>
171
                                <i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $key, '+', $type ) ) ?>"></i>
172
                                <i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $key, '-', $type ) ) ?>"></i>
173
                            </span>
174
                        </label>
175
                    </a>
176
                </li>
177
                <?php } ?>
178
            </ul>
179
        </div>
180
<?php
181
    }
182
183
	public static function hex2rgb( $hex ) {
184
		$hex = str_replace( '#', '', $hex );
185
186
		list( $r, $g, $b ) = sscanf( $hex, '%02x%02x%02x' );
187
188
		$rgb = array( $r, $g, $b );
189
		return implode( ',', $rgb );
190
	}
191
192
	/**
193
	 * @param $hex string - The original color in hex format #ffffff
194
	 * @param $steps integer - should be between -255 and 255. Negative = darker, positive = lighter
195
	 * @since 2.3
196
	 */
197
	public static function adjust_brightness( $hex, $steps ) {
198
		$steps = max( -255, min( 255, $steps ) );
199
200
		// Normalize into a six character long hex string
201
		$hex = str_replace( '#', '', $hex );
202
		if ( strlen( $hex ) == 3 ) {
203
			$hex = str_repeat( substr( $hex, 0, 1 ), 2 );
204
			$hex .= str_repeat( substr( $hex, 1, 1 ), 2 );
205
			$hex .= str_repeat( substr( $hex, 2, 1 ), 2 );
206
		}
207
208
		// Split into three parts: R, G and B
209
		$color_parts = str_split( $hex, 2 );
210
		$return = '#';
211
212
		foreach ( $color_parts as $color ) {
213
			$color   = hexdec( $color ); // Convert to decimal
214
			$color   = max( 0, min( 255, $color + $steps ) ); // Adjust color
215
			$return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code
216
		}
217
218
		return $return;
219
	}
220
221
	/**
222
	 * @since 2.3
223
	 */
224
	public static function get_settings_for_output( $style ) {
225
		if ( self::previewing_style() ) {
226
			if ( isset( $_POST['frm_style_setting'] ) && isset( $_POST['frm_style_setting']['post_content'] ) ) {
227
				$settings = $_POST['frm_style_setting']['post_content'];
228
			} else {
229
				$settings = $_GET;
230
			}
231
			FrmAppHelper::sanitize_value( 'sanitize_text_field', $settings );
232
233
			$style_name = FrmAppHelper::get_param( 'style_name', '', 'get', 'sanitize_title' );
234
			$settings['style_class'] = '';
235
			if ( ! empty( $style_name ) ) {
236
				$settings['style_class'] = $style_name . '.';
237
			}
238
		} else {
239
			$settings = $style->post_content;
240
			$settings['style_class'] = 'frm_style_' . $style->post_name . '.';
241
		}
242
243
		$settings['style_class'] .= 'with_frm_style';
244
		$settings['font'] = stripslashes( $settings['font'] );
245
		$settings['change_margin'] = self::description_margin_for_screensize( $settings['width'] );
246
247
		$checkbox_opts = array( 'important_style', 'auto_width', 'submit_style', 'collapse_icon', 'center_form' );
248
		foreach ( $checkbox_opts as $opt ) {
249
			if ( ! isset( $settings[ $opt ] ) ) {
250
				$settings[ $opt ] = 0;
251
			}
252
		}
253
254
		self::prepare_color_output( $settings );
255
256
		return $settings;
257
	}
258
259
	/**
260
	 * @since 2.3
261
	 */
262
	public static function prepare_color_output( &$settings, $allow_transparent = true ) {
263
		$colors = self::allow_color_override();
264
		foreach ( $colors as $css => $opts ) {
265
			if ( $css === 'transparent' && ! $allow_transparent ) {
266
				$css = '';
267
			}
268
			foreach ( $opts as $opt ) {
269
				self::get_color_output( $css, $settings[ $opt ] );
270
			}
271
		}
272
	}
273
274
	/**
275
	 * @since 2.3
276
	 */
277
	private static function allow_color_override() {
278
		$frm_style = new FrmStyle();
279
		$colors = $frm_style->get_color_settings();
280
281
		$transparent = array( 'fieldset_color', 'fieldset_bg_color', 'bg_color', 'section_bg_color', 'error_bg', 'success_bg_color', 'progress_bg_color', 'progress_active_bg_color' );
282
283
		return array(
284
			'transparent' => $transparent,
285
			''            => array_diff( $colors, $transparent ),
286
		);
287
	}
288
289
	/**
290
	 * @since 2.3
291
	 */
292
	private static function get_color_output( $default, &$color ) {
293
		$color = trim( $color );
294
		if ( empty( $color ) ) {
295
			$color = $default;
296
		} elseif ( strpos( $color, '#' ) === false ) {
297
			$color = '#' . $color;
298
		}
299
	}
300
301
	/**
302
	 * If left/right label is over a certain size,
303
	 * adjust the field description margin at a different screen size
304
	 *
305
	 * @since 2.3
306
	 */
307
	private static function description_margin_for_screensize( $width ) {
308
		$temp_label_width = str_replace( 'px', '', $width );
309
		$change_margin = false;
310
		if ( $temp_label_width >= 230 ) {
311
			$change_margin = '800px';
312
		} else if ( $width >= 215 ) {
313
			$change_margin = '700px';
314
		} else if ( $width >= 180 ) {
315
			$change_margin = '650px';
316
		}
317
		return $change_margin;
318
	}
319
320
	/**
321
	 * @since 2.3
322
	 */
323
	public static function previewing_style() {
324
		$ajax_change = isset( $_POST['action'] ) && $_POST['action'] === 'frm_change_styling' && isset( $_POST['frm_style_setting'] );
325
		return $ajax_change || isset( $_GET['flat'] );
326
	}
327
328
	/**
329
	 * @deprecated 3.01
330
	 * @codeCoverageIgnore
331
	 */
332
	public static function get_sigle_label_postitions() {
333
		return FrmDeprecated::get_single_label_positions();
0 ignored issues
show
Bug introduced by
The method get_single_label_positions() does not exist on FrmDeprecated. Did you maybe mean get_sigle_label_postitions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
334
	}
335
336
	/**
337
	 * @deprecated 3.02.03
338
	 * @codeCoverageIgnore
339
	 */
340
	public static function jquery_themes() {
341
		return FrmDeprecated::jquery_themes();
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::jquery_themes() has been deprecated with message: 3.02.03

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
342
	}
343
344
	/**
345
	 * @deprecated 3.02.03
346
	 * @codeCoverageIgnore
347
	 */
348
	public static function jquery_css_url( $theme_css ) {
349
		return FrmDeprecated::jquery_css_url( $theme_css );
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::jquery_css_url() has been deprecated with message: 3.02.03

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
350
	}
351
352
	/**
353
	 * @deprecated 3.02.03
354
	 * @codeCoverageIgnore
355
	 */
356
	public static function enqueue_jquery_css() {
357
		FrmDeprecated::enqueue_jquery_css();
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::enqueue_jquery_css() has been deprecated with message: 3.02.03

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
358
	}
359
360
	/**
361
	 * @deprecated 3.02.03
362
	 * @codeCoverageIgnore
363
	 */
364
	public static function get_form_for_page() {
365
		return FrmDeprecated::get_form_for_page();
1 ignored issue
show
Deprecated Code introduced by
The method FrmDeprecated::get_form_for_page() has been deprecated with message: 3.02.03

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
366
	}
367
}
368