-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.php
140 lines (119 loc) · 4.24 KB
/
example.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
<?php
// Make sure you run `composer install`!
// include the library.
require_once( 'qbo-connect.php' );
/**
* Example Zao\QBO_API\Connect usage
* To test it out, go to your site's WP dashboard:
* YOURSITE-URL/wp-admin/?api-connect
*/
function qp_api_initiate_sample_connection() {
if ( ! isset( $_GET['api-connect'] ) ) {
return;
}
global $api_connect; // hold this in a global for demonstration purposes.
// Output our errors/notices in the admin dashboard.
add_action( 'all_admin_notices', 'qp_api_show_sample_connection_notices' );
// Get the connect object
$api_connect = new Zao\QBO_API\Connect();
// Consumer credentials
$client = array(
// App credentials set up on the server
'client_id' => 'YOUR CLIENT KEY',
'client_secret' => 'YOUR CLIENT SECRET',
// Must match stored callback URL setup on server.
'callback_uri' => admin_url() . '?api-connect',
// Test in sandbox mode.
'sandbox' => true,
// 'autoredirect_authoriziation' => false,
);
/*
* Initate the API connection.
*
* if the oauth connection is not yet authorized, (and autoredirect_authoriziation
* is not explicitly set to false) you will be auto-redirected to the other site to
* receive authorization.
*/
$initiated = $api_connect->init( $client );
// Remove old errors
$api_connect->delete_stored_error();
// If you need to reset the stored connection data for any reason:
if ( isset( $_GET['reset-connection'] ) ) {
$api_connect->reset_connection();
wp_die( 'Connection deleted. <a href="'. esc_url( remove_query_arg( 'reset-connection' ) ) .'">Try again?</a>' );
}
// If oauth discovery failed, the WP_Error object will explain why.
if ( is_wp_error( $initiated ) ) {
// Save this error to the library's error storage (to output as admin notice)
return $api_connect->update_stored_error( $initiated );
}
/*
* if autoredirect_authoriziation IS set to false, you'll need to use the
* authorization URL to redirect the user to login for authorization.
*/
// $authorization_url = $api_connect->get_full_authorization_url();
// wp_redirect( $authorization_url );
// exit();
//
// // OR:
// $maybe_error = $api_connect->redirect_to_login();
// if ( is_wp_error( $maybe_error ) ) {
// wp_die( $authorization_url->get_error_message(), $authorization_url->get_error_code() );
// }
}
add_action( 'admin_init', 'qp_api_initiate_sample_connection' );
function qp_api_show_sample_connection_notices() {
global $api_connect;
/*
* If something went wrong in the process, errors will be stored.
* We can fetch them this way.
*/
if ( $api_connect->get_stored_error() ) {
$message = '<div id="message" class="error"><p><strong>Error Message:</strong> ' . $api_connect->get_stored_error_message() . '</p></div>';
$message .= '<div id="message" class="error"><p><strong>Error request arguments:</strong></p><xmp>' . $api_connect->get_stored_error_request_args() . '</xmp></div>';
// Output message, and bail.
return print( $message );
}
$reset_button = '<p><a class="button-secondary" href="'. add_query_arg( 'reset-connection', true ) .'">' . __( 'Reset Connection', 'rest-connect-ui' ) . '</a></p>';
$company = $api_connect->get_company_info();
if ( is_wp_error( $company ) ) {
echo '<div id="message" class="error">';
echo wpautop( $company->get_error_message() );
echo '</div>';
} else {
$props = array();
foreach ( get_object_vars( $company ) as $prop_name => $prop_value ) {
$props[] = '<tr><td>'. print_r( $prop_name, true ) .'</td><td>'. print_r( $prop_value, true ) .'</td></tr>';
}
echo '<div id="message" class="updated">';
echo '
<table class="wp-list-table widefat">
<thead>
<tr>
<th>' . __( 'Connected Company Name', 'qbo-connect-ui' ) . '</th>
<th>' . __( 'Connected Company ID', 'qbo-connect-ui' ) . '</th>
</tr>
</thead>
<tbody>
<tr>
<td>'. esc_html( $company->CompanyName ) .'</td>
<td>'. esc_html( $company->Id ) .'</td>
</tr>
</tbody>
</table>
<br>
<table class="wp-list-table widefat">
<thead>
<tr>
<th>'. __( 'Company Property:', 'qbo-connect-ui' ) .'</th>
<th>'. __( 'Company Property Value:', 'qbo-connect-ui' ) .'</th>
</tr>
</thead>
<tbody>
'. implode( "\n", $props ) .'
</tbody>
</table>
';
echo '</div>';
}
}