Give_Blank_Slate::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
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
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
class Give_Blank_Slate {
17
	/**
18
	 * The current screen ID.
19
	 *
20
	 * @since  1.8.13
21
	 * @var string
22
	 * @access public
23
	 */
24
	public $screen = '';
25
26
	/**
27
	 * Whether at least one donation form exists.
28
	 *
29
	 * @since  1.8.13
30
	 * @var bool
31
	 * @access private
32
	 */
33
	private $form = false;
34
35
	/**
36
	 * Whether at least one donation exists.
37
	 *
38
	 * @since  1.8.13
39
	 * @var bool
40
	 * @access private
41
	 */
42
	private $donation = false;
43
44
	/**
45
	 * Whether at least one donor exists.
46
	 *
47
	 * @since  1.8.13
48
	 * @var bool
49
	 * @access private
50
	 */
51
	private $donor = false;
52
53
	/**
54
	 * The content of the blank slate panel.
55
	 *
56
	 * @since  1.8.13
57
	 * @var array
58
	 * @access private
59
	 */
60
	private $content = array();
61
62
	/**
63
	 * Constructs the Give_Blank_Slate class.
64
	 *
65
	 * @since 1.8.13
66
	 */
67
	public function __construct() {
68
		$this->screen = get_current_screen()->id;
69
	}
70
71
	/**
72
	 * Initializes the class and hooks into WordPress.
73
	 *
74
	 * @since 1.8.13
75
	 */
76
	public function init() {
77
		// Bail early if screen cannot be detected.
78
		if ( empty( $this->screen ) ) {
79
			return null;
80
		}
81
82
		$content = array();
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
84
		// Define content and hook into the appropriate action.
85
		switch ( $this->screen ) {
86
			// Forms screen.
87
			case 'edit-give_forms':
88
				$this->form = $this->post_exists( 'give_forms' );
89
90
				if ( $this->form ) {
91
					// Form exists. Bail out.
92
					return false;
93
				} else {
94
					// No forms exist.
95
					$content = $this->get_content( 'no_forms' );
96
				}
97
98
				add_action( 'manage_posts_extra_tablenav', array( $this, 'render' ) );
99
				break;
100
			// Donations screen.
101
			case 'give_forms_page_give-payment-history':
102
				$this->form     = $this->post_exists( 'give_forms' );
103
				$this->donation = $this->post_exists( 'give_payment' );
104
105
				if ( $this->donation ) {
106
					// Donation exists. Bail out.
107
					return false;
108
				} elseif ( ! $this->form ) {
109
					// No forms and no donations exist.
110
					$content = $this->get_content( 'no_donations_or_forms' );
111
				} else {
112
					// No donations exist but a form does exist.
113
					$content = $this->get_content( 'no_donations' );
114
				}
115
116
				add_action( 'give_payments_page_bottom', array( $this, 'render' ) );
117
				break;
118
			// Donors screen.
119
			case 'give_forms_page_give-donors':
120
				$this->form  = $this->post_exists( 'give_forms' );
121
				$this->donor = $this->donor_exists();
122
123
				if ( $this->donor ) {
124
					// Donor exists. Bail out.
125
					return false;
126
				} elseif ( ! $this->form ) {
127
					// No forms and no donors exist.
128
					$content = $this->get_content( 'no_donors_or_forms' );
129
				} else {
130
					// No donors exist but a form does exist.
131
					$content = $this->get_content( 'no_donors' );
132
				}
133
134
				add_action( 'give_donors_table_bottom', array( $this, 'render' ) );
135
				break;
136
			default:
137
				return null;
138
		}
139
140
		$this->content = $content;
141
142
		// Hide non-essential UI elements.
143
		add_action( 'admin_head', array( $this, 'hide_ui' ) );
144
	}
145
146
	/**
147
	 * Renders the blank slate message.
148
	 *
149
	 * @since 1.8.13
150
	 *
151
	 * @param string $which The location of the list table hook: 'top' or 'bottom'.
152
	 */
153
	public function render( $which = 'bottom' ) {
154
		// Bail out to prevent content from rendering twice.
155
		if ( 'top' === $which ) {
156
			return null;
157
		}
158
159
		$screen = $this->screen;
160
161
		/**
162
		 * Filters the content of the blank slate.
163
		 *
164
		 * @since 1.8.13
165
		 *
166
		 * @param array $content {
167
		 *    Array of blank slate content.
168
		 *
169
		 *    @type string $image_url URL of the blank slate image.
170
		 *    @type string $image_alt Image alt text.
171
		 *    @type string $heading   Heading text.
172
		 *    @type string $message   Body copy.
173
		 *    @type string $cta_text  Call to action text.
174
		 *    @type string $cta_link  Call to action URL.
175
		 *    @type string $help      Help text.
176
		 * }
177
		 *
178
		 * @param string $screen The current screen ID.
179
		 */
180
		$content = apply_filters( 'give_blank_slate_content', $this->content, $screen );
181
182
		$template_path = GIVE_PLUGIN_DIR . 'includes/admin/views/blank-slate.php';
183
184
		include $template_path;
185
	}
186
187
	/**
188
	 * Hides non-essential UI elements when blank slate content is on screen.
189
	 *
190
	 * @since 1.8.13
191
	 */
192
	function hide_ui() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
193
		?>
194
		<style type="text/css">
195
			.give-filters,
196
			.search-box,
197
			.subsubsub,
198
			.wp-list-table,
199
			.tablenav.top,
200
			.give_forms_page_give-payment-history .tablenav.bottom,
201
			.give_forms_page_give-donors .tablenav.bottom,
202
			.tablenav-pages {
203
				display: none;
204
			}
205
		</style>
206
		<?php
207
	}
