Completed
Push — milestone/2_0/react-ui ( 3dbb9a...65728f )
by
unknown
03:10
created

Rich_Text_Field::editor_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
ccs 0
cts 9
cp 0
crap 2
rs 9.4285
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
/**
6
 * WYSIWYG rich text area field class.
7
 */
8
class Rich_Text_Field extends Textarea_Field {
9
10
	/**
11
	 * Defines if the rich text field should be loaded only when scrolled into view
12
	 * 
13
	 * @var boolean
14
	 */
15
	protected $lazyload = true;
16
17
	/**
18
	 * {@inheritDoc}
19
	 */
20
	public static function field_type_activated() {
21
		add_action( 'admin_print_footer_scripts', array( get_class(), 'editor_init' ) );
22
	}
23
24
	/**
25
	 * Display the editor.
26
	 *
27
	 * Instead of enqueueing all required scripts and stylesheets and setting up TinyMCE,
28
	 * wp_editor() automatically enqueues and sets up everything.
29
	 */
30
	public static function editor_init() {
31
		?>
32
		<div style="display:none;">
33
			<?php
34
			$settings = array(
35
				'tinymce' => array(
36
					'resize' => true,
37
					'wp_autoresize_on' => true,
38
				),
39
			);
40
41
			add_filter( 'user_can_richedit', '__return_true' );
42
			wp_editor( '', 'carbon_settings', $settings );
43
			remove_filter( 'user_can_richedit', '__return_true' );
44
			?>
45
		</div>
46
		<?php
47
	}
48
49
	/**
50
	 * Returns an array that holds the field data, suitable for JSON representation.
51
	 *
52
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
53
	 * @return array
54
	 */
55
	public function to_json( $load ) {
56
		$field_data = parent::to_json( $load );
57
58
		ob_start();
59
		remove_action( 'media_buttons', 'media_buttons' );
60
		do_action( 'media_buttons' );
61
		add_action( 'media_buttons', 'media_buttons' );
62
		$media_buttons = ob_get_clean();
63
64
		$field_data = array_merge( $field_data, array(
65
			'rich_editing' => user_can_richedit(),
66
			'media_buttons' => $media_buttons,
67
		) );
68
69
		return $field_data;
70
	}
71
}
72