| @@ -47,9 +47,9 @@ | ||
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | 49 | * @param stdClass|WP_Post $active_style |
| 50 | 50 | * @param WP_Post $default_style |
| 51 | - * @param string|int $form_id | |
| 51 | + * @param int|string $form_id | |
| 52 | 52 | * @param bool $enabled |
| 53 | 53 | */ |
| 54 | 54 | public function __construct( $active_style, $default_style, $form_id, $enabled ) { |
| 55 | 55 | $this->view_file_path = FrmAppHelper::plugin_path() . '/classes/views/styles/_style-card.php'; |
| @@ -67,8 +67,9 @@ | ||
| 67 | 67 | * @since 6.0 |
| 68 | 68 | * |
| 69 | 69 | * @param stdClass|WP_Post $style |
| 70 | 70 | * @param bool $hidden Used for pagination. |
| 71 | + * | |
| 71 | 72 | * @return void |
| 72 | 73 | */ |
| 73 | 74 | private function echo_style_card( $style, $hidden = false ) { |
| 74 | 75 | $params = $this->get_params_for_style_card( $style, $hidden ); |
| @@ -85,8 +86,9 @@ | ||
| 85 | 86 | * @since 6.0 |
| 86 | 87 | * |
| 87 | 88 | * @param stdClass|WP_Post $style |
| 88 | 89 | * @param bool $hidden |
| 90 | + * | |
| 89 | 91 | * @return array |
| 90 | 92 | */ |
| 91 | 93 | private function get_params_for_style_card( $style, $hidden = false ) { |
| 92 | 94 | if ( ! empty( $style->post_content['position'] ) ) { |
| @@ -92,10 +94,9 @@ | ||
| 92 | 94 | if ( ! empty( $style->post_content['position'] ) ) { |
| 93 | 95 | $label_position = $style->post_content['position']; |
| 94 | 96 | } else { |
| 95 | 97 | $frm_style = new FrmStyle(); |
| 96 | - $defaults = $frm_style->get_defaults(); | |
| 97 | - $label_position = $defaults['position']; | |
| 98 | + $label_position = $frm_style->get_defaults()['position']; | |
| 98 | 99 | } |
| 99 | 100 | |
| 100 | 101 | $class_name = 'frm_style_' . $style->post_name; |
| 101 | 102 | $params = array( |
| @@ -107,14 +108,17 @@ | ||
| 107 | 108 | 'data-label-position' => $label_position, |
| 108 | 109 | ); |
| 109 | 110 | |
| 110 | 111 | $is_active_style = $style->ID === $this->active_style->ID; |
| 112 | + | |
| 111 | 113 | if ( $is_active_style ) { |
| 112 | 114 | $params['class'] .= ' frm-active-style-card frm-currently-set-style-card'; |
| 113 | 115 | } |
| 116 | + | |
| 114 | 117 | if ( $hidden ) { |
| 115 | 118 | $params['class'] .= ' frm_hidden'; |
| 116 | 119 | } |
| 120 | + | |
| 117 | 121 | if ( self::has_dark_background( $style ) ) { |
| 118 | 122 | $params['class'] .= ' frm-dark-style'; |
| 119 | 123 | } |
| 120 | 124 | |
| @@ -124,8 +128,9 @@ | ||
| 124 | 128 | * @since 6.0 |
| 125 | 129 | * |
| 126 | 130 | * @param array $params |
| 127 | 131 | * @param array $args { |
| 132 | + * | |
| 128 | 133 | * @type WP_Post $style |
| 129 | 134 | * } |
| 130 | 135 | */ |
| 131 | 136 | return apply_filters( 'frm_style_card_params', $params, compact( 'style' ) ); |
| @@ -131,9 +136,10 @@ | ||
| 131 | 136 | return apply_filters( 'frm_style_card_params', $params, compact( 'style' ) ); |
| 132 | 137 | } |
| 133 | 138 | |
| 134 | 139 | /** |
| 135 | - * @param WP_Post|stdClass $style | |
| 140 | + * @param stdClass|WP_Post $style | |
| 141 | + * | |
| 136 | 142 | * @return bool |
| 137 | 143 | */ |
| 138 | 144 | private static function has_dark_background( $style ) { |
| 139 | 145 | $key = 'fieldset_bg_color'; |
| @@ -143,14 +149,15 @@ | ||
| 143 | 149 | } |
| 144 | 150 | |
| 145 | 151 | $color = $style->post_content[ $key ]; |
| 146 | 152 | |
| 147 | - if ( 0 === strpos( $color, 'rgba' ) ) { | |
| 153 | + if ( str_starts_with( $color, 'rgba' ) ) { | |
| 148 | 154 | preg_match_all( '/([\\d.]+)/', $color, $matches ); |
| 149 | 155 | |
| 150 | 156 | if ( isset( $matches[1][3] ) && is_numeric( $matches[1][3] ) ) { |
| 151 | 157 | // Consider a faded out rgba value as light even when the color is dark. |
| 152 | 158 | $color_opacity = floatval( $matches[1][3] ); |
| 159 | + | |
| 153 | 160 | if ( $color_opacity < 0.5 ) { |
| 154 | 161 | return false; |
| 155 | 162 | } |
| 156 | 163 | } |
| @@ -162,14 +169,18 @@ | ||
| 162 | 169 | |
| 163 | 170 | /** |
| 164 | 171 | * @since 6.0 |
| 165 | 172 | * |
| 166 | - * @param array $style API style data { | |
| 173 | + * @param array $style { | |
| 174 | + * API style data. | |
| 175 | + * | |
| 167 | 176 | * @type array $settings |
| 168 | 177 | * @type string $name |
| 169 | 178 | * @type string $slug |
| 170 | 179 | * } |
| 180 | + * | |
| 171 | 181 | * @param bool $hidden |
| 182 | + * | |
| 172 | 183 | * @return bool True if the template was valid and echoed. |
| 173 | 184 | */ |
| 174 | 185 | private function echo_card_template( $style, $hidden = false ) { |
| 175 | 186 | if ( empty( $style['settings'] ) || ! is_array( $style['settings'] ) ) { |
| @@ -193,14 +204,16 @@ | ||
| 193 | 204 | * Set up a locked style card for the upgrade modal. |
| 194 | 205 | * |
| 195 | 206 | * @param array $params |
| 196 | 207 | * @param array $args { |
| 197 | - * @type WP_Post|stdClass $style | |
| 208 | + * | |
| 209 | + * @type stdClass|WP_Post $style | |
| 198 | 210 | * } |
| 211 | + * | |
| 199 | 212 | * @param stdClass $style_object |
| 200 | 213 | * @param array $style |
| 201 | 214 | */ |
| 202 | - $param_filter = function( $params, $args ) use ( $style_object, $style ) { | |
| 215 | + $param_filter = function ( $params, $args ) use ( $style_object, $style ) { | |
| 203 | 216 | if ( $args['style'] !== $style_object ) { |
| 204 | 217 | return $params; |
| 205 | 218 | } |
| 206 | 219 | |
| @@ -220,15 +233,16 @@ | ||
| 220 | 233 | * Include the template key for the preview in Pro. |
| 221 | 234 | * |
| 222 | 235 | * @param array $params |
| 223 | 236 | * @param array $style |
| 237 | + * | |
| 224 | 238 | * @return array |
| 225 | 239 | */ |
| 226 | - $param_filter = function( $params ) use ( $style ) { | |
| 240 | + $param_filter = function ( $params ) use ( $style ) { | |
| 227 | 241 | $params['data-template-key'] = $style['slug']; |
| 228 | 242 | return $params; |
| 229 | 243 | }; |
| 230 | - } | |
| 244 | + }//end if | |
| 231 | 245 | |
| 232 | 246 | $this->is_new_template = ! empty( $style['is_new'] ); |
| 233 | 247 | |
| 234 | 248 | add_filter( 'frm_style_card_params', $param_filter, 10, $this->locked ? 2 : 1 ); |
| @@ -243,10 +257,11 @@ | ||
| 243 | 257 | * It's kept static as it only requires a $style as input and also gets called when resetting a style. |
| 244 | 258 | * |
| 245 | 259 | * @since 6.0 |
| 246 | 260 | * |
| 247 | - * @param WP_Post|stdClass $style A new style (including duplicated styles) is not a WP_Post object. | |
| 261 | + * @param stdClass|WP_Post $style A new style (including duplicated styles) is not a WP_Post object. | |
| 248 | 262 | * Template cards also use an stdClss instead of a WP_Post object. |
| 263 | + * | |
| 249 | 264 | * @return string |
| 250 | 265 | */ |
| 251 | 266 | public static function get_style_param_for_card( $style ) { |
| 252 | 267 | $styles = array(); |
| @@ -254,10 +269,11 @@ | ||
| 254 | 269 | // Add the background color setting for fieldsets to the card. |
| 255 | 270 | if ( empty( $style->post_content['fieldset_bg_color'] ) ) { |
| 256 | 271 | $background_color = '#fff'; |
| 257 | 272 | } else { |
| 258 | - $background_color = ( 0 === strpos( $style->post_content['fieldset_bg_color'], 'rgb' ) ? $style->post_content['fieldset_bg_color'] : '#' . $style->post_content['fieldset_bg_color'] ); | |
| 273 | + $background_color = str_starts_with( $style->post_content['fieldset_bg_color'], 'rgb' ) ? $style->post_content['fieldset_bg_color'] : '#' . $style->post_content['fieldset_bg_color']; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 259 | 274 | } |
| 275 | + | |
| 260 | 276 | $styles[] = '--preview-background-color: ' . $background_color; |
| 261 | 277 | |
| 262 | 278 | if ( empty( $style->post_content['submit_border_color'] ) ) { |
| 263 | 279 | $style->post_content['submit_border_color'] = 'transparent'; |
| @@ -264,9 +280,8 @@ | ||
| 264 | 280 | } |
| 265 | 281 | |
| 266 | 282 | // Apply additional styles from the style. |
| 267 | 283 | $rules_to_apply = self::get_style_keys_for_card(); |
| 268 | - | |
| 269 | 284 | $frm_style = new FrmStyle(); |
| 270 | 285 | $color_settings = $frm_style->get_color_settings(); |
| 271 | 286 | |
| 272 | 287 | foreach ( $rules_to_apply as $key ) { |
| @@ -274,11 +289,11 @@ | ||
| 274 | 289 | // A template from the API may not include every style key. If something is missing, skip it. |
| 275 | 290 | continue; |
| 276 | 291 | } |
| 277 | 292 | |
| 278 | - $value = $style->post_content[ $key ]; | |
| 293 | + $value = $style->post_content[ $key ]; | |
| 294 | + $is_hex = in_array( $key, $color_settings, true ) && $value && '#' !== $value[0] && ! str_contains( $value, 'rgb' ) && $value !== 'transparent'; | |
| 279 | 295 | |
| 280 | - $is_hex = in_array( $key, $color_settings, true ) && $value && '#' !== $value[0] && false === strpos( $value, 'rgb' ) && $value !== 'transparent'; | |
| 281 | 296 | if ( $is_hex ) { |
| 282 | 297 | $value = '#' . $value; |
| 283 | 298 | } |
| 284 | 299 | |
| @@ -332,8 +347,9 @@ | ||
| 332 | 347 | 'id' => $id, |
| 333 | 348 | 'class' => 'frm-style-card-wrapper with_frm_style', |
| 334 | 349 | 'style' => $wrapper_style, |
| 335 | 350 | ); |
| 351 | + | |
| 336 | 352 | if ( $this->enabled ) { |
| 337 | 353 | $card_wrapper_params['class'] .= ' frm-styles-enabled'; |
| 338 | 354 | } |
| 339 | 355 | |
| @@ -338,8 +354,9 @@ | ||
| 338 | 354 | } |
| 339 | 355 | |
| 340 | 356 | $first_style = reset( $styles ); |
| 341 | 357 | $is_template_wrapper = is_array( $first_style ); |
| 358 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 342 | 359 | ?> |
| 343 | 360 | <div <?php FrmAppHelper::array_to_html_params( $card_wrapper_params, true ); ?>> |
| 344 | 361 | <?php |
| 345 | 362 | if ( $is_template_wrapper ) { |
| @@ -349,8 +366,9 @@ | ||
| 349 | 366 | } |
| 350 | 367 | ?> |
| 351 | 368 | </div> |
| 352 | 369 | <?php |
| 370 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 353 | 371 | } |
| 354 | 372 | |
| 355 | 373 | /** |
| 356 | 374 | * Overwrite some styles. We want to make sure the sizes are normalized for the cards. |
| @@ -380,8 +398,9 @@ | ||
| 380 | 398 | /** |
| 381 | 399 | * @since 6.0 |
| 382 | 400 | * |
| 383 | 401 | * @param array<array> $styles |
| 402 | + * | |
| 384 | 403 | * @return void |
| 385 | 404 | */ |
| 386 | 405 | private function echo_template_cards( $styles ) { |
| 387 | 406 | array_walk( |
| @@ -391,11 +410,12 @@ | ||
| 391 | 410 | * |
| 392 | 411 | * @param array|string $style |
| 393 | 412 | * @param string $key The key for the API data. It may be a numeric ID, or a key like "active_sub" or "expires". |
| 394 | 413 | * @param int $count Used for pagination. |
| 414 | + * | |
| 395 | 415 | * @return void |
| 396 | 416 | */ |
| 397 | - function( $style, $key ) use ( &$count ) { | |
| 417 | + function ( $style, $key ) { | |
| 398 | 418 | if ( ! is_numeric( $key ) ) { |
| 399 | 419 | // Skip active_sub/expires keys. |
| 400 | 420 | return; |
| 401 | 421 | } |
| @@ -406,8 +426,9 @@ | ||
| 406 | 426 | } |
| 407 | 427 | |
| 408 | 428 | /** |
| 409 | 429 | * @param array<WP_Post> $styles |
| 430 | + * | |
| 410 | 431 | * @return void |
| 411 | 432 | */ |
| 412 | 433 | private function echo_custom_cards( $styles ) { |
| 413 | 434 | $count = 0; |
| @@ -414,16 +435,17 @@ | ||
| 414 | 435 | $this->locked = false; |
| 415 | 436 | array_walk( |
| 416 | 437 | $styles, |
| 417 | 438 | /** |
| 418 | - * Echo a style card for a single style in the $styles array. | |
| 419 | - * | |
| 420 | - * @param WP_Post $style | |
| 421 | - * @param int $count Used for pagination. | |
| 422 | - * @return void | |
| 423 | - */ | |
| 424 | - function( $style ) use ( &$count ) { | |
| 425 | - $hidden = $count > ( self::PAGE_SIZE - 1 ); | |
| 439 | + * Echo a style card for a single style in the $styles array. | |
| 440 | + * | |
| 441 | + * @param WP_Post $style | |
| 442 | + * @param int $count Used for pagination. | |
| 443 | + * | |
| 444 | + * @return void | |
| 445 | + */ | |
| 446 | + function ( $style ) use ( &$count ) { | |
| 447 | + $hidden = $count > self::PAGE_SIZE - 1; | |
| 426 | 448 | $this->echo_style_card( $style, $hidden ); |
| 427 | 449 | ++$count; |
| 428 | 450 | } |
| 429 | 451 | ); |
| @@ -434,8 +456,9 @@ | ||
| 434 | 456 | /** |
| 435 | 457 | * @since 6.0 |
| 436 | 458 | * |
| 437 | 459 | * @param int $count |
| 460 | + * | |
| 438 | 461 | * @return void |
| 439 | 462 | */ |
| 440 | 463 | private function maybe_echo_card_pagination( $count ) { |
| 441 | 464 | if ( $count <= self::PAGE_SIZE ) { |
| @@ -441,8 +464,9 @@ | ||
| 441 | 464 | if ( $count <= self::PAGE_SIZE ) { |
| 442 | 465 | // Not enough cards to require pagination. |
| 443 | 466 | return; |
| 444 | 467 | } |
| 468 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 445 | 469 | ?> |
| 446 | 470 | <div class="frm-style-card-pagination frm_wrap"> |
| 447 | 471 | <a href="#" class="frm-show-all-styles"> |
| 448 | 472 | <?php |
| @@ -454,8 +478,9 @@ | ||
| 454 | 478 | ?> |
| 455 | 479 | </a> |
| 456 | 480 | </div> |
| 457 | 481 | <?php |
| 482 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 458 | 483 | } |
| 459 | 484 | |
| 460 | 485 | /** |
| 461 | 486 | * @since 6.0 |
| @@ -472,8 +497,9 @@ | ||
| 472 | 497 | /** |
| 473 | 498 | * Remove the default style from an array of styles. |
| 474 | 499 | * |
| 475 | 500 | * @param array $styles |
| 501 | + * | |
| 476 | 502 | * @return array |
| 477 | 503 | */ |
| 478 | 504 | public function filter_custom_styles( $styles ) { |
| 479 | 505 | return array_filter( |
| @@ -479,11 +505,12 @@ | ||
| 479 | 505 | return array_filter( |
| 480 | 506 | $styles, |
| 481 | 507 | /** |
| 482 | 508 | * @param WP_Post $style |
| 509 | + * | |
| 483 | 510 | * @return bool |
| 484 | 511 | */ |
| 485 | - function( $style ) { | |
| 512 | + function ( $style ) { | |
| 486 | 513 | return $this->default_style->ID !== $style->ID; |
| 487 | 514 | } |
| 488 | 515 | ); |
| 489 | 516 | } |