/includes/Widget.php

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