Completed
Push — develop ( 4188c8...4421e3 )
by Marco
01:14
created

wp_pre_35_upload_iframe_src()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 2
nop 1
dl 0
loc 7
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Black Studio TinyMCE Widget - Compatibility with older WordPress versions
4
 *
5
 * @package Black_Studio_TinyMCE_Widget
6
 */
7
8
// Exit if accessed directly.
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
if ( ! class_exists( 'Black_Studio_TinyMCE_Compatibility_WordPress' ) ) {
14
15
	/**
16
	 * Class that provides compatibility code with older WordPress versions
17
	 *
18
	 * @package Black_Studio_TinyMCE_Widget
19
	 * @since 2.0.0
20
	 */
21
	final class Black_Studio_TinyMCE_Compatibility_WordPress {
22
23
		/**
24
		 * The single instance of the class
25
		 *
26
		 * @var object
27
		 * @since 2.0.0
28
		 */
29
		protected static $_instance = null;
30
31
		/**
32
		 * Return the single class instance
33
		 *
34
		 * @return object
35
		 * @since 2.0.0
36
		 */
37
		public static function instance() {
38
			if ( is_null( self::$_instance ) ) {
39
				self::$_instance = new self();
40
			}
41
			return self::$_instance;
42
		}
43
44
		/**
45
		 * Class constructor
46
		 *
47
		 * @uses get_bloginfo()
48
		 * @uses add_action()
49
		 *
50
		 * @since 2.0.0
51
		 */
52
		protected function __construct() {
53
			$current_version   = get_bloginfo( 'version' );
54
			$previous_versions = array( '3.2', '3.3', '3.5', '3.9' );
55
			foreach ( $previous_versions as $previous_version ) {
56
				if ( version_compare( $current_version, $previous_version, '<' ) ) {
57
					add_action( 'admin_init', array( $this, 'wp_pre_' . str_replace( '.', '', $previous_version ) ), intval( 10 * $previous_version ) );
58
				}
59
			}
60
		}
61
62
		/**
63
		 * Prevent the class from being cloned
64
		 *
65
		 * @return void
66
		 * @since 2.0.0
67
		 */
68
		protected function __clone() {
69
			_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; uh?' ), '2.0' );
70
		}
71
72
		/**
73
		 * Compatibility for WordPress prior to 3.2
74
		 *
75
		 * @uses remove_action()
76
		 * @uses add_action()
77
		 *
78
		 * @return void
79
		 * @since 2.0.0
80
		 */
81
		public function wp_pre_32() {
82
			if ( bstw()->admin()->enabled() ) {
83
				remove_action( 'admin_print_footer_scripts', array( bstw()->admin(), 'admin_print_footer_scripts' ) );
84
				add_action( 'admin_print_footer_scripts', array( $this, 'wp_pre_32_admin_print_footer_scripts' ) );
85
			}
86
		}
87
88
		/**
89
		 * Enqueue footer scripts for WordPress prior to 3.2
90
		 *
91
		 * @uses wp_tiny_mce()
92
		 * @uses wp_tiny_mce_preload_dialogs()
93
		 *
94
		 * @return void
95
		 * @since 2.0.0
96
		 */
97
		public function wp_pre_32_admin_print_footer_scripts() {
98
			if ( function_exists( 'wp_tiny_mce' ) ) {
99
				wp_tiny_mce( false, array() );
100
			}
101
			if ( function_exists( 'wp_tiny_mce_preload_dialogs' ) ) {
102
				wp_tiny_mce_preload_dialogs();
103
			}
104
		}
105
106
		/**
107
		 * Compatibility for WordPress prior to 3.3
108
		 *
109
		 * @uses add_filter()
110
		 * @uses add_action()
111
		 * @uses remove_action()
112
		 * @uses get_bloginfo()
113
		 *
114
		 * @return void
115
		 * @since 2.0.0
116
		 */
117
		public function wp_pre_33() {
118
			$wp_version = get_bloginfo( 'version' );
119
			if ( bstw()->admin()->enabled() ) {
120
				add_filter( 'tiny_mce_before_init', array( $this, 'wp_pre_33_tiny_mce_before_init' ), 67 );
121
				add_filter( 'black-studio-tinymce-widget-script', array( $this, 'wp_pre_33_handle' ), 67 );
122
				add_filter( 'black-studio-tinymce-widget-style', array( $this, 'wp_pre_33_handle' ), 67 );
123
				remove_action( 'admin_print_styles', array( bstw()->admin(), 'admin_print_styles' ) );
124
				add_action( 'admin_print_styles', array( $this, 'wp_pre_33_admin_print_styles' ) );
125
				remove_action( 'admin_print_scripts', array( bstw()->admin(), 'admin_print_scripts' ) );
126
				add_action( 'admin_print_scripts', array( $this, 'wp_pre_33_admin_print_scripts' ) );
127
				remove_action( 'admin_print_footer_scripts', array( bstw()->admin(), 'admin_print_footer_scripts' ) );
128
				if ( ! version_compare( $wp_version, '3.2', '<' ) ) {
129
					remove_action( 'admin_print_footer_scripts', array( $this, 'wp_pre_32_admin_print_footer_scripts' ) );
130
				}
131
				add_action( 'admin_print_footer_scripts', array( $this, 'wp_pre_33_admin_print_footer_scripts' ) );
132
				remove_action( 'admin_print_scripts', array( bstw()->admin(), 'pointer_load' ) );
133
				remove_filter( 'black_studio_tinymce_admin_pointers-widgets', array( bstw()->admin(), 'pointer_register' ) );
134
			}
135
		}
136
137
		/**
138
		 * Remove WP fullscreen mode and set the native tinyMCE fullscreen mode for WordPress prior to 3.3
139
		 *
140
		 * @param mixed[] $settings Array of settings.
141
		 * @return mixed[]
142
		 * @since 2.0.0
143
		 */
144
		public function wp_pre_33_tiny_mce_before_init( $settings ) {
145
			$plugins = explode( ',', $settings['plugins'] );
146
			if ( isset( $plugins['wpfullscreen'] ) ) {
147
				unset( $plugins['wpfullscreen'] );
148
			}
149
			if ( ! isset( $plugins['fullscreen'] ) ) {
150
				$plugins[] = 'fullscreen';
151
			}
152
			$settings['plugins'] = implode( ',', $plugins );
153
			return $settings;
154
		}
155
156
		/**
157
		 * Enqueue styles for WordPress prior to 3.3
158
		 *
159
		 * @uses wp_enqueue_style()
160
		 * @uses Black_Studio_TinyMCE_Admin::enqueue_style()
161
		 *
162
		 * @return void
163
		 * @since 2.0.0
164
		 */
165
		public function wp_pre_33_admin_print_styles() {
166
			wp_enqueue_style( 'thickbox' );
167
			wp_enqueue_style( 'editor-buttons' );
168
			bstw()->admin()->enqueue_style();
169
		}
170
171
		/**
172
		 * Enqueue header scripts for WordPress prior to 3.3
173
		 *
174
		 * @uses wp_enqueue_script()
175
		 * @uses Black_Studio_TinyMCE_Admin::enqueue_script()
176
		 * @uses Black_Studio_TinyMCE_Admin::localize_script()
177
		 *
178
		 * @return void
179
		 * @since 2.0.0
180
		 */
181
		public function wp_pre_33_admin_print_scripts() {
182
			wp_enqueue_script( 'media-upload' );
183
			bstw()->admin()->enqueue_script();
184
			bstw()->admin()->localize_script();
185
		}
186
187
		/**
188
		 * Filter to enqueue style / script for WordPress prior to 3.3
189
		 *
190
		 * @return string
191
		 * @since 2.0.0
192
		 */
193
		public function wp_pre_33_handle() {
194
			return 'black-studio-tinymce-widget-pre33';
195
		}
196
197
		/**
198
		 * Enqueue footer scripts for WordPress prior to 3.3
199
		 *
200
		 * @uses wp_tiny_mce()
201
		 * @uses wp_preload_dialog()
202
		 *
203
		 * @return void
204
		 * @since 2.0.0
205
		 */
206
		public function wp_pre_33_admin_print_footer_scripts() {
207
			if ( function_exists( 'wp_tiny_mce' ) ) {
208
				wp_tiny_mce( false, array() );
209
			}
210
			if ( function_exists( 'wp_preload_dialogs' ) ) {
211
				wp_preload_dialogs( array( 'plugins' => 'wpdialogs,wplink,wpfullscreen' ) );
212
			}
213
		}
214
215
		/**
216
		 * Compatibility for WordPress prior to 3.5
217
		 *
218
		 * @uses add_filter()
219
		 * @uses Black_Studio_TinyMCE_Admin::enabled()
220
		 *
221
		 * @return void
222
		 * @since 2.0.0
223
		 */
224
		public function wp_pre_35() {
225
			if ( bstw()->admin()->enabled() ) {
226
				add_filter( '_upload_iframe_src', array( $this, 'wp_pre_35_upload_iframe_src' ), 65 );
227
			}
228
		}
229
230
		/**
231
		 * Enable full media options in upload dialog for WordPress prior to 3.5
232
		 * (this is done excluding post_id parameter in Thickbox iframe url)
233
		 *
234
		 * @global string $pagenow
235
		 * @param string $upload_iframe_src Source of the iframe for the upload dialog.
236
		 * @return string
237
		 * @since 2.0.0
238
		 */
239
		public function wp_pre_35_upload_iframe_src( $upload_iframe_src ) {
240
			global $pagenow;
241
			if ( 'widgets.php' === $pagenow || ( 'admin-ajax.php' === $pagenow && isset( $_POST['id_base'] ) && 'black-studio-tinymce' === $_POST['id_base'] ) ) {
242
				$upload_iframe_src = str_replace( 'post_id=0', '', $upload_iframe_src );
243
			}
244
			return $upload_iframe_src;
245
		}
246
247
		/**
248
		 * Compatibility for WordPress prior to 3.9
249
		 *
250
		 * @uses add_action()
251
		 * @uses remove_action()
252
		 * @uses add_filter()
253
		 * @uses get_bloginfo()
254
		 * @uses Black_Studio_TinyMCE_Admin::enabled()
255
		 *
256
		 * @return void
257
		 * @since 2.0.0
258
		 */
259
		public function wp_pre_39() {
260
			$wp_version = get_bloginfo( 'version' );
261
			if ( bstw()->admin()->enabled() ) {
262
				add_filter( 'black-studio-tinymce-widget-script', array( $this, 'wp_pre_39_handle' ), 61 );
263
				add_filter( 'tiny_mce_before_init', array( $this, 'wp_pre_39_tiny_mce_before_init' ), 61 );
264
				add_action( 'admin_print_footer_scripts', array( $this, 'wp_pre_39_admin_print_footer_scripts' ) );
265
				remove_action( 'admin_print_footer_scripts', array( bstw()->admin(), 'admin_print_footer_scripts' ) );
266
				if ( ! version_compare( $wp_version, '3.2', '<' ) ) {
267
					remove_action( 'admin_print_footer_scripts', array( $this, 'wp_pre_32_admin_print_footer_scripts' ) );
268
				}
269
				if ( ! version_compare( $wp_version, '3.3', '<' ) ) {
270
					remove_action( 'admin_print_footer_scripts', array( $this, 'wp_pre_33_admin_print_footer_scripts' ) );
271
				}
272
				add_action( 'black_studio_tinymce_editor', array( $this, 'wp_pre_39_editor' ), 10, 4 );
273
				remove_action( 'black_studio_tinymce_editor', array( bstw()->admin(), 'editor' ), 10, 3 );
274
			}
275
		}
276
277
		/**
278
		 * Filter to enqueue style / script for WordPress prior to 3.9
279
		 *
280
		 * @return string
281
		 * @since 2.0.0
282
		 */
283
		public function wp_pre_39_handle() {
284
			return 'black-studio-tinymce-widget-pre39';
285
		}
286
287
		/**
288
		 * TinyMCE initialization for WordPress prior to 3.9
289
		 *
290
		 * @param mixed[] $settings Array of settings.
291
		 * @return mixed[]
292
		 * @since 2.0.0
293
		 */
294
		public function wp_pre_39_tiny_mce_before_init( $settings ) {
295
			$custom_settings = array(
296
				'remove_linebreaks'       => false,
297
				'convert_newlines_to_brs' => false,
298
				'force_p_newlines'        => true,
299
				'force_br_newlines'       => false,
300
				'remove_redundant_brs'    => false,
301
				'forced_root_block'       => 'p',
302
				'apply_source_formatting' => true,
303
			);
304
			// Return modified settings.
305
			return array_merge( $settings, $custom_settings );
306
		}
307
308
		/**
309
		 * Enqueue footer scripts for WordPress prior to 3.9
310
		 *
311
		 * @uses wp_editor()
312
		 *
313
		 * @return void
314
		 * @since 2.0.0
315
		 */
316
		public function wp_pre_39_admin_print_footer_scripts() {
317
			if ( function_exists( 'wp_editor' ) ) {
318
				wp_editor( '', 'black-studio-tinymce-widget' );
319
			}
320
		}
321
322
		/**
323
		 * Output the visual editor code for WordPress prior to 3.9
324
		 *
325
		 * @uses esc_attr()
326
		 * @uses esc_textarea()
327
		 * @uses do_action()
328
		 *
329
		 * @param string $text Widget text.
330
		 * @param string $id   Widget ID.
331
		 * @param string $name Widget name.
332
		 * @param string $type Widget type.
333
		 * @return void
334
		 * @since 2.0.0
335
		 */
336
		public function wp_pre_39_editor( $text, $id, $name = '', $type = 'visual' ) {
337
			$switch_class = 'visual' === $type ? 'html-active' : 'tmce-active';
338
			?>
339
			<div id="<?php echo esc_attr( $id ); ?>-wp-content-wrap" class="wp-core-ui wp-editor-wrap <?php echo esc_attr( $switch_class ); ?> has-dfw">
340
				<div id="<?php echo esc_attr( $id ); ?>-wp-content-editor-tools" class="wp-editor-tools hide-if-no-js">
341
					<div class="wp-editor-tabs">
342
						<a id="<?php echo esc_attr( $id ); ?>-content-html" class="wp-switch-editor switch-html"><?php esc_html_e( 'HTML' ); ?></a>
343
						<a id="<?php echo esc_attr( $id ); ?>-content-tmce" class="wp-switch-editor switch-tmce"><?php esc_html_e( 'Visual' ); ?></a>
344
					</div>
345
					<div id="<?php esc_attr( $id ); ?>-wp-content-media-buttons" class="wp-media-buttons">
346
						<?php do_action( 'media_buttons', $id ); ?>
347
					</div>
348
				</div>
349
				<div class="wp-editor-container">
350
					<textarea class="widefat" rows="20" cols="40" id="<?php echo esc_attr( $id ); ?>" name="<?php echo esc_attr( $name ); ?>"><?php echo esc_textarea( $text ); ?></textarea>
351
				</div>
352
			</div>
353
			<?php
354
		}
355
356
	} // END class Black_Studio_TinyMCE_Compatibility_Wordpress
357
358
} // END class_exists check
359