PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.1.2
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.1.2
1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / admin / admin.php

admin.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 1.1.2, at inc/admin/admin.php

528 lines 17.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin Interface
4 *
5 * @package SureDonation
6 */
7
8 namespace SureDonation\Inc\Admin;
9
10 use SureDonation\Inc\Helper;
11 use SureDonation\Inc\Onboarding;
12 use SureDonation\Inc\Pdf\Pdf_Utils;
13 use SureDonation\Inc\Payments\Payment_Helper;
14 use SureDonation\Inc\Payments\PayPal\PayPal_Helper;
15 use SureDonation\Inc\Traits\Get_Instance;
16
17 // Exit if accessed directly.
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21
22 /**
23 * Admin class.
24 *
25 * @since 0.0.1
26 */
27 class Admin {
28 use Get_Instance;
29
30 /**
31 * Constructor.
32 *
33 * @since 0.0.1
34 */
35 public function __construct() {
36 add_action( 'admin_menu', [ $this, 'register_menu' ] );
37 add_action( 'admin_menu', [ $this, 'hide_forms_submenu' ], 999 );
38 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ] );
39 add_action( 'admin_init', [ $this, 'redirect_cpt_listings' ] );
40 add_action( 'admin_init', [ $this, 'register_privacy_policy_content' ] );
41 }
42
43 /**
44 * Register suggested privacy-policy text for the plugin's data transfers.
45 *
46 * Surfaces the Phone field's automatic country detection (which sends the
47 * visitor's IP to ipapi.co) in the WordPress Privacy Policy Guide
48 * (Settings → Privacy → Policy Guide) so site owners can disclose it in
49 * their own policy. This only suggests text to admins — it publishes
50 * nothing and is not shown to donors.
51 *
52 * @since 1.1.1
53 * @return void
54 */
55 public function register_privacy_policy_content() {
56 if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) {
57 return;
58 }
59
60 $content = __( 'When a Phone field with automatic country detection is used, this site sends the visitor’s IP address to a third-party service (ipapi.co) to determine their country code. The IP address is not stored for this purpose; the resulting country code is cached temporarily. Automatic detection can be disabled per Phone field, or site-wide via the “suredonation_phone_geo_enabled” filter.', 'suredonation' );
61
62 wp_add_privacy_policy_content( 'SureDonation', wp_kses_post( wpautop( $content ) ) );
63 }
64
65 /**
66 * Redirect the default CPT list screens for suredonation_form and
67 * suredonation_cmpgn to their corresponding plugin pages.
68 *
69 * For the form listing, attempt to resolve the originating campaign
70 * from the referer (the "back" arrow on the form editor sends the
71 * user here) and redirect to that campaign's single view.
72 *
73 * @return void
74 * @since 1.0.0
75 */
76 public function redirect_cpt_listings() {
77 global $pagenow;
78
79 if ( 'edit.php' !== $pagenow ) {
80 return;
81 }
82
83 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading post_type from query, no state mutation.
84 $post_type = isset( $_GET['post_type'] ) ? sanitize_key( wp_unslash( $_GET['post_type'] ) ) : '';
85
86 if ( 'suredonation_form' === $post_type ) {
87 $campaign_id = $this->resolve_campaign_id_from_referer();
88 $target = $campaign_id
89 ? admin_url( 'admin.php?page=suredonation#/campaigns/' . $campaign_id )
90 : admin_url( 'admin.php?page=suredonation#/campaigns' );
91
92 wp_safe_redirect( $target );
93 exit;
94 }
95
96 if ( 'suredonation_cmpgn' === $post_type ) {
97 wp_safe_redirect( admin_url( 'admin.php?page=suredonation#/campaigns' ) );
98 exit;
99 }
100 }
101
102 /**
103 * Resolve the campaign ID from the referer when the form list page
104 * is reached from the form editor's "back" button.
105 *
106 * @return int Campaign ID, or 0 when it cannot be determined.
107 * @since 1.0.0
108 */
109 private function resolve_campaign_id_from_referer() {
110 $referer = wp_get_referer();
111 if ( ! $referer ) {
112 return 0;
113 }
114
115 $parts = wp_parse_url( $referer );
116 if ( empty( $parts['query'] ) ) {
117 return 0;
118 }
119
120 $query = [];
121 parse_str( $parts['query'], $query );
122
123 if ( empty( $query['post'] ) || ! is_numeric( $query['post'] ) ) {
124 return 0;
125 }
126
127 $form_id = absint( $query['post'] );
128 if ( 'suredonation_form' !== get_post_type( $form_id ) ) {
129 return 0;
130 }
131
132 $campaign_id = get_post_meta( $form_id, '_suredonation_campaign_id', true );
133 if ( ! is_numeric( $campaign_id ) ) {
134 return 0;
135 }
136 return absint( $campaign_id );
137 }
138
139 /**
140 * Enqueue admin scripts and styles.
141 *
142 * @param string $hook Current admin page hook.
143 * @return void
144 * @since 0.0.1
145 */
146 public function enqueue_admin_scripts( $hook ) {
147 // Only load on our plugin pages.
148 if ( strpos( $hook, 'suredonation' ) === false ) {
149 return;
150 }
151
152 // Onboarding gets its own lean bundle — bail before enqueuing the
153 // main admin app on that page.
154 if ( false !== strpos( $hook, Onboarding::PAGE_SLUG ) ) {
155 $this->enqueue_onboarding_assets();
156 return;
157 }
158
159 $asset_file = SUREDONATION_DIR . 'assets/build/admin.asset.php';
160
161 if ( ! file_exists( $asset_file ) ) {
162 return;
163 }
164
165 $asset = include $asset_file;
166
167 // Enqueue WordPress editor for TinyMCE support.
168 wp_enqueue_editor();
169
170 // Enqueue the media library so the campaign image picker can open it.
171 wp_enqueue_media();
172
173 // Enqueue editor buttons style (needed for TinyMCE icons).
174 wp_enqueue_style( 'editor-buttons' );
175
176 // Enqueue admin app script.
177 wp_enqueue_script(
178 'suredonation-admin',
179 SUREDONATION_URL . 'assets/build/admin.js',
180 $asset['dependencies'],
181 $asset['version'],
182 true
183 );
184
185 // Enqueue admin app styles.
186 wp_enqueue_style(
187 'suredonation-admin',
188 SUREDONATION_URL . 'assets/build/admin.css',
189 [],
190 $asset['version']
191 );
192
193 // The campaign drawer sits at a very high z-index. Lift the media library
194 // above it, and keep the media modal above its OWN backdrop so the picker
195 // is not dimmed by either overlay.
196 wp_add_inline_style(
197 'suredonation-admin',
198 '.media-modal-backdrop{z-index:2147483646 !important;}.media-modal{z-index:2147483647 !important;}'
199 );
200
201 // Load JS translations for the admin app.
202 wp_set_script_translations( 'suredonation-admin', 'suredonation' );
203
204 // Localize script with plugin data.
205 wp_localize_script(
206 'suredonation-admin',
207 'suredonation_admin',
208 [
209 'version' => SUREDONATION_VER,
210 'apiUrl' => rest_url( 'suredonation/v1' ),
211 'nonce' => wp_create_nonce( 'wp_rest' ),
212 'docsURL' => 'https://suredonation.com/docs/',
213 'plugin_url' => SUREDONATION_URL,
214 'site_url' => site_url(),
215 'admin_url' => admin_url(),
216 'is_first_campaign_created' => $this->has_campaigns(),
217 'is_onboarding_completed' => Onboarding::get_instance()->is_completed(),
218 'rotating_plugin_banner' => $this->get_rotating_plugin_banner(),
219 'smart_tags' => Helper::get_smart_tags(),
220 'is_pro_active' => defined( 'SUREDONATION_PRO_VER' ),
221 'pro_plugin_version' => defined( 'SUREDONATION_PRO_VER' ) ? SUREDONATION_PRO_VER : '',
222 'pro_plugin_name' => defined( 'SUREDONATION_PRO_PRODUCT' ) ? SUREDONATION_PRO_PRODUCT : 'SureDonation Pro',
223 'is_license_active' => defined( 'SUREDONATION_PRO_VER' ) && class_exists( 'SureDonationPro\Inc\Licensing\Licensing' ) ? \SureDonationPro\Inc\Licensing\Licensing::is_license_active() : false,
224 'pdf_library_exists' => Pdf_Utils::check_if_library_exists(),
225 'php_version_compatible' => Pdf_Utils::is_php_compatible(),
226 'pdf_nonce' => wp_create_nonce( 'suredonation_pdf_nonce' ),
227 'ajax_url' => admin_url( 'admin-ajax.php' ),
228 'plugin_installer_nonce' => wp_create_nonce( 'updates' ),
229 'plugin_manager_nonce' => wp_create_nonce( 'suredonation_plugin_manager' ),
230 'has_abilities_api' => class_exists( 'WP_Ability' ),
231 'current_user_login' => wp_get_current_user()->user_login,
232 // Bootstrap settings data so the General currency list and the
233 // PayPal connection state render synchronously (no post-mount
234 // fetch flicker). PayPal data is the non-sensitive subset only.
235 'payments' => [
236 'currencies' => Payment_Helper::get_currencies_list(),
237 'paypal' => PayPal_Helper::get_connection_state(),
238 ],
239 ]
240 );
241 }
242
243 /**
244 * Register admin menu.
245 *
246 * @return void
247 * @since 0.0.1
248 */
249 public function register_menu() {
250 $menu_slug = 'suredonation';
251
252 // SureDonation mark for the admin menu — single path with evenodd
253 // fill so the inner heart cuts a hole that reveals the sidebar
254 // background (mirrors the pattern used by SureForms).
255 $menu_icon_svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none"><path fill-rule="evenodd" clip-rule="evenodd" d="M0 3.64405C0 1.6315 1.62712 0 3.63425 0H20.2517C22.2588 0 23.886 1.6315 23.886 3.64405V20.3063C23.886 22.3188 22.2588 23.9503 20.2517 23.9503H3.63425C1.62712 23.9503 0 22.3188 0 20.3063V3.64405ZM12.6516 5.79655C14.4525 3.99075 17.3723 3.99075 19.1734 5.79655C20.9742 7.60234 20.9742 10.5301 19.1734 12.3359L17.5686 13.9451C17.2776 14.2368 16.8057 14.2368 16.5147 13.9451C16.2237 13.6533 16.2237 13.1802 16.5147 12.8884L18.1196 11.2793C19.3385 10.0571 19.3385 8.07537 18.1196 6.85312C16.9007 5.6309 14.9243 5.63092 13.7054 6.85312L11.3607 9.20415C11.0701 9.49552 11.0701 9.96791 11.3607 10.2593C11.6513 10.5506 12.1224 10.5506 12.413 10.2593L13.4659 9.20346C13.7569 8.91169 14.2288 8.91169 14.5198 9.20346C14.8107 9.49523 14.8107 9.96828 14.5198 10.26L13.4667 11.3159C12.5942 12.1909 11.1794 12.1909 10.3069 11.3159C9.43429 10.441 9.43429 9.02249 10.3069 8.14757L10.8879 7.56486L10.1781 6.85312C8.95914 5.63089 6.98277 5.63089 5.76382 6.85312C4.54486 8.07537 4.54488 10.0571 5.76382 11.2793L12.6985 18.2326C12.9895 18.5244 12.9895 18.9974 12.6985 19.2892C12.4075 19.581 11.9358 19.581 11.6448 19.2892L4.71008 12.3359C2.90915 10.5301 2.90913 7.60233 4.71008 5.79655C6.51102 3.99075 9.43091 3.99075 11.2318 5.79655L11.9417 6.50827L12.6516 5.79655ZM11.8605 14.9781C12.1515 14.6863 12.6233 14.6863 12.9143 14.9781L14.5397 16.6079C14.8307 16.8996 14.8307 17.3727 14.5397 17.6645C14.2487 17.9562 13.7769 17.9562 13.4859 17.6645L11.8605 16.0347C11.5695 15.7429 11.5695 15.2699 11.8605 14.9781ZM13.6482 13.1856C13.9392 12.8939 14.4109 12.8939 14.7019 13.1856L16.3273 14.8154C16.6183 15.1072 16.6183 15.5802 16.3273 15.872C16.0363 16.1638 15.5645 16.1638 15.2735 15.872L13.6482 14.2422C13.3572 13.9504 13.3572 13.4774 13.6482 13.1856Z" fill="#D1D5DB"/></svg>';
256
257 // Main menu page.
258 add_menu_page(
259 __( 'SureDonation', 'suredonation' ),
260 __( 'SureDonation', 'suredonation' ),
261 'manage_options',
262 $menu_slug,
263 [ $this, 'render_dashboard' ],
264 'data:image/svg+xml;base64,' . base64_encode( $menu_icon_svg ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Building data URI for menu icon.
265 25
266 );
267
268 // Register submenus.
269 $this->register_submenus( $menu_slug );
270 }
271
272 /**
273 * Render dashboard page.
274 *
275 * @return void
276 * @since 0.0.1
277 */
278 public function render_dashboard() {
279 ?>
280 <div id="suredonation-admin-root"></div>
281 <?php
282 }
283
284 /**
285 * Hide the "All Forms" submenu item.
286 *
287 * WordPress automatically adds a submenu for the suredonation_form CPT.
288 * We hide it since forms are managed through campaigns.
289 *
290 * @return void
291 * @since 0.0.1
292 */
293 public function hide_forms_submenu() {
294 remove_submenu_page( 'suredonation', 'edit.php?post_type=suredonation_form' );
295 }
296
297 /**
298 * Check if any campaigns have been created.
299 *
300 * @return bool True if at least one campaign exists.
301 * @since 0.0.1
302 */
303 private function has_campaigns() {
304 $campaigns = get_posts(
305 [
306 'post_type' => 'suredonation_cmpgn',
307 'posts_per_page' => 1,
308 'post_status' => [ 'publish', 'draft', 'private' ],
309 'fields' => 'ids',
310 ]
311 );
312
313 return ! empty( $campaigns );
314 }
315
316 /**
317 * Get rotating plugin banner data.
318 *
319 * @return array<string,mixed>|null Plugin banner data, or null when the plugin is already active.
320 * @since 0.0.1
321 */
322 private function get_rotating_plugin_banner() {
323 // Match SureForms: never nudge a plugin that is already active, so the
324 // dashboard card is hidden once SureRank is activated.
325 $status = $this->get_plugin_status( 'surerank/surerank.php' );
326 if ( 'Activated' === $status ) {
327 return null;
328 }
329
330 // Get SVG logo as base64.
331 $logo_svg = file_get_contents( SUREDONATION_DIR . 'images/surerank.svg' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file read.
332 $logo_data = $logo_svg ? 'data:image/svg+xml;base64,' . base64_encode( $logo_svg ) : ''; // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
333
334 // SureRank plugin data.
335 return [
336 'slug' => 'surerank',
337 'path' => 'surerank/surerank.php',
338 'title' => 'SureRank',
339 'singleLineDescription' => __( 'SEO Made Easy for Your Website', 'suredonation' ),
340 'subtitle' => __( 'Beautiful pages, persuasive content, and custom code in seconds. The possibilities are endless!', 'suredonation' ),
341 'logo' => $logo_data,
342 'status' => $status,
343 ];
344 }
345
346 /**
347 * Get plugin installation status.
348 *
349 * @param string $plugin_file Plugin file path (e.g., 'plugin-folder/plugin-file.php').
350 * @return string Plugin status: 'Activated', 'Installed', or 'Install'.
351 * @since 0.0.1
352 */
353 private function get_plugin_status( $plugin_file ) {
354 if ( ! function_exists( 'is_plugin_active' ) ) {
355 include_once ABSPATH . 'wp-admin/includes/plugin.php';
356 }
357
358 if ( is_plugin_active( $plugin_file ) ) {
359 return 'Activated';
360 }
361
362 $all_plugins = get_plugins();
363 if ( isset( $all_plugins[ $plugin_file ] ) ) {
364 return 'Installed';
365 }
366
367 return 'Install';
368 }
369
370 /**
371 * Register submenus.
372 *
373 * @param string $menu_slug Menu slug.
374 * @return void
375 * @since 0.0.1
376 */
377 private function register_submenus( $menu_slug ) {
378 $capability = 'manage_options';
379
380 $submenus = [
381 [
382 'id' => $menu_slug,
383 'page_title' => __( 'Dashboard', 'suredonation' ),
384 ],
385 [
386 'id' => 'suredonation#/campaigns',
387 'page_title' => __( 'Campaigns', 'suredonation' ),
388 ],
389 [
390 'id' => 'suredonation#/donations',
391 'page_title' => __( 'Donations', 'suredonation' ),
392 ],
393 [
394 'id' => 'suredonation#/donors',
395 'page_title' => __( 'Donors', 'suredonation' ),
396 ],
397 [
398 'id' => 'suredonation#/reports',
399 'page_title' => __( 'Reports', 'suredonation' ),
400 ],
401 [
402 'id' => 'suredonation#/settings',
403 'page_title' => __( 'Settings', 'suredonation' ),
404 ],
405 ];
406
407 // Register the submenus.
408 foreach ( $submenus as $submenu ) {
409 add_submenu_page(
410 $menu_slug,
411 $submenu['page_title'],
412 $submenu['page_title'],
413 $capability,
414 $submenu['id'],
415 [ $this, 'render_dashboard' ]
416 );
417 }
418
419 // Hidden onboarding page (no menu parent so it never shows in
420 // the sidebar but is still routable via admin.php?page=...).
421 add_submenu_page(
422 '',
423 __( 'SureDonation Setup', 'suredonation' ),
424 __( 'SureDonation Setup', 'suredonation' ),
425 $capability,
426 Onboarding::PAGE_SLUG,
427 [ $this, 'render_onboarding' ]
428 );
429 }
430
431 /**
432 * Render onboarding root container.
433 *
434 * @return void
435 * @since 1.0.0
436 */
437 public function render_onboarding() {
438 ?>
439 <div id="suredonation-onboarding-root"></div>
440 <?php
441 }
442
443 /**
444 * Enqueue the dedicated onboarding bundle.
445 *
446 * Loaded only on the onboarding admin page so the main admin app
447 * doesn't pay the cost on activation redirects.
448 *
449 * @return void
450 * @since 1.0.0
451 */
452 private function enqueue_onboarding_assets() {
453 $asset_file = SUREDONATION_DIR . 'assets/build/admin/onboarding/index.asset.php';
454
455 if ( ! file_exists( $asset_file ) ) {
456 return;
457 }
458
459 $asset = include $asset_file;
460
461 wp_enqueue_script(
462 'suredonation-onboarding',
463 SUREDONATION_URL . 'assets/build/admin/onboarding/index.js',
464 $asset['dependencies'],
465 $asset['version'],
466 true
467 );
468
469 wp_enqueue_style(
470 'suredonation-onboarding',
471 SUREDONATION_URL . 'assets/build/admin/onboarding/index.css',
472 [],
473 $asset['version']
474 );
475
476 wp_set_script_translations( 'suredonation-onboarding', 'suredonation' );
477
478 $current_user = wp_get_current_user();
479 $is_pro = defined( 'SUREDONATION_PRO_VER' );
480
481 // Detect GiveWP rows once at enqueue time so the JS doesn't have to
482 // spin a loader (and won't ever flash an error) just to decide whether
483 // to render the optional migration step.
484 $has_givewp_data = false;
485 try {
486 $has_givewp_data = \SureDonation\Inc\Import\Givewp\Source::get_instance()->has_givewp_data();
487 } catch ( \Throwable $e ) {
488 $has_givewp_data = false;
489 }
490
491 wp_localize_script(
492 'suredonation-onboarding',
493 'suredonation_onboarding',
494 [
495 'apiUrl' => rest_url( 'suredonation/v1' ),
496 'nonce' => wp_create_nonce( 'wp_rest' ),
497 'pluginUrl' => SUREDONATION_URL,
498 'adminUrl' => admin_url(),
499 'dashboardUrl' => admin_url( 'admin.php?page=suredonation' ),
500 'paymentsUrl' => admin_url( 'admin.php?page=suredonation#/settings?tab=payments' ),
501 'campaignsUrl' => admin_url( 'admin.php?page=suredonation#/campaigns' ),
502 'docsUrl' => 'https://suredonation.com/docs/',
503 'isProActive' => $is_pro,
504 'hasGiveWPData' => $has_givewp_data,
505 'currentUser' => [
506 'firstName' => $current_user ? $current_user->first_name : '',
507 'lastName' => $current_user ? $current_user->last_name : '',
508 'email' => $current_user ? $current_user->user_email : '',
509 ],
510 'privacyUrl' => 'https://suredonation.com/privacy-policy/',
511 ]
512 );
513
514 // Shim suredonation_admin on the onboarding page too — shared API
515 // helpers (e.g. importApi.js) read apiUrl + nonce from that global,
516 // so without this the migration session fails the REST cookie check.
517 wp_localize_script(
518 'suredonation-onboarding',
519 'suredonation_admin',
520 [
521 'apiUrl' => rest_url( 'suredonation/v1' ),
522 'nonce' => wp_create_nonce( 'wp_rest' ),
523 'is_pro_active' => $is_pro,
524 ]
525 );
526 }
527 }
528