-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwp-bitly.php
264 lines (213 loc) · 7.4 KB
/
wp-bitly.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
<?php
/**
* WP Bitly
* This plugin can be used to generate shortlinks for your websites posts, pages, and custom post types.
* Extremely lightweight and easy to set up!
*
* @package wp-bitly
* @author Temerity Studios <[email protected]>
* @author Chip Bennett
* @license GPL-2.0+
* @link http://wordpress.org/plugins/wp-bitly
* @wordpress-plugin
* Plugin Name: WP Bitly
* Plugin URI: http://wordpress.org/plugins/wp-bitly
* Description: WP Bitly can be used to generate shortlinks for your website posts, pages, and custom post types. Extremely lightweight and easy to set up!
* Version: 2.5.2
* Author: <a href="https://temeritystudios.com/">Temerity Studios</a>
* Text Domain: wp-bitly
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
*/
if (!defined('WPINC')) {
die;
}
define('WPBITLY_VERSION', ' 2.5.2');
define('WPBITLY_DIR', WP_PLUGIN_DIR . '/' . basename(dirname(__FILE__)));
define('WPBITLY_URL', plugins_url() . '/' . basename(dirname(__FILE__)));
define('WPBITLY_LOG', WPBITLY_DIR . '/log/debug.txt');
define('WPBITLY_ERROR', __('WP Bitly Error: No such option %1$s', 'wp-bitly'));
define('WPBITLY_OPTIONS', 'wpbitly-options');
define('WPBITLY_AUTHORIZED', 'wpbitly-authorized');
define('WPBITLY_BITLY_API', 'https://api-ssl.bitly.com/v3/');
define('WPBITLY_TEMERITY_API', 'https://api.temeritystudios.com/');
/**
* The primary controller class for everything wonderful that WP Bitly does.
* We're not sure entirely what that means yet; if you figure it out, please
* let us know and we'll say something snazzy about it here.
*
* @package wp-bitly
*/
final class WPBitly
{
/**
* @var $_instance An instance of ones own instance
*/
private static $_instance;
/**
* @var array The WP Bitly configuration is stored in here
*/
private $_options = array();
/**
* Returns a single instance of WPBitly.
*
* @since 2.0
* @static
* @uses WPBitly::populateOptions() To create our options array.
* @uses WPBitly::includes_files() To do something that sounds a lot like what it sounds like.
* @uses WPBitly::defineHooks() To set up any necessary WordPress hooks.
* @return WPBitly
*/
public static function getIn()
{
if (null === self::$_instance) {
self::$_instance = new self;
self::$_instance->populateOptions();
self::$_instance->includeFiles();
self::$_instance->defineHooks();
}
return self::$_instance;
}
/**
* Populate WPBitly::$options with the configuration settings.
*
* @since 2.0
*/
public function populateOptions()
{
$defaults = apply_filters('wpbitly_default_options', array(
'version' => WPBITLY_VERSION,
'oauth_token' => '',
'oauth_login' => '',
'post_types' => array('post', 'page'),
'debug' => false,
));
$this->_options = wp_parse_args(get_option(WPBITLY_OPTIONS), $defaults);
}
/**
* Save all current options to the database
*
* @since 2.4.0
*/
private function _saveOptions()
{
update_option('wpbitly-options', $this->_options);
}
/**
* Access to our WPBitly::$_options array.
*
* @since 2.2.5
* @param $option string The name of the option we need to retrieve
* @return mixed Returns the option
*/
public function getOption($option)
{
if (!isset($this->_options[ $option ])) {
trigger_error(sprintf(WPBITLY_ERROR, ' <code>' . $option . '</code>'), E_USER_ERROR);
}
return $this->_options[ $option ];
}
/**
* Sets a single WPBitly::$_options value on the fly
*
* @since 2.2.5
* @param $option string The name of the option we're setting
* @param $value mixed The value, could be bool, string, array
*/
public function setOption($option, $value)
{
if (!isset($this->_options[ $option ])) {
trigger_error(sprintf(WPBITLY_ERROR, ' <code>' . $option . '</code>'), E_USER_ERROR);
}
$this->_options[ $option ] = $value;
$this->_saveOptions();
}
/**
* Used to short circuit any shortlink functions if we haven't authenticated to Bitly
*
* @since 2.4.0
* @return bool
*/
public function isAuthorized()
{
return get_option(WPBITLY_AUTHORIZED, false);
}
/**
* @param bool $auth
*/
public function authorize($auth = true)
{
if ($auth != true) {
$auth = false;
}
update_option(WPBITLY_AUTHORIZED, $auth);
}
/**
* So many files! Without this function we'd probably include things
* in the wrong order or not at all, and wars would erupt across the planet.
*
* @since 2.0
*/
public function includeFiles()
{
require_once(WPBITLY_DIR . '/includes/functions.php');
if (is_admin()) {
require_once(WPBITLY_DIR . '/includes/class.wp-bitly-admin.php');
}
}
/**
* Hook any necessary WordPress actions or filters that we'll be needing in order to make
* the plugin work its magic. This method also registers our super amazing shortcode.
*
* @since 2.0
*/
public function defineHooks()
{
add_action('init', array($this, 'loadPluginTextdomain'));
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'addActionLinks'));
add_action('admin_bar_menu', 'wp_admin_bar_shortlink_menu', 90);
//add_action('save_post', 'wpbitly_generate_shortlink');
add_filter('pre_get_shortlink', 'wpbitly_get_shortlink', 20, 2);
add_shortcode('wpbitly', 'wpbitly_shortlink');
}
/**
* Add a settings link to the plugins page so people can figure out where we are.
*
* @since 2.0
*
* @param $links An array returned by WordPress with our plugin action links
*
* @return array The slightly modified 'rray.
*/
public function addActionLinks($links)
{
return array_merge(array('settings' => '<a href="' . admin_url('options-writing.php') . '">' . __('Settings', 'wp-bitly') . '</a>'), $links);
}
/**
* This would be much easier if we all spoke Esperanto (or Old Norse).
*
* @since 2.0
*/
public function loadPluginTextdomain()
{
$languages = apply_filters('wpbitly_languages_dir', WPBITLY_DIR . '/languages/');
$locale = apply_filters('plugin_locale', get_locale(), 'wp-bitly');
$mofile = $languages . $locale . '.mo';
if (file_exists($mofile)) {
load_textdomain('wp-bitly', $mofile);
} else {
load_plugin_textdomain('wp-bitly', false, $languages);
}
}
}
/**
* Call this in place of WPBitly::getIn()
*
* @return WPBitly
*/
function wpbitly()
{
return WPBitly::getIn(); // there.
}
wpbitly();