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

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 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 9.4285
1
<?php
2
/**
3
 * Black Studio TinyMCE Widget - Compatibility with WordPress versions prior to 3.9
4
 *
5
 * @package Black_Studio_TinyMCE_Widget
6
 */
7
8
namespace Black_Studio_TinyMCE_Widget\Compatibility\WordPress;
9
10
// Exit if accessed directly.
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
if ( ! class_exists( 'Black_Studio_TinyMCE_Widget\\Compatibility\\WordPress\\WordPress_Pre_39', false ) ) {
16
17
	/**
18
	 * Class that provides compatibility code for WordPress versions prior to 3.9
19
	 *
20
	 * @package Black_Studio_TinyMCE_Widget
21
	 * @since 3.0.0
22
	 */
23
	final class Pre_39 {
24
25
		/**
26
		 * The single instance of the class
27
		 *
28
		 * @var object
29
		 * @since 3.0.0
30
		 */
31
		protected static $_instance = null;
32
33
		/**
34
		 * Return the single class instance
35
		 *
36
		 * @return object
37
		 * @since 3.0.0
38
		 */
39
		public static function instance() {
40
			if ( is_null( self::$_instance ) ) {
41
				self::$_instance = new self();
42
			}
43
			return self::$_instance;
44
		}
45
46
		/**
47
		 * Class constructor
48
		 *
49
		 * @uses is_admin()
50
		 * @uses add_action()
51
		 *
52
		 * @since 3.0.0
53
		 */
54
		protected function __construct() {
55
			if ( is_admin() ) {
56
				add_action( 'admin_init', array( $this, 'admin_init' ), 39 );
57
			}
58
		}
59
60
		/**
61
		 * Prevent the class from being cloned
62
		 *
63
		 * @return void
64
		 * @since 3.0.0
65
		 */
66
		protected function __clone() {
67
			_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; uh?' ), '3.0' );
68
		}
69
70
		/**
71
		 * Admin init
72
		 *
73
		 * @uses add_action()
74
		 * @uses remove_action()
75
		 * @uses add_filter()
76
		 * @uses get_bloginfo()
77
		 *
78
		 * @return void
79
		 * @since 3.0.0
80
		 */
81
		public function admin_init() {
82
			$wp_version = get_bloginfo( 'version' );
83
			if ( bstw()->admin()->enabled() ) {
84
				add_filter( 'black_studio_tinymce_widget_script', array( $this, 'handle' ), 61 );
85
				add_filter( 'black_studio_tinymce_widget_script_path', array( $this, 'path' ), 61 );
86
				add_filter( 'tiny_mce_before_init', array( $this, 'tiny_mce_before_init' ), 61 );
87
				add_action( 'admin_print_footer_scripts', array( $this, 'admin_print_footer_scripts' ) );
88
				remove_action( 'admin_print_footer_scripts', array( bstw()->admin(), 'admin_print_footer_scripts' ) );
89
				if ( version_compare( $wp_version, '3.3', '<' ) ) {
90
					remove_filter( 'black_studio_tinymce_widget_script_path', array( bstw()->compatibility()->module( 'pre_33' ), 'path' ), 67 );
91
				}
92
				add_action( 'black_studio_tinymce_editor', array( $this, 'editor' ), 10, 4 );
93
				remove_action( 'black_studio_tinymce_editor', array( bstw()->admin(), 'editor' ), 10, 3 );
94
			}
95
		}
96
97
		/**
98
		 * Filter to enqueue style / script
99
		 *
100
		 * @return string
101
		 * @since 3.0.0
102
		 */
103
		public function handle() {
104
			return 'black-studio-tinymce-widget-pre39';
105
		}
106
107
		/**
108
		 * Filter for styles / scripts path
109
		 *
110
		 * @param string $path Path for styles / scripts.
111
		 *
112
		 * @return string
113
		 * @since 3.0.0
114
		 */
115
		public function path( $path ) {
116
			return 'compat/wordpress/' . $path;
117
		}
118
119
		/**
120
		 * TinyMCE initialization
121
		 *
122
		 * @param mixed[] $settings Array of settings.
123
		 * @return mixed[]
124
		 * @since 3.0.0
125
		 */
126
		public function tiny_mce_before_init( $settings ) {
127
			$custom_settings = array(
128
				'remove_linebreaks'       => false,
129
				'convert_newlines_to_brs' => false,
130
				'force_p_newlines'        => true,
131
				'force_br_newlines'       => false,
132
				'remove_redundant_brs'    => false,
133
				'forced_root_block'       => 'p',
134
				'apply_source_formatting' => true,
135
			);
136
			// Return modified settings.
137
			return array_merge( $settings, $custom_settings );
138
		}
139
140
		/**
141
		 * Enqueue footer scripts
142
		 *
143
		 * @uses wp_editor()
144
		 *
145
		 * @return void
146
		 * @since 3.0.0
147
		 */
148
		public function admin_print_footer_scripts() {
149
			if ( function_exists( 'wp_editor' ) ) {
150
				wp_editor( '', 'black-studio-tinymce-widget' );
151
			}
152
		}
153
154
		/**
155
		 * Output the visual editor code
156
		 *
157
		 * @uses esc_attr()
158
		 * @uses esc_textarea()
159
		 * @uses esc_html_e()
160
		 * @uses do_action()
161
		 *
162
		 * @param string $text Widget text.
163
		 * @param string $id   Widget ID.
164
		 * @param string $name Widget name.
165
		 * @param string $type Widget type.
166
		 * @return void
167
		 * @since 3.0.0
168
		 */
169
		public function editor( $text, $id, $name = '', $type = 'visual' ) {
170
			$switch_class = 'visual' === $type ? 'html-active' : 'tmce-active';
171
			?>
172
			<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">
173
				<div id="<?php echo esc_attr( $id ); ?>-wp-content-editor-tools" class="wp-editor-tools hide-if-no-js">
174
					<div class="wp-editor-tabs">
175
						<a id="<?php echo esc_attr( $id ); ?>-content-html" class="wp-switch-editor switch-html"><?php esc_html_e( 'HTML' ); ?></a>
176
						<a id="<?php echo esc_attr( $id ); ?>-content-tmce" class="wp-switch-editor switch-tmce"><?php esc_html_e( 'Visual' ); ?></a>
177
					</div>
178
					<div id="<?php esc_attr( $id ); ?>-wp-content-media-buttons" class="wp-media-buttons">
179
						<?php do_action( 'media_buttons', $id ); ?>
180
					</div>
181
				</div>
182
				<div class="wp-editor-container">
183
					<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>
184
				</div>
185
			</div>
186
			<?php
187
		}
188
189
	} // END class
190
191
} // END class_exists
192