Test Failed
Push — master ( 339236...7fe983 )
by Devin
09:29 queued 04:18
created

Give_Blank_Slate::init()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 69
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 39
nc 10
nop 0
dl 0
loc 69
rs 6.0493
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Give Blank Slate Class
4
 *
5
 * @package     Give
6
 * @subpackage  Admin
7
 * @copyright   Copyright (c) 2017, WordImpress
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 bool
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $content of type array is incompatible with the declared type boolean of property $content.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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