PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.11
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.11
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / includes / admin / campaign-builder / fields / class-social-sharing.php

class-social-sharing.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.11, at includes/admin/campaign-builder/fields/class-social-sharing.php

543 lines 21.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class to add a social sharing area 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.7
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Field_Social_Sharing' ) ) :
18
19 /**
20 * Class to add campaign organizer social sharing field to a campaign form in the builder.
21 */
22 class Charitable_Field_Social_Sharing 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__( 'Social Sharing', 'charitable' );
33 $this->type = 'social-sharing';
34 $this->icon = 'fa-share-alt';
35 $this->order = 100;
36
37 $this->align_default = 'left';
38
39 // Edit/Duplication information.
40 $this->can_be_edited = true;
41 $this->can_be_deleted = true;
42 $this->can_be_duplicated = true;
43 $this->edit_label = esc_html__( 'Edit Social Sharing', 'charitable' );
44 $this->edit_type = 'social-sharing';
45 $this->edit_section = 'standard';
46
47 // Misc.
48 $this->tooltip = '';
49
50 // Define additional field properties.
51 add_action( 'charitable_frontend_js', [ $this, 'frontend_js' ] );
52 }
53
54 /**
55 * Social Sharing options panel inside the builder.
56 *
57 * @since 1.8.0
58 *
59 * @param array $field Social Sharing settings.
60 */
61 public function field_options( $field ) {
62 /*
63 * Basic field options.
64 */
65
66 // Options open markup.
67 }
68
69 /**
70 * Render the field.
71 *
72 * @since 1.8.0
73 *
74 * @param array $field_data Any field data.
75 * @param array $campaign_data Amount data and settings.
76 * @param integer $field_id The field ID.
77 * @param string $mode Where the field is being displayed ("preview" or "template").
78 * @param array $template_data Tempalate data.
79 */
80 public function render( $field_data = false, $campaign_data = false, $field_id = false, $mode = 'template', $template_data = false ) {
81
82 // $social_networks = $this->get_social_networks( $campaign_data );
83
84 $headline = ! empty( $field_data['headline'] ) ? esc_html( $field_data['headline'] ) : esc_html__( 'Share:', 'charitable' );
85
86 $html = '<div class="charitable-field-' . $mode . '-social-sharing">
87 <div class="charitable-field-' . $mode . '-social-sharing-headline-container placeholder">';
88
89 if ( false !== $headline ) {
90 $html .= '<h5 class="charitable-field-' . $mode . '-headline">' . $headline . '</h5>';
91 }
92
93 $html .= '</div>
94 <div class="charitable-field-row">';
95
96 $social_networks_settings = (array) isset( $campaign_data['fields'][ intval( $field_id ) ]['social_networks'] ) && ! empty( $campaign_data['fields'][ intval( $field_id ) ]['social_networks'] ) ? $campaign_data['fields'][ intval( $field_id ) ]['social_networks'] : false;
97
98 $defaults = array(
99 'twitter' => 1,
100 'facebook' => 1,
101 'linkedin' => 1,
102 'pinterest' => 1,
103 'mastodon' => 0,
104 'threads' => 0,
105 'bluesky' => 1,
106 );
107
108 // Load the class templates file.
109 $file = charitable()->get_path( 'includes' ) . 'admin/campaign-builder/templates/class-templates.php';
110
111 if ( file_exists( $file ) ) {
112 require_once $file;
113 }
114 $template_id = ! empty( $campaign_data['template_id'] ) ? esc_attr( $campaign_data['template_id'] ) : false;
115 $suffix = ! empty( $template_data['meta']['suffixes'][ $this->type ] ) ? esc_attr( $template_data['meta']['suffixes'][ $this->type ] ) : false;
116
117 // $social_networks_settings = wp_parse_args( $social_networks_settings, $defaults );
118 if ( ! is_array( $social_networks_settings ) || empty( $social_networks_settings ) ) {
119 $social_networks_settings = $defaults;
120 } else {
121 foreach ( $defaults as $key => $value ) {
122 if ( ! isset( $social_networks_settings[ $key ] ) ) {
123 $social_networks_settings[ $key ] = 0;
124 }
125 }
126 }
127 $social_networks_template = ( $mode === 'template' ) ? $this->get_social_networks( $campaign_data, $mode, $suffix ) : false;
128
129 $open_new_tab = ! empty( $field_data['open_new_tab'] ) ? esc_html( $field_data['open_new_tab'] ) : 1;
130
131 if ( ! empty( $social_networks_settings ) ) {
132
133 foreach ( $social_networks_settings as $network => $selected ) {
134
135 if ( 1 === intval( $selected ) ) {
136
137 if ( $mode === 'template' ) {
138
139 $label = $network;
140
141 $icon_url = ( ! empty( $social_networks_template[ $label ]['icon_url'] ) ) ? $social_networks_template[ $label ]['icon_url'] : charitable()->get_path( 'directory', false ) . 'assets/images/campaign-builder/fields/social-sharing/mastodon.svg'; // todo: replace with a genric icon of some sort.
142 $link = isset( $field_data[ $label . '_url' ] ) ? esc_url( $field_data[ $label . '_url' ] ) : '#';
143 $share_url = false;
144 $href = false;
145
146 // Prepare the custom share URL.
147 if ( 'mastodon' !== $network && ! empty( $social_networks_template[ $label ]['share_url'] ) ) {
148 $share_url = str_replace( '{$text}', rawurlencode( wp_strip_all_tags( $campaign_data['title'] ) ), $social_networks_template[ $label ]['share_url'] );
149 $share_url = str_replace( '{$url}', rawurlencode( get_permalink( $campaign_data['id'] ) ), $share_url );
150 $share_url = apply_filters( 'charitable_campaign_social_sharing_link_' . $network, $share_url, $social_networks_template[ $label ], $campaign_data );
151 $href = '<a href="' . esc_url( $share_url ) . '" target="_blank">';
152 } elseif ( 'mastodon' === $network ) {
153 $href = '<a href="#" target="mastodon" class="charitable-mastodon-share">';
154 }
155
156 if ( $link && $href ) {
157
158 $html .= '<div class="charitable-social-field-column charitable-social-sharing-' . $mode . '-' . esc_attr( $network ) . '">
159 <div class="charitable-campaign-social-link" data-social-network="' . $label . '">
160 ' . $href . '
161 <div><img title="' . esc_html__( 'Share On', 'charitable' ) . ' ' . $social_networks_template[ $label ]['public_label'] . '" src="' . $icon_url . '" alt="' . $social_networks_template[ $label ]['public_label'] . '" /></div>
162 <p>' . $social_networks_template[ $label ]['public_label'] . '</p>
163 </a>
164 </div>
165 </div>';
166
167 }
168 } else {
169
170 $html .= '<div class="charitable-social-field-column charitable-social-sharing-' . $mode . '-' . esc_attr( $network ) . '">
171 <span class="charitable-placeholder"></span>
172 </div>';
173
174 }
175 } elseif ( $mode === 'preview' ) {
176 $html .= '<div class="charitable-social-field-column charitable-hidden charitable-social-sharing-' . $mode . '-' . esc_attr( $network ) . '">
177 <span class="charitable-placeholder"></span>
178 </div>';
179 }
180 }
181 } else {
182
183 $html .= '<div class="charitable-social-field-column charitable-hidden charitable-social-sharing-' . $mode . '-twitter">
184 <span class="charitable-placeholder"></span>
185 </div>
186 <div class="charitable-social-field-column charitable-hidden charitable-social-sharing-' . $mode . '-facebook">
187 <span class="charitable-placeholder"></span>
188 </div>
189 <div class="charitable-social-field-column charitable-hidden charitable-social-sharing-' . $mode . '-linkedin">
190 <span class="charitable-placeholder"></span>
191 </div>
192 <div class="charitable-social-field-column charitable-hidden charitable-social-sharing-' . $mode . '-pinterest">
193 <span class="charitable-placeholder"></span>
194 </div>
195 <div class="charitable-social-field-column charitable-hidden charitable-social-sharing-' . $mode . '-mastodon">
196 <span class="charitable-placeholder"></span>
197 </div>
198 <div class="charitable-social-field-column charitable-hidden charitable-social-sharing-' . $mode . '-threads">
199 <span class="charitable-placeholder"></span>
200 </div>
201 <div class="charitable-social-field-column charitable-hidden charitable-social-sharing-' . $mode . '-bluesky">
202 <span class="charitable-placeholder"></span>
203 </div>';
204
205 }
206
207 $html .= '</div></div>';
208
209 return $html;
210 }
211
212 /**
213 * Social Sharing preview inside the builder.
214 *
215 * @since 1.8.0
216 *
217 * @param array $field_data Field data and settings.
218 * @param array $campaign_data Campaign data and settings.
219 * @param array $field_id Field ID.
220 * @param string $theme Template data.
221 */
222 public function field_preview( $field_data = false, $campaign_data = false, $field_id = false, $theme = '' ) {
223
224 $html = $this->field_title( $this->name );
225 $html .= $this->field_wrapper( $this->render( $field_data, $campaign_data, $field_id, 'preview', $theme ), $field_data );
226
227 echo $html; // phpcs:ignore
228 }
229
230 /**
231 * The display on the campaign front-end.
232 *
233 * @since 1.8.0
234 *
235 * @param string $field_type The passed field type.
236 * @param array $field_data Any field data.
237 * @param array $campaign_data Amount data and settings.
238 * @param bool $is_preview_page If the page is a preview page.
239 * @param int $field_id The field ID.
240 * @param array $template_data Template data.
241 */
242 public function field_display( $field_type = '', $field_data = false, $campaign_data = false, $is_preview_page = false, $field_id = false, $template_data = false ) {
243
244 $html = $this->field_display_wrapper( $this->render( $field_data, $campaign_data, $field_id, 'template', $template_data ), $field_data );
245
246 echo apply_filters( 'charitable_campaign_builder_' . esc_attr( $this->type ) . '_field_display', $html, $campaign_data ); // phpcs:ignore
247 }
248
249 /**
250 * The display on the form settings backend when the user clicks on the field/block.
251 *
252 * @since 1.8.0
253 *
254 * @param array $field_id Social Sharing settings.
255 * @param array $campaign_data Campaign data and settings.
256 */
257 public function settings_display( $field_id = false, $campaign_data = false ) {
258
259 if ( ! class_exists( 'Charitable_Builder_Form_Fields' ) ) {
260 return;
261 }
262
263 $charitable_builder_form_fields = new Charitable_Builder_Form_Fields();
264
265 $settings = isset( $campaign_data['fields'][ $field_id ] ) ? $campaign_data['fields'][ $field_id ] : false;
266 $social_networks = $this->get_social_networks( $campaign_data );
267
268 if ( empty( $social_networks ) ) {
269 return;
270 }
271
272 add_action( 'charitable_builder_' . esc_attr( $this->type ) . '_settings_display_start', [ $this, 'settings_section_top' ], 10, 2 );
273 add_action( 'charitable_builder_' . esc_attr( $this->type ) . '_settings_display_end', [ $this, 'settings_section_bottom' ], 10, 2 );
274
275 ob_start();
276
277 ?>
278
279 <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>
280
281 <div class="charitable-panel-field charitable-panel-field-section" data-field-id="<?php echo intval( $field_id ); ?>">
282
283 <?php do_action( 'charitable_builder_' . esc_attr( $this->type ) . '_settings_display_start', $field_id, $campaign_data ); ?>
284
285 </div>
286
287 <?php
288
289 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore
290 isset( $settings['headline'] ) ? $settings['headline'] : esc_html__( 'Share:', 'charitable' ),
291 esc_html__( 'Headline', 'charitable' ),
292 array(
293 'id' => 'field_' . esc_attr( $this->type ) . '_headline' . '_' . intval( $field_id ), // phpcs:ignore
294 'name' => array( '_fields', intval( $field_id ), 'headline' ),
295 'field_id' => intval( $field_id ),
296 'class' => 'charitable-campaign-builder-headline',
297 )
298 );
299
300 // Go through all the social networks.
301 echo $charitable_builder_form_fields->generate_checkboxes( // phpcs:ignore
302 isset( $settings['social_networks'] ) ? $settings['social_networks'] : false,
303 esc_html__( 'Choose your social network(s):', 'charitable' ),
304 array(
305 'id' => 'field_' . esc_attr( $this->type ) . '_social_networks' . '_' . intval( $field_id ), // phpcs:ignore
306 'name' => array( '_fields', intval( $field_id ), 'social_networks' ),
307 'field_id' => intval( $field_id ),
308 'tooltip' => esc_html( $this->tooltip ),
309 'class' => 'charitable-social-network-checkboxes',
310 'options' => array(
311 'Twitter / X' => 'twitter',
312 'Facebook' => 'facebook',
313 'LinkedIn' => 'linkedin',
314 'Pinterest' => 'pinterest',
315 'Mastodon' => 'mastodon',
316 'Threads' => 'threads',
317 'Bluesky' => 'bluesky',
318 ),
319 'defaults' => array(
320 'twitter',
321 'facebook',
322 'linkedin',
323 'pinterest',
324 'mastodon',
325 'threads',
326 'bluesky',
327 ),
328 )
329 );
330
331 echo $charitable_builder_form_fields->generate_toggle( // phpcs:ignore
332 isset( $settings['open_new_tab'] ) ? $settings['open_new_tab'] : 1,
333 esc_html__( 'Open Links In New Tab', 'charitable' ),
334 array(
335 'id' => 'field_' . esc_attr( $this->type ) . '_open_new_tab' . '_' . intval( $field_id ), // phpcs:ignore
336 'name' => array( '_fields', intval( $field_id ), 'open_new_tab' ),
337 'field_id' => intval( $field_id ),
338 )
339 );
340
341 echo $charitable_builder_form_fields->generate_divider( false, false, array( 'field_id' => $field_id ) ); // phpcs:ignore
342
343 echo $charitable_builder_form_fields->generate_number_slider( // phpcs:ignore
344 isset( $settings['width_percentage'] ) ? $settings['width_percentage'] : 100,
345 esc_html__( 'Width', 'charitable' ),
346 array(
347 'id' => 'field_' . esc_attr( $this->type ) . '_width_percentage' . '_' . intval( $field_id ), // phpcs:ignore
348 'name' => array( '_fields', intval( $field_id ), 'width_percentage' ),
349 'field_type' => esc_attr( $this->type ),
350 'field_id' => intval( $field_id ),
351 'symbol' => '%',
352 'min' => 0,
353 'min_actual' => '50',
354 'css_class' => 'charitable-indicator-on-hover',
355 'tooltip' => esc_html__( 'Adjust the width of the field within the column.', 'charitable' ),
356 )
357 );
358
359 echo $charitable_builder_form_fields->generate_align( // phpcs:ignore
360 isset( $settings['align'] ) ? $settings['align'] : esc_attr( $this->align_default ),
361 esc_html__( 'Align', 'charitable' ),
362 array(
363 'id' => 'field_' . esc_attr( $this->type ) . '_align' . '_' . intval( $field_id ), // phpcs:ignore
364 'name' => array( '_fields', intval( $field_id ), 'align' ),
365 'field_id' => intval( $field_id ),
366 )
367 );
368
369 /* CSS CLASS */
370
371 echo $charitable_builder_form_fields->generate_text( // phpcs:ignore
372 isset( $settings['css_class'] ) ? $settings['css_class'] : false,
373 esc_html__( 'CSS Class', 'charitable' ),
374 array(
375 'id' => 'field_' . esc_attr( $this->type ) . '_css_class' . '_' . intval( $field_id ), // phpcs:ignore
376 'name' => array( '_fields', intval( $field_id ), 'css_class' ),
377 'field_id' => intval( $field_id ),
378 'tooltip' => esc_html__( 'Add CSS classes (seperated by a space) for this field to customize it\'s appearance in your theme.', 'charitable' ),
379 )
380 );
381
382 ?>
383
384 <div class="charitable-panel-field charitable-panel-field-section" data-field-id="<?php echo intval( $field_id ); ?>">
385
386 <?php do_action( 'charitable_builder_' . esc_attr( $this->type ) . '_settings_display_end', $field_id, $campaign_data ); ?>
387
388 </div>
389
390 <?php
391
392 $html = ob_get_clean();
393
394 remove_action( 'charitable_builder_' . esc_attr( $this->type ) . '_settings_display_start', [ $this, 'settings_section_top' ], 10 );
395 remove_action( 'charitable_builder_' . esc_attr( $this->type ) . '_settings_display_end', [ $this, 'settings_section_bottom' ], 10 );
396
397 return $html;
398 }
399
400 /**
401 * Display content above the content settings in the panel in the admin via hook.
402 *
403 * @since 1.8.0
404 *
405 * @param integer $field_id Field ID.
406 * @param array $campaign_data Data on campaign.
407 */
408 public function settings_section_top( $field_id = false, $campaign_data = false ) {
409
410 $message = '<p>' . esc_html__( 'Don\'t see a social network here that you use and would like added?', 'charitable' ) . ' <a target="_blank" class="charitable-new-tab-link" data-type="' . esc_attr( $this->edit_type ) . '" data-section="' . $this->edit_section . '" href="https://wpcharitable.com/contact">' . esc_html__( 'Let us know!', 'charitable' ) . '</a></p>';
411
412 echo $message; // phpcs:ignore.
413 }
414
415 /**
416 * Display content above the content settings in the panel in the admin via hook.
417 *
418 * @since 1.8.0
419 *
420 * @param integer $field_id Field ID.
421 * @param array $campaign_data Data on campaign.
422 */
423 public function settings_section_bottom( $field_id = false, $campaign_data = false ) {
424 }
425
426 /**
427 * Enqueue frontend limit option js.
428 *
429 * @since 1.8.0
430 *
431 * @param array $forms Forms on the current page.
432 */
433 public function frontend_js( $forms ) {
434 }
435
436 /**
437 * Format and sanitize field.
438 *
439 * @since 1.8.0
440 *
441 * @param int $field_id Social Sharing ID.
442 * @param mixed $field_submit Social Sharing value that was submitted.
443 * @param array $campaign_data Form data and settings.
444 */
445 public function format( $field_id, $field_submit, $campaign_data ) {
446 }
447
448 /**
449 * Validate field on form submit.
450 *
451 * @since 1.8.0
452 *
453 * @param int $field_id Social Sharing ID.
454 * @param mixed $field_submit Social Sharing value that was submitted.
455 * @param array $campaign_data Form data and settings.
456 */
457 public function validate( $field_id, $field_submit, $campaign_data ) {
458 }
459
460 /**
461 * Return main list of social networks.
462 *
463 * @since 1.8.0
464 *
465 * @param array $campaign_data Form data and settings.
466 * @param string $mode Preview or template.
467 * @param string $suffix Any suffix.
468 */
469 public function get_social_networks( $campaign_data = false, $mode = 'preview', $suffix = '' ) {
470
471 return (array) apply_filters(
472 'charitable_builder_social_sharing_networks',
473 array(
474 'twitter' => array(
475 'label' => esc_html__( 'Twitter / X', 'charitable' ),
476 'css_class' => '',
477 'field_id' => 'twitter_url',
478 'public_label' => esc_html__( 'Twitter / X', 'charitable' ),
479 'icon_url' => charitable()->get_path( 'directory', false ) . 'assets/images/campaign-builder/fields/social-sharing/twitter' . $suffix . '.svg',
480 'share_url' => 'https://x.com/intent/post?text={$text}&url={$url}',
481 ),
482 'facebook' => array(
483 'label' => esc_html__( 'Facebook', 'charitable' ),
484 'css_class' => '',
485 'field_id' => 'facebook_url',
486 'public_label' => esc_html__( 'Facebook', 'charitable' ),
487 'icon_url' => charitable()->get_path( 'directory', false ) . 'assets/images/campaign-builder/fields/social-sharing/facebook' . $suffix . '.svg',
488 'share_url' => 'https://www.facebook.com/sharer/sharer.php?u={$url}',
489 ),
490 'linkedin' => array(
491 'label' => esc_html__( 'LinkedIn', 'charitable' ),
492 'css_class' => '',
493 'field_id' => 'linkedin_url',
494 'public_label' => esc_html__( 'LinkedIn', 'charitable' ),
495 'icon_url' => charitable()->get_path( 'directory', false ) . 'assets/images/campaign-builder/fields/social-sharing/linkedin' . $suffix . '.svg',
496 'share_url' => 'https://www.linkedin.com/sharing/share-offsite/?url={$url}',
497 ),
498 'pinterest' => array(
499 'label' => esc_html__( 'Pinterest', 'charitable' ),
500 'css_class' => '',
501 'field_id' => 'pinterest_url',
502 'public_label' => esc_html__( 'Pinterest', 'charitable' ),
503 'icon_url' => charitable()->get_path( 'directory', false ) . 'assets/images/campaign-builder/fields/social-sharing/pinterest' . $suffix . '.svg',
504 'share_url' => 'https://www.pinterest.com/pin/create/button/?url={$url}&description={$text}',
505 ),
506 'mastodon' => array(
507 'label' => esc_html__( 'Mastodon', 'charitable' ),
508 'css_class' => '',
509 'field_id' => 'mastodon_url',
510 'public_label' => esc_html__( 'Mastodon', 'charitable' ),
511 'icon_url' => charitable()->get_path( 'directory', false ) . 'assets/images/campaign-builder/fields/social-sharing/mastodon' . $suffix . '.svg',
512 'share_url' => '',
513 'on_click' => 'charitableMastodonShare',
514 'data' => array(
515 'src' => false,
516 ),
517 ),
518 'threads' => array(
519 'label' => esc_html__( 'Threads', 'charitable' ),
520 'css_class' => '',
521 'field_id' => 'threads_url',
522 'public_label' => esc_html__( 'Threads', 'charitable' ),
523 'icon_url' => charitable()->get_path( 'directory', false ) . 'assets/images/campaign-builder/fields/social-sharing/threads' . $suffix . '.svg',
524 'share_url' => 'https://www.threads.net/intent/post?text={$text}%0D%0A%0D%0A{$url}',
525 ),
526 'bluesky' => array(
527 'label' => esc_html__( 'Bluesky', 'charitable' ),
528 'css_class' => '',
529 'field_id' => 'bluesky_url',
530 'public_label' => esc_html__( 'Bluesky', 'charitable' ),
531 'icon_url' => charitable()->get_path( 'directory', false ) . 'assets/images/campaign-builder/fields/social-sharing/bluesky' . $suffix . '.svg',
532 'share_url' => 'https://bsky.app/intent/compose?text={$text}%0D%0A%0D%0A{$url}',
533 ),
534
535 ),
536 $campaign_data
537 );
538 }
539 }
540
541 new Charitable_Field_Social_Sharing();
542
543 endif;