| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to add photo 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_Photo' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Field_Photo |
| 21 |
* |
| 22 |
* @since 1.8.0 |
| 23 |
*/ |
| 24 |
class Charitable_Field_Photo extends Charitable_Builder_Field { |
| 25 |
|
| 26 |
/** |
| 27 |
* Primary class constructor. |
| 28 |
* |
| 29 |
* @since 1.8.0 |
| 30 |
*/ |
| 31 |
public function init() { |
| 32 |
|
| 33 |
// Define field type information. |
| 34 |
$this->name = esc_html__( 'Photo', 'charitable' ); |
| 35 |
$this->type = 'photo'; |
| 36 |
$this->icon = 'fa-image'; |
| 37 |
$this->order = 30; |
| 38 |
|
| 39 |
$this->align_default = 'center'; |
| 40 |
|
| 41 |
// Edit/Duplication information. |
| 42 |
$this->edit_label = esc_html__( 'Edit Photo', 'charitable' ); |
| 43 |
$this->edit_type = 'photo'; |
| 44 |
$this->edit_section = 'standard'; |
| 45 |
$this->can_be_edited = true; |
| 46 |
$this->can_be_deleted = true; |
| 47 |
$this->can_be_duplicated = true; |
| 48 |
|
| 49 |
// Misc. |
| 50 |
$this->tooltip = esc_html__( 'Select a photo from your media gallery.', 'charitable' ); |
| 51 |
|
| 52 |
// Define additional field properties. |
| 53 |
add_action( 'charitable_builder_frontend_js', array( $this, 'frontend_js' ) ); |
| 54 |
add_action( 'charitable_builder_backend_scripts', array( $this, 'builder_js' ) ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Photo options panel inside the builder. |
| 59 |
* |
| 60 |
* @since 1.8.0 |
| 61 |
* |
| 62 |
* @param array $field Photo settings. |
| 63 |
*/ |
| 64 |
public function field_options( $field ) { |
| 65 |
/* |
| 66 |
* Basic field options. |
| 67 |
*/ |
| 68 |
|
| 69 |
// Options open markup. |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Render the field. |
| 74 |
* |
| 75 |
* @since 1.8.0 |
| 76 |
* |
| 77 |
* @param array $field_data Any field data. |
| 78 |
* @param array $campaign_data Amount data and settings. |
| 79 |
* @param integer $field_id The field ID. |
| 80 |
* @param string $mode Where the field is being displayed ("preview" or "template"). |
| 81 |
* @param array $template_data Tempalate data. |
| 82 |
*/ |
| 83 |
public function render( $field_data = false, $campaign_data = false, $field_id = false, $mode = 'template', $template_data = false ) { |
| 84 |
|
| 85 |
if ( 'preview' === $mode ) { |
| 86 |
|
| 87 |
// Define data. |
| 88 |
$width_percentage = isset( $field_data['width_percentage'] ) ? absint( $field_data['width_percentage'] ) : 100; |
| 89 |
if ( $width_percentage >= 100 ) { |
| 90 |
$width_percentage = 95; |
| 91 |
} |
| 92 |
|
| 93 |
// Do we have an uploaded photo we can use as a better placeholder? |
| 94 |
$image_url = ! empty( $field_data['file'] ) ? esc_url( $field_data['file'] ) : false; |
| 95 |
$image_url = false === $image_url && ! empty( $field_data['default'] ) ? esc_url( $field_data['default'] ) : $image_url; |
| 96 |
|
| 97 |
if ( $image_url ) { |
| 98 |
return '<div class="primary-image-container has-image placeholder"><div class="primary-image"><img class="charitable-campaign-builder-preview-photo" src="' . esc_url( $image_url ) . '" /></div></div>'; |
| 99 |
} else { |
| 100 |
return '<div class="primary-image-container placeholder"><div class="primary-image"><img src="' . esc_url( charitable()->get_path( 'directory', false ) . 'assets/images/campaign-builder/fields/photo/temp-icon.svg' ) . '" class="temp-icon" alt="" /></div></div>'; |
| 101 |
} |
| 102 |
|
| 103 |
} else { |
| 104 |
|
| 105 |
$image_url = ! empty( $field_data['file'] ) ? esc_url( $field_data['file'] ) : ''; |
| 106 |
$image_url = '' === $image_url && filter_var( $field_data['default'], FILTER_VALIDATE_URL ) ? esc_url( $field_data['default'] ) : $image_url; |
| 107 |
$image_url = '' === $image_url && ! empty( $field_data['default'] && ! empty( $campaign_data['template_id'] ) ) ? esc_url( charitable()->get_path( 'directory', false ) . 'assets/images/campaign-builder/templates/' . esc_attr( $campaign_data['template_id'] ) . '/' . $field_data['default'] ) : $image_url; |
| 108 |
$image_url = '' === $image_url ? apply_filters( 'charitable_campaign_builder_photo_image_placeholder_url', charitable()->get_path( 'directory', false ) . 'assets/images/campaign-builder/photo-default-image.png' ) : $image_url; |
| 109 |
$alt_text = ! empty( $field_data['alt_text'] ) ? trim( wp_strip_all_tags( $field_data['alt_text'] ) ): ''; |
| 110 |
$css_class = ! empty( $field_data['css_class'] ) ? ' class="' . esc_attr( $field_data['css_class'] ) . '" ' : ''; |
| 111 |
$photo_attr = apply_filters( 'charitable_campaign_builder_photo_image_attributes', false ); |
| 112 |
|
| 113 |
ob_start(); |
| 114 |
|
| 115 |
?> |
| 116 |
|
| 117 |
<div class="charitable-campaign-primary-image"> |
| 118 |
<img <?php echo esc_attr( $css_class ); ?> src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $alt_text ); ?>" <?php echo esc_attr( $photo_attr ); ?> /> |
| 119 |
</div> |
| 120 |
|
| 121 |
|
| 122 |
<?php |
| 123 |
|
| 124 |
$html = ob_get_clean(); |
| 125 |
|
| 126 |
return $html; |
| 127 |
|
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Photo preview inside the builder. |
| 133 |
* |
| 134 |
* @since 1.8.0 |
| 135 |
* |
| 136 |
* @param array $field_data Field data and settings. |
| 137 |
* @param array $campaign_data Campaign data and settings. |
| 138 |
* @param array $field_id Field ID. |
| 139 |
* @param string $theme Template data. |
| 140 |
*/ |
| 141 |
public function field_preview( $field_data = false, $campaign_data = false, $field_id = false, $theme = '' ) { |
| 142 |
|
| 143 |
$html = $this->field_title( $this->name ); |
| 144 |
$html .= $this->field_wrapper( $this->render( $field_data, $campaign_data, $field_id, 'preview' ), $field_data ); |
| 145 |
|
| 146 |
echo wp_kses_post( $html ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Photo display on the form front-end. |
| 151 |
* |
| 152 |
* @since 1.8.0 |
| 153 |
* |
| 154 |
* @param string $field_type Field Type. |
| 155 |
* @param array $field_data Deprecated. |
| 156 |
* @param array $campaign_data Form data and settings. |
| 157 |
*/ |
| 158 |
public function field_display( $field_type = '', $field_data = false, $campaign_data = false ) { |
| 159 |
|
| 160 |
$html = $this->field_display_wrapper( $this->render( $field_data, $campaign_data ), $field_data ); |
| 161 |
|
| 162 |
echo wp_kses_post( apply_filters( 'charitable_campaign_builder_' . $this->type . '_field_display', $html, $campaign_data ) ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Photo display on the form front-end. |
| 167 |
* |
| 168 |
* @since 1.8.0 |
| 169 |
* |
| 170 |
* @param array $field_id Photo settings. |
| 171 |
* @param array $campaign_data Form data and settings. |
| 172 |
*/ |
| 173 |
public function settings_display( $field_id = false, $campaign_data = false ) { |
| 174 |
|
| 175 |
$charitable_builder_form_fields = new Charitable_Builder_Form_Fields(); |
| 176 |
|
| 177 |
$settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false; |
| 178 |
|
| 179 |
// special check - if "default" is just a filename, readjust the default to a full url. |
| 180 |
if ( ! empty( $settings['default'] ) && ! empty( $campaign_data['template_id'] ) ) { |
| 181 |
$default_basename = basename( $settings['default'] ); |
| 182 |
if ( $default_basename === $settings['default'] ) { |
| 183 |
$settings['default'] = charitable()->get_path( 'assets', false ) . 'images/campaign-builder/templates/' . $campaign_data['template_id'] . '/' . $default_basename; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
ob_start(); |
| 188 |
|
| 189 |
?> |
| 190 |
|
| 191 |
<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> |
| 192 |
|
| 193 |
<?php |
| 194 |
|
| 195 |
echo $charitable_builder_form_fields->generate_uploader( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 196 |
isset( $settings['file'] ) ? $settings['file'] : false, |
| 197 |
esc_html__( 'Photo URL', 'charitable' ), |
| 198 |
array( |
| 199 |
'id' => 'field_' . esc_attr( $this->type ) . '_file' . '_' . intval( $field_id ), // phpcs:ignore |
| 200 |
'name' => array( '_fields', intval( $field_id ), 'file' ), |
| 201 |
'field_id' => esc_attr( $field_id ), |
| 202 |
'button_label' => esc_html__( 'Upload', 'charitable' ), |
| 203 |
'placeholder' => 'https://', |
| 204 |
'description' => esc_html__( 'Upload a new image or add an existing image.', 'charitable' ), |
| 205 |
'tooltip' => esc_attr( $this->tooltip ), |
| 206 |
) |
| 207 |
); |
| 208 |
|
| 209 |
echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 210 |
isset( $settings['alt_text'] ) ? $settings['alt_text'] : false, |
| 211 |
esc_html__( 'ALT Text', 'charitable' ), |
| 212 |
array( |
| 213 |
'id' => 'field_' . esc_attr( $this->type ) . '_alt_text' . '_' . intval( $field_id ), // phpcs:ignore |
| 214 |
'name' => array( '_fields', intval( $field_id ), 'alt_text' ), |
| 215 |
'field_id' => intval( $field_id ), |
| 216 |
'tooltip' => esc_html__( 'Provide text for the ALT tag of this image.', 'charitable' ), |
| 217 |
) |
| 218 |
); |
| 219 |
|
| 220 |
echo $charitable_builder_form_fields->generate_hidden_field( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 221 |
isset( $settings['default'] ) ? $settings['default'] : false, |
| 222 |
array( |
| 223 |
'id' => 'field_' . esc_attr( $this->type ) . '_default' . '_' . intval( $field_id ), // phpcs:ignore |
| 224 |
'name' => array( '_fields', intval( $field_id ), 'default' ), |
| 225 |
'field_id' => intval( $field_id ), |
| 226 |
) |
| 227 |
); |
| 228 |
|
| 229 |
echo $charitable_builder_form_fields->generate_number_slider( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 230 |
isset( $settings['width_percentage'] ) ? $settings['width_percentage'] : 100, |
| 231 |
esc_html__( 'Width', 'charitable' ), |
| 232 |
array( |
| 233 |
'id' => 'field_' . esc_attr( $this->type ) . '_width_percentage' . '_' . intval( $field_id ), // phpcs:ignore |
| 234 |
'name' => array( '_fields', intval( $field_id ), 'width_percentage' ), |
| 235 |
'field_type' => esc_attr( $this->type ), |
| 236 |
'field_id' => intval( $field_id ), |
| 237 |
'symbol' => '%', |
| 238 |
'min' => 0, |
| 239 |
'min_actual' => 40, |
| 240 |
'css_class' => 'charitable-indicator-on-hover', |
| 241 |
'tooltip' => esc_html__( 'Adjust the width of the field within the column.', 'charitable' ), |
| 242 |
) |
| 243 |
); |
| 244 |
|
| 245 |
echo $charitable_builder_form_fields->generate_align( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 246 |
isset( $settings['align'] ) ? $settings['align'] : esc_attr( $this->align_default ), |
| 247 |
esc_html__( 'Align', 'charitable' ), |
| 248 |
array( |
| 249 |
'id' => 'field_' . esc_attr( $this->type ) . '_align' . '_' . intval( $field_id ), // phpcs:ignore |
| 250 |
'name' => array( '_fields', intval( $field_id ), 'align' ), |
| 251 |
'field_id' => intval( $field_id ), |
| 252 |
) |
| 253 |
); |
| 254 |
|
| 255 |
echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 256 |
isset( $settings['css_class'] ) ? $settings['css_class'] : false, |
| 257 |
esc_html__( 'CSS Class', 'charitable' ), |
| 258 |
array( |
| 259 |
'id' => 'field_' . esc_attr( $this->type ) . '_css_class' . '_' . intval( $field_id ), |
| 260 |
'name' => array( '_fields', intval( $field_id ), 'css_class' ), |
| 261 |
'field_id' => intval( $field_id ), |
| 262 |
'tooltip' => esc_html__( 'Add CSS classes (seperated by a space) for this field to customize it\'s appearance in your theme.', 'charitable' ), |
| 263 |
) |
| 264 |
); |
| 265 |
|
| 266 |
$html = ob_get_clean(); |
| 267 |
|
| 268 |
return $html; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Return HTML for display via ajax. |
| 273 |
* |
| 274 |
* @since 1.8.0 |
| 275 |
* @version 1.8.9.1 |
| 276 |
*/ |
| 277 |
public function settings_display_ajax() { |
| 278 |
|
| 279 |
// phpcs:disable WordPress.Security.NonceVerification.Missing |
| 280 |
$field_id = isset( $_POST['field_id'] ) ? intval( $_POST['field_id'] ) : false; |
| 281 |
$campaign_id = isset( $_POST['campaign_id'] ) ? intval( $_POST['campaign_id'] ) : false; |
| 282 |
// phpcs:enable WordPress.Security.NonceVerification.Missing |
| 283 |
|
| 284 |
if ( false === $field_id ) { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
$charitable_builder_form_fields = new Charitable_Builder_Form_Fields(); |
| 289 |
|
| 290 |
$campaign_data = get_post_meta( $campaign_id, 'campaign_settings_v2', true ); |
| 291 |
$settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false; |
| 292 |
|
| 293 |
ob_start(); |
| 294 |
|
| 295 |
?> |
| 296 |
|
| 297 |
<?php |
| 298 |
|
| 299 |
echo $charitable_builder_form_fields->generate_number_slider( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 300 |
isset( $settings['width_percentage'] ) ? $settings['width_percentage'] : 100, |
| 301 |
esc_html__( 'Width', 'charitable' ), |
| 302 |
array( |
| 303 |
'id' => 'field_' . esc_attr( $this->type ) . '_width_percentage' . '_' . intval( $field_id ), // phpcs:ignore |
| 304 |
'name' => array( '_fields', intval( $field_id ), 'width_percentage' ), |
| 305 |
'field_type' => esc_attr( $this->type ), |
| 306 |
'field_id' => intval( $field_id ), |
| 307 |
'symbol' => '%', |
| 308 |
'min' => 10, |
| 309 |
'tooltip' => esc_html__( 'Adjust the width of the field within the column.', 'charitable' ), |
| 310 |
) |
| 311 |
); |
| 312 |
|
| 313 |
echo $charitable_builder_form_fields->generate_align( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 314 |
isset( $settings['align'] ) ? $settings['align'] : false, |
| 315 |
esc_html__( 'Align', 'charitable' ), |
| 316 |
array( |
| 317 |
'id' => 'field_' . esc_attr( $this->type ) . '_align' . '_' . intval( $field_id ), // phpcs:ignore |
| 318 |
'name' => array( '_fields', intval( $field_id ), 'align' ), |
| 319 |
'field_id' => intval( $field_id ), |
| 320 |
) |
| 321 |
); |
| 322 |
|
| 323 |
echo $charitable_builder_form_fields->generate_text( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 324 |
$settings['css_class'], |
| 325 |
esc_html__( 'CSS Class', 'charitable' ), |
| 326 |
array( |
| 327 |
'id' => 'field_' . esc_attr( $this->type ) . '_css_class', |
| 328 |
'name' => array( 'fields', esc_attr( $this->type ), 'css_class' ), |
| 329 |
'tooltip' => esc_html( $this->tooltip ), |
| 330 |
) |
| 331 |
); |
| 332 |
|
| 333 |
?> |
| 334 |
|
| 335 |
<?php |
| 336 |
|
| 337 |
$html = ob_get_clean(); |
| 338 |
|
| 339 |
wp_send_json_success( array( 'html' => $html ) ); |
| 340 |
|
| 341 |
exit; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Enqueue frontend limit option js. |
| 346 |
* |
| 347 |
* @since 1.8.0 |
| 348 |
* |
| 349 |
* @param string $min The suffix, if any, to include for the minified version. |
| 350 |
*/ |
| 351 |
public function builder_js( $min = '' ) { |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Enqueue frontend limit option js. |
| 356 |
* |
| 357 |
* @since 1.8.0 |
| 358 |
* |
| 359 |
* @param string $min The suffix, if any, to include for the minified version. |
| 360 |
*/ |
| 361 |
public function frontend_js( $min = '' ) { |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Format and sanitize field. |
| 366 |
* |
| 367 |
* @since 1.8.0 |
| 368 |
* |
| 369 |
* @param int $field Photo ID. |
| 370 |
* @param mixed $field_data Photo value that was submitted. |
| 371 |
* @param array $campaign_data Form data and settings. |
| 372 |
*/ |
| 373 |
public function format( $field, $field_data = false, $campaign_data = false ) { |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Validate field on form submit. |
| 378 |
* |
| 379 |
* @since 1.8.0 |
| 380 |
* |
| 381 |
* @param int $field Photo ID. |
| 382 |
* @param mixed $field_data Photo value that was submitted. |
| 383 |
* @param array $campaign_data Form data and settings. |
| 384 |
*/ |
| 385 |
public function validate( $field, $field_data = false, $campaign_data = false ) { |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
new Charitable_Field_Photo(); |
| 390 |
|
| 391 |
endif; |