-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhm-top-posts-admin.php
160 lines (118 loc) · 4.59 KB
/
hm-top-posts-admin.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
<?php
/**
* Register the Google analytics settings
*
* @access public
* @return null
*/
function hmtp_top_posts_setting_admin_init() {
register_setting( 'hmtp_top_posts_setting', 'hmtp_top_posts_setting_profile_id' );
register_setting( 'hmtp_top_posts_setting', 'hmtp_top_posts_setting_username' );
register_setting( 'hmtp_top_posts_setting', 'hmtp_top_posts_setting_password', 'hmtp_top_posts_setting_option_sanitize' );
register_setting( 'hmtp_top_posts_setting', 'hmtp_top_posts_setting_auth_delete', 'hmtp_top_posts_setting_option_auth_delete' );
register_setting( 'hmtp_top_posts_setting', 'hmtp_top_posts_allow_opt_out' );
}
add_action( 'admin_init', 'hmtp_top_posts_setting_admin_init' );
/**
* Add the auto links page to the settings menu
*
* @access public
* @return null
*/
function hmtp_top_posts_setting_admin_menu() {
add_options_page( 'HM Top Posts', 'Top Posts', 'manage_options', 'hmtp_top_posts_setting', 'hmtp_top_posts_setting_admin_page' );
}
add_action( 'admin_menu', 'hmtp_top_posts_setting_admin_menu' );
/**
* Output the auto links admin page
*
* @access public
* @return null
*/
function hmtp_top_posts_setting_admin_page() { ?>
<style>
#hmtp_top_posts_setting_existing th { text-align: left; vertical-align: top; width: 150px; }
#hmtp_top_posts_setting_existing th,
#hmtp_top_posts_setting_existing td { padding-bottom: 10px; }
</style>
<div class="wrap">
<h2>Top Posts Settings</h2>
<p>Top posts uses google analytics data. Enter your details below.</p>
<p>Your password is not saved, only used to generate an auth token.</p>
<form action="options.php" method="post">
<table id="hmtp_top_posts_setting_existing">
<tr>
<th>Profile Id</th>
<td><input type="text" name="hmtp_top_posts_setting_profile_id" value="<?php echo get_option( 'hmtp_top_posts_setting_profile_id' ); ?>" class="regular-text"/></td>
</tr>
<tr>
<th>Email</th>
<td><input type="text" name="hmtp_top_posts_setting_username" value="<?php echo get_option( 'hmtp_top_posts_setting_username' ); ?>" class="regular-text"/></td>
</tr>
<tr>
<th>Password</th>
<td><input type="password" name="hmtp_top_posts_setting_password" value="<?php echo get_option( 'hmtp_top_posts_setting_password' ); ?>" class="regular-text"/></td>
</tr>
<?php if ( $auth_code = get_option('hmtp_top_posts_setting_auth_code') ) : ?>
<tr>
<th>Auth Code</th>
<td>
<input readonly="readonly" class="regular-text" value="<?php echo $auth_code; ?>" /><br/>
<label><input type="checkbox" name="hmtp_top_posts_setting_auth_delete" /> Check box to delete auth code.</label>
</td>
</tr>
<?php endif; ?>
<tr>
<th>Enable Post opt-out.</th>
<td>
<label><input type="checkbox" name="hmtp_top_posts_allow_opt_out" <?php checked( 'on', get_option( 'hmtp_top_posts_allow_opt_out' ) ); ?> /> Allow opting out on a per post basis.</label>
</td>
</tr>
</table>
<?php settings_fields( 'hmtp_top_posts_setting' ); ?>
<p class="submit"><input type="submit" name="hmtp_top_posts_setting_submit" id="submit" class="button-primary" value="Save Changes"></p>
</form>
</div>
<?php
$top_posts = new HMTP_Top_Posts();
$results = $top_posts->get_results();
if ( $results ) : ?>
<h4>Top Posts</h4>
<ul>
<?php foreach ( $results as $post ) : ?>
<li><?php printf( '%s (%d)', get_the_title( $post['post_id'] ), $post['views'] ); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php }
/**
* hmtp_top_posts_setting_option_sanitize function.
*
* sanitizes the related_articles options, converting to the format term => link
* called from the register_settings function.
*
*/
function hmtp_top_posts_setting_option_sanitize( $password ) {
$username = get_option( 'hmtp_top_posts_setting_username' );
$ga_profile_id = get_option( 'hmtp_top_posts_setting_profile_id' );
if ( empty( $password ) || ! $username )
return null;
try {
$ga = new gapi( $username, $password );
$auth_token = $ga->getAuthToken();
delete_option( 'hmtp_top_posts_error_message' );
} catch( Exception $e ){
update_option( 'hmtp_top_posts_error_message', $e->getMessage() );
return;
}
if ( ! empty( $auth_token ) )
update_option( 'hmtp_top_posts_setting_auth_code', $auth_token );
return $password;
}
function hmtp_top_posts_setting_option_auth_delete( $auth_delete ){
if ( isset( $auth_delete ) && $auth_delete == 'on' ) {
delete_option( 'hmtp_top_posts_setting_auth_code' );
delete_option( 'hmtp_top_posts_error_message' );
}
return false;
}