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 (#1032)
by Tom
02:35
created

Plugin::generate_key()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 11
nc 4
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
23
	/**
24
	 * Insures we always return the same object.
25
	 *
26
	 * @return Plugin
27
	 */
28
	public static function get_instance() {
29
30
		if ( ! ( self::$instance instanceof Plugin ) ) {
31
			self::$instance = new Plugin();
32
		}
33
34
		return self::$instance;
35
	}
36
37
	/**
38
	 * Initialize the plugin.
39
	 */
40
	public function plugins_loaded() {
41
42
		if ( true !== $this->maybe_self_deactivate() ) {
43
44
			$this->constants();
45
46
			$this->includes();
47
48
			$this->hooks();
49
50
			$this->text_domain();
51
52
			// If we get here, then BWP is loaded
53
			do_action( 'backupwordpress_loaded' );
54
55
		}
56
57
	}
58
59
	/**
60
	 * Check plugin requirements.
61
	 *
62
	 * @return bool True is fails requirements. False otherwise.
63
	 */
64
	public function maybe_self_deactivate() {
65
66
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-setup.php' );
67
68 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...
69
70
			add_action( 'admin_init', array( '\HMBKP_Setup', 'self_deactivate' ) );
71
72
			add_action( 'all_admin_notices', array( '\HMBKP_Setup', 'display_admin_notices' ) );
73
74
			return true;
75
76
		}
77
78
		return false;
79
80
	}
81
82
	/**
83
	 * Define all the constants.
84
	 */
85
	public function constants() {
86
87
		if ( ! defined( 'HMBKP_PLUGIN_SLUG' ) ) {
88
			define( 'HMBKP_PLUGIN_SLUG', dirname( HMBKP_BASENAME ) );
89
		}
90
91
		if ( ! defined( 'HMBKP_PLUGIN_URL' ) ) {
92
			define( 'HMBKP_PLUGIN_URL', plugin_dir_url( HMBKP_BASENAME ) );
93
		}
94
95
		if ( ! defined( 'HMBKP_PLUGIN_LANG_DIR' ) ) {
96
			define( 'HMBKP_PLUGIN_LANG_DIR', apply_filters( 'hmbkp_filter_lang_dir', HMBKP_PLUGIN_SLUG . '/languages/' ) );
97
		}
98
99
		if ( ! defined( 'HMBKP_ADMIN_URL' ) ) {
100
			$page = is_multisite() ? network_admin_url( 'settings.php' ) : admin_url( 'tools.php' );
101
			define( 'HMBKP_ADMIN_URL', add_query_arg( 'page', HMBKP_PLUGIN_SLUG, $page ) );
102
		}
103
104
		if ( ! defined( 'HMBKP_ADMIN_PAGE' ) ) {
105
			$prefix = is_multisite() ? 'settings_page_' : 'tools_page_';
106
107
			define( 'HMBKP_ADMIN_PAGE', $prefix . HMBKP_PLUGIN_SLUG );
108
		}
109
110
		define( 'HMBKP_SECURE_KEY', $this->generate_key() );
111
112
	}
113
114
	/**
115
	 * Load all BackUpWordPress functions.
116
	 */
117
	protected function includes() {
118
119
		require_once( HMBKP_PLUGIN_PATH . 'vendor/autoload.php' );
120
121
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-notices.php' );
122
123
		// Load the admin menu
124
		require_once( HMBKP_PLUGIN_PATH . 'admin/menu.php' );
125
		require_once( HMBKP_PLUGIN_PATH . 'admin/actions.php' );
126
127
		// Load Backdrop if necessary.
128
		if ( ! class_exists( 'HM_Backdrop_Task' ) ) {
129
			require_once( HMBKP_PLUGIN_PATH . 'backdrop/hm-backdrop.php' );
130
		}
131
132
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-requirements.php' );
133
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-requirement.php' );
134
135
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-path.php' );
136
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-excludes.php' );
137
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-site-size.php' );
138
139
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-utilities.php' );
140
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-status.php' );
141
142
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine.php' );
143
144
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database.php' );
145
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database-mysqldump.php' );
146
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database-imysqldump.php' );
147
148
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file.php' );
149
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file-zip.php' );
150
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file-zip-archive.php' );
151
152
		require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup.php' );
153
154
		// Load the backup scheduling classes
155
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-scheduled-backup.php' );
156
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-schedules.php' );
157
158
		// Load the core functions
159
		require_once( HMBKP_PLUGIN_PATH . 'functions/core.php' );
160
		require_once( HMBKP_PLUGIN_PATH . 'functions/interface.php' );
161
162
		// Load the services
163
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-services.php' );
164
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-service.php' );
165
166
		// Load the email service
167
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-email-service.php' );
168
169
		// Load the webhook services
170
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-webhook-service.php' );
171
		require_once( HMBKP_PLUGIN_PATH . 'classes/class-wpremote-webhook-service.php' );
172
173
		require_once( HMBKP_PLUGIN_PATH . 'classes/deprecated.php' );
174
175
		// Load the wp cli command
176
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
177
			include( HMBKP_PLUGIN_PATH . 'classes/class-backupwordpress-wp-cli-command.php' );
178
		}
