-
Notifications
You must be signed in to change notification settings - Fork 4
/
twitchcraft.php
440 lines (358 loc) · 10.8 KB
/
twitchcraft.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/**
* @twitchcraft
* Plugin Name: TwitchCraft
* Description: A WordPress plugin that allows a user to conditionally load content based on whether or not a Twitch streamer is broadcasting live.
* Version: 2.0
* Author: Mitch Canter
* Author URI: http://www.mitchcanter.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: twitchcraft
*/
/*
Twitch IS live Shortcode
* [twitch_live user='username']
* Displays content the channel IS live. Shortcode Usage
*/
function twitch_shortcodes() {
add_shortcode( 'twitch_live', 'twitch_is_live_shortcode' );
add_shortcode( 'twitch_offline', 'twitch_is_not_live_shortcode' );
}
add_action( 'init', 'twitch_shortcodes' );
function twitch_is_live_shortcode( $atts = array(), $content = null ) {
$atts = array_change_key_case( (array) $atts, CASE_LOWER );
$twitch_is_live_atts = shortcode_atts(
array( 'user' => 'lofilion' ),
$atts
);
$client_id = get_option( 'twitch_client_id' );
$client_id = $client_id['client_id'];
$client_secret = get_option( 'twitch_client_id' );
$client_secret = $client_secret['client_secret'];
if ( strlen( $client_id ) < 1 ) {
print 'No Client ID Specified!';
return false;
} elseif ( strlen( $client_secret ) < 1 ) {
print 'No Client Secret Specified!';
return false;
} else {
// all systems go. let's get an access token!
$url = 'https://id.twitch.tv/oauth2/token?client_id=' . $client_id . '&client_secret=' . $client_secret . '&grant_type=client_credentials';
$response = wp_remote_post( $url );
if ( ! is_wp_error( $response ) ) {
$stream_obj = json_decode( $response['body'] );
if ( $stream_obj ) {
// grab the token. store the token. love the token.
$token = $stream_obj->access_token;
// all systems go. grab the user data!
$user = $atts['user'];
$url = 'https://api.twitch.tv/helix/streams?user_login=' . $user;
$args = array(
'headers' => array(
'client-id' => $client_id,
'Authorization' => 'Bearer ' . $token,
),
);
$response = wp_remote_get( $url, $args );
if ( ! is_wp_error( $response ) ) {
$stream = json_decode( $response['body'] );
if ( $stream->data ) {
return $content;
} else {
return false;
}
} else {
print 'Unable to access Twitch API.';
return false;
}
} else {
echo 'Unable to Authenticate.';
return false;
}
} else {
print 'Unable to access Twitch API.';
return false;
}
}
}
/*
* Twitch IS NOT live Shortcode
* [twitch_offline user='username']
* Displays content the channel IS NOT live. Shortcode Usage
*/
function twitch_is_not_live_shortcode( $atts = array(), $content = null ) {
$atts = array_change_key_case( (array) $atts, CASE_LOWER );
$twitch_is_live_atts = shortcode_atts(
array( 'user' => 'lofilion' ),
$atts
);
$client_id = get_option( 'twitch_client_id' );
$client_id = $client_id['client_id'];
$client_secret = get_option( 'twitch_client_id' );
$client_secret = $client_secret['client_secret'];
if ( strlen( $client_id ) < 1 ) {
print 'No Client ID Specified!';
return false;
} elseif ( strlen( $client_secret ) < 1 ) {
print 'No Client Secret Specified!';
return false;
} else {
// all systems go. let's get an access token!
$url = 'https://id.twitch.tv/oauth2/token?client_id=' . $client_id . '&client_secret=' . $client_secret . '&grant_type=client_credentials';
$response = wp_remote_post( $url );
if ( ! is_wp_error( $response ) ) {
$stream_obj = json_decode( $response['body'] );
if ( $stream_obj ) {
// grab the token. store the token. love the token.
$token = $stream_obj->access_token;
// all systems go. grab the user data!
$user = $atts['user'];
$url = 'https://api.twitch.tv/helix/streams?user_login=' . $user;
$args = array(
'headers' => array(
'client-id' => $client_id,
'Authorization' => 'Bearer ' . $token,
),
);
$response = wp_remote_get( $url, $args );
if ( ! is_wp_error( $response ) ) {
$stream = json_decode( $response['body'] );
if ( ! $stream->data ) {
return $content;
} else {
return false;
}
} else {
print 'Unable to access Twitch API.';
return false;
}
} else {
echo 'Unable to Authenticate.';
return false;
}
} else {
print 'Unable to access Twitch API.';
return false;
}
}
}
/**
* Options Panel
*
*/
class TwitchConditionalSettings {
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page() {
// This page will be under "Settings".
add_options_page(
'Settings Admin',
'Twitch Conditional',
'manage_options',
'twitch-conditional-settings',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page() {
// Set class property.
$this->options = get_option( 'twitch_client_id' );
?>
<div class="wrap">
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields.
settings_fields( 'twitch_options' );
do_settings_sections( 'twitch-conditional-settings' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init() {
register_setting(
'twitch_options', // Option group.
'twitch_client_id', // Option name.
array( $this, 'sanitize' ) // Sanitize.
);
add_settings_section(
'twitch_section_id', // ID.
'Twitch Conditional Settings', // clientid.
array( $this, 'print_section_info' ), // Callback.
'twitch-conditional-settings' // Page.
);
add_settings_field(
'client_id',
'Client ID',
array( $this, 'clientid_callback' ),
'twitch-conditional-settings',
'twitch_section_id'
);
add_settings_field(
'client_secret',
'Client Secret',
array( $this, 'clientsecret_callback' ),
'twitch-conditional-settings',
'twitch_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys.
*/
public function sanitize( $input ) {
$new_input = array();
if ( isset( $input['client_id'] ) ) {
$new_input['client_id'] = sanitize_text_field( $input['client_id'] );
}
if ( isset( $input['client_secret'] ) ) {
$new_input['client_secret'] = sanitize_text_field( $input['client_secret'] );
}
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info() {
print 'Please visit <a href="https://www.twitch.tv/settings/connections">your Twitch "Connections" Settings page</a> and register a new Developer Application. Once you have done this, enter your Client ID Below. Twitch requires a client ID with any API Requests.';
}
/**
* Get the settings option array and print one of its values
*/
public function clientid_callback() {
printf(
'<input type="text" id="client_id" name="twitch_client_id[client_id]" value="%s" />',
isset( $this->options['client_id'] ) ? esc_attr( $this->options['client_id'] ) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function clientsecret_callback() {
printf(
'<input type="text" id="client_secret" name="twitch_client_id[client_secret]" value="%s" />',
isset( $this->options['client_secret'] ) ? esc_attr( $this->options['client_secret'] ) : ''
);
}
}
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function create_block_twitchcraft_block_init() {
register_block_type(
__DIR__ . '/build',
array(
'render_callback' => 'render_block_twitchcraft',
)
);
}
add_action( 'init', 'create_block_twitchcraft_block_init' );
/**
* Displays the content on the frontend based on if a channel is live or not.
*
* @param array $attributes The attributes passed by the block.
* @param string $content The block content.
* @return string
*/
function render_block_twitchcraft( $attributes, $content ) {
ob_start();
$channel = is_channel_live( $attributes['userName'] );
if ( isset( $channel['is_live'] ) ) {
?>
<div><?php echo wp_kses_post( $attributes['liveContent'] ); ?></div>
<?php
} else {
?>
<div><?php echo wp_kses_post( $attributes['offlineContent'] ); ?></div>
<?php
}
return ob_get_clean();
}
if ( is_admin() ) {
$my_settings_page = new TwitchConditionalSettings();
}
/**
* Check if the Twitch Channel is currently live.
*
* @param string $username The channel name.
* @param array $twich_data an array of data with error information and channel status.
*/
function is_channel_live( $username ) {
$twitch_data = array();
$client_id = get_option( 'twitch_client_id' );
$client_id = $client_id['client_id'];
$client_secret = get_option( 'twitch_client_id' );
$client_secret = $client_secret['client_secret'];
if ( strlen( $client_id ) < 1 ) {
print 'No Client ID Specified!';
return false;
} elseif ( strlen( $client_secret ) < 1 ) {
print 'No Client Secret Specified!';
return false;
} else {
// all systems go. let's get an access token!
$url = sprintf( 'https://id.twitch.tv/oauth2/token?client_id=%s&client_secret=%s&grant_type=client_credentials', $client_id, $client_secret );
$response = wp_remote_post( $url );
if ( ! is_wp_error( $response ) ) {
$stream_obj = json_decode( $response['body'] );
if ( $stream_obj ) {
// grab the token. store the token. love the token.
$token = $stream_obj->access_token;
$url = sprintf( 'https://api.twitch.tv/helix/streams?user_login=%s', $username );
$args = array(
'headers' => array(
'client-id' => $client_id,
'Authorization' => 'Bearer ' . $token,
),
);
$response = wp_remote_get( $url, $args );
if ( ! is_wp_error( $response ) ) {
$stream = json_decode( $response['body'] );
if ( ! empty( $stream->data ) ) {
$twitch_data['is_live'] = true;
}
} else {
$twitch_data = array(
'message' => __( 'Unable to access Twitch API', 'twitchcraft' ),
'error' => true,
);
}
} else {
$twitch_data = array(
'message' => __( 'Unable to Authenticate.', 'twitchcraft' ),
'error' => true,
);
}
} else {
$twitch_data = array(
'message' => __( 'Unable to access Twitch API.', 'twitchcraft' ),
'error' => true,
);
}
}
return $twitch_data;
}