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

557 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) 2019, 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' ) ) { exit; }
15
16 if ( ! class_exists( 'Charitable_Admin' ) ) :
17
18 /**
19 * Charitable_Admin
20 *
21 * @final
22 * @since 1.0.0
23 */
24 final class Charitable_Admin {
25
26 /**
27 * The single instance of this class.
28 *
29 * @var Charitable_Admin|null
30 */
31 private static $instance = null;
32
33 /**
34 * Donation actions class.
35 *
36 * @var Charitable_Donation_Admin_Actions
37 */
38 private $donation_actions;
39
40 /**
41 * Set up the class.
42 *
43 * Note that the only way to instantiate an object is with the charitable_start method,
44 * which can only be called during the start phase. In other words, don't try
45 * to instantiate this object.
46 *
47 * @since 1.0.0
48 */
49 protected function __construct() {
50 $this->load_dependencies();
51
52 $this->donation_actions = new Charitable_Donation_Admin_Actions;
53
54 do_action( 'charitable_admin_loaded' );
55 }
56
57 /**
58 * Returns and/or create the single instance of this class.
59 *
60 * @since 1.2.0
61 *
62 * @return Charitable_Admin
63 */
64 public static function get_instance() {
65 if ( is_null( self::$instance ) ) {
66 self::$instance = new self();
67 }
68
69 return self::$instance;
70 }
71
72 /**
73 * Include admin-only files.
74 *
75 * @since 1.0.0
76 *
77 * @return void
78 */
79 private function load_dependencies() {
80 $admin_dir = charitable()->get_path( 'includes' ) . 'admin/';
81
82 require_once( $admin_dir . 'charitable-core-admin-functions.php' );
83 require_once( $admin_dir . 'campaigns/charitable-admin-campaign-hooks.php' );
84 require_once( $admin_dir . 'dashboard-widgets/charitable-dashboard-widgets-hooks.php' );
85 require_once( $admin_dir . 'donations/charitable-admin-donation-hooks.php' );
86 require_once( $admin_dir . 'settings/charitable-settings-admin-hooks.php' );
87 }
88
89 /**
90 * Get Charitable_Donation_Admin_Actions class.
91 *
92 * @since 1.5.0
93 *
94 * @return Charitable_Donation_Admin_Actions
95 */
96 public function get_donation_actions() {
97 return $this->donation_actions;
98 }
99
100 /**
101 * Do an admin action.
102 *
103 * @since 1.5.0
104 *
105 * @return boolean|WP_Error WP_Error in case of error. Mixed results if the action was performed.
106 */
107 public function maybe_do_admin_action() {
108 if ( ! array_key_exists( 'charitable_admin_action', $_GET ) ) {
109 return false;
110 }
111
112 if ( count( array_diff( array( 'action_type', '_nonce', 'object_id' ), array_keys( $_GET ) ) ) ) {
113 return new WP_Error( __( 'Action could not be executed.', 'charitable' ) );
114 }
115
116 if ( ! wp_verify_nonce( $_GET['_nonce'], 'donation_action' ) ) {
117 return new WP_Error( __( 'Action could not be executed. Nonce check failed.', 'charitable' ) );
118 }
119
120 if ( 'donation' != $_GET['action_type'] ) {
121 return new WP_Error( __( 'Action from an unknown action type executed.', 'charitable' ) );
122 }
123
124 return $this->donation_actions->do_action( $_GET['charitable_admin_action'], $_GET['object_id'] );
125 }
126
127 /**
128 * Loads admin-only scripts and stylesheets.
129 *
130 * @since 1.0.0
131 *
132 * @return void
133 */
134 public function admin_enqueue_scripts() {
135 if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
136 $suffix = '';
137 $version = '';
138 } else {
139 $suffix = '.min';
140 $version = charitable()->get_version();
141 }
142
143 $assets_dir = charitable()->get_path( 'assets', false );
144
145 /* Menu styles are loaded everywhere in the WordPress dashboard. */
146 wp_register_style(
147 'charitable-admin-menu',
148 $assets_dir . 'css/charitable-admin-menu' . $suffix . '.css',
149 array(),
150 $version
151 );
152
153 wp_enqueue_style( 'charitable-admin-menu' );
154
155 /* Admin page styles are registered but only enqueued when necessary. */
156 wp_register_style(
157 'charitable-admin-pages',
158 $assets_dir . 'css/charitable-admin-pages' . $suffix . '.css',
159 array(),
160 $version
161 );
162
163 /* The following styles are only loaded on Charitable screens. */
164 $screen = get_current_screen();
165
166 if ( ! is_null( $screen ) && in_array( $screen->id, $this->get_charitable_screens() ) ) {
167
168 wp_register_style(
169 'charitable-admin',
170 $assets_dir . 'css/charitable-admin' . $suffix . '.css',
171 array(),
172 $version
173 );
174
175 wp_enqueue_style( 'charitable-admin' );
176
177 $dependencies = array( 'jquery-ui-datepicker', 'jquery-ui-tabs', 'jquery-ui-sortable' );
178 $localized_vars = array(
179 'suggested_amount_placeholder' => __( 'Amount', 'charitable' ),
180 'suggested_amount_description_placeholder' => __( 'Optional Description', 'charitable' ),
181 );
182
183 if ( 'donation' == $screen->id ) {
184 wp_register_script(
185 'accounting',
186 $assets_dir . 'js/libraries/accounting'. $suffix . '.js',
187 array( 'jquery-core' ),
188 $version,
189 true
190 );
191
192 $dependencies[] = 'accounting';
193 $localized_vars = array_merge(
194 $localized_vars,
195 array(
196 'currency_format_num_decimals' => esc_attr( charitable_get_currency_helper()->get_decimals() ),
197 'currency_format_decimal_sep' => esc_attr( charitable_get_currency_helper()->get_decimal_separator() ),
198 'currency_format_thousand_sep' => esc_attr( charitable_get_currency_helper()->get_thousands_separator() ),
199 'currency_format' => esc_attr( charitable_get_currency_helper()->get_accounting_js_format() ),
200 )
201 );
202 }
203
204 wp_register_script(
205 'charitable-admin',
206 $assets_dir . 'js/charitable-admin' . $suffix . '.js',
207 $dependencies,
208 $version,
209 false
210 );
211
212 wp_enqueue_script( 'charitable-admin' );
213
214 /**
215 * Filter the admin Javascript vars.
216 *
217 * @since 1.0.0
218 *
219 * @param array $localized_vars The vars.
220 */
221 $localized_vars = apply_filters( 'charitable_localized_javascript_vars', $localized_vars );
222
223 wp_localize_script( 'charitable-admin', 'CHARITABLE', $localized_vars );
224
225 }//end if
226
227 wp_register_script(
228 'charitable-admin-notice',
229 $assets_dir . 'js/charitable-admin-notice' . $suffix . '.js',
230 array( 'jquery-core' ),
231 $version,
232 false
233 );
234
235 wp_register_script(
236 'charitable-admin-media',
237 $assets_dir . 'js/charitable-admin-media' . $suffix . '.js',
238 array( 'jquery-core' ),
239 $version,
240 false
241 );
242
243 wp_register_script(
244 'lean-modal',
245 $assets_dir . 'js/libraries/leanModal' . $suffix . '.js',
246 array( 'jquery-core' ),
247 $version,
248 true
249 );
250
251 wp_register_style(
252 'lean-modal-css',
253 $assets_dir . 'css/modal' . $suffix . '.css',
254 array(),
255 $version
256 );
257
258 wp_register_script(
259 'charitable-admin-tables',
260 $assets_dir . 'js/charitable-admin-tables' . $suffix . '.js',
261 array( 'jquery-core', 'lean-modal' ),
262 $version,
263 true
264 );
265
266 wp_register_script(
267 'select2',
268 $assets_dir . 'js/libraries/select2' . $suffix . '.js',
269 array( 'jquery-core' ),
270 '4.0.12',
271 true
272 );
273
274 wp_register_style(
275 'select2-css',
276 $assets_dir . 'css/libraries/select2' . $suffix . '.css',
277 array(),
278 '4.0.12'
279 );
280 }
281
282 /**
283 * Set admin body classes.
284 *
285 * @since 1.5.0
286 *
287 * @param string $classes Existing list of classes.
288 * @return string
289 */
290 public function set_body_class( $classes ) {
291 $screen = get_current_screen();
292
293 if ( 'donation' == $screen->post_type && ( 'add' == $screen->action || isset( $_GET['show_form'] ) ) ) {
294 $classes .= ' charitable-admin-donation-form';
295 }
296
297 return $classes;
298 }
299
300 /**
301 * Add notices to the dashboard.
302 *
303 * @since 1.4.0
304 *
305 * @return void
306 */
307 public function add_notices() {
308 /* Get any version update notices first. */
309 $this->add_version_update_notices();
310
311 /* Render notices. */
312 charitable_get_admin_notices()->render();
313 }
314
315 /**
316 * Add version update notices to the dashboard.
317 *
318 * @since 1.4.6
319 *
320 * @return void
321 */
322 public function add_version_update_notices() {
323 if ( ! current_user_can( 'manage_options' ) ) {
324 return;
325 }
326
327 $notices = array(
328 /* translators: %s: link */
329 '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' ),
330 'https://www.wpcharitable.com/charitable-1-5-release-notes/?utm_source=notice&utm_medium=wordpress-dashboard&utm_campaign=release-notes&utm_content=release-150'
331 ),
332 /* translators: %s: link */
333 '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' ),
334 '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'
335 ),
336 );
337
338 $helper = charitable_get_admin_notices();
339
340 foreach ( $notices as $notice => $message ) {
341 if ( ! get_transient( 'charitable_' . $notice . '_notice' ) ) {
342 continue;
343 }
344
345 $helper->add_version_update( $message, $notice );
346 }
347 }
348
349 /**
350 * Dismiss a notice.
351 *
352 * @since 1.4.0
353 *
354 * @return void
355 */
356 public function dismiss_notice() {
357 if ( ! isset( $_POST['notice'] ) ) {
358 wp_send_json_error();
359 }
360
361 $ret = delete_transient( 'charitable_' . $_POST['notice'] . '_notice', true );
362
363 if ( ! $ret ) {
364 wp_send_json_error( $ret );
365 }
366
367 wp_send_json_success();
368 }
369
370 /**
371 * Adds one or more classes to the body tag in the dashboard.
372 *
373 * @since 1.0.0
374 *
375 * @param string $classes Current body classes.
376 * @return string Altered body classes.
377 */
378 public function add_admin_body_class( $classes ) {
379 $screen = get_current_screen();
380
381 if ( in_array( $screen->post_type, array( Charitable::DONATION_POST_TYPE, Charitable::CAMPAIGN_POST_TYPE ) ) ) {
382 $classes .= ' post-type-charitable';
383 }
384
385 return $classes;
386 }
387
388 /**
389 * Add custom links to the plugin actions.
390 *
391 * @since 1.0.0
392 *
393 * @param string[] $links Plugin action links.
394 * @return string[]
395 */
396 public function add_plugin_action_links( $links ) {
397 $links[] = '<a href="' . admin_url( 'admin.php?page=charitable-settings' ) . '">' . __( 'Settings', 'charitable' ) . '</a>';
398 return $links;
399 }
400
401 /**
402 * Add Extensions link to the plugin row meta.
403 *
404 * @since 1.2.0
405 *
406 * @param string[] $links Plugin action links.
407 * @param string $file The plugin file.
408 * @return string[] $links
409 */
410 public function add_plugin_row_meta( $links, $file ) {
411 if ( plugin_basename( charitable()->get_path() ) != $file ) {
412 return $links;
413 }
414
415 $extensions_link = esc_url(
416 add_query_arg(
417 array(
418 'utm_source' => 'plugins-page',
419 'utm_medium' => 'plugin-row',
420 'utm_campaign' => 'admin',
421 ),
422 'https://wpcharitable.com/extensions/'
423 )
424 );
425
426 $links[] = '<a href="' . $extensions_link . '">' . __( 'Extensions', 'charitable' ) . '</a>';
427
428 return $links;
429 }
430
431 /**
432 * Remove the jQuery UI styles added by Ninja Forms.
433 *
434 * @since 1.2.0
435 *
436 * @param string $context Media buttons context.
437 * @return string
438 */
439 public function remove_jquery_ui_styles_nf( $context ) {
440 wp_dequeue_style( 'jquery-smoothness' );
441 return $context;
442 }
443
444 /**
445 * Export donations.
446 *
447 * @since 1.3.0
448 *
449 * @return false|void Returns false if the export failed. Exits otherwise.
450 */
451 public function export_donations() {
452 if ( ! wp_verify_nonce( $_GET['_charitable_export_nonce'], 'charitable_export_donations' ) ) {
453 return false;
454 }
455
456 /**
457 * Filter the donation export arguments.
458 *
459 * @since 1.3.0
460 *
461 * @param array $args Export arguments.
462 */
463 $export_args = apply_filters( 'charitable_donations_export_args', array(
464 'start_date' => $_GET['start_date'],
465 'end_date' => $_GET['end_date'],
466 'status' => $_GET['post_status'],
467 'campaign_id' => $_GET['campaign_id'],
468 'report_type' => $_GET['report_type'],
469 ) );
470
471 /**
472 * Filter the export class name.
473 *
474 * @since 1.3.0
475 *
476 * @param string $report_type The type of report.
477 * @param array $args Export arguments.
478 */
479 $export_class = apply_filters( 'charitable_donations_export_class', 'Charitable_Export_Donations', $_GET['report_type'], $export_args );
480
481 new $export_class( $export_args );
482
483 die();
484 }
485
486 /**
487 * Export campaigns.
488 *
489 * @since 1.6.0
490 *
491 * @return false|void Returns false if the export failed. Exits otherwise.
492 */
493 public function export_campaigns() {
494 if ( ! wp_verify_nonce( $_GET['_charitable_export_nonce'], 'charitable_export_campaigns' ) ) {
495 return false;
496 }
497
498 /**
499 * Filter the donation export arguments.
500 *
501 * @since 1.6.0
502 *
503 * @param array $args Export arguments.
504 */
505 $export_args = apply_filters( 'charitable_campaigns_export_args', array(
506 'start_date' => $_GET['start_date'],
507 'end_date' => $_GET['end_date'],
508 'status' => $_GET['status'],
509 'report_type' => $_GET['report_type'],
510 ) );
511
512 /**
513 * Filter the export class name.
514 *
515 * @since 1.6.0
516 *
517 * @param string $report_type The type of report.
518 * @param array $args Export arguments.
519 */
520 $export_class = apply_filters( 'charitable_campaigns_export_class', 'Charitable_Export_Campaigns', $_GET['report_type'], $export_args );
521
522 new $export_class( $export_args );
523
524 die();
525 }
526
527
528 /**
529 * Returns an array of screen IDs where the Charitable scripts should be loaded.
530 *
531 * @uses charitable_admin_screens
532 *
533 * @since 1.0.0
534 *
535 * @return array
536 */
537 public function get_charitable_screens() {
538 /**
539 * Filter admin screens where Charitable styles & scripts should be loaded.
540 *
541 * @since 1.0.0
542 *
543 * @param string[] $screens List of screen ids.
544 */
545 return apply_filters( 'charitable_admin_screens', array(
546 'campaign',
547 'donation',
548 'charitable_page_charitable-settings',
549 'edit-campaign',
550 'edit-donation',
551 'dashboard',
552 ) );
553 }
554 }
555
556 endif;
557