1 <?php
2
3 4 5 6 7
8 class Fieldmanager_RichTextArea extends Fieldmanager_Field {
9
10 11 12 13
14 public $field_class = 'richtext';
15
16 17 18
19 public $apply_mce_filters = true;
20
21 22 23 24
25 public $init_options = array();
26
27 28 29 30 31
32 public $editor_settings = array();
33
34 35 36
37 public $add_code_plugin = false;
38
39 40 41 42
43 public $buttons_1;
44
45 46 47 48
49 public $buttons_2;
50
51 52 53 54
55 public $buttons_3;
56
57 58 59 60
61 public $buttons_4;
62
63 64 65 66 67
68 public $stylesheet;
69
70 71 72 73 74
75 protected $edit_config = false;
76
77 78 79 80 81 82
83 public function __construct( $label = '', $options = array() ) {
84 $this->sanitize = array( $this, 'sanitize' );
85 fm_add_script( 'fm_richtext', 'js/richtext.js', array( 'jquery', 'fieldmanager_script' ), '1.0.7' );
86
87 parent::__construct( $label, $options );
88 }
89
90 91 92 93 94 95
96 public function sanitize( $value ) {
97 return wp_filter_post_kses( wpautop( $value ) );
98 }
99
100 101 102 103 104 105
106 public function form_element( $value = '' ) {
107 $proto = $this->has_proto();
108 $wrapper_classes = array();
109
110 $this->prep_editor_config();
111
112 $settings = $this->array_merge_deep( $this->editor_settings, array(
113 'textarea_name' => $this->get_form_name(),
114 'editor_class' => 'fm-element fm-richtext',
115 'tinymce' => array( 'wp_skip_init' => true ),
116 ) );
117
118 if ( $proto ) {
119 add_filter( 'the_editor', array( $this, 'add_proto_id' ) );
120 }
121
122 if ( ! isset( $settings['default_editor'] ) ) {
123 $settings['default_editor'] = 'tinymce';
124 } elseif ( 'cookie' == $settings['default_editor'] ) {
125 if ( $proto ) {
126 $settings['default_editor'] = 'tinymce';
127 } else {
128 $cookie_value = '';
129 if ( $user = wp_get_current_user() ) {
130 $setting_key = str_replace( '-', '_', $this->get_element_id() );
131 $setting_key = preg_replace( '/[^a-z0-9_]/i', '', $setting_key );
132 $cookie_value = get_user_setting( 'editor_' . $setting_key, 'tinymce' );
133 }
134
135 $settings['default_editor'] = in_array( $cookie_value, array( 'tinymce', 'html' ) ) ? $cookie_value : 'tinymce';
136 }
137
138 $wrapper_classes[] = 'fm-richtext-remember-editor';
139 }
140
141 $this->add_editor_filters();
142
143 ob_start();
144 wp_editor( $value, $this->get_element_id(), $settings );
145 $content = ob_get_clean();
146
147 $this->remove_editor_filters();
148
149 if ( $proto ) {
150 remove_filter( 'the_editor', array( $this, 'add_proto_id' ) );
151 }
152
153
154 if ( ! empty( $wrapper_classes ) ) {
155 $content = str_replace( 'wp-core-ui wp-editor-wrap', 'wp-core-ui wp-editor-wrap ' . implode( ' ', $wrapper_classes ), $content );
156 }
157
158 return $content;
159 }
160
161 162 163
164 protected function prep_editor_config() {
165
166 if ( ! empty( $this->init_options ) ) {
167 if ( ! isset( $this->stylesheet ) && ! empty( $this->init_options['content_css'] ) ) {
168 $this->stylesheet = $this->init_options['content_css'];
169 unset( $this->init_options['content_css'] );
170 }
171 if ( empty( $this->editor_settings['tinymce'] ) ) {
172 $this->editor_settings['tinymce'] = array();
173 }
174 $this->editor_settings['tinymce'] = wp_parse_args( $this->editor_settings['tinymce'], $this->init_options );
175 }
176
177 if ( isset( $this->stylesheet ) ) {
178 $this->edit_config = true;
179 }
180 }
181
182 183 184 185 186 187
188 public function add_proto_id( $editor ) {
189 return str_replace( '<textarea', '<textarea data-proto-id="' . $this->get_element_id() . '"', $editor );
190 }
191
192 193 194 195 196 197
198 public function customize_buttons( $buttons ) {
199 switch ( current_filter() ) {
200 case 'teeny_mce_buttons':
201 case 'mce_buttons' : return $this->buttons_1;
202 case 'mce_buttons_2' : return $this->buttons_2;
203 case 'mce_buttons_3' : return $this->buttons_3;
204 case 'mce_buttons_4' : return $this->buttons_4;
205 }
206 return $buttons;
207 }
208
209 210 211 212 213 214
215 public function editor_config( $mceInit ) {
216 if ( isset( $this->stylesheet ) ) {
217 $this->stylesheet = explode( ',', $this->stylesheet );
218 $this->stylesheet = array_map( 'esc_url_raw', $this->stylesheet );
219 $mceInit['content_css'] = implode( ',', $this->stylesheet );
220 }
221 return $mceInit;
222 }
223
224 225 226
227 protected function add_editor_filters() {
228 if ( isset( $this->buttons_1 ) ) {
229 add_filter( 'mce_buttons', array( $this, 'customize_buttons' ) );
230 add_filter( 'teeny_mce_buttons', array( $this, 'customize_buttons' ) );
231 }
232 if ( isset( $this->buttons_2 ) ) {
233 add_filter( 'mce_buttons_2', array( $this, 'customize_buttons' ) );
234 }
235 if ( isset( $this->buttons_3 ) ) {
236 add_filter( 'mce_buttons_3', array( $this, 'customize_buttons' ) );
237 }
238 if ( isset( $this->buttons_4 ) ) {
239 add_filter( 'mce_buttons_4', array( $this, 'customize_buttons' ) );
240 }
241 if ( $this->edit_config ) {
242 if ( ! empty( $this->editor_settings['teeny'] ) ) {
243 add_filter( 'teeny_mce_before_init', array( $this, 'editor_config' ) );
244 } else {
245 add_filter( 'tiny_mce_before_init', array( $this, 'editor_config' ) );
246 }
247 }
248
249
250
251
252
253 remove_filter( 'the_editor_content', 'wp_htmledit_pre' );
254 remove_filter( 'the_editor_content', 'wp_richedit_pre' );
255 }
256
257 258 259 260
261 protected function remove_editor_filters() {
262 remove_filter( 'mce_buttons', array( $this, 'customize_buttons' ) );
263 remove_filter( 'mce_buttons_2', array( $this, 'customize_buttons' ) );
264 remove_filter( 'mce_buttons_3', array( $this, 'customize_buttons' ) );
265 remove_filter( 'mce_buttons_4', array( $this, 'customize_buttons' ) );
266 remove_filter( 'teeny_mce_before_init', array( $this, 'editor_config' ) );
267 remove_filter( 'tiny_mce_before_init', array( $this, 'editor_config' ) );
268 }
269
270 271 272 273 274 275 276 277
278 protected function array_merge_deep() {
279 $result = array();
280 foreach ( func_get_args() as $array ) {
281 foreach ( $array as $key => $value) {
282 if ( is_integer( $key ) ) {
283
284
285
286 $result[] = $value;
287 } elseif ( isset( $result[ $key ] ) && is_array( $result[ $key ] ) && is_array( $value ) ) {
288
289 $result[ $key ] = $this->array_merge_deep( $result[ $key ], $value );
290 } else {
291
292 $result[ $key ] = $value;
293 }
294 }
295 }
296 return $result;
297 }
298
299 }
300