| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmStyle { |
| 7 |
public $number = false; // Unique ID number of the current instance. |
| 8 |
public $id = 0; // the id of the post |
| 9 |
|
| 10 |
/** |
| 11 |
* @param int|string $id The id of the stylsheet or 'default' |
| 12 |
*/ |
| 13 |
public function __construct( $id = 0 ) { |
| 14 |
$this->id = $id; |
| 15 |
} |
| 16 |
|
| 17 |
public function get_new() { |
| 18 |
$this->id = 0; |
| 19 |
|
| 20 |
$max_slug_value = 2147483647; |
| 21 |
$min_slug_value = 37; // we want to have at least 2 characters in the slug |
| 22 |
$key = base_convert( rand( $min_slug_value, $max_slug_value ), 10, 36 ); |
| 23 |
|
| 24 |
$style = array( |
| 25 |
'post_type' => FrmStylesController::$post_type, |
| 26 |
'ID' => '', |
| 27 |
'post_title' => __( 'New Style', 'formidable' ), |
| 28 |
'post_name' => $key, |
| 29 |
'post_content' => $this->get_defaults(), |
| 30 |
'menu_order' => '', |
| 31 |
'post_status' => 'publish', |
| 32 |
); |
| 33 |
|
| 34 |
return (object) $style; |
| 35 |
} |
| 36 |
|
| 37 |
public function save( $settings ) { |
| 38 |
return FrmDb::save_settings( $settings, 'frm_styles' ); |
| 39 |
} |
| 40 |
|
| 41 |
public function duplicate( $id ) { |
| 42 |
// duplicating is a pro feature |
| 43 |
} |
| 44 |
|
| 45 |
public function update( $id = 'default' ) { |
| 46 |
$all_instances = $this->get_all(); |
| 47 |
|
| 48 |
if ( empty( $id ) ) { |
| 49 |
$new_style = (array) $this->get_new(); |
| 50 |
$all_instances[] = $new_style; |
| 51 |
} |
| 52 |
|
| 53 |
$action_ids = array(); |
| 54 |
|
| 55 |
foreach ( $all_instances as $number => $new_instance ) { |
| 56 |
$new_instance = (array) $new_instance; |
| 57 |
$this->id = $new_instance['ID']; |
| 58 |
if ( $id != $this->id || ! $_POST || ! isset( $_POST['frm_style_setting'] ) ) { |
| 59 |
$all_instances[ $number ] = $new_instance; |
| 60 |
|
| 61 |
if ( $new_instance['menu_order'] && $_POST && empty( $_POST['prev_menu_order'] ) && isset( $_POST['frm_style_setting']['menu_order'] ) ) { |
| 62 |
// this style was set to default, so remove default setting on previous default style |
| 63 |
$new_instance['menu_order'] = 0; |
| 64 |
$action_ids[] = $this->save( $new_instance ); |
| 65 |
} |
| 66 |
|
| 67 |
// don't continue if not saving this style |
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
$new_instance['post_title'] = isset( $_POST['frm_style_setting']['post_title'] ) ? sanitize_text_field( wp_unslash( $_POST['frm_style_setting']['post_title'] ) ) : ''; |
| 72 |
|
| 73 |
// Don't wp_unslash yet since it removes backslashes. |
| 74 |
$new_instance['post_content'] = isset( $_POST['frm_style_setting']['post_content'] ) ? $_POST['frm_style_setting']['post_content'] : ''; // WPCS: sanitization ok. |
| 75 |
FrmAppHelper::sanitize_value( 'sanitize_textarea_field', $new_instance['post_content'] ); |
| 76 |
$new_instance['post_type'] = FrmStylesController::$post_type; |
| 77 |
$new_instance['post_status'] = 'publish'; |
| 78 |
$new_instance['menu_order'] = isset( $_POST['frm_style_setting']['menu_order'] ) ? absint( $_POST['frm_style_setting']['menu_order'] ) : 0; |
| 79 |
|
| 80 |
if ( empty( $id ) ) { |
| 81 |
$new_instance['post_name'] = $new_instance['post_title']; |
| 82 |
} |
| 83 |
|
| 84 |
$default_settings = $this->get_defaults(); |
| 85 |
|
| 86 |
foreach ( $default_settings as $setting => $default ) { |
| 87 |
if ( ! isset( $new_instance['post_content'][ $setting ] ) ) { |
| 88 |
$new_instance['post_content'][ $setting ] = $default; |
| 89 |
} |
| 90 |
|
| 91 |
if ( $this->is_color( $setting ) ) { |
| 92 |
$new_instance['post_content'][ $setting ] = str_replace( '#', '', $new_instance['post_content'][ $setting ] ); |
| 93 |
} elseif ( in_array( $setting, array( 'submit_style', 'important_style', 'auto_width' ) ) |
| 94 |
&& ! isset( $new_instance['post_content'][ $setting ] ) |
| 95 |
) { |
| 96 |
$new_instance['post_content'][ $setting ] = 0; |
| 97 |
} elseif ( $setting == 'font' ) { |
| 98 |
$new_instance['post_content'][ $setting ] = $this->force_balanced_quotation( $new_instance['post_content'][ $setting ] ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
$all_instances[ $number ] = $new_instance; |
| 103 |
|
| 104 |
$action_ids[] = $this->save( $new_instance ); |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
$this->save_settings(); |
| 109 |
|
| 110 |
return $action_ids; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @since 3.01.01 |
| 115 |
*/ |
| 116 |
private function is_color( $setting ) { |
| 117 |
$extra_colors = array( 'error_bg', 'error_border', 'error_text' ); |
| 118 |
|
| 119 |
return strpos( $setting, 'color' ) !== false || in_array( $setting, $extra_colors ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* @since 3.01.01 |
| 124 |
*/ |
| 125 |
public function get_color_settings() { |
| 126 |
$defaults = $this->get_defaults(); |
| 127 |
$settings = array_keys( $defaults ); |
| 128 |
|
| 129 |
return array_filter( $settings, array( $this, 'is_color' ) ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Create static css file |
| 134 |
*/ |
| 135 |
public function save_settings() { |
| 136 |
$filename = FrmAppHelper::plugin_path() . '/css/custom_theme.css.php'; |
| 137 |
update_option( 'frm_last_style_update', gmdate( 'njGi' ) ); |
| 138 |
|
| 139 |
if ( ! is_file( $filename ) ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
$this->clear_cache(); |
| 144 |
|
| 145 |
$css = $this->get_css_content( $filename ); |
| 146 |
|
| 147 |
$create_file = new FrmCreateFile( |
| 148 |
array( |
| 149 |
'file_name' => FrmStylesController::get_file_name(), |
| 150 |
'new_file_path' => FrmAppHelper::plugin_path() . '/css', |
| 151 |
) |
| 152 |
); |
| 153 |
$create_file->create_file( $css ); |
| 154 |
|
| 155 |
update_option( 'frmpro_css', $css, 'no' ); |
| 156 |
set_transient( 'frmpro_css', $css, MONTH_IN_SECONDS ); |
| 157 |
} |
| 158 |
|
| 159 |
private function get_css_content( $filename ) { |
| 160 |
$css = '/* ' . __( 'WARNING: Any changes made to this file will be lost when your Formidable settings are updated', 'formidable' ) . ' */' . "\n"; |
| 161 |
|
| 162 |
$saving = true; |
| 163 |
$frm_style = $this; |
| 164 |
|
| 165 |
ob_start(); |
| 166 |
include( $filename ); |
| 167 |
$css .= preg_replace( '/\/\*(.|\s)*?\*\//', '', str_replace( array( "\r\n", "\r", "\n", "\t", ' ' ), '', ob_get_contents() ) ); |
| 168 |
ob_end_clean(); |
| 169 |
|
| 170 |
return FrmStylesController::replace_relative_url( $css ); |
| 171 |
} |
| 172 |
|
| 173 |
private function clear_cache() { |
| 174 |
$default_post_atts = array( |
| 175 |
'post_type' => FrmStylesController::$post_type, |
| 176 |
'post_status' => 'publish', |
| 177 |
'numberposts' => 99, |
| 178 |
'orderby' => 'title', |
| 179 |
'order' => 'ASC', |
| 180 |
); |
| 181 |
|
| 182 |
FrmDb::delete_cache_and_transient( json_encode( $default_post_atts ), 'frm_styles' ); |
| 183 |
FrmDb::cache_delete_group( 'frm_styles' ); |
| 184 |
FrmDb::delete_cache_and_transient( 'frmpro_css' ); |
| 185 |
} |
| 186 |
|
| 187 |
public function destroy( $id ) { |
| 188 |
return wp_delete_post( $id ); |
| 189 |
} |
| 190 |
|
| 191 |
public function get_one() { |
| 192 |
if ( 'default' == $this->id ) { |
| 193 |
$style = $this->get_default_style(); |
| 194 |
if ( $style ) { |
| 195 |
$this->id = $style->ID; |
| 196 |
} else { |
| 197 |
$this->id = 0; |
| 198 |
} |
| 199 |
|
| 200 |
return $style; |
| 201 |
} |
| 202 |
|
| 203 |
$style = get_post( $this->id ); |
| 204 |
|
| 205 |
if ( ! $style ) { |
| 206 |
return $style; |
| 207 |
} |
| 208 |
|
| 209 |
$style->post_content = FrmAppHelper::maybe_json_decode( $style->post_content ); |
| 210 |
|
| 211 |
$default_values = $this->get_defaults(); |
| 212 |
|
| 213 |
// fill default values |
| 214 |
$style->post_content = $this->override_defaults( $style->post_content ); |
| 215 |
$style->post_content = wp_parse_args( $style->post_content, $default_values ); |
| 216 |
|
| 217 |
return $style; |
| 218 |
} |
| 219 |
|
| 220 |
public function get_all( $orderby = 'title', $order = 'ASC', $limit = 99 ) { |
| 221 |
$post_atts = array( |
| 222 |
'post_type' => FrmStylesController::$post_type, |
| 223 |
'post_status' => 'publish', |
| 224 |
'numberposts' => $limit, |
| 225 |
'orderby' => $orderby, |
| 226 |
'order' => $order, |
| 227 |
); |
| 228 |
|
| 229 |
$temp_styles = FrmDb::check_cache( json_encode( $post_atts ), 'frm_styles', $post_atts, 'get_posts' ); |
| 230 |
|
| 231 |
if ( empty( $temp_styles ) ) { |
| 232 |
global $wpdb; |
| 233 |
// make sure there wasn't a conflict with the query |
| 234 |
$query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->posts . ' WHERE post_type=%s AND post_status=%s ORDER BY post_title ASC LIMIT 99', FrmStylesController::$post_type, 'publish' ); |
| 235 |
$temp_styles = FrmDb::check_cache( 'frm_backup_style_check', 'frm_styles', $query, 'get_results' ); |
| 236 |
|
| 237 |
if ( empty( $temp_styles ) ) { |
| 238 |
// create a new style if there are none |
| 239 |
$new = $this->get_new(); |
| 240 |
$new->post_title = __( 'Formidable Style', 'formidable' ); |
| 241 |
$new->post_name = $new->post_title; |
| 242 |
$new->menu_order = 1; |
| 243 |
$new = $this->save( (array) $new ); |
| 244 |
$this->update( 'default' ); |
| 245 |
|
| 246 |
$post_atts['include'] = $new; |
| 247 |
|
| 248 |
$temp_styles = get_posts( $post_atts ); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
$default_values = $this->get_defaults(); |
| 253 |
$default_style = false; |
| 254 |
|
| 255 |
$styles = array(); |
| 256 |
foreach ( $temp_styles as $style ) { |
| 257 |
$this->id = $style->ID; |
| 258 |
if ( $style->menu_order ) { |
| 259 |
if ( $default_style ) { |
| 260 |
// only return one default |
| 261 |
$style->menu_order = 0; |
| 262 |
} else { |
| 263 |
// check for a default style |
| 264 |
$default_style = $style->ID; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
$style->post_content = FrmAppHelper::maybe_json_decode( $style->post_content ); |
| 269 |
|
| 270 |
// fill default values |
| 271 |
$style->post_content = $this->override_defaults( $style->post_content ); |
| 272 |
$style->post_content = wp_parse_args( $style->post_content, $default_values ); |
| 273 |
|
| 274 |
$styles[ $style->ID ] = $style; |
| 275 |
} |
| 276 |
|
| 277 |
if ( ! $default_style ) { |
| 278 |
$default_style = reset( $styles ); |
| 279 |
|
| 280 |
$styles[ $default_style->ID ]->menu_order = 1; |
| 281 |
} |
| 282 |
|
| 283 |
return $styles; |
| 284 |
} |
| 285 |
|
| 286 |
public function get_default_style( $styles = null ) { |
| 287 |
if ( ! isset( $styles ) ) { |
| 288 |
$styles = $this->get_all( 'menu_order', 'DESC', 1 ); |
| 289 |
} |
| 290 |
|
| 291 |
foreach ( $styles as $style ) { |
| 292 |
if ( $style->menu_order ) { |
| 293 |
return $style; |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
public function override_defaults( $settings ) { |
| 299 |
if ( ! is_array( $settings ) ) { |
| 300 |
return $settings; |
| 301 |
} |
| 302 |
|
| 303 |
$settings['line_height'] = ( ! isset( $settings['field_height'] ) || $settings['field_height'] == '' || $settings['field_height'] == 'auto' ) ? 'normal' : $settings['field_height']; |
| 304 |
|
| 305 |
if ( ! isset( $settings['form_desc_size'] ) && isset( $settings['description_font_size'] ) ) { |
| 306 |
$settings['form_desc_size'] = $settings['description_font_size']; |
| 307 |
$settings['form_desc_color'] = $settings['description_color']; |
| 308 |
$settings['title_color'] = $settings['label_color']; |
| 309 |
} |
| 310 |
|
| 311 |
if ( ! isset( $settings['section_color'] ) && isset( $settings['label_color'] ) ) { |
| 312 |
$settings['section_color'] = $settings['label_color']; |
| 313 |
$settings['section_border_color'] = $settings['border_color']; |
| 314 |
} |
| 315 |
|
| 316 |
if ( ! isset( $settings['submit_hover_bg_color'] ) && isset( $settings['submit_bg_color'] ) ) { |
| 317 |
$settings['submit_hover_bg_color'] = $settings['submit_bg_color']; |
| 318 |
$settings['submit_hover_color'] = $settings['submit_text_color']; |
| 319 |
$settings['submit_hover_border_color'] = $settings['submit_border_color']; |
| 320 |
|
| 321 |
$settings['submit_active_bg_color'] = $settings['submit_bg_color']; |
| 322 |
$settings['submit_active_color'] = $settings['submit_text_color']; |
| 323 |
$settings['submit_active_border_color'] = $settings['submit_border_color']; |
| 324 |
} |
| 325 |
|
| 326 |
return apply_filters( 'frm_override_default_styles', $settings ); |
| 327 |
} |
| 328 |
|
| 329 |
public function get_defaults() { |
| 330 |
$defaults = array( |
| 331 |
'theme_css' => 'ui-lightness', |
| 332 |
'theme_name' => 'UI Lightness', |
| 333 |
|
| 334 |
'center_form' => '', |
| 335 |
'form_width' => '100%', |
| 336 |
'form_align' => 'left', |
| 337 |
'direction' => is_rtl() ? 'rtl' : 'ltr', |
| 338 |
'fieldset' => '0px', |
| 339 |
'fieldset_color' => '000000', |
| 340 |
'fieldset_padding' => '0 0 15px 0', |
| 341 |
'fieldset_bg_color' => '', |
| 342 |
|
| 343 |
'title_size' => '40px', |
| 344 |
'title_color' => '444444', |
| 345 |
'title_margin_top' => '10px', |
| 346 |
'title_margin_bottom' => '60px', |
| 347 |
'form_desc_size' => '14px', |
| 348 |
'form_desc_color' => '666666', |
| 349 |
'form_desc_margin_top' => '10px', |
| 350 |
'form_desc_margin_bottom' => '25px', |
| 351 |
'form_desc_padding' => '0', |
| 352 |
|
| 353 |
'font' => '"Lucida Grande","Lucida Sans Unicode",Tahoma,sans-serif', |
| 354 |
'font_size' => '15px', |
| 355 |
'label_color' => '3f4b5b', |
| 356 |
'weight' => 'normal', |
| 357 |
'position' => 'none', |
| 358 |
'align' => 'left', |
| 359 |
'width' => '150px', |
| 360 |
'required_color' => 'B94A48', |
| 361 |
'required_weight' => 'bold', |
| 362 |
'label_padding' => '0 0 3px 0', |
| 363 |
|
| 364 |
'description_font_size' => '12px', |
| 365 |
'description_color' => '666666', |
| 366 |
'description_weight' => 'normal', |
| 367 |
'description_style' => 'normal', |
| 368 |
'description_align' => 'left', |
| 369 |
'description_margin' => '0', |
| 370 |
|
| 371 |
'field_font_size' => '14px', |
| 372 |
'field_height' => '32px', |
| 373 |
'line_height' => 'normal', |
| 374 |
'field_width' => '100%', |
| 375 |
'auto_width' => false, |
| 376 |
'field_pad' => '6px 10px', |
| 377 |
'field_margin' => '20px', |
| 378 |
'field_weight' => 'normal', |
| 379 |
'text_color' => '555555', |
| 380 |
//'border_color_hv' => 'cccccc', |
| 381 |
'border_color' => 'BFC3C8', |
| 382 |
'field_border_width' => '1px', |
| 383 |
'field_border_style' => 'solid', |
| 384 |
|
| 385 |
'bg_color' => 'ffffff', |
| 386 |
//'bg_color_hv' => 'ffffff', |
| 387 |
'remove_box_shadow' => '', |
| 388 |
'bg_color_active' => 'ffffff', |
| 389 |
'border_color_active' => '66afe9', |
| 390 |
'remove_box_shadow_active' => '', |
| 391 |
'text_color_error' => '444444', |
| 392 |
'bg_color_error' => 'ffffff', |
| 393 |
'border_color_error' => 'B94A48', |
| 394 |
'border_width_error' => '1px', |
| 395 |
'border_style_error' => 'solid', |
| 396 |
'bg_color_disabled' => 'ffffff', |
| 397 |
'border_color_disabled' => 'E5E5E5', |
| 398 |
'text_color_disabled' => 'A1A1A1', |
| 399 |
|
| 400 |
'radio_align' => 'block', |
| 401 |
'check_align' => 'block', |
| 402 |
'check_font_size' => '13px', |
| 403 |
'check_label_color' => '444444', |
| 404 |
'check_weight' => 'normal', |
| 405 |
|
| 406 |
'section_font_size' => '18px', |
| 407 |
'section_color' => '444444', |
| 408 |
'section_weight' => 'bold', |
| 409 |
'section_pad' => '15px 0 3px 0', |
| 410 |
'section_mar_top' => '15px', |
| 411 |
'section_mar_bottom' => '30px', |
| 412 |
'section_bg_color' => '', |
| 413 |
'section_border_color' => 'e8e8e8', |
| 414 |
'section_border_width' => '2px', |
| 415 |
'section_border_style' => 'solid', |
| 416 |
'section_border_loc' => '-top', |
| 417 |
'collapse_icon' => '6', |
| 418 |
'collapse_pos' => 'after', |
| 419 |
'repeat_icon' => '1', |
| 420 |
'repeat_icon_color' => 'ffffff', |
| 421 |
|
| 422 |
'submit_style' => false, |
| 423 |
'submit_font_size' => '15px', |
| 424 |
'submit_width' => 'auto', |
| 425 |
'submit_height' => 'auto', |
| 426 |
'submit_bg_color' => '579AF6', |
| 427 |
'submit_border_color' => '579AF6', |
| 428 |
'submit_border_width' => '1px', |
| 429 |
'submit_text_color' => 'ffffff', |
| 430 |
'submit_weight' => 'normal', |
| 431 |
'submit_border_radius' => '4px', |
| 432 |
'submit_bg_img' => '', |
| 433 |
'submit_margin' => '10px', |
| 434 |
'submit_padding' => '10px 20px', |
| 435 |
'submit_shadow_color' => 'eeeeee', |
| 436 |
'submit_hover_bg_color' => 'efefef', |
| 437 |
'submit_hover_color' => '444444', |
| 438 |
'submit_hover_border_color' => 'cccccc', |
| 439 |
'submit_active_bg_color' => 'efefef', |
| 440 |
'submit_active_color' => '444444', |
| 441 |
'submit_active_border_color' => 'cccccc', |
| 442 |
|
| 443 |
'border_radius' => '4px', |
| 444 |
'error_bg' => 'F2DEDE', |
| 445 |
'error_border' => 'EBCCD1', |
| 446 |
'error_text' => 'B94A48', |
| 447 |
'error_font_size' => '14px', |
| 448 |
|
| 449 |
'success_bg_color' => 'DFF0D8', |
| 450 |
'success_border_color' => 'D6E9C6', |
| 451 |
'success_text_color' => '468847', |
| 452 |
'success_font_size' => '14px', |
| 453 |
|
| 454 |
'important_style' => false, |
| 455 |
|
| 456 |
'progress_bg_color' => 'eaeaea', |
| 457 |
'progress_active_color' => 'ffffff', |
| 458 |
'progress_active_bg_color' => '579AF6', |
| 459 |
'progress_color' => '3f4b5b', |
| 460 |
'progress_border_color' => 'E5E5E5', |
| 461 |
'progress_border_size' => '2px', |
| 462 |
'progress_size' => '30px', |
| 463 |
|
| 464 |
'custom_css' => '', |
| 465 |
); |
| 466 |
|
| 467 |
return apply_filters( 'frm_default_style_settings', $defaults ); |
| 468 |
} |
| 469 |
|
| 470 |
public function get_field_name( $field_name, $post_field = 'post_content' ) { |
| 471 |
return 'frm_style_setting' . ( empty( $post_field ) ? '' : '[' . $post_field . ']' ) . '[' . $field_name . ']'; |
| 472 |
} |
| 473 |
|
| 474 |
public static function get_bold_options() { |
| 475 |
return array( |
| 476 |
100 => 100, |
| 477 |
200 => 200, |
| 478 |
300 => 300, |
| 479 |
'normal' => __( 'normal', 'formidable' ), |
| 480 |
500 => 500, |
| 481 |
600 => 600, |
| 482 |
'bold' => __( 'bold', 'formidable' ), |
| 483 |
800 => 800, |
| 484 |
900 => 900, |
| 485 |
); |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Don't let imbalanced font families ruin the whole stylesheet |
| 490 |
*/ |
| 491 |
public function force_balanced_quotation( $value ) { |
| 492 |
$balanced_characters = array( '"', "'" ); |
| 493 |
foreach ( $balanced_characters as $char ) { |
| 494 |
$char_count = substr_count( $value, $char ); |
| 495 |
$is_balanced = $char_count % 2 == 0; |
| 496 |
if ( ! $is_balanced ) { |
| 497 |
$value .= $char; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
return $value; |
| 502 |
} |
| 503 |
} |
| 504 |
|