208
209
	/**
210
	 * Determines if at least one post of a given post type exists.
211
	 *
212
	 * @since 1.8.13
213
	 *
214
	 * @param string $post_type Post type used in the query.
215
	 * @return bool True if post exists, otherwise false.
216
	 */
217
	private function post_exists( $post_type ) {
218
		// Attempt to get a single post of the post type.
219
		$query = new WP_Query( array(
220
			'post_type'              => $post_type,
221
			'posts_per_page'         => 1,
222
			'no_found_rows'          => false,
223
			'update_post_meta_cache' => false,
224
			'update_post_term_cache' => false,
225
			'fields'                 => 'ids',
226
			'post_status'            => array( 'any', 'trash' ),
227
		) );
228
229
		return $query->have_posts();
230
	}
231
232
	/**
233
	 * Determines if at least one donor exists.
234
	 *
235
	 * @since 1.8.13
236
	 *
237
	 * @return bool True if donor exists, otherwise false.
238
	 */
239
	private function donor_exists() {
240
		$donors = Give()->donors->get_donors( array( 'number' => 1 ) );
241
242
		return ! empty( $donors );
243
	}
244
245
	/**
246
	 * Gets the content of a blank slate message based on provided context.
247
	 *
248
	 * @since 1.8.13
249
	 *
250
	 * @param string $context The key used to determine which content is returned.
251
	 * @return array Blank slate content.
252
	 */
253
	private function get_content( $context ) {
254
		// Define default content.
255
		$defaults = array(
256
			'image_url' => GIVE_PLUGIN_URL . 'assets/dist/images/give-icon-full-circle.svg',
257
			'image_alt' => __( 'Give Icon', 'give' ),
258
			'heading'   => __( 'No donation forms found.', 'give' ),
259
			'message'   => __( 'The first step towards accepting online donations is to create a form.', 'give' ),
260
			'cta_text'  => __( 'Create Donation Form', 'give' ),
261
			'cta_link'  => admin_url( 'post-new.php?post_type=give_forms' ),
262
			'help'      => sprintf(
263
				/* translators: 1: Opening anchor tag. 2: Closing anchor tag. */
264
				__( 'Need help? Get started with %1$sGive 101%2$s.', 'give' ),
265
				'<a href="http://docs.givewp.com/give101/" target="_blank">',
266
				'</a>'
267
			),
268
		);
269
270
		// Define contextual content.
271
		$content = array(
272
			'no_donations_or_forms' => array(
273
				'heading' => __( 'No donations found.', 'give' ),
274
				'message' => __( 'Your donation history will appear here, but first, you need a donation form!', 'give' ),
275
			),
276
			'no_donations'          => array(
277
				'heading'  => __( 'No donations found.', 'give' ),
278
				'message'  => __( 'When your first donation arrives, a record of the donation will appear here.', 'give' ),
279
				'cta_text' => __( 'View All Forms', 'give' ),
280
				'cta_link' => admin_url( 'edit.php?post_type=give_forms' ),
281
				'help'     => sprintf(
282
					/* translators: 1: Opening anchor tag. 2: Closing anchor tag. */
283
					__( 'Need help? Learn more about %1$sDonations%2$s.', 'give' ),
284
					'<a href="http://docs.givewp.com/core-donations/">',
285
					'</a>'
286
				),
287
			),
288
			'no_donors_or_forms'    => array(
289
				'heading' => __( 'No donors  found.', 'give' ),
290
				'message' => __( 'Your donor history will appear here, but first, you need a donation form!', 'give' ),
291
			),
292
			'no_donors'             => array(
293
				'heading'  => __( 'No donors found.', 'give' ),
294
				'message'  => __( 'When your first donation arrives, the donor will appear here.', 'give' ),
295
				'cta_text' => __( 'View All Forms', 'give' ),
296
				'cta_link' => admin_url( 'edit.php?post_type=give_forms' ),
297
				'help'     => sprintf(
298
					/* translators: 1: Opening anchor tag. 2: Closing anchor tag. */
299
					__( 'Need help? Learn more about %1$sDonors%2$s.', 'give' ),
300
					'<a href="http://docs.givewp.com/core-donors/">',
301
					'</a>'
302
				),
303
			),
304
		);
305
306
		if ( isset( $content[ $context ] ) ) {
307
			// Merge contextual content with defaults.
308
			return wp_parse_args( $content[ $context ], $defaults );
309
		} else {
310
			// Return defaults if context is undefined.
311
			return $defaults;
312
		}
313
	}
314
}
315