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 (#909)
by Paul
02:29
created

Plugin::display_feature_message()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.5806
cc 4
eloc 16
nc 5
nop 0
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
/**
6
 * Class Plugin
7
 */
8
final class Plugin {
9
	const PLUGIN_VERSION = '3.5';
10
11
	/**
12
	 * @var Plugin The singleton instance.
13
	 */
14
	private static $instance;
15
16
	/**
17
	 * Instantiates a new Plugin object.
18
	 */
19
	private function __construct() {
20
		add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
21
22
		$hide_notice = get_site_option( 'hmbkp_hide_info_notice', false );
23
24
		if ( ! $hide_notice ) {
25
			add_action( 'admin_notices', array( $this, 'display_feature_message' ) );
26
			add_action( 'network_admin_notices', array( $this, 'display_feature_message' ) );
27
		}
28
29
	}
30
31
	/**
32
	 * Insures we always return the same object.
33
	 *
34
	 * @return Plugin
35
	 */
36
	public static function get_instance() {
37
38
		if ( ! ( self::$instance instanceof Plugin ) ) {
39
			self::$instance = new Plugin();
40
		}
41
42
		return self::$instance;
43
	}
44
45
	/**
46
	 * Initialize the plugin.
47
	 */
48
	public function plugins_loaded() {
49
50
		if ( true !== $this->maybe_self_deactivate() ) {
51
52
			$this->constants();
53
54
			$this->includes();
55
56
			$this->hooks();
57
58
			$this->text_domain();
59
60
			// If we get here, then BWP is loaded
61
			do_action( 'backupwordpress_loaded' );
62
63
		}
64
65
	}
66
67
	/**
68
	 * Check plugin requirements.
69
	 *
70
	 * @return bool True is fails requirements. False otherwise.
71
	 */
72
	public function maybe_self_deactivate() {
73
74
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-setup.php' );
75
76 View Code Duplication
		if ( false === \HMBKP_Setup::meets_requirements() ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
78
			add_action( 'admin_init', array( '\HMBKP_Setup', 'self_deactivate' ) );
79
80
			add_action( 'all_admin_notices', array( '\HMBKP_Setup', 'display_admin_notices' ) );
81
82
			return true;
83
84
		}
85
86
		return false;
87
88
	}
89
90
	/**
91
	 * Define all the constants.
92
	 */
93
	public function constants() {
94
95
		if ( ! defined( 'HMBKP_PLUGIN_SLUG' ) ) {
96
			define( 'HMBKP_PLUGIN_SLUG', dirname( HMBKP_BASENAME ) );
97
		}
98
99
		if ( ! defined( 'HMBKP_PLUGIN_URL' ) ) {
100
			define( 'HMBKP_PLUGIN_URL', plugin_dir_url( HMBKP_BASENAME ) );
101
		}
102
103
		if ( ! defined( 'HMBKP_PLUGIN_LANG_DIR' ) ) {
104
			define( 'HMBKP_PLUGIN_LANG_DIR', apply_filters( 'hmbkp_filter_lang_dir', HMBKP_PLUGIN_SLUG . '/languages/' ) );
105
		}
106
107
		if ( ! defined( 'HMBKP_ADMIN_URL' ) ) {
108
			$page = is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' );
109
			define( 'HMBKP_ADMIN_URL', add_query_arg( 'page', HMBKP_PLUGIN_SLUG, $page ) );
110
		}
111
112
		if ( ! defined( 'HMBKP_ADMIN_PAGE' ) ) {
113
			$prefix = is_multisite() ? 'settings_page_' : 'tools_page_';
114
115
			define( 'HMBKP_ADMIN_PAGE', $prefix . HMBKP_PLUGIN_SLUG );
116
		}
117
118
		define( 'HMBKP_SECURE_KEY', $this->generate_key() );
119
120
	}
121
122
	/**
123
	 * Load all BackUpWordPress functions.
124
	 */
125
	protected function includes() {
126
127
		require_once( HMBKP_PLUGIN_PATH . 'vendor/autoload.php' );
128
129
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-notices.php' );
130
131
		// Load the admin menu
132
		require_once( HMBKP_PLUGIN_PATH . 'admin/menu.php' );
133
		require_once( HMBKP_PLUGIN_PATH . 'admin/actions.php' );
134
135
		// Load Backdrop if necessary.
136
		if ( ! class_exists( 'HM_Backdrop_Task' ) ) {
137
			require_once( HMBKP_PLUGIN_PATH . 'backdrop/hm-backdrop.php' );
138
		}
139
140
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-requirements.php' );
141
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-requirement.php' );
142
143
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-path.php' );
144
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-excludes.php' );
145
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-site-size.php' );
146
147
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-utilities.php' );
148
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-status.php' );
149
150
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine.php' );
151
152
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database.php' );
153
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database-mysqldump.php' );
154
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database-imysqldump.php' );
155
156
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file.php' );
157
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file-zip.php' );
158
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file-zip-archive.php' );
159
160
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup.php' );
161
162
		// Load the backup scheduling classes
163
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-scheduled-backup.php' );
164
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-schedules.php' );
165
166
		// Load the core functions
167
		require_once( HMBKP_PLUGIN_PATH . 'functions/core.php' );
168
		require_once( HMBKP_PLUGIN_PATH . 'functions/interface.php' );
169
170
		// Load the services
171
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-services.php' );
172
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-service.php' );
173
174
		// Load the email service
175
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-email-service.php' );
176
177
		// Load the webhook services
178
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-webhook-service.php' );
179
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-wpremote-webhook-service.php' );
180
181
		require_once( HMBKP_PLUGIN_PATH . 'classes/deprecated.php' );
182
183
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-extensions.php' );
184
185
		// Load the wp cli command
186
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
187
			include( HMBKP_PLUGIN_PATH . 'classes/class-backupwordpress-wp-cli-command.php' );
188
		}
189
190
	}
191
192
	/**
193
	 * Hook into WordPress page lifecycle and execute BackUpWordPress functions.
194
	 */
195
	public function hooks() {
196
197
		add_action( 'activated_plugin', array( $this, 'load_first' ) );
198
199
		add_action( 'admin_init', array( $this, 'upgrade' ) );
200
201
		add_action( 'admin_init', array( $this, 'init' ) );
202
203
		add_action( 'hmbkp_schedule_hook', array( $this, 'schedule_hook_run' ) );
204
205
		add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
206
207
		add_action( 'admin_footer-' . HMBKP_ADMIN_PAGE, array( $this, 'load_intercom_script' ) );
208
209
		add_action( 'admin_enqueue_scripts', array( $this, 'styles' ) );
210
211
	}
212
213
	/**
214
	 * Load the Javascript in the admin.
215
	 *
216
	 * @param $hook The name of the admin page hook.
217
	 */
218
	public function scripts( $hook ) {
219
220
		if ( HMBKP_ADMIN_PAGE !== $hook ) {
221
			return;
222
		}
223
224
		$js_file = HMBKP_PLUGIN_URL . 'assets/hmbkp.min.js';
225
226
		// TODO shuold this also support WP_SCRIPT_DEBUG
227
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
228
			$js_file = HMBKP_PLUGIN_URL . 'assets/hmbkp.js';
229
		}
230
231
		wp_enqueue_script( 'hmbkp', $js_file, array( 'heartbeat' ), sanitize_key( self::PLUGIN_VERSION ) );
232
233
		wp_localize_script(
234
			'hmbkp',
235
			'hmbkp',
236
			array(
237
				'page_slug'                => HMBKP_PLUGIN_SLUG,
238
				'nonce'                    => wp_create_nonce( 'hmbkp_nonce' ),
239
				'hmbkp_run_schedule_nonce' => wp_create_nonce( 'hmbkp_run_schedule' ),
240
				'update'                   => __( 'Update', 'backupwordpress' ),
241
				'cancel'                   => __( 'Cancel', 'backupwordpress' ),
242
				'delete_schedule'          => __( 'Are you sure you want to delete this schedule? All of its backups will also be deleted.', 'backupwordpress' ) . "\n\n" . __( '\'Cancel\' to go back, \'OK\' to delete.', 'backupwordpress' ) . "\n",
243
				'delete_backup'            => __( 'Are you sure you want to delete this backup?', 'backupwordpress' ) . "\n\n" . __( '\'Cancel\' to go back, \'OK\' to delete.', 'backupwordpress' ) . "\n",
244
				'remove_exclude_rule'      => __( 'Are you sure you want to remove this exclude rule?', 'backupwordpress' ) . "\n\n" . __( '\'Cancel\' to go back, \'OK\' to delete.', 'backupwordpress' ) . "\n",
245
				'remove_old_backups'       => __( 'Reducing the number of backups that are stored on this server will cause some of your existing backups to be deleted. Are you sure that\'s what you want?', 'backupwordpress' ) . "\n\n" . __( '\'Cancel\' to go back, \'OK\' to delete.', 'backupwordpress' ) . "\n"
246
			)
247
		);
248
249
	}
