| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to add donation button field to a campaign form in the builder. |
| 4 |
* |
| 5 |
* @package Charitable |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.8.0 |
| 10 |
* @version 1.8.9.1 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Field_Donate_Button' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Class to add campaign donate button field to a campaign form in the builder. |
| 21 |
* |
| 22 |
* @version 1.8.9.1 |
| 23 |
*/ |
| 24 |
class Charitable_Field_Donate_Button extends Charitable_Builder_Field { |
| 25 |
|
| 26 |
/** |
| 27 |
* Primary class constructor. |
| 28 |
* |
| 29 |
* @since 1.8.0 |
| 30 |
*/ |
| 31 |
public function init() { |
| 32 |
|
| 33 |
// Basic information. |
| 34 |
$this->name = esc_html__( 'Donate Button', 'charitable' ); |
| 35 |
$this->type = 'donate-button'; |
| 36 |
$this->icon = 'fa-link'; |
| 37 |
$this->order = 60; |
| 38 |
|
| 39 |
$this->align_default = 'center'; |
| 40 |
|
| 41 |
// Edit/Duplication information. |
| 42 |
$this->can_be_edited = true; |
| 43 |
$this->can_be_deleted = true; |
| 44 |
$this->can_be_duplicated = true; |
| 45 |
$this->edit_label = esc_html__( 'Edit Donate Button', 'charitable' ); |
| 46 |
$this->edit_type = 'donate-button'; |
| 47 |
$this->edit_section = 'recommended'; |
| 48 |
|
| 49 |
// Misc. |
| 50 |
$this->tooltip = ''; |
| 51 |
|
| 52 |
// Define additional field properties. |
| 53 |
add_action( 'charitable_frontend_js', [ $this, 'frontend_js' ] ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Social Sharing options panel inside the builder. |
| 58 |
* |
| 59 |
* @since 1.8.0 |
| 60 |
* |
| 61 |
* @param array $field Donate Button settings. |
| 62 |
*/ |
| 63 |
public function field_options( $field ) { |
| 64 |
/* |
| 65 |
* Basic field options. |
| 66 |
*/ |
| 67 |
|
| 68 |
// Options open markup. |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Render the field. |
| 73 |
* |
| 74 |
* @since 1.8.0 |
| 75 |
* |
| 76 |
* @param array $field_data Any field data. |
| 77 |
* @param array $campaign_data Amount data and settings. |
| 78 |
* @param integer $field_id The field ID. |
| 79 |
* @param string $mode Where the field is being displayed ("preview" or "template"). |
| 80 |
* @param array $template_data Tempalate data. |
| 81 |
*/ |
| 82 |
public function render( $field_data = false, $campaign_data = false, $field_id = false, $mode = 'template', $template_data = false ) { |
| 83 |
|
| 84 |
$button_label = ! empty( $field_data['button_label'] ) ? esc_html( $field_data['button_label'] ) : esc_html__( 'Donate', 'charitable' ); |
| 85 |
|
| 86 |
$html = '<div class="charitable-field-preview-donate-button"><div class="charitable-field-donate-button-row"> |
| 87 |
<div class="charitable-field-donate-button-column"> |
| 88 |
<span class="charitable-placeholder placeholder button charitable-prevent-select">' . $button_label . '</span> |
| 89 |
</div> |
| 90 |
</div></div>'; |
| 91 |
|
| 92 |
return $html; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Social Sharing preview inside the builder. |
| 97 |
* |
| 98 |
* @since 1.8.0 |
| 99 |
* |
| 100 |
* @param array $field_data Field data and settings. |
| 101 |
* @param array $campaign_data Campaign data and settings. |
| 102 |
* @param array $field_id Field ID. |
| 103 |
* @param string $theme Template data. |
| 104 |
*/ |
| 105 |
public function field_preview( $field_data = false, $campaign_data = false, $field_id = false, $theme = '' ) { |
| 106 |
|
| 107 |
if ( empty( $field_data['align'] ) ) { |
| 108 |
$field_data['align'] = $this->align_default; |
| 109 |
} |
| 110 |
|
| 111 |
$html = $this->field_title( $this->name ); |
| 112 |
$html .= $this->field_wrapper( $this->render( $field_data, $campaign_data, $field_id ), $field_data ); |
| 113 |
|
| 114 |
echo wp_kses_post( $html ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* The display on the campaign front-end. |
| 119 |
* |
| 120 |
* @since 1.8.0 |
| 121 |
* |
| 122 |
* @param array $field Donate Button settings. |
| 123 |
* @param array $field_data Any field data. |
| 124 |
* @param array $campaign_data Form data and settings. |
| 125 |
* @param boolean $is_preview_page Is this being generated on a preview page. |
| 126 |
*/ |
| 127 |
public function field_display( $field, $field_data = false, $campaign_data = false, $is_preview_page = false ) { |
| 128 |
|
| 129 |
$campaign = isset( $campaign_data['id'] ) && 0 !== intval( $campaign_data['id'] ) ? charitable_get_campaign( $campaign_data['id'] ) : false; |
| 130 |
|
| 131 |
if ( ! $campaign ) : |
| 132 |
return; |
| 133 |
endif; |
| 134 |
|
| 135 |
$button_label = ! empty( $field_data['button_label'] ) ? esc_html( $field_data['button_label'] ) : 'Donate'; |
| 136 |
$css_class = ! empty( $field_data['css_class'] ) ? ' class="' . esc_html( $field_data['css_class'] ) . '" ' : ''; |
| 137 |
$donation_form_display = charitable_get_option( 'donation_form_display', 'separate_page' ); // could also be same_page or modal. |
| 138 |
|
| 139 |
// We want the button to contain a donation amount, but we go with what is in the SESSION first, then the DEFAULT amount. |
| 140 |
$donation_amount = $campaign->get_donation_amount_in_session(); |
| 141 |
|
| 142 |
ob_start(); |
| 143 |
|
| 144 |
?> |
| 145 |
|
| 146 |
<div <?php echo esc_attr( $css_class ); ?>> |
| 147 |
|
| 148 |
<?php |
| 149 |
|
| 150 |
if ( ! $campaign->can_receive_donations() && ! $is_preview_page ) { |
| 151 |
|
| 152 |
$charitable_campaign_button_closed_message = apply_filters( 'charitable_campaign_button_closed_message', esc_html__( 'This campaign is closed or goal has been reached.', 'charitable' ), $campaign ); |
| 153 |
|
| 154 |
?> |
| 155 |
|
| 156 |
<button type="submit" name="charitable_submit" disabled class="<?php echo esc_attr( charitable_get_button_class( 'donate', true ) ); ?>"> |
| 157 |
<?php echo esc_html( $charitable_campaign_button_closed_message ); ?> |
| 158 |
</button> |
| 159 |
|
| 160 |
<?php } elseif ( 'modal' === $donation_form_display ) { ?> |
| 161 |
|
| 162 |
<div class="campaign-donation"> |
| 163 |
<a data-trigger-modal="charitable-donation-form-modal-<?php echo intval( $campaign->ID ); ?>" |
| 164 |
data-campaign-id="<?php echo intval( $campaign->ID ); ?>" |
| 165 |
class="<?php echo esc_attr( charitable_get_button_class( 'donate' ) ); ?>" |
| 166 |
href="<?php echo esc_url( charitable_get_permalink( 'campaign_donation_page', array( 'campaign_id' => $campaign->ID ) ) ); ?>" |
| 167 |
<?php // translators: %s is the campaign title. ?> |
| 168 |
aria-label="<?php echo esc_attr( sprintf( _x( 'Make a donation to %s', 'make a donation to campaign', 'charitable' ), get_the_title( $campaign->ID ) ) ); ?>"> |
| 169 |
<?php echo esc_html( wp_strip_all_tags( $button_label ) ); ?> |
| 170 |
</a> |
| 171 |
</div> |
| 172 |
|
| 173 |
|
| 174 |
<?php |
| 175 |
} elseif ( 'same_page' === $donation_form_display ) { |
| 176 |
|
| 177 |
?> |
| 178 |
|
| 179 |
<a class="<?php echo esc_attr( charitable_get_button_class( 'donate' ) ); ?>" |
| 180 |
href="#charitable-donation-form" |
| 181 |
<?php // translators: %s is the campaign title. ?> |
| 182 |
aria-label="<?php echo esc_attr( sprintf( _x( 'Make a donation to %s', 'make a donation to campaign', 'charitable' ), get_the_title( $campaign->ID ) ) ); ?>" |
| 183 |
> |
| 184 |
<?php echo esc_html( wp_strip_all_tags( $button_label ) ); ?> |
| 185 |
</a> |
| 186 |
|
| 187 |
|
| 188 |
<?php } else { ?> |
| 189 |
|
| 190 |
<form class="campaign-donation" method="post"> |
| 191 |
<?php wp_nonce_field( 'charitable-donate', 'charitable-donate-now' ); ?> |
| 192 |
<input type="hidden" name="charitable_action" value="start_donation" /> |
| 193 |
<input type="hidden" name="campaign_id" value="<?php echo esc_html( $campaign->ID ); ?>" /> |
| 194 |
<input type="hidden" name="charitable_donation_amount" value="<?php $donation_amount; ?>" /> |
| 195 |
<input type="hidden" name="charitable_builder" value="true" /> |
| 196 |
<button type="submit" name="charitable_submit" class="<?php echo esc_attr( charitable_get_button_class( 'donate' ) ); ?>"><?php echo esc_html( wp_strip_all_tags( $button_label ) ); ?></button> |
| 197 |
</form> |
| 198 |
|
| 199 |
<?php } ?> |
| 200 |
|
| 201 |
</div> |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
<?php |
| 206 |
|
| 207 |
$html = ob_get_clean(); |
| 208 |
|
| 209 |
$html = $this->field_display_wrapper( $html, $field_data ); |
| 210 |
|
| 211 |
echo apply_filters( 'charitable_campaign_builder_' . $this->type . '_field_display', $html, $campaign_data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* The display on the form settings backend when the user clicks on the field/block. |
| 216 |
* |
| 217 |
* @since 1.8.0 |
| 218 |
* |
| 219 |
* @param array $field_id Field ID. |
| 220 |
* @param array $campaign_data Campaign data and settings. |
| 221 |
*/ |
| 222 |
public function settings_display( $field_id = false, $campaign_data = false ) { |
| 223 |
|
| 224 |
if ( ! class_exists( 'Charitable_Builder_Form_Fields' ) ) { |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
$charitable_builder_form_fields = new Charitable_Builder_Form_Fields(); |
| 229 |
|
| 230 |
$settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false; |
| 231 |
|
| 232 |
add_action( 'charitable_builder_' . $this->type . '_settings_display_start', [ $this, 'settings_section_top' ], 10, 2 ); |
| 233 |
add_action( 'charitable_builder_' . $this->type . '_settings_display_end', [ $this, 'settings_section_bottom' ], 10, 2 ); |
| 234 |
|
| 235 |
ob_start(); |
| 236 |
|
| 237 |
?> |
| 238 |
|
| 239 |
<h4 class="charitable-panel-field" data-field-id="<?php echo intval( $field_id ); ?>"><?php echo esc_html( $this->name ); ?> (ID: <?php echo intval( $field_id ); ?>)</h4> |
| 240 |
|
| 241 |
<div class="charitable-panel-field charitable-panel-field-section" data-field-id="<?php echo intval( $field_id ); ?>"> |
| 242 |
|
| 243 |
<?php echo do_action( 'charitable_builder_' . $this->type . '_settings_display_start', $field_id, $campaign_data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 244 |
|
| 245 |
</div> |
| 246 |
|
| 247 |
<?php |
| 248 |
|
| 249 |
echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 250 |
isset( $settings['button_label'] ) ? $settings['button_label'] : esc_html__( 'Donate', 'charitable' ), |
| 251 |
esc_html__( 'Button Label', 'charitable' ), |
| 252 |
array( |
| 253 |
'id' => 'field_' . esc_attr( $this->type ) . '_button_label' . '_' . intval( $field_id ), // phpcs:ignore |
| 254 |
'name' => array( '_fields', intval( $field_id ), 'button_label' ), |
| 255 |
'class' => 'charitable-campaign-builder-donate-button-button-label', |
| 256 |
'field_id' => intval( $field_id ), |
| 257 |
) |
| 258 |
); |
| 259 |
|
| 260 |
echo $charitable_builder_form_fields->generate_number_slider( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 261 |
isset( $settings['width_percentage'] ) ? $settings['width_percentage'] : 100, |
| 262 |
esc_html__( 'Width', 'charitable' ), |
| 263 |
array( |
| 264 |
'id' => 'field_' . esc_attr( $this->type ) . '_width_percentage' . '_' . intval( $field_id ), // phpcs:ignore |
| 265 |
'name' => array( '_fields', intval( $field_id ), 'width_percentage' ), |
| 266 |
'field_type' => esc_attr( $this->type ), |
| 267 |
'css_class' => 'charitable-indicator-on-hover', |
| 268 |
'field_id' => intval( $field_id ), |
| 269 |
'symbol' => '%', |
| 270 |
'min' => 0, |
| 271 |
'min_actual' => 20, |
| 272 |
) |
| 273 |
); |
| 274 |
|
| 275 |
echo $charitable_builder_form_fields->generate_align( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 276 |
isset( $settings['align'] ) ? $settings['align'] : esc_attr( $this->align_default ), |
| 277 |
esc_html__( 'Align', 'charitable' ), |
| 278 |
array( |
| 279 |
'id' => 'field_' . esc_attr( $this->type ) . '_align' . '_' . intval( $field_id ), // phpcs:ignore |
| 280 |
'name' => array( '_fields', intval( $field_id ), 'align' ), |
| 281 |
'field_id' => intval( $field_id ), |
| 282 |
) |
| 283 |
); |
| 284 |
|
| 285 |
/* CSS CLASS */ |
| 286 |
|
| 287 |
echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 288 |
isset( $settings['css_class'] ) ? $settings['css_class'] : false, |
| 289 |
esc_html__( 'CSS Class', 'charitable' ), |
| 290 |
array( |
| 291 |
'id' => 'field_' . esc_attr( $this->type ) . '_css_class' . '_' . intval( $field_id ), |
| 292 |
'name' => array( '_fields', intval( $field_id ), 'css_class' ), |
| 293 |
'field_id' => intval( $field_id ), |
| 294 |
'tooltip' => esc_html__( 'Add CSS classes (seperated by a space) for this field to customize it\'s appearance in your theme.', 'charitable' ), |
| 295 |
) |
| 296 |
); |
| 297 |
|
| 298 |
?> |
| 299 |
|
| 300 |
<div class="charitable-panel-field charitable-panel-field-section" data-field-id="<?php echo intval( $field_id ); ?>"> |
| 301 |
|
| 302 |
<?php do_action( 'charitable_builder_' . $this->type . '_settings_display_end', $field_id, $campaign_data ); ?> |
| 303 |
|
| 304 |
</div> |
| 305 |
|
| 306 |
<?php |
| 307 |
|
| 308 |
$html = ob_get_clean(); |
| 309 |
|
| 310 |
return $html; |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Display content above the content settings in the panel in the admin via hook. |
| 315 |
* |
| 316 |
* @since 1.8.0 |
| 317 |
* |
| 318 |
* @param integer $field_id Field ID. |
| 319 |
* @param array $campaign_data Data on campaign. |
| 320 |
*/ |
| 321 |
public function settings_section_top( $field_id = false, $campaign_data = false ) { |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Display content above the content settings in the panel in the admin via hook. |
| 326 |
* |
| 327 |
* @since 1.8.0 |
| 328 |
* |
| 329 |
* @param integer $field_id Field ID. |
| 330 |
* @param array $campaign_data Data on campaign. |
| 331 |
*/ |
| 332 |
public function settings_section_bottom( $field_id = false, $campaign_data = false ) { |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Enqueue frontend limit option js. |
| 337 |
* |
| 338 |
* @since 1.8.0 |
| 339 |
* |
| 340 |
* @param array $forms Forms on the current page. |
| 341 |
*/ |
| 342 |
public function frontend_js( $forms ) { |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Format and sanitize field. |
| 347 |
* |
| 348 |
* @since 1.8.0 |
| 349 |
* |
| 350 |
* @param int $field_id Donate Button ID. |
| 351 |
* @param mixed $field_submit Donate Button value that was submitted. |
| 352 |
* @param array $campaign_data Form data and settings. |
| 353 |
*/ |
| 354 |
public function format( $field_id, $field_submit, $campaign_data ) { |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Validate field on form submit. |
| 359 |
* |
| 360 |
* @since 1.8.0 |
| 361 |
* |
| 362 |
* @param int $field_id Donate Button ID. |
| 363 |
* @param mixed $field_submit Donate Button value that was submitted. |
| 364 |
* @param array $campaign_data Form data and settings. |
| 365 |
*/ |
| 366 |
public function validate( $field_id, $field_submit, $campaign_data ) { |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
new Charitable_Field_Donate_Button(); |
| 371 |
|
| 372 |
endif; |