| 1 |
<?php |
| 2 |
/** |
| 3 |
* Displays a dashboard notice when GiveWP is detected. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_GiveWP_Notice |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.8.10 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_GiveWP_Notice' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_GiveWP_Notice |
| 21 |
* |
| 22 |
* @since 1.8.10 |
| 23 |
*/ |
| 24 |
class Charitable_GiveWP_Notice { |
| 25 |
|
| 26 |
/** |
| 27 |
* The single instance of this class. |
| 28 |
* |
| 29 |
* @var Charitable_GiveWP_Notice|null |
| 30 |
*/ |
| 31 |
private static $instance = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* Create class object. |
| 35 |
* |
| 36 |
* @since 1.8.10 |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Check if GiveWP is installed (active or inactive). |
| 43 |
* |
| 44 |
* @since 1.8.10 |
| 45 |
* |
| 46 |
* @return bool |
| 47 |
*/ |
| 48 |
private function is_givewp_installed() { |
| 49 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 50 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 51 |
} |
| 52 |
|
| 53 |
$plugins = get_plugins(); |
| 54 |
|
| 55 |
foreach ( $plugins as $plugin_file => $plugin_data ) { |
| 56 |
if ( strpos( $plugin_file, 'give.php' ) !== false || strpos( $plugin_file, 'give/' ) === 0 ) { |
| 57 |
return true; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get dashboard notices for GiveWP import suggestion. |
| 66 |
* |
| 67 |
* @since 1.8.10 |
| 68 |
* |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
public function get_dashboard_notices() { |
| 72 |
$dashboard_notices = get_option( 'charitable_dashboard_notifications', array() ); |
| 73 |
|
| 74 |
// If the notice has already been dismissed, bail. |
| 75 |
if ( isset( $dashboard_notices['givewp_import'] ) && isset( $dashboard_notices['givewp_import']['dismissed'] ) ) { |
| 76 |
return $dashboard_notices; |
| 77 |
} |
| 78 |
|
| 79 |
// If GiveWP is not installed, remove notice if it exists and bail. |
| 80 |
if ( ! $this->is_givewp_installed() ) { |
| 81 |
if ( isset( $dashboard_notices['givewp_import'] ) ) { |
| 82 |
unset( $dashboard_notices['givewp_import'] ); |
| 83 |
update_option( 'charitable_dashboard_notifications', $dashboard_notices ); |
| 84 |
} |
| 85 |
return $dashboard_notices; |
| 86 |
} |
| 87 |
|
| 88 |
// If the notice already exists and has the current format, don't add it again. |
| 89 |
if ( isset( $dashboard_notices['givewp_import'] ) && ! empty( $dashboard_notices['givewp_import']['button_url'] ) ) { |
| 90 |
return $dashboard_notices; |
| 91 |
} |
| 92 |
|
| 93 |
$dashboard_notices['givewp_import'] = array( |
| 94 |
'type' => 'notice', |
| 95 |
'priority' => 5, |
| 96 |
'dismiss' => true, |
| 97 |
'title' => esc_html__( 'Notice', 'charitable' ), |
| 98 |
'custom_css' => 'charitable-notification-type-notice', |
| 99 |
'message' => '<p>' . __( 'We noticed you have GiveWP installed. You can import your donation data from GiveWP to Charitable.', 'charitable' ) . '</p>', |
| 100 |
'button_url' => admin_url( 'admin.php?page=charitable-tools&tab=import' ), |
| 101 |
'button_text' => __( 'Import Now', 'charitable' ), |
| 102 |
); |
| 103 |
|
| 104 |
update_option( 'charitable_dashboard_notifications', $dashboard_notices ); |
| 105 |
|
| 106 |
return $dashboard_notices; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Create and return the class object. |
| 111 |
* |
| 112 |
* @since 1.8.10 |
| 113 |
* |
| 114 |
* @return Charitable_GiveWP_Notice |
| 115 |
*/ |
| 116 |
public static function get_instance() { |
| 117 |
if ( is_null( self::$instance ) ) { |
| 118 |
self::$instance = new self(); |
| 119 |
} |
| 120 |
|
| 121 |
return self::$instance; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
endif; |
| 126 |
|