-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitly-external.php
170 lines (146 loc) · 4.67 KB
/
bitly-external.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
<?php
/*
Plugin Name: Wordpress Bit.ly External URLS
Plugin URI: https://github.com/hmimthiaz/bitly-external
Description: This plugin helps to replace all the external URL to bit.ly link URL
Author: Imthiaz Rafiq
Version: 1.0 Alpha
Author URI: http://imthi.com/
*/
class IRBitlyExternal {
var $bitlyUsername;
var $bitlyApikey;
var $enabled;
var $tableBitlyExternal;
/**
*
* @var IRBitlyExternal
*/
protected static $_instance = null;
/*
*
* @var wpdb
*/
var $db;
private function __clone() {
}
/**
* Singleton instance
*
* @return IRBitlyExternal
*/
public static function getInstance() {
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
function __construct() {
global $wpdb;
$this->bitlyUsername = get_option('bitly_external_plugin_username');
$this->bitlyApikey = get_option('bitly_external_plugin_api_key');
$this->blockedDomains = get_option('bitly_external_plugin_blocked_domains');
if (!empty($this->blockedDomains)) {
$this->blockedDomains = explode(';', $this->blockedDomains);
}else{
$this->blockedDomains = $this->getUrlHostname(get_option('siteurl'));
update_option('bitly_external_plugin_blocked_domains', $this->blockedDomains);
}
if (!empty($this->bitlyUsername) && !empty($this->bitlyApikey)) {
$this->enabled = TRUE;
} else {
$this->enabled = FALSE;
}
$this->db = $wpdb;
$this->tableBitlyExternal = $wpdb->prefix . 'bitly_external';
register_activation_hook(plugin_basename(__FILE__), array(
&$this,
'activatePlugin'));
register_deactivation_hook(plugin_basename(__FILE__), array(
&$this,
'deactivatePlugin'));
add_action('admin_menu', array(&$this, 'adminMenu'));
if (isset($_GET ['page']) && $_GET ['page'] == 'bitlyexternal-options') {
ob_start();
}
if ($this->enabled) {
if (!function_exists('pq')) {
include_once 'lib/phpQuery.php';
}
if (!class_exists('Bitly')) {
include_once 'lib/bitly.php';
}
add_filter('the_content', array(&$this, 'theContent'));
}
}
function getUrlHostname($linkurl) {
return parse_url(strtolower($linkurl), PHP_URL_HOST);
}
function theContent($content) {
$doc = phpQuery::newDocumentHTML($content);
phpQuery::selectDocument($doc);
foreach (pq('a') as $link) {
$linkurl = pq($link)->attr('href');
$linkHostname = $this->getUrlHostname($linkurl);
if ($linkHostname != FALSE) {
if (!in_array($linkHostname, $this->blockedDomains)) {
$linkHash = md5($linkurl);
$query = "SELECT * FROM {$this->tableBitlyExternal} WHERE id='{$linkHash}'LIMIT 1;";
$linkData = $this->db->get_row($query, ARRAY_A);
if (empty($linkData)) {
$bitly = new Bitly($this->bitlyUsername, $this->bitlyApikey);
$shortURL = $bitly->shorten($linkurl);
$shortURLData = get_object_vars($bitly->getData());
if (!empty($shortURLData)) {
$linkData = array(
'id' => $linkHash,
'url' => $linkurl,
'short_url' => $shortURLData['shortUrl'],
'hash' => $shortURLData['userHash'],
'created' => current_time('mysql'));
$this->db->insert($this->tableBitlyExternal, $linkData);
}
}
if (!empty($linkData)) {
pq($link)->attr('href', $linkData['short_url']);
}
}
}
}
return $doc->htmlOuter();
}
function adminMenu() {
if (function_exists('add_submenu_page')) {
add_submenu_page('plugins.php', __('Bit.ly External'), __('Bit.ly External'), 'manage_options', 'bitlyexternal-options', array(
&$this,
'pluginOption'));
}
}
function pluginOption() {
if (isset($_POST ['Submit'])) {
if (function_exists('current_user_can') && !current_user_can('manage_options')) {
die(__('Cheatin’ uh?'));
}
update_option('bitly_external_plugin_username', $_POST ['bitly_external_plugin_username']);
update_option('bitly_external_plugin_api_key', $_POST ['bitly_external_plugin_api_key']);
update_option('bitly_external_plugin_blocked_domains', $_POST ['bitly_external_plugin_blocked_domains']);
ob_end_clean();
wp_redirect('plugins.php?page=bitlyexternal-options&msg=' . urlencode("Setting saved"));
exit();
}
include_once ('plugin-options.php');
}
function activatePlugin() {
$query = "CREATE TABLE IF NOT EXISTS `{$this->tableBitlyExternal}` (
`id` varchar(32) NOT NULL,
`url` varchar(255) NOT NULL,
`short_url` varchar(100) NOT NULL,
`hash` varchar(15) NOT NULL,
`created` datetime NOT NULL,
UNIQUE KEY `id` (`id`));";
$this->db->query($query);
}
function deactivatePlugin() {
}
}
$bitlyExternal = IRBitlyExternal::getInstance();