View.php
139 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Tribe\Tooltip; |
| 4 | |
| 5 | /** |
| 6 | * Class View |
| 7 | * |
| 8 | * @since 4.9.8 |
| 9 | */ |
| 10 | class View extends \Tribe__Template { |
| 11 | |
| 12 | /** |
| 13 | * Where in the themes we will look for templates |
| 14 | * |
| 15 | * @since 4.10.2 |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | public $template_namespace = 'tooltips'; |
| 20 | |
| 21 | /** |
| 22 | * View constructor. |
| 23 | * |
| 24 | * @since 4.9.8 |
| 25 | */ |
| 26 | public function __construct() { |
| 27 | $this->set_template_origin( \Tribe__Main::instance() ); |
| 28 | $this->set_template_folder( 'src/views/tooltip' ); |
| 29 | |
| 30 | // Configures this templating class to extract variables |
| 31 | $this->set_template_context_extract( true ); |
| 32 | |
| 33 | // Uses the public folders |
| 34 | $this->set_template_folder_lookup( true ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Public wrapper for build method |
| 39 | * |
| 40 | * @since 4.9.8 |
| 41 | * |
| 42 | * @param array|string $message Array of messages or single message as string. |
| 43 | * @param array $args { |
| 44 | * List of arguments to override tooltip template. |
| 45 | * |
| 46 | * @var array $context Any additional context data you need to expose to this file (optional). |
| 47 | * @var string $classes Additional classes for the icon span (optional). |
| 48 | * @var string $direction Direction the tooltip should be from the trigger (down). |
| 49 | * @var string $icon dashicon classname to use, without the `dashicon-` (info). |
| 50 | * @var string $wrap_classes Classes for the tooltip wrapper (optional). |
| 51 | * } |
| 52 | * @return string A string of html for the tooltip. |
| 53 | */ |
| 54 | public function render_tooltip( $message, $args = [] ) { |
| 55 | if ( empty( $message ) ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | /** @var \Tribe__Assets $assets */ |
| 60 | $assets = tribe( 'assets' ); |
| 61 | $assets->enqueue_group( 'tribe-tooltip' ); |
| 62 | |
| 63 | $html = $this->build_tooltip( $message, $args ); |
| 64 | |
| 65 | return $html; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Factory method for tooltip HTML |
| 70 | * |
| 71 | * @since 4.9.8 |
| 72 | * |
| 73 | * @param array|string $message array of messages or single message as string. |
| 74 | * @param array $args { |
| 75 | * List of arguments to override tooltip template. |
| 76 | * |
| 77 | * @var array $context Any additional context data you need to expose to this file (optional). |
| 78 | * @var string $classes Additional classes for the icon span (optional). |
| 79 | * @var string $direction Direction the tooltip should be from the trigger (down). |
| 80 | * @var string $icon dashicon classname to use, without the `dashicon-` (info). |
| 81 | * @var string $wrap_classes Classes for the tooltip wrapper (optional). |
| 82 | * } |
| 83 | * @return string A string of html for the tooltip. |
| 84 | */ |
| 85 | private function build_tooltip( $message, $original_args ) { |
| 86 | $default_args = [ |
| 87 | 'classes' => '', |
| 88 | 'context' => '', |
| 89 | 'direction' => 'down', |
| 90 | 'icon' => 'info', |
| 91 | 'wrap_classes' => '', |
| 92 | ]; |
| 93 | |
| 94 | $args = wp_parse_args( $original_args, $default_args ); |
| 95 | |
| 96 | // Check for message to be passed. |
| 97 | if ( empty( $message ) ) { |
| 98 | return ''; |
| 99 | } |
| 100 | |
| 101 | // Setup message as an array of messages |
| 102 | $messages = (array) $message; |
| 103 | |
| 104 | $args['messages'] = $messages; |
| 105 | |
| 106 | ob_start(); |
| 107 | |
| 108 | /** |
| 109 | * Allow us to filter the tooltip template |
| 110 | * |
| 111 | * @since 4.9.8 |
| 112 | * |
| 113 | * @param string $template The tooltip template name. |
| 114 | * @param array $args Extra arguments, defaults include icon, classes, direction, and context. |
| 115 | */ |
| 116 | $template_name = apply_filters( 'tribe_tooltip_template', 'tooltip', $args ); |
| 117 | |
| 118 | $template = $this->template( $template_name, $args, false ); |
| 119 | |
| 120 | if ( ! empty( $template ) ) { |
| 121 | echo $template; |
| 122 | } |
| 123 | |
| 124 | $html = ob_get_clean(); |
| 125 | |
| 126 | /** |
| 127 | * Allow us to filter the tooltip output |
| 128 | * |
| 129 | * @since 4.9.8 |
| 130 | * |
| 131 | * @param string $html The tooltip HTML. |
| 132 | * @param array $messages An array of message strings. |
| 133 | * @param array $args Extra arguments, defaults include icon, classes, direction, and context. |
| 134 | */ |
| 135 | return apply_filters( 'tribe_tooltip_html', $html, $messages, $args ); |
| 136 | } |
| 137 | |
| 138 | } |
| 139 |