PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0
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 1.8.0 All 210 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, at includes/admin/class-charitable-admin.php

643 lines 15.8 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 }//end if
260
261 wp_register_script(
262 'charitable-admin-notice',
263 $assets_dir . 'js/charitable-admin-notice' . $suffix . '.js',
264 array( 'jquery' ),
265 $version,
266 false
267 );
268
269 wp_register_script(
270 'charitable-admin-media',
271 $assets_dir . 'js/charitable-admin-media' . $suffix . '.js',
272 array( 'jquery' ),
273 $version,
274 false
275 );
276
277 wp_register_script(
278 'lean-modal',
279 $assets_dir . 'js/libraries/leanModal' . $suffix . '.js',
280 array( 'jquery' ),
281 $version,
282 true
283 );
284
285 wp_register_style(
286 'lean-modal-css',
287 $assets_dir . 'css/modal' . $suffix . '.css',
288 array(),
289 $version
290 );
291
292 wp_register_script(
293 'charitable-admin-tables',
294 $assets_dir . 'js/charitable-admin-tables' . $suffix . '.js',
295 array( 'jquery', 'lean-modal' ),
296 $version,
297 true
298 );
299
300 wp_register_script(
301 'select2',
302 $assets_dir . 'js/libraries/select2' . $suffix . '.js',
303 array( 'jquery' ),
304 '4.0.12',
305 true
306 );
307
308 wp_register_style(
309 'select2-css',
310 $assets_dir . 'css/libraries/select2' . $suffix . '.css',
311 array(),
312 '4.0.12'
313 );
314 }
315
316 /**
317 * Set admin body classes.
318 *
319 * @since 1.5.0
320 *
321 * @param string $classes Existing list of classes.
322 * @return string
323 */
324 public function set_body_class( $classes ) {
325 $screen = get_current_screen();
326
327 if ( 'donation' == $screen->post_type && ( 'add' == $screen->action || isset( $_GET['show_form'] ) ) ) {
328 $classes .= ' charitable-admin-donation-form';
329 }
330
331 return $classes;
332 }
333
334 /**
335 * Add notices to the dashboard.
336 *
337 * @since 1.4.0
338 *
339 * @return void
340 */
341 public function add_notices() {
342 /* Get any version update notices first. */
343 $this->add_version_update_notices();
344
345 /* Render notices. */
346 charitable_get_admin_notices()->render();
347 }
348
349 /**
350 * Add version update notices to the dashboard.
351 *
352 * @since 1.4.6
353 *
354 * @return void
355 */
356 public function add_version_update_notices() {
357 if ( ! current_user_can( 'manage_options' ) ) {
358 return;
359 }
360
361 $notices = array(
362 /* translators: %s: link */
363 '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' ),
364 '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'
365 ),
366 /* translators: %s: link */
367 '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' ),
368 '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'
369 ),
370 );
371
372 $helper = charitable_get_admin_notices();
373
374 foreach ( $notices as $notice => $message ) {
375 if ( ! get_transient( 'charitable_' . $notice . '_notice' ) ) {
376 continue;
377 }
378
379 $helper->add_version_update( $message, $notice );
380 }
381 }
382
383 /**
384 * Dismiss a notice.
385 *
386 * @since 1.4.0
387 *
388 * @return void
389 */
390 public function dismiss_notice() {
391 if ( ! isset( $_POST['notice'] ) ) {
392 wp_send_json_error();
393 }
394
395 $ret = delete_transient( 'charitable_' . $_POST['notice'] . '_notice', true );
396
397 if ( ! $ret ) {
398 wp_send_json_error( $ret );
399 }
400
401 wp_send_json_success();
402 }
403
404 /**
405 * Dismiss a banner.
406 *
407 * @since 1.7.0
408 *
409 * @return void
410 */
411 public function dismiss_banner() {
412 if ( ! isset( $_POST['banner_id'] ) ) {
413 wp_send_json_error();
414 }
415
416 $ret = set_transient( 'charitable_' . $_POST['banner_id'] . '_banner', 1, WEEK_IN_SECONDS );
417
418 if ( ! $ret ) {
419 wp_send_json_error( $ret );
420 }
421
422 wp_send_json_success();
423 }
424
425
426 /**
427 * Dismiss a five star rating.
428 *
429 * @since 1.7.0
430 *
431 * @return void
432 */
433 public function dismiss_five_star_rating() {
434 if ( ! isset( $_POST['banner_id'] ) ) {
435 wp_send_json_error();
436 }
437
438 $check = get_transient( 'charitable_' . $_POST['banner_id'] . '_banner' );
439
440 if ( ! $check ) {
441 $ret = set_transient( 'charitable_' . $_POST['banner_id'] . '_banner', true );
442 if ( ! $ret ) {
443 wp_send_json_error( $ret );
444 }
445 }
446
447 wp_send_json_success();
448 }
449
450
451
452 /**
453 * Adds one or more classes to the body tag in the dashboard.
454 *
455 * @since 1.0.0
456 *
457 * @param string $classes Current body classes.
458 * @return string Altered body classes.
459 */
460 public function add_admin_body_class( $classes ) {
461 $screen = get_current_screen();
462
463 if ( in_array( $screen->post_type, array( Charitable::DONATION_POST_TYPE, Charitable::CAMPAIGN_POST_TYPE ) ) ) {
464 $classes .= ' post-type-charitable';
465 }
466
467 return $classes;
468 }
469
470 /**
471 * Add custom links to the plugin actions.
472 *
473 * @since 1.0.0
474 *
475 * @param string[] $links Plugin action links.
476 * @return string[]
477 */
478 public function add_plugin_action_links( $links ) {
479 $links[] = '<a href="' . admin_url( 'admin.php?page=charitable-settings' ) . '">' . __( 'Settings', 'charitable' ) . '</a>';
480 return $links;
481 }
482
483 /**
484 * Add Extensions link to the plugin row meta.
485 *
486 * @since 1.2.0
487 *
488 * @param string[] $links Plugin action links.
489 * @param string $file The plugin file.
490 * @return string[] $links
491 */
492 public function add_plugin_row_meta( $links, $file ) {
493 if ( plugin_basename( charitable()->get_path() ) != $file ) {
494 return $links;
495 }
496
497 // Use an updated UTM.
498 $extensions_link = charitable_ga_url(
499 'https://wpcharitable.com/extensions/',
500 urlencode( 'Admin Plugin Page'),
501 urlencode( 'Extensions' )
502 );
503
504 $links[] = '<a href="' . $extensions_link . '" target="_blank" rel="noopener">' . __( 'Extensions', 'charitable' ) . '</a>';
505
506 return $links;
507 }
508
509 /**
510 * Remove the jQuery UI styles added by Ninja Forms.
511 *
512 * @since 1.2.0
513 *
514 * @param string $context Media buttons context.
515 * @return string
516 */
517 public function remove_jquery_ui_styles_nf( $context ) {
518 wp_dequeue_style( 'jquery-smoothness' );
519 return $context;
520 }
521
522 /**
523 * Export donations.
524 *
525 * @since 1.3.0
526 *
527 * @return false|void Returns false if the export failed. Exits otherwise.
528 */
529 public function export_donations() {
530 if ( ! wp_verify_nonce( $_GET['_charitable_export_nonce'], 'charitable_export_donations' ) ) {
531 return false;
532 }
533
534 /**
535 * Filter the donation export arguments.
536 *
537 * @since 1.3.0
538 *
539 * @param array $args Export arguments.
540 */
541 $export_args = apply_filters(
542 'charitable_donations_export_args',
543 array(
544 'start_date' => $_GET['start_date'],
545 'end_date' => $_GET['end_date'],
546 'status' => $_GET['post_status'],
547 'campaign_id' => $_GET['campaign_id'],
548 'report_type' => $_GET['report_type'],
549 )
550 );
551
552 /**
553 * Filter the export class name.
554 *
555 * @since 1.3.0
556 *
557 * @param string $report_type The type of report.
558 * @param array $args Export arguments.
559 */
560 $export_class = apply_filters( 'charitable_donations_export_class', 'Charitable_Export_Donations', $_GET['report_type'], $export_args );
561
562 new $export_class( $export_args );
563
564 die();
565 }
566
567 /**
568 * Export campaigns.
569 *
570 * @since 1.6.0
571 *
572 * @return false|void Returns false if the export failed. Exits otherwise.
573 */
574 public function export_campaigns() {
575 if ( ! wp_verify_nonce( $_GET['_charitable_export_nonce'], 'charitable_export_campaigns' ) ) {
576 return false;
577 }
578
579 /**
580 * Filter the campaign export arguments.
581 *
582 * @since 1.6.0
583 *
584 * @param array $args Export arguments.
585 */
586 $export_args = apply_filters(
587 'charitable_campaigns_export_args',
588 array(
589 'start_date_from' => $_GET['start_date_from'],
590 'start_date_to' => $_GET['start_date_to'],
591 'end_date_from' => $_GET['end_date_from'],
592 'end_date_to' => $_GET['end_date_to'],
593 'status' => $_GET['status'],
594 'report_type' => $_GET['report_type'],
595 )
596 );
597
598 /**
599 * Filter the export class name.
600 *
601 * @since 1.6.0
602 *
603 * @param string $report_type The type of report.
604 * @param array $args Export arguments.
605 */
606 $export_class = apply_filters( 'charitable_campaigns_export_class', 'Charitable_Export_Campaigns', $_GET['report_type'], $export_args );
607
608 new $export_class( $export_args );
609
610 die();
611 }
612
613 /**
614 * Returns an array of screen IDs where the Charitable scripts should be loaded.
615 *
616 * @uses charitable_admin_screens
617 *
618 * @since 1.0.0
619 *
620 * @return array
621 */
622 public function get_charitable_screens() {
623 /**
624 * Filter admin screens where Charitable styles & scripts should be loaded.
625 *
626 * @since 1.0.0
627 *
628 * @param string[] $screens List of screen ids.
629 */
630 return apply_filters( 'charitable_admin_screens', array(
631 'campaign',
632 'donation',
633 'charitable_page_charitable-settings',
634 'edit-campaign',
635 'edit-donation',
636 'dashboard',
637 'toplevel_page_charitable'
638 ) );
639 }
640 }
641
642 endif;
643