Нет описания

class-widget.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * WPForms widget.
  4. *
  5. * @since 1.0.2
  6. */
  7. class WPForms_Widget extends WP_Widget {
  8. /**
  9. * Hold widget settings defaults, populated in constructor.
  10. *
  11. * @since 1.0.2
  12. *
  13. * @var array
  14. */
  15. protected $defaults;
  16. /**
  17. * Constructor
  18. *
  19. * @since 1.0.2
  20. */
  21. public function __construct() {
  22. // Widget defaults.
  23. $this->defaults = array(
  24. 'title' => '',
  25. 'form_id' => '',
  26. 'show_title' => false,
  27. 'show_desc' => false,
  28. );
  29. // Widget Slug.
  30. $widget_slug = 'wpforms-widget';
  31. // Widget basics.
  32. $widget_ops = array(
  33. 'classname' => $widget_slug,
  34. 'description' => esc_html_x( 'Display a form.', 'Widget', 'wpforms-lite' ),
  35. );
  36. // Widget controls.
  37. $control_ops = array(
  38. 'id_base' => $widget_slug,
  39. );
  40. // Load widget.
  41. parent::__construct( $widget_slug, esc_html_x( 'WPForms', 'Widget', 'wpforms-lite' ), $widget_ops, $control_ops );
  42. }
  43. /**
  44. * Output the HTML for this widget.
  45. *
  46. * @since 1.0.2
  47. *
  48. * @param array $args An array of standard parameters for widgets in this theme.
  49. * @param array $instance An array of settings for this widget instance.
  50. */
  51. public function widget( $args, $instance ) {
  52. // Merge with defaults.
  53. $instance = wp_parse_args( (array) $instance, $this->defaults );
  54. echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  55. // Title.
  56. if ( ! empty( $instance['title'] ) ) {
  57. echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  58. }
  59. // Form.
  60. if ( ! empty( $instance['form_id'] ) ) {
  61. wpforms()->frontend->output( absint( $instance['form_id'] ), $instance['show_title'], $instance['show_desc'] );
  62. }
  63. echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  64. }
  65. /**
  66. * Deal with the settings when they are saved by the admin. Here is
  67. * where any validation should be dealt with.
  68. *
  69. * @since 1.0.2
  70. *
  71. * @param array $new_instance An array of new settings as submitted by the admin.
  72. * @param array $old_instance An array of the previous settings.
  73. *
  74. * @return array The validated and (if necessary) amended settings
  75. */
  76. public function update( $new_instance, $old_instance ) {
  77. $new_instance['title'] = wp_strip_all_tags( $new_instance['title'] );
  78. $new_instance['form_id'] = ! empty( $new_instance['form_id'] ) ? (int) $new_instance['form_id'] : 0;
  79. $new_instance['show_title'] = isset( $new_instance['show_title'] ) ? '1' : false;
  80. $new_instance['show_desc'] = isset( $new_instance['show_desc'] ) ? '1' : false;
  81. return $new_instance;
  82. }
  83. /**
  84. * Display the form for this widget on the Widgets page of the WP Admin area.
  85. *
  86. * @since 1.0.2
  87. *
  88. * @param array $instance An array of the current settings for this widget.
  89. */
  90. public function form( $instance ) {
  91. // Merge with defaults.
  92. $instance = wp_parse_args( (array) $instance, $this->defaults );
  93. ?>
  94. <p>
  95. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
  96. <?php echo esc_html( _x( 'Title:', 'Widget', 'wpforms-lite' ) ); ?>
  97. </label>
  98. <input type="text"
  99. id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
  100. name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
  101. value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat"/>
  102. </p>
  103. <p>
  104. <label for="<?php echo esc_attr( $this->get_field_id( 'form_id' ) ); ?>">
  105. <?php echo esc_html( _x( 'Form:', 'Widget', 'wpforms-lite' ) ); ?>
  106. </label>
  107. <select class="widefat"
  108. id="<?php echo esc_attr( $this->get_field_id( 'form_id' ) ); ?>"
  109. name="<?php echo esc_attr( $this->get_field_name( 'form_id' ) ); ?>">
  110. <?php
  111. $forms = wpforms()->form->get();
  112. if ( ! empty( $forms ) ) {
  113. echo '<option value="" selected disabled>' . esc_html_x( 'Select your form', 'Widget', 'wpforms-lite' ) . '</option>';
  114. foreach ( $forms as $form ) {
  115. echo '<option value="' . esc_attr( $form->ID ) . '" ' . selected( $instance['form_id'], $form->ID, false ) . '>' . esc_html( $form->post_title ) . '</option>';
  116. }
  117. } else {
  118. echo '<option value="">' . esc_html_x( 'No forms', 'Widget', 'wpforms-lite' ) . '</option>';
  119. }
  120. ?>
  121. </select>
  122. </p>
  123. <p>
  124. <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_title' ) ); ?>"
  125. name="<?php echo esc_attr( $this->get_field_name( 'show_title' ) ); ?>" <?php checked( '1', $instance['show_title'] ); ?>>
  126. <label for="<?php echo esc_attr( $this->get_field_id( 'show_title' ) ); ?>">
  127. <?php echo esc_html( _x( 'Display form name', 'Widget', 'wpforms-lite' ) ); ?>
  128. </label>
  129. <br>
  130. <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_desc' ) ); ?>"
  131. name="<?php echo esc_attr( $this->get_field_name( 'show_desc' ) ); ?>" <?php checked( '1', $instance['show_desc'] ); ?>>
  132. <label for="<?php echo esc_attr( $this->get_field_id( 'show_desc' ) ); ?>">
  133. <?php echo esc_html( _x( 'Display form description', 'Widget', 'wpforms-lite' ) ); ?>
  134. </label>
  135. </p>
  136. <?php
  137. }
  138. }
  139. /**
  140. * Register WPForms plugin widgets.
  141. */
  142. function wpforms_register_widgets() {
  143. register_widget( 'WPForms_Widget' );
  144. }
  145. add_action( 'widgets_init', 'wpforms_register_widgets' );