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

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