| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Jet_Style\Css_Compilers; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 7 |
|
| 8 |
// If this file is called directly, abort. |
| 9 |
if ( ! defined( 'WPINC' ) ) { |
| 10 |
die; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* @since 3.1.0 |
| 15 |
* |
| 16 |
* Class Border_Css_Compiler |
| 17 |
* @package Jet_Form_Builder\Compatibility\Wp_Experiments\Css_Compilers |
| 18 |
*/ |
| 19 |
class Border_Css_Compiler extends Base_Css_Compiler { |
| 20 |
|
| 21 |
const SIDES = array( 'top', 'right', 'bottom', 'left' ); |
| 22 |
|
| 23 |
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 24 |
public function is_supported( array $path ): bool { |
| 25 |
return 'border' === Array_Tools::last( $path ); |
| 26 |
} |
| 27 |
|
| 28 |
public function compile_declarations( |
| 29 |
\WP_Style_Engine_CSS_Declarations $declarations, |
| 30 |
array $root_styles, |
| 31 |
array &$class_names |
| 32 |
) { |
| 33 |
$base_border = Array_Tools::get( $root_styles, $this->path, array() ); |
| 34 |
|
| 35 |
if ( empty( $base_border ) || ! is_array( $base_border ) ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$is_hover = $this->has_hover_path(); |
| 40 |
|
| 41 |
foreach ( array( 'style', 'width', 'color' ) as $type ) { |
| 42 |
$top_value = $this->get_side_value( $base_border, 'top', $type ); |
| 43 |
|
| 44 |
$declarations->add_declaration( |
| 45 |
$this->get_top_var( $type ), |
| 46 |
$top_value |
| 47 |
); |
| 48 |
|
| 49 |
$right_value = $this->get_side_value( $base_border, 'right', $type ); |
| 50 |
|
| 51 |
$declarations->add_declaration( |
| 52 |
$this->get_right_var( $type ), |
| 53 |
$right_value |
| 54 |
); |
| 55 |
|
| 56 |
$bottom_value = $this->get_side_value( $base_border, 'bottom', $type ); |
| 57 |
|
| 58 |
$declarations->add_declaration( |
| 59 |
$this->get_bottom_var( $type ), |
| 60 |
$bottom_value |
| 61 |
); |
| 62 |
|
| 63 |
$left_value = $this->get_side_value( $base_border, 'left', $type ); |
| 64 |
|
| 65 |
$declarations->add_declaration( |
| 66 |
$this->get_left_var( $type ), |
| 67 |
$left_value |
| 68 |
); |
| 69 |
|
| 70 |
if ( ! $is_hover ) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
// get not empty values |
| 75 |
$values = array_filter( array( $top_value, $right_value, $bottom_value, $left_value ) ); |
| 76 |
|
| 77 |
if ( count( $values ) ) { |
| 78 |
$class_names[] = 'has-hover-border-' . $type; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function compile_class_names( array &$class_names, array $root_styles ) { |
| 84 |
// do nothing |
| 85 |
} |
| 86 |
|
| 87 |
public function is_linked( $value ): bool { |
| 88 |
$first_key = array_keys( $value )[0] ?? ''; |
| 89 |
|
| 90 |
return in_array( $first_key, self::SIDES, true ); |
| 91 |
} |
| 92 |
|
| 93 |
public function get_side_value( $value, string $side, $property ): string { |
| 94 |
if ( ! in_array( $side, self::SIDES, true ) ) { |
| 95 |
return ''; |
| 96 |
} |
| 97 |
|
| 98 |
if ( $this->is_linked( $value ) ) { |
| 99 |
$response = $value[ $side ][ $property ] ?? ''; |
| 100 |
} else { |
| 101 |
$response = $value[ $property ] ?? ''; |
| 102 |
} |
| 103 |
|
| 104 |
return trim( $response ); |
| 105 |
} |
| 106 |
|
| 107 |
public function get_top_var( $type ): string { |
| 108 |
return "{$this->css_var}-top-{$type}"; |
| 109 |
} |
| 110 |
|
| 111 |
public function get_right_var( $type ): string { |
| 112 |
return "{$this->css_var}-right-{$type}"; |
| 113 |
} |
| 114 |
|
| 115 |
public function get_bottom_var( $type ): string { |
| 116 |
return "{$this->css_var}-bottom-{$type}"; |
| 117 |
} |
| 118 |
|
| 119 |
public function get_left_var( $type ): string { |
| 120 |
return "{$this->css_var}-left-{$type}"; |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|