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

559 lines 14.0 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 Eric Daams
7 * @copyright Copyright (c) 2020, Studio 164a
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.5.0
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 /* Admin page styles are registered but only enqueued when necessary. */
158 wp_register_style(
159 'charitable-admin-pages',
160 $assets_dir . 'css/charitable-admin-pages' . $suffix . '.css',
161 array(),
162 $version
163 );
164
165 /* The following styles are only loaded on Charitable screens. */
166 $screen = get_current_screen();
167
168 if ( ! is_null( $screen ) && in_array( $screen->id, $this->get_charitable_screens() ) ) {
169
170 wp_register_style(
171 'charitable-admin',
172 $assets_dir . 'css/charitable-admin' . $suffix . '.css',
173 array(),
174 $version
175 );
176
177 wp_enqueue_style( 'charitable-admin' );
178
179 $dependencies = array( 'jquery-ui-datepicker', 'jquery-ui-tabs', 'jquery-ui-sortable' );
180 $localized_vars = array(
181 'suggested_amount_placeholder' => __( 'Amount', 'charitable' ),
182 'suggested_amount_description_placeholder' => __( 'Optional Description', 'charitable' ),
183 );
184
185 if ( 'donation' == $screen->id ) {
186 wp_register_script(
187 'accounting',
188 $assets_dir . 'js/libraries/accounting'. $suffix . '.js',
189 array( 'jquery-core' ),
190 $version,
191 true
192 );
193
194 $dependencies[] = 'accounting';
195 $localized_vars = array_merge(
196 $localized_vars,
197 array(
198 'currency_format_num_decimals' => esc_attr( charitable_get_currency_helper()->get_decimals() ),
199 'currency_format_decimal_sep' => esc_attr( charitable_get_currency_helper()->get_decimal_separator() ),
200 'currency_format_thousand_sep' => esc_attr( charitable_get_currency_helper()->get_thousands_separator() ),
201 'currency_format' => esc_attr( charitable_get_currency_helper()->get_accounting_js_format() ),
202 )
203 );
204 }
205
206 wp_register_script(
207 'charitable-admin',
208 $assets_dir . 'js/charitable-admin' . $suffix . '.js',
209 $dependencies,
210 $version,
211 false
212 );
213
214 wp_enqueue_script( 'charitable-admin' );
215
216 /**
217 * Filter the admin Javascript vars.
218 *
219 * @since 1.0.0
220 *
221 * @param array $localized_vars The vars.
222 */
223 $localized_vars = apply_filters( 'charitable_localized_javascript_vars', $localized_vars );
224
225 wp_localize_script( 'charitable-admin', 'CHARITABLE', $localized_vars );
226
227 }//end if
228
229 wp_register_script(
230 'charitable-admin-notice',
231 $assets_dir . 'js/charitable-admin-notice' . $suffix . '.js',
232 array( 'jquery-core' ),
233 $version,
234 false
235 );
236
237 wp_register_script(
238 'charitable-admin-media',
239 $assets_dir . 'js/charitable-admin-media' . $suffix . '.js',
240 array( 'jquery-core' ),
241 $version,
242 false
243 );
244
245 wp_register_script(
246 'lean-modal',
247 $assets_dir . 'js/libraries/leanModal' . $suffix . '.js',
248 array( 'jquery-core' ),
249 $version,
250 true
251 );
252
253 wp_register_style(
254 'lean-modal-css',
255 $assets_dir . 'css/modal' . $suffix . '.css',
256 array(),
257 $version
258 );
259
260 wp_register_script(
261 'charitable-admin-tables',
262 $assets_dir . 'js/charitable-admin-tables' . $suffix . '.js',
263 array( 'jquery-core', 'lean-modal' ),
264 $version,
265 true
266 );
267
268 wp_register_script(
269 'select2',
270 $assets_dir . 'js/libraries/select2' . $suffix . '.js',
271 array( 'jquery-core' ),
272 '4.0.12',
273 true
274 );
275
276 wp_register_style(
277 'select2-css',
278 $assets_dir . 'css/libraries/select2' . $suffix . '.css',
279 array(),
280 '4.0.12'
281 );
282 }
283
284 /**
285 * Set admin body classes.
286 *
287 * @since 1.5.0
288 *
289 * @param string $classes Existing list of classes.
290 * @return string
291 */
292 public function set_body_class( $classes ) {
293 $screen = get_current_screen();
294
295 if ( 'donation' == $screen->post_type && ( 'add' == $screen->action || isset( $_GET['show_form'] ) ) ) {
296 $classes .= ' charitable-admin-donation-form';
297 }
298
299 return $classes;
300 }
301
302 /**
303 * Add notices to the dashboard.
304 *
305 * @since 1.4.0
306 *
307 * @return void
308 */
309 public function add_notices() {
310 /* Get any version update notices first. */
311 $this->add_version_update_notices();
312
313 /* Render notices. */
314 charitable_get_admin_notices()->render();
315 }
316
317 /**
318 * Add version update notices to the dashboard.
319 *
320 * @since 1.4.6
321 *
322 * @return void
323 */
324 public function add_version_update_notices() {
325 if ( ! current_user_can( 'manage_options' ) ) {
326 return;
327 }
328
329 $notices = array(
330 /* translators: %s: link */
331 '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' ),
332 'https://www.wpcharitable.com/charitable-1-5-release-notes/?utm_source=notice&utm_medium=wordpress-dashboard&utm_campaign=release-notes&utm_content=release-150'
333 ),
334 /* translators: %s: link */
335 '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' ),
336 'https://www.wpcharitable.com/charitable-1-6-user-privacy-gdpr-better-refunds-and-a-new-shortcode/?utm_source=notice&utm_medium=wordpress-dashboard&utm_campaign=release-notes&utm_content=release-160'
337 ),
338 );
339
340 $helper = charitable_get_admin_notices();
341
342 foreach ( $notices as $notice => $message ) {
343 if ( ! get_transient( 'charitable_' . $notice . '_notice' ) ) {
344 continue;
345 }
346
347 $helper->add_version_update( $message, $notice );
348 }
349 }
350
351 /**
352 * Dismiss a notice.
353 *
354 * @since 1.4.0
355 *
356 * @return void
357 */
358 public function dismiss_notice() {
359 if ( ! isset( $_POST['notice'] ) ) {
360 wp_send_json_error();
361 }
362
363 $ret = delete_transient( 'charitable_' . $_POST['notice'] . '_notice', true );
364
365 if ( ! $ret ) {
366 wp_send_json_error( $ret );
367 }
368
369 wp_send_json_success();
370 }
371
372 /**
373 * Adds one or more classes to the body tag in the dashboard.
374 *
375 * @since 1.0.0
376 *
377 * @param string $classes Current body classes.
378 * @return string Altered body classes.
379 */
380 public function add_admin_body_class( $classes ) {
381 $screen = get_current_screen();
382
383 if ( in_array( $screen->post_type, array( Charitable::DONATION_POST_TYPE, Charitable::CAMPAIGN_POST_TYPE ) ) ) {
384 $classes .= ' post-type-charitable';
385 }
386
387 return $classes;
388 }
389
390 /**
391 * Add custom links to the plugin actions.
392 *
393 * @since 1.0.0
394 *
395 * @param string[] $links Plugin action links.
396 * @return string[]
397 */
398 public function add_plugin_action_links( $links ) {
399 $links[] = '<a href="' . admin_url( 'admin.php?page=charitable-settings' ) . '">' . __( 'Settings', 'charitable' ) . '</a>';
400 return $links;
401 }
402
403 /**
404 * Add Extensions link to the plugin row meta.
405 *
406 * @since 1.2.0
407 *
408 * @param string[] $links Plugin action links.
409 * @param string $file The plugin file.
410 * @return string[] $links
411 */
412 public function add_plugin_row_meta( $links, $file ) {
413 if ( plugin_basename( charitable()->get_path() ) != $file ) {
414 return $links;
415 }
416
417 $extensions_link = esc_url(
418 add_query_arg(
419 array(
420 'utm_source' => 'plugins-page',
421 'utm_medium' => 'plugin-row',
422 'utm_campaign' => 'admin',
423 ),
424 'https://wpcharitable.com/extensions/'
425 )
426 );
427
428 $links[] = '<a href="' . $extensions_link . '">' . __( 'Extensions', 'charitable' ) . '</a>';
429
430 return $links;
431 }
432
433 /**
434 * Remove the jQuery UI styles added by Ninja Forms.
435 *
436 * @since 1.2.0
437 *
438 * @param string $context Media buttons context.
439 * @return string
440 */
441 public function remove_jquery_ui_styles_nf( $context ) {
442 wp_dequeue_style( 'jquery-smoothness' );
443 return $context;
444 }
445
446 /**
447 * Export donations.
448 *
449 * @since 1.3.0
450 *
451 * @return false|void Returns false if the export failed. Exits otherwise.
452 */
453 public function export_donations() {
454 if ( ! wp_verify_nonce( $_GET['_charitable_export_nonce'], 'charitable_export_donations' ) ) {
455 return false;
456 }
457
458 /**
459 * Filter the donation export arguments.
460 *
461 * @since 1.3.0
462 *
463 * @param array $args Export arguments.
464 */
465 $export_args = apply_filters( 'charitable_donations_export_args', array(
466 'start_date' => $_GET['start_date'],
467 'end_date' => $_GET['end_date'],
468 'status' => $_GET['post_status'],
469 'campaign_id' => $_GET['campaign_id'],
470 'report_type' => $_GET['report_type'],
471 ) );
472
473 /**
474 * Filter the export class name.
475 *
476 * @since 1.3.0
477 *
478 * @param string $report_type The type of report.
479 * @param array $args Export arguments.
480 */
481 $export_class = apply_filters( 'charitable_donations_export_class', 'Charitable_Export_Donations', $_GET['report_type'], $export_args );
482
483 new $export_class( $export_args );
484
485 die();
486 }
487
488 /**
489 * Export campaigns.
490 *
491 * @since 1.6.0
492 *
493 * @return false|void Returns false if the export failed. Exits otherwise.
494 */
495 public function export_campaigns() {
496 if ( ! wp_verify_nonce( $_GET['_charitable_export_nonce'], 'charitable_export_campaigns' ) ) {
497 return false;
498 }
499
500 /**
501 * Filter the donation export arguments.
502 *
503 * @since 1.6.0
504 *
505 * @param array $args Export arguments.
506 */
507 $export_args = apply_filters( 'charitable_campaigns_export_args', array(
508 'start_date' => $_GET['start_date'],
509 'end_date' => $_GET['end_date'],
510 'status' => $_GET['status'],
511 'report_type' => $_GET['report_type'],
512 ) );
513
514 /**
515 * Filter the export class name.
516 *
517 * @since 1.6.0
518 *
519 * @param string $report_type The type of report.
520 * @param array $args Export arguments.
521 */
522 $export_class = apply_filters( 'charitable_campaigns_export_class', 'Charitable_Export_Campaigns', $_GET['report_type'], $export_args );
523
524 new $export_class( $export_args );
525
526 die();
527 }
528
529
530 /**
531 * Returns an array of screen IDs where the Charitable scripts should be loaded.
532 *
533 * @uses charitable_admin_screens
534 *
535 * @since 1.0.0
536 *
537 * @return array
538 */
539 public function get_charitable_screens() {
540 /**
541 * Filter admin screens where Charitable styles & scripts should be loaded.
542 *
543 * @since 1.0.0
544 *
545 * @param string[] $screens List of screen ids.
546 */
547 return apply_filters( 'charitable_admin_screens', array(
548 'campaign',
549 'donation',
550 'charitable_page_charitable-settings',
551 'edit-campaign',
552 'edit-donation',
553 'dashboard',
554 ) );
555 }
556 }
557
558 endif;
559