| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to add donation options 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.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Field_Shortcode' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Class to add campaign shortcode field to a campaign form in the builder. |
| 21 |
*/ |
| 22 |
class Charitable_Field_Shortcode extends Charitable_Builder_Field { |
| 23 |
|
| 24 |
/** |
| 25 |
* Primary class constructor. |
| 26 |
* |
| 27 |
* @since 1.8.0 |
| 28 |
*/ |
| 29 |
public function init() { |
| 30 |
|
| 31 |
// Define field type information. |
| 32 |
$this->name = esc_html__( 'Shortcode', 'charitable' ); |
| 33 |
$this->type = 'shortcode'; |
| 34 |
$this->icon = 'fa-code'; |
| 35 |
$this->order = 1000; |
| 36 |
|
| 37 |
// Edit/Duplication information. |
| 38 |
$this->can_be_edited = true; |
| 39 |
$this->can_be_deleted = true; |
| 40 |
$this->can_be_duplicated = true; |
| 41 |
$this->edit_label = esc_html__( 'Edit Shotcode', 'charitable' ); |
| 42 |
$this->edit_type = 'shortcode'; // was settings |
| 43 |
$this->edit_section = 'standard'; |
| 44 |
|
| 45 |
// Misc. |
| 46 |
$this->tooltip = esc_html__( 'Display any valid WordPress shortcode.', 'charitable' ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Donation Options options panel inside the builder. |
| 51 |
* |
| 52 |
* @since 1.8.0 |
| 53 |
* |
| 54 |
* @param array $field Donation Options settings. |
| 55 |
*/ |
| 56 |
public function field_options( $field ) { |
| 57 |
/* |
| 58 |
* Basic field options. |
| 59 |
*/ |
| 60 |
|
| 61 |
// Options open markup. |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Donation Options preview inside the builder. |
| 66 |
* |
| 67 |
* @since 1.8.0 |
| 68 |
* |
| 69 |
* @param array $field_data Field data and settings. |
| 70 |
* @param array $campaign_data Campaign data and settings. |
| 71 |
* @param array $field_id Field ID. |
| 72 |
* @param string $theme Template data. |
| 73 |
*/ |
| 74 |
public function field_preview( $field_data = false, $campaign_data = false, $field_id = false, $theme = '' ) { |
| 75 |
|
| 76 |
$headline = ! empty( $field_data['headline'] ) ? esc_html( $field_data['headline'] ) : false; |
| 77 |
$shortcode = ! empty( $field_data['shortcode'] ) ? $this->format_for_output( $field_data['shortcode'], $campaign_data ) : false; |
| 78 |
|
| 79 |
$title_html = $this->field_title( $this->name ); |
| 80 |
|
| 81 |
$html = '<div class="charitable-field-preview-shortcode shortcode" data-field-type="' . $this->type . '"> |
| 82 |
<div class="placeholder"><h5 class="charitable-field-preview-headline">' . $headline . '</h5></div> |
| 83 |
<div class="row"> |
| 84 |
<div class="column"> |
| 85 |
<span class="placeholder shortcode-preview">' . $shortcode . '</span> |
| 86 |
</div> |
| 87 |
</div> |
| 88 |
</div>'; |
| 89 |
|
| 90 |
$final_html = $title_html . $this->field_wrapper( $html, $field_data ); |
| 91 |
|
| 92 |
echo $final_html; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Donation Options display on the form front-end. |
| 97 |
* |
| 98 |
* @since 1.8.0 |
| 99 |
* |
| 100 |
* @param array $field Donation Options settings. |
| 101 |
* @param array $deprecated Deprecated. |
| 102 |
* @param array $form_data Form data and settings. |
| 103 |
*/ |
| 104 |
public function field_display( $field, $field_data = false, $campaign_data = false ) { |
| 105 |
|
| 106 |
$campaign = isset( $campaign_data['id'] ) && 0 !== intval( $campaign_data['id'] ) ? charitable_get_campaign( $campaign_data['id'] ) : false; |
| 107 |
|
| 108 |
if ( ! $campaign ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
$shortcode = ! empty( $field_data['shortcode'] ) ? $this->format_for_output( $field_data['shortcode'], $campaign_data ) : false; |
| 113 |
$css_class = ! empty( $field_data['css_class'] ) ? ' class="' . esc_html( $field_data['css_class'] ) . '" ' : ''; |
| 114 |
|
| 115 |
ob_start(); |
| 116 |
|
| 117 |
if ( $shortcode ) : |
| 118 |
|
| 119 |
?> |
| 120 |
|
| 121 |
<div class="charitable-campaign-field_<?php echo $this->type; ?>"> |
| 122 |
<div <?php echo $css_class; ?>> |
| 123 |
<?php if ( $shortcode ) : ?> |
| 124 |
<?php echo do_shortcode( $shortcode ); ?> |
| 125 |
<?php endif; ?> |
| 126 |
</div> |
| 127 |
</div> |
| 128 |
|
| 129 |
<?php |
| 130 |
|
| 131 |
endif; |
| 132 |
|
| 133 |
$html = ob_get_clean(); |
| 134 |
|
| 135 |
echo apply_filters( 'charitable_campaign_builder_' . $this->type . '_field_display', $html, $campaign_data ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* The display on the form settings backend when the user clicks on the field/block. |
| 140 |
* |
| 141 |
* @since 1.8.0 |
| 142 |
* |
| 143 |
* @param array $field Social Sharing settings. |
| 144 |
* @param array $campaign_data Campaign data and settings. |
| 145 |
*/ |
| 146 |
public function settings_display( $field_id = false, $campaign_data = false ) { |
| 147 |
|
| 148 |
$charitable_builder_form_fields = new Charitable_Builder_Form_Fields(); |
| 149 |
|
| 150 |
$settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false; |
| 151 |
|
| 152 |
ob_start(); |
| 153 |
|
| 154 |
?> |
| 155 |
|
| 156 |
<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> |
| 157 |
|
| 158 |
<?php |
| 159 |
|
| 160 |
echo $charitable_builder_form_fields->generate_text( |
| 161 |
isset( $settings['headline'] ) ? $settings['headline'] : '', |
| 162 |
esc_html__( 'Headline', 'charitable' ), |
| 163 |
array( |
| 164 |
'id' => 'field_' . esc_attr( $this->type ) . '_headline' . '_' . intval( $field_id ), // phpcs:ignore |
| 165 |
'name' => array( '_fields', intval( $field_id ), 'headline' ), |
| 166 |
'field_id' => intval( $field_id ), |
| 167 |
'tooltip' => esc_html__( 'Add a headline to this field.', 'charitable' ), |
| 168 |
'class' => 'charitable-campaign-builder-headline', |
| 169 |
) |
| 170 |
); |
| 171 |
|
| 172 |
echo $charitable_builder_form_fields->generate_text( |
| 173 |
isset( $settings['shortcode'] ) ? $this->format_for_setting( $settings['shortcode'] ) : false, |
| 174 |
esc_html__( 'Shortcode', 'charitable' ), |
| 175 |
array( |
| 176 |
'id' => 'field_' . esc_attr( $this->type ) . '_shortcode' . '_' . intval( $field_id ), // phpcs:ignore |
| 177 |
'name' => array( '_fields', intval( $field_id ), 'shortcode' ), |
| 178 |
'field_id' => intval( $field_id ), |
| 179 |
'tooltip' => esc_html( $this->tooltip ), |
| 180 |
'class' => 'charitable-campaign-builder-shortcode', |
| 181 |
) |
| 182 |
); |
| 183 |
|
| 184 |
echo $charitable_builder_form_fields->generate_text( |
| 185 |
isset( $settings['css_class'] ) ? $settings['css_class'] : false, |
| 186 |
esc_html__( 'CSS Class', 'charitable' ), |
| 187 |
array( |
| 188 |
'id' => 'field_' . esc_attr( $this->type ) . '_css_class' . '_' . intval( $field_id ), |
| 189 |
'name' => array( '_fields', intval( $field_id ), 'css_class' ), |
| 190 |
'field_id' => intval( $field_id ), |
| 191 |
'tooltip' => esc_html__( 'Add CSS classes (seperated by a space) for this field to customize it\'s appearance in your theme.', 'charitable' ), |
| 192 |
) |
| 193 |
); |
| 194 |
|
| 195 |
?> |
| 196 |
|
| 197 |
<?php |
| 198 |
|
| 199 |
$html = ob_get_clean(); |
| 200 |
|
| 201 |
return $html; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Enqueue frontend limit option js. |
| 206 |
* |
| 207 |
* @since 1.8.0 |
| 208 |
* |
| 209 |
* @param array $forms Forms on the current page. |
| 210 |
*/ |
| 211 |
public function frontend_js( $forms ) { |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Format and sanitize field value for builder, in the setting left pane. |
| 216 |
* |
| 217 |
* @since 1.8.0 |
| 218 |
* |
| 219 |
* @param int $field_value The value. |
| 220 |
*/ |
| 221 |
public function format_for_setting( $field_value = false ) { |
| 222 |
|
| 223 |
return htmlspecialchars( $field_value ); // this will convert quotes, usually used in shortcodes. |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Format and sanitize field, for use in the frontend (preview and campaign page). |
| 228 |
* |
| 229 |
* @since 1.8.0 |
| 230 |
* |
| 231 |
* @param int $field_value The value. |
| 232 |
*/ |
| 233 |
public function format_for_output( $field_value = false, $campaign_data = false ) { |
| 234 |
|
| 235 |
return $field_value; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Validate field on form submit. |
| 240 |
* |
| 241 |
* @since 1.8.0 |
| 242 |
* |
| 243 |
* @param int $field_id Donation Options ID. |
| 244 |
* @param mixed $field_submit Donation Options value that was submitted. |
| 245 |
* @param array $form_data Form data and settings. |
| 246 |
*/ |
| 247 |
public function validate( $field_id, $field_submit, $form_data ) { |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
new Charitable_Field_Shortcode(); |
| 252 |
|
| 253 |
endif; |