Completed
Push — add/handling-connection-errors ( a02b33...6bb14a )
by
unknown
08:09 queued 20s
created

Invalid_Blog_Token::__construct()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 1
dl 0
loc 29
rs 8.5226
c 0
b 0
f 0
1
<?php
2
/**
3
 * The Jetpack Connection error handler class for invalid blog tokens
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection\Error_Handlers;
9
10
/**
11
 * This class handles all the error codes that indicates a broken blog token and
12
 * suggests the user to reconnect.
13
 *
14
 * @since 8.7.0
15
 */
16
class Invalid_Blog_Token {
17
18
	/**
19
	 * Set up hooks
20
	 *
21
	 * @since 8.7.0
22
	 *
23
	 * @param array $errors The array containing verified errors stored in the database.
24
	 */
25
	public function __construct( $errors ) {
26
27
		/**
28
		 * Filters the message to be displayed in the admin notices area when there's a invalid blog token xmlrpc error
29
		 *
30
		 * Return an empty value to disable the message.
31
		 *
32
		 * @since 8.7.0
33
		 *
34
		 * @param string $message The error message.
35
		 */
36
		$this->message = apply_filters( 'jetpack_connection_invalid_blog_token_admin_notice', __( 'Your connection with WordPress.com seems to be broken. If you\'re experiencing issues, please try reconnecting.', 'jetpack' ) );
0 ignored issues
show
Bug introduced by
The property message does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
38
		if ( empty( $this->message ) ) {
39
			return;
40
		}
41
42
		// In this class, we will only handle errors with the blog token, so ignoring if there are only errors with user tokens.
43
		if ( ! isset( $errors[0] ) || ! isset( $errors['invalid'] ) ) {
44
			add_action( 'react_connection_errors_initial_state', array( $this, 'jetpack_react_dashboard_error' ) );
45
			// do not add admin notice to the jetpack dashboard.
46
			global $pagenow;
47
			if ( 'admin.php' === $pagenow || isset( $_GET['page'] ) && 'jetpack' === $_GET['page'] ) { // phpcs:ignore
48
				return;
49
			}
50
			add_action( 'admin_notices', array( $this, 'admin_notice' ) );
51
		}
52
53
	}
54
55
	/**
56
	 * Prints an admin notice for the blog token error
57
	 *
58
	 * @since 8.7.0
59
	 *
60
	 * @return void
61
	 */
62
	public function admin_notice() {
63
		?>
64
		<div class="notice notice-error is-dismissible jetpack-message jp-connect" style="display:block !important;">
65
			<p><?php echo esc_html( $this->message ); ?></p>
66
		</div>
67
		<?php
68
	}
69
70
	/**
71
	 * Adds the error message to the Jetpack React Dashboard
72
	 *
73
	 * @param array $errors The array of errors.
74
	 * @return array
75
	 */
76
	public function jetpack_react_dashboard_error( $errors ) {
77
78
		$errors[] = array(
79
			'code'    => 'invalid_blog_token',
80
			'message' => $this->message,
81
			'action'  => 'reconnect',
82
		);
83
		return $errors;
84
	}
85
86
87
}
88