Completed
Push — master ( fec9e0...63ebd4 )
by Sudar
01:40
created

update_capabilities_for_user_roles()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 9
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 17
ccs 0
cts 14
cp 0
crap 30
rs 8.8571
1
<?php namespace EmailLog\Core\Request;
2
3
use EmailLog\Core\Loadie;
4
use EmailLog\Core\UI\Page\LogListPage;
5
6
/**
7
 * Actions performed in Log List.
8
 *
9
 * @since 2.0.0
10
 */
11
class LogListAction implements Loadie {
12
13
	/**
14
	 * Setup actions.
15
	 *
16
	 * @inheritdoc
17
	 */
18
	public function load() {
19
		add_action( 'wp_ajax_el-log-list-view-message', array( $this, 'view_log_message' ) );
20
21
		add_action( 'el-log-list-delete', array( $this, 'delete_logs' ) );
22
		add_action( 'el-log-list-delete-all', array( $this, 'delete_all_logs' ) );
23
		add_action( 'el-log-list-manage-user-roles-changed', array( $this, 'update_capabilities_for_user_roles' ), 10, 2 );
24
	}
25
26
	/**
27
	 * AJAX callback for displaying email content.
28
	 *
29
	 * @since 1.6
30
	 */
31
	public function view_log_message() {
32
		if ( ! current_user_can( LogListPage::CAPABILITY ) ) {
33
			wp_die();
34
		}
35
36
		$id = absint( $_GET['log_id'] );
37
38
		if ( $id <= 0 ) {
39
			wp_die();
40
		}
41
42
		$log_items = $this->get_table_manager()->fetch_log_items_by_id( array( $id ) );
43
		if ( count( $log_items ) > 0 ) {
44
			$log_item = $log_items[0];
45
46
			ob_start();
47
			?>
48
			<table style="width: 100%;">
49
				<tr style="background: #eee;">
50
					<td style="padding: 5px;"><?php _e( 'Sent at', 'email-log' ); ?>:</td>
51
					<td style="padding: 5px;"><?php echo $log_item['sent_date'] ?></td>
52
				</tr>
53
				<tr style="background: #eee;">
54
					<td style="padding: 5px;"><?php _e( 'To', 'email-log' ); ?>:</td>
55
					<td style="padding: 5px;"><?php echo $log_item['to_email'] ?></td>
56
				</tr>
57
				<tr style="background: #eee;">
58
					<td style="padding: 5px;"><?php _e( 'Subject', 'email-log' ); ?>:</td>
59
					<td style="padding: 5px;"><?php echo $log_item['subject'] ?></td>
60
				</tr>
61
62
				<?php
63
			   /**
64
				* After the headers are displayed in the View Message thickbox.
65
				* This action can be used to add additional headers.
66
				*
67
				* @since 2.0.0
68
				*
69
				* @param array $log_item Log item that is getting rendered.
70
				*/
71
				do_action( 'el_view_log_after_headers', $log_item );
72
				?>
73
74
			</table>
75
76
			<div id="tabs">
77
				<ul>
78
					<li><a href="#tabs-1"><?php _e( 'HTML', 'email-log' ); ?></a></li>
79
					<li><a href="#tabs-2"><?php _e( 'Text', 'email-log' ); ?></a></li>
80
				</ul>
81
				<div id="tabs-1">
82
					<?php echo $log_item['message']; ?>
83
				</div>
84
				<div id="tabs-2">
85
					<textarea class="tabs-text-textarea"><?php echo esc_textarea( $log_item['message'] ); ?></textarea>
86
				</div>
87
			</div>
88
89
			<div id="view-message-footer">
90
				<a href="#" id="thickbox-footer-close"><?php _e( 'Close', 'email-log' ); ?></a>
91
			</div>
92
93
			<?php
94
			$output = ob_get_clean();
95
			echo $output;
96
		}
97
98
		wp_die(); // this is required to return a proper result.
99
	}
100
101
	/**
102
	 * Delete log entries by id.
103
	 *
104
	 * @param array $data Request data.
105
	 */
106
	public function delete_logs( $data ) {
107
		$ids = $data['email-log'];
108
109
		if ( ! is_array( $ids ) ) {
110
			$ids = array( $ids );
111
		}
112
113
		$ids = array_map( 'absint', $ids );
114
		$id_list = implode( ',', $ids );
115
116
		$logs_deleted = $this->get_table_manager()->delete_logs( $id_list );
117
		$this->render_log_deleted_notice( $logs_deleted );
118
	}
119
120
	/**
121
	 * Delete all log entries.
122
	 */
123
	public function delete_all_logs() {
124
		$logs_deleted = $this->get_table_manager()->delete_all_logs();
125
		$this->render_log_deleted_notice( $logs_deleted );
126
	}
127
128
	/**
129
	 * Update user role capabilities when the allowed user role list is changed.
130
	 *
131
	 * The capability will be removed from old user roles and added to new user roles.
132
	 *
133
	 * @since 2.1.0
134
	 *
135
	 * @param array $old_roles Old user roles.
136
	 * @param array $new_roles New user roles.
137
	 */
138
	public function update_capabilities_for_user_roles( $old_roles, $new_roles ) {
139
		foreach ( $old_roles as $old_role ) {
140
			$role = get_role( $old_role );
141
142
			if ( ! is_null( $role ) ) {
143
				$role->remove_cap( LogListPage::CAPABILITY );
144
			}
145
		}
146
147
		foreach ( $new_roles as $new_role ) {
148
			$role = get_role( $new_role );
149
150
			if ( ! is_null( $role ) ) {
151
				$role->add_cap( LogListPage::CAPABILITY );
152
			}
153
		}
154
	}
155
156
	/**
157
	 * Render Logs deleted notice.
158
	 *
159
	 * @param int|False $logs_deleted Number of entries deleted, False otherwise.
160
	 */
161
	protected function render_log_deleted_notice( $logs_deleted ) {
162
		$message = __( 'There was some problem in deleting the email logs', 'email-log' );
163
		$type    = 'error';
164
165
		if ( absint( $logs_deleted ) > 0 ) {
166
			$message = sprintf( _n( '1 email log deleted.', '%s email logs deleted', $logs_deleted, 'email-log' ), $logs_deleted );
167
			$type    = 'updated';
168
		}
169
170
		add_settings_error(
171
			'log-list',
172
			'deleted-email-logs',
173
			$message,
174
			$type
175
		);
176
	}
177
178
	/**
179
	 * Get TableManager instance.
180
	 *
181
	 * @return \EmailLog\Core\DB\TableManager TableManager instance.
182
	 */
183
	protected function get_table_manager() {
184
		$email_log = email_log();
185
186
		return $email_log->table_manager;
187
	}
188
}
189