Background_Position.php
76 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles CSS output for background-position. |
| 4 | * |
| 5 | * @package Kirki |
| 6 | * @subpackage Controls |
| 7 | * @copyright Copyright (c) 2023, Themeum |
| 8 | * @license https://opensource.org/licenses/MIT |
| 9 | * @since 2.2.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Kirki\Module\CSS\Property; |
| 13 | |
| 14 | use Kirki\Module\CSS\Property; |
| 15 | |
| 16 | /** |
| 17 | * Output overrides. |
| 18 | */ |
| 19 | class Background_Position extends Property { |
| 20 | |
| 21 | /** |
| 22 | * Modifies the value. |
| 23 | * |
| 24 | * @access protected |
| 25 | */ |
| 26 | protected function process_value() { |
| 27 | $this->value = trim( $this->value ); |
| 28 | |
| 29 | // If you use calc() there, I suppose you know what you're doing. |
| 30 | // No need to process this any further, just exit. |
| 31 | if ( false !== strpos( $this->value, 'calc' ) ) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | // If the value is initial or inherit, we don't need to do anything. |
| 36 | // Just exit. |
| 37 | if ( 'initial' === $this->value || 'inherit' === $this->value ) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | $x_dimensions = [ 'left', 'center', 'right' ]; |
| 42 | $y_dimensions = [ 'top', 'center', 'bottom' ]; |
| 43 | |
| 44 | // If there's a space, we have an X and a Y value. |
| 45 | if ( false !== strpos( $this->value, ' ' ) ) { |
| 46 | $xy = explode( ' ', $this->value ); |
| 47 | |
| 48 | $x = trim( $xy[0] ); |
| 49 | $y = trim( $xy[1] ); |
| 50 | |
| 51 | // If x is not left/center/right, we need to sanitize it. |
| 52 | if ( ! in_array( $x, $x_dimensions, true ) ) { |
| 53 | $x = sanitize_text_field( $x ); |
| 54 | } |
| 55 | if ( ! in_array( $y, $y_dimensions, true ) ) { |
| 56 | $y = sanitize_text_field( $y ); |
| 57 | } |
| 58 | $this->value = $x . ' ' . $y; |
| 59 | return; |
| 60 | } |
| 61 | $x = 'center'; |
| 62 | foreach ( $x_dimensions as $x_dimension ) { |
| 63 | if ( false !== strpos( $this->value, $x_dimension ) ) { |
| 64 | $x = $x_dimension; |
| 65 | } |
| 66 | } |
| 67 | $y = 'center'; |
| 68 | foreach ( $y_dimensions as $y_dimension ) { |
| 69 | if ( false !== strpos( $this->value, $y_dimension ) ) { |
| 70 | $y = $y_dimension; |
| 71 | } |
| 72 | } |
| 73 | $this->value = $x . ' ' . $y; |
| 74 | } |
| 75 | } |
| 76 |