179
180
	}
181
182
	/**
183
	 * Hook into WordPress page lifecycle and execute BackUpWordPress functions.
184
	 */
185
	public function hooks() {
186
187
		add_action( 'activated_plugin', array( $this, 'load_first' ) );
188
189
		add_action( 'admin_init', array( $this, 'upgrade' ) );
190
191
		add_action( 'admin_init', array( $this, 'init' ) );
192
193
		add_action( 'hmbkp_schedule_hook', array( $this, 'schedule_hook_run' ) );
194
195
		add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
196
197
		add_action( 'admin_footer-' . HMBKP_ADMIN_PAGE, array( $this, 'load_intercom_script' ) );
198
199
		add_action( 'admin_enqueue_scripts', array( $this, 'styles' ) );
200
201
	}
202
203
	/**
204
	 * Load the Javascript in the admin.
205
	 *
206
	 * @param $hook The name of the admin page hook.
207
	 */
208
	public function scripts( $hook ) {
209
210
		if ( HMBKP_ADMIN_PAGE !== $hook ) {
211
			return;
212
		}
213
214
		$js_file = HMBKP_PLUGIN_URL . 'assets/hmbkp.min.js';
215
216
		// TODO shuold this also support WP_SCRIPT_DEBUG
217
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
218
			$js_file = HMBKP_PLUGIN_URL . 'assets/hmbkp.js';
219
		}
220
221
		wp_enqueue_script( 'hmbkp', $js_file, array( 'heartbeat' ), sanitize_key( self::PLUGIN_VERSION ) );
222
223
		wp_localize_script(
224
			'hmbkp',
225
			'hmbkp',
226
			array(
227
				'page_slug'                => HMBKP_PLUGIN_SLUG,
228
				'nonce'                    => wp_create_nonce( 'hmbkp_nonce' ),
229
				'hmbkp_run_schedule_nonce' => wp_create_nonce( 'hmbkp_run_schedule' ),
230
				'update'                   => __( 'Update', 'backupwordpress' ),
231
				'cancel'                   => __( 'Cancel', 'backupwordpress' ),
232
				'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",
233
				'delete_backup'            => __( 'Are you sure you want to delete this backup?', 'backupwordpress' ) . "\n\n" . __( '\'Cancel\' to go back, \'OK\' to delete.', 'backupwordpress' ) . "\n",
234
				'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",
235
				'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",
236
			)
237
		);
238
239
	}
240
241
	/**
242
	 * Loads the plugin text domain for translation.
243
	 * This setup allows a user to just drop his custom translation files into the WordPress language directory
244
	 * Files will need to be in a subdirectory with the name of the textdomain 'backupwordpress'
245
	 */
246
	public function text_domain() {
247
248
		// Set unique textdomain string
249
		$textdomain = 'backupwordpress';
250
251
		// The 'plugin_locale' filter is also used by default in load_plugin_textdomain()
252
		$locale = apply_filters( 'plugin_locale', get_locale(), $textdomain );
253
254
		// Set filter for WordPress languages directory
255
		$hmbkp_wp_lang_dir = apply_filters( 'hmbkp_do_filter_wp_lang_dir', trailingslashit( WP_LANG_DIR ) . trailingslashit( $textdomain ) . $textdomain . '-' . $locale . '.mo' );
256
257
		// Translations: First, look in WordPress' "languages" folder = custom & update-secure!
258
		load_textdomain( $textdomain, $hmbkp_wp_lang_dir );
259
260
		// Translations: Secondly, look in plugin's "languages" folder = default
261
		load_plugin_textdomain( $textdomain, false, HMBKP_PLUGIN_LANG_DIR );
262
263
	}
264
265
	/**
266
	 * Determine if we need to run an upgrade routine.
267
	 */
