Completed
Push — development ( e6ea3e...8fd056 )
by
unknown
02:56
created

Rich_Text_Field   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 80
ccs 0
cts 29
cp 0
rs 10
wmc 4
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A editor_init() 0 18 1
A field_type_activated() 0 3 1
A upload_image_button_html() 0 6 1
A to_json() 0 21 1
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( 'in_admin_header', 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
	 * Display Upload Image Button
51
	 *
52
	 */
53
	public function upload_image_button_html() {
54
		$upload_image_button = '<a href="#" class="button insert-media add_media" data-editor="<%- id %>" title="Add Media">
55
			<span class="wp-media-buttons-icon"></span> Add Media
56
		</a>';
57
		echo apply_filters( 'crb_upload_image_button_html', $upload_image_button, $this->base_name );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'apply_filters'
Loading history...
58
	}
59
60
	/**
61
	 * Returns an array that holds the field data, suitable for JSON representation.
62
	 *
63
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
64
	 * @return array
65
	 */
66
	public function to_json( $load ) {
67
		$field_data = parent::to_json( $load );
68
69
		ob_start();
70
		remove_action( 'media_buttons', 'media_buttons' );
71
72
		$this->upload_image_button_html();
73
74
		do_action( 'media_buttons' );
75
76
		add_action( 'media_buttons', 'media_buttons' );
77
78
		$media_buttons = apply_filters( 'crb_media_buttons_html', ob_get_clean(), $this->base_name );
79
80
		$field_data = array_merge( $field_data, array(
81
			'rich_editing' => user_can_richedit(),
82
			'media_buttons' => $media_buttons,
83
		) );
84
85
		return $field_data;
86
	}
87
}
88