/
readability-custom-url-shortener.php
68 lines (59 loc) · 2.21 KB
/
readability-custom-url-shortener.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
<?php
/*
Plugin Name: Custom URL Shortener (Readability)
Plugin URI: http://www.joedolson.com/wp-tweets-pro
Description: Adds support for Readability as a URL shortener in WP to Twitter
Version: 1.0.0
Author: Joseph Dolson
Author URI: http://www.joedolson.com/
*/
/**
* Add Readability as a selectable option.
*/
add_filter( 'wpt_choose_shortener', 'my_choose_shortener', 10, 2 );
function my_choose_shortener( $output, $shortener ) {
// use only lowercase alphanumerics, dashes, and underscores.
// Value will be sanitized using sanitize_key.
$output .= '<option value="readability"' . selected( $shortener, 'readability', false ) . '>Readability</option>';
return $output;
}
add_filter( 'wpt_shortener_settings', 'my_shortener_settings', 10, 4 );
function my_shortener_settings( $output, $selected ) {
if ( $selected == 'readability' ) {
$output .= 'No settings are required for your chosen URL shortener.';
}
return $output;
}
// This is an example of how you would do this if it were required.
/*
add_filter( 'wpt_save_shortener_settings', 'my_save_shortener_settings' );
function my_save_shortener_settings( $message ) {
if ( isset( $_POST['readability_api_key'] ) ) {
$api_key = sanitize_text_field( $_POST['readability_api_key'] );
update_option( 'readability_api_key', $api_key );
$message .= __( 'Your Readability API key has been updated.', 'textdomain' );
}
return $message;
}
*/
add_filter( 'wpt_do_shortening', 'my_do_shortening', 10, 6 );
function my_do_shortening( $shrink, $shortener, $url, $post_title, $post_ID, $testmode ) {
// ensure that the return value is always defined as a valid URL.
$shrink = $url;
if ( $shortener == 'readability' ) {
// if you needed an API key, you'd fetch it now.
//$apiKey = get_option( 'readability_api_key' );
$response = wp_remote_post( add_query_arg( 'url', $url, 'http://readability.com/api/shortener/v1/urls' ), array( 'body' => array( 'url' => $url ) ) );
if ( !is_wp_error( $response ) ) {
$json = json_decode( $response['body'] );
$meta = $json->meta;
// Ignore error and continue with default plug-in shortening.
if ( ! empty( $meta ) ) {
$shrink = $meta->rdd_url;
} else {
// this was an error
}
}
}
return $shrink;
}