| @@ -1,16 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Give Form Widget |
| 4 | 4 | * |
| 5 | - * @package WordImpress | |
| 5 | + * @package GiveWP | |
| 6 | 6 | * @subpackage Admin/Forms |
| 7 | - * @copyright Copyright (c) 2016, WordImpress | |
| 7 | + * @copyright Copyright (c) 2016, GiveWP | |
| 8 | 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | 9 | * @since 1.0 |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | // Exit if accessed directly. |
| 13 | +use Give\Helpers\Form\Utils as FormUtils; | |
| 14 | + | |
| 13 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | 16 | exit; |
| 15 | 17 | } |
| 16 | 18 | |
| @@ -19,10 +21,37 @@ | ||
| 19 | 21 | * |
| 20 | 22 | * @since 1.0 |
| 21 | 23 | */ |
| 22 | 24 | class Give_Forms_Widget extends WP_Widget { |
| 25 | + /** | |
| 26 | + * Script handle name. | |
| 27 | + * | |
| 28 | + * @since 2.7.0 | |
| 29 | + * @var string | |
| 30 | + */ | |
| 31 | + private $scriptHandle = 'give-admin-widgets-scripts'; | |
| 23 | 32 | |
| 24 | 33 | /** |
| 34 | + * Widget identifier. | |
| 35 | + * | |
| 36 | + * We will use this to assign unique id to widget setting container and to generate unique inline script. | |
| 37 | + * | |
| 38 | + * @since 2.7.0 | |
| 39 | + * @var string | |
| 40 | + */ | |
| 41 | + private $widgetIdentifier = ''; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Widget id prefix. | |
| 45 | + * | |
| 46 | + * We will use this to assign unique id to widget setting container. | |
| 47 | + * | |
| 48 | + * @since 2.7.0 | |
| 49 | + * @var string | |
| 50 | + */ | |
| 51 | + private $widgetIdPrefix = 'give_forms_widget_container-'; | |
| 52 | + | |
| 53 | + /** | |
| 25 | 54 | * The widget class name |
| 26 | 55 | * |
| 27 | 56 | * @var string |
| 28 | 57 | */ |
| @@ -32,43 +61,38 @@ | ||
| 32 | 61 | * Instantiate the class |
| 33 | 62 | */ |
| 34 | 63 | public function __construct() { |
| 35 | 64 | $this->self = get_class( $this ); |
| 36 | - | |
| 37 | 65 | parent::__construct( |
| 38 | 66 | strtolower( $this->self ), |
| 39 | - esc_html__( 'Give - Donation Form', 'give' ), | |
| 40 | - array( | |
| 41 | - 'description' => esc_html__( 'Display a Give Donation Form in your theme\'s widget powered sidebar.', 'give' ), | |
| 42 | - ) | |
| 67 | + esc_html__( 'GiveWP - Donation Form', 'give' ), | |
| 68 | + [ | |
| 69 | + 'description' => esc_html__( 'Display a GiveWP Donation Form in your theme\'s widget powered sidebar.', 'give' ), | |
| 70 | + ] | |
| 43 | 71 | ); |
| 44 | 72 | |
| 45 | - add_action( 'widgets_init', array( $this, 'widget_init' ) ); | |
| 46 | - add_action( 'admin_enqueue_scripts', array( $this, 'admin_widget_scripts' ) ); | |
| 73 | + add_action( 'widgets_init', [ $this, 'widget_init' ] ); | |
| 74 | + add_action( 'admin_enqueue_scripts', [ $this, 'admin_widget_scripts' ] ); | |
| 47 | 75 | } |
| 48 | 76 | |
| 49 | 77 | /** |
| 50 | 78 | * Load widget assets only on the widget page |
| 51 | 79 | * |
| 52 | - * @param string $hook Use it to target a specific admin page. | |
| 53 | - * | |
| 54 | 80 | * @return void |
| 55 | 81 | */ |
| 56 | - public function admin_widget_scripts( $hook ) { | |
| 82 | + public function admin_widget_scripts() { | |
| 83 | + global $pagenow; | |
| 57 | 84 | |
| 85 | + // Load script only on widgets.php page. | |
| 86 | + if ( ! in_array( $pagenow, [ 'widgets.php', 'customize.php' ] ) ) { | |
| 87 | + return; | |
| 88 | + } | |
| 89 | + | |
| 58 | 90 | // Directories of assets. |
| 59 | - $js_dir = GIVE_PLUGIN_URL . 'assets/js/admin/'; | |
| 60 | - $js_plugins = GIVE_PLUGIN_URL . 'assets/js/plugins/'; | |
| 61 | - $css_dir = GIVE_PLUGIN_URL . 'assets/css/'; | |
| 91 | + $js_dir = GIVE_PLUGIN_URL . 'build/assets/dist/'; | |
| 62 | 92 | |
| 63 | - // Use minified libraries if SCRIPT_DEBUG is turned off. | |
| 64 | - $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; | |
| 65 | - | |
| 66 | - // Widget Script. | |
| 67 | - if ( 'widgets.php' === $hook ) { | |
| 68 | - | |
| 69 | - wp_enqueue_script( 'give-admin-widgets-scripts', $js_dir . 'admin-widgets' . $suffix . '.js', array( 'jquery' ), GIVE_VERSION, false ); | |
| 70 | - } | |
| 93 | + wp_enqueue_script( "{$this->scriptHandle}-js", $js_dir . 'js/admin-widgets.js', [ 'give-admin-scripts' ], GIVE_VERSION, false ); | |
| 94 | + wp_enqueue_style( "{$this->scriptHandle}-css", $js_dir . 'css/admin-widgets.css', [], GIVE_VERSION, false ); | |
| 71 | 95 | } |
| 72 | 96 | |
| 73 | 97 | /** |
| 74 | 98 | * Echo the widget content. |
| @@ -77,12 +101,19 @@ | ||
| 77 | 101 | * before_widget, and after_widget. |
| 78 | 102 | * @param array $instance The settings for the particular instance of the widget. |
| 79 | 103 | */ |
| 80 | 104 | public function widget( $args, $instance ) { |
| 81 | - $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; | |
| 82 | - $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); | |
| 83 | - $form_id = (int) $instance['id']; | |
| 105 | + $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; | |
| 106 | + $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); | |
| 84 | 107 | |
| 108 | + // Exit do not have valid for id. | |
| 109 | + if ( ! array_key_exists( 'id', $instance ) || empty( $instance['id'] ) ) { | |
| 110 | + return; | |
| 111 | + } | |
| 112 | + | |
| 113 | + $form_id = (int) $instance['id']; | |
| 114 | + $isLegacyForm = FormUtils::isLegacyForm( $form_id ); | |
| 115 | + | |
| 85 | 116 | echo $args['before_widget']; // XSS ok. |
| 86 | 117 | |
| 87 | 118 | /** |
| 88 | 119 | * Fires before widget settings form in the admin area. |
| @@ -94,10 +125,25 @@ | ||
| 94 | 125 | do_action( 'give_before_forms_widget', $form_id ); |
| 95 | 126 | |
| 96 | 127 | echo $title ? $args['before_title'] . $title . $args['after_title'] : ''; // XSS ok. |
| 97 | 128 | |
| 98 | - give_get_donation_form( $instance ); | |
| 129 | + // Use alias setting to set display setting when form template other then Legacy. | |
| 130 | + if ( ! $isLegacyForm ) { | |
| 131 | + $instance['display_style'] = $instance['tmp_display_style']; | |
| 132 | + $instance['continue_button_title'] = $instance['tmp_continue_button_title']; | |
| 99 | 133 | |
| 134 | + unset( $instance['tmp_display_style'], $instance['tmp_continue_button_title'] ); | |
| 135 | + | |
| 136 | + if ( 'button' === $instance['display_style'] && ! empty( $instance['introduction_text'] ) ) { | |
| 137 | + printf( | |
| 138 | + '<p>%1$s</p>', | |
| 139 | + $instance['introduction_text'] | |
| 140 | + ); | |
| 141 | + } | |
| 142 | + } | |
| 143 | + | |
| 144 | + echo give_form_shortcode( $instance ); | |
| 145 | + | |
| 100 | 146 | echo $args['after_widget']; // XSS ok. |
| 101 | 147 | |
| 102 | 148 | /** |
| 103 | 149 | * Fires after widget settings form in the admin area. |
| @@ -114,97 +160,150 @@ | ||
| 114 | 160 | * |
| 115 | 161 | * @param array $instance Current settings. |
| 116 | 162 | */ |
| 117 | 163 | public function form( $instance ) { |
| 118 | - $defaults = array( | |
| 119 | - 'title' => '', | |
| 120 | - 'id' => '', | |
| 121 | - 'float_labels' => 'global', | |
| 122 | - 'display_style' => 'modal', | |
| 123 | - 'show_content' => 'none', | |
| 124 | - 'continue_button_title' => '', | |
| 125 | - ); | |
| 164 | + $defaults = [ | |
| 165 | + 'title' => '', | |
| 166 | + 'id' => 0, | |
| 167 | + 'float_labels' => 'global', | |
| 168 | + 'display_style' => 'modal', | |
| 169 | + 'show_content' => 'none', | |
| 170 | + 'continue_button_title' => __( 'Continue', 'give' ), | |
| 171 | + 'introduction_text' => __( 'Help our organization by donating today! All donations go directly to making a difference for our cause.', 'give' ), | |
| 172 | + 'button_text' => __( 'Donate Now', 'give' ), | |
| 173 | + 'button_color' => '#28C77B', | |
| 126 | 174 | |
| 175 | + // These settings are aliases for shortcode setting which prevent conflict when saving and showing setting. Later we will use them to set original settings. | |
| 176 | + 'tmp_display_style' => 'button', | |
| 177 | + 'tmp_continue_button_title' => __( 'Continue', 'give' ), | |
| 178 | + ]; | |
| 179 | + | |
| 127 | 180 | $instance = wp_parse_args( (array) $instance, $defaults ); |
| 128 | 181 | |
| 129 | 182 | // Backward compatibility: Set float labels as default if, it was set as empty previous. |
| 130 | 183 | $instance['float_labels'] = empty( $instance['float_labels'] ) ? 'global' : $instance['float_labels']; |
| 131 | 184 | |
| 132 | - // Query Give Forms. | |
| 133 | - $args = array( | |
| 134 | - 'post_type' => 'give_forms', | |
| 135 | - 'posts_per_page' => - 1, | |
| 136 | - 'post_status' => 'publish', | |
| 137 | - ); | |
| 138 | - | |
| 139 | - $give_forms = get_posts( $args ); | |
| 185 | + $this->getScriptForBuilders(); | |
| 140 | 186 | ?> |
| 141 | - <div class="give_forms_widget_container"> | |
| 187 | + <div id="<?php echo $this->widgetIdPrefix . $this->widgetIdentifier; ?>" class="give_forms_widget_container"> | |
| 142 | 188 | |
| 143 | 189 | <?php // Widget: widget Title. ?> |
| 144 | 190 | <p> |
| 145 | 191 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'give' ); ?></label> |
| 146 | - <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" /><br> | |
| 192 | + <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" /> | |
| 147 | 193 | <small class="give-field-description"><?php esc_html_e( 'Leave blank to hide the widget title.', 'give' ); ?></small> |
| 148 | 194 | </p> |
| 149 | 195 | |
| 150 | 196 | <?php // Widget: Give Form. ?> |
| 151 | - <p> | |
| 152 | - <label for="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>"><?php esc_html_e( 'Give Form:', 'give' ); ?></label> | |
| 153 | - <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>"> | |
| 154 | - <option value="current"><?php esc_html_e( '- Select -', 'give' ); ?></option> | |
| 155 | - <?php foreach ( $give_forms as $give_form ) { ?> | |
| 156 | - <?php /* translators: %s: Title */ ?> | |
| 157 | - <?php $form_title = empty( $give_form->post_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $give_form->ID ) : $give_form->post_title; ?> | |
| 158 | - <option <?php selected( absint( $instance['id'] ), $give_form->ID ); ?> value="<?php echo esc_attr( $give_form->ID ); ?>"><?php echo esc_html( $form_title ); ?></option> | |
| 159 | - <?php } ?> | |
| 160 | - </select><br> | |
| 161 | - <small class="give-field-description"><?php esc_html_e( 'Select a Give Form to embed in this widget.', 'give' ); ?></small> | |
| 197 | + <p class="donation-form give-hidden"> | |
| 198 | + <?php | |
| 199 | + $selectFieldName = esc_attr( $this->get_field_name( 'id' ) ); | |
| 200 | + $selectFieldId = esc_attr( sanitize_key( str_replace( '-', '_', esc_attr( $this->get_field_id( 'id' ) ) ) ) ); | |
| 201 | + printf( | |
| 202 | + '<label for="%1$s">%2$s</label>', | |
| 203 | + $selectFieldId, | |
| 204 | + esc_html__( 'Donation Form:', 'give' ) | |
| 205 | + ); | |
| 206 | + | |
| 207 | + echo Give()->html->forms_dropdown( | |
| 208 | + [ | |
| 209 | + 'selected' => $instance['id'] ?: false, | |
| 210 | + 'id' => $selectFieldId, | |
| 211 | + 'name' => $selectFieldName, | |
| 212 | + 'placeholder' => esc_attr__( '- Select -', 'give' ), | |
| 213 | + 'query_args' => [ | |
| 214 | + 'post_status' => 'publish', | |
| 215 | + ], | |
| 216 | + 'select_atts' => 'style="width: 100%"', | |
| 217 | + ] | |
| 218 | + ); | |
| 219 | + ?> | |
| 220 | + <small class="give-field-description"><?php esc_html_e( 'Select a donation form to use for this widget.', 'give' ); ?></small> | |
| 162 | 221 | </p> |
| 163 | 222 | |
| 164 | - <?php // Widget: Display Style. ?> | |
| 165 | - <p class="give_forms_display_style_setting_row"> | |
| 166 | - <label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>"><?php esc_html_e( 'Display Style:', 'give' ); ?></label><br> | |
| 167 | - <label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-onpage"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-onpage" name="<?php echo esc_attr( $this->get_field_name( 'display_style' ) ); ?>" value="onpage" <?php checked( $instance['display_style'], 'onpage' ); ?>> <?php echo esc_html__( 'All Fields', 'give' ); ?></label> | |
| 168 | - <label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-reveal"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-reveal" name="<?php echo esc_attr( $this->get_field_name( 'display_style' ) ); ?>" value="reveal" <?php checked( $instance['display_style'], 'reveal' ); ?>> <?php echo esc_html__( 'Reveal', 'give' ); ?></label> | |
| 169 | - <label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-modal"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-modal" name="<?php echo esc_attr( $this->get_field_name( 'display_style' ) ); ?>" value="modal" <?php checked( $instance['display_style'], 'modal' ); ?>> <?php echo esc_html__( 'Modal', 'give' ); ?></label> | |
| 170 | - <label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-button"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-button" name="<?php echo esc_attr( $this->get_field_name( 'display_style' ) ); ?>" value="button" <?php checked( $instance['display_style'], 'button' ); ?>> <?php echo esc_html__( 'Button', 'give' ); ?></label><br> | |
| 171 | - <small class="give-field-description"> | |
| 172 | - <?php echo esc_html__( 'Select a Give Form style.', 'give' ); ?> | |
| 173 | - </small> | |
| 174 | - </p> | |
| 223 | + <div class="js-legacy-form-template-settings js-form-template-settings give-hidden"> | |
| 224 | + <legend class="screen-reader-text"><?php _e( 'Options for Legacy form template ', 'give' ); ?></legend> | |
| 225 | + <?php // Widget: Display Style. ?> | |
| 226 | + <p class="give_forms_display_style_setting_row"> | |
| 227 | + <label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>"><?php esc_html_e( 'Display Style:', 'give' ); ?></label> | |
| 228 | + <label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-onpage"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-onpage" name="<?php echo esc_attr( $this->get_field_name( 'display_style' ) ); ?>" value="onpage" <?php checked( $instance['display_style'], 'onpage' ); ?>> <?php echo esc_html__( 'All Fields', 'give' ); ?></label> | |
| 229 | + <label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-reveal"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-reveal" name="<?php echo esc_attr( $this->get_field_name( 'display_style' ) ); ?>" value="reveal" <?php checked( $instance['display_style'], 'reveal' ); ?>> <?php echo esc_html__( 'Reveal', 'give' ); ?></label> | |
| 230 | + <label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-modal"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-modal" name="<?php echo esc_attr( $this->get_field_name( 'display_style' ) ); ?>" value="modal" <?php checked( $instance['display_style'], 'modal' ); ?>> <?php echo esc_html__( 'Modal', 'give' ); ?></label> | |
| 231 | + <label for="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-button"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'display_style' ) ); ?>-button" name="<?php echo esc_attr( $this->get_field_name( 'display_style' ) ); ?>" value="button" <?php checked( $instance['display_style'], 'button' ); ?>> <?php echo esc_html__( 'Button', 'give' ); ?></label> | |
| 232 | + <small class="give-field-description"><?php echo esc_html__( 'Select a donation form style.', 'give' ); ?></small> | |
| 233 | + </p> | |
| 175 | 234 | |
| 176 | - <?php // Widget: Continue Button Title. ?> | |
| 177 | - <p class="give_forms_continue_button_title_setting_row"> | |
| 178 | - <label for="<?php echo esc_attr( $this->get_field_id( 'continue_button_title' ) ); ?>"><?php esc_html_e( 'Button Text:', 'give' ); ?></label> | |
| 179 | - <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'continue_button_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'continue_button_title' ) ); ?>" value="<?php echo esc_attr( $instance['continue_button_title'] ); ?>" /><br> | |
| 180 | - <small class="give-field-description"><?php esc_html_e( 'The button label for displaying the additional payment fields.', 'give' ); ?></small> | |
| 181 | - </p> | |
| 235 | + <?php // Widget: Continue Button Title. ?> | |
| 236 | + <p class="give_forms_continue_button_title_setting_row"> | |
| 237 | + <label for="<?php echo esc_attr( $this->get_field_id( 'continue_button_title' ) ); ?>"><?php esc_html_e( 'Button Text:', 'give' ); ?></label> | |
| 238 | + <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'continue_button_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'continue_button_title' ) ); ?>" value="<?php echo esc_attr( $instance['continue_button_title'] ); ?>" /> | |
| 239 | + <small class="give-field-description"><?php esc_html_e( 'The button label for displaying the additional payment fields.', 'give' ); ?></small> | |
| 240 | + </p> | |
| 182 | 241 | |
| 183 | - <?php // Widget: Floating Labels. ?> | |
| 184 | - <p> | |
| 185 | - <label for="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>"><?php esc_html_e( 'Floating Labels (optional):', 'give' ); ?></label><br> | |
| 186 | - <label for="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-global"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-global" name="<?php echo esc_attr( $this->get_field_name( 'float_labels' ) ); ?>" value="global" <?php checked( $instance['float_labels'], 'global' ); ?>> <?php echo esc_html__( 'Global Option', 'give' ); ?></label> | |
| 187 | - <label for="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-enabled"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-enabled" name="<?php echo esc_attr( $this->get_field_name( 'float_labels' ) ); ?>" value="enabled" <?php checked( $instance['float_labels'], 'enabled' ); ?>> <?php echo esc_html__( 'Yes', 'give' ); ?></label> | |
| 188 | - <label for="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-disabled"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-disabled" name="<?php echo esc_attr( $this->get_field_name( 'float_labels' ) ); ?>" value="disabled" <?php checked( $instance['float_labels'], 'disabled' ); ?>> <?php echo esc_html__( 'No', 'give' ); ?></label><br> | |
| 189 | - <small class="give-field-description"> | |
| 190 | - <?php | |
| 191 | - printf( | |
| 192 | - /* translators: %s: http://docs.givewp.com/form-floating-labels */ | |
| 193 | - esc_html__( 'Override the <a href="%s" target="_blank">floating labels</a> setting for this Give form.', 'give' ), | |
| 194 | - esc_url( 'http://docs.givewp.com/form-floating-labels' ) | |
| 195 | - ); | |
| 196 | - ?> | |
| 242 | + <?php // Widget: Floating Labels. ?> | |
| 243 | + <p> | |
| 244 | + <label for="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>"><?php esc_html_e( 'Floating Labels (optional):', 'give' ); ?></label> | |
| 245 | + <label for="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-global"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-global" name="<?php echo esc_attr( $this->get_field_name( 'float_labels' ) ); ?>" value="global" <?php checked( $instance['float_labels'], 'global' ); ?>> <?php echo esc_html__( 'Global Option', 'give' ); ?></label> | |
| 246 | + <label for="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-enabled"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-enabled" name="<?php echo esc_attr( $this->get_field_name( 'float_labels' ) ); ?>" value="enabled" <?php checked( $instance['float_labels'], 'enabled' ); ?>> <?php echo esc_html__( 'Enabled', 'give' ); ?></label> | |
| 247 | + <label for="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-disabled"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'float_labels' ) ); ?>-disabled" name="<?php echo esc_attr( $this->get_field_name( 'float_labels' ) ); ?>" value="disabled" <?php checked( $instance['float_labels'], 'disabled' ); ?>> <?php echo esc_html__( 'Disabled', 'give' ); ?></label> | |
| 248 | + <small class="give-field-description"> | |
| 249 | + <?php | |
| 250 | + printf( | |
| 251 | + /* translators: %s: Documentation link to http://docs.givewp.com/form-floating-labels */ | |
| 252 | + __( 'Override the <a href="%s" target="_blank">floating labels</a> setting for this GiveWP form.', 'give' ), | |
| 253 | + esc_url( 'http://docs.givewp.com/form-floating-labels' ) | |
| 254 | + ); | |
| 255 | + ?> | |
| 197 | 256 | </small> |
| 198 | - </p> | |
| 257 | + </p> | |
| 199 | 258 | |
| 200 | - <?php // Widget: Display Content. ?> | |
| 201 | - <p> | |
| 202 | - <label for="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>"><?php esc_html_e( 'Display Content (optional):', 'give' ); ?></label><br> | |
| 203 | - <label for="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-none"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-none" name="<?php echo esc_attr( $this->get_field_name( 'show_content' ) ); ?>" value="none" <?php checked( $instance['show_content'], 'none' ); ?>> <?php echo esc_html__( 'None', 'give' ); ?></label> | |
| 204 | - <label for="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-above"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-above" name="<?php echo esc_attr( $this->get_field_name( 'show_content' ) ); ?>" value="above" <?php checked( $instance['show_content'], 'above' ); ?>> <?php echo esc_html__( 'Above', 'give' ); ?></label> | |
| 205 | - <label for="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-below"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-below" name="<?php echo esc_attr( $this->get_field_name( 'show_content' ) ); ?>" value="below" <?php checked( $instance['show_content'], 'below' ); ?>> <?php echo esc_html__( 'Below', 'give' ); ?></label><br> | |
| 206 | - <small class="give-field-description"><?php esc_html_e( 'Override the display content setting for this Give form.', 'give' ); ?></small> | |
| 259 | + <?php // Widget: Display Content. ?> | |
| 260 | + <p> | |
| 261 | + <label for="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>"><?php esc_html_e( 'Display Content (optional):', 'give' ); ?></label> | |
| 262 | + <label for="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-none"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-none" name="<?php echo esc_attr( $this->get_field_name( 'show_content' ) ); ?>" value="none" <?php checked( $instance['show_content'], 'none' ); ?>> <?php echo esc_html__( 'None', 'give' ); ?></label> | |
| 263 | + <label for="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-above"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-above" name="<?php echo esc_attr( $this->get_field_name( 'show_content' ) ); ?>" value="above" <?php checked( $instance['show_content'], 'above' ); ?>> <?php echo esc_html__( 'Above', 'give' ); ?></label> | |
| 264 | + <label for="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-below"><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>-below" name="<?php echo esc_attr( $this->get_field_name( 'show_content' ) ); ?>" value="below" <?php checked( $instance['show_content'], 'below' ); ?>> <?php echo esc_html__( 'Below', 'give' ); ?></label> | |
| 265 | + <small class="give-field-description"><?php esc_html_e( 'Override the display content setting for this GiveWP form.', 'give' ); ?></small> | |
| 266 | + </p> | |
| 267 | + </div> | |
| 268 | + | |
| 269 | + <div class="js-new-form-template-settings js-form-template-settings give-hidden"> | |
| 270 | + <legend class="screen-reader-text"><?php _e( 'Options for Legacy form template ', 'give' ); ?></legend> | |
| 271 | + | |
| 272 | + <?php // Widget: Display Style. ?> | |
| 273 | + <p class="give_forms_display_style_setting_row"> | |
| 274 | + <label for="<?php echo esc_attr( $this->get_field_id( 'tmp_display_style' ) ); ?>"><?php esc_html_e( 'Display Style:', 'give' ); ?></label> | |
| 275 | + <label for="<?php echo esc_attr( $this->get_field_id( 'tmp_display_style' ) ); ?>-button"><span><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'tmp_display_style' ) ); ?>-button" name="<?php echo esc_attr( $this->get_field_name( 'tmp_display_style' ) ); ?>" value="button" <?php checked( $instance['tmp_display_style'], 'button' ); ?>></span><span><?php echo esc_html__( 'Display a button and launch the donation form on click', 'give' ); ?></span></label> | |
| 276 | + <label for="<?php echo esc_attr( $this->get_field_id( 'tmp_display_style' ) ); ?>-onpage"><span><input type="radio" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'tmp_display_style' ) ); ?>-onpage" name="<?php echo esc_attr( $this->get_field_name( 'tmp_display_style' ) ); ?>" value="onpage" <?php checked( $instance['tmp_display_style'], 'onpage' ); ?>></span><span><?php echo esc_html__( 'Display the entire donation form in the sidebar', 'give' ); ?></span></label> | |
| 277 | + <small class="give-field-description"><?php echo esc_html__( 'Select a donation form style.', 'give' ); ?></small> | |
| 278 | + </p> | |
| 279 | + | |
| 280 | + <?php // Widget: Introduction Text. ?> | |
| 281 | + <p class="give_forms_introduction_text_setting_row"> | |
| 282 | + <label for="<?php echo esc_attr( $this->get_field_id( 'introduction_text' ) ); ?>"><?php esc_html_e( 'Widget Text:', 'give' ); ?></label> | |
| 283 | + <textarea id="<?php echo esc_attr( $this->get_field_id( 'introduction_text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'introduction_text' ) ); ?>" class="widefat"><?php echo esc_textarea( $instance['introduction_text'] ); ?></textarea> | |
| 284 | + <small class="give-field-description"><?php esc_html_e( 'Provide an introduction text to invite the visitor to become a donor. Leave this blank to not display any text.', 'give' ); ?></small> | |
| 285 | + </p> | |
| 286 | + | |
| 287 | + <?php // Widget: Continue Button Text. ?> | |
| 288 | + <p class="give_forms_button_text_setting_row"> | |
| 289 | + <label for="<?php echo esc_attr( $this->get_field_id( 'tmp_continue_button_title' ) ); ?>"><?php esc_html_e( 'Button Text:', 'give' ); ?></label> | |
| 290 | + <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'tmp_continue_button_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'tmp_continue_button_title' ) ); ?>" value="<?php echo esc_attr( $instance['tmp_continue_button_title'] ); ?>" /> | |
| 291 | + <small class="give-field-description"><?php esc_html_e( 'This label will appear on button.', 'give' ); ?></small> | |
| 292 | + </p> | |
| 293 | + | |
| 294 | + <?php // Widget: Continue Button Color. ?> | |
| 295 | + <p class="give_forms_button_color_setting_row"> | |
| 296 | + <label for="<?php echo esc_attr( $this->get_field_id( 'button_color' ) ); ?>"><?php esc_html_e( 'Button Color:', 'give' ); ?></label> | |
| 297 | + <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'button_color' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'button_color' ) ); ?>" value="<?php echo esc_attr( $instance['button_color'] ); ?>" /> | |
| 298 | + <small class="give-field-description"><?php esc_html_e( 'Set the color of button.', 'give' ); ?></small> | |
| 299 | + </p> | |
| 300 | + </div> | |
| 301 | + | |
| 302 | + <div class="js-loader"> | |
| 303 | + <p><span class="give-spinner spinner is-show"></span> <i><?php _e( 'Loading settings...', 'give' ); ?></i></p> | |
| 304 | + </div> | |
| 305 | + <?php wp_nonce_field( 'give-donation-form-widget', '_wpnonce', false ); ?> | |
| 207 | 306 | </div> |
| 208 | 307 | <?php |
| 209 | 308 | } |
| 210 | 309 | |
| @@ -238,7 +337,179 @@ | ||
| 238 | 337 | */ |
| 239 | 338 | public function flush_widget_cache() { |
| 240 | 339 | wp_cache_delete( $this->self, 'widget' ); |
| 241 | 340 | } |
| 341 | + | |
| 342 | + /** | |
| 343 | + * Get inline script for widget to add support for widget in page builder like Divi, Elementor and Beaver Builder. | |
| 344 | + * | |
| 345 | + * @since 2.7.0 | |
| 346 | + */ | |
| 347 | + private function getScriptForBuilders() { | |
| 348 | + global $pagenow; | |
| 349 | + | |
| 350 | + // Do not output inline script if admin widget script already printed. | |
| 351 | + if ( wp_script_is( "{$this->scriptHandle}-js", 'done' ) || in_array( $pagenow, [ 'widgets.php', 'customize.php' ] ) ) { | |
| 352 | + return; | |
| 353 | + } | |
| 354 | + | |
| 355 | + $this->widgetIdentifier = uniqid(); | |
| 356 | + $containerId = $this->widgetIdPrefix . $this->widgetIdentifier; | |
| 357 | + ?> | |
| 358 | + <style> | |
| 359 | + .give_forms_widget_container label:not([for*='display_style']):not([for*='float_labels']):not([for*='show_content']), | |
| 360 | + p.give_forms_display_style_setting_row > label:first-child, | |
| 361 | + .give_forms_widget_container label[for$='float_labels'], | |
| 362 | + .give_forms_widget_container label[for$='show_content']{ | |
| 363 | + display: block; | |
| 364 | + font-weight: 500; | |
| 365 | + } | |
| 366 | + | |
| 367 | + p.give_forms_display_style_setting_row > label:first-child, | |
| 368 | + .give_forms_widget_container label[for$='float_labels'], | |
| 369 | + .give_forms_widget_container label[for$='show_content']{ | |
| 370 | + margin-bottom: 8px; | |
| 371 | + } | |
| 372 | + | |
| 373 | + .give_forms_widget_container input { | |
| 374 | + margin-left: 0 !important; | |
| 375 | + margin-bottom: 0 !important; | |
| 376 | + } | |
| 377 | + | |
| 378 | + .give_forms_widget_container input[type="radio"]{ | |
| 379 | + margin: 0! important; | |
| 380 | + } | |
| 381 | + | |
| 382 | + .give_forms_widget_container .give-field-description { | |
| 383 | + color: #aaaaaa; | |
| 384 | + font-style: italic; | |
| 385 | + margin: 0; | |
| 386 | + padding-top: 8px; | |
| 387 | + display: block; | |
| 388 | + } | |
| 389 | + | |
| 390 | + .give_forms_widget_container select, | |
| 391 | + .give_forms_widget_container textarea{ | |
| 392 | + margin: 5px 10px 0 0; | |
| 393 | + } | |
| 394 | + | |
| 395 | + .give_forms_widget_container .give_forms_display_style_setting_row label[for$="tmp_display_style"]{ | |
| 396 | + display: flex; | |
| 397 | + align-items: center; | |
| 398 | + } | |
| 399 | + | |
| 400 | + .give_forms_widget_container .give_forms_display_style_setting_row label[for*="tmp_display_style-"]{ | |
| 401 | + display: flex; | |
| 402 | + } | |
| 403 | + | |
| 404 | + .give_forms_widget_container .give_forms_display_style_setting_row label[for*="tmp_display_style-"] span:last-child{ | |
| 405 | + margin-left: 5px; | |
| 406 | + } | |
| 407 | + </style> | |
| 408 | + <script> | |
| 409 | + /** | |
| 410 | + * Display setting fields on basis of donation form setting. | |
| 411 | + * | |
| 412 | + * @since 2.7.0 | |
| 413 | + * @param {Array} $el | |
| 414 | + */ | |
| 415 | + function showConditionalFieldWhenEditDonationFormSetting<?php echo esc_js( $this->widgetIdentifier ); ?>( $el ) { | |
| 416 | + const $this = $el, | |
| 417 | + $container = $this.closest( '.give_forms_widget_container' ), | |
| 418 | + $loader = jQuery( '.js-loader', $container ), | |
| 419 | + $oldSettings = jQuery( '.js-legacy-form-template-settings', $container ), | |
| 420 | + $newSettings = jQuery( '.js-new-form-template-settings', $container ); | |
| 421 | + | |
| 422 | + $oldSettings.hide().removeClass( 'active' ); | |
| 423 | + $newSettings.hide().removeClass( 'active' ); | |
| 424 | + | |
| 425 | + $loader.show(); | |
| 426 | + | |
| 427 | + jQuery.post( | |
| 428 | + ajaxurl, | |
| 429 | + { | |
| 430 | + action: 'give_get_form_template_id', | |
| 431 | + formId: $this.val(), | |
| 432 | + security: jQuery( 'input[name="_wpnonce"]', $container ).val(), | |
| 433 | + }, | |
| 434 | + function( response ) { | |
| 435 | + $loader.hide(); | |
| 436 | + | |
| 437 | + // Exit if result is not successful. | |
| 438 | + if (true === response.success) { | |
| 439 | + if ('legacy' === response.data) { | |
| 440 | + $oldSettings.show().addClass( 'active' ); | |
| 441 | + } else { | |
| 442 | + $newSettings.show().addClass( 'active' ); | |
| 443 | + } | |
| 444 | + } | |
| 445 | + | |
| 446 | + showConditionalFieldWhenEditDisplayStyleSetting<?php echo esc_js( $this->widgetIdentifier ); ?>( $this ); | |
| 447 | + }, | |
| 448 | + ); | |
| 449 | + } | |
| 450 | + | |
| 451 | + /** | |
| 452 | + * Display setting fields on basis of display_style setting. | |
| 453 | + * | |
| 454 | + * @since 2.7.0 | |
| 455 | + * @param {Array} $el | |
| 456 | + */ | |
| 457 | + function showConditionalFieldWhenEditDisplayStyleSetting<?php echo esc_js( $this->widgetIdentifier ); ?>( $el ) { | |
| 458 | + const $container = $el.closest( '.give_forms_widget_container' ), | |
| 459 | + $fieldset = jQuery( '.js-form-template-settings.active', $container ), | |
| 460 | + $parent = jQuery( 'p.give_forms_display_style_setting_row', $fieldset ), | |
| 461 | + isFormHasNewTemplate = $fieldset.hasClass( 'js-new-form-template-settings' ), | |
| 462 | + isFormHasLegacyTemplate = $fieldset.hasClass( 'js-legacy-form-template-settings' ); | |
| 463 | + | |
| 464 | + if (isFormHasLegacyTemplate) { | |
| 465 | + const $continue_button_title = $parent.next(); | |
| 466 | + | |
| 467 | + if ('onpage' === jQuery( 'input:checked', $parent ).val()) { | |
| 468 | + $continue_button_title.hide(); | |
| 469 | + } else { | |
| 470 | + $continue_button_title.show(); | |
| 471 | + } | |
| 472 | + } else if (isFormHasNewTemplate) { | |
| 473 | + if ('button' === jQuery( 'input:checked', $parent ).val()) { | |
| 474 | + $fieldset.find( 'p' ).not( $parent ).show(); | |
| 475 | + } else { | |
| 476 | + $fieldset.find( 'p' ).not( $parent ).hide(); | |
| 477 | + } | |
| 478 | + } | |
| 479 | + } | |
| 480 | + | |
| 481 | + /* Display style change handler. */ | |
| 482 | + jQuery( document ).on( 'change', '#<?php echo esc_js( $containerId ); ?> .give_forms_display_style_setting_row input', function() { | |
| 483 | + showConditionalFieldWhenEditDisplayStyleSetting<?php echo esc_js( $this->widgetIdentifier ); ?>( jQuery( this ) ); | |
| 484 | + } ); | |
| 485 | + | |
| 486 | + /* Donation form change handler. */ | |
| 487 | + jQuery( document ).on( 'change', '#<?php echo esc_js( $containerId ); ?> select.give-select', function() { | |
| 488 | + const $container = jQuery(this).closest('.give_forms_widget_container'), | |
| 489 | + $parent = jQuery( this ).parent(); | |
| 490 | + | |
| 491 | + $parent.removeClass('give-hidden'); | |
| 492 | + jQuery('.js-loader', $container ).addClass('give-hidden'); | |
| 493 | + | |
| 494 | + // Render form template settings only if form selected. | |
| 495 | + if( parseInt(jQuery( this ).val()) ) { | |
| 496 | + showConditionalFieldWhenEditDonationFormSetting<?php echo esc_js( $this->widgetIdentifier ); ?>( jQuery( this ) ); | |
| 497 | + } else{ | |
| 498 | + jQuery( '#<?php echo esc_js( $containerId ); ?> .give-hidden' ).hide(); | |
| 499 | + } | |
| 500 | + } ); | |
| 501 | + | |
| 502 | + jQuery( '#<?php echo esc_js( $containerId ); ?> select.give-select' ).trigger('change'); | |
| 503 | + </script> | |
| 504 | + <?php | |
| 505 | + } | |
| 242 | 506 | } |
| 243 | 507 | |
| 244 | -new Give_Forms_Widget(); | |
| 508 | + | |
| 509 | +/** | |
| 510 | + * @since 4.3.0 use widgets_init action and register_widget | |
| 511 | + */ | |
| 512 | +add_action( 'widgets_init', static function() { | |
| 513 | + register_widget( 'Give_Forms_Widget' ); | |
| 514 | +} ); | |
| 515 | + | |