forked from rickard2/utcw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utcw.php
331 lines (292 loc) · 6.98 KB
/
utcw.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
<?php
/**
* Ultimate Tag Cloud Widget
* @author Rickard Andersson <[email protected]>
* @version 2.0
* @license GPLv2
* @package utcw
* @subpackage main
* @since 2.0
*/
if ( ! defined( 'ABSPATH' ) ) die();
/**
* Current version number
*
* @var string
* @since 2.0
*/
define( 'UTCW_VERSION', '2.0.1' );
/**
* If development mode is currently enabled
*
* @var bool
* @since 2.0
*/
define( 'UTCW_DEV', false );
/**
* Regular expression for matching hexadecimal colors
*
* @var string
* @since 2.0
*/
define( 'UTCW_HEX_COLOR_REGEX', '/#([a-f0-9]{6}|[a-f0-9]{3})/i' );
/**
* printf format for rendering hexadecimal colors
*
* @var string
* @since 2.0
*/
define( 'UTCW_HEX_COLOR_FORMAT', '#%02x%02x%02x' );
require_once 'utcw-config.php';
require_once 'utcw-widget.php';
require_once 'utcw-data.php';
require_once 'utcw-render.php';
require_once 'utcw-term.php';
/**
* Class for general plugin integration with WordPress
*
* @since 2.0
* @package utcw
* @subpackage main
*/
class UTCW_Plugin {
/**
* An array of which taxonomies are available
*
* @var array
* @since 2.0
*/
protected $allowed_taxonomies = array();
/**
* An array of which post types are available
*
* @var array
* @since 2.0
*/
protected $allowed_post_types = array();
/**
* Singleton instance
*
* @var UTCW_Plugin
* @since 2.0
*/
private static $instance;
/**
* Initializes the WordPress hooks needed
*
* @todo Add tests that these hooks are set
* @since 2.0
*/
private function __construct() {
add_action( 'admin_head-widgets.php', array( $this, 'init_admin_assets' ) );
add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
add_action( 'widgets_init', create_function( '', 'return register_widget("UTCW");' ) );
add_shortcode( 'utcw', array( $this, 'utcw_shortcode' ) );
}
/**
* Returns an instance of the plugin
*
* @static
* @return UTCW_Plugin
* @since 2.0
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Action handler for 'wp_loaded' hook
* Loads taxonomies and post types
*
* @since 2.0
*/
function wp_loaded() {
$this->allowed_taxonomies = get_taxonomies();
$this->allowed_post_types = get_post_types( array( 'public' => true ) );
}
/**
* Shortcode handler for 'utcw' hook
*
* @param array $args
*
* @return string
* @since 2.0
*/
function utcw_shortcode( array $args ) {
global $wpdb;
$config = new UTCW_Config( $args, $this );
$data = new UTCW_Data( $config, $this, $wpdb );
$render = new UTCW_Render( $config, $data, $this );
return $render->get_cloud();
}
/**
* Action handler for 'admin_head-widgets.php' hook
* Loads assets needed by the administration interface
*
* @since 2.0
*/
public function init_admin_assets() {
wp_enqueue_style( 'utcw-admin', plugins_url( 'ultimate-tag-cloud-widget/css/style.css' ), array(), UTCW_VERSION );
// In development mode, add the libraries and main file individually
if ( UTCW_DEV ) {
wp_enqueue_script( 'utcw-lib-jsuri', plugins_url( 'ultimate-tag-cloud-widget/js/lib/jsuri-1.1.1.min.js' ), array(), UTCW_VERSION, true );
wp_enqueue_script( 'utcw-lib-tooltip', plugins_url( 'ultimate-tag-cloud-widget/js/lib/tooltip.min.js' ), array( 'jquery' ), UTCW_VERSION, true );
wp_enqueue_script( 'utcw', plugins_url( 'ultimate-tag-cloud-widget/js/utcw.js' ), array( 'utcw-lib-jsuri', 'utcw-lib-tooltip', 'jquery' ), UTCW_VERSION, true );
} else {
wp_enqueue_script( 'utcw', plugins_url( 'ultimate-tag-cloud-widget/js/utcw.min.js' ), array( 'jquery' ), UTCW_VERSION, true );
}
}
/**
* Returns an array with the names of allowed taxonomies
*
* @return array
* @since 2.0
*/
public function get_allowed_taxonomies() {
return $this->allowed_taxonomies;
}
/**
* Returns allowed taxonomies as objects
*
* @return array
* @since 2.0
*/
public function get_allowed_taxonomies_objects() {
return get_taxonomies( array(), 'objects' );
}
/**
* Returns an array with taxonomy as key and an array of term objects for each taxonomy. Like:
*
* @return array
* @since 2.0
*/
public function get_terms() {
$terms = array();
foreach ( $this->get_allowed_taxonomies() as $taxonomy ) {
$terms[ $taxonomy ] = get_terms( $taxonomy );
}
return $terms;
}
/**
* Returns an array with the names of allowed post types
*
* @return array
* @since 2.0
*/
public function get_allowed_post_types() {
return $this->allowed_post_types;
}
/**
* Returns the users on this blog
*
* @return array
* @since 2.0
*/
function get_users() {
global $wp_version;
if ( (float)$wp_version < 3.1 ) {
return get_users_of_blog();
} else {
return get_users();
}
}
/**
* Saves the configuration
*
* @param string $name Name of configuration
* @param array $config Exported configuration from UTCW_Config
*
* @return bool
* @since 2.0
*/
function save_configuration( $name, array $config ) {
$configs = $this->get_configurations();
$configs[ $name ] = $config;
return update_option( 'utcw_saved_configs', $configs );
}
/**
* Loads saved configuration
*
* @param string $name Name of configuration
*
* @return array|bool Returns an instance array on success and boolean false on failure
* @since 2.0
*/
function load_configuration( $name ) {
$configs = $this->get_configurations();
if ( isset( $configs[ $name ] ) ) {
return $configs[ $name ];
}
return false;
}
/**
* Get saved configurations
*
* @return array
* @since 2.0
*/
function get_configurations() {
return get_option( 'utcw_saved_configs', array() );
}
/**
* Returns boolean true if the current page request is for an authenticated user
*
* @return bool
* @since 2.0
*/
public function is_authenticated_user() {
return is_user_logged_in();
}
/**
* Returns an absolute URI to the archive page for the term
*
* @param int $term_id Term ID
* @param string $taxonomy Taxonomy name
*
* @return string Returns URI on success and empty string on failure
* @since 2.0
*/
public function get_term_link( $term_id, $taxonomy ) {
$link = get_term_link( $term_id, $taxonomy );
return ! is_wp_error( $link ) ? $link : '';
}
/**
* Check if the term exist for taxonomy
*
* @param int $term_id Term ID
* @param string $taxonomy Taxonomy name
*
* @return bool
* @since 2.0
*/
public function check_term_taxonomy( $term_id, $taxonomy ) {
return ! ! get_term( $term_id, $taxonomy );
}
/**
* Apply filters
*
* @param string $tag
* @param mixed $value
*
* @return mixed|void
* @since 2.0
*/
public function apply_filters( $tag, $value ) {
return apply_filters( $tag, $value );
}
}
// Instantiates the plugin
UTCW_Plugin::get_instance();
/**
* Function for theme integration
*
* @param array $args
*
* @since 1.3
*/
function do_utcw( array $args ) {
$plugin = UTCW_Plugin::get_instance();
echo $plugin->utcw_shortcode( $args );
}