default-widgets.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Widget API: Default core widgets
  4. *
  5. * @package WordPress
  6. * @subpackage Widgets
  7. * @since 2.8.0
  8. */
  9. /** WP_Widget_Pages class */
  10. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-pages.php' );
  11. /** WP_Widget_Links class */
  12. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-links.php' );
  13. /** WP_Widget_Search class */
  14. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-search.php' );
  15. /** WP_Widget_Archives class */
  16. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-archives.php' );
  17. /** WP_Widget_Media class */
  18. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-media.php' );
  19. /** WP_Widget_Media_Audio class */
  20. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-media-audio.php' );
  21. /** WP_Widget_Media_Image class */
  22. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-media-image.php' );
  23. /** WP_Widget_Media_Video class */
  24. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-media-video.php' );
  25. /** WP_Widget_Meta class */
  26. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-meta.php' );
  27. /** WP_Widget_Meta class */
  28. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-meta.php' );
  29. /** WP_Widget_Calendar class */
  30. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-calendar.php' );
  31. /** WP_Widget_Text class */
  32. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-text.php' );
  33. /** WP_Widget_Categories class */
  34. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-categories.php' );
  35. /** WP_Widget_Recent_Posts class */
  36. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-recent-posts.php' );
  37. /** WP_Widget_Recent_Comments class */
  38. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-recent-comments.php' );
  39. /** WP_Widget_RSS class */
  40. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-rss.php' );
  41. /** WP_Widget_Tag_Cloud class */
  42. require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-tag-cloud.php' );
  43. /** WP_Nav_Menu_Widget class */
  44. require_once( ABSPATH . WPINC . '/widgets/class-wp-nav-menu-widget.php' );
  45. /**
  46. * Core class used to implement a Custom HTML widget.
  47. *
  48. * Note that this class is only located in this file in the 4.8 branch
  49. * for the sake of automatic updates. In 4.9 and above, it is located at
  50. * `wp-includes/widgets/class-wp-widget-custom-html.php`.
  51. *
  52. * @since 4.8.1
  53. *
  54. * @see WP_Widget
  55. */
  56. class WP_Widget_Custom_HTML extends WP_Widget {
  57. /**
  58. * Default instance.
  59. *
  60. * @since 4.8.1
  61. * @var array
  62. */
  63. protected $default_instance = array(
  64. 'title' => '',
  65. 'content' => '',
  66. );
  67. /**
  68. * Sets up a new Custom HTML widget instance.
  69. *
  70. * @since 4.8.1
  71. */
  72. public function __construct() {
  73. $widget_ops = array(
  74. 'classname' => 'widget_custom_html',
  75. 'description' => __( 'Arbitrary HTML code.' ),
  76. 'customize_selective_refresh' => true,
  77. );
  78. $control_ops = array(
  79. 'width' => 400,
  80. 'height' => 350,
  81. );
  82. parent::__construct( 'custom_html', __( 'Custom HTML' ), $widget_ops, $control_ops );
  83. }
  84. /**
  85. * Outputs the content for the current Custom HTML widget instance.
  86. *
  87. * @since 4.8.1
  88. *
  89. * @param array $args Display arguments including 'before_title', 'after_title',
  90. * 'before_widget', and 'after_widget'.
  91. * @param array $instance Settings for the current Custom HTML widget instance.
  92. */
  93. public function widget( $args, $instance ) {
  94. $instance = array_merge( $this->default_instance, $instance );
  95. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  96. $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
  97. // Prepare instance data that looks like a normal Text widget.
  98. $simulated_text_widget_instance = array_merge( $instance, array(
  99. 'text' => isset( $instance['content'] ) ? $instance['content'] : '',
  100. 'filter' => false, // Because wpautop is not applied.
  101. 'visual' => false, // Because it wasn't created in TinyMCE.
  102. ) );
  103. unset( $simulated_text_widget_instance['content'] ); // Was moved to 'text' prop.
  104. /** This filter is documented in wp-includes/widgets/class-wp-widget-text.php */
  105. $content = apply_filters( 'widget_text', $instance['content'], $simulated_text_widget_instance, $this );
  106. /**
  107. * Filters the content of the Custom HTML widget.
  108. *
  109. * @since 4.8.1
  110. *
  111. * @param string $content The widget content.
  112. * @param array $instance Array of settings for the current widget.
  113. * @param WP_Widget_Custom_HTML $this Current Custom HTML widget instance.
  114. */
  115. $content = apply_filters( 'widget_custom_html_content', $content, $instance, $this );
  116. // Inject the Text widget's container class name alongside this widget's class name for theme styling compatibility.
  117. $args['before_widget'] = preg_replace( '/(?<=\sclass=["\'])/', 'widget_text ', $args['before_widget'] );
  118. echo $args['before_widget'];
  119. if ( ! empty( $title ) ) {
  120. echo $args['before_title'] . $title . $args['after_title'];
  121. }
  122. echo '<div class="textwidget custom-html-widget">'; // The textwidget class is for theme styling compatibility.
  123. echo $content;
  124. echo '</div>';
  125. echo $args['after_widget'];
  126. }
  127. /**
  128. * Handles updating settings for the current Custom HTML widget instance.
  129. *
  130. * @since 4.8.1
  131. *
  132. * @param array $new_instance New settings for this instance as input by the user via
  133. * WP_Widget::form().
  134. * @param array $old_instance Old settings for this instance.
  135. * @return array Settings to save or bool false to cancel saving.
  136. */
  137. public function update( $new_instance, $old_instance ) {
  138. $instance = array_merge( $this->default_instance, $old_instance );
  139. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  140. if ( current_user_can( 'unfiltered_html' ) ) {
  141. $instance['content'] = $new_instance['content'];
  142. } else {
  143. $instance['content'] = wp_kses_post( $new_instance['content'] );
  144. }
  145. return $instance;
  146. }
  147. /**
  148. * Outputs the Custom HTML widget settings form.
  149. *
  150. * @since 4.8.1
  151. *
  152. * @param array $instance Current instance.
  153. * @returns void
  154. */
  155. public function form( $instance ) {
  156. $instance = wp_parse_args( (array) $instance, $this->default_instance );
  157. ?>
  158. <p>
  159. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  160. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>"/>
  161. </p>
  162. <p>
  163. <label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php _e( 'Content:' ); ?></label>
  164. <textarea class="widefat code" rows="16" cols="20" id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>"><?php echo esc_textarea( $instance['content'] ); ?></textarea>
  165. </p>
  166. <?php if ( ! current_user_can( 'unfiltered_html' ) ) : ?>
  167. <?php
  168. $probably_unsafe_html = array( 'script', 'iframe', 'form', 'input', 'style' );
  169. $allowed_html = wp_kses_allowed_html( 'post' );
  170. $disallowed_html = array_diff( $probably_unsafe_html, array_keys( $allowed_html ) );
  171. ?>
  172. <?php if ( ! empty( $disallowed_html ) ) : ?>
  173. <p>
  174. <?php _e( 'Some HTML tags are not permitted, including:' ); ?>
  175. <code><?php echo join( '</code>, <code>', $disallowed_html ); ?></code>
  176. </p>
  177. <?php endif; ?>
  178. <?php endif; ?>
  179. <?php
  180. }
  181. }