268
	public function upgrade() {
269
270
		// Fire the update action
271
		if ( self::PLUGIN_VERSION != get_option( 'hmbkp_plugin_version' ) ) {
272
			update();
273
		}
274
275
	}
276
277
	/**
278
	 * Runs on every admin page load
279
	 */
280
	public function init() {
281
282
		// If we have multiple paths for some reason then clean them up
283
		Path::get_instance()->merge_existing_paths();
284
	}
285
286
	/**
287
	 * Generate a unique key.
288
	 *
289
	 * @return string
290
	 */
291
	protected function generate_key() {
292
293
		$check = apply_filters( 'hmbkp_generate_key', null );
294
295
		if ( null !== $check ) {
296
			return $check;
297
		}
298
299
		$key = array( ABSPATH, time() );
300
		$constants = array( 'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT', 'SECRET_KEY' );
301
302
		foreach ( $constants as $constant ) {
303
			if ( defined( $constant ) ) {
304
				$key[] = constant( $constant );
305
			}
306
		}
307
308
		shuffle( $key );
309
310
		return md5( serialize( $key ) );
311
312
	}
313
314
	/**
315
	 * Ensure BackUpWordPress is loaded before add-ons, changes the order of the serialized values in the DB field.
316
	 */
317
	public function load_first() {
318
319
		$active_plugins = get_option( 'active_plugins' );
320
321
		$plugin_path = plugin_basename( __FILE__ );
322
323
		$key = array_search( $plugin_path, $active_plugins );
324
325
		if ( $key > 0 ) {
326
327
			array_splice( $active_plugins, $key, 1 );
328
329
			array_unshift( $active_plugins, $plugin_path );
330
331
			update_option( 'active_plugins', $active_plugins );
332
333
		}
334
335
	}
336
337
	/**
338
	 * Function to run when the schedule cron fires.
339
	 *
340
	 * @param $schedule_id
341
	 */
342
	public function schedule_hook_run( $schedule_id ) {
343
344
		if ( ! is_backup_possible() ) {
345
			return;
346
		}
347
348
		$schedules = Schedules::get_instance();
349
		$schedule  = $schedules->get_schedule( $schedule_id );
350
351
		if ( ! $schedule ) {
352
			return;
353
		}
354
355
		$schedule->run();
356
357
	}
358
359
	/**
360
	 * Enqueue the plugin styles.
361
	 *
362
	 * @param $hook
363
	 */
364
	public function styles( $hook ) {
365
366
		if ( HMBKP_ADMIN_PAGE !== $hook ) {
367
			return;
368
		}
369
370
		$css_file = HMBKP_PLUGIN_URL . 'assets/hmbkp.min.css';
371
372
		if ( WP_DEBUG ) {
373
			$css_file = HMBKP_PLUGIN_URL . 'assets/hmbkp.css';
374
		}
375
376
		wp_enqueue_style( 'backupwordpress', $css_file, false, sanitize_key( self::PLUGIN_VERSION ) );
377
378
	}
379
380
	/**
381
	 * Load Intercom and send across user information and server info. Only loaded if the user has opted in.
382
	 *
383
	 * @param $hook
384
	 */
385
	public function load_intercom_script() {
386
387
		if ( ! get_option( 'hmbkp_enable_support' ) ) {
388
			return;
389
		}
390
391
		$info = array();
392
393
		foreach ( Requirements::get_requirement_groups() as $group ) {
394
			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...
395
				$info[ $requirement->name() ] = $requirement->result();
396
			}
397
		}
398
399
		foreach ( Services::get_services() as $file => $service ) {
400
			array_merge( $info, call_user_func( array( $service, 'intercom_data' ) ) );
401
		}
402
403
		$current_user = wp_get_current_user();
404
405
		$info['user_hash']  = hash_hmac( 'sha256', $current_user->user_email, 'fcUEt7Vi4ym5PXdcr2UNpGdgZTEvxX9NJl8YBTxK' );
406
		$info['email']      = $current_user->user_email;
407
		$info['created_at'] = strtotime( $current_user->user_registered );
408
		$info['app_id']     = '7f1l4qyq';
409
		$info['name']       = $current_user->display_name;
410
		$info['widget']     = array( 'activator' => '#intercom' ); ?>
411
412
		<script id="IntercomSettingsScriptTag">
413
			window.intercomSettings = <?php echo json_encode( $info ); ?>;
414
		</script>
415
		<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>
416
417
	<?php }
418
}
419
420
if ( is_multisite() && ! is_main_site() ) {
421
	return;
422
}
423
424
Plugin::get_instance();
425