Completed
Push — develop ( 4421e3...14767b )
by Marco
01:12
created

wp_pre_39_admin_print_footer_scripts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 9.4285
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.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.5
74
		 *
75
		 * @uses add_filter()
76
		 * @uses Black_Studio_TinyMCE_Admin::enabled()
77
		 *
78
		 * @return void
79
		 * @since 2.0.0
80
		 */
81
		public function wp_pre_35() {
82
			if ( bstw()->admin()->enabled() ) {
83
				add_filter( '_upload_iframe_src', array( $this, 'wp_pre_35_upload_iframe_src' ), 65 );
84
			}
85
		}
86
87
		/**
88
		 * Enable full media options in upload dialog for WordPress prior to 3.5
89
		 * (this is done excluding post_id parameter in Thickbox iframe url)
90
		 *
91
		 * @global string $pagenow
92
		 * @param string $upload_iframe_src Source of the iframe for the upload dialog.
93
		 * @return string
94
		 * @since 2.0.0
95
		 */
96
		public function wp_pre_35_upload_iframe_src( $upload_iframe_src ) {
97
			global $pagenow;
98
			if ( 'widgets.php' === $pagenow || ( 'admin-ajax.php' === $pagenow && isset( $_POST['id_base'] ) && 'black-studio-tinymce' === $_POST['id_base'] ) ) {
99
				$upload_iframe_src = str_replace( 'post_id=0', '', $upload_iframe_src );
100
			}
101
			return $upload_iframe_src;
102
		}
103
104
		/**
105
		 * Compatibility for WordPress prior to 3.9
106
		 *
107
		 * @uses add_action()
108
		 * @uses remove_action()
109
		 * @uses add_filter()
110
		 * @uses get_bloginfo()
111
		 * @uses Black_Studio_TinyMCE_Admin::enabled()
112
		 *
113
		 * @return void
114
		 * @since 2.0.0
115
		 */
116
		public function wp_pre_39() {
117
			$wp_version = get_bloginfo( 'version' );
118
			if ( bstw()->admin()->enabled() ) {
119
				add_filter( 'black_studio_tinymce_widget_script', array( $this, 'wp_pre_39_handle' ), 61 );
120
				add_filter( 'tiny_mce_before_init', array( $this, 'wp_pre_39_tiny_mce_before_init' ), 61 );
121
				add_action( 'admin_print_footer_scripts', array( $this, 'wp_pre_39_admin_print_footer_scripts' ) );
122
				remove_action( 'admin_print_footer_scripts', array( bstw()->admin(), 'admin_print_footer_scripts' ) );
123
				if ( ! version_compare( $wp_version, '3.2', '<' ) ) {
124
					remove_action( 'admin_print_footer_scripts', array( $this, 'wp_pre_32_admin_print_footer_scripts' ) );
125
				}
126
				if ( ! version_compare( $wp_version, '3.3', '<' ) ) {
127
					remove_action( 'admin_print_footer_scripts', array( $this, 'wp_pre_33_admin_print_footer_scripts' ) );
128
				}
129
				add_action( 'black_studio_tinymce_editor', array( $this, 'wp_pre_39_editor' ), 10, 4 );
130
				remove_action( 'black_studio_tinymce_editor', array( bstw()->admin(), 'editor' ), 10, 3 );
131
			}
132
		}
133
134
		/**
135
		 * Filter to enqueue style / script for WordPress prior to 3.9
136
		 *
137
		 * @return string
138
		 * @since 2.0.0
139
		 */
140
		public function wp_pre_39_handle() {
141
			return 'black-studio-tinymce-widget-pre39';
142
		}
143
144
		/**
145
		 * TinyMCE initialization for WordPress prior to 3.9
146
		 *
147
		 * @param mixed[] $settings Array of settings.
148
		 * @return mixed[]
149
		 * @since 2.0.0
150
		 */
151
		public function wp_pre_39_tiny_mce_before_init( $settings ) {
152
			$custom_settings = array(
153
				'remove_linebreaks'       => false,
154
				'convert_newlines_to_brs' => false,
155
				'force_p_newlines'        => true,
156
				'force_br_newlines'       => false,
157
				'remove_redundant_brs'    => false,
158
				'forced_root_block'       => 'p',
159
				'apply_source_formatting' => true,
160
			);
161
			// Return modified settings.
162
			return array_merge( $settings, $custom_settings );
163
		}
164
165
		/**
166
		 * Enqueue footer scripts for WordPress prior to 3.9
167
		 *
168
		 * @uses wp_editor()
169
		 *
170
		 * @return void
171
		 * @since 2.0.0
172
		 */
173
		public function wp_pre_39_admin_print_footer_scripts() {
174
			if ( function_exists( 'wp_editor' ) ) {
175
				wp_editor( '', 'black-studio-tinymce-widget' );
176
			}
177
		}
178
179
		/**
180
		 * Output the visual editor code for WordPress prior to 3.9
181
		 *
182
		 * @uses esc_attr()
183
		 * @uses esc_textarea()
184
		 * @uses do_action()
185
		 *
186
		 * @param string $text Widget text.
187
		 * @param string $id   Widget ID.
188
		 * @param string $name Widget name.
189
		 * @param string $type Widget type.
190
		 * @return void
191
		 * @since 2.0.0
192
		 */
193
		public function wp_pre_39_editor( $text, $id, $name = '', $type = 'visual' ) {
194
			$switch_class = 'visual' === $type ? 'html-active' : 'tmce-active';
195
			?>
196
			<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">
197
				<div id="<?php echo esc_attr( $id ); ?>-wp-content-editor-tools" class="wp-editor-tools hide-if-no-js">
198
					<div class="wp-editor-tabs">
199
						<a id="<?php echo esc_attr( $id ); ?>-content-html" class="wp-switch-editor switch-html"><?php esc_html_e( 'HTML' ); ?></a>
200
						<a id="<?php echo esc_attr( $id ); ?>-content-tmce" class="wp-switch-editor switch-tmce"><?php esc_html_e( 'Visual' ); ?></a>
201
					</div>
202
					<div id="<?php esc_attr( $id ); ?>-wp-content-media-buttons" class="wp-media-buttons">
203
						<?php do_action( 'media_buttons', $id ); ?>
204
					</div>
205
				</div>
206
				<div class="wp-editor-container">
207
					<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>
208
				</div>
209
			</div>
210
			<?php
211
		}
212
213
	} // END class Black_Studio_TinyMCE_Compatibility_Wordpress
214
215
} // END class_exists check
216