250
251
	/**
252
	 * Loads the plugin text domain for translation.
253
	 * This setup allows a user to just drop his custom translation files into the WordPress language directory
254
	 * Files will need to be in a subdirectory with the name of the textdomain 'backupwordpress'
255
	 */
256
	public function text_domain() {
257
258
		// Set unique textdomain string
259
		$textdomain = 'backupwordpress';
260
261
		// The 'plugin_locale' filter is also used by default in load_plugin_textdomain()
262
		$locale = apply_filters( 'plugin_locale', get_locale(), $textdomain );
263
264
		// Set filter for WordPress languages directory
265
		$hmbkp_wp_lang_dir = apply_filters( 'hmbkp_do_filter_wp_lang_dir', trailingslashit( WP_LANG_DIR ) . trailingslashit( $textdomain ) . $textdomain . '-' . $locale . '.mo' );
266
267
		// Translations: First, look in WordPress' "languages" folder = custom & update-secure!
268
		load_textdomain( $textdomain, $hmbkp_wp_lang_dir );
269
270
		// Translations: Secondly, look in plugin's "languages" folder = default
271
		load_plugin_textdomain( $textdomain, false, HMBKP_PLUGIN_LANG_DIR );
272
273
	}
274
275
	/**
276
	 * Determine if we need to run an upgrade routine.
277
	 */
