| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\GlobalClasses\Concerns; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
trait Has_Preview_Context { |
| 10 |
private bool $is_preview = false; |
| 11 |
|
| 12 |
public function set_preview( bool $is_preview = true ): self { |
| 13 |
if ( $is_preview === $this->is_preview ) { |
| 14 |
return $this; |
| 15 |
} |
| 16 |
|
| 17 |
$this->is_preview = $is_preview; |
| 18 |
$this->on_preview_change(); |
| 19 |
|
| 20 |
return $this; |
| 21 |
} |
| 22 |
|
| 23 |
protected function is_preview(): bool { |
| 24 |
return $this->is_preview; |
| 25 |
} |
| 26 |
|
| 27 |
protected function get_context_key( string $key ): string { |
| 28 |
$map = $this->get_context_keys()[ $key ] ?? null; |
| 29 |
|
| 30 |
if ( null === $map ) { |
| 31 |
throw new \InvalidArgumentException( sprintf( 'Unknown context key: %s', esc_html( $key ) ) ); |
| 32 |
} |
| 33 |
|
| 34 |
return $this->is_preview ? $map['preview'] : $map['frontend']; |
| 35 |
} |
| 36 |
|
| 37 |
protected function get_context_keys(): array { |
| 38 |
if ( empty( $this->context_keys ) ) { |
| 39 |
return []; |
| 40 |
} |
| 41 |
|
| 42 |
return $this->context_keys; |
| 43 |
} |
| 44 |
|
| 45 |
protected function on_preview_change(): void { |
| 46 |
} |
| 47 |
} |
| 48 |
|