-
Notifications
You must be signed in to change notification settings - Fork 0
/
tooltips.php
executable file
·124 lines (97 loc) · 3.5 KB
/
tooltips.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Plugin Name: Tippy Tooltips
* Description: Add Tippy.js tooltips to WordPress
* Version: 1.0.1
* Requires at least: 4.7
* Requires PHP: 5.6
* Author: Daniel M. Hendricks
* Author URI: https://daniel.hn/
* Plugin URI: https://github.com/dmhendricks/tippy-tooltips-wordpress/
* License: GPL2+
* License URI: https://github.com/dmhendricks/tippy-tooltips-wordpress/blob/master/LICENSE
*/
namespace CloudVerve;
defined( 'ABSPATH' ) || die();
final class Tippy_Tooltips {
private static $instance;
protected static $config;
final public static function init() {
if ( !isset( self::$instance ) && !( self::$instance instanceof Tippy_Tooltips ) ) {
self::$instance = new Tippy_Tooltips;
self::$config = self::get_plugin_meta();
// Enqueue scripts
add_action( 'wp_enqueue_scripts', array( self::$instance, 'enqueue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( self::$instance, 'enqueue_scripts' ) );
}
return self::$instance;
}
/**
* Enqueue required scripts. Usage:
* WP Admin only: define( 'TIPPY_ENQUEUE_SCRIPTS', 'admin' );
* Frontend only: define( 'TIPPY_ENQUEUE_SCRIPTS', 'public' );
* Both (default): define( 'TIPPY_ENQUEUE_SCRIPTS', true );
*
* @since 1.0.0
*/
public static function enqueue_scripts() {
$action = current_action();
$target = defined( 'TIPPY_ENQUEUE_SCRIPTS' ) ? TIPPY_ENQUEUE_SCRIPTS : true;
$script_url = self::get_config( 'plugin_url' ) . 'dist/js/tippy-tooltips.min.js';
// Use jsDelivr CDN
if( defined( 'TIPPY_ENABLE_CDN' ) && TIPPY_ENABLE_CDN ) {
$script_url = 'https://cdn.jsdelivr.net/gh/dmhendricks/tippy-tooltips-wordpress/dist/js/tippy-tooltips.min.js';
}
// Register script
wp_register_script( 'tippy-tooltips', $script_url, null, self::get_config( 'version' ), true );
if( defined( 'TIPPY_ENQUEUE_SCRIPTS') && TIPPY_ENQUEUE_SCRIPTS === false ) return;
// Enqueue script
if( $target === true || ( $action == 'wp_enqueue_scripts' && $target == 'public' ) || ( $action == 'admin_enqueue_scripts' && $target == 'admin' ) ) {
wp_enqueue_script( 'tippy-tooltips' );
}
}
/**
* Get meta fields from plugin header
*
* @return array
* @since 1.0.0
*/
public static function get_plugin_meta() {
// Get plugin meta data
$plugin_data = get_file_data( __FILE__, [
'name' => 'Plugin Name',
'version' => 'Version'
], 'plugin' );
// Add plugin directory and URL
$plugin_meta = array_merge( $plugin_data, [
'plugin_dir' => dirname( __FILE__ ),
'plugin_url' => plugin_dir_url( __FILE__ )
]);
return $plugin_meta;
}
/**
* Get plugin configuration variable. Example usage:
* var_dump( self::get_config( 'key' ) );
*
* @param string $key Configuration variable path to retrieve
* @param mixed $default The default value to return if $key is not found
* @since 1.0.0
*/
public static function get_config( $key = null, $default = null ) {
// If key not specified, return entire registry
if ( !$key ) {
return self::$config;
}
// Else return $key value or null if doesn't exist
$value = self::$config;
foreach( explode( '/', $key ) as $k ) {
if ( !isset( $value[$k] ) ) {
return $default;
}
$value = &$value[$k];
}
return $value;
}
}
// Initialize plugin
if( !wp_doing_cron() && !wp_doing_ajax() ) Tippy_Tooltips::init();