* if (!class_exists('WpmuDev_HelpTooltips')) require_once YOUR_PLUGIN_BASE_DIR . '/lib/external/class_wd_help_tooltips.php';
* $tips = new WpmuDev_HelpTooltips();
* $tips->set_icon_url("URL_TO_YOUR_ICON");
* echo $tips->add_tip("Tip 1 text here");
* // ...
* echo $tips->add_tip("Tip 2 text here");
*
* This is a basic usage scenario.
*
* Alternative usage example:
*
* if (!class_exists('WpmuDev_HelpTooltips')) require_once YOUR_PLUGIN_BASE_DIR . '/lib/external/class_wd_help_tooltips.php';
* $tips = new WpmuDev_HelpTooltips();
* $tips->set_icon_url("URL_TO_YOUR_ICON");
* $tips->bind_tip('My tip text here', '.icon32:first ~h2');
* // Note that you don't echo anything in this usage example - the tip will be
* // added automatically next to the supplied selector (second argument)
*
* This scenario may be useful for e.g. adding our tips to UI elements created by WP or other plugins.
* You can freely alternate between add_tip() and bind_tip() methods,
* just remember to echo add_tip() and *not* echo bind_tip().
*
* Another alternative usage example, setting tips for multiple pages in one place:
*
* if (!class_exists('WpmuDev_HelpTooltips')) require_once YOUR_PLUGIN_BASE_DIR . '/lib/external/class_wd_help_tooltips.php';
* // Tips added to $tips1 object will only show on Social Marketing add/edit advert page
* $tips1 = new WpmuDev_HelpTooltips();
* $tips1->set_icon_url("URL_TO_YOUR_ICON");
* $tips1->set_screen_id("social_marketing_ad");
* $tips1->bind_tip('My tip 1 text here', '.icon32:first ~h2');
* // ...
* // Tips added to $tips2 object will only show on Social Marketing getting started page
* $tips2 = new WpmuDev_HelpTooltips();
* $tips2->set_icon_url("URL_TO_YOUR_ICON");
* $tips2->set_screen_id("social_marketing_ad_page_wdsm-get_started");
* $tips2->bind_tip('My tip 2 text here', '.icon32:first ~h2');
*
* Using add_tip() method will do nothing in this last scenario, as it doesn't make sense in that context.
* This scenario may be useful for adding tooltips to all pages in one central location.
* E.g. for adding tooltips in a plugin add-on.
*/
class WpmuDev_HelpTooltips {
/**
* Holds an array of inline tips: used as dependency inclusion switch.
*/
private $_inline_tips = array();
/**
* Holds an array of bound tips: used as dependency inclusion switch and bound tips buffer.
*/
private $_bound_tips = array();
/**
* Holds an array of bound tips selectors: used as bound tips selectors buffer.
*/
private $_bound_selectors = array();
/**
* Full URL to help icon, which is used as tip anchor
* and as notice background image.
*/
private $_icon_url;
/**
* Flag that determines do we want to use notices
* (tips expanded on click).
* Defaults to true.
*/
private $_use_notice = true;
/**
* Limits tip output to a screen (page).
* Optional.
* Works best with bind_tip() method.
*/
private $_screen_id = false;
/**
* Bind to footer hooks when instantiated.
*/
public function __construct () {
global $wp_version;
$version = preg_replace('/-.*$/', '', $wp_version);
if (version_compare($version, '3.3', '>=')) {
add_action('admin_footer', array($this, 'add_bound_tips'), 999);
add_action('admin_print_footer_scripts', array($this, 'initialize'));
}
}
/**
* Sets icon URL.
* @param string $icon_url Full URL to help anchor icon
*/
public function set_icon_url ($icon_url) {
$this->_icon_url = $icon_url;
}
/**
* Set show notices (tips expanded on click) flag.
* @param bool $use_notice True to use notices (default), false otherwise.
*/
public function set_use_notice ($use_notice=true) {
$this->_use_notice = $use_notice;
}
/**
* Set screen limiting flag.
* @param $screen_id Screen ID that tips in this object apply to.
*/
public function set_screen_id ($screen_id) {
$this->_screen_id = $screen_id;
}
/**
* Returns inline tip markup.
* Scenario: for echoing inline tips next to elements on the page.
* Usage example:
*
* echo $tips->add_tip('My tip text here');
*
* @param string $tip Tip text
* @return string Inline tip markup
*/
public function add_tip ($tip) {
if (!$this->_check_screen()) return false;
$this->_inline_tips[] = $tip;
return $this->_get_tip_markup($tip);
}
/**
* Binds a tip to selector.
* This is different from inline tips, as you don't have to output them yourself.
* Scenario: for adding help tips next to elements determined by the selector on page load time.
* Usage example:
*
* $tips->bind_tip('My tip text here', '.icon32:first ~h2');
*
* @param string $tip Tip text
* @param string $selector jQuery selector of the element that tip is related to.
*/
public function bind_tip ($tip, $bind_to_selector) {
$tip_id = 'wpmudev-help-tip-for-' . md5($bind_to_selector);
$this->_bound_tips[$tip_id] = $tip;
$this->_bound_selectors[$tip_id] = $bind_to_selector;
}
/**
* Bounded tips injection handler.
* Will queue up the bounded tips.
*/
function add_bound_tips () {
if (!$this->_check_screen()) return false;
if (!$this->_bound_tips) return false;
foreach ($this->_bound_tips as $id => $tip) {
echo $this->_get_tip_markup($tip, 'id="' . $id . '" style=display:none');
}
}
/**
* Dependency injection handler.
* Will only add dependencies if there are actual tooltips to show.
*/
function initialize () {
if (!$this->_check_screen()) return false;
if (!$this->_inline_tips && !$this->_bound_tips) return false;
$this->_print_styles();
$this->_print_scripts();
}
/**
* Screen limitation check.
* @return bool True if we're good to go, false if we're on a wrong screen.
*/
private function _check_screen () {
if (!$this->_screen_id) return true; // No screen dependency
$screen = get_current_screen();
if (!is_object($screen)) return false; // Actually older then 3.3
if ($this->_screen_id != @$screen->id) return false; // Not for this screen
return true;
}
private function _get_tip_markup ($tip, $arg='') {
return "{$tip}";
}
/**
* Private helper method that prints style dependencies.
*/
private function _print_styles () {
// Have we already done this?
if (!defined('WPMUDEV_TOOLTIPS_CSS_ADDED')) define('WPMUDEV_TOOLTIPS_CSS_ADDED', true);
else return false;
?>
_bound_selectors);
?>