| 1 |
<?php |
| 2 |
/** |
| 3 |
* Give Blank Slate Class |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
* @subpackage Admin |
| 7 |
* @copyright Copyright (c) 2017, GiveWP |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.8.13 |
| 10 |
*/ |
| 11 |
|
| 12 |
use Give\DonationForms\V2\DonationFormsAdminPage; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
class Give_Blank_Slate { |
| 19 |
/** |
| 20 |
* The current screen ID. |
| 21 |
* |
| 22 |
* @since 1.8.13 |
| 23 |
* @var string |
| 24 |
* @access public |
| 25 |
*/ |
| 26 |
public $screen = ''; |
| 27 |
|
| 28 |
/** |
| 29 |
* Whether at least one donation form exists. |
| 30 |
* |
| 31 |
* @since 1.8.13 |
| 32 |
* @var bool |
| 33 |
* @access private |
| 34 |
*/ |
| 35 |
private $form = false; |
| 36 |
|
| 37 |
/** |
| 38 |
* Whether at least one donation exists. |
| 39 |
* |
| 40 |
* @since 1.8.13 |
| 41 |
* @var bool |
| 42 |
* @access private |
| 43 |
*/ |
| 44 |
private $donation = false; |
| 45 |
|
| 46 |
/** |
| 47 |
* Whether at least one donor exists. |
| 48 |
* |
| 49 |
* @since 1.8.13 |
| 50 |
* @var bool |
| 51 |
* @access private |
| 52 |
*/ |
| 53 |
private $donor = false; |
| 54 |
|
| 55 |
/** |
| 56 |
* The content of the blank slate panel. |
| 57 |
* |
| 58 |
* @since 1.8.13 |
| 59 |
* @var array |
| 60 |
* @access private |
| 61 |
*/ |
| 62 |
private $content = array(); |
| 63 |
|
| 64 |
/** |
| 65 |
* Constructs the Give_Blank_Slate class. |
| 66 |
* |
| 67 |
* @since 1.8.13 |
| 68 |
*/ |
| 69 |
public function __construct() { |
| 70 |
$this->screen = get_current_screen()->id; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Initializes the class and hooks into WordPress. |
| 75 |
* |
| 76 |
* @since 1.8.13 |
| 77 |
*/ |
| 78 |
public function init() { |
| 79 |
// Bail early if screen cannot be detected. |
| 80 |
if ( empty( $this->screen ) ) { |
| 81 |
return null; |
| 82 |
} |
| 83 |
|
| 84 |
$content = array(); |
| 85 |
|
| 86 |
// Define content and hook into the appropriate action. |
| 87 |
switch ( $this->screen ) { |
| 88 |
// Forms screen. |
| 89 |
case 'edit-give_forms': |
| 90 |
$this->form = $this->post_exists( 'give_forms' ); |
| 91 |
|
| 92 |
if ( $this->form ) { |
| 93 |
// Form exists. Bail out. |
| 94 |
return false; |
| 95 |
} else { |
| 96 |
// No forms exist. |
| 97 |
$content = $this->get_content( 'no_forms' ); |
| 98 |
} |
| 99 |
|
| 100 |
add_action( 'manage_posts_extra_tablenav', array( $this, 'render' ) ); |
| 101 |
break; |
| 102 |
// Donations screen. |
| 103 |
case 'give_forms_page_give-payment-history': |
| 104 |
$this->form = $this->post_exists( 'give_forms' ); |
| 105 |
$this->donation = $this->post_exists( 'give_payment' ); |
| 106 |
|
| 107 |
if ( $this->donation ) { |
| 108 |
// Donation exists. Bail out. |
| 109 |
return false; |
| 110 |
} elseif ( ! $this->form ) { |
| 111 |
// No forms and no donations exist. |
| 112 |
$content = $this->get_content( 'no_donations_or_forms' ); |
| 113 |
} else { |
| 114 |
// No donations exist but a form does exist. |
| 115 |
$content = $this->get_content( 'no_donations' ); |
| 116 |
} |
| 117 |
|
| 118 |
add_action( 'give_payments_page_bottom', array( $this, 'render' ) ); |
| 119 |
break; |
| 120 |
// Donors screen. |
| 121 |
case 'give_forms_page_give-donors': |
| 122 |
$this->form = $this->post_exists( 'give_forms' ); |
| 123 |
$this->donor = $this->donor_exists(); |
| 124 |
|
| 125 |
if ( $this->donor ) { |
| 126 |
// Donor exists. Bail out. |
| 127 |
return false; |
| 128 |
} elseif ( ! $this->form ) { |
| 129 |
// No forms and no donors exist. |
| 130 |
$content = $this->get_content( 'no_donors_or_forms' ); |
| 131 |
} else { |
| 132 |
// No donors exist but a form does exist. |
| 133 |
$content = $this->get_content( 'no_donors' ); |
| 134 |
} |
| 135 |
|
| 136 |
add_action( 'give_donors_table_bottom', array( $this, 'render' ) ); |
| 137 |
break; |
| 138 |
default: |
| 139 |
return null; |
| 140 |
} |
| 141 |
|
| 142 |
$this->content = $content; |
| 143 |
|
| 144 |
// Hide non-essential UI elements. |
| 145 |
add_action( 'admin_head', array( $this, 'hide_ui' ) ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Renders the blank slate message. |
| 150 |
* |
| 151 |
* @since 1.8.13 |
| 152 |
* |
| 153 |
* @param string $which The location of the list table hook: 'top' or 'bottom'. |
| 154 |
*/ |
| 155 |
public function render( $which = 'bottom' ) { |
| 156 |
// Bail out to prevent content from rendering twice. |
| 157 |
if ( 'top' === $which ) { |
| 158 |
return null; |
| 159 |
} |
| 160 |
|
| 161 |
$screen = $this->screen; |
| 162 |
|
| 163 |
/** |
| 164 |
* Filters the content of the blank slate. |
| 165 |
* |
| 166 |
* @since 1.8.13 |
| 167 |
* |
| 168 |
* @param array $content { |
| 169 |
* Array of blank slate content. |
| 170 |
* |
| 171 |
* @type string $image_url URL of the blank slate image. |
| 172 |
* @type string $image_alt Image alt text. |
| 173 |
* @type string $heading Heading text. |
| 174 |
* @type string $message Body copy. |
| 175 |
* @type string $cta_text Call to action text. |
| 176 |
* @type string $cta_link Call to action URL. |
| 177 |
* @type string $help Help text. |
| 178 |
* } |
| 179 |
* |
| 180 |
* @param string $screen The current screen ID. |
| 181 |
*/ |
| 182 |
$content = apply_filters( 'give_blank_slate_content', $this->content, $screen ); |
| 183 |
|
| 184 |
$template_path = GIVE_PLUGIN_DIR . 'includes/admin/views/blank-slate.php'; |
| 185 |
|
| 186 |
include $template_path; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Hides non-essential UI elements when blank slate content is on screen. |
| 191 |
* |
| 192 |
* @since 1.8.13 |
| 193 |
*/ |
| 194 |
function hide_ui() { |
| 195 |
?> |
| 196 |
<style type="text/css"> |
| 197 |
.give-filters, |
| 198 |
.search-box, |
| 199 |
.subsubsub, |
| 200 |
.wp-list-table, |
| 201 |
.tablenav.top, |
| 202 |
.give_forms_page_give-payment-history .tablenav.bottom, |
| 203 |
.give_forms_page_give-donors .tablenav.bottom, |
| 204 |
.tablenav-pages { |
| 205 |
display: none; |
| 206 |
} |
| 207 |
</style> |
| 208 |
<?php |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Determines if at least one post of a given post type exists. |
| 213 |
* |
| 214 |
* @since 1.8.13 |
| 215 |
* |
| 216 |
* @param string $post_type Post type used in the query. |
| 217 |
* @return bool True if post exists, otherwise false. |
| 218 |
*/ |
| 219 |
private function post_exists( $post_type ) { |
| 220 |
// Attempt to get a single post of the post type. |
| 221 |
$query = new WP_Query( |
| 222 |
array( |
| 223 |
'post_type' => $post_type, |
| 224 |
'posts_per_page' => 1, |
| 225 |
'no_found_rows' => false, |
| 226 |
'update_post_meta_cache' => false, |
| 227 |
'update_post_term_cache' => false, |
| 228 |
'fields' => 'ids', |
| 229 |
'post_status' => array( 'any', 'trash' ), |
| 230 |
) |
| 231 |
); |
| 232 |
|
| 233 |
return $query->have_posts(); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Determines if at least one donor exists. |
| 238 |
* |
| 239 |
* @since 1.8.13 |
| 240 |
* |
| 241 |
* @return bool True if donor exists, otherwise false. |
| 242 |
*/ |
| 243 |
private function donor_exists() { |
| 244 |
$donors = Give()->donors->get_donors( array( 'number' => 1 ) ); |
| 245 |
|
| 246 |
return ! empty( $donors ); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Gets the content of a blank slate message based on provided context. |
| 251 |
* |
| 252 |
* @since 1.8.13 |
| 253 |
* |
| 254 |
* @param string $context The key used to determine which content is returned. |
| 255 |
* @return array Blank slate content. |
| 256 |
*/ |
| 257 |
private function get_content( $context ) { |
| 258 |
// Define default content. |
| 259 |
$defaults = array( |
| 260 |
'image_url' => GIVE_PLUGIN_URL . 'build/assets/dist/images/give-icon-full-circle.svg', |
| 261 |
'image_alt' => __( 'GiveWP Icon', 'give'), |
| 262 |
'heading' => __('No campaign forms found.', 'give'), |
| 263 |
'message' => __('The first step towards accepting online donations is to create a campaign.', |
| 264 |
'give'), |
| 265 |
'cta_text' => __('Create Campaign Form', 'give'), |
| 266 |
'cta_link' => admin_url('edit.php?post_type=give_forms&page=give-campaigns&new=campaign'), |
| 267 |
'help' => sprintf( |
| 268 |
/* translators: 1: Opening anchor tag. 2: Closing anchor tag. */ |
| 269 |
__( 'Need help? Get started with %1$sGive 101%2$s.', 'give' ), |
| 270 |
'<a href="http://docs.givewp.com/give101/" target="_blank">', |
| 271 |
'</a>' |
| 272 |
), |
| 273 |
); |
| 274 |
|
| 275 |
// Define contextual content. |
| 276 |
$content = array( |
| 277 |
'no_donations_or_forms' => array( |
| 278 |
'heading' => __( 'No donations found.', 'give' ), |
| 279 |
'message' => __( 'Your donation history will appear here, but first, you need a donation form!', 'give' ), |
| 280 |
), |
| 281 |
'no_donations' => array( |
| 282 |
'heading' => __( 'No donations found.', 'give' ), |
| 283 |
'message' => __( 'When your first donation arrives, a record of the donation will appear here.', 'give' ), |
| 284 |
'cta_text' => __( 'View All Forms', 'give' ), |
| 285 |
'cta_link' => DonationFormsAdminPage::getUrl(), |
| 286 |
'help' => sprintf( |
| 287 |
/* translators: 1: Opening anchor tag. 2: Closing anchor tag. */ |
| 288 |
__( 'Need help? Learn more about %1$sDonations%2$s.', 'give' ), |
| 289 |
'<a href="http://docs.givewp.com/core-donations/">', |
| 290 |
'</a>' |
| 291 |
), |
| 292 |
), |
| 293 |
'no_donors_or_forms' => array( |
| 294 |
'heading' => __( 'No donors found.', 'give' ), |
| 295 |
'message' => __( 'Your donor history will appear here, but first, you need a donation form!', 'give' ), |
| 296 |
), |
| 297 |
'no_donors' => array( |
| 298 |
'heading' => __( 'No donors found.', 'give' ), |
| 299 |
'message' => __( 'When your first donation arrives, the donor will appear here.', 'give' ), |
| 300 |
'cta_text' => __( 'View All Forms', 'give' ), |
| 301 |
'cta_link' => DonationFormsAdminPage::getUrl(), |
| 302 |
'help' => sprintf( |
| 303 |
/* translators: 1: Opening anchor tag. 2: Closing anchor tag. */ |
| 304 |
__( 'Need help? Learn more about %1$sDonors%2$s.', 'give' ), |
| 305 |
'<a href="http://docs.givewp.com/core-donors/">', |
| 306 |
'</a>' |
| 307 |
), |
| 308 |
), |
| 309 |
); |
| 310 |
|
| 311 |
if ( isset( $content[ $context ] ) ) { |
| 312 |
// Merge contextual content with defaults. |
| 313 |
return wp_parse_args( $content[ $context ], $defaults ); |
| 314 |
} else { |
| 315 |
// Return defaults if context is undefined. |
| 316 |
return $defaults; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|