Completed
Push — master ( 030e64...7d42b5 )
by Stephanie
02:43
created

FrmStylesHelper::minus_icons()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
class FrmStylesHelper {
3
4
    public static function jquery_themes() {
5
        $themes = array(
6
            'ui-lightness'  => 'UI Lightness',
7
            'ui-darkness'   => 'UI Darkness',
8
            'smoothness'    => 'Smoothness',
9
            'start'         => 'Start',
10
            'redmond'       => 'Redmond',
11
            'sunny'         => 'Sunny',
12
            'overcast'      => 'Overcast',
13
            'le-frog'       => 'Le Frog',
14
            'flick'         => 'Flick',
15
			'pepper-grinder' => 'Pepper Grinder',
16
            'eggplant'      => 'Eggplant',
17
            'dark-hive'     => 'Dark Hive',
18
            'cupertino'     => 'Cupertino',
19
            'south-street'  => 'South Street',
20
            'blitzer'       => 'Blitzer',
21
            'humanity'      => 'Humanity',
22
            'hot-sneaks'    => 'Hot Sneaks',
23
            'excite-bike'   => 'Excite Bike',
24
            'vader'         => 'Vader',
25
            'dot-luv'       => 'Dot Luv',
26
            'mint-choc'     => 'Mint Choc',
27
            'black-tie'     => 'Black Tie',
28
            'trontastic'    => 'Trontastic',
29
            'swanky-purse'  => 'Swanky Purse',
30
        );
31
32
        $themes = apply_filters('frm_jquery_themes', $themes);
33
        return $themes;
34
    }
35
36
	public static function jquery_css_url( $theme_css ) {
37
        if ( $theme_css == -1 ) {
38
            return;
39
        }
40
41
        if ( ! $theme_css || $theme_css == '' || $theme_css == 'ui-lightness' ) {
42
            $css_file = FrmAppHelper::plugin_url() . '/css/ui-lightness/jquery-ui.css';
43
        } else if ( preg_match('/^http.?:\/\/.*\..*$/', $theme_css) ) {
44
            $css_file = $theme_css;
45
        } else {
46
            $uploads = self::get_upload_base();
47
			$file_path = '/formidable/css/' . $theme_css . '/jquery-ui.css';
48
            if ( file_exists($uploads['basedir'] . $file_path) ) {
49
                $css_file = $uploads['baseurl'] . $file_path;
50
            } else {
51
				$css_file = FrmAppHelper::jquery_ui_base_url() . '/themes/' . $theme_css . '/jquery-ui.min.css';
52
            }
53
        }
54
55
        return $css_file;
56
    }
57
58
    public static function enqueue_jquery_css() {
59
		$form = self::get_form_for_page();
60
		$theme_css = FrmStylesController::get_style_val( 'theme_css', $form );
61
        if ( $theme_css != -1 ) {
62
            wp_enqueue_style('jquery-theme', self::jquery_css_url($theme_css), array(), FrmAppHelper::plugin_version());
63
        }
64
    }
65
66
	public static function get_form_for_page() {
67
		global $frm_vars;
68
		$form_id = 'default';
69
		if ( ! empty( $frm_vars['forms_loaded'] ) ) {
70
			foreach ( $frm_vars['forms_loaded'] as $form ) {
71
				if ( is_object( $form ) ) {
72
					$form_id = $form->id;
73
					break;
74
				}
75
			}
76
		}
77
		return $form_id;
78
	}
79
80
    public static function get_upload_base() {
81
        $uploads = wp_upload_dir();
82
        if ( is_ssl() && ! preg_match('/^https:\/\/.*\..*$/', $uploads['baseurl']) ) {
83
            $uploads['baseurl'] = str_replace('http://', 'https://', $uploads['baseurl']);
84
        }
85
86
        return $uploads;
87
    }
88
89
	public static function style_menu( $active = '' ) {
90
?>
91
        <h2 class="nav-tab-wrapper">
92
			<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...
93
			<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...
94
			<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...
95
        </h2>
96
<?php
97
    }
98
99
	/**
100
	 * @since 2.05
101
	 */
102
	public static function get_css_label_positions() {
103
		return array(
104
			'none'     => __( 'top', 'formidable' ),
105
			'left'     => __( 'left', 'formidable' ),
106
			'right'    => __( 'right', 'formidable' ),
107
			'no_label' => __( 'none', 'formidable' ),
108
			'inside'   => __( 'inside', 'formidable' ),
109
		);
110
	}
111
112
	public static function get_sigle_label_postitions() {
113
		return array(
114
			'top'    => __( 'Top', 'formidable' ),
115
			'left'   => __( 'Left', 'formidable' ),
116
			'right'  => __( 'Right', 'formidable' ),
117
			'inline' => __( 'Inline (left without a set width)', 'formidable' ),
118
			'none'   => __( 'None', 'formidable' ),
119
			'hidden' => __( 'Hidden (but leave the space)', 'formidable' ),
120
			'inside' => __( 'Placeholder inside the field', 'formidable' ),
121
		);
122
	}
123
124
	public static function minus_icons() {
125
		return array(
126
			0 => array(
127
				'-' => '62e',
128
				'+' => '62f',
129
			),
130
			1 => array(
131
				'-' => '600',
132
				'+' => '602',
133
			),
134
			2 => array(
135
				'-' => '604',
136
				'+' => '603',
137
			),
138
			3 => array(
139
				'-' => '633',
140
				'+' => '632',
141
			),
142
			4 => array(
143
				'-' => '613',
144
				'+' => '60f',
145
			),
146
		);
147
	}
148
149
	public static function arrow_icons() {
150
		$minus_icons = self::minus_icons();
151
152
		return array(
153
			6 => array(
154
				'-' => '62d',
155
				'+' => '62a',
156
			),
157
			0 => array(
158
				'-' => '60d',
159
				'+' => '609',
160
			),
161
			1 => array(
162
				'-' => '60e',
163
				'+' => '60c',
164
			),
165
			2 => array(
166
				'-' => '630',
167
				'+' => '631',
168
			),
169
			3 => array(
170
				'-' => '62b',
171
				'+' => '628',
172
			),
173
			4 => array(
174
				'-' => '62c',
175
				'+' => '629',
176
			),
177
			5 => array(
178
				'-' => '635',
179
				'+' => '634',
180
			),
181
			'p0' => $minus_icons[0],
182
			'p1' => $minus_icons[1],
183
			'p2' => $minus_icons[2],
184
			'p3' => $minus_icons[3],
185
			'p4' => $minus_icons[4],
186
		);
187
	}
188
189
    /**
190
     * @since 2.0
191
     * @return The class for this icon
192
     */
193
	public static function icon_key_to_class( $key, $icon = '+', $type = 'arrow' ) {
194
		if ( 'arrow' == $type && is_numeric( $key ) ) {
195
			//frm_arrowup6_icon
196
			$arrow = array( '-' => 'down', '+' => 'up' );
197
			$class = 'frm_arrow' . $arrow[ $icon ];
198
		} else {
199
			//frm_minus1_icon
200
			$key = str_replace( 'p', '', $key );
201
			$plus = array(
202
				'-' => 'minus',
203
				'+' => 'plus',
204
			);
205
			$class = 'frm_' . $plus[ $icon ];
206
		}
207
208
        if ( $key ) {
209
            $class .= $key;
210
        }
211
        $class .= '_icon';
212
213
        return $class;
214
    }
215
216
	public static function bs_icon_select( $style, $frm_style, $type = 'arrow' ) {
217
		$function_name = $type . '_icons';
218
		$icons = self::$function_name();
219
		unset( $function_name );
220
221
        $name = ( 'arrow' == $type ) ? 'collapse_icon' : 'repeat_icon';
222
?>
223
    	<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">
224
            <?php foreach ( $icons as $key => $icon ) { ?>
225
			<option value="<?php echo esc_attr( $key ) ?>" <?php selected( $style->post_content[ $name ], $key ) ?>>
226
				<?php echo '&#xe' . $icon['+'] . '; &#xe' . $icon['-'] . ';'; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$icon'
Loading history...
227
            </option>
228
            <?php } ?>
229
    	</select>
230
231
        <div class="btn-group hide-if-no-js" id="frm_<?php echo esc_attr( $name ) ?>_select">
232
            <button class="multiselect dropdown-toggle btn btn-default" data-toggle="dropdown" type="button">
233
				<i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $style->post_content[ $name ], '+', $type ) ) ?>"></i>
