Completed
Push — master ( fd1ed9...cc771e )
by Stephanie
02:50
created

FrmStylesHelper::hex2rgb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
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 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_single_label_positions() {
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(
197
				'-' => 'down',
198
				'+' => 'up',
199
			);
200
			$class = 'frm_arrow' . $arrow[ $icon ];
201
		} else {
202
			//frm_minus1_icon
203
			$key = str_replace( 'p', '', $key );
204
			$plus = array(
205
				'-' => 'minus',
206
				'+' => 'plus',
207
			);
208
			$class = 'frm_' . $plus[ $icon ];
209
		}
210
211
        if ( $key ) {
212
            $class .= $key;
213
        }
214
        $class .= '_icon';
215
216
        return $class;
217
    }
218
219
	public static function bs_icon_select( $style, $frm_style, $type = 'arrow' ) {
220
		$function_name = $type . '_icons';
221
		$icons = self::$function_name();
222
		unset( $function_name );
223
224
        $name = ( 'arrow' == $type ) ? 'collapse_icon' : 'repeat_icon';
225
?>
226
    	<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">
227
            <?php foreach ( $icons as $key => $icon ) { ?>
228
			<option value="<?php echo esc_attr( $key ) ?>" <?php selected( $style->post_content[ $name ], $key ) ?>>
229
				<?php echo '&#xe' . $icon['+'] . '; &#xe' . $icon['-'] . ';'; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$icon'
Loading history...
230
            </option>
231
            <?php } ?>
232
    	</select>
233
234
        <div class="btn-group hide-if-no-js" id="frm_<?php echo esc_attr( $name ) ?>_select">
235
            <button class="multiselect dropdown-toggle btn btn-default" data-toggle="dropdown" type="button">
236
				<i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $style->post_content[ $name ], '+', $type ) ) ?>"></i>
237
				<i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $style->post_content[ $name ], '-', $type ) ) ?>"></i>
238
                <b class="caret"></b>
239
            </button>
240
            <ul class="multiselect-container frm-dropdown-menu">
241
                <?php foreach ( $icons as $key => $icon ) { ?>
242
                <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...
243
                    <a href="javascript:void(0);">
244
                        <label>
245
                            <input type="radio" value="<?php echo esc_attr( $key ) ?>"/>
246
                            <span>
247
                                <i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $key, '+', $type ) ) ?>"></i>
248
                                <i class="frm_icon_font <?php echo esc_attr( self::icon_key_to_class( $key, '-', $type ) ) ?>"></i>
249
                            </span>
250
                        </label>
251
                    </a>
252
                </li>
253
                <?php } ?>
254
            </ul>
255
        </div>
256
<?php
257
    }
258
259
	public static function hex2rgb( $hex ) {
260
		$hex = str_replace( '#', '', $hex );
261
262
		list( $r, $g, $b ) = sscanf( $hex, '%02x%02x%02x' );
263
264
		$rgb = array( $r, $g, $b );
265
		return implode( ',', $rgb );
266
	}
267
268
	/**
269
	 * @param $hex string - The original color in hex format #ffffff
270
	 * @param $steps integer - should be between -255 and 255. Negative = darker, positive = lighter
271
	 * @since 2.3
272
	 */
273
	public static function adjust_brightness( $hex, $steps ) {
274
		$steps = max( -255, min( 255, $steps ) );
275
276
		// Normalize into a six character long hex string
277
		$hex = str_replace( '#', '', $hex );
278
		if ( strlen( $hex ) == 3 ) {
279
			$hex = str_repeat( substr( $hex, 0, 1 ), 2 );
280
			$hex .= str_repeat( substr( $hex, 1, 1 ), 2 );
281
			$hex .= str_repeat( substr( $hex, 2, 1 ), 2 );
282
		}
283
284
		// Split into three parts: R, G and B
285
		$color_parts = str_split( $hex, 2 );
286
		$return = '#';
287
288
		foreach ( $color_parts as $color ) {
289
			$color   = hexdec( $color ); // Convert to decimal
290
			$color   = max( 0, min( 255, $color + $steps ) ); // Adjust color
291
			$return .= str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT ); // Make two char hex code
292
		}
