widget.php
245 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Give Form Widget |
| 4 | * |
| 5 | * @package GiveWP |
| 6 | * @subpackage Admin/Forms |
| 7 | * @copyright Copyright (c) 2016, GiveWP |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.0 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Give Form widget |
| 19 | * |
| 20 | * @since 1.0 |
| 21 | */ |
| 22 | class Give_Forms_Widget extends WP_Widget { |
| 23 | |
| 24 | /** |
| 25 | * The widget class name |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $self; |
| 30 | |
| 31 | /** |
| 32 | * Instantiate the class |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | $this->self = get_class( $this ); |
| 36 | |
| 37 | parent::__construct( |
| 38 | 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 | ) |
| 43 | ); |
| 44 | |
| 45 | add_action( 'widgets_init', array( $this, 'widget_init' ) ); |
| 46 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_widget_scripts' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Load widget assets only on the widget page |
| 51 | * |
| 52 | * @param string $hook Use it to target a specific admin page. |
| 53 | * |
| 54 | * @return void |
| 55 | */ |
| 56 | public function admin_widget_scripts( $hook ) { |
| 57 | |
| 58 | // 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/'; |
| 62 | |
| 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 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Echo the widget content. |
| 75 | * |
| 76 | * @param array $args Display arguments including before_title, after_title, |
| 77 | * before_widget, and after_widget. |
| 78 | * @param array $instance The settings for the particular instance of the widget. |
| 79 | */ |
| 80 | 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']; |
| 84 | |
| 85 | echo $args['before_widget']; // XSS ok. |
| 86 | |
| 87 | /** |
| 88 | * Fires before widget settings form in the admin area. |
| 89 | * |
| 90 | * @param integer $form_id Form ID. |
| 91 | * |
| 92 | * @since 1.0 |
| 93 | */ |
| 94 | do_action( 'give_before_forms_widget', $form_id ); |
| 95 | |
| 96 | echo $title ? $args['before_title'] . $title . $args['after_title'] : ''; // XSS ok. |
| 97 | |
| 98 | give_get_donation_form( $instance ); |
| 99 | |
| 100 | echo $args['after_widget']; // XSS ok. |
| 101 | |
| 102 | /** |
| 103 | * Fires after widget settings form in the admin area. |
| 104 | * |
| 105 | * @param integer $form_id Form ID. |
| 106 | * |
| 107 | * @since 1.0 |
| 108 | */ |
| 109 | do_action( 'give_after_forms_widget', $form_id ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Output the settings update form. |
| 114 | * |
| 115 | * @param array $instance Current settings. |
| 116 | */ |
| 117 | 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 | ); |
| 126 | |
| 127 | $instance = wp_parse_args( (array) $instance, $defaults ); |
| 128 | |
| 129 | // Backward compatibility: Set float labels as default if, it was set as empty previous. |
| 130 | $instance['float_labels'] = empty( $instance['float_labels'] ) ? 'global' : $instance['float_labels']; |
| 131 | |
| 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 ); |
| 140 | ?> |
| 141 | <div class="give_forms_widget_container"> |
| 142 | |
| 143 | <?php // Widget: widget Title. ?> |
| 144 | <p> |
| 145 | <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> |
| 147 | <small class="give-field-description"><?php esc_html_e( 'Leave blank to hide the widget title.', 'give' ); ?></small> |
| 148 | </p> |
| 149 | |
| 150 | <?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> |
| 162 | </p> |
| 163 | |
| 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> |
| 175 | |
| 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> |
| 182 | |
| 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__( 'Enabled', '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__( 'Disabled', 'give' ); ?></label><br> |
| 189 | <small class="give-field-description"> |
| 190 | <?php |
| 191 | printf( |
| 192 | /* translators: %s: Documentation link to http://docs.givewp.com/form-floating-labels */ |
| 193 | __( '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 | ?> |
| 197 | </small> |
| 198 | </p> |
| 199 | |
| 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> |
| 207 | </div> |
| 208 | <?php |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Register the widget |
| 213 | * |
| 214 | * @return void |
| 215 | */ |
| 216 | public function widget_init() { |
| 217 | register_widget( $this->self ); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Update the widget |
| 222 | * |
| 223 | * @param array $new_instance The new options. |
| 224 | * @param array $old_instance The previous options. |
| 225 | * |
| 226 | * @return array |
| 227 | */ |
| 228 | public function update( $new_instance, $old_instance ) { |
| 229 | $this->flush_widget_cache(); |
| 230 | |
| 231 | return $new_instance; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Flush widget cache |
| 236 | * |
| 237 | * @return void |
| 238 | */ |
| 239 | public function flush_widget_cache() { |
| 240 | wp_cache_delete( $this->self, 'widget' ); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | new Give_Forms_Widget(); |
| 245 |