Completed
Push — renovate/automattic-calypso-co... ( 246b03...041a44 )
by
unknown
10:02 queued 01:24
created

Admin::get_active_modules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Simple admin interface to activate/deactivate modules
4
 *
5
 * @package Jetpack
6
 */
7
8
namespace Automattic\Jetpack\Debug_Helper;
9
10
/**
11
 * Class Jetpack_Debug_Helper_Admin
12
 */
13
class Admin {
14
15
	/**
16
	 * Option name.
17
	 */
18
	const OPTION_NAME = 'jetpack_debug_helper_active_modules';
19
20
	/**
21
	 * Constructor.
22
	 */
23
	public function __construct() {
24
		add_action( 'admin_menu', array( $this, 'register_submenu_page' ), 1000 );
25
	}
26
27
	/**
28
	 * Register's submenu.
29
	 */
30
	public function register_submenu_page() {
31
		add_submenu_page(
32
			'jetpack',
33
			'Debug tools',
34
			'Debug tools',
35
			'manage_options',
36
			'debug-tools',
37
			array( $this, 'render_ui' ),
38
			99
39
		);
40
	}
41
42
	/**
43
	 * Get the list of active modules
44
	 *
45
	 * @return array
46
	 */
47
	public static function get_active_modules() {
48
		return get_option( self::OPTION_NAME, array() );
49
	}
50
51
	/**
52
	 * Render UI.
53
	 */
54
	public function render_ui() {
55
56
		$this->update_option();
57
58
		$stored_options = get_option( self::OPTION_NAME, array() );
59
		global $jetpack_dev_debug_modules;
60
		?>
61
		<h1>Jetpack Debug tools</h1>
62
		<p>This plugin adds debugging tools to your jetpack. Choose which tools you want to activate.</p>
63
64
		<form method="post">
65
			<input type="hidden" name="action" value="store_debug_active_modules">
66
			<?php wp_nonce_field( 'store-debug-modules' ); ?>
67
68
			<?php foreach ( $jetpack_dev_debug_modules as $module_slug => $module_details ) : ?>
69
70
				<p>
71
					<input type="checkbox" name="active_modules[]" value="<?php echo esc_attr( $module_slug ); ?>" <?php checked( in_array( $module_slug, $stored_options, true ) ); ?> />
72
					<b><?php echo esc_html( $module_details['name'] ); ?></b>
73
					<?php echo esc_html( $module_details['description'] ); ?>
74
				</p>
75
76
			<?php endforeach; ?>
77
78
			<input type="submit" value="Save" class="button button-primary">
79
80
		</form>
81
		<br>
82
83
		<?php
84
	}
85
86
	/**
87
	 * Store options.
88
	 */
89
	public function update_option() {
90
91
		if ( isset( $_POST['action'] ) && 'store_debug_active_modules' === $_POST['action'] ) {
92
			check_admin_referer( 'store-debug-modules' );
93
			update_option( self::OPTION_NAME, $_POST['active_modules'] );
94
		}
95
96
	}
97
98
}
99
100
add_action(
101
	'plugins_loaded',
102
	function() {
103
		new Admin();
104
	}
105
);
106
107