Completed
Push — develop ( 85c2ba...5fea9a )
by Marco
04:56
created

Admin_Pointer::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Black Studio TinyMCE Widget - Admin pointer handling
4
 *
5
 * @package Black_Studio_TinyMCE_Widget
6
 */
7
8
namespace Black_Studio_TinyMCE_Widget\Admin;
9
10
// Exit if accessed directly.
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Class that provides admin pointer
17
 *
18
 * @package Black_Studio_TinyMCE_Widget
19
 * @since 2.1.0
20
 */
21
22
if ( ! class_exists( 'Black_Studio_TinyMCE_Widget\\Admin\\Admin_Pointer' ) ) {
23
24
	/**
25
	 * Class that provides admin pointer
26
	 *
27
	 * @package Black_Studio_TinyMCE_Widget
28
	 * @since 2.1.0
29
	 */
30
	final class Admin_Pointer {
31
32
		/**
33
		 * The single instance of the class
34
		 *
35
		 * @var object
36
		 * @since 2.1.0
37
		 */
38
		protected static $_instance = null;
39
40
		/**
41
		 * Return the single class instance
42
		 *
43
		 * @return object
44
		 * @since 2.1.0
45
		 */
46
		public static function instance() {
47
			if ( is_null( self::$_instance ) ) {
48
				self::$_instance = new self();
49
			}
50
			return self::$_instance;
51
		}
52
53
		/**
54
		 * Class constructor
55
		 *
56
		 * @uses add_action()
57
		 * @uses add_filter()
58
		 * @uses get_option()
59
		 * @uses get_bloginfo()
60
		 *
61
		 * @global object $wp_embed
62
		 * @since 2.0.0
63
		 */
64
		protected function __construct() {
65
			// Register action and filter hooks.
66
			add_action( 'admin_init', array( $this, 'admin_init' ), 20 );
67
		}
68
69
		/**
70
		 * Prevent the class from being cloned
71
		 *
72
		 * @return void
73
		 * @since 2.1.0
74
		 */
75
		protected function __clone() {
76
			_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; uh?' ), '2.0' );
77
		}
78
79
		/**
80
		 * Add actions and filters (only in widgets admin page)
81
		 *
82
		 * @uses add_action()
83
		 * @uses add_filter()
84
		 *
85
		 * @return void
86
		 * @since 2.1.0
87
		 */
88
		public function admin_init() {
89
			global $pagenow;
90
			if ( 'widgets.php' === $pagenow ) {
91
				add_action( 'admin_print_scripts', array( $this, 'load' ) );
92
				add_filter( 'black_studio_tinymce_admin_pointers_widgets', array( $this, 'register' ), 10 );
93
				add_filter( 'black_studio_tinymce_admin_pointers_widgets', array( $this, 'filter_dismissed' ), 20 );
94
			}
95
		}
96
97
		/**
98
		 * Helper function to enqueue script
99
		 *
100
		 * @uses wp_enqueue_style()
101
		 * @uses wp_enqueue_script()
102
		 * @uses plugins_url()
103
		 * @uses wp_localize_script()
104
		 * @uses SCRIPT_DEBUG
105
		 *
106
		 * @param mixed[] $pointers Array of pointers.
107
		 * @return void
108
		 * @since 2.1.0
109
		 */
110
		public function enqueue( $pointers ) {
111
			$script = 'black-studio-tinymce-widget-pointer';
112
			$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js';
113
			wp_enqueue_style( 'wp-pointer' );
114
			wp_enqueue_script(
115
				$script,
116
				plugins_url( 'js/' . $script . $suffix, dirname( __FILE__ ) ),
117
				array( 'wp-pointer' ),
118
				bstw()->get_version(),
119
				true
120
			);
121
			wp_localize_script( 'black-studio-tinymce-widget-pointer', 'bstw_pointers', $pointers );
122
		}
123
124
		/**
125
		 * Load admin pointer(s)
126
		 *
127
		 * @uses get_current_screen()
128
		 * @uses apply_filters()
129
		 *
130
		 * @return void
131
		 * @since 2.1.0
132
		 */
133
		public function load() {
134
			$screen   = get_current_screen();
135
			$pointers = apply_filters( 'black_studio_tinymce_admin_pointers_' . $screen->id, array() );
136
			if ( ! empty( $pointers ) ) {
137
				$this->enqueue( $pointers );
138
			}
139
		}
140
141
		/**
142
		 * Remove dismissed pointer(s)
143
		 *
144
		 * @uses get_user_meta()
145
		 * @uses get_current_user_id()
146
		 *
147
		 * @param mixed[] $pointers Array of pointers.
148
		 * @return mixed[]
149
		 * @since 2.1.0
150
		 */
151
		public function filter_dismissed( $pointers ) {
152
			$valid_pointers = array();
153
			if ( is_array( $pointers ) && function_exists( 'get_user_meta' ) ) {
154
				$dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
155
				foreach ( $pointers as $pointer_id => $pointer ) {
156
					if ( ! in_array( $pointer_id, $dismissed, true ) ) {
157
						$pointer['pointer_id']        = $pointer_id;
158
						$valid_pointers['pointers'][] = $pointer;
159
					}
160
				}
161
			}
162
			return $valid_pointers;
163
		}
164
165
		/**
166
		 * Register admin pointer(s)
167
		 *
168
		 * @param mixed[] $pointers Array of pointers.
169
		 * @return mixed[]
170
		 * @since 2.1.0
171
		 */
172
		public function register( $pointers ) {
173
			$pointers['black_studio_tinymce_widget'] = array(
174
				'target'  => 'div[id$=black-studio-tinymce-__i__] .widget-top',
175
				'options' => array(
176
					'content'  => sprintf( '<h3>%s</h3> <p>%s</p>',
177
						/* translators: title for the dismissable admin pointer tooltip (same as plugin name) */
178
						__( 'Black Studio TinyMCE Widget', 'black-studio-tinymce-widget' ),
179
						/* translators: text for the dismissable admin pointer tooltip */
180
						__( 'The Visual Editor widget allows you to insert rich text and media objects in your sidebars', 'black-studio-tinymce-widget' )
181
					),
182
					'position' => array(
183
						'edge'  => 'left',
184
						'align' => 'middle',
185
					),
186
				),
187
			);
188
			return $pointers;
189
		}
190
191
192
	} // END class Black_Studio_TinyMCE_Admin_Pointer
193
194
} // END class_exists check
195