PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.7
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.7
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 / class-charitable-admin.php

class-charitable-admin.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.7, at includes/admin/class-charitable-admin.php

698 lines 17.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class that sets up the Charitable Admin functionality.
4 *
5 * @package Charitable/Classes/Charitable_Admin
6 * @author David Bisset
7 * @copyright Copyright (c) 2022, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.6.44
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Admin' ) ) :
19
20 /**
21 * Charitable_Admin
22 *
23 * @final
24 * @since 1.0.0
25 */
26 final class Charitable_Admin {
27
28 /**
29 * The single instance of this class.
30 *
31 * @var Charitable_Admin|null
32 */
33 private static $instance = null;
34
35 /**
36 * Donation actions class.
37 *
38 * @var Charitable_Donation_Admin_Actions
39 */
40 private $donation_actions;
41
42 /**
43 * Set up the class.
44 *
45 * Note that the only way to instantiate an object is with the charitable_start method,
46 * which can only be called during the start phase. In other words, don't try
47 * to instantiate this object.
48 *
49 * @since 1.0.0
50 */
51 protected function __construct() {
52 $this->load_dependencies();
53
54 $this->donation_actions = new Charitable_Donation_Admin_Actions;
55
56 do_action( 'charitable_admin_loaded' );
57
58 add_action( 'wp_ajax_charitable_lite_settings_upgrade', array( $this, 'dismiss_lite_cta' ) );
59 }
60
61 /**
62 * Returns and/or create the single instance of this class.
63 *
64 * @since 1.2.0
65 *
66 * @return Charitable_Admin
67 */
68 public static function get_instance() {
69 if ( is_null( self::$instance ) ) {
70 self::$instance = new self();
71 }
72
73 return self::$instance;
74 }
75
76 /**
77 * Include admin-only files.
78 *
79 * @since 1.0.0
80 *
81 * @return void
82 */
83 private function load_dependencies() {
84 $admin_dir = charitable()->get_path( 'includes' ) . 'admin/';
85
86 require_once( $admin_dir . 'charitable-core-admin-functions.php' );
87 require_once( $admin_dir . 'campaigns/charitable-admin-campaign-hooks.php' );
88 require_once( $admin_dir . 'dashboard-widgets/charitable-dashboard-widgets-hooks.php' );
89 require_once( $admin_dir . 'donations/charitable-admin-donation-hooks.php' );
90 require_once( $admin_dir . 'settings/charitable-settings-admin-hooks.php' );
91 }
92
93 /**
94 * Get Charitable_Donation_Admin_Actions class.
95 *
96 * @since 1.5.0
97 *
98 * @return Charitable_Donation_Admin_Actions
99 */
100 public function get_donation_actions() {
101 return $this->donation_actions;
102 }
103
104 /**
105 * Do an admin action.
106 *
107 * @since 1.5.0
108 *
109 * @return boolean|WP_Error WP_Error in case of error. Mixed results if the action was performed.
110 */
111 public function maybe_do_admin_action() {
112 if ( ! array_key_exists( 'charitable_admin_action', $_GET ) ) {
113 return false;
114 }
115
116 if ( count( array_diff( array( 'action_type', '_nonce', 'object_id' ), array_keys( $_GET ) ) ) ) {
117 return new WP_Error( __( 'Action could not be executed.', 'charitable' ) );
118 }
119
120 if ( ! wp_verify_nonce( $_GET['_nonce'], 'donation_action' ) ) {
121 return new WP_Error( __( 'Action could not be executed. Nonce check failed.', 'charitable' ) );
122 }
123
124 if ( 'donation' != $_GET['action_type'] ) {
125 return new WP_Error( __( 'Action from an unknown action type executed.', 'charitable' ) );
126 }
127
128 return $this->donation_actions->do_action( $_GET['charitable_admin_action'], $_GET['object_id'] );
129 }
130
131 /**
132 * Loads admin-only scripts and stylesheets.
133 *
134 * @since 1.0.0
135 *
136 * @return void
137 */
138 public function admin_enqueue_scripts() {
139 if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
140 $suffix = '';
141 $version = '';
142 } else {
143 $suffix = '.min';
144 $version = charitable()->get_version();
145 }
146
147 $assets_dir = charitable()->get_path( 'assets', false );
148
149 /* Menu styles are loaded everywhere in the WordPress dashboard. */
150 wp_register_style(
151 'charitable-admin-menu',
152 $assets_dir . 'css/charitable-admin-menu' . $suffix . '.css',
153 array(),
154 $version
155 );
156
157 wp_enqueue_style( 'charitable-admin-menu' );
158
159 wp_register_script(
160 'charitable-admin-pages-all',
161 $assets_dir . 'js/charitable-admin-pages' . $suffix . '.js',
162 array( 'jquery' ),
163 $version,
164 false
165 );
166
167 wp_enqueue_script( 'charitable-admin-pages-all' );
168
169 /* Admin page styles are registered but only enqueued when necessary. */
170 wp_register_style(
171 'charitable-admin-pages',
172 $assets_dir . 'css/charitable-admin-pages' . $suffix . '.css',
173 array(),
174 $version
175 );
176
177 /* The following styles are only loaded on Charitable screens. */
178 $screen = get_current_screen();
179
180 /* This check covers the category and tags pages, only load the main admin css (upgrade banner, etc.) */
181 if ( ! is_null( $screen ) && in_array( $screen->base, array( 'edit-tags', 'edit-categories' ) ) ) {
182
183 wp_register_style(
184 'charitable-admin',
185 $assets_dir . 'css/charitable-admin' . $suffix . '.css',
186 array(),
187 $version
188 );
189
190 wp_enqueue_style( 'charitable-admin' );
191
192 wp_register_script(
193 'charitable-admin-notice',
194 $assets_dir . 'js/charitable-admin-notice' . $suffix . '.js',
195 array( 'jquery' ),
196 $version,
197 false
198 );
199
200 }
201
202
203 /* This check covers the category and tags pages, only load the main admin css (upgrade banner, etc.) */
204 if ( ! is_null( $screen ) && in_array( $screen->base, array( 'charitable_page_charitable-addons' ) ) ) {
205
206
207
208 }
209
210 if ( ! is_null( $screen ) && in_array( $screen->id, $this->get_charitable_screens() ) ) {
211
212 wp_register_style(
213 'charitable-admin',
214 $assets_dir . 'css/charitable-admin' . $suffix . '.css',
215 array(),
216 $version
217 );
218
219 wp_enqueue_style( 'charitable-admin' );
220
221 $dependencies = array( 'jquery-ui-datepicker', 'jquery-ui-tabs', 'jquery-ui-sortable' );
222 $localized_vars = array(
223 'suggested_amount_placeholder' => __( 'Amount', 'charitable' ),
224 'suggested_amount_description_placeholder' => __( 'Optional Description', 'charitable' ),
225 );
226
227 if ( 'donation' == $screen->id ) {
228 wp_register_script(
229 'accounting',
230 $assets_dir . 'js/libraries/accounting'. $suffix . '.js',
231 array( 'jquery' ),
232 $version,
233 true
234 );
235
236 $dependencies[] = 'accounting';
237 $localized_vars = array_merge(
238 $localized_vars,
239 array(
240 'currency_format_num_decimals' => esc_attr( charitable_get_currency_helper()->get_decimals() ),
241 'currency_format_decimal_sep' => esc_attr( charitable_get_currency_helper()->get_decimal_separator() ),
242 'currency_format_thousand_sep' => esc_attr( charitable_get_currency_helper()->get_thousands_separator() ),
243 'currency_format' => esc_attr( charitable_get_currency_helper()->get_accounting_js_format() ),
244 )
245 );
246 }
247
248 wp_register_script(
249 'charitable-admin',
250 $assets_dir . 'js/charitable-admin' . $suffix . '.js',
251 $dependencies,
252 $version,
253 false
254 );
255
256 wp_enqueue_script( 'charitable-admin' );
257
258 /**
259 * Filter the admin Javascript vars.
260 *
261 * @since 1.0.0
262 *
263 * @param array $localized_vars The vars.
264 */
265 $localized_vars = apply_filters( 'charitable_localized_javascript_vars', $localized_vars );
266
267 wp_localize_script( 'charitable-admin', 'CHARITABLE', $localized_vars );
268
269 // $strings = [
270 // 'nonce' => wp_create_nonce( 'charitable-admin' ),
271 // 'ajax_url' => admin_url( 'admin-ajax.php' ),
272 // 'davidtest' => 1,
273 // ];
274
275 // /**
276 // * Allow theme/plugin developers to adjust main strings on backend/admin part.
277 // *
278 // * @since 1.7.0.3
279 // *
280 // * @param array $strings Main admin localized strings.
281 // */
282 // $strings = (array) apply_filters( 'charitable_admin_strings', $strings );
283
284 // wp_localize_script(
285 // 'charitable-admin',
286 // 'charitable_admin',
287 // $strings
288 // );
289
290 } // end if
291
292 wp_register_script(
293 'charitable-admin-notice',
294 $assets_dir . 'js/charitable-admin-notice' . $suffix . '.js',
295 array( 'jquery' ),
296 $version,
297 false
298 );
299
300 wp_register_script(
301 'charitable-admin-media',
302 $assets_dir . 'js/charitable-admin-media' . $suffix . '.js',
303 array( 'jquery' ),
304 $version,
305 false
306 );
307
308 wp_register_script(
309 'lean-modal',
310 $assets_dir . 'js/libraries/leanModal' . $suffix . '.js',
311 array( 'jquery' ),
312 $version,
313 true
314 );
315
316 wp_register_style(
317 'lean-modal-css',
318 $assets_dir . 'css/modal' . $suffix . '.css',
319 array(),
320 $version
321 );
322
323 wp_register_script(
324 'charitable-admin-tables',
325 $assets_dir . 'js/charitable-admin-tables' . $suffix . '.js',
326 array( 'jquery', 'lean-modal' ),
327 $version,
328 true
329 );
330
331 wp_register_script(
332 'select2',
333 $assets_dir . 'js/libraries/select2' . $suffix . '.js',
334 array( 'jquery' ),
335 '4.0.12',
336 true
337 );
338
339 wp_register_style(
340 'select2-css',
341 $assets_dir . 'css/libraries/select2' . $suffix . '.css',
342 array(),
343 '4.0.12'
344 );
345
346 do_action( 'after_charitable_admin_enqueue_scripts', $suffix, $version, $assets_dir );
347 }
348
349 /**
350 * Set admin body classes.
351 *
352 * @since 1.5.0
353 *
354 * @param string $classes Existing list of classes.
355 * @return string
356 */
357 public function set_body_class( $classes ) {
358 $screen = get_current_screen();
359
360 if ( 'donation' == $screen->post_type && ( 'add' == $screen->action || isset( $_GET['show_form'] ) ) ) {
361 $classes .= ' charitable-admin-donation-form';
362 }
363
364 return $classes;
365 }
366
367 /**
368 * Add notices to the dashboard.
369 *
370 * @since 1.4.0
371 *
372 * @return void
373 */
374 public function add_notices() {
375 /* Get any version update notices first. */
376 $this->add_version_update_notices();
377
378 /* Render notices. */
379 charitable_get_admin_notices()->render();
380 }
381
382 /**
383 * Add version update notices to the dashboard.
384 *
385 * @since 1.4.6
386 *
387 * @return void
388 */
389 public function add_version_update_notices() {
390 if ( ! current_user_can( 'manage_options' ) ) {
391 return;
392 }
393
394 $notices = array(
395 /* translators: %s: link */
396 'release-150' => sprintf( __( "Charitable 1.5 is packed with new features and improvements. <a href='%s' target='_blank'>Find out what's new</a>.", 'charitable' ),
397 'https://wpcharitable.com/charitable-1-5-release-notes/?utm_source=WordPress&utm_campaign=WP+Charitable&utm_medium=Admin+Notice&utm_content=Version+One+Five+Whats+New'
398 ),
399 /* translators: %s: link */
400 'release-160' => sprintf( __( 'Charitable 1.6 introduces important new user privacy features and other improvements. <a href="%s" target="_blank">Find out what\'s new</a>.', 'charitable' ),
401 'https://www.wpcharitable.com/charitable-1-6-user-privacy-gdpr-better-refunds-and-a-new-shortcode/?utm_source=WordPress&utm_campaign=WP+Charitable&utm_medium=Admin+Notice&utm_content=Version+One+Six+Whats+New'
402 ),
403 );
404
405 $helper = charitable_get_admin_notices();
406
407 foreach ( $notices as $notice => $message ) {
408 if ( ! get_transient( 'charitable_' . $notice . '_notice' ) ) {
409 continue;
410 }
411
412 $helper->add_version_update( $message, $notice );
413 }
414 }
415
416 /**
417 * Dismiss a notice.
418 *
419 * @since 1.4.0
420 *
421 * @return void
422 */
423 public function dismiss_notice() {
424 if ( ! isset( $_POST['notice'] ) ) {
425 wp_send_json_error();
426 }
427
428 $ret = delete_transient( 'charitable_' . $_POST['notice'] . '_notice', true );
429
430 if ( ! $ret ) {
431 wp_send_json_error( $ret );
432 }
433
434 wp_send_json_success();
435 }
436
437 /**
438 * Dismiss a banner.
439 *
440 * @since 1.7.0
441 *
442 * @return void
443 */
444 public function dismiss_banner() {
445 if ( ! isset( $_POST['banner_id'] ) ) {
446 wp_send_json_error();
447 }
448
449 $ret = set_transient( 'charitable_' . $_POST['banner_id'] . '_banner', 1, WEEK_IN_SECONDS );
450
451 if ( ! $ret ) {
452 wp_send_json_error( $ret );
453 }
454
455 wp_send_json_success();
456 }
457
458
459 /**
460 * Dismiss a five star rating.
461 *
462 * @since 1.7.0
463 *
464 * @return void
465 */
466 public function dismiss_five_star_rating() {
467 if ( ! isset( $_POST['banner_id'] ) ) {
468 wp_send_json_error();
469 }
470
471 $check = get_transient( 'charitable_' . $_POST['banner_id'] . '_banner' );
472
473 if ( ! $check ) {
474 $ret = set_transient( 'charitable_' . $_POST['banner_id'] . '_banner', true );
475 if ( ! $ret ) {
476 wp_send_json_error( $ret );
477 }
478 }
479
480 wp_send_json_success();
481 }
482
483 /**
484 * Dismiss a five star rating.
485 *
486 * @since 1.7.0
487 *
488 * @return void
489 */
490 public function dismiss_lite_cta() {
491 if ( ! isset( $_POST['chartiable_action'] ) ) {
492 wp_send_json_error();
493 }
494
495 $updated = update_option( 'charitable_lite_settings_upgrade', true );
496
497 if ( $updated ) {
498 wp_send_json_success();
499 } else {
500 wp_send_json_error();
501 }
502
503 }
504
505 /**
506 * Adds one or more classes to the body tag in the dashboard.
507 *
508 * @since 1.0.0
509 *
510 * @param string $classes Current body classes.
511 * @return string Altered body classes.
512 */
513 public function add_admin_body_class( $classes ) {
514 $screen = get_current_screen();
515
516 if ( in_array( $screen->post_type, array( Charitable::DONATION_POST_TYPE, Charitable::CAMPAIGN_POST_TYPE ) ) ) {
517 $classes .= ' post-type-charitable';
518 }
519
520 return $classes;
521 }
522
523 /**
524 * Add custom links to the plugin actions.
525 *
526 * @since 1.0.0
527 *
528 * @param string[] $links Plugin action links.
529 * @return string[]
530 */
531 public function add_plugin_action_links( $links ) {
532 $links[] = '<a href="' . admin_url( 'admin.php?page=charitable-settings' ) . '">' . __( 'Settings', 'charitable' ) . '</a>';
533 return $links;
534 }
535
536 /**
537 * Add Extensions link to the plugin row meta.
538 *
539 * @since 1.2.0
540 *
541 * @param string[] $links Plugin action links.
542 * @param string $file The plugin file.
543 * @return string[] $links
544 */
545 public function add_plugin_row_meta( $links, $file ) {
546 if ( plugin_basename( charitable()->get_path() ) != $file ) {
547 return $links;
548 }
549
550 // Use an updated UTM.
551 $extensions_link = charitable_ga_url(
552 'https://wpcharitable.com/extensions/',
553 urlencode( 'Admin Plugin Page'),
554 urlencode( 'Extensions' )
555 );
556
557 $links[] = '<a href="' . $extensions_link . '" target="_blank" rel="noopener">' . __( 'Extensions', 'charitable' ) . '</a>';
558
559 return $links;
560 }
561
562 /**
563 * Remove the jQuery UI styles added by Ninja Forms.
564 *
565 * @since 1.2.0
566 *
567 * @param string $context Media buttons context.
568 * @return string
569 */
570 public function remove_jquery_ui_styles_nf( $context ) {
571 wp_dequeue_style( 'jquery-smoothness' );
572 return $context;
573 }
574
575 /**
576 * Export donations.
577 *
578 * @since 1.3.0
579 *
580 * @return false|void Returns false if the export failed. Exits otherwise.
581 */
582 public function export_donations() {
583
584 if ( ! wp_verify_nonce( $_GET['_charitable_export_nonce'], 'charitable_export_donations' ) ) {
585 return false;
586 }
587
588 /**
589 * Filter the donation export arguments.
590 *
591 * @since 1.3.0
592 *
593 * @param array $args Export arguments.
594 */
595 $export_args = apply_filters(
596 'charitable_donations_export_args',
597 array(
598 'start_date' => $_GET['start_date'],
599 'end_date' => $_GET['end_date'],
600 'status' => $_GET['post_status'],
601 'campaign_id' => $_GET['campaign_id'],
602 'report_type' => $_GET['report_type'],
603 )
604 );
605
606 /**
607 * Filter the export class name.
608 *
609 * @since 1.3.0
610 *
611 * @param string $report_type The type of report.
612 * @param array $args Export arguments.
613 */
614 $export_class = apply_filters( 'charitable_donations_export_class', 'Charitable_Export_Donations', $_GET['report_type'], $export_args );
615
616 new $export_class( $export_args );
617
618 die();
619 }
620
621 /**
622 * Export campaigns.
623 *
624 * @since 1.6.0
625 *
626 * @return false|void Returns false if the export failed. Exits otherwise.
627 */
628 public function export_campaigns() {
629 if ( ! wp_verify_nonce( $_GET['_charitable_export_nonce'], 'charitable_export_campaigns' ) ) {
630 return false;
631 }
632
633 /**
634 * Filter the campaign export arguments.
635 *
636 * @since 1.6.0
637 *
638 * @param array $args Export arguments.
639 */
640 $export_args = apply_filters(
641 'charitable_campaigns_export_args',
642 array(
643 'start_date_from' => $_GET['start_date_from'],
644 'start_date_to' => $_GET['start_date_to'],
645 'end_date_from' => $_GET['end_date_from'],
646 'end_date_to' => $_GET['end_date_to'],
647 'status' => $_GET['status'],
648 'report_type' => $_GET['report_type'],
649 )
650 );
651
652 /**
653 * Filter the export class name.
654 *
655 * @since 1.6.0
656 *
657 * @param string $report_type The type of report.
658 * @param array $args Export arguments.
659 */
660 $export_class = apply_filters( 'charitable_campaigns_export_class', 'Charitable_Export_Campaigns', $_GET['report_type'], $export_args );
661
662 new $export_class( $export_args );
663
664 die();
665 }
666
667 /**
668 * Returns an array of screen IDs where the Charitable scripts should be loaded.
669 *
670 * @uses charitable_admin_screens
671 *
672 * @since 1.0.0
673 *
674 * @return array
675 */
676 public function get_charitable_screens() {
677 /**
678 * Filter admin screens where Charitable styles & scripts should be loaded.
679 *
680 * @since 1.0.0
681 *
682 * @param string[] $screens List of screen ids.
683 */
684 return apply_filters( 'charitable_admin_screens', array(
685 'campaign',
686 'donation',
687 'charitable_page_charitable-settings',
688 'edit-campaign',
689 'edit-donation',
690 'dashboard',
691 'toplevel_page_charitable',
692 'charitable_page_charitable-addons',
693 ) );
694 }
695 }
696
697 endif;
698