234
				<i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $style->post_content[ $name ], '-', $type ) ) ?>"></i>
235
                <b class="caret"></b>
236
            </button>
237
            <ul class="multiselect-container frm-dropdown-menu">
238
                <?php foreach ( $icons as $key => $icon ) { ?>
239
                <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...
240
                    <a href="javascript:void(0);">
241
                        <label>
242
                            <input type="radio" value="<?php echo esc_attr( $key ) ?>"/>
243
                            <span>
244
                                <i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $key, '+', $type ) ) ?>"></i>
245
                                <i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $key, '-', $type ) ) ?>"></i>
246
                            </span>
247
                        </label>
248
                    </a>
249
                </li>
250
                <?php } ?>
251
            </ul>
252
        </div>
253
<?php
254
    }
255
256
	public static function hex2rgb( $hex ) {
257
        $hex = str_replace('#', '', $hex);
258
259
        if ( strlen($hex) == 3 ) {
260
			$r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) );
261
			$g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) );
262
			$b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) );
263
        } else {
264
			$r = hexdec( substr( $hex, 0, 2 ) );
265
			$g = hexdec( substr( $hex, 2, 2 ) );
266
			$b = hexdec( substr( $hex, 4, 2 ) );
267
        }
268
		$rgb = array( $r, $g, $b );
269
        return implode(',', $rgb); // returns the rgb values separated by commas
270
        //return $rgb; // returns an array with the rgb values
271
    }
