Completed
Push — master ( 1e23f1...3c8881 )
by Stephanie
03:12
created

FrmStylesHelper::arrow_icons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 18
rs 9.4285
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 _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 _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 _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.04.02
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( '-' => '62e', '+' => '62f' ),
127
			1 => array( '-' => '600', '+' => '602' ),
128
			2 => array( '-' => '604', '+' => '603' ),
129
			3 => array( '-' => '633', '+' => '632' ),
130
			4 => array( '-' => '613', '+' => '60f' ),
131
        );
132
    }
133
134
    public static function arrow_icons() {
135
        $minus_icons = self::minus_icons();
136
137
        return array(
138
			6 => array( '-' => '62d', '+' => '62a' ),
139
			0 => array( '-' => '60d', '+' => '609' ),
140
			1 => array( '-' => '60e', '+' => '60c' ),
141
			2 => array( '-' => '630', '+' => '631' ),
142
			3 => array( '-' => '62b', '+' => '628' ),
143
			4 => array( '-' => '62c', '+' => '629' ),
144
			5 => array( '-' => '635', '+' => '634' ),
145
            'p0' => $minus_icons[0],
146
            'p1' => $minus_icons[1],
147
            'p2' => $minus_icons[2],
148
            'p3' => $minus_icons[3],
149
            'p4' => $minus_icons[4],
150
        );
151
    }
152
153
    /**
154
     * @since 2.0
155
     * @return The class for this icon
156
     */
157
	public static function icon_key_to_class( $key, $icon = '+', $type = 'arrow' ) {
158
        if ( 'arrow' == $type && is_numeric($key) ) {
159
            //frm_arrowup6_icon
160
			$arrow = array( '-' => 'down', '+' => 'up' );
161
			$class = 'frm_arrow' . $arrow[ $icon ];
162
        } else {
163
            //frm_minus1_icon
164
            $key = str_replace('p', '', $key);
165
			$plus = array( '-' => 'minus', '+' => 'plus' );
166
			$class = 'frm_' . $plus[ $icon ];
167
        }
168
169
        if ( $key ) {
170
            $class .= $key;
171
        }
172
        $class .= '_icon';
173
174
        return $class;
175
    }
176
177
	public static function bs_icon_select( $style, $frm_style, $type = 'arrow' ) {
178
		$function_name = $type . '_icons';
179
		$icons = self::$function_name();
180
		unset( $function_name );
181
182
        $name = ( 'arrow' == $type ) ? 'collapse_icon' : 'repeat_icon';
183
?>
184
    	<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">
185
            <?php foreach ( $icons as $key => $icon ) { ?>
186
			<option value="<?php echo esc_attr( $key ) ?>" <?php selected( $style->post_content[ $name ], $key ) ?>>
187
				<?php echo '&#xe' . $icon['+'] . '; &#xe' . $icon['-'] . ';'; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$icon'
Loading history...
188
            </option>
189
            <?php } ?>
190
    	</select>
191
192
        <div class="btn-group hide-if-no-js" id="frm_<?php echo esc_attr( $name ) ?>_select">
193
            <button class="multiselect dropdown-toggle btn btn-default" data-toggle="dropdown" type="button">
194
				<i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $style->post_content[ $name ], '+', $type ) ) ?>"></i>
195
				<i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $style->post_content[ $name ], '-', $type ) ) ?>"></i>
196
                <b class="caret"></b>
197
            </button>
198
            <ul class="multiselect-container frm-dropdown-menu">
199
                <?php foreach ( $icons as $key => $icon ) { ?>
200
                <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...
201
                    <a href="javascript:void(0);">
202
                        <label>
203
                            <input type="radio" value="<?php echo esc_attr( $key ) ?>"/>
204
                            <span>
205
                                <i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $key, '+', $type ) ) ?>"></i>
206
                                <i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $key, '-', $type ) ) ?>"></i>
207
                            </span>
208
                        </label>
209
                    </a>
210
                </li>
211
                <?php } ?>
212
            </ul>
213
        </div>
214
<?php
215
    }
216
217
	public static function hex2rgb( $hex ) {
218
        $hex = str_replace('#', '', $hex);
219
220
        if ( strlen($hex) == 3 ) {
221
			$r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) );
222
			$g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) );
223
			$b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) );
224
        } else {
225
			$r = hexdec( substr( $hex, 0, 2 ) );
226
			$g = hexdec( substr( $hex, 2, 2 ) );
227
			$b = hexdec( substr( $hex, 4, 2 ) );
228
        }
229
		$rgb = array( $r, $g, $b );
230
        return implode(',', $rgb); // returns the rgb values separated by commas
231
        //return $rgb; // returns an array with the rgb values
232
    }
233
234
	/**
235
	 * @param $hex string - The original color in hex format #ffffff
236
	 * @param $steps integer - should be between -255 and 255. Negative = darker, positive = lighter
237
	 * @since 2.3
238
	 */
