| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Settings Hooks. |
| 4 |
* |
| 5 |
* Action/filter hooks used for Charitable Settings API. |
| 6 |
* |
| 7 |
* @package Charitable/Functions/Admin |
| 8 |
* @author David Bisset |
| 9 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 10 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 11 |
* @since 1.8.1.6 |
| 12 |
*/ |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Register Charitable tools. |
| 21 |
* |
| 22 |
* @see Charitable_Tools::register_settings() |
| 23 |
*/ |
| 24 |
add_action( 'admin_init', array( Charitable_Tools::get_instance(), 'register_settings' ) ); |
| 25 |
|
| 26 |
/** |
| 27 |
* Add settings to the Tools tab. |
| 28 |
* |
| 29 |
* @since 1.8.1.6 and 1.8.2 |
| 30 |
* |
| 31 |
* @see Charitable_Tools_Settings::add_import_fields() |
| 32 |
*/ |
| 33 |
add_filter( 'charitable_tools_tab_fields_export', array( Charitable_Tools_Settings::get_instance(), 'add_tools_export_fields' ), 5 ); |
| 34 |
add_filter( 'charitable_tools_tab_fields_snippets', array( Charitable_Tools_Settings::get_instance(), 'add_tools_snippets_fields' ), 5 ); |
| 35 |
add_filter( 'charitable_tools_tab_fields_customize', array( Charitable_Tools_Settings::get_instance(), 'add_tools_customize_fields' ), 5 ); |
| 36 |
|
| 37 |
/** |
| 38 |
* Import sub-tab fields. |
| 39 |
* |
| 40 |
* @since 1.8.10 |
| 41 |
*/ |
| 42 |
add_filter( 'charitable_tools_tab_fields_sub_import__charitable', array( Charitable_Tools_Settings::get_instance(), 'add_tools_import_charitable_fields' ), 5 ); |
| 43 |
add_filter( 'charitable_tools_tab_fields_sub_import__givewp', array( Charitable_Tools_Settings::get_instance(), 'add_tools_import_givewp_fields' ), 5 ); |
| 44 |
add_filter( 'charitable_tools_tab_fields_sub_import__givebutter', array( Charitable_Tools_Settings::get_instance(), 'add_tools_import_givebutter_fields' ), 5 ); |
| 45 |
|
| 46 |
/** |
| 47 |
* Add settings to the Misc tab. |
| 48 |
* |
| 49 |
* @since 1.8.9 |
| 50 |
* |
| 51 |
* @see Charitable_Tools_Misc::add_misc_fields() |
| 52 |
*/ |
| 53 |
add_filter( 'charitable_tools_tab_fields_misc', array( Charitable_Tools_Misc::get_instance(), 'add_misc_fields' ), 5 ); |
| 54 |
|
| 55 |
/** |
| 56 |
* Handle bulk removal of donations. |
| 57 |
* |
| 58 |
* @since 1.8.9 |
| 59 |
* |
| 60 |
* @see Charitable_Tools_Misc::bulk_remove_donations() |
| 61 |
*/ |
| 62 |
add_filter( 'charitable_save_tools', array( Charitable_Tools_Misc::get_instance(), 'bulk_remove_donations' ), 10, 3 ); |
| 63 |
|
| 64 |
/** |
| 65 |
* Look for export/import attempts. |
| 66 |
* |
| 67 |
* @see Charitable_Export_Settings::add_export_fields() |
| 68 |
*/ |
| 69 |
add_action( 'admin_init', array( Charitable_Export_Items::get_instance(), 'admin_accept_export_campaign_request' ) ); |
| 70 |
add_action( 'admin_init', array( Charitable_Export_Items::get_instance(), 'admin_accept_export_donations_request' ) ); |
| 71 |
add_action( 'admin_init', array( Charitable_Import_Items::get_instance(), 'admin_accept_import_campaign_request' ) ); |
| 72 |
add_action( 'admin_init', array( Charitable_Import_Items::get_instance(), 'admin_accept_import_donations_request' ) ); |
| 73 |
|
| 74 |
/** |
| 75 |
* Import sub-tab default redirect. |
| 76 |
* |
| 77 |
* @since 1.8.10 |
| 78 |
*/ |
| 79 |
add_action( 'admin_init', array( Charitable_Tools::get_instance(), 'tools_sub_tab_default' ), 5 ); |
| 80 |
|
| 81 |
/** |
| 82 |
* GiveWP and GiveButter CSV import handlers. |
| 83 |
* |
| 84 |
* @since 1.8.10 |
| 85 |
*/ |
| 86 |
add_action( 'admin_init', array( Charitable_Import_Items::get_instance(), 'admin_accept_import_donations_givewp_request' ) ); |
| 87 |
add_action( 'admin_init', array( Charitable_Import_Items::get_instance(), 'admin_accept_import_donations_givebutter_request' ) ); |
| 88 |
|
| 89 |
/** |
| 90 |
* GiveWP migration tool AJAX handlers. |
| 91 |
* |
| 92 |
* @since 1.8.10 |
| 93 |
*/ |
| 94 |
add_action( 'wp_ajax_charitable_givewp_import_start', array( Charitable_GiveWP_Importer::get_instance(), 'ajax_import_start' ) ); |
| 95 |
add_action( 'wp_ajax_charitable_givewp_import_batch', array( Charitable_GiveWP_Importer::get_instance(), 'ajax_import_batch' ) ); |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Add the tools tab settings fields. |
| 100 |
* |
| 101 |
* @since 1.8.1.6 |
| 102 |
* |
| 103 |
* @return array<string,array> |
| 104 |
*/ |
| 105 |
add_action( 'admin_enqueue_scripts', array( Charitable_Intergrations_WPCode::get_instance(), 'enqueue_scripts' ) ); |
| 106 |
add_action( 'admin_enqueue_scripts', array( Charitable_Tools_System_Info::get_instance(), 'enqueue_scripts' ) ); |
| 107 |
|
| 108 |
/** |
| 109 |
* Register AJAX action for email diagnostics. |
| 110 |
* |
| 111 |
* @since 1.8.9.2 |
| 112 |
*/ |
| 113 |
add_action( 'wp_ajax_charitable_email_diagnostics', array( Charitable_Tools_System_Info::get_instance(), 'ajax_email_diagnostics' ) ); |
| 114 |
|
| 115 |
/** |
| 116 |
* Register AJAX action for test email. |
| 117 |
* |
| 118 |
* @since 1.8.9.2 |
| 119 |
*/ |
| 120 |
add_action( 'wp_ajax_charitable_send_test_email', array( Charitable_Tools_System_Info::get_instance(), 'ajax_send_test_email' ) ); |
| 121 |
|
| 122 |
/** |
| 123 |
* Clear error logs via AJAX. |
| 124 |
* |
| 125 |
* @since 1.8.9.2 |
| 126 |
* |
| 127 |
* @see Charitable_Tools_System_Info::ajax_clear_error_logs() |
| 128 |
*/ |
| 129 |
add_action( 'wp_ajax_charitable_clear_error_logs', array( Charitable_Tools_System_Info::get_instance(), 'ajax_clear_error_logs' ) ); |
| 130 |
|
| 131 |
/** |
| 132 |
* Export error logs via AJAX. |
| 133 |
* |
| 134 |
* @since 1.8.9.2 |
| 135 |
* |
| 136 |
* @see Charitable_Tools_System_Info::ajax_export_error_logs() |
| 137 |
*/ |
| 138 |
add_action( 'wp_ajax_charitable_export_error_logs', array( Charitable_Tools_System_Info::get_instance(), 'ajax_export_error_logs' ) ); |
| 139 |
|
| 140 |
/** |
| 141 |
* Register AJAX action for debug log scanner. |
| 142 |
* |
| 143 |
* @since 1.8.9.2 |
| 144 |
* |
| 145 |
* @see Charitable_Tools_System_Info::ajax_debug_log_scan() |
| 146 |
*/ |
| 147 |
add_action( 'wp_ajax_charitable_debug_log_scan', array( Charitable_Tools_System_Info::get_instance(), 'ajax_debug_log_scan' ) ); |
| 148 |
|
| 149 |
/** |
| 150 |
* Capture failed plugin installs/updates into a small ring buffer so the |
| 151 |
* "Recent Plugin Install/Update Errors" section of System Info has data |
| 152 |
* to show. Pure pass-through on success. |
| 153 |
* |
| 154 |
* @since 1.8.10.5 |
| 155 |
*/ |
| 156 |
add_filter( 'upgrader_install_package_result', array( 'Charitable_Tools_System_Info', 'log_install_result' ), 10, 2 ); |
| 157 |
|
| 158 |
add_action( 'admin_enqueue_scripts', array( Charitable_Tools_Misc::get_instance(), 'enqueue_scripts' ) ); |
| 159 |
|
| 160 |
/** |
| 161 |
* Enqueue GiveWP migration tool scripts. |
| 162 |
* |
| 163 |
* @since 1.8.10 |
| 164 |
*/ |
| 165 |
add_action( 'admin_enqueue_scripts', array( Charitable_GiveWP_Importer::get_instance(), 'enqueue_scripts' ) ); |
| 166 |
|
| 167 |
/** |
| 168 |
* Logger AJAX handlers and script enqueue. |
| 169 |
* |
| 170 |
* @since 1.8.11 |
| 171 |
*/ |
| 172 |
add_action( 'wp_ajax_charitable_get_log_record', array( 'Charitable_Log', 'ajax_get_record' ) ); |
| 173 |
add_action( 'wp_ajax_charitable_delete_all_logs', array( 'Charitable_Log', 'ajax_delete_all_logs' ) ); |
| 174 |
add_action( 'wp_ajax_charitable_export_logs_csv', array( 'Charitable_Log', 'ajax_export_csv' ) ); |
| 175 |
add_action( 'wp_ajax_charitable_toggle_logging', array( 'Charitable_Log', 'ajax_toggle_logging' ) ); |
| 176 |
add_action( 'wp_ajax_charitable_save_log_retention', array( 'Charitable_Log', 'ajax_save_retention' ) ); |
| 177 |
add_action( 'admin_enqueue_scripts', array( 'Charitable_Log', 'enqueue_scripts' ) ); |
| 178 |
|
| 179 |
/** |
| 180 |
* Tools > AI MCP. |
| 181 |
* |
| 182 |
* Surfaces the Abilities API, which ships with no UI of its own, and carries the |
| 183 |
* AI write-access toggle. Charitable_Tools_AI_MCP is autoloaded via the classmap. |
| 184 |
* |
| 185 |
* Ported from Charitable Pro (pinned commit 4f84275197) for Lite 1.8.13. The |
| 186 |
* `ai-mcp` tab itself is registered directly in |
| 187 |
* Charitable_Tools::get_sections(), and the "no form / no Save Changes button" |
| 188 |
* behaviour Pro gets from its `charitable_tools_tabs_without_form` filter is |
| 189 |
* given here by adding 'ai-mcp' to the `$charitable_tab_no_form_tag` array in |
| 190 |
* includes/admin/views/tools/tools.php - the hardcoded-array convention that |
| 191 |
* file already uses for 'import', 'export', 'system-info' and the rest, rather |
| 192 |
* than porting a filter Lite's view template does not read. |
| 193 |
* |
| 194 |
* @since 1.8.13 |
| 195 |
*/ |
| 196 |
add_filter( |
| 197 |
'charitable_tools_tab_fields_ai-mcp', |
| 198 |
array( Charitable_Tools_AI_MCP::get_instance(), 'add_fields' ), |
| 199 |
5 |
| 200 |
); |
| 201 |
|
| 202 |
/** |
| 203 |
* Enqueue the AI MCP panel assets, on that one tab only. |
| 204 |
* |
| 205 |
* @since 1.8.13 |
| 206 |
* |
| 207 |
* @return void |
| 208 |
*/ |
| 209 |
add_action( |
| 210 |
'admin_enqueue_scripts', |
| 211 |
function () { |
| 212 |
|
| 213 |
if ( ! function_exists( 'charitable_is_tools_view' ) || ! charitable_is_tools_view() ) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
$tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 218 |
|
| 219 |
if ( 'ai-mcp' !== $tab ) { |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
wp_enqueue_style( 'dashicons' ); |
| 224 |
|
| 225 |
wp_enqueue_style( |
| 226 |
'charitable-ai-mcp', |
| 227 |
charitable()->get_path( 'assets', false ) . 'css/admin/charitable-ai-mcp.css', |
| 228 |
array(), |
| 229 |
charitable()->get_version() |
| 230 |
); |
| 231 |
|
| 232 |
if ( ! current_user_can( 'manage_charitable_settings' ) ) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
/* |
| 237 |
* jquery-confirm powers the "Watch video" modal. Guarded the same way |
| 238 |
* Charitable_Admin_Splash guards it, because the splash is enqueued on |
| 239 |
* this same screen and whichever runs first should win. Do NOT rely on |
| 240 |
* the splash having done it: the link has to work on a site where the |
| 241 |
* splash is suppressed (hide_announcements), and without the library the |
| 242 |
* handler falls through to the anchor's YouTube href. |
| 243 |
*/ |
| 244 |
if ( ! wp_style_is( 'jquery-confirm', 'enqueued' ) ) { |
| 245 |
wp_enqueue_style( |
| 246 |
'jquery-confirm', |
| 247 |
charitable()->get_path( 'directory', false ) . 'assets/lib/jquery.confirm/jquery-confirm.min.css', |
| 248 |
null, |
| 249 |
'3.3.4' |
| 250 |
); |
| 251 |
} |
| 252 |
|
| 253 |
if ( ! wp_script_is( 'jquery-confirm', 'enqueued' ) ) { |
| 254 |
wp_enqueue_script( |
| 255 |
'jquery-confirm', |
| 256 |
charitable()->get_path( 'directory', false ) . 'assets/lib/jquery.confirm/jquery-confirm.min.js', |
| 257 |
array( 'jquery' ), |
| 258 |
'3.3.4', |
| 259 |
false |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
wp_enqueue_script( |
| 264 |
'charitable-ai-mcp', |
| 265 |
charitable()->get_path( 'assets', false ) . 'js/admin/charitable-ai-mcp.js', |
| 266 |
array( 'jquery', 'jquery-confirm' ), |
| 267 |
charitable()->get_version(), |
| 268 |
true |
| 269 |
); |
| 270 |
|
| 271 |
wp_localize_script( |
| 272 |
'charitable-ai-mcp', |
| 273 |
'charitable_ai_mcp', |
| 274 |
array( |
| 275 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 276 |
'nonce' => wp_create_nonce( 'charitable_ai_mcp_toggle' ), |
| 277 |
// Charitable_Admin_Plugins_Third_Party guards both the install and |
| 278 |
// the activate call with the shared 'charitable-admin' nonce, so the |
| 279 |
// in-place bridge install needs that one rather than the toggle nonce. |
| 280 |
'plugin_nonce' => wp_create_nonce( 'charitable-admin' ), |
| 281 |
'i18n' => array( |
| 282 |
'error' => __( 'That could not be saved. Please try again.', 'charitable' ), |
| 283 |
'installing' => __( 'Installing WPVibe…', 'charitable' ), |
| 284 |
'activating' => __( 'Activating WPVibe…', 'charitable' ), |
| 285 |
'installError' => __( 'WPVibe could not be installed. Install it from the Plugins screen instead.', 'charitable' ), |
| 286 |
/* The iframe's title attribute, read by screen readers in the video modal. */ |
| 287 |
'videoTitle' => esc_attr__( 'Using Charitable with AI', 'charitable' ), |
| 288 |
/* |
| 289 |
* The AI Activity toggle's accessible name, which is the only |
| 290 |
* thing that can say which way the control goes - its visible |
| 291 |
* label is a chevron. The collapsed wording matches the |
| 292 |
* `screen-reader-text` rendered in views/ai-mcp-activity.php, |
| 293 |
* since that is the state the panel loads in. |
| 294 |
*/ |
| 295 |
'showActivity' => __( 'Show AI activity', 'charitable' ), |
| 296 |
'hideActivity' => __( 'Hide AI activity', 'charitable' ), |
| 297 |
), |
| 298 |
) |
| 299 |
); |
| 300 |
} |
| 301 |
); |
| 302 |
|
| 303 |
/** |
| 304 |
* AJAX: flip the AI write gate. |
| 305 |
* |
| 306 |
* No _nopriv counterpart, deliberately - this changes a security setting. |
| 307 |
* |
| 308 |
* @since 1.8.13 |
| 309 |
*/ |
| 310 |
add_action( |
| 311 |
'wp_ajax_charitable_ai_mcp_toggle_write', |
| 312 |
array( Charitable_Tools_AI_MCP::get_instance(), 'ajax_toggle_write' ) |
| 313 |
); |
| 314 |
|
| 315 |
/** |
| 316 |
* Remember when a user has opened WPVibe's own admin page. |
| 317 |
* |
| 318 |
* On `current_screen`, and NOT gated to the Tools tab, because WPVibe's page is |
| 319 |
* a different screen entirely and the visit has to be caught wherever it |
| 320 |
* happens. Flips the AI MCP tab's primary button from "Open WPVibe Setup" to |
| 321 |
* "Go to WPVibe" so a user is not told to set up something they have already |
| 322 |
* opened. The write is a one-time latch (see maybe_mark_bridge_visited()), so |
| 323 |
* this costs a single user-meta read on other admin screens. |
| 324 |
* |
| 325 |
* @since 1.8.13 |
| 326 |
*/ |
| 327 |
add_action( |
| 328 |
'current_screen', |
| 329 |
array( Charitable_Tools_AI_MCP::get_instance(), 'maybe_mark_bridge_visited' ) |
| 330 |
); |
| 331 |
|