Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use dlvr.it shortlinks in WordPress
<?php
// http://pmg.co
class wpDlvrit
{
protected $key;
function __construct( $key = false )
{
$this->set_key( $key );
}
function set_key( $key )
{
if( $key )
{
$this->key = $key;
}
else
{
$opts = get_option( 'wpdlvrit_options' );
$key = isset( $opts['api_key'] ) ? $opts['api_key'] : new WP_Error();
if( is_wp_error( $key ) )
{
$key->add( 'no_api_key', __( "You haven't set up your Dlvr.it API key yet!" ) );
}
$this->key = $key;
}
}
function get_key()
{
return $this->key;
}
function get_routes( $format = 'json' )
{
$data = array(
'method' => 'POST',
'body' => array( 'key' => $this->get_key() )
);
$url = 'http://api.dlvr.it/1/routes.' . $format;
$response = wp_remote_post( $url, $data );
$response = $this->validate( $response );
return $response;
}
function get_accounts( $format = 'json' )
{
$data = array(
'method' => 'POST',
'body' => array( 'key' => $this->get_key() )
);
$url = 'http://api.dlvr.it/1/accounts.' . $format;
$response = wp_remote_post( $url, $data );
$response = $this->validate( $response );
return $response;
}
function get_shortlink( $long_url, $format = 'json' )
{
$data = array(
'method' => 'POST',
'body' => array(
'key' => $this->get_key(),
'url' => $long_url
)
);
$url = 'http://api.dlvr.it/1/shorten.' . $format;
$response = wp_remote_post( $url, $data );
$response = $this->validate( $response );
return $response;
}
function post_to_account( $account_id, $message, $format = 'json' )
{
$data = array(
'method' => 'POST',
'body' => array(
'key' => $this->get_key(),
'id' => $account_id,
'msg' => $message
)
);
$url = 'http://api.dlvr.it/1/postToAccount.' . $format;
$response = wp_remote_post( $url, $data );
$response = $this->validate( $response );
return $response;
}
function post_to_route( $account_id, $message, $format = 'json' )
{
$data = array(
'method' => 'POST',
'body' => array(
'key' => $this->get_key(),
'id' => $account_id,
'msg' => $message
)
);
$url = 'http://api.dlvr.it/1/postToRoute.' . $format;
$response = wp_remote_post( $url, $data );
$response = $this->validate( $response );
}
// Validate our responses ...
protected function validate( $response )
{
if( is_wp_error( $response ) || 200 != $response['response']['code'] ) return false;
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.