WP_Widget_Black_Studio_TinyMCE   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 138
rs 10
c 0
b 0
f 0
wmc 17
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
B widget() 0 22 6
A update() 0 19 4
B form() 0 29 6
1
<?php
2
/**
3
 * Black Studio TinyMCE Widget - Widget class
4
 *
5
 * @package Black_Studio_TinyMCE_Widget
6
 */
7
8
namespace Black_Studio_TinyMCE_Widget;
9
10
use WP_Widget;
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
if ( ! class_exists( 'WP_Widget_Black_Studio_TinyMCE' ) ) {
18
19
	/**
20
	 * Widget class
21
	 *
22
	 * @package Black_Studio_TinyMCE_Widget
23
	 * @since 0.5
24
	 */
25
	class WP_Widget_Black_Studio_TinyMCE extends WP_Widget {
26
27
		/**
28
		 * Widget Class constructor
29
		 *
30
		 * @uses WP_Widget::__construct()
31
		 * @since 0.5
32
		 */
33
		public function __construct() {
34
			/* translators: title of the widget */
35
			$widget_title = __( 'Visual Editor', 'black-studio-tinymce-widget' );
36
			/* translators: description of the widget, shown in available widgets */
37
			$widget_description = __( 'Arbitrary text or HTML with visual editor', 'black-studio-tinymce-widget' );
38
			$widget_ops         = array(
39
				'classname'   => 'widget_black_studio_tinymce',
40
				'description' => $widget_description,
41
			);
42
			$control_ops        = array(
43
				'width'  => 800,
44
				'height' => 600,
45
			);
46
			parent::__construct( 'black-studio-tinymce', $widget_title, $widget_ops, $control_ops );
47
		}
48
49
		/**
50
		 * Output widget HTML code
51
		 *
52
		 * @uses apply_filters()
53
		 * @uses WP_Widget::$id_base
54
		 *
55
		 * @param string[] $args     Array of arguments.
56
		 * @param mixed[]  $instance Widget instance.
57
		 * @return void
58
		 * @since 0.5
59
		 */
60
		public function widget( $args, $instance ) {
61
			$before_widget = $args['before_widget'];
62
			$after_widget  = $args['after_widget'];
63
			$before_title  = $args['before_title'];
64
			$after_title   = $args['after_title'];
65
			do_action( 'black_studio_tinymce_before_widget', $args, $instance );
66
			$before_text = apply_filters( 'black_studio_tinymce_before_text', '<div class="textwidget">', $instance );
67
			$after_text  = apply_filters( 'black_studio_tinymce_after_text', '</div>', $instance );
68
			$title       = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
69
			$text        = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance, $this );
70
			$hide_empty  = apply_filters( 'black_studio_tinymce_hide_empty', false, $instance );
71
			if ( ! ( $hide_empty && empty( $text ) ) ) {
72
				$output = $before_widget;
73
				if ( ! empty( $title ) ) {
74
					$output .= $before_title . $title . $after_title;
75
				}
76
				$output .= $before_text . $text . $after_text;
77
				$output .= $after_widget;
78
				echo $output; // xss ok.
79
			}
80
			do_action( 'black_studio_tinymce_after_widget', $args, $instance );
81
		}
82
83
		/**
84
		 * Update widget data
85
		 *
86
		 * @uses current_user_can()
87
		 * @uses wp_filter_post_kses()
88
		 * @uses apply_filters()
89
		 *
90
		 * @param mixed[] $new_instance New widget instance.
91
		 * @param mixed[] $old_instance Old widget instance.
92
		 * @return mixed[]
93
		 * @since 0.5
94
		 */
95
		public function update( $new_instance, $old_instance ) {
96
			$instance          = $old_instance;
97
			$instance['title'] = strip_tags( $new_instance['title'] );
98
			if ( current_user_can( 'unfiltered_html' ) ) {
99
				$instance['text'] = $new_instance['text'];
100
			} else {
101
				$instance['text'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text'] ) ) ); // wp_filter_post_kses() expects slashed.
102
			}
103
			$instance['type']   = strip_tags( $new_instance['type'] );
104
			$instance['filter'] = strip_tags( $new_instance['filter'] );
105
			$additional_fields  = apply_filters( 'black_studio_tinymce_widget_additional_fields', array() );
106
			if ( ! empty( $additional_fields ) ) {
107
				foreach ( $additional_fields as $additional_field ) {
108
					$instance[ $additional_field ] = $new_instance[ $additional_field ];
109
				}
110
			}
111
			$instance = apply_filters( 'black_studio_tinymce_widget_update', $instance, $this );
112
			return $instance;
113
		}
114
115
		/**
116
		 * Output widget form
117
		 *
118
		 * @uses wp_parse_args()
119
		 * @uses apply_filters()
120
		 * @uses esc_attr()
121
		 * @uses esc_textarea()
122
		 * @uses WP_Widget::get_field_id()
123
		 * @uses WP_Widget::get_field_name()
124
		 * @uses _e()
125
		 * @uses do_action()
126
		 * @uses apply_filters()
127
		 *
128
		 * @param mixed[] $instance Widget instance.
129
		 * @return void
130
		 * @since 0.5
131
		 */
132
		public function form( $instance ) {
133
			global $wp_customize;
134
			$instance = wp_parse_args( (array) $instance, array(
135
				'title' => '',
136
				'text'  => '',
137
				'type'  => 'visual',
138
			) );
139
			// Force Visual mode in Customizer (to avoid glitches).
140
			if ( $wp_customize && ! defined( 'DOING_AJAX' ) ) {
141
				$instance['type'] = 'visual';
142
			}
143
			// Guess (wpautop) filter value for widgets created with previous version.
144
			if ( ! isset( $instance['filter'] ) ) {
145
				$instance['filter'] = 'visual' === $instance['type'] && substr( $instance['text'], 0, 3 ) !== '<p>' ? 1 : 0;
146
			}
147
			$title = strip_tags( $instance['title'] );
148
			do_action( 'black_studio_tinymce_before_editor', $instance, $this );
149
			?>
150
			<input id="<?php echo esc_attr( $this->get_field_id( 'type' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>" type="hidden" value="<?php echo esc_attr( $instance['type'] ); ?>" />
151
			<p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:' ); ?></label>
152
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
153
			<?php
154
			do_action( 'black_studio_tinymce_editor', $instance['text'], $this->get_field_id( 'text' ), $this->get_field_name( 'text' ), $instance['type'] );
155
			do_action( 'black_studio_tinymce_after_editor', $instance, $this );
156
			?>
157
			<input id="<?php echo esc_attr( $this->get_field_id( 'filter' ) ); ?>-hidden" name="<?php echo esc_attr( $this->get_field_name( 'filter' ) ); ?>" type="hidden" value="0" />
158
			<p><input id="<?php echo esc_attr( $this->get_field_id( 'filter' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'filter' ) ); ?>" type="checkbox" value="1" <?php checked( $instance['filter'] ); ?> />&nbsp;<label for="<?php echo esc_attr( $this->get_field_id( 'filter' ) ); ?>"><?php esc_html_e( 'Automatically add paragraphs' ); ?></label></p>
159
			<?php
160
		}
161
162
	} // END class
163
} // END class_exists
164