272
273
	/**
274
	 * @param $hex string - The original color in hex format #ffffff
275
	 * @param $steps integer - should be between -255 and 255. Negative = darker, positive = lighter
276
	 * @since 2.3
277
	 */
278
	public static function adjust_brightness( $hex, $steps ) {
279
		$steps = max( -255, min( 255, $steps ) );
280
281
		// Normalize into a six character long hex string
282
		$hex = str_replace( '#', '', $hex );
283
		if ( strlen( $hex ) == 3 ) {
284
			$hex = str_repeat( substr( $hex, 0, 1 ), 2 );
285
			$hex .= str_repeat( substr( $hex, 1, 1 ), 2 );
286
			$hex .= str_repeat( substr( $hex, 2, 1 ), 2 );
287
		}
288
289
		// Split into three parts: R, G and B
290
		$color_parts = str_split( $hex, 2 );
291
		$return = '#';
292
293
		foreach ( $color_parts as $color ) {
294
			$color   = hexdec( $color ); // Convert to decimal
295
			$color   = max( 0, min( 255, $color + $steps ) ); // Adjust color
296
			$return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code
297
		}
298
299
		return $return;
300
	}
301
302
	/**
303
	 * @since 2.3
304
	 */
305
	public static function get_settings_for_output( $style ) {
306
		if ( self::previewing_style() ) {
307
			if ( isset( $_GET['frm_style_setting'] ) ) {
308
				$settings = $_GET['frm_style_setting']['post_content'];
309
			} else {
310
				$settings = $_GET;
311
			}
312
			FrmAppHelper::sanitize_value( 'sanitize_text_field', $settings );
313
314
			$style_name = FrmAppHelper::simple_get( 'style_name', 'sanitize_title' );
315
			$settings['style_class'] = '';
316
			if ( ! empty( $style_name ) ) {
317
				$settings['style_class'] = $style_name . '.';
318
			}
319
		} else {
320
			$settings = $style->post_content;
321
			$settings['style_class'] = 'frm_style_' . $style->post_name . '.';
322
		}
323
324
		$settings['style_class'] .= 'with_frm_style';
325
		$settings['font'] = stripslashes( $settings['font'] );
326
		$settings['change_margin'] = self::description_margin_for_screensize( $settings['width'] );
327
328
		$checkbox_opts = array( 'important_style', 'auto_width', 'submit_style', 'collapse_icon', 'center_form' );
329
		foreach ( $checkbox_opts as $opt ) {
330
			if ( ! isset( $settings[ $opt ] ) ) {
331
				$settings[ $opt ] = 0;
332
			}
333
		}
334
335
		self::prepare_color_output( $settings );
336
337
		return $settings;
338
	}
339
340
	/**
341
	 * @since 2.3
342
	 */
343
	private static function prepare_color_output( &$settings ) {
344
		$colors = self::allow_color_override();
345
		foreach ( $colors as $css => $opts ) {
346
			foreach ( $opts as $opt ) {
347
				self::get_color_output( $css, $settings[ $opt ] );
348
			}
349
		}
350
	}
351
352
	/**
353
	 * @since 2.3
354
	 */
355
	private static function allow_color_override() {
356
		return array(
357
			'transparent' => array( 'fieldset_color', 'fieldset_bg_color', 'bg_color', 'section_bg_color', 'error_bg', 'success_bg_color', 'progress_bg_color', 'progress_active_bg_color' ),
358
			'' => array( 'title_color', 'section_color', 'submit_text_color', 'label_color', 'check_label_color', 'form_desc_color', 'description_color', 'text_color', 'text_color_disabled', 'border_color', 'submit_bg_color', 'submit_border_color', 'error_text', 'progress_border_color', 'progress_color', 'progress_active_color', 'submit_hover_bg_color', 'submit_hover_border_color', 'submit_hover_color', 'submit_active_color', 'submit_active_border_color', 'submit_active_bg_color' ),
359
		);
360
	}
361
362
	/**
363
	 * @since 2.3
364
	 */
365
	private static function get_color_output( $default, &$color ) {
366
		$color = ( trim( $color ) == '' ) ? $default : '#' . $color;
367
	}
368
369
	/**
370
	 * If left/right label is over a certain size,
371
	 * adjust the field description margin at a different screen size
372
	 * @since 2.3
373
	 */
374
	private static function description_margin_for_screensize( $width ) {
375
		$temp_label_width = str_replace( 'px', '', $width );
376
		$change_margin = false;
377
		if ( $temp_label_width >= 230 ) {
378
			$change_margin = '800px';
379
		} else if ( $width >= 215 ) {
380
			$change_margin = '700px';
381
		} else if ( $width >= 180 ) {
382
			$change_margin = '650px';
383
		}
384
		return $change_margin;
385
	}
386
387
	/**
388
	 * @since 2.3
389
	 */
390
	public static function previewing_style() {
391
		return isset( $_GET['frm_style_setting'] ) || isset( $_GET['flat'] );
392
	}
393
}
394