Widget.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Adds Ninja Forms widget.
  4. */
  5. class NF_Widget extends WP_Widget {
  6. /**
  7. * Register widget with WordPress.
  8. */
  9. public function __construct() {
  10. parent::__construct(
  11. 'ninja_forms_widget', // Base ID
  12. 'Ninja Forms Widget', // Name
  13. array( 'description' => __( 'Ninja Forms Widget', 'ninja-forms' ), ) // Args
  14. );
  15. }
  16. /**
  17. * Front-end display of widget.
  18. *
  19. * @see WP_Widget::widget()
  20. *
  21. * @param array $args Widget arguments.
  22. * @param array $instance Saved values from database.
  23. */
  24. public function widget( $args, $instance ) {
  25. $form = Ninja_Forms()->form( $instance['form_id'] )->get();
  26. $title = $form->get_setting( 'title' );
  27. $title = apply_filters( 'widget_title', $title );
  28. $display_title = $instance['display_title'];
  29. echo $args[ 'before_widget' ];
  30. if ( ! empty( $title ) AND $display_title == 1 )
  31. echo $args[ 'before_title' ] . esc_html( $title ) . $args[ 'after_title' ];
  32. Ninja_Forms()->display( $instance['form_id'] );
  33. echo $args[ 'after_widget' ];
  34. }
  35. /**
  36. * Sanitize widget form values as they are saved.
  37. *
  38. * @see WP_Widget::update()
  39. *
  40. * @param array $new_instance Values just sent to be saved.
  41. * @param array $old_instance Previously saved values from database.
  42. *
  43. * @return array Updated safe values to be saved.
  44. */
  45. public function update( $new_instance, $old_instance ) {
  46. $instance = array();
  47. $instance['form_id'] = $new_instance['form_id'];
  48. $instance['display_title'] = $new_instance['display_title'];
  49. return $instance;
  50. }
  51. /**
  52. * Back-end widget form.
  53. *
  54. * @see WP_Widget::form()
  55. *
  56. * @param array $instance Previously saved values from database.
  57. */
  58. public function form( $instance ) {
  59. if( isset( $instance['form_id'] ) ){
  60. $form_id = $instance['form_id'];
  61. }else{
  62. $form_id = '';
  63. }
  64. if( isset( $instance['display_title'] ) ){
  65. $display_title = $instance['display_title'];
  66. }else{
  67. $display_title = 0;
  68. }
  69. ?>
  70. <p>
  71. <label>
  72. <?php _e( 'Display Title', 'ninja-forms' ); ?>
  73. <input type="hidden" value="0" name="<?php echo esc_attr( $this->get_field_name( 'display_title' ) ); ?>">
  74. <input type="checkbox" value="1" id="<?php echo esc_attr( $this->get_field_id( 'display_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_title' ) ); ?>" <?php checked( $display_title, 1 );?>>
  75. </label>
  76. </p>
  77. <p>
  78. <select id="<?php echo esc_attr( $this->get_field_id( 'form_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'form_id' ) ); ?>">
  79. <option value="0">-- <?php _e('None', 'ninja-forms');?></option>
  80. <?php
  81. $all_forms = Ninja_Forms()->form()->get_forms();
  82. foreach($all_forms as $form){
  83. ?>
  84. <option value="<?php echo intval( $form->get_id() );?>" <?php selected( $form->get_id(), $form_id );?>>
  85. <?php echo esc_html( $form->get_setting( 'title' ) );?>
  86. </option>
  87. <?php
  88. }
  89. ?>
  90. </select>
  91. </p>
  92. <?php
  93. }
  94. } // class Foo_Widget
  95. /**
  96. * Register NF widget
  97. *
  98. * @see 'widgets_init'
  99. */
  100. function NF_register_widgets() {
  101. register_widget( 'NF_Widget' );
  102. }
  103. add_action( 'widgets_init', 'NF_register_widgets' );