-
Notifications
You must be signed in to change notification settings - Fork 6
/
admin_page.php
167 lines (138 loc) · 6.04 KB
/
admin_page.php
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
156
157
158
159
160
161
162
163
164
165
166
167
<?php
if ( is_multisite() )
add_action( 'network_admin_menu', 'git_theme_updater_add_menu' );
else
add_action( 'admin_menu', 'git_theme_updater_add_menu' );
function git_theme_updater_add_menu() {
add_menu_page('Git Theme Updater Config Page', 'ThemeUpdater', 'administrator', 'git_theme_updater_config_page', 'git_theme_updater_config_page');
}
function git_theme_updater_config_page() {
?>
<style>
.form-table {
width: 90%;
margin: 20px 0 30px;
display: block;
background: #fafafa;
padding: 20px;
border: 1px solid #eee;
}
h2 {
margin-bottom: 30px;
border-bottom: 1px solid #000;
padding-bottom: 15px;
}
</style>
<div id="icon-themes" class="icon32"><br></div><h2><?php _e("Settings for Git Theme Updater","gitThemeUpdater"); ?></h2>
<?php
if (isset($_REQUEST['save'])) {
git_theme_updater_save_options($_REQUEST['theme']);
}
$themes = wp_get_themes();
foreach ($themes as $name => $theme) {
//Normalize Name
$name = str_replace( array('.', ' ', ','), '_', $name );
$gitUri = "GTU_gituri_". $name;
$gitUriID = "GTU_gituri_id_". $name;
$gitUriSecret = "GTU_gituri_secret_". $name;
$gitUriToken = "GTU_gituri_token_". $name;
$gitUriValue = get_option($gitUri);
$gitUriIDValue = get_option($gitUriID);
$gitUriSecretValue = get_option($gitUriSecret);
$gitUriTokenValue = get_option($gitUriToken);
$t = wp_get_theme($name);
if ($gitUriValue =="" || !$gitUriValue)
$gitUriValue = $t->get('Github Theme URI');
?><h3><?php echo $t; ?></h3>
<form method="POST" action="admin.php?page=git_theme_updater_config_page">
<input type="hidden" name="save" value="data" />
<input type="hidden" name="theme" value="<?php echo $name; ?>" />
<table class="form-table">
<tbody>
<tr valign="top">
<th scope="row"><label for="<?php echo $gitUri; ?>"><?php _e("Github Theme URI","gitThemeUpdater"); ?></label></th>
<td><input name="<?php echo $gitUri; ?>" type="text" id="<?php echo $gitUri; ?>" value="<?php echo $gitUriValue; ?>" class="regular-text"></td>
</tr>
<tr valign="top">
<th scope="row"><label for="<?php echo $gitUriID; ?>"><?php _e("GitHub Client ID","gitThemeUpdater"); ?></label></th>
<td><input name="<?php echo $gitUriID; ?>" type="text" id="<?php echo $gitUriID; ?>" value="<?php echo get_option($gitUriID); ?>" class="regular-text"></td>
</tr>
<tr valign="top">
<th scope="row"><label for="<?php echo $gitUriSecret; ?>"><?php _e("GitHub Client Secret","gitThemeUpdater"); ?></label></th>
<td><input name="<?php echo $gitUriSecret; ?>" type="text" id="<?php echo $gitUriSecret; ?>" value="<?php echo get_option($gitUriSecret); ?>" class="regular-text"></td>
</tr>
<?php if ($gitUriSecretValue && $gitUriIDValue) {
if ($gitUriTokenValue) {
?><tr valign="top">
<td colspan="2"><strong><?php _e("Authorized!","gitThemeUpdater"); ?></strong> <a href="admin.php?page=git_theme_updater_config_page&save=forgottoken&theme=<?php echo $name; ?>"><?php _e("Click here to forgot authorization.","gitThemeUpdater"); ?></a></td>
</tr>
<?php
} else {
$return = get_admin_url();
if ( is_multisite() )
$return .= "network/";
$return .= "admin.php?page=git_theme_updater_config_page&save=returntoken&theme=". $name;
?><tr valign="top">
<td colspan="2"><a href="https://github.com/login/oauth/authorize?client_id=<?php echo $gitUriIDValue; ?>&client_secret=<?php echo $gitUriSecretValue; ?>&scope=repo&redirect_uri=<?php echo urlencode($return); ?>"><?php _e("Click here to authorize.","gitThemeUpdater"); ?></a></td>
</tr>
<?php
}
} else { ?>
<tr valign="top">
<td colspan="2"><?php _e("If it's a private project, we'll need to authenticate this site with Github. You will need to register an developer application to get the info above. Make sure the Callback URL have this site domain.","gitThemeUpdater"); ?> <a href="https://github.com/settings/applications" target="_blank"><?php _e("Register here.","gitThemeUpdater"); ?></a></td>
</tr>
<?php } ?>
<tr><td><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e("Save Theme Options","gitThemeUpdater"); ?>"></td></tr>
</tbody>
</table>
</form>
<?php
}
}
function git_theme_updater_save_options($name) {
if ($_POST['save'] == "data") {
$gitUri = "GTU_gituri_". $name;
update_option($gitUri,$_POST[$gitUri]);
$gitUriID = "GTU_gituri_id_". $name;
update_option($gitUriID,$_POST[$gitUriID]);
$gitUriSecret = "GTU_gituri_secret_". $name;
update_option($gitUriSecret,$_POST[$gitUriSecret]);
?>
<div id="setting-error-settings_updated" class="updated settings-error">
<p><strong><?php _e("Settings Saved!","gitThemeUpdater"); ?></strong></p>
</div>
<?php
} elseif ($_GET['save'] == "forgottoken") {
update_option("GTU_gituri_token_". $name,"");
?>
<div id="setting-error-settings_updated" class="updated settings-error">
<p><strong><?php _e("Token erased!","gitThemeUpdater"); ?></strong></p>
</div>
<?php
} elseif ($_GET['save'] == "returntoken") {
if ($_GET['code']) {
$url = "https://github.com/login/oauth/access_token?client_id=". get_option("GTU_gituri_id_".$name) ."&client_secret=". get_option("GTU_gituri_secret_".$name) ."&code=". $_GET['code'];
$token = parse_str(file_get_contents($url), $data);
if ($data['access_token']) {
update_option("GTU_gituri_token_". $name,$data['access_token']);
?>
<div id="setting-error-settings_updated" class="updated settings-error">
<p><strong><?php _e("Token saved!","gitThemeUpdater"); ?></strong></p>
</div>
<?php
} else {
?>
<div id="setting-error-settings_updated" class="error settings-error">
<p><strong><?php _e("Error while fetching the access token!","gitThemeUpdater"); ?></strong></p>
</div>
<?php
}
} else {
?>
<div id="setting-error-settings_updated" class="error settings-error">
<p><strong><?php _e("Error while fetching the access code!","gitThemeUpdater"); ?></strong></p>
</div>
<?php
}
}
}