Completed
Push — develop ( cdc232...56a932 )
by Marco
01:33
created

Admin::admin_print_styles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Black Studio TinyMCE Widget - Admin features
4
 *
5
 * @package Black_Studio_TinyMCE_Widget
6
 */
7
8
namespace Black_Studio_TinyMCE_Widget\Admin;
9
10
use WP_Query;
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
if ( ! class_exists( 'Black_Studio_TinyMCE_Widget\\Admin\\Admin', false ) ) {
18
19
	/**
20
	 * Class that provides admin functionalities
21
	 *
22
	 * @package Black_Studio_TinyMCE_Widget
23
	 * @since 2.0.0
24
	 */
25
	final class Admin {
26
27
		/**
28
		 * The single instance of the class
29
		 *
30
		 * @var object
31
		 * @since 2.0.0
32
		 */
33
		protected static $_instance = null;
34
35
		/**
36
		 * Array containing the plugin links
37
		 *
38
		 * @var array
39
		 * @since 2.0.0
40
		 */
41
		protected $links;
42
43
		/**
44
		 * Return the single class instance
45
		 *
46
		 * @return object
47
		 * @since 2.0.0
48
		 */
49
		public static function instance() {
50
			if ( is_null( self::$_instance ) ) {
51
				self::$_instance = new self();
52
			}
53
			return self::$_instance;
54
		}
55
56
		/**
57
		 * Class constructor
58
		 *
59
		 * @uses add_action()
60
		 * @uses add_filter()
61
		 * @uses get_option()
62
		 * @uses get_bloginfo()
63
		 *
64
		 * @since 2.0.0
65
		 */
66
		protected function __construct() {
67
			// Register action and filter hooks.
68
			add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
69
			add_action( 'admin_init', array( $this, 'admin_init' ), 20 );
70
			add_action( 'init', array( $this, 'register_dummy_post_type' ) );
71
		}
72
73
		/**
74
		 * Prevent the class from being cloned
75
		 *
76
		 * @return void
77
		 * @since 2.0.0
78
		 */
79
		protected function __clone() {
80
			_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; uh?' ), '2.0' );
81
		}
82
83
		/**
84
		 * Load language files
85
		 *
86
		 * @uses load_plugin_textdomain()
87
		 *
88
		 * @return void
89
		 * @since 2.0.0
90
		 */
91
		public function load_textdomain() {
92
			load_plugin_textdomain( 'black-studio-tinymce-widget', false, dirname( dirname( dirname( plugin_basename( __FILE__ ) ) ) ) . '/languages/' );
93
		}
94
95
		/**
96
		 * Checks if the plugin admin code should be loaded
97
		 *
98
		 * @uses apply_filters()
99
		 *
100
		 * @global string $pagenow
101
		 * @return boolean
102
		 * @since 2.0.0
103
		 */
104
		public function enabled() {
105
			global $pagenow;
106
			$enabled_pages = apply_filters( 'black_studio_tinymce_enable_pages', array( 'widgets.php', 'customize.php', 'admin-ajax.php' ) );
107
			return apply_filters( 'black_studio_tinymce_enable', in_array( $pagenow, $enabled_pages, true ) );
108
		}
109
110
		/**
111
		 * Add actions and filters (only in widgets admin page)
112
		 *
113
		 * @uses add_action()
114
		 * @uses add_filter()
115
		 * @uses do_action()
116
		 *
117
		 * @return void
118
		 * @since 2.0.0
119
		 */
120
		public function admin_init() {
121
			$this->init_links();
122
			add_action( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );
123
			if ( $this->enabled() ) {
124
				add_action( 'black_studio_tinymce_before_editor', array( $this, 'display_links' ) );
125
				add_action( 'black_studio_tinymce_editor', array( $this, 'editor' ), 10, 4 );
126
				add_action( 'black_studio_tinymce_after_editor', array( $this, 'fix_the_editor_content_filter' ) );
127
				if ( ! user_can_richedit() ) {
128
					add_action( 'admin_notices', array( $this, 'visual_editor_disabled_notice' ) );
129
				}
130
				add_action( 'wp_ajax_bstw_visual_editor_disabled_dismiss_notice', array( $this, 'visual_editor_disabled_dismiss_notice' ) );
131
				do_action( 'black_studio_tinymce_load' );
132
			}
133
		}
134
135
		/**
136
		 * Output the visual editor
137
		 *
138
		 * @uses wp_editor()
139
		 *
140
		 * @param string $text      Text inside the editor.
141
		 * @param string $editor_id Editor instance ID.
142
		 * @param string $name      Editor instance name.
143
		 * @param string $type      Editor instance type.
144
		 * @return void
145
		 * @since 2.0.0
146
		 */
147
		public function editor( $text, $editor_id, $name = '', $type = 'visual' ) {
148
			wp_editor( $text, $editor_id, array(
149
				'textarea_name'  => $name,
150
				'default_editor' => 'visual' === $type ? 'tmce' : 'html',
151
			) );
152
		}
153
154
		/**
155
		 * Remove editor content filters for multiple editor instances
156
		 * Workaround for WordPress Core bug #28403 https://core.trac.wordpress.org/ticket/28403
157
		 *
158
		 * @uses remove_filter
159
		 *
160
		 * @return void
161
		 * @since 2.1.7
162
		 */
163
		public function fix_the_editor_content_filter() {
164
			remove_filter( 'the_editor_content', 'wp_htmledit_pre' );
165
			remove_filter( 'the_editor_content', 'wp_richedit_pre' );
166
		}
167
168
		/**
169
		 * Initialize plugin links
170
		 *
171
		 * @return void
172
		 * @since 2.0.0
173
		 */
174
		public function init_links() {
175
			$this->links = array(
176
				/* translators: text used for plugin home link */
177
				'https://wordpress.org/plugins/black-studio-tinymce-widget/' => __( 'Home', 'black-studio-tinymce-widget' ),
178
				/* translators: text used for support faq link */
179
				'https://wordpress.org/plugins/black-studio-tinymce-widget/faq/' => __( 'FAQ', 'black-studio-tinymce-widget' ),
180
				/* translators: text used for support forum link */
181
				'https://wordpress.org/support/plugin/black-studio-tinymce-widget' => __( 'Support', 'black-studio-tinymce-widget' ),
182
				/* translators: text used for reviews link */
183
				'https://wordpress.org/support/view/plugin-reviews/black-studio-tinymce-widget' => __( 'Rate', 'black-studio-tinymce-widget' ),
184
				/* translators: text used for follow on twitter link */
185
				'https://twitter.com/blackstudioita' => __( 'Follow', 'black-studio-tinymce-widget' ),
186
				/* translators: text used for donation link */
187
				'https://www.blackstudio.it/en/wordpress-plugins/black-studio-tinymce-widget/' => __( 'Donate', 'black-studio-tinymce-widget' ),
188
			);
189
		}
190
191
		/**
192
		 * Display plugin links
193
		 *
194
		 * @return void
195
		 * @since 2.0.0
196
		 */
197
		public function display_links() {
198
			echo "\t<div class='bstw-links'>\n";
199
			echo "\t\t<span class='bstw-links-list'>\n";
200
			$counter = count( $this->links ) - 1;
201
			foreach ( $this->links as $url => $label ) {
202
				$separator = ( $counter-- > 0 ? ' | ' : '' );
203
				echo "\t\t\t<a href='" . esc_url( $url ) . "' target='_blank'>" . esc_html( $label ) . "</a>$separator\n"; // xss ok.
204
			}
205
			echo "\t\t</span>\n";
206
			/* translators: text used for the icon that shows the plugin links */
207
			echo "\t\t<a class='bstw-links-icon icon16 icon-plugins' href='#' title='" . esc_attr( __( 'About Black Studio TinyMCE Widget plugin', 'black-studio-tinymce-widget' ) ) . "'></a>\n";
208
			echo "\t</div>\n";
209
		}
210
211
		/**
212
		 * Show row meta on the plugin screen
213
		 *
214
		 * @uses esc_html()
215
		 * @uses esc_url()
216
		 *
217
		 * @param string[] $links Array of links.
218
		 * @param string   $file  Plugin's filename.
219
		 * @return string[]
220
		 * @since 2.0.0
221
		 */
222
		public function plugin_row_meta( $links, $file ) {
223
			if ( bstw()->get_basename() === $file ) {
224
				foreach ( $this->links as $url => $label ) {
225
					$links[ $label ] = '<a href="' . esc_url( $url ) . '" target="_blank">' . esc_html( $label ) . '</a>';
226
				}
227
			}
228
			return $links;
229
		}
230
231
		/**
232
		 * Show admin notice when visual editor is disabled in current user's profile settings
233
		 *
234
		 * @uses get_user_meta()
235
		 * @uses get_current_user_id()
236
		 *
237
		 * @return void
238
		 * @since 2.4.0
239
		 */
240
		public function visual_editor_disabled_notice() {
241
			global $pagenow;
242
			$dismissed = false;
243
			if ( function_exists( 'get_user_meta' ) ) {
244
				$dismissed = get_user_meta( get_current_user_id(), '_bstw_visual_editor_disabled_notice_dismissed', true );
245
			}
246
			if ( 'widgets.php' === $pagenow && empty( $dismissed ) ) {
247
				echo '<div class="bstw-visual-editor-disabled-notice notice notice-warning is-dismissible">';
248
				/* translators: warning message shown when when visual editor is disabled in current user's profile settings */
249
				echo '<p>' . esc_html( __( 'Visual Editor is disabled in your Profile settings. You need to enable it in order to use the Visual Editor widget at its full potential.', 'black-studio-tinymce-widget' ) ) . '</p>';
250
				echo '</div>';
251
			}
252
		}
253
254
		/**
255
		 * Store dismission of the "Visual Editor disabled" notice for the current user
256
		 *
257
		 * @uses add_user_meta()
258
		 * @uses get_current_user_id()
259
		 *
260
		 * @return void
261
		 * @since 2.4.0
262
		 */
263
		public function visual_editor_disabled_dismiss_notice() {
264
			if ( function_exists( 'add_user_meta' ) ) {
265
				add_user_meta( get_current_user_id(), '_bstw_visual_editor_disabled_notice_dismissed', true );
266
			}
267
		}
268
269
		/**
270
		 * Register a private custom post type to be used for link embed previews
271
		 *
272
		 * @uses register_post_type()
273
		 *
274
		 * @return void
275
		 * @since 3.0.0
276
		 */
277
		public function register_dummy_post_type() {
278
			$args = array(
279
				'public'             => false,
280
				'publicly_queryable' => false,
281
				'show_ui'            => false,
282
				'query_var'          => false,
283
				'rewrite'            => false,
284
				'capability_type'    => 'post',
285
				'hierarchical'       => false,
286
				'menu_position'      => null,
287
				'show_in_nav_menus'  => false,
288
				'has_archive'        => false,
289
			);
290
			register_post_type( 'bstw_dummy', $args );
291
		}
292
293
		/**
294
		 * Get dummy post ID for link embed previews
295
		 *
296
		 * @uses WP_Query()
297
		 * @uses wp_insert_post()
298
		 * @uses update_option()
299
		 * @uses get_option()
300
		 *
301
		 * @return int
302
		 * @since 3.0.0
303
		 */
304
		public function get_dummy_post_id() {
305
			$query_post = new WP_Query( 'post_type=bstw_dummy' );
306
			if ( $query_post->post_count > 0 ) {
307
				$dummy_post_id = $query_post->post->ID;
308
			} else {
309
				$dummy_post_id = wp_insert_post( array( 'post_type' => 'bstw_dummy' ) );
310
			}
311
			return $dummy_post_id;
312
		}
313
314
	} // END class
315
316
} // END class_exists check
317