Completed
Push — issues/1796 ( 616c54 )
by Ravinder
20:12
created

Give_Email_Notification_Table::column_name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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