WordPress_GitHub_Sync_Admin::field_callback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Administrative UI views and callbacks
4
 * @package WordPress_GitHub_Sync
5
 */
6
7
/**
8
 * Class WordPress_GitHub_Sync_Admin
9
 */
10
class WordPress_GitHub_Sync_Admin {
11
12
	/**
13
	 * Hook into GitHub API
14
	 */
15
	public function __construct() {
16
		add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
17
		add_action( 'admin_init', array( $this, 'register_settings' ) );
18
		add_action( 'current_screen', array( $this, 'trigger_cron' ) );
19
	}
20
21
	/**
22
	 * Callback to render the settings page view
23
	 */
24
	public function settings_page() {
25
		include dirname( dirname( __FILE__ ) ) . '/views/options.php';
26
	}
27
28
	/**
29
	 * Callback to register the plugin's options
30
	 */
31
	public function register_settings() {
32
		add_settings_section(
33
			'general',
34
			'General Settings',
35
			array( $this, 'section_callback' ),
36
			WordPress_GitHub_Sync::$text_domain
37
		);
38
39
		register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_host' );
40
		add_settings_field( 'wpghs_host', __( 'GitHub hostname', 'wp-github-sync' ), array( $this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array(
41
				'default'   => 'https://api.github.com',
42
				'name'      => 'wpghs_host',
43
				'help_text' => __( 'The GitHub host to use. This only needs to be changed to support a GitHub Enterprise installation.', 'wp-github-sync' ),
44
			)
45
		);
46
47
		register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_repository' );
48
		add_settings_field( 'wpghs_repository', __( 'Repository', 'wp-github-sync' ), array( $this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array(
49
				'default'   => '',
50
				'name'      => 'wpghs_repository',
51
				'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>github/hubot.github.com</code>. The repository should contain an initial commit, which is satisfied by including a README when you create the repository on GitHub.', 'wp-github-sync' ),
52
			)
53
		);
54
55
		register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_oauth_token' );
56
		add_settings_field( 'wpghs_oauth_token', __( 'Oauth Token', 'wp-github-sync' ), array( $this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array(
57
				'default'   => '',
58
				'name'      => 'wpghs_oauth_token',
59
				'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'wp-github-sync' ),
60
			)
61
		);
62
63
		register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_secret' );
64
		add_settings_field( 'wpghs_secret', __( 'Webhook Secret', 'wp-github-sync' ), array( $this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array(
65
				'default'   => '',
66
				'name'      => 'wpghs_secret',
67
				'help_text' => __( "The webhook's secret phrase. This should be password strength, as it is used to verify the webhook's payload.", 'wp-github-sync' ),
68
			)
69
		);
70
71
		register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_default_user' );
72
		add_settings_field( 'wpghs_default_user', __( 'Default Import User', 'wp-github-sync' ), array( &$this, 'user_field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array(
73
				'default'   => '',
74
				'name'      => 'wpghs_default_user',
75
				'help_text' => __( 'The fallback user for import, in case WordPress <--> GitHub Sync cannot find the committer in the database.', 'wp-github-sync' ),
76
			)
77
		);
78
	}
79
80
	/**
81
	 * Callback to render an individual options field
82
	 *
83
	 * @param array $args Field arguments.
84
	 */
85
	public function field_callback( $args ) {
86
		include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php';
87
	}
88
89
	/**
90
	 * Callback to render the default import user field.
91
	 *
92
	 * @param array $args Field arguments.
93
	 */
94
	public function user_field_callback( $args ) {
95
		include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php';
96
	}
97
98
	/**
99
	 * Displays settings messages from background processes
100
	 */
101
	public function section_callback() {
102
		if ( get_current_screen()->id !== 'settings_page_' . WordPress_GitHub_Sync::$text_domain ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
103
			return;
104
		}
105
106
		if ( 'yes' === get_option( '_wpghs_export_started' ) ) { ?>
107
			<div class="updated">
108
				<p><?php esc_html_e( 'Export to GitHub started.', 'wp-github-sync' ); ?></p>
109
			</div><?php
110
			delete_option( '_wpghs_export_started' );
111
		}
112
113 View Code Duplication
		if ( $message = get_option( '_wpghs_export_error' ) ) { ?>
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
			<div class="error">
115
				<p><?php esc_html_e( 'Export to GitHub failed with error:', 'wp-github-sync' ); ?> <?php echo esc_html( $message );?></p>
116
			</div><?php
117
			delete_option( '_wpghs_export_error' );
118
		}
119
120
		if ( 'yes' === get_option( '_wpghs_export_complete' ) ) { ?>
121
			<div class="updated">
122
				<p><?php esc_html_e( 'Export to GitHub completed successfully.', 'wp-github-sync' );?></p>
123
			</div><?php
124
			delete_option( '_wpghs_export_complete' );
125
		}
126
127
		if ( 'yes' === get_option( '_wpghs_import_started' ) ) { ?>
128
			<div class="updated">
129
			<p><?php esc_html_e( 'Import from GitHub started.', 'wp-github-sync' ); ?></p>
130
			</div><?php
131
			delete_option( '_wpghs_import_started' );
132
		}
133
134 View Code Duplication
		if ( $message = get_option( '_wpghs_import_error' ) ) { ?>
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
			<div class="error">
136
			<p><?php esc_html_e( 'Import from GitHub failed with error:', 'wp-github-sync' ); ?> <?php echo esc_html( $message );?></p>
137
			</div><?php
138
			delete_option( '_wpghs_import_error' );
139
		}
140
141
		if ( 'yes' === get_option( '_wpghs_import_complete' ) ) { ?>
142
			<div class="updated">
143
			<p><?php esc_html_e( 'Import from GitHub completed successfully.', 'wp-github-sync' );?></p>
144
			</div><?php
145
			delete_option( '_wpghs_import_complete' );
146
		}
147
	}
148
149
	/**
150
	 * Add options menu to admin navbar
151
	 */
152
	public function add_admin_menu() {
153
		add_options_page(
154
			__( 'WordPress <--> GitHub Sync', 'wp-github-sync' ),
155
			__( 'GitHub Sync', 'wp-github-sync' ),
156
			'manage_options',
157
			WordPress_GitHub_Sync::$text_domain,
158
			array( $this, 'settings_page' )
159
		);
160
	}
161
162
	/**
163
	 * Admin callback to trigger import/export because WordPress admin routing lol
164
	 */
165
	public function trigger_cron() {
166
		if ( ! current_user_can( 'manage_options' ) ) {
167
			return;
168
		}
169
170
		if ( get_current_screen()->id !== 'settings_page_' . WordPress_GitHub_Sync::$text_domain ) {
0 ignored issues
show
introduced by
Found "!== '". Use Yoda Condition checks, you must
Loading history...
171
			return;
172
		}
173
174
		if ( ! isset( $_GET['action'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
175
			return;
176
		}
177
178
		if ( 'export' === $_GET['action'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
179
			WordPress_GitHub_Sync::$instance->start_export();
180
		}
181
182
		if ( 'import' === $_GET['action'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
183
			WordPress_GitHub_Sync::$instance->start_import();
184
		}
185
186
		wp_redirect( admin_url( 'options-general.php?page=wp-github-sync' ) );
187
		die;
188
	}
189
}
190