forked from mAAdhaTTah/wordpress-github-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.php
More file actions
133 lines (113 loc) · 4.7 KB
/
Copy pathadmin.php
File metadata and controls
133 lines (113 loc) · 4.7 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
<?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::$text_domain ), 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::$text_domain )
)
);
register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_repository' );
add_settings_field( 'wpghs_repository', __( 'Repository', WordPress_GitHub_Sync::$text_domain ), 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::$text_domain )
)
);
register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_oauth_token' );
add_settings_field( 'wpghs_oauth_token', __( 'Oauth Token', WordPress_GitHub_Sync::$text_domain ), 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::$text_domain )
)
);
register_setting( WordPress_GitHub_Sync::$text_domain, 'wpghs_secret' );
add_settings_field( 'wpghs_secret', __( 'Webhook Secret', WordPress_GitHub_Sync::$text_domain ), 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::$text_domain )
)
);
}
/**
* Callback to render an individual options field
*/
public function field_callback($args) {
include dirname( dirname( __FILE__ ) ) . '/views/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 _e( 'Export to GitHub started.', WordPress_GitHub_Sync::$text_domain ); ?></p>
</div><?php
delete_option( '_wpghs_export_started' );
}
if ( $message = get_option( '_wpghs_export_error' ) ) { ?>
<div class="error">
<p><?php _e( 'Export to GitHub failed with error:', WordPress_GitHub_Sync::$text_domain ); ?> <?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 _e( 'Export to GitHub completed successfully.', WordPress_GitHub_Sync::$text_domain );?></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::$text_domain ), '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();
}
}
}