Notice
2 weeks ago
Activation_Page.php
2 weeks ago
Help_Page.php
2 weeks ago
Helpers.php
2 weeks ago
Live_Date_Preview.php
2 weeks ago
Notices.php
2 weeks ago
Live_Date_Preview.php
69 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Facilitiates live date previews in the Events > Settings > Display admin screen. |
| 4 | */ |
| 5 | class Tribe__Admin__Live_Date_Preview { |
| 6 | protected $target_fields = [ |
| 7 | 'dateWithYearFormat', |
| 8 | 'dateWithoutYearFormat', |
| 9 | 'monthAndYearFormat', |
| 10 | 'weekDayFormat', |
| 11 | ]; |
| 12 | |
| 13 | /** |
| 14 | * Static Singleton Holder |
| 15 | * |
| 16 | * @var self |
| 17 | */ |
| 18 | protected static $instance; |
| 19 | |
| 20 | /** |
| 21 | * Static Singleton Factory Method |
| 22 | * |
| 23 | * @return self |
| 24 | */ |
| 25 | public static function instance() { |
| 26 | return self::$instance ? self::$instance : self::$instance = new self; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Adds live date previews to the display settings tab (nothing is setup unless |
| 31 | * the user is actually on that tab). |
| 32 | */ |
| 33 | public function __construct() { |
| 34 | add_action( 'tribe_settings_after_do_tabs', [ $this, 'listen' ] ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * If the user looking at the Display settings tab, adds live date preview facilities. |
| 39 | */ |
| 40 | public function listen() { |
| 41 | // We are only interested in the "Display" tab |
| 42 | if ( 'display' !== Tribe__Settings::instance()->currentTab ) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Add or remove fields which should have live date/time preview facilities. |
| 48 | * |
| 49 | * @var array $target_fields |
| 50 | */ |
| 51 | $this->target_fields = (array) apply_filters( 'tribe_settings_date_preview_fields', $this->target_fields ); |
| 52 | |
| 53 | add_filter( 'tribe_field_div_end', [ $this, 'setup_date_previews' ], 10, 2 ); |
| 54 | |
| 55 | // We are still before `admin_enqueue_scripts` making it safe to use `tribe_asset` |
| 56 | tribe_asset( Tribe__Main::instance(), 'tribe-date-live-refresh', 'admin-date-preview.js', [ 'jquery' ], 'admin_enqueue_scripts' ); |
| 57 | } |
| 58 | |
| 59 | public function setup_date_previews( $html, $field ) { |
| 60 | // Not one of the fields we're interested in? Return without modification |
| 61 | if ( ! in_array( $field->id, $this->target_fields ) ) { |
| 62 | return $html; |
| 63 | } |
| 64 | |
| 65 | $preview = esc_html( date_i18n( $field->value ) ); |
| 66 | return " <code class='live-date-preview'> $preview </code> $html"; |
| 67 | } |
| 68 | } |
| 69 |