| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Styles; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
const FONTS_KEY_PREFIX = 'elementor_atomic_styles_fonts-'; |
| 10 |
|
| 11 |
class Style_Fonts { |
| 12 |
private string $style_key; |
| 13 |
|
| 14 |
public function __construct( string $style_key ) { |
| 15 |
$this->style_key = $style_key; |
| 16 |
} |
| 17 |
|
| 18 |
public static function make( string $style_key ) { |
| 19 |
return new static( $style_key ); |
| 20 |
} |
| 21 |
|
| 22 |
public function add( string $font ) { |
| 23 |
$style_fonts = $this->get_fonts(); |
| 24 |
|
| 25 |
if ( ! in_array( $font, $style_fonts, true ) ) { |
| 26 |
$style_fonts[] = $font; |
| 27 |
|
| 28 |
$this->update_fonts( $style_fonts ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
public function get(): array { |
| 33 |
return $this->get_fonts(); |
| 34 |
} |
| 35 |
|
| 36 |
public function clear() { |
| 37 |
$this->update_fonts( [] ); |
| 38 |
} |
| 39 |
|
| 40 |
private function get_fonts(): array { |
| 41 |
$style_fonts_key = $this->get_key(); |
| 42 |
return get_option( $style_fonts_key, [] ); |
| 43 |
} |
| 44 |
|
| 45 |
private function update_fonts( array $fonts ) { |
| 46 |
$style_fonts_key = $this->get_key(); |
| 47 |
|
| 48 |
if ( empty( $fonts ) ) { |
| 49 |
delete_option( $style_fonts_key ); |
| 50 |
|
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
update_option( $style_fonts_key, $fonts, false ); |
| 55 |
} |
| 56 |
|
| 57 |
private function get_key(): string { |
| 58 |
return FONTS_KEY_PREFIX . $this->style_key; |
| 59 |
} |
| 60 |
} |
| 61 |
|