class_wd_help_tooltips.php
463 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Very simple tooltip implementation for admin pages. |
| 4 | * |
| 5 | * Example usage: |
| 6 | * <code> |
| 7 | * if (!class_exists('WpmuDev_HelpTooltips')) require_once YOUR_PLUGIN_BASE_DIR . '/lib/external/class_wd_help_tooltips.php'; |
| 8 | * $tips = new WpmuDev_HelpTooltips(); |
| 9 | * $tips->set_icon_url("URL_TO_YOUR_ICON"); |
| 10 | * echo $tips->add_tip("Tip 1 text here"); |
| 11 | * // ... |
| 12 | * echo $tips->add_tip("Tip 2 text here"); |
| 13 | * </code> |
| 14 | * This is a basic usage scenario. |
| 15 | * |
| 16 | * Alternative usage example: |
| 17 | * <code> |
| 18 | * if (!class_exists('WpmuDev_HelpTooltips')) require_once YOUR_PLUGIN_BASE_DIR . '/lib/external/class_wd_help_tooltips.php'; |
| 19 | * $tips = new WpmuDev_HelpTooltips(); |
| 20 | * $tips->set_icon_url("URL_TO_YOUR_ICON"); |
| 21 | * $tips->bind_tip('My tip text here', '.icon32:first ~h2'); |
| 22 | * // Note that you don't echo anything in this usage example - the tip will be |
| 23 | * // added automatically next to the supplied selector (second argument) |
| 24 | * </code> |
| 25 | * This scenario may be useful for e.g. adding our tips to UI elements created by WP or other plugins. |
| 26 | * You can freely alternate between add_tip() and bind_tip() methods, |
| 27 | * just remember to echo add_tip() and *not* echo bind_tip(). |
| 28 | * |
| 29 | * Another alternative usage example, setting tips for multiple pages in one place: |
| 30 | * <code> |
| 31 | * if (!class_exists('WpmuDev_HelpTooltips')) require_once YOUR_PLUGIN_BASE_DIR . '/lib/external/class_wd_help_tooltips.php'; |
| 32 | * // Tips added to $tips1 object will only show on Social Marketing add/edit advert page |
| 33 | * $tips1 = new WpmuDev_HelpTooltips(); |
| 34 | * $tips1->set_icon_url("URL_TO_YOUR_ICON"); |
| 35 | * $tips1->set_screen_id("social_marketing_ad"); |
| 36 | * $tips1->bind_tip('My tip 1 text here', '.icon32:first ~h2'); |
| 37 | * // ... |
| 38 | * // Tips added to $tips2 object will only show on Social Marketing getting started page |
| 39 | * $tips2 = new WpmuDev_HelpTooltips(); |
| 40 | * $tips2->set_icon_url("URL_TO_YOUR_ICON"); |
| 41 | * $tips2->set_screen_id("social_marketing_ad_page_wdsm-get_started"); |
| 42 | * $tips2->bind_tip('My tip 2 text here', '.icon32:first ~h2'); |
| 43 | * </code> |
| 44 | * Using add_tip() method will do nothing in this last scenario, as it doesn't make sense in that context. |
| 45 | * This scenario may be useful for adding tooltips to all pages in one central location. |
| 46 | * E.g. for adding tooltips in a plugin add-on. |
| 47 | */ |
| 48 | |
| 49 | if(!class_exists('WpmuDev_HelpTooltips')) { |
| 50 | class WpmuDev_HelpTooltips { |
| 51 | |
| 52 | /** |
| 53 | * Holds an array of inline tips: used as dependency inclusion switch. |
| 54 | */ |
| 55 | private $_inline_tips = array(); |
| 56 | |
| 57 | /** |
| 58 | * Holds an array of bound tips: used as dependency inclusion switch and bound tips buffer. |
| 59 | */ |
| 60 | private $_bound_tips = array(); |
| 61 | |
| 62 | /** |
| 63 | * Holds an array of bound tips selectors: used as bound tips selectors buffer. |
| 64 | */ |
| 65 | private $_bound_selectors = array(); |
| 66 | |
| 67 | /** |
| 68 | * Full URL to help icon, which is used as tip anchor |
| 69 | * and as notice background image. |
| 70 | */ |
| 71 | private $_icon_url; |
| 72 | |
| 73 | /** |
| 74 | * Flag that determines do we want to use notices |
| 75 | * (tips expanded on click). |
| 76 | * Defaults to true. |
| 77 | */ |
| 78 | private $_use_notice = true; |
| 79 | |
| 80 | /** |
| 81 | * Limits tip output to a screen (page). |
| 82 | * Optional. |
| 83 | * Works best with bind_tip() method. |
| 84 | */ |
| 85 | private $_screen_id = false; |
| 86 | |
| 87 | /** |
| 88 | * Bind to footer hooks when instantiated. |
| 89 | */ |
| 90 | public function __construct () { |
| 91 | global $wp_version; |
| 92 | $version = preg_replace('/-.*$/', '', $wp_version); |
| 93 | |
| 94 | if (version_compare($version, '3.3', '>=')) { |
| 95 | add_action('admin_footer', array($this, 'add_bound_tips'), 999); |
| 96 | add_action('admin_print_footer_scripts', array($this, 'initialize')); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets icon URL. |
| 102 | * @param string $icon_url Full URL to help anchor icon |
| 103 | */ |
| 104 | public function set_icon_url ($icon_url) { |
| 105 | $this->_icon_url = $icon_url; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Set show notices (tips expanded on click) flag. |
| 110 | * @param bool $use_notice True to use notices (default), false otherwise. |
| 111 | */ |
| 112 | public function set_use_notice ($use_notice=true) { |
| 113 | $this->_use_notice = $use_notice; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Set screen limiting flag. |
| 118 | * @param $screen_id Screen ID that tips in this object apply to. |
| 119 | */ |
| 120 | public function set_screen_id ($screen_id) { |
| 121 | $this->_screen_id = $screen_id; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns inline tip markup. |
| 126 | * Scenario: for echoing inline tips next to elements on the page. |
| 127 | * Usage example: |
| 128 | * <code> |
| 129 | * echo $tips->add_tip('My tip text here'); |
| 130 | * </code> |
| 131 | * @param string $tip Tip text |
| 132 | * @return string Inline tip markup |
| 133 | */ |
| 134 | public function add_tip ($tip) { |
| 135 | if (!$this->_check_screen()) return false; |
| 136 | $this->_inline_tips[] = $tip; |
| 137 | return $this->_get_tip_markup($tip); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Binds a tip to selector. |
| 142 | * This is different from inline tips, as you don't have to output them yourself. |
| 143 | * Scenario: for adding help tips next to elements determined by the selector on page load time. |
| 144 | * Usage example: |
| 145 | * <code> |
| 146 | * $tips->bind_tip('My tip text here', '.icon32:first ~h2'); |
| 147 | * </code> |
| 148 | * @param string $tip Tip text |
| 149 | * @param string $selector jQuery selector of the element that tip is related to. |
| 150 | */ |
| 151 | public function bind_tip ($tip, $bind_to_selector) { |
| 152 | $tip_id = 'wpmudev-help-tip-for-' . md5($bind_to_selector); |
| 153 | $this->_bound_tips[$tip_id] = $tip; |
| 154 | $this->_bound_selectors[$tip_id] = $bind_to_selector; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Bounded tips injection handler. |
| 159 | * Will queue up the bounded tips. |
| 160 | */ |
| 161 | function add_bound_tips () { |
| 162 | if (!$this->_check_screen()) return false; |
| 163 | if (!$this->_bound_tips) return false; |
| 164 | |
| 165 | foreach ($this->_bound_tips as $id => $tip) { |
| 166 | echo $this->_get_tip_markup($tip, 'id="' . $id . '" style=display:none'); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Dependency injection handler. |
| 172 | * Will only add dependencies if there are actual tooltips to show. |
| 173 | */ |
| 174 | function initialize () { |
| 175 | if (!$this->_check_screen()) return false; |
| 176 | if (!$this->_inline_tips && !$this->_bound_tips) return false; |
| 177 | |
| 178 | $this->_print_styles(); |
| 179 | $this->_print_scripts(); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Screen limitation check. |
| 184 | * @return bool True if we're good to go, false if we're on a wrong screen. |
| 185 | */ |
| 186 | private function _check_screen () { |
| 187 | if (!$this->_screen_id) return true; // No screen dependency |
| 188 | |
| 189 | $screen = get_current_screen(); |
| 190 | if (!is_object($screen)) return false; // Actually older then 3.3 |
| 191 | if ($this->_screen_id != @$screen->id) return false; // Not for this screen |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | private function _get_tip_markup ($tip, $arg='') { |
| 197 | return "<span class='wpmudev-help' {$arg}>{$tip}</span>"; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Private helper method that prints style dependencies. |
| 202 | */ |
| 203 | private function _print_styles () { |
| 204 | // Have we already done this? |
| 205 | if (!defined('WPMUDEV_TOOLTIPS_CSS_ADDED')) define('WPMUDEV_TOOLTIPS_CSS_ADDED', true); |
| 206 | else return false; |
| 207 | |
| 208 | ?> |
| 209 | <style type="text/css"> |
| 210 | .wpmudev-help { |
| 211 | display: block; |
| 212 | background-color: #ffffe0; |
| 213 | border: 1px solid #e6db55; |
| 214 | padding: 1em 1em; |
| 215 | -moz-border-radius:3px; |
| 216 | -khtml-border-radius:3px; |
| 217 | -webkit-border-radius:3px; |
| 218 | border-radius:3px; |
| 219 | } |
| 220 | <?php |
| 221 | if ($this->_icon_url) { |
| 222 | ?> |
| 223 | .wpmudev-help { |
| 224 | background: url(<?php echo $this->_icon_url; ?>) no-repeat scroll 10px center #ffffe0; |
| 225 | padding-left: 40px; |
| 226 | } |
| 227 | .wpmudev-help-trigger span { |
| 228 | display: block; |
| 229 | position: absolute; |
| 230 | left: -12000000px; |
| 231 | } |
| 232 | .wpmudev-help-trigger { |
| 233 | position: relative; |
| 234 | background: url(<?php echo $this->_icon_url; ?>) no-repeat scroll center bottom transparent; |
| 235 | padding: 1px 12px; |
| 236 | text-decoration: none; |
| 237 | } |
| 238 | <?php |
| 239 | } |
| 240 | ?> |
| 241 | #wpmudev-tooltip-source { |
| 242 | margin: 0 13px; |
| 243 | padding: 8px; |
| 244 | |
| 245 | background: #fff; |
| 246 | border-style: solid; |
| 247 | border-width: 1px; |
| 248 | /* Fallback for non-rgba-compliant browsers */ |
| 249 | border-color: #dfdfdf; |
| 250 | /* Use rgba to look better against non-white backgrounds. */ |
| 251 | border-color: rgba(0,0,0,.125); |
| 252 | -webkit-border-radius: 3px; |
| 253 | border-radius: 3px; |
| 254 | |
| 255 | -webkit-box-shadow: 0 2px 4px rgba(0,0,0,.19); |
| 256 | -moz-box-shadow: 0 2px 4px rgba(0,0,0,.19); |
| 257 | box-shadow: 0 2px 4px rgba(0,0,0,.19); |
| 258 | } |
| 259 | .wpmudev-left_pointer { |
| 260 | float: left; |
| 261 | width: 14px; |
| 262 | height: 30px; |
| 263 | margin-top: 8px; |
| 264 | background: url(<?php echo site_url("/wp-includes/images/arrow-pointer-blue.png");?>) 0 -15px no-repeat; |
| 265 | } |
| 266 | .wpmudev-right_pointer { |
| 267 | float: right; |
| 268 | width: 14px; |
| 269 | height: 30px; |
| 270 | margin-top: 8px; |
| 271 | background: url(<?php echo site_url("/wp-includes/images/arrow-pointer-blue.png");?>) -16px -15px no-repeat; |
| 272 | } |
| 273 | </style> |
| 274 | <?php |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Private helper method that prints javascript dependencies. |
| 279 | */ |
| 280 | private function _print_scripts () { |
| 281 | // Have we already done this? |
| 282 | if (!defined('WPMUDEV_TOOLTIPS_JS_ADDED')) define('WPMUDEV_TOOLTIPS_JS_ADDED', true); |
| 283 | else return false; |
| 284 | |
| 285 | // Initialize bound selectors |
| 286 | $selectors = json_encode($this->_bound_selectors); |
| 287 | |
| 288 | ?> |
| 289 | <script type="text/javascript"> |
| 290 | (function ($) { |
| 291 | |
| 292 | /** |
| 293 | * Converts help text placeholders to tooltip items. |
| 294 | */ |
| 295 | function initialize_help_item ($me) { |
| 296 | var $prev = $me.prev(); |
| 297 | var help = ' <a class="wpmudev-help-trigger" href="#help"><span><?php _e('Help');?></span></a>'; |
| 298 | $prev = $prev.length ? |
| 299 | $prev.after(help) |
| 300 | : |
| 301 | $me.before(help) |
| 302 | ; |
| 303 | $me.hide(); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Finds a help block corresponding to trigger. |
| 308 | */ |
| 309 | function get_help_block ($me) { |
| 310 | var $parent = $me.parent(); |
| 311 | return $parent.find('.wpmudev-help'); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Handles help block visibility. |
| 316 | */ |
| 317 | function show_help_block ($me) { |
| 318 | var $help = get_help_block($me); |
| 319 | if (!$help.length) return false; |
| 320 | |
| 321 | if ($("#wpmudev-tooltip").length) $("#wpmudev-tooltip").remove(); |
| 322 | if ($help.is(":visible")) $help.hide('fast'); |
| 323 | else $help.show('fast'); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Pops tooltip open. |
| 328 | */ |
| 329 | function open_tooltip ($me) { |
| 330 | var $help = get_help_block($me); |
| 331 | if ($help.is(":visible")) return false; |
| 332 | |
| 333 | if ($("#wpmudev-tooltip").length) $("#wpmudev-tooltip").remove(); |
| 334 | if (!$("#wpmudev-tooltip").length) $("body").append('<div id="wpmudev-tooltip"><div class="wpmudev-pointer wpmudev-left_pointer"></div><div id="wpmudev-tooltip-source"></div></div>'); |
| 335 | var $tip = $("#wpmudev-tooltip"); |
| 336 | if (!$tip.length) return false; |
| 337 | |
| 338 | var width = 200; |
| 339 | var margin = 20; |
| 340 | var src_pos = $me.offset(); |
| 341 | |
| 342 | var top_pos = src_pos.top + ($me.height() / 2); |
| 343 | var left_pos = src_pos.left + margin; |
| 344 | var $pointer = $tip.find(".wpmudev-pointer"); |
| 345 | |
| 346 | // Setup left/right orientation |
| 347 | <?php if (!is_rtl()) { ?> |
| 348 | if ((left_pos+width+60) >= $(window).width()) { |
| 349 | left_pos = src_pos.left - ($me.outerWidth()+width+margin); |
| 350 | $pointer |
| 351 | .removeClass("wpmudev-left_pointer") |
| 352 | .addClass("wpmudev-right_pointer") |
| 353 | ; |
| 354 | } |
| 355 | <?php } else { ?> |
| 356 | var min_left = left_pos - (width+60); |
| 357 | if (min_left > 0) { |
| 358 | left_pos = min_left; |
| 359 | $pointer |
| 360 | .removeClass("wpmudev-left_pointer") |
| 361 | .addClass("wpmudev-right_pointer") |
| 362 | ; |
| 363 | } |
| 364 | <?php } ?> |
| 365 | |
| 366 | // IE safeguard |
| 367 | if ($.browser.msie) { |
| 368 | var $pointer_left = $tip.find(".wpmudev-left_pointer"); |
| 369 | if ($pointer_left.length) $pointer_left.css("position", "absolute"); |
| 370 | } |
| 371 | |
| 372 | $tip |
| 373 | // Populate tip text |
| 374 | .find("#wpmudev-tooltip-source") |
| 375 | .width(width) |
| 376 | .html($help.html()) |
| 377 | .end() |
| 378 | // Position tip |
| 379 | .css({ |
| 380 | "position": "absolute", |
| 381 | }) |
| 382 | .offset({ |
| 383 | "top": top_pos - ($tip.height()/2), |
| 384 | "left": left_pos |
| 385 | }) |
| 386 | // Vertically align pointer |
| 387 | .find(".wpmudev-pointer") |
| 388 | .css({ |
| 389 | "margin-top": ($tip.height() - 32) / 2 |
| 390 | }) |
| 391 | .end() |
| 392 | // Show entire tip |
| 393 | .show() |
| 394 | ; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Closes tooltip. |
| 399 | */ |
| 400 | function close_tooltip () { |
| 401 | if (!$("#wpmudev-tooltip").length) return false; |
| 402 | |
| 403 | // IE conditional alternate removal |
| 404 | if ($.browser.msie) { |
| 405 | $("#wpmudev-tooltip").hide('fast'); |
| 406 | } else { |
| 407 | // Not IE, do regular transparency animation |
| 408 | $("#wpmudev-tooltip") |
| 409 | .animate({ |
| 410 | "opacity": 0 |
| 411 | }, |
| 412 | 'fast', |
| 413 | function () { |
| 414 | $(this).remove(); |
| 415 | } |
| 416 | ); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | |
| 421 | // Init |
| 422 | $(function () { |
| 423 | |
| 424 | // Populate and place bound tips |
| 425 | $.each($.parseJSON('<?php echo $selectors; ?>'), function (tip_id, selector) { |
| 426 | var $tip = $("#" + tip_id); |
| 427 | if (!$tip.length) return true; |
| 428 | |
| 429 | var $selector = $(selector); |
| 430 | if (!$selector.length) return true; |
| 431 | |
| 432 | $selector.append($tip); |
| 433 | }); |
| 434 | |
| 435 | // Initialize help and add handles |
| 436 | $(".wpmudev-help").each(function () { |
| 437 | initialize_help_item($(this)); |
| 438 | |
| 439 | }); |
| 440 | |
| 441 | // Handle help requests |
| 442 | $(".wpmudev-help-trigger") |
| 443 | .click(function (e) { |
| 444 | <?php if ($this->_use_notice) { ?> |
| 445 | show_help_block($(this)); |
| 446 | <?php } ?> |
| 447 | return false; |
| 448 | }) |
| 449 | .mouseover(function (e) { |
| 450 | open_tooltip($(this)); |
| 451 | |
| 452 | }) |
| 453 | .mouseout(close_tooltip) |
| 454 | ; |
| 455 | |
| 456 | |
| 457 | }); |
| 458 | })(jQuery); |
| 459 | </script> |
| 460 | <?php |
| 461 | } |
| 462 | } |
| 463 | } |