| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Interface |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SureDonation\Inc\Admin; |
| 9 |
|
| 10 |
use SureDonation\Inc\Traits\Get_Instance; |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Admin class. |
| 19 |
* |
| 20 |
* @since 0.0.1 |
| 21 |
*/ |
| 22 |
class Admin { |
| 23 |
use Get_Instance; |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor. |
| 27 |
* |
| 28 |
* @since 0.0.1 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
add_action( 'admin_menu', [ $this, 'register_menu' ] ); |
| 32 |
add_action( 'admin_menu', [ $this, 'hide_forms_submenu' ], 999 ); |
| 33 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ] ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Enqueue admin scripts and styles. |
| 38 |
* |
| 39 |
* @param string $hook Current admin page hook. |
| 40 |
* @return void |
| 41 |
* @since 0.0.1 |
| 42 |
*/ |
| 43 |
public function enqueue_admin_scripts( $hook ) { |
| 44 |
// Only load on our plugin pages. |
| 45 |
if ( strpos( $hook, 'suredonation' ) === false ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
$asset_file = SUREDONATION_DIR . 'assets/build/admin.asset.php'; |
| 50 |
|
| 51 |
if ( ! file_exists( $asset_file ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$asset = include $asset_file; |
| 56 |
|
| 57 |
// Enqueue WordPress editor for TinyMCE support. |
| 58 |
wp_enqueue_editor(); |
| 59 |
|
| 60 |
// Enqueue editor buttons style (needed for TinyMCE icons). |
| 61 |
wp_enqueue_style( 'editor-buttons' ); |
| 62 |
|
| 63 |
// Enqueue admin app script. |
| 64 |
wp_enqueue_script( |
| 65 |
'suredonation-admin', |
| 66 |
SUREDONATION_URL . 'assets/build/admin.js', |
| 67 |
$asset['dependencies'], |
| 68 |
$asset['version'], |
| 69 |
true |
| 70 |
); |
| 71 |
|
| 72 |
// Enqueue admin app styles. |
| 73 |
wp_enqueue_style( |
| 74 |
'suredonation-admin', |
| 75 |
SUREDONATION_URL . 'assets/build/admin.css', |
| 76 |
[], |
| 77 |
$asset['version'] |
| 78 |
); |
| 79 |
|
| 80 |
// Localize script with plugin data. |
| 81 |
wp_localize_script( |
| 82 |
'suredonation-admin', |
| 83 |
'suredonation_admin', |
| 84 |
[ |
| 85 |
'version' => SUREDONATION_VER, |
| 86 |
'apiUrl' => rest_url( 'suredonation/v1' ), |
| 87 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 88 |
'docsURL' => 'https://suredonation.com/docs/', |
| 89 |
'plugin_url' => SUREDONATION_URL, |
| 90 |
'site_url' => site_url(), |
| 91 |
'admin_url' => admin_url(), |
| 92 |
'is_first_campaign_created' => $this->has_campaigns(), |
| 93 |
'rotating_plugin_banner' => $this->get_rotating_plugin_banner(), |
| 94 |
] |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Register admin menu. |
| 100 |
* |
| 101 |
* @return void |
| 102 |
* @since 0.0.1 |
| 103 |
*/ |
| 104 |
public function register_menu() { |
| 105 |
$menu_slug = 'suredonation'; |
| 106 |
|
| 107 |
// Main menu page. |
| 108 |
add_menu_page( |
| 109 |
__( 'SureDonation', 'suredonation' ), |
| 110 |
__( 'SureDonation', 'suredonation' ), |
| 111 |
'manage_options', |
| 112 |
$menu_slug, |
| 113 |
[ $this, 'render_dashboard' ], |
| 114 |
'dashicons-money-alt', |
| 115 |
25 |
| 116 |
); |
| 117 |
|
| 118 |
// Register submenus. |
| 119 |
$this->register_submenus( $menu_slug ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Render dashboard page. |
| 124 |
* |
| 125 |
* @return void |
| 126 |
* @since 0.0.1 |
| 127 |
*/ |
| 128 |
public function render_dashboard() { |
| 129 |
?> |
| 130 |
<div id="suredonation-admin-root"></div> |
| 131 |
<?php |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Hide the "All Forms" submenu item. |
| 136 |
* |
| 137 |
* WordPress automatically adds a submenu for the suredonation_form CPT. |
| 138 |
* We hide it since forms are managed through campaigns. |
| 139 |
* |
| 140 |
* @return void |
| 141 |
* @since 0.0.1 |
| 142 |
*/ |
| 143 |
public function hide_forms_submenu() { |
| 144 |
remove_submenu_page( 'suredonation', 'edit.php?post_type=suredonation_form' ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Check if any campaigns have been created. |
| 149 |
* |
| 150 |
* @return bool True if at least one campaign exists. |
| 151 |
* @since 0.0.1 |
| 152 |
*/ |
| 153 |
private function has_campaigns() { |
| 154 |
$campaigns = get_posts( |
| 155 |
[ |
| 156 |
'post_type' => 'suredonation_cmpgn', |
| 157 |
'posts_per_page' => 1, |
| 158 |
'post_status' => [ 'publish', 'draft', 'private' ], |
| 159 |
'fields' => 'ids', |
| 160 |
] |
| 161 |
); |
| 162 |
|
| 163 |
return ! empty( $campaigns ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get rotating plugin banner data. |
| 168 |
* |
| 169 |
* @return array<string,mixed> Plugin banner data or null if not available. |
| 170 |
* @since 0.0.1 |
| 171 |
*/ |
| 172 |
private function get_rotating_plugin_banner() { |
| 173 |
// Get SVG logo as base64. |
| 174 |
$logo_svg = file_get_contents( SUREDONATION_DIR . 'images/surerank.svg' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local file read. |
| 175 |
$logo_data = $logo_svg ? 'data:image/svg+xml;base64,' . base64_encode( $logo_svg ) : ''; // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 176 |
|
| 177 |
// SureRank plugin data. |
| 178 |
return [ |
| 179 |
'slug' => 'flavor', |
| 180 |
'title' => 'SureRank', |
| 181 |
'singleLineDescription' => __( 'SEO Made Easy for Your Website', 'suredonation' ), |
| 182 |
'subtitle' => __( 'Beautiful pages, persuasive content, and custom code in seconds. The possibilities are endless!', 'suredonation' ), |
| 183 |
'logo' => $logo_data, |
| 184 |
'status' => $this->get_plugin_status( 'flavor/flavor.php' ), |
| 185 |
]; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get plugin installation status. |
| 190 |
* |
| 191 |
* @param string $plugin_file Plugin file path (e.g., 'plugin-folder/plugin-file.php'). |
| 192 |
* @return string Plugin status: 'Activated', 'Installed', or 'Install'. |
| 193 |
* @since 0.0.1 |
| 194 |
*/ |
| 195 |
private function get_plugin_status( $plugin_file ) { |
| 196 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 197 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 198 |
} |
| 199 |
|
| 200 |
if ( is_plugin_active( $plugin_file ) ) { |
| 201 |
return 'Activated'; |
| 202 |
} |
| 203 |
|
| 204 |
$all_plugins = get_plugins(); |
| 205 |
if ( isset( $all_plugins[ $plugin_file ] ) ) { |
| 206 |
return 'Installed'; |
| 207 |
} |
| 208 |
|
| 209 |
return 'Install'; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Register submenus. |
| 214 |
* |
| 215 |
* @param string $menu_slug Menu slug. |
| 216 |
* @return void |
| 217 |
* @since 0.0.1 |
| 218 |
*/ |
| 219 |
private function register_submenus( $menu_slug ) { |
| 220 |
$capability = 'manage_options'; |
| 221 |
|
| 222 |
$submenus = [ |
| 223 |
[ |
| 224 |
'id' => $menu_slug, |
| 225 |
'page_title' => __( 'Dashboard', 'suredonation' ), |
| 226 |
], |
| 227 |
[ |
| 228 |
'id' => 'suredonation#/campaigns', |
| 229 |
'page_title' => __( 'Campaigns', 'suredonation' ), |
| 230 |
], |
| 231 |
[ |
| 232 |
'id' => 'suredonation#/donations', |
| 233 |
'page_title' => __( 'Donations', 'suredonation' ), |
| 234 |
], |
| 235 |
[ |
| 236 |
'id' => 'suredonation#/settings', |
| 237 |
'page_title' => __( 'Settings', 'suredonation' ), |
| 238 |
], |
| 239 |
]; |
| 240 |
|
| 241 |
// Register the submenus. |
| 242 |
foreach ( $submenus as $submenu ) { |
| 243 |
add_submenu_page( |
| 244 |
$menu_slug, |
| 245 |
$submenu['page_title'], |
| 246 |
$submenu['page_title'], |
| 247 |
$capability, |
| 248 |
$submenu['id'], |
| 249 |
[ $this, 'render_dashboard' ] |
| 250 |
); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|