293
294
		return $return;
295
	}
296
297
	/**
298
	 * @since 2.3
299
	 */
300
	public static function get_settings_for_output( $style ) {
301
		if ( self::previewing_style() ) {
302
			if ( isset( $_GET['frm_style_setting'] ) ) {
303
				$settings = $_GET['frm_style_setting']['post_content'];
304
			} else {
305
				$settings = $_GET;
306
			}
307
			FrmAppHelper::sanitize_value( 'sanitize_text_field', $settings );
308
309
			$style_name = FrmAppHelper::simple_get( 'style_name', 'sanitize_title' );
310
			$settings['style_class'] = '';
311
			if ( ! empty( $style_name ) ) {
312
				$settings['style_class'] = $style_name . '.';
313
			}
314
		} else {
315
			$settings = $style->post_content;
316
			$settings['style_class'] = 'frm_style_' . $style->post_name . '.';
317
		}
318
319
		$settings['style_class'] .= 'with_frm_style';
320
		$settings['font'] = stripslashes( $settings['font'] );
321
		$settings['change_margin'] = self::description_margin_for_screensize( $settings['width'] );
322
323
		$checkbox_opts = array( 'important_style', 'auto_width', 'submit_style', 'collapse_icon', 'center_form' );
324
		foreach ( $checkbox_opts as $opt ) {
325
			if ( ! isset( $settings[ $opt ] ) ) {
326
				$settings[ $opt ] = 0;
327
			}
328
		}
329
330
		self::prepare_color_output( $settings );
331
332
		return $settings;
333
	}
334
335
	/**
336
	 * @since 2.3
337
	 */
338
	private static function prepare_color_output( &$settings ) {
339
		$colors = self::allow_color_override();
340
		foreach ( $colors as $css => $opts ) {
341
			foreach ( $opts as $opt ) {
342
				self::get_color_output( $css, $settings[ $opt ] );
343
			}
344
		}
345
	}
346
347
	/**
348
	 * @since 2.3
349
	 */
350
	private static function allow_color_override() {
351
		return array(
352
			'transparent' => array( 'fieldset_color', 'fieldset_bg_color', 'bg_color', 'section_bg_color', 'error_bg', 'success_bg_color', 'progress_bg_color', 'progress_active_bg_color' ),
353
			'' => 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' ),
354
		);
355
	}
356
357
	/**
358
	 * @since 2.3
359
	 */
360
	private static function get_color_output( $default, &$color ) {
361
		$color = ( trim( $color ) == '' ) ? $default : '#' . $color;
362
	}
363
364
	/**
365
	 * If left/right label is over a certain size,
366
	 * adjust the field description margin at a different screen size
367
	 * @since 2.3
368
	 */
369
	private static function description_margin_for_screensize( $width ) {
370
		$temp_label_width = str_replace( 'px', '', $width );
371
		$change_margin = false;
372
		if ( $temp_label_width >= 230 ) {
373
			$change_margin = '800px';
374
		} else if ( $width >= 215 ) {
375
			$change_margin = '700px';
376
		} else if ( $width >= 180 ) {
377
			$change_margin = '650px';
378
		}
379
		return $change_margin;
380
	}
381
382
	/**
383
	 * @since 2.3
384
	 */
385
	public static function previewing_style() {
386
		return isset( $_GET['frm_style_setting'] ) || isset( $_GET['flat'] );
387
	}
388
389
	/**
390
	 * @deprecated 3.0.07
391
	 * @codeCoverageIgnore
392
	 */
393
	public static function get_sigle_label_postitions() {
394
		_deprecated_function( __METHOD__, '3.0.07', 'FrmStylesHelper::get_single_label_positions' );
395
		return FrmStylesHelper::get_single_label_positions();
396
	}
397
}
398