PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.12.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.12.2
1.8.13.1 1.8.13 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 All 212 releases
charitable / includes / admin / notifications / class-charitable-notification-settings.php

class-charitable-notification-settings.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.12.2, at includes/admin/notifications/class-charitable-notification-settings.php

103 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Notification Settings.
4 *
5 * Manages site-wide category toggle settings for the notifications sidebar.
6 *
7 * @package Charitable/Classes/Charitable_Notification_Settings
8 * @author David Bisset
9 * @copyright Copyright (c) 2024, WP Charitable LLC
10 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11 * @since 1.8.12
12 * @version 1.8.12
13 */
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 class Charitable_Notification_Settings {
20
21 private static $instance = null;
22
23 /**
24 * All known categories with their default enabled state.
25 */
26 const DEFAULTS = [
27 'license' => true,
28 'lifecycle' => true,
29 'milestone' => true,
30 'health' => true,
31 'legal' => true,
32 'nudge' => true,
33 'new_feed' => true,
34 'legacy_feed' => true,
35 ];
36
37 public static function get_instance() {
38 if ( is_null( self::$instance ) ) {
39 self::$instance = new self();
40 }
41 return self::$instance;
42 }
43
44 public function init() {
45 add_action( 'wp_ajax_charitable_save_notification_settings', [ $this, 'ajax_save' ] );
46 }
47
48 /**
49 * Check whether a category is enabled site-wide.
50 *
51 * @param string $category One of the keys in self::DEFAULTS.
52 * @return bool
53 */
54 public function is_category_enabled( $category ) {
55 $settings = $this->get_settings();
56 if ( ! isset( $settings[ $category ] ) ) {
57 return true; // Unknown category — show by default.
58 }
59 return (bool) $settings[ $category ];
60 }
61
62 /**
63 * Return the current settings, merged with defaults.
64 *
65 * @return array
66 */
67 public function get_settings() {
68 $notifications = Charitable_Notifications::get_instance();
69 $option = $notifications->get_option();
70 return array_merge( self::DEFAULTS, (array) $option['settings'] );
71 }
72
73 /**
74 * AJAX handler — saves category toggles.
75 *
76 * Expects POST: nonce, settings (JSON object of category => bool).
77 */
78 public function ajax_save() {
79 check_ajax_referer( 'charitable_admin_nonce', 'nonce' );
80
81 if ( ! current_user_can( 'manage_charitable_settings' ) ) {
82 wp_send_json_error( 'Unauthorized' );
83 }
84
85 $raw = isset( $_POST['settings'] ) ? $_POST['settings'] : '{}'; // phpcs:ignore
86 $incoming = json_decode( wp_unslash( $raw ), true );
87
88 if ( ! is_array( $incoming ) ) {
89 wp_send_json_error( 'Invalid settings data' );
90 }
91
92 // Sanitize — only allow known keys, cast to bool.
93 $clean = [];
94 foreach ( self::DEFAULTS as $key => $default ) {
95 $clean[ $key ] = isset( $incoming[ $key ] ) ? (bool) $incoming[ $key ] : $default;
96 }
97
98 Charitable_Notifications::get_instance()->save_option_partial( [ 'settings' => $clean ] );
99
100 wp_send_json_success( $clean );
101 }
102 }
103