/
settings-twitter.php
171 lines (134 loc) · 3.88 KB
/
settings-twitter.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
168
169
<?php
/**
* Creates the settings page
*
* @since 3.7
* @version 3.7
* @package Uncanny_Automator
* @author Agustin B.
*/
namespace Uncanny_Automator;
class Twitter_Settings extends Settings\Premium_Integration_Settings {
/**
* @var \Uncanny_Automator\Twitter_Functions
*/
protected $functions;
protected $client;
protected $is_connected;
/**
* The default connection method.
*
* @var string
*/
protected $default_connection_type = 'hybrid';
/**
* Sets up the properties of the settings page
*/
public function set_properties() {
$this->functions = new Twitter_Functions();
// Define the ID
// This should go first
$this->set_id( 'twitter-api' );
// Set the icon
// This expects a valid <uo-icon> ID
// Check the Design Guidelines to see the list of valid IDs
$this->set_icon( 'TWITTER' );
// Set the name
// As this is the brand name, it probably shouldn't be translatable
$this->set_name( 'X/Twitter' );
$this->register_option( 'automator_twitter_api_key' );
$this->register_option( 'automator_twitter_api_secret' );
$this->register_option( 'automator_twitter_access_token' );
$this->register_option( 'automator_twitter_access_token_secret' );
// Set assets (optional)
$this->set_css( '/twitter/settings/assets/style.css' );
$this->set_js( '/twitter/settings/assets/script.js' );
}
public function get_status() {
try {
$this->client = $this->functions->get_client();
$this->is_connected = true;
} catch ( \Exception $th ) {
$this->client = false;
$this->is_connected = false;
}
// Set the status
// This expects a valid <uo-tab> status
// Check the Design Guidelines to see the list of valid statuses
return $this->is_connected ? 'success' : '';
}
/**
* Creates the output of the settings page
*/
public function output() {
if ( $this->functions->is_user_app_connected() ) {
include_once 'view-twitter-user-app.php';
return;
}
// Load view
include_once 'view-twitter.php';
}
/**
* settings_updated
*
* @return void
*/
public function settings_updated() {
try {
$client = array(
'api_key' => get_option( 'automator_twitter_api_key', '' ),
'api_secret' => get_option( 'automator_twitter_api_secret', '' ),
'oauth_token' => get_option( 'automator_twitter_access_token', '' ),
'oauth_token_secret' => get_option( 'automator_twitter_access_token_secret', '' ),
);
$user = $this->functions->verify_credentials( $client );
update_option( '_uncannyowl_twitter_settings', $client );
update_option( 'automator_twitter_user', $user );
$this->add_alert(
array(
'type' => 'success',
'heading' => __( 'You have successfully connected your X/Twitter account.', 'uncanny-automator' ),
)
);
$this->is_connected = true;
} catch ( \Exception $e ) {
$this->is_connected = false;
$this->set_status( '' );
$this->add_alert(
array(
'type' => 'error',
'heading' => 'Connection error',
'content' => __( 'There was an error connecting your X/Twitter account: ', 'uncanny-automator' ) . wp_json_encode( $e->getMessage() ),
)
);
delete_option( '_uncannyowl_twitter_settings' );
delete_option( 'automator_twitter_user' );
$this->set_default_connection_type( 'self-hosted' );
return;
}
}
/**
* Sets the default connection type.
*
* @param string $type "hybrid"|"self-hosted".
*
* @return void
*/
public function set_default_connection_type( $type ) {
if ( ! is_string( $type ) ) {
$this->default_connection_type = 'hybrid';
}
if ( ! in_array( $type, array( 'hybrid', 'self-hosted' ), true ) ) {
$this->default_connection_type = 'hybrid';
}
$this->default_connection_type = $type;
}
/**
* Retrieves the default connection type.
*
* @return string
*/
public function get_default_connection_type() {
return $this->default_connection_type;
}
}