278
	public function upgrade() {
279
280
		// Fire the update action
281
		if ( self::PLUGIN_VERSION != get_option( 'hmbkp_plugin_version' ) ) {
282
			update();
283
		}
284
285
	}
286
287
	/**
288
	 * Runs on every admin page load
289
	 */
290
	public function init() {
291
292
		// If we have multiple paths for some reason then clean them up
293
		Path::get_instance()->merge_existing_paths();
294
	}
295
296
	/**
297
	 * Generate a unique key.
298
	 *
299
	 * @return string
300
	 */
301
	protected function generate_key() {
302
303
		$check = apply_filters( "hmbkp_generate_key", null );
304
		if ( null !== $check )
305
			return $check;
306
307
		$key = array( ABSPATH, time() );
308
		$constants = array( 'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT', 'SECRET_KEY' );
309
310
		foreach ( $constants as $constant ) {
311
			if ( defined( $constant ) ) {
312
				$key[] = constant( $constant );
313
			}
314
		}
315
316
		shuffle( $key );
317
318
		return md5( serialize( $key ) );
319
320
	}
321
322
	/**
323
	 * Ensure BackUpWordPress is loaded before add-ons, changes the order of the serialized values in the DB field.
324
	 */
325
	public function load_first() {
326
327
		$active_plugins = get_option( 'active_plugins' );
328
329
		$plugin_path = plugin_basename( __FILE__ );
330
331
		$key = array_search( $plugin_path, $active_plugins );
332
333
		if ( $key > 0 ) {
334
335
			array_splice( $active_plugins, $key, 1 );
336
337
			array_unshift( $active_plugins, $plugin_path );
338
339
			update_option( 'active_plugins', $active_plugins );
340
341
		}
342
343
	}
344
345
	/**
346
	 * Function to run when the schedule cron fires.
347
	 *
348
	 * @param $schedule_id
349
	 */
350
	public function schedule_hook_run( $schedule_id ) {
351
352
		if ( ! is_backup_possible() ) {
353
			return;
354
		}
355
356
		$schedules = Schedules::get_instance();
357
		$schedule  = $schedules->get_schedule( $schedule_id );
358
359
		if ( ! $schedule ) {
360
			return;
361
		}
362
363
		$schedule->run();
364
365
	}
