NF_Widget

Adds Ninja Forms widget.

Defined (1)

The class is defined in the following location(s).

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