239
	public static function adjust_brightness( $hex, $steps ) {
240
		$steps = max( -255, min( 255, $steps ) );
241
242
		// Normalize into a six character long hex string
243
		$hex = str_replace( '#', '', $hex );
244
		if ( strlen( $hex ) == 3 ) {
245
			$hex = str_repeat( substr( $hex, 0, 1 ), 2 );
246
			$hex .= str_repeat( substr( $hex, 1, 1 ), 2 );
247
			$hex .= str_repeat( substr( $hex, 2, 1 ), 2 );
248
		}
249
250
		// Split into three parts: R, G and B
251
		$color_parts = str_split( $hex, 2 );
252
		$return = '#';
253
254
		foreach ( $color_parts as $color ) {
255
			$color   = hexdec( $color ); // Convert to decimal
256
			$color   = max( 0,min( 255,$color + $steps ) ); // Adjust color
257
			$return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code
258
		}
259
260
		return $return;
261
	}
262
263
	/**
264
	 * @since 2.3
265
	 */
266
	public static function get_settings_for_output( $style ) {
267
		if ( self::previewing_style() ) {
268
			if ( isset( $_GET['frm_style_setting'] ) ) {
269
				$settings = $_GET['frm_style_setting']['post_content'];
270
			} else {
271
				$settings = $_GET;
272
			}
273
			FrmAppHelper::sanitize_value( 'sanitize_text_field', $settings );
274
275
			$style_name = FrmAppHelper::simple_get( 'style_name', 'sanitize_title' );
276
			$settings['style_class'] = '';
277
			if ( ! empty( $style_name ) ) {
278
				$settings['style_class'] = $style_name . '.';
279
			}
280
		} else {
281
			$settings = $style->post_content;
282
			$settings['style_class'] = 'frm_style_' . $style->post_name . '.';
283
		}
284
285
		$settings['style_class'] .= 'with_frm_style';
286
		$settings['font'] = stripslashes( $settings['font'] );
287
		$settings['change_margin'] = self::description_margin_for_screensize( $settings['width'] );
288
289
		$checkbox_opts = array( 'important_style', 'auto_width', 'submit_style', 'collapse_icon', 'center_form' );
290
		foreach ( $checkbox_opts as $opt ) {
291
			if ( ! isset( $settings[ $opt ] ) ) {
292
				$settings[ $opt ] = 0;
293
			}
294
		}
295
296
		self::prepare_color_output( $settings );
297
298
		return $settings;
299
	}
300
301
	/**
302
	 * @since 2.3
303
	 */
304
	private static function prepare_color_output( &$settings ) {
305
		$colors = self::allow_color_override();
306
		foreach ( $colors as $css => $opts ) {
307
			foreach ( $opts as $opt ) {
308
				self::get_color_output( $css, $settings[ $opt ] );
309
			}
310
		}
311
	}
312
313
	/**
314
	 * @since 2.3
315
	 */
316
	private static function allow_color_override() {
317
		return array(
318
			'transparent' => array(
319
				'fieldset_color', 'fieldset_bg_color', 'bg_color',
320
				'bg_color_disabled', 'bg_color_active', 'bg_color_error',
321
				'section_bg_color', 'error_bg', 'success_bg_color',
322
				'progress_bg_color', 'progress_active_bg_color',
323
			),
324
			'' => array(
325
				'title_color', 'section_color', 'submit_text_color',
326
				'label_color', 'check_label_color', 'form_desc_color',
327
				'description_color', 'text_color', 'text_color_disabled',
328
				'border_color', 'submit_bg_color', 'submit_border_color',
329
				'error_text', 'progress_border_color', 'progress_color',
330
				'progress_active_color',
331
				'submit_hover_bg_color', 'submit_hover_border_color', 'submit_hover_color',
332
				'submit_active_color', 'submit_active_border_color', 'submit_active_bg_color',
333
			),
334
		);
335
	}
336
337
	/**
338
	 * @since 2.3
339
	 */
340
	private static function get_color_output( $default, &$color ) {
341
		$color = ( trim( $color ) == '' ) ? $default : '#' . $color;
342
	}
343
344
	/**
345
	 * If left/right label is over a certain size,
346
	 * adjust the field description margin at a different screen size
347
	 * @since 2.3
348
	 */
349
	private static function description_margin_for_screensize( $width ) {
350
		$temp_label_width = str_replace( 'px', '', $width );
351
		$change_margin = false;
352
		if ( $temp_label_width >= 230 ) {
353
			$change_margin = '800px';
354
		} else if ( $width >= 215 ) {
355
			$change_margin = '700px';
356
		} else if ( $width >= 180 ) {
357
			$change_margin = '650px';
358
		}
359
		return $change_margin;
360
	}
361
362
	/**
363
	 * @since 2.3
364
	 */
365
	public static function previewing_style() {
366
		return isset( $_GET['frm_style_setting'] ) || isset( $_GET['flat'] );
367
	}
368
}
369