366
367
	/**
368
	 * Enqueue the plugin styles.
369
	 *
370
	 * @param $hook
371
	 */
372
	public function styles( $hook ) {
373
374
		if ( HMBKP_ADMIN_PAGE !== $hook ) {
375
			return;
376
		}
377
378
		$css_file = HMBKP_PLUGIN_URL . 'assets/hmbkp.min.css';
379
380
		if ( WP_DEBUG ) {
381
			$css_file = HMBKP_PLUGIN_URL . 'assets/hmbkp.css';
382
		}
383
384
		wp_enqueue_style( 'backupwordpress', $css_file, false, sanitize_key( self::PLUGIN_VERSION ) );
385
386
	}
387
388
	/**
389
	 * Load Intercom and send across user information and server info. Only loaded if the user has opted in.
390
	 *
391
	 * @param $hook
392
	 */
393
	public function load_intercom_script() {
394
395
		if ( ! get_option( 'hmbkp_enable_support' ) ) {
396
			return;
397
		}
398
399
		$info = array();
400
401
		foreach ( Requirements::get_requirement_groups() as $group ) {
402
			foreach ( Requirements::get_requirements( $group ) as $requirement ) {
0 ignored issues
show
Documentation introduced by
$group is of type integer|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
403
				$info[ $requirement->name() ] = $requirement->result();
404
			}
405
		}
406
407
		foreach ( Services::get_services() as $file => $service ) {
408
			array_merge( $info, call_user_func( array( $service, 'intercom_data' ) ) );
409
		}
410
411
		$current_user = wp_get_current_user();
412
413
		$info['user_hash']  = hash_hmac( 'sha256', $current_user->user_email, 'fcUEt7Vi4ym5PXdcr2UNpGdgZTEvxX9NJl8YBTxK' );
414
		$info['email']      = $current_user->user_email;
415
		$info['created_at'] = strtotime( $current_user->user_registered );
416
		$info['app_id']     = '7f1l4qyq';
417
		$info['name']       = $current_user->display_name;
418
		$info['widget']     = array( 'activator' => '#intercom' ); ?>
419
420
		<script id="IntercomSettingsScriptTag">
421
			window.intercomSettings = <?php echo json_encode( $info ); ?>;
422
		</script>
423
		<script>!function(){function e(){var a=c.createElement("script");a.type="text/javascript",a.async=!0,a.src="https://static.intercomcdn.com/intercom.v1.js";var b=c.getElementsByTagName("script")[0];b.parentNode.insertBefore(a,b)}var a=window,b=a.Intercom;if("function"==typeof b)b("reattach_activator"),b("update",intercomSettings);else{var c=document,d=function(){d.c(arguments)};d.q=[],d.c=function(a){d.q.push(a)},a.Intercom=d,a.attachEvent?a.attachEvent("onload",e):a.addEventListener("load",e,!1)}}();</script>
424
425
	<?php }
426
427
	public function display_feature_message() {
428
429
		$current_screen = get_current_screen();
430
431
		if ( ! isset( $current_screen ) ) {
432
			return;
433
		}
434
435
		$page = is_multisite() ? HMBKP_ADMIN_PAGE . '-network' : HMBKP_ADMIN_PAGE;
436
		if ( $current_screen->id !== $page ) {
437
			return;
438
		}
439
440
		/* translators: %1$s and %2$s expand to anchor tags linking to the new extensions page. */
441
		$info_message = sprintf(
442
			__( 'Thanks for updating BackUpWordPress, why not check out %1$sour extensions?%2$s', 'backupwordpress' ),
443
			'<a href="' . get_settings_url( HMBKP_PLUGIN_SLUG . '_extensions' ) . '">',
444
			'</a>'
445
		);
446
		?>
447
448
		<div id="hmbkp-info-message" class="updated notice is-dismissible">
449
450
			<p><?php echo $info_message; ?></p>
451
452
			<button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'backupwordpress' ); ?></span></button>
453
454
		</div>
455
456
	<?php }
457
458
}
459
460
if ( is_multisite() && ! is_main_site() ) {
461
	return;
462
}
463
464
Plugin::get_instance();
465