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

HMBKP_Setup::trim_prefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Class BackUpWordPress_Setup
4
 */
5
class HMBKP_Setup {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
7
	/**
8
	 * Defines the minimum version of WordPress required by BWP.
9
	 */
10
	const MIN_WP_VERSION = '3.9';
11
12
	/**
13
	 * Defines the minimum version of PHP required by BWP.
14
	 */
15
	const MIN_PHP_VERSION = '5.3.2';
16
17
	/**
18
	 * Setup the plugin defaults on activation
19
	 */
20
	public static function activate() {
21
22
		if ( ! current_user_can( 'activate_plugins' ) ) {
23
			return;
24
		}
25
26
		// loads the translation files for the Error message in the wp_die call.
27
		load_plugin_textdomain( 'backupwordpress', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
28
29
		if ( ! self::meets_requirements() ) {
30
31
			wp_die( self::get_notice_message(), __( 'BackUpWordPress', 'backupwordpress' ), array( 'back_link' => true ) );
32
33
		}
34
35
		// Run deactivate on activation in-case it was deactivated manually
36
		self::deactivate();
37
38
	}
39
40
	/**
41
	 * Cleanup on plugin deactivation
42
	 *
43
	 * Removes options and clears all cron schedules
44
	 */
45
	public static function deactivate() {
46
47
		if ( ! current_user_can( 'activate_plugins' ) ) {
48
			return;
49
		}
50
51
		self::delete_schedules();
52
53
		self::delete_transients();
54
55
	}
56
57
	/**
58
	 * Deletes the backup schedule database entries and WP Cron entries.
59
	 */
60
	public static function delete_schedules() {
61
62
		// Delete Cron schedules.
63
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
64
65
		$schedules = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", 'hmbkp_schedule_%' ) );
66
67
		foreach ( array_map( array( 'self', 'trim_prefix' ), $schedules ) as $item ) {
68
			wp_clear_scheduled_hook( 'hmbkp_schedule_hook', array( 'id' => $item ) );
69
		}
70
71
72
	}
73
74
	public static function trim_prefix( $item ) {
75
		return ltrim( $item, 'hmbkp_schedule_' );
76
	}
77
78
	/**
79
	 * Deletes the plugin's transients from the database.
80
	 */
81
	public static function delete_transients() {
82
83
		// Delete all transients
84
		$transients = array(
85
			'hmbkp_plugin_data',
86
			'hmbkp_directory_filesizes',
87
			'hmbkp_directory_filesizes_running',
88
			'hmbkp_wp_cron_test_beacon',
89
			'hm_backdrop',
90
		);
91
92
		array_map( 'delete_transient', $transients );
93
	}
94
95
	/**
96
	 * Deactivate BackUpWordPress.
97
	 */
98
	public static function self_deactivate() {
1 ignored issue
show
Coding Style introduced by
self_deactivate uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
99
100
		if ( ! current_user_can( 'activate_plugins' ) ) {
101
			return;
102
		}
103
104
		if ( ! function_exists( 'deactivate_plugins' ) ) {
105
			require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
106
		}
107
108
		deactivate_plugins( dirname( dirname(__FILE__ ) ) . '/backupwordpress.php' );
109
110
		if ( isset( $_GET['activate'] ) ) {
111
			unset( $_GET['activate'] );
112
		}
113
114
	}
115
116
	/**
117
	 * Determine if this WordPress install meets the minimum requirements for BWP to run.
118
	 *
119
	 * @return bool
120
	 */
121
	public static function meets_requirements() {
122
123
		if ( false === self::is_supported_php_version() ) {
124
			return false;
125
		}
126
127
		if ( false === self::is_supported_wp_version() ) {
1 ignored issue
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(false === self:...upported_wp_version());.
Loading history...
128
			return false;
129
		}
130
131
		return true;
132
	}
133
134
	/**
135
	 * Checks the current PHP version against the required version.
136
	 *
137
	 * @return bool 'Operator' parameter specified, returns a boolean.
138
	 */
139
	protected static function is_supported_php_version() {
140
141
		return version_compare( phpversion(), self::MIN_PHP_VERSION, '>=' );
142
	}
143
144
	/**
145
	 * Checks the current WordPress version against the required version.
146
	 *
147
	 * @return bool 'Operator' parameter specified, returns a boolean.
148
	 */
149
	protected static function is_supported_wp_version() {
150
151
		return version_compare( get_bloginfo( 'version' ), self::MIN_WP_VERSION, '>=' );
152
	}
153
154
	/**
155
	 * Displays a user friendly message in the WordPress admin.
156
	 */
157
	public static function display_admin_notices() {
158
159
		echo '<div class="error"><p>' . self::get_notice_message() . '</p></div>';
160
161
	}
162
163
	/**
164
	 * Returns a localized user friendly error message.
165
	 *
166
	 * @return string
167
	 */
168
	public static function get_notice_message() {
169
170
		return sprintf(
171
			__( 'BackUpWordPress requires PHP version %1$s or later and WordPress version %2$s or later to run. It has not been activated. %3$s%4$s%5$sLearn more%6$s', 'backupwordpress' ),
172
			self::MIN_PHP_VERSION,
173
			self::MIN_WP_VERSION,
174
			'<a href="',
175
			'https://bwp.hmn.md/unsupported-php-version-error/',
176
			'">',
177
			'</a>'
178
		);
179
	}
180
181
}
182