Completed
Push — add/secondary-user-in-place-co... ( 792b2c...105dce )
by
unknown
09:07
created

Invalid_Blog_Token::__construct()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 1
dl 0
loc 31
rs 8.4906
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
			return;
45
		}
46
47
		add_action( 'react_connection_errors_initial_state', array( $this, 'jetpack_react_dashboard_error' ) );
48
		// do not add admin notice to the jetpack dashboard.
49
		global $pagenow;
50
		if ( 'admin.php' === $pagenow || isset( $_GET['page'] ) && 'jetpack' === $_GET['page'] ) { // phpcs:ignore
51
			return;
52
		}
53
		add_action( 'admin_notices', array( $this, 'admin_notice' ) );
54
55
	}
56
57
	/**
58
	 * Prints an admin notice for the blog token error
59
	 *
60
	 * @since 8.7.0
61
	 *
62
	 * @return void
63
	 */
64
	public function admin_notice() {
65
66
		if ( ! current_user_can( 'jetpack_connect' ) ) {
67
			return;
68
		}
69
70
		?>
71
		<div class="notice notice-error is-dismissible jetpack-message jp-connect" style="display:block !important;">
72
			<p><?php echo esc_html( $this->message ); ?></p>
73
		</div>
74
		<?php
75
	}
76
77
	/**
78
	 * Adds the error message to the Jetpack React Dashboard
79
	 *
80
	 * @param array $errors The array of errors.
81
	 * @return array
82
	 */
83
	public function jetpack_react_dashboard_error( $errors ) {
84
85
		$errors[] = array(
86
			'code'    => 'invalid_blog_token',
87
			'message' => $this->message,
88
			'action'  => 'reconnect',
89
		);
90
		return $errors;
91
	}
92
93
94
}
95