Repository navigation
Expand file tree
/
Copy pathadmin.php
More file actions
155 lines (131 loc) · 5.41 KB
/
Copy pathadmin.php
File metadata and controls
155 lines (131 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Administrative UI views and callbacks
*/
class WordPress_GitHub_Sync_Admin {
/**
* Hook into GitHub API
*/
public function __construct() {
add_action( 'admin_menu', array( &$this, 'add_admin_menu' ) );
add_action( 'admin_init', array( &$this, 'register_settings' ) );
add_action( 'current_screen', array( &$this, 'callback' ) );
}
/**
* Callback to render the settings page view
*/
public function settings_page() {
include dirname( dirname( __FILE__ ) ) . '/views/options.php';
}
/**
* Callback to register the plugin's options
*/
public function register_settings() {
add_settings_section( 'general', 'General Settings', array( &$this, 'section_callback' ), WordPress_GitHub_Sync::$text_domain );
register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_host' );
add_settings_field( 'wpghs_host', __( 'GitHub hostname', 'wordpress-github-sync' ), array( &$this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array(
'default' => 'https://api.github.com',
'name' => 'wpghs_host',
'help_text' => __( 'The GitHub host to use. Can be changed to support a GitHub Enterprise installation.', 'wordpress-github-sync' ),
)
);
register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_repository' );
add_settings_field( 'wpghs_repository', __( 'Repository', 'wordpress-github-sync' ), array( &$this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array(
'default' => '',
'name' => 'wpghs_repository',
'help_text' => __( 'The GitHub repository to commit to, with owner (<code>[OWNER]/[REPOSITORY]</code>), e.g., <code>benbalter/benbalter.github.com</code>. The repository should contain an initial commit.', 'wordpress-github-sync' ),
)
);
register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_oauth_token' );
add_settings_field( 'wpghs_oauth_token', __( 'Oauth Token', 'wordpress-github-sync' ), array( &$this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array(
'default' => '',
'name' => 'wpghs_oauth_token',
'help_text' => __( "A <a href='https://github.com/settings/tokens/new'>personal oauth token</a> with <code>public_repo</code> scope.", 'wordpress-github-sync' ),
)
);
register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_secret' );
add_settings_field( 'wpghs_secret', __( 'Webhook Secret', 'wordpress-github-sync' ), array( &$this, 'field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array(
'default' => '',
'name' => 'wpghs_secret',
'help_text' => __( "The webhook's secret phrase.", 'wordpress-github-sync' ),
)
);
register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_default_user' );
add_settings_field( 'wpghs_default_user', __( 'Default Import User', 'wordpress-github-sync' ), array( &$this, 'user_field_callback' ), WordPress_GitHub_Sync::$text_domain, 'general', array(
'default' => '',
'name' => 'wpghs_default_user',
'help_text' => __( 'The fallback user for import, in case WordPress <--> GitHub Sync cannot find the committer in the database.', 'wordpress-github-sync' ),
)
);
}
/**
* Callback to render an individual options field
*
* @param array $args
*/
public function field_callback( $args ) {
include dirname( dirname( __FILE__ ) ) . '/views/setting-field.php';
}
/**
* Callback to render the default import user field.
*
* @param array $args
*/
public function user_field_callback( $args ) {
include dirname( dirname( __FILE__ ) ) . '/views/user-setting-field.php';
}
/**
* Displays settings messages from background processes
*/
public function section_callback() {
if ( 'settings_page_' . WordPress_GitHub_Sync::$text_domain !== get_current_screen()->id ) {
return;
}
if ( 'yes' === get_option( '_wpghs_export_started' ) ) { ?>
<div class="updated">
<p><?php esc_html_e( 'Export to GitHub started.', 'wordpress-github-sync' ); ?></p>
</div><?php
delete_option( '_wpghs_export_started' );
}
if ( $message = get_option( '_wpghs_export_error' ) ) { ?>
<div class="error">
<p><?php esc_html_e( 'Export to GitHub failed with error:', 'wordpress-github-sync' ); ?> <?php echo esc_html( $message );?></p>
</div><?php
delete_option( '_wpghs_export_error' );
}
if ( 'yes' === get_option( '_wpghs_export_complete' ) ) { ?>
<div class="updated">
<p><?php esc_html_e( 'Export to GitHub completed successfully.', 'wordpress-github-sync' );?></p>
</div><?php
delete_option( '_wpghs_export_complete' );
}
}
/**
* Add options menu to admin navbar
*/
public function add_admin_menu() {
add_options_page( __( 'WordPress <--> GitHub Sync', WordPress_GitHub_Sync::$text_domain ), __( 'GitHub Sync', 'wordpress-github-sync' ), 'manage_options', WordPress_GitHub_Sync::$text_domain, array( &$this, 'settings_page' ) );
}
/**
* Admin callback to trigger import/export because WordPress admin routing lol
*/
public function callback() {
global $wpghs;
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( 'settings_page_' . WordPress_GitHub_Sync::$text_domain !== get_current_screen()->id ) {
return;
}
if ( ! isset( $_GET['action'] ) ) {
return;
}
if ( 'export' === $_GET['action'] ) {
$wpghs->start_export();
}
if ( 'import' === $_GET['action'] ) {
$wpghs->start_import();
}
wp_redirect( admin_url( 'options-general.php?page=wordpress-github-sync' ) );
}
}