Completed
Push — develop ( e73ae0...03416e )
by Marco
01:12
created

filter_dismissed()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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