GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#1088)
by John
03:25
created

Email_Service::constant()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 3
Ratio 12 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 14
c 2
b 1
f 0
nc 4
nop 0
dl 3
loc 25
rs 8.8571
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
/**
6
 * Email notifications for backups
7
 *
8
 * @extends Service
9
 */
10
class Email_Service extends Service {
11
12
	/**
13
	 * Human readable name for this service
14
	 * @var string
15
	 */
16
	public $name = 'Email';
17
18
	/**
19
	 * Output the email form field
20
	 *
21
	 * @access  public
22
	 */
23
	public function field() {
24
25
	?>
26
27
		<tr>
28
29
			<th scope="row">
30
				<label for="<?php echo esc_attr( $this->get_field_name( 'email' ) ); ?>"><?php _e( 'Email notification', 'backupwordpress' ); ?></label>
31
			</th>
32
33
			<td>
34
				<input type="text" id="<?php echo esc_attr( $this->get_field_name( 'email' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'email' ) ); ?>" value="<?php echo esc_attr( $this->get_field_value( 'email' ) ); ?>" placeholder="[email protected]" />
35
36
				<p class="description"><?php printf( __( 'Receive a notification email when a backup completes. If the backup is small enough (&lt; %s), then it will be attached to the email. Separate multiple email addresses with a comma.', 'backupwordpress' ), '<code>' . size_format( get_max_attachment_size() ) . '</code>' ); ?></p>
37
			</td>
38
39
		</tr>
40
41
	<?php }
42
43
	/**
44
	 * Not used as we only need a field
45
	 *
46
	 * @see  field
47
	 * @return string Empty string
48
	 */
49
	public function form() {
50
		return '';
51
	}
52
53
	public static function constant() {
54
55
	?>
56
57
		<tr<?php if ( defined( 'HMBKP_ATTACHMENT_MAX_FILESIZE' ) ) { ?> class="hmbkp_active"<?php } ?>>
58
59
			<td><code>HMBKP_ATTACHMENT_MAX_FILESIZE</code></td>
60
61
			<td>
62
63 View Code Duplication
				<?php if ( defined( 'HMBKP_ATTACHMENT_MAX_FILESIZE' ) ) { ?>
64
				<p><?php printf( __( 'You\'ve set it to: %s', 'backupwordpress' ), '<code>' . esc_html( HMBKP_ATTACHMENT_MAX_FILESIZE ) . '</code>' ); ?></p>
65
				<?php } ?>
66
67
				<p><?php printf(
68
					__( 'The maximum file size of your backup that will be attached to your notification emails. Defaults to %1$s. e.g. %2$s', 'backupwordpress' ),
69
					'<code>10MB</code>',
70
					"<code>define( 'HMBKP_ATTACHMENT_MAX_FILESIZE', '25MB' );</code>"
71
				); ?></p>
72
73
			</td>
74
75
		</tr>
76
77
	<?php }
78
79
	/**
80
	 * The sentence fragment that is output as part of the schedule sentence
81
	 *
82
	 * @return string
83
	 */
84
	public function display() {
85
86
		if ( $emails = $this->get_email_address_array() ) {
87
88
			$email = '<code>' . implode( '</code>, <code>', array_map( 'esc_html', $emails ) ) . '</code>';
89
90
			return sprintf( __( 'Send an email notification to %s', 'backupwordpress' ), $email );
91
92
		}
93
94
		return '';
95
96
	}
97
98
	/**
99
	 * Used to determine if the service is in use or not
100
	 */
101
	public function is_service_active() {
102
		return (bool) $this->get_email_address_array();
103
	}
104
105
	/**
106
	 * Validate the email and return an error if validation fails
107
	 *
108
	 * @param  array  &$new_data Array of new data, passed by reference
109
	 * @param  array  $old_data  The data we are replacing
110
	 * @return null|array        Null on success, array of errors if validation failed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|null.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
111
	 */
112
	public function update( &$new_data, $old_data ) {
113
114
		$errors = array();
115
116
		if ( isset( $new_data['email'] ) ) {
117
118
			if ( ! empty( $new_data['email'] ) ) {
119
120
				foreach ( explode( ',', $new_data['email'] ) as $email ) {
121
122
					$email = trim( $email );
123
124
					if ( ! is_email( $email ) ) {
125
						$errors['email'] = sprintf( __( '%s isn\'t a valid email',  'backupwordpress' ), $email );
126
					}
127
				}
128
			}
129
130
			if ( ! empty( $errors['email'] ) ) {
131
				$new_data['email'] = '';
132
			}
133
134
			return $errors;
135
136
		}
137
	}
138
139
	/**
140
	 * Get an array or validated email address's
141
	 * @return array An array of validated email address's
142
	 */
143
	private function get_email_address_array() {
144
		$emails = array_map( 'trim', explode( ',', $this->get_field_value( 'email' ) ) );
145
		return array_filter( array_unique( $emails ), 'is_email' );
146
	}
147
148
	/**
149
	 * Fire the email notification on the hmbkp_backup_complete
150
	 *
151
	 * @see  Backup::do_action
152
	 * @param  string $action The action received from the backup
153
	 * @return void
154
	 */
155
	public function action( $action, Backup $backup ) {
156
157
		if ( 'hmbkp_backup_complete' === $action && $this->get_email_address_array() ) {
158
159
			$file = $backup->get_backup_filepath();
160
161
			$sent = false;
162
163
			$download = add_query_arg( 'hmbkp_download', base64_encode( $file ), HMBKP_ADMIN_URL );
164
			$domain   = parse_url( home_url(), PHP_URL_HOST ) . parse_url( home_url(), PHP_URL_PATH );
165
166
			$headers  = 'From: BackUpWordPress <' . apply_filters( 'hmbkp_from_email', get_bloginfo( 'admin_email' ) ) . '>' . "\r\n";
167
168
			// The backup failed, send a message saying as much
169
			if ( ! file_exists( $file ) && ( $errors = array_merge( $backup->get_errors(), $backup->get_warnings() ) ) ) {
170
171
				$error_message = '';
172
173
				foreach ( $errors as $error_set ) {
174
					$error_message .= implode( "\n - ", $error_set );
175
				}
176
177
				if ( $error_message ) {
178
					$error_message = ' - ' . $error_message;
179
				}
180
181
				$subject = sprintf( __( 'Backup of %s Failed', 'backupwordpress' ), $domain );
182
183
				$message = sprintf( __( 'BackUpWordPress was unable to backup your site %1$s.', 'backupwordpress' ) . "\n\n" . __( 'Here are the errors that we\'ve encountered:', 'backupwordpress' ) . "\n\n" . '%2$s' . "\n\n" . __( 'If the errors above look like Martian, forward this email to %3$s and we\'ll take a look', 'backupwordpress' ) . "\n\n" . __( "Kind Regards,\nThe Apologetic BackUpWordPress Backup Emailing Robot", 'backupwordpress' ), home_url(), $error_message, '[email protected]' );
184
185
				wp_mail( $this->get_email_address_array(), $subject, $message, $headers );
186
187
				return;
188
189
			}
190
191
			$subject = sprintf( __( 'Backup of %s', 'backupwordpress' ), $domain );
192
193
			// If it's larger than the max attachment size limit assume it's not going to be able to send the backup
194
			if ( @filesize( $file ) < get_max_attachment_size() ) {
195
196
				$message = sprintf( __( 'BackUpWordPress has completed a backup of your site %1$s.', 'backupwordpress' ) . "\n\n" . __( 'The backup file should be attached to this email.', 'backupwordpress' ) . "\n\n" . __( 'You can download the backup file by clicking the link below:', 'backupwordpress' ) . "\n\n" . '%2$s' . "\n\n" . __( "Kind Regards,\nThe Happy BackUpWordPress Backup Emailing Robot", 'backupwordpress' ), home_url(),  $download );
197
198
				$sent = wp_mail( $this->get_email_address_array(), $subject, $message, $headers, $file );
199
200
			}
201
202
			// If we didn't send the email above then send just the notification
203
			if ( ! $sent ) {
204
205
				$message = sprintf( __( 'BackUpWordPress has completed a backup of your site %1$s.', 'backupwordpress' ) . "\n\n" . __( 'Unfortunately, the backup file was too large to attach to this email.', 'backupwordpress' ) . "\n\n" . __( 'You can download the backup file by clicking the link below:', 'backupwordpress' ) . "\n\n" . '%2$s' . "\n\n" . __( "Kind Regards,\nThe Happy BackUpWordPress Backup Emailing Robot", 'backupwordpress' ), home_url(), $download );
206
				wp_mail( $this->get_email_address_array(), $subject, $message, $headers );
207
208
			}
209
		}
210
	}
211
212
	public static function intercom_data() {
213
		return array();
214
	}
215
216
	public static function intercom_data_html() {}
217
}
218
219
// Register the service
220
Services::register( __FILE__, 'HM\BackUpWordPress\Email_Service' );
221