-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-pjw-groupme-importer.php
80 lines (71 loc) · 2.17 KB
/
class-pjw-groupme-importer.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
<?php
/**
* Simple GroupMe Importer class
*
*/
class pjw_groupme_importer {
private $version = 0.1;
private $debug;
private $meta_key = 'pjw_groupme_oauth_token';
public function __construct( $debug = false ) {
add_action( 'init', array( $this, 'action_init' ) );
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
add_filter( 'query_vars', array( $this, 'filter_query_vars' ) );
add_action( 'parse_request', array( $this, 'action_parse_request' ) );
$this->debug = $debug;
}
private function debug_log( $thing ) {
if ( $this->debug ) {
error_log( __CLASS__ . ':' . print_r( $thing, true ) );
}
}
public function action_init() {
add_rewrite_rule( '^groupme-oauth-callback$', 'index.php?_pjw_group_me_oauth=1', 'top' );
}
public function action_admin_init() {
if ( get_option( __CLASS__ . '-version' ) != $this->version ) {
flush_rewrite_rules();
update_option( __CLASS__ . '-version', $this->version );
}
register_importer('pjw-groupme-importer', 'GroupMe Importer', 'Lets you import groupme group conversations into WordPress', array( $this, 'importer_go' ) );
}
public function filter_query_vars( $_query_vars ) {
$_query_vars[] = '_pjw_group_me_oauth';
return $_query_vars;
}
public function action_parse_request() {
if ( isset( $GLOBALS['wp']->query_vars[ '_pjw_group_me_oauth' ] ) ) {
update_user_meta( get_current_user_id(), $this->meta_key, $_REQUEST['access_token'], '' );
wp_safe_redirect( admin_url( 'admin.php?import=pjw-groupme-importer' ) );
die();
}
}
public function importer_go() {
?>
<div class="wrap">
<h1>GroupMe Importer</h1>
<ol>
<?php
$_access_token = get_user_meta( get_current_user_id(), $this->meta_key, true );
if ( empty( $_access_token ) ) {
echo "<li>Connect</li>\n";
} else {
echo "<li>Connected ✓</li>\n";
}
?>
<li>Groups
<ul>
<?php
$rest_api = new pjw_groupme_rest_api( $_access_token );
$groups = $rest_api->groups();
foreach ( $groups as $group ) {
echo "<li><img src='{$group->image_url}' width='25px' height='25px'/> {$group->name} - {$group->id}</li>\n";
}
?>
</ul>
</li>
</ol>
</div>
<?php
}
}