Completed
Push — issues/1132 ( 716ec5 )
by Ravinder
40:09 queued 19:53
created

Give_Email_Notification_Table::get_columns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Email Notification
5
 *
6
 * This class handles table html  for email notifications listing.
7
 *
8
 * @package     Give
9
 * @subpackage  Classes/Emails
10
 * @copyright   Copyright (c) 2016, WordImpress
11
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
12
 * @since       2.0
13
 */
14
class Give_Email_Notification_Table extends WP_List_Table {
15
	/**
16
	 * @var Give_Email_Notifications $email_notifications
17
	 * @since  2.0
18
	 * @access private
19
	 */
20
	private $email_notifications;
21
22
23
	/**
24
	 * Number of email notifications per page
25
	 *
26
	 * @since  2.0
27
	 * @access private
28
	 * @var int
29
	 */
30
	private $per_page = - 1;
31
32
	/**
33
	 * Give_Email_Notification_Table constructor.
34
	 *
35
	 * @since  2.0
36
	 * @access public
37
	 */
38
	public function __construct() {
39
		parent::__construct( array(
40
			'singular' => __( 'Give Email Notification', 'give' ),
41
			'plural'   => __( 'Give Email Notifications', 'give' ),
42
		) );
43
44
		$this->email_notifications = Give_Email_Notifications::get_instance();
45
	}
46
47
48
	/**
49
	 * Get table columns.
50
	 *
51
	 * @since  2.0
52
	 * @access public
53
	 *
54
	 * @return array
55
	 */
56
	public function get_columns() {
57
		/**
58
		 * Filter the table columns
59
		 *
60
		 * @since 2.0
61
		 */
62
		return apply_filters( 'give_email_notification_setting_columns', array(
63
			'cb'         => __( 'Email Status', 'give' ),
64
			'name'       => __( 'Email', 'give' ),
65
			'email_type' => __( 'Content Type', 'give' ),
66
			'recipient'  => __( 'Recipient(s)', 'give' ),
67
			'setting'    => __( 'Edit Email', 'give' ),
68
		) );
69
	}
70
71
	/**
72
	 * Get name column.
73
	 *
74
	 * @since  2.0
75
	 * @access public
76
	 *
77
	 * @param Give_Email_Notification $email
78
	 *
79
	 * @return  string
80
	 */
81
	public function column_name( $email ) {
82
		$edit_url = esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=emails&section=' . $email->config['id'] ) );
83
		$actions  = $this->get_row_actions( $email );
84
85
		ob_start();
86
		?>
87
		<a class="row-title" href="<?php echo $edit_url; ?>"><?php echo $email->config['label']; ?></a>
88
89
		<?php if ( $desc = $email->config['description'] ) : ?>
90
			<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php echo esc_attr( $desc ); ?>"></span>
91
		<?php endif; ?>
92
93
		<?php echo $this->row_actions( $actions ); ?>
94
		<?php
95
		return ob_get_clean();
96
	}
97
98
	/**
99
	 * Get recipient column.
100
	 *
101
	 * @since  2.0
102
	 * @access public
103
	 *
104
	 * @param Give_Email_Notification $email
105
	 *
106
	 * @return string
107
	 */
108
	public function column_recipient( $email ) {
109
		ob_start();
110
111
		if( Give_Email_Notification_Util::has_recipient_field( $email ) ) {
112
			$recipients = $email->get_recipient();
113
			if ( is_array( $recipients ) ) {
114
				$recipients = implode( '<br>', $recipients );
115
			}
116
117
			echo $recipients;
118
119
		} elseif ( ! empty( $email->config['recipient_group_name'] ) ) {
120
			echo $email->config['recipient_group_name'];
121
		}
122
123
		return ob_get_clean();
124
	}
125
126
	/**
127
	 * Get status column.
128
	 *
129
	 * @since  2.0
130
	 * @access public
131
	 *
132
	 * @param Give_Email_Notification $email
133
	 *
134
	 * @return string
135
	 */
136
	public function column_cb( $email ) {
137
		ob_start();
138
139
		$notification_status = $email->get_notification_status();
140
		$default_class       = "give-email-notification-status give-email-notification-{$notification_status} dashicons";
141
		$attributes['class'] = Give_Email_Notification_Util::is_email_notification_active( $email )
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
142
			? "{$default_class} dashicons-yes"
143
			: "{$default_class} dashicons-no-alt";
144
145
		$attributes['data-status'] = "{$notification_status}";
146
		$attributes['data-id']     = "{$email->config['id']}";
147
148
		$attributes['data-edit'] = (int) Give_Email_Notification_Util::is_notification_status_editable( $email );
149
150
		if ( ! $attributes['data-edit'] ) {
151
			$attributes['data-tooltip'] = $email->config['notices']['non-notification-status-editable'];
152
		}
153
154
		$attribute_str = '';
155
		foreach ( $attributes as $tag => $value ) {
156
			$attribute_str .= " {$tag}=\"{$value}\"";
157
		}
158
		?>
159
		<span<?php echo $attribute_str; ?>></span><span class="spinner"></span>
160
		<?php
161
162
		return ob_get_clean();
163
	}
164
165
166
	/**
167
	 * Get email_type column.
168
	 *
169
	 * @since  2.0
170
	 * @access public
171
	 *
172
	 * @param Give_Email_Notification $email
173
	 *
174
	 * @return string
175
	 */
176
	public function column_email_type( Give_Email_Notification $email ) {
177
		return Give_Email_Notification_Util::get_formatted_email_type( $email->config['content_type'] );
178
	}
179
180
	/**
181
	 * Get setting column.
182
	 *
183
	 * @since  2.0
184
	 * @access public
185
	 *
186
	 * @param Give_Email_Notification $email
187
	 *
188
	 * @return string
189
	 */
190
	public function column_setting( Give_Email_Notification $email ) {
191
		ob_start();
192
		?>
193
		<a class="button button-small" data-tooltip="<?php echo __( 'Edit', 'give' ); ?> <?php echo $email->config['label']; ?>" href="<?php echo esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=emails&section=' . $email->config['id'] ) ); ?>"><span class="dashicons dashicons-admin-generic"></span></a>
194
		<?php
195
		return ob_get_clean();
196
	}
197
198
199
	/**
200
	 * Print row actions.
201
	 *
202
	 * @since  2.0
203
	 * @access private
204
	 *
205
	 * @param Give_Email_Notification $email
206
	 *
207
	 * @return array
208
	 */
209
	private function get_row_actions( $email ) {
210
		$edit_url = esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=emails&section=' . $email->config['id'] ) );
211
212
		/**
213
		 * Filter the row actions
214
		 *
215
		 * @since 2.0
216
		 *
217
		 * @param array $row_actions
218
		 */
219
		$row_actions = apply_filters(
220
			'give_email_notification_row_actions',
221
			array(
222
				'edit' => "<a href=\"{$edit_url}\">" . __( 'Edit', 'give' ) . '</a>',
223
			),
224
			$email
225
		);
226
227
		return $row_actions;
228
	}
229
230
231
	/**
232
	 * Prepare email notifications
233
	 *
234
	 * @since  2.0
235
	 * @access public
236
	 */
237
	public function prepare_items() {
238
		// Set columns.
239
		$columns               = $this->get_columns();
240
		$hidden                = array();
241
		$email_notifications   = array();
242
		$sortable              = $this->get_sortable_columns();
243
		$this->_column_headers = array( $columns, $hidden, $sortable, $this->get_primary_column_name() );
244
245
		// Set email notifications.
246
		/* @var Give_Email_Notification $email_notification */
247
		foreach ( $this->email_notifications->get_email_notifications() as $email_notification ) {
248
			if ( Give_Email_Notification_Util::is_show_on_emails_setting_page( $email_notification ) ) {
249
				$email_notifications[] = $email_notification;
250
			}
251
		}
252
253
		$totalItems  = count( $email_notifications );
254
		$this->items = $email_notifications;
255
		$this->set_pagination_args( array(
256
			'total_items' => $totalItems,
257
			'per_page'    => $this->per_page,
258
		) );
259
	}
260
261
	/**
262
	 * Message to be displayed when there are no items
263
	 *
264
	 * @since  2.0
265
	 * @access public
266
	 */
267
	public function no_items() {
268
		_e( 'No give email notification found.' );
269
	}
270
271
	/**
272
	 * Get primary column.
273
	 *
274
	 * @since  2,0
275
	 * @access public
276
	 *
277
	 * @return string Name of the default primary column.
278
	 */
279
	public function get_primary_column_name() {
280
		return 'name';
281
	}
282
}
283