Completed
Push — fix/useBlockEditContext-usage ( 1e6b8b...d10938 )
by
unknown
199:36 queued 191:34
created

Broken_Token_XmlRpc::print_current_errors()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php // phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_print_r
2
/**
3
 * XMLRPC Brokeness.
4
 *
5
 * @package Jetpack.
6
 */
7
8
use Automattic\Jetpack\Connection\Error_Handler;
9
10
/**
11
 * Class Broken_Token_XmlRpc
12
 */
13
class Broken_Token_XmlRpc {
14
15
	/**
16
	 * Broken_Token_XmlRpc constructor.
17
	 */
18
	public function __construct() {
19
20
		add_action( 'admin_menu', array( $this, 'register_submenu_page' ), 1000 );
21
22
		add_action( 'admin_post_clear_all_xmlrpc_errors', array( $this, 'admin_post_clear_all_xmlrpc_errors' ) );
23
24
		$this->error_manager = new Error_Handler();
0 ignored issues
show
Bug introduced by
The property error_manager 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...
25
		$this->stored_errors = $this->error_manager->get_stored_errors();
0 ignored issues
show
Bug introduced by
The property stored_errors 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...
26
		$this->dev_debug_on  = defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG;
0 ignored issues
show
Bug introduced by
The property dev_debug_on 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...
27
	}
28
29
	/**
30
	 * Register submenu page.
31
	 */
32
	public function register_submenu_page() {
33
		add_submenu_page(
34
			'jetpack',
35
			'XML-RPC Errors',
36
			'XML-RPC Errors',
37
			'manage_options',
38
			'broken-token-xmlrpc-errors',
39
			array( $this, 'render_ui' ),
40
			99
41
		);
42
	}
43
44
	/**
45
	 * Render UI.
46
	 */
47
	public function render_ui() {
48
		?>
49
			<h1>XML-RPC errors</h1>
50
			<p>
51
				This page helps you to trigger XML-RPC requests with invalid signatures.
52
			</p>
53
			<?php if ( $this->dev_debug_on ) : ?>
54
				<div class="notice notice-success">
55
					<p>JETPACK_DEV_DEBUG constant is ON. This means every xml-rpc error will be reported. You're good to test.</p>
56
				</div>
57
			<?php else : ?>
58
				<div class="notice notice-warning">
59
					<p>JETPACK_DEV_DEBUG constant is OFF. This means xml-rpc error will only be reported once evey hour. Set it to true so you can test it.</p>
60
				</div>
61
			<?php endif; ?>
62
63
			<p>
64
				Now head to <a href="https://jetpack.com/debug/?url=<?php echo esc_url_raw( get_home_url() ); ?>">Jetpack Debugger</a> and trigger some requests!
65
			</p>
66
67
			<div id="current_xmlrpc_errors">
68
69
70
				<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post">
71
					<input type="hidden" name="action" value="clear_all_xmlrpc_errors">
72
					<?php wp_nonce_field( 'clear-xmlrpc-errors' ); ?>
73
					<h2>
74
						Current Unverified Errors
75
						<input type="submit" value="Clear all unverified errors" class="button button-primary">
76
					</h2>
77
				</form>
78
				<div id="stored-xmlrpc-error">
79
					<?php $this->print_current_errors(); ?>
80
				</div>
81
			</div>
82
83
		<?php
84
	}
85
86
	/**
87
	 * Print current errors.
88
	 */
89
	public function print_current_errors() {
90
		foreach ( $this->stored_errors as $error_code => $error_group ) {
91
92
			echo '<h4>' . esc_html( $error_code ) . '</h4>';
93
94
			foreach ( $error_group as $user_id => $error ) {
95
				?>
96
				<b>User: <?php echo esc_html( $user_id ); ?></b>
97
				<pre><?php print_r( $error ); ?></pre>
98
				<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post">
99
					<input type="hidden" name="action" value="verify_error">
100
					<input type="hidden" name="nonce" value="<?php echo esc_attr( $error['error_data']['nonce'] ); ?>">
101
					<?php wp_nonce_field( 'verify-error' ); ?>
102
					<input type="submit" value="Verify error" class="button button-primary">
103
				</form>
104
				<hr />
105
				<?php
106
			}
107
		}
108
	}
109
110
	/**
111
	 * Clear all XMLRPC Errors.
112
	 */
113
	public function admin_post_clear_all_xmlrpc_errors() {
114
		check_admin_referer( 'clear-xmlrpc-errors' );
115
		$this->error_manager->delete_stored_errors();
116
		$this->admin_post_redirect_referrer();
117
	}
118
119
	/**
120
	 * Just redirects back to the referrer. Keeping it DRY.
121
	 */
122
	public function admin_post_redirect_referrer() {
123
		if ( wp_get_referer() ) {
124
			wp_safe_redirect( wp_get_referer() );
125
		} else {
126
			wp_safe_redirect( get_home_url() );
127
		}
128
	}
129
130
}
131
132
// phpcs:enable
133