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

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