You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

119 lines
4.0 KiB

  1. <?php
  2. /**
  3. * Widget API: WP_Widget_Text class
  4. *
  5. * @package WordPress
  6. * @subpackage Widgets
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement a Text widget.
  11. *
  12. * @since 2.8.0
  13. *
  14. * @see WP_Widget
  15. */
  16. class WP_Widget_Text extends WP_Widget {
  17. /**
  18. * Sets up a new Text widget instance.
  19. *
  20. * @since 2.8.0
  21. * @access public
  22. */
  23. public function __construct() {
  24. $widget_ops = array(
  25. 'classname' => 'widget_text',
  26. 'description' => __( 'Arbitrary text or HTML.' ),
  27. 'customize_selective_refresh' => true,
  28. );
  29. $control_ops = array( 'width' => 400, 'height' => 350 );
  30. parent::__construct( 'text', __( 'Text' ), $widget_ops, $control_ops );
  31. }
  32. /**
  33. * Outputs the content for the current Text widget instance.
  34. *
  35. * @since 2.8.0
  36. * @access public
  37. *
  38. * @param array $args Display arguments including 'before_title', 'after_title',
  39. * 'before_widget', and 'after_widget'.
  40. * @param array $instance Settings for the current Text widget instance.
  41. */
  42. public function widget( $args, $instance ) {
  43. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  44. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
  45. $widget_text = ! empty( $instance['text'] ) ? $instance['text'] : '';
  46. /**
  47. * Filters the content of the Text widget.
  48. *
  49. * @since 2.3.0
  50. * @since 4.4.0 Added the `$this` parameter.
  51. *
  52. * @param string $widget_text The widget content.
  53. * @param array $instance Array of settings for the current widget.
  54. * @param WP_Widget_Text $this Current Text widget instance.
  55. */
  56. $text = apply_filters( 'widget_text', $widget_text, $instance, $this );
  57. echo $args['before_widget'];
  58. if ( ! empty( $title ) ) {
  59. echo $args['before_title'] . $title . $args['after_title'];
  60. } ?>
  61. <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
  62. <?php
  63. echo $args['after_widget'];
  64. }
  65. /**
  66. * Handles updating settings for the current Text widget instance.
  67. *
  68. * @since 2.8.0
  69. * @access public
  70. *
  71. * @param array $new_instance New settings for this instance as input by the user via
  72. * WP_Widget::form().
  73. * @param array $old_instance Old settings for this instance.
  74. * @return array Settings to save or bool false to cancel saving.
  75. */
  76. public function update( $new_instance, $old_instance ) {
  77. $instance = $old_instance;
  78. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  79. if ( current_user_can( 'unfiltered_html' ) ) {
  80. $instance['text'] = $new_instance['text'];
  81. } else {
  82. $instance['text'] = wp_kses_post( $new_instance['text'] );
  83. }
  84. $instance['filter'] = ! empty( $new_instance['filter'] );
  85. return $instance;
  86. }
  87. /**
  88. * Outputs the Text widget settings form.
  89. *
  90. * @since 2.8.0
  91. * @access public
  92. *
  93. * @param array $instance Current settings.
  94. */
  95. public function form( $instance ) {
  96. $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
  97. $filter = isset( $instance['filter'] ) ? $instance['filter'] : 0;
  98. $title = sanitize_text_field( $instance['title'] );
  99. ?>
  100. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  101. <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($title); ?>" /></p>
  102. <p><label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label>
  103. <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo esc_textarea( $instance['text'] ); ?></textarea></p>
  104. <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox"<?php checked( $filter ); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
  105. <?php
  106. }
  107. }