| 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) 2022, Studio 164a |
| 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 |
/* 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' ), |
| 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' ), |
| 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' ), |
| 241 |
$version, |
| 242 |
false |
| 243 |
); |
| 244 |
|
| 245 |
wp_register_script( |
| 246 |
'lean-modal', |
| 247 |
$assets_dir . 'js/libraries/leanModal' . $suffix . '.js', |
| 248 |
array( 'jquery' ), |
| 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', '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' ), |
| 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( |
| 466 |
'charitable_donations_export_args', |
| 467 |
array( |
| 468 |
'start_date' => $_GET['start_date'], |
| 469 |
'end_date' => $_GET['end_date'], |
| 470 |
'status' => $_GET['post_status'], |
| 471 |
'campaign_id' => $_GET['campaign_id'], |
| 472 |
'report_type' => $_GET['report_type'], |
| 473 |
) |
| 474 |
); |
| 475 |
|
| 476 |
/** |
| 477 |
* Filter the export class name. |
| 478 |
* |
| 479 |
* @since 1.3.0 |
| 480 |
* |
| 481 |
* @param string $report_type The type of report. |
| 482 |
* @param array $args Export arguments. |
| 483 |
*/ |
| 484 |
$export_class = apply_filters( 'charitable_donations_export_class', 'Charitable_Export_Donations', $_GET['report_type'], $export_args ); |
| 485 |
|
| 486 |
new $export_class( $export_args ); |
| 487 |
|
| 488 |
die(); |
| 489 |
} |
| 490 |
|
| 491 |
/** |
| 492 |
* Export campaigns. |
| 493 |
* |
| 494 |
* @since 1.6.0 |
| 495 |
* |
| 496 |
* @return false|void Returns false if the export failed. Exits otherwise. |
| 497 |
*/ |
| 498 |
public function export_campaigns() { |
| 499 |
if ( ! wp_verify_nonce( $_GET['_charitable_export_nonce'], 'charitable_export_campaigns' ) ) { |
| 500 |
return false; |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
* Filter the campaign export arguments. |
| 505 |
* |
| 506 |
* @since 1.6.0 |
| 507 |
* |
| 508 |
* @param array $args Export arguments. |
| 509 |
*/ |
| 510 |
$export_args = apply_filters( |
| 511 |
'charitable_campaigns_export_args', |
| 512 |
array( |
| 513 |
'start_date_from' => $_GET['start_date_from'], |
| 514 |
'start_date_to' => $_GET['start_date_to'], |
| 515 |
'end_date_from' => $_GET['end_date_from'], |
| 516 |
'end_date_to' => $_GET['end_date_to'], |
| 517 |
'status' => $_GET['status'], |
| 518 |
'report_type' => $_GET['report_type'], |
| 519 |
) |
| 520 |
); |
| 521 |
|
| 522 |
/** |
| 523 |
* Filter the export class name. |
| 524 |
* |
| 525 |
* @since 1.6.0 |
| 526 |
* |
| 527 |
* @param string $report_type The type of report. |
| 528 |
* @param array $args Export arguments. |
| 529 |
*/ |
| 530 |
$export_class = apply_filters( 'charitable_campaigns_export_class', 'Charitable_Export_Campaigns', $_GET['report_type'], $export_args ); |
| 531 |
|
| 532 |
new $export_class( $export_args ); |
| 533 |
|
| 534 |
die(); |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
/** |
| 539 |
* Returns an array of screen IDs where the Charitable scripts should be loaded. |
| 540 |
* |
| 541 |
* @uses charitable_admin_screens |
| 542 |
* |
| 543 |
* @since 1.0.0 |
| 544 |
* |
| 545 |
* @return array |
| 546 |
*/ |
| 547 |
public function get_charitable_screens() { |
| 548 |
/** |
| 549 |
* Filter admin screens where Charitable styles & scripts should be loaded. |
| 550 |
* |
| 551 |
* @since 1.0.0 |
| 552 |
* |
| 553 |
* @param string[] $screens List of screen ids. |
| 554 |
*/ |
| 555 |
return apply_filters( 'charitable_admin_screens', array( |
| 556 |
'campaign', |
| 557 |
'donation', |
| 558 |
'charitable_page_charitable-settings', |
| 559 |
'edit-campaign', |
| 560 |
'edit-donation', |
| 561 |
'dashboard', |
| 562 |
) ); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
endif; |
| 567 |
|