self = get_class( $this ); parent::__construct( strtolower( $this->self ), esc_html__( 'GiveWP - Donation Form', 'give' ), [ 'description' => esc_html__( 'Display a GiveWP Donation Form in your theme\'s widget powered sidebar.', 'give' ), ] ); add_action( 'widgets_init', [ $this, 'widget_init' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'admin_widget_scripts' ] ); } /** * Load widget assets only on the widget page * * @return void */ public function admin_widget_scripts() { global $pagenow; // Load script only on widgets.php page. if ( ! in_array( $pagenow, [ 'widgets.php', 'customize.php' ] ) ) { return; } // Directories of assets. $js_dir = GIVE_PLUGIN_URL . 'assets/dist/'; wp_enqueue_script( "{$this->scriptHandle}-js", $js_dir . 'js/admin-widgets.js', [ 'give-admin-scripts' ], GIVE_VERSION, false ); wp_enqueue_style( "{$this->scriptHandle}-css", $js_dir . 'css/admin-widgets.css', [], 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 ); // Exit do not have valid for id. if ( ! array_key_exists( 'id', $instance ) || empty( $instance['id'] ) ) { return; } $form_id = (int) $instance['id']; $isLegacyForm = FormUtils::isLegacyForm( $form_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. // Use alias setting to set display setting when form template other then Legacy. if ( ! $isLegacyForm ) { $instance['display_style'] = $instance['tmp_display_style']; $instance['continue_button_title'] = $instance['tmp_continue_button_title']; unset( $instance['tmp_display_style'], $instance['tmp_continue_button_title'] ); if ( 'button' === $instance['display_style'] && ! empty( $instance['introduction_text'] ) ) { printf( '
%1$s
', $instance['introduction_text'] ); } } echo give_form_shortcode( $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 = [ 'title' => '', 'id' => 0, 'float_labels' => 'global', 'display_style' => 'modal', 'show_content' => 'none', 'continue_button_title' => __( 'Continue', 'give' ), 'introduction_text' => __( 'Help our organization by donating today! All donations go directly to making a difference for our cause.', 'give' ), 'button_text' => __( 'Donate Now', 'give' ), 'button_color' => '#28C77B', // These settings are aliases for shortcode setting which prevent conflict when saving and showing setting. Later we will use them to set original settings. 'tmp_display_style' => 'button', 'tmp_continue_button_title' => __( 'Continue', 'give' ), ]; $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']; $this->getScriptForBuilders(); ?> 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' ); } /** * Get inline script for widget to add support for widget in page builder like Divi, Elementor and Beaver Builder. * * @since 2.7.0 */ private function getScriptForBuilders() { global $pagenow; // Do not output inline script if admin widget script already printed. if ( wp_script_is( "{$this->scriptHandle}-js", 'done' ) || in_array( $pagenow, [ 'widgets.php', 'customize.php' ] ) ) { return; } $this->widgetIdentifier = uniqid(); $containerId = $this->widgetIdPrefix . $this->widgetIdentifier; ?>