Admin_Pointer::__clone()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
		 *
58
		 * @global object $wp_embed
59
		 * @since 2.0.0
60
		 */
61
		protected function __construct() {
62
			// Register action and filter hooks.
63
			add_action( 'admin_init', array( $this, 'admin_init' ), 20 );
64
		}
65
66
		/**
67
		 * Prevent the class from being cloned
68
		 *
69
		 * @return void
70
		 * @since 2.1.0
71
		 */
72
		protected function __clone() {
73
			_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; uh?' ), '2.0' );
74
		}
75
76
		/**
77
		 * Add actions and filters (only in widgets admin page)
78
		 *
79
		 * @uses add_action()
80
		 * @uses add_filter()
81
		 *
82
		 * @return void
83
		 * @since 2.1.0
84
		 */
85
		public function admin_init() {
86
			global $pagenow;
87
			if ( 'widgets.php' === $pagenow ) {
88
				add_action( 'admin_print_scripts', array( $this, 'load' ) );
89
				add_filter( 'black_studio_tinymce_admin_pointers_widgets', array( $this, 'register' ), 10 );
90
				add_filter( 'black_studio_tinymce_admin_pointers_widgets', array( $this, 'filter_dismissed' ), 20 );
91
			}
92
		}
93
94
		/**
95
		 * Helper function to enqueue script
96
		 *
97
		 * @uses wp_enqueue_style()
98
		 * @uses wp_enqueue_script()
99
		 * @uses plugins_url()
100
		 * @uses wp_localize_script()
101
		 * @uses SCRIPT_DEBUG
102
		 *
103
		 * @param mixed[] $pointers Array of pointers.
104
		 * @return void
105
		 * @since 2.1.0
106
		 */
107
		public function enqueue( $pointers ) {
108
			$script = 'black-studio-tinymce-widget-pointer';
109
			$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js';
110
			wp_enqueue_style( 'wp-pointer' );
111
			wp_enqueue_script(
112
				$script,
113
				plugins_url( 'js/' . $script . $suffix, dirname( __FILE__ ) ),
114
				array( 'wp-pointer' ),
115
				bstw()->get_version(),
116
				true
117
			);
118
			wp_localize_script( 'black-studio-tinymce-widget-pointer', 'bstwPointers', $pointers );
119
		}
120
121
		/**
122
		 * Load admin pointer(s)
123
		 *
124
		 * @uses get_current_screen()
125
		 * @uses apply_filters()
126
		 *
127
		 * @return void
128
		 * @since 2.1.0
129
		 */
130
		public function load() {
131
			$screen   = get_current_screen();
132
			$pointers = apply_filters( 'black_studio_tinymce_admin_pointers_' . $screen->id, array() );
133
			if ( ! empty( $pointers ) ) {
134
				$this->enqueue( $pointers );
135
			}
136
		}
137
138
		/**
139
		 * Remove dismissed pointer(s)
140
		 *
141
		 * @uses get_user_meta()
142
		 * @uses get_current_user_id()
143
		 *
144
		 * @param mixed[] $pointers Array of pointers.
145
		 * @return mixed[]
146
		 * @since 2.1.0
147
		 */
148
		public function filter_dismissed( $pointers ) {
149
			$valid_pointers = array();
150
			if ( is_array( $pointers ) && function_exists( 'get_user_meta' ) ) {
151
				$dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
152
				foreach ( $pointers as $pointer_id => $pointer ) {
153
					if ( ! in_array( $pointer_id, $dismissed, true ) ) {
154
						$pointer['pointer_id']        = $pointer_id;
155
						$valid_pointers['pointers'][] = $pointer;
156
					}
157
				}
158
			}
159
			return $valid_pointers;
160
		}
161
162
		/**
163
		 * Register admin pointer(s)
164
		 *
165
		 * @param mixed[] $pointers Array of pointers.
166
		 * @return mixed[]
167
		 * @since 2.1.0
168
		 */
169
		public function register( $pointers ) {
170
			$pointers['black_studio_tinymce_widget'] = array(
171
				'target'  => 'div[id$=black-studio-tinymce-__i__] .widget-top',
172
				'options' => array(
173
					'content'  => sprintf( '<h3>%s</h3> <p>%s</p>',
174
						/* translators: title for the dismissable admin pointer tooltip (same as plugin name) */
175
						__( 'Black Studio TinyMCE Widget', 'black-studio-tinymce-widget' ),
176
						/* translators: text for the dismissable admin pointer tooltip */
177
						__( 'The Visual Editor widget allows you to insert rich text and media objects in your sidebars', 'black-studio-tinymce-widget' )
178
					),
179
					'position' => array(
180
						'edge'  => 'left',
181
						'align' => 'middle',
182
					),
183
				),
184
			);
185
			return $pointers;
186
		}
187
188
	} // END class
189
190
} // END class_exists check
191