| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to add donation form 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_Donation_Form' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Class to add campaign form field to a campaign form in the builder. |
| 21 |
*/ |
| 22 |
class Charitable_Field_Donation_Form extends Charitable_Builder_Field { |
| 23 |
|
| 24 |
/** |
| 25 |
* Primary class constructor. |
| 26 |
* |
| 27 |
* @since 1.8.0 |
| 28 |
*/ |
| 29 |
public function init() { |
| 30 |
|
| 31 |
// Basic information. |
| 32 |
$this->name = esc_html__( 'Donation Form', 'charitable' ); |
| 33 |
$this->type = 'donation-form'; |
| 34 |
$this->icon = 'fa-file-image-o'; |
| 35 |
$this->order = 50; |
| 36 |
$this->group = 'pro'; |
| 37 |
|
| 38 |
// Edit/Duplication information. |
| 39 |
$this->can_be_edited = true; |
| 40 |
$this->can_be_deleted = true; |
| 41 |
$this->can_be_duplicated = false; |
| 42 |
$this->edit_label = esc_html__( 'Edit Donation Form', 'charitable' ); |
| 43 |
$this->edit_type = 'donation-form'; |
| 44 |
$this->edit_section = 'pro'; |
| 45 |
|
| 46 |
// Misc. |
| 47 |
$this->tooltip = ''; |
| 48 |
|
| 49 |
// Define additional field properties. |
| 50 |
add_action( 'charitable_frontend_js', [ $this, 'frontend_js' ] ); |
| 51 |
add_action( 'charitable_builder_backend_scripts', [ $this, 'builder_js' ] ); // admin_enqueue_scripts. |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Field options panel inside the builder. |
| 57 |
* |
| 58 |
* @since 1.8.0 |
| 59 |
* |
| 60 |
* @param array $field Field settings. |
| 61 |
*/ |
| 62 |
public function field_options( $field ) { |
| 63 |
/* |
| 64 |
* Basic field options. |
| 65 |
*/ |
| 66 |
|
| 67 |
// Options open markup. |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Render the field. |
| 72 |
* |
| 73 |
* @since 1.8.0 |
| 74 |
* |
| 75 |
* @param array $field_data Any field data. |
| 76 |
* @param array $campaign_data Amount data and settings. |
| 77 |
* @param integer $field_id The field ID. |
| 78 |
* @param string $mode Where the field is being displayed ("preview" or "template"). |
| 79 |
* @param array $template_data Tempalate data. |
| 80 |
*/ |
| 81 |
public function render( $field_data = false, $campaign_data = false, $field_id = false, $mode = 'template', $template_data = false ) { |
| 82 |
|
| 83 |
if ( 'preview' === $mode ) { |
| 84 |
|
| 85 |
$html = ' |
| 86 |
<!--<span class="charitable-placeholder"></span>--> |
| 87 |
<span class="charitable-form-placeholder"> |
| 88 |
<div class="charitable-preview-notice"> |
| 89 |
<h6><i class="fa fa-file-image-o"></i> Donation Form</h6> |
| 90 |
<p>Contents of this field are not displayed in the campaign builder preview.</p> |
| 91 |
</div> |
| 92 |
</span> |
| 93 |
'; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
return $html; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Donation Form preview inside the builder. |
| 102 |
* |
| 103 |
* @since 1.8.0 |
| 104 |
* |
| 105 |
* @param array $field_data Field data and settings. |
| 106 |
* @param array $campaign_data Campaign data and settings. |
| 107 |
* @param array $field_id Field ID. |
| 108 |
* @param string $theme Template data. |
| 109 |
*/ |
| 110 |
public function field_preview( $field_data = false, $campaign_data = false, $field_id = false, $theme = '' ) { |
| 111 |
|
| 112 |
$html = $this->field_title( $this->name ); |
| 113 |
$html .= $this->field_wrapper( $this->render( $field_data, $campaign_data, $field_id, 'preview' ), $field_data ); |
| 114 |
|
| 115 |
echo $html; // phpcs:ignore |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* The display on the campaign front-end. |
| 120 |
* |
| 121 |
* @since 1.8.0 |
| 122 |
* |
| 123 |
* @param string $field The passed field type. |
| 124 |
* @param array $field_data Any field data. |
| 125 |
* @param array $campaign_data Form data and settings. |
| 126 |
*/ |
| 127 |
public function field_display( $field, $field_data = false, $campaign_data = false ) { |
| 128 |
|
| 129 |
add_filter( 'charitable_campaign_can_receive_donations', [ $this, 'enable_campaign_form' ], 10, 2 ); |
| 130 |
|
| 131 |
$campaign_id = isset( $campaign_data['id'] ) && 0 !== intval( $campaign_data['id'] ) ? intval( $campaign_data['id'] ) : false; |
| 132 |
|
| 133 |
if ( 0 === intval( $campaign_id ) ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
$html = ''; |
| 138 |
|
| 139 |
$defaults = array( |
| 140 |
'campaign_id' => 0, |
| 141 |
); |
| 142 |
|
| 143 |
$atts = array( |
| 144 |
'campaign_id' => $campaign_id, |
| 145 |
); |
| 146 |
|
| 147 |
/* Parse incoming $atts into an array and merge it with $defaults. */ |
| 148 |
$args = shortcode_atts( $defaults, $atts, 'charitable_donation_form' ); |
| 149 |
|
| 150 |
$headline = ! empty( $field_data['headline'] ) ? '<h5 class="charitable-field-template-headline">' . esc_html( $field_data['headline'] ) . '</h5>' : ''; |
| 151 |
|
| 152 |
ob_start(); |
| 153 |
|
| 154 |
echo $headline; // phpcs:ignore |
| 155 |
|
| 156 |
charitable_template_donation_form( $args['campaign_id'], $args ); |
| 157 |
|
| 158 |
$html = ob_get_clean(); |
| 159 |
|
| 160 |
remove_filter( 'charitable_campaign_can_receive_donations', [ $this, 'enable_campaign_form' ], 10, 2 ); |
| 161 |
|
| 162 |
$html = $this->field_display_wrapper( $html, $field_data ); |
| 163 |
|
| 164 |
echo apply_filters( 'charitable_campaign_builder_' . $this->type . '_field_display', $html, $campaign_data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Stand in function enabling the campaign form. |
| 169 |
* |
| 170 |
* @since 1.8.0 |
| 171 |
* |
| 172 |
* @param boolean $is_able Is able. |
| 173 |
* @param object $object The object. |
| 174 |
*/ |
| 175 |
public function enable_campaign_form( $is_able, $object = false ) { // phpcs:ignore |
| 176 |
|
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Donation Form display on the form front-end. |
| 182 |
* |
| 183 |
* @since 1.8.0 |
| 184 |
* |
| 185 |
* @param integer $field_id Number ID. |
| 186 |
* @param array $campaign_data Form data and settings. |
| 187 |
*/ |
| 188 |
public function settings_display( $field_id = false, $campaign_data = false ) { |
| 189 |
|
| 190 |
$charitable_builder_form_fields = new Charitable_Builder_Form_Fields(); |
| 191 |
|
| 192 |
$settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false; |
| 193 |
|
| 194 |
ob_start(); |
| 195 |
|
| 196 |
?> |
| 197 |
|
| 198 |
<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> |
| 199 |
|
| 200 |
<?php |
| 201 |
|
| 202 |
echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 203 |
isset( $settings['headline'] ) ? $settings['headline'] : '', |
| 204 |
esc_html__( 'Headline', 'charitable' ), |
| 205 |
array( |
| 206 |
'id' => 'field_' . esc_attr( $this->type ) . '_headline' . '_' . intval( $field_id ), // phpcs:ignore |
| 207 |
'name' => array( '_fields', intval( $field_id ), 'headline' ), |
| 208 |
'field_id' => intval( $field_id ), |
| 209 |
'tooltip' => esc_html__( 'Add a headline to this field.', 'charitable' ), |
| 210 |
'class' => 'charitable-campaign-builder-headline', |
| 211 |
) |
| 212 |
); |
| 213 |
|
| 214 |
echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 215 |
isset( $settings['css_class'] ) ? $settings['css_class'] : false, |
| 216 |
esc_html__( 'CSS Class', 'charitable' ), |
| 217 |
array( |
| 218 |
'id' => 'field_' . esc_attr( $this->type ) . '_css_class' . '_' . intval( $field_id ), // phpcs:ignore |
| 219 |
'name' => array( '_fields', intval( $field_id ), 'css_class' ), |
| 220 |
'field_id' => intval( $field_id ), |
| 221 |
'tooltip' => esc_html__( 'Add CSS classes (seperated by a space) for this field to customize it\'s appearance in your theme.', 'charitable' ), |
| 222 |
) |
| 223 |
); |
| 224 |
|
| 225 |
?> |
| 226 |
|
| 227 |
<?php |
| 228 |
|
| 229 |
$html = ob_get_clean(); |
| 230 |
|
| 231 |
return $html; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Display settings for AJAX requests. |
| 236 |
* |
| 237 |
* @since 1.8.0 |
| 238 |
* @version 1.8.9.1 |
| 239 |
* |
| 240 |
* @return void |
| 241 |
*/ |
| 242 |
public function settings_display_ajax() { |
| 243 |
// phpcs:disable WordPress.Security.NonceVerification.Missing |
| 244 |
$field_id = isset( $_POST['field_id'] ) ? intval( wp_unslash( $_POST['field_id'] ) ) : 0; |
| 245 |
$campaign_id = isset( $_POST['campaign_id'] ) ? intval( wp_unslash( $_POST['campaign_id'] ) ) : 0; // todo: should this be added? see a few lines down. |
| 246 |
// phpcs:enable WordPress.Security.NonceVerification.Missing |
| 247 |
|
| 248 |
$charitable_builder_form_fields = new Charitable_Builder_Form_Fields(); |
| 249 |
|
| 250 |
$campaign_data = get_post_meta( $campaign_id, 'campaign_settings_v2', true ); |
| 251 |
$settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false; |
| 252 |
|
| 253 |
ob_start(); |
| 254 |
|
| 255 |
?> |
| 256 |
|
| 257 |
<?php |
| 258 |
|
| 259 |
echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 260 |
isset( $settings['headline'] ) ? $settings['headline'] : '', |
| 261 |
esc_html__( 'Headline', 'charitable' ), |
| 262 |
array( |
| 263 |
'id' => 'field_' . esc_attr( $this->type ) . '_headline' . '_' . intval( $field_id ), // phpcs:ignore |
| 264 |
'name' => array( '_fields', intval( $field_id ), 'headline' ), |
| 265 |
'field_id' => intval( $field_id ), |
| 266 |
'tooltip' => esc_html__( 'Add a headline to this field.', 'charitable' ), |
| 267 |
'class' => 'charitable-campaign-builder-headline', |
| 268 |
) |
| 269 |
); |
| 270 |
|
| 271 |
echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 272 |
$settings['css_class'], |
| 273 |
esc_html__( 'CSS Class', 'charitable' ), |
| 274 |
array( |
| 275 |
'id' => 'field_' . esc_attr( $this->type ) . '_css_class', |
| 276 |
'name' => array( 'fields', esc_attr( $this->type ), 'css_class' ), |
| 277 |
'tooltip' => esc_html( $this->tooltip ), |
| 278 |
) |
| 279 |
); |
| 280 |
|
| 281 |
?> |
| 282 |
|
| 283 |
<?php |
| 284 |
|
| 285 |
$html = ob_get_clean(); |
| 286 |
|
| 287 |
wp_send_json_success( [ 'html' => $html ] ); |
| 288 |
|
| 289 |
exit; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Enqueue frontend js. |
| 294 |
* |
| 295 |
* @since 1.8.0 |
| 296 |
*/ |
| 297 |
public function frontend_js() { |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Enqueue frontend limit option js. |
| 302 |
* |
| 303 |
* @since 1.8.0 |
| 304 |
* |
| 305 |
* @param array $min Min string. |
| 306 |
*/ |
| 307 |
public function builder_js( $min ) { |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Format and sanitize field. |
| 312 |
* |
| 313 |
* @since 1.8.0 |
| 314 |
* |
| 315 |
* @param int $field_id Field type. |
| 316 |
* @param mixed $field_submit Field value that was submitted. |
| 317 |
* @param array $campaign_data Campaign data and settings. |
| 318 |
*/ |
| 319 |
public function format( $field_id, $field_submit, $campaign_data ) { |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Validate field on form submit. |
| 324 |
* |
| 325 |
* @since 1.8.0 |
| 326 |
* |
| 327 |
* @param int $field_id Field ID. |
| 328 |
* @param mixed $field_submit Field value that was submitted. |
| 329 |
* @param array $campaign_data Campaign data and settings. |
| 330 |
*/ |
| 331 |
public function validate( $field_id, $field_submit, $campaign_data ) { |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Possible depreciated function. |
| 336 |
* |
| 337 |
* @since 1.8.0 |
| 338 |
* |
| 339 |
* @param string $field Field ID. |
| 340 |
* @param array $field_atts Field value that was submitted. |
| 341 |
* @param array $campaign_data Campaign data and settings. |
| 342 |
*/ |
| 343 |
public function section_top( $field, $field_atts, $campaign_data ) { |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Possible depreciated function. |
| 348 |
* |
| 349 |
* @since 1.8.0 |
| 350 |
* |
| 351 |
* @param string $field Field type. |
| 352 |
* @param array $field_atts Field value that was submitted. |
| 353 |
* @param array $campaign_data Campaign data and settings. |
| 354 |
*/ |
| 355 |
public function section_bottom( $field, $field_atts, $campaign_data ) { |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
new Charitable_Field_Donation_Form(); |
| 360 |
|
| 361 |
endif; |