self = get_class( $this ); parent::__construct( strtolower( $this->self ), esc_html__( 'Give - Donation Form', 'give' ), array( 'description' => esc_html__( 'Display a Give Donation Form in your theme\'s widget powered sidebar.', 'give' ), ) ); add_action( 'widgets_init', array( $this, 'widget_init' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_widget_scripts' ) ); } /** * Load widget assets only on the widget page * * @param string $hook Use it to target a specific admin page. * * @return void */ public function admin_widget_scripts( $hook ) { // Directories of assets. $js_dir = GIVE_PLUGIN_URL . 'assets/js/admin/'; $js_plugins = GIVE_PLUGIN_URL . 'assets/js/plugins/'; $css_dir = GIVE_PLUGIN_URL . 'assets/css/'; // Use minified libraries if SCRIPT_DEBUG is turned off. $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; // Widget Script. if ( 'widgets.php' === $hook ) { wp_enqueue_script( 'give-admin-widgets-scripts', $js_dir . 'admin-widgets' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false ); } } /** * Echo the widget content. * * @param array $args Display arguments including before_title, after_title, * before_widget, and after_widget. * @param array $instance The settings for the particular instance of the widget. */ public function widget( $args, $instance ) { $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $form_id = (int) $instance['id']; echo $args['before_widget']; // XSS ok. /** * Fires before widget settings form in the admin area. * * @param integer $form_id Form ID. * * @since 1.0 */ do_action( 'give_before_forms_widget', $form_id ); echo $title ? $args['before_title'] . $title . $args['after_title'] : ''; // XSS ok. give_get_donation_form( $instance ); echo $args['after_widget']; // XSS ok. /** * Fires after widget settings form in the admin area. * * @param integer $form_id Form ID. * * @since 1.0 */ do_action( 'give_after_forms_widget', $form_id ); } /** * Output the settings update form. * * @param array $instance Current settings. */ public function form( $instance ) { $defaults = array( 'title' => '', 'id' => '', 'float_labels' => 'global', 'display_style' => 'modal', 'show_content' => 'none', 'continue_button_title' => '', ); $instance = wp_parse_args( (array) $instance, $defaults ); // Backward compatibility: Set float labels as default if, it was set as empty previous. $instance['float_labels'] = empty( $instance['float_labels'] ) ? 'global' : $instance['float_labels']; // Query Give Forms. $args = array( 'post_type' => 'give_forms', 'posts_per_page' => - 1, 'post_status' => 'publish', ); $give_forms = get_posts( $args ); ?>
self ); } /** * Update the widget * * @param array $new_instance The new options. * @param array $old_instance The previous options. * * @return array */ public function update( $new_instance, $old_instance ) { $this->flush_widget_cache(); return $new_instance; } /** * Flush widget cache * * @return void */ public function flush_widget_cache() { wp_cache_delete( $this->self, 'widget' ); } } new Give_Forms_Widget();