Completed
Push — master ( 9c9379...09a5d1 )
by James
02:45
created

WordPress_GitHub_Sync_Admin::section_callback()   C

Complexity

Conditions 8
Paths 65

Size

Total Lines 47
Code Lines 33

Duplication

Lines 12
Ratio 25.53 %
Metric Value
dl 12
loc 47
rs 5.7377
cc 8
eloc 33
nc 65
nop 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', 'wordpress-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. Can be changed to support a GitHub Enterprise installation.', 'wordpress-github-sync' ),
44
			)
45
		);
46
47
		register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_repository' );
48
		add_settings_field( 'wpghs_repository', __( 'Repository', 'wordpress-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.', 'wordpress-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', 'wordpress-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.", 'wordpress-github-sync' ),
60
			)
61
		);
62
63
		register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_secret' );
64
		add_settings_field( 'wpghs_secret', __( 'Webhook Secret', 'wordpress-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.", 'wordpress-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', 'wordpress-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.', 'wordpress-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.', 'wordpress-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:', 'wordpress-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.', 'wordpress-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 to GitHub started.', 'wordpress-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 to GitHub failed with error:', 'wordpress-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 to GitHub completed successfully.', 'wordpress-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', 'wordpress-github-sync' ),
155
			__( 'GitHub Sync', 'wordpress-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=wordpress-github-sync' ) );
187
		die;
188
	}
189
}
190