-
Notifications
You must be signed in to change notification settings - Fork 4
/
wp-cdnjs.php
623 lines (535 loc) · 17.1 KB
/
wp-cdnjs.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
<?php
/*
Plugin Name: WP cdnjs
Plugin URI: http://wordpress.org/plugins/wp-cdnjs/
Description: Effortlessly include any CSS or JavaScript Library hosted at cdnjs.com on your WordPress site.
Version: 0.1.4
Author: Mindshare Labs / ANAGR.AM
Author URI: https://mindsharelabs.com/
License: GNU General Public License
License URI: license.txt
Text Domain: wp-cdnjs
Domain Path: /lang
*/
/**
*
* Copyright 2014 Mindshare Studios, Inc. (http://mind.sh/are/)
*
* Plugin template was forked from the WP Settings Framework by Gilbert Pellegrom http://dev7studios.com
* and the WordPress Plugin Boilerplate by Christopher Lamm http://www.theantichris.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 3, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
// deny direct access
if(!function_exists('add_action')) {
header('Status: 403 Forbidden');
header('HTTP/1.1 403 Forbidden');
exit();
}
if(!defined('WP_CDNJS_VERSION')) {
define('WP_CDNJS_VERSION', '0.1.4');
}
if(!defined('WP_CDNJS_MIN_WP_VERSION')) {
define('WP_CDNJS_MIN_WP_VERSION', '4.0');
}
if(!defined('WP_CDNJS_PLUGIN_NAME')) {
define('WP_CDNJS_PLUGIN_NAME', 'WP cdnjs');
}
if(!defined('WP_CDNJS_PLUGIN_SLUG')) {
define('WP_CDNJS_PLUGIN_SLUG', dirname(plugin_basename(__FILE__))); // plugin-slug
}
if(!defined('WP_CDNJS_DIR_PATH')) {
define('WP_CDNJS_DIR_PATH', plugin_dir_path(__FILE__));
}
if(!defined('WP_CDNJS_DIR_URL')) {
define('WP_CDNJS_DIR_URL', trailingslashit(plugins_url(NULL, __FILE__)));
}
if(!defined('WP_CDNJS_OPTIONS')) {
define('WP_CDNJS_OPTIONS', 'cdnjs');
}
if(!defined('WP_CDNJS_TEMPLATE_PATH')) {
define('WP_CDNJS_TEMPLATE_PATH', trailingslashit(get_template_directory()) . trailingslashit(WP_CDNJS_PLUGIN_SLUG));
// e.g. /wp-content/themes/__ACTIVE_THEME__/plugin-slug
}
// check WordPress version
global $wp_version;
if(version_compare($wp_version, WP_CDNJS_MIN_WP_VERSION, "<")) {
exit(WP_CDNJS_PLUGIN_NAME . ' requires WordPress ' . WP_CDNJS_MIN_WP_VERSION . ' or newer.');
}
if(!class_exists('WP_CDNJS')) : /**
* Class wp_cdnjs
*/ {
class wp_cdnjs {
/**
* @var wp_cdnjs_settings
*/
private $settings_framework;
/**
* CDNJS base URL
*
* @var string
*/
private $cdnjs_uri = '//cdnjs.cloudflare.com/ajax/libs/';
/**
* CDNJS API base URL
*
* @var string
*/
private $cdnjs_api_uri = '//api.cdnjs.com/';
/**
* Version of Select2 to use for the admin screens.
*
* @var string
*/
private $select2_version = '3.5.2';
/**
* Version of Font Awesome to use for the admin screens.
*
* @var string
*/
private $fa_version = '4.2.0';
/**
* Initialize the plugin. Set up actions / filters.
*
*/
public function __construct() {
// i8n
add_action('plugins_loaded', array($this, 'load_textdomain'));
// Admin scripts
add_action('admin_enqueue_scripts', array($this, 'register_admin_scripts'));
add_action('admin_enqueue_scripts', array($this, 'register_admin_styles'));
// Plugin action links
add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2);
// Activation hooks
register_activation_hook(__FILE__, array($this, 'activate'));
//register_deactivation_hook(__FILE__, array($this, 'deactivate'));
// Uninstall hook
register_uninstall_hook(WP_CDNJS_DIR_PATH . 'uninstall.php', NULL);
// Settings Framework
add_action('admin_menu', array($this, 'admin_menu'), 99);
require_once(WP_CDNJS_DIR_PATH . 'lib/settings-framework/settings-framework.php');
$this->settings_framework = new wp_cdnjs_settings(WP_CDNJS_DIR_PATH . 'views/wp-cdnjs-settings.php', WP_CDNJS_OPTIONS);
// Add an optional settings-framework validation filter (recommended)
add_filter($this->settings_framework->get_option_group() . '_validate', array($this, 'validate_settings'));
// Enqueue CDNJS libraries
add_action('init', array($this, 'init'), 0, 0);
}
/**
* Allows user to override the default action used to enqueue scripts.
* See FAQ in readme.txt for usage.
*
*/
public function init() {
$init_action = apply_filters('wp_cdnjs_init_action', 'init');
add_action($init_action, array($this, 'cdnjs_scripts'));
}
/**
* Returns the class name and version.
*
* @return string
*/
public function __toString() {
return get_class($this) . ' ' . $this->get_version();
}
/**
* Returns the plugin version number.
*
* @return string
*/
public function get_version() {
return WP_CDNJS_VERSION;
}
/**
* Returns the plugin URL
*
* @return string
*/
public function get_plugin_url() {
return WP_CDNJS_DIR_URL;
}
/**
* @return string
*/
public function get_plugin_path() {
return WP_CDNJS_DIR_PATH;
}
/**
* Register the plugin text domain for translation
*
*/
public function load_textdomain() {
load_plugin_textdomain('wp-cdnjs', FALSE, WP_CDNJS_DIR_PATH . '/lang');
}
/**
* Activation
*
* Adds default option to enable scripts if settings are not present in DB
*
*/
public function activate() {
$settings = $this->get_settings(WP_CDNJS_OPTIONS);
if(!$settings) {
$settings = update_option(WP_CDNJS_OPTIONS, array(
'cdnjs_settings_enable_scripts' => TRUE
));
}
}
/**
* Deactivation
*/
public function deactivate() {
}
/**
* Install
*/
public function install() {
}
/**
* WordPress options page
*
*/
public function admin_menu() {
// Settings page
add_submenu_page('options-general.php', __(WP_CDNJS_PLUGIN_NAME . ' Settings', 'wp-cdnjs'), __(WP_CDNJS_PLUGIN_NAME . ' Settings', 'wp-cdnjs'), 'manage_options', WP_CDNJS_PLUGIN_SLUG, array(
$this,
'settings_page'
));
}
/**
* Settings page
*
*/
public function settings_page() {
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"></div>
<h2><?php echo WP_CDNJS_PLUGIN_NAME; ?></h2>
<?php
// Output settings-framework form
$this->settings_framework->settings();
?>
</div>
<?php
// Get settings
$settings = $this->get_settings(WP_CDNJS_OPTIONS);
// for debugging
//echo '<pre>' . var_export($settings, TRUE) . '</pre>';
}
/**
* Settings validation
*
* @see $sanitize_callback from http://codex.wordpress.org/Function_Reference/register_setting
*
* @param $input
*
* @return mixed
*/
public function validate_settings($input) {
return $input;
}
/**
* Converts the settings-framework filename to option group id
*
* @param $settings_file string settings-framework file
*
* @return string option group id
*/
public function get_option_group($settings_file) {
$option_group = preg_replace("/[^a-z0-9]+/i", "", basename($settings_file, '.php'));
return $option_group;
}
/**
* Get the settings from a settings-framework file/option group
*
* @param $option_group string option group id
*
* @return array settings
*/
public function get_settings($option_group) {
return get_option($option_group);
}
/**
* Get a setting from an option group
*
* @param $option_group string option group id
* @param $section_id string section id
* @param $field_id string field id
*
* @return mixed setting or false if no setting exists
*/
public function get_setting($option_group, $section_id, $field_id) {
$options = get_option($option_group);
if(isset($options[$option_group . '_' . $section_id . '_' . $field_id])) {
return $options[$option_group . '_' . $section_id . '_' . $field_id];
}
return FALSE;
}
/**
* Programmatically inserts a setting into the options array.
*
* @param $option_group
* @param $section_id
* @param $field_id
* @param $value
*
* @return bool
*/
public function apply_setting($option_group, $section_id, $field_id, $value) {
$options = get_option($option_group);
if($options) {
$options[$option_group . '_' . $section_id . '_' . $field_id] = $value;
update_option($option_group, $options);
}
return FALSE;
}
/**
* Simple API function for adding CDN scripts via theme or plugin files.
*
* @param $scripts array An Array of script to enqueue. Once added the script will show up on the WP cdnjs admin screen.
* @param $scripts array [string]slug CDN slug
* @param $scripts array [array]details Array of settings for this library
* @param $details [string]name Name of the library to include.
* @param $details [string]version Library version.
* @param $details [array]assets Array of additional assets to include.
* @param $details [bool]location Boolean to include script in header or footer.
* @param $details [bool]enabled Should the script be enabled or disabled.
*
* @return bool
*/
public function register_script($scripts) {
$registered = $this->get_setting(WP_CDNJS_OPTIONS, 'settings', 'scripts', $scripts);
if(!is_array($registered)) {
$registered = array();
}
if(is_array($scripts)) {
$scripts = array_merge($scripts, $registered);
$this->apply_setting(WP_CDNJS_OPTIONS, 'settings', 'scripts', $scripts);
}
return FALSE;
}
/**
*
* Checks the options array to see if a specific CDN library is already enqueued. Does NOT check the enabled/disabled state, just checks to see if the user has a library added on the admin screen.
*
* @param string $script_slug Slug of the library to check.
*
* @return bool
*/
public function is_script_registered($script_slug) {
$script_slug = sanitize_key($script_slug);
if(isset($script_slug)) {
$search = $this->get_setting(WP_CDNJS_OPTIONS, 'settings', 'scripts');
if(is_array($search) && array_key_exists($script_slug, $search)) {
return TRUE;
} else {
return FALSE;
}
}
return FALSE;
}
/**
* Delete all the saved settings from a settings-framework file/option group
*
* @param $option_group string option group id
*/
public function delete_settings($option_group) {
delete_option($option_group);
}
/**
* Deletes a setting from an option group
*
* @param $option_group string option group id
* @param $section_id string section id
* @param $field_id string field id
*
* @return mixed setting or false if no setting exists
*/
public function delete_setting($option_group, $section_id, $field_id) {
$options = get_option($option_group);
if(isset($options[$option_group . '_' . $section_id . '_' . $field_id])) {
$options[$option_group . '_' . $section_id . '_' . $field_id] = NULL;
return update_option($option_group, $options);
}
return FALSE;
}
/**
* Looks up a CDN library by name, first checking to see if the data
* is already stored locally in a transient. Returns an array that can
* be directly inserted into wp-cdnjs' settings array.
*
* @param $search
* @return array|bool|mixed|string
*/
public function lookup_cdnjs_library($search) {
$transient_key = $this->get_transient_key($search);
// Check for transient to avoid an unneeded lookup, if none, grab remote json data from the CDN
if(FALSE === ($data = get_transient($transient_key))) {
$this->format_lookup_result($search);
set_transient($transient_key, $data, apply_filters('wp_cdnjs_update_interval', 7 * 24 * HOUR_IN_SECONDS));
}
// wrap the library data in a new array with the slug as the index
$data = array(sanitize_key($data['name']) => $data);
return $data;
}
/**
* @param $search
* @return bool
*/
public function format_lookup_result($search) {
// Get remote HTML file
//$protocol = is_SSL() ? 'https://' : 'http://';
$response = wp_remote_get('https:' . $this->cdnjs_api_uri . 'libraries?search=' . $search . '&fields=version,filename,description,assets');
// Check for error
if(is_wp_error($response)) {
//var_export($response);
return FALSE;
}
// Parse remote HTML file
$data = wp_remote_retrieve_body($response);
// Check for error
if(is_wp_error($data)) {
return FALSE;
} else {
$data = json_decode($data, TRUE);
// just grab the results sub array
$data = $data['results'][0];
// modify the assets array to only include the latest version files
if(array_key_exists('assets', $data)) {
$data['assets'] = $data['assets'][0]['files'];
// Loop through assets to see if .min version is available and use that
$final_assets = array();
foreach($data['assets'] as $key => $asset) {
// more cleanup
unset($data['assets'][$key]['size']);
//$data['assets'][$key]['name'] = $data['assets'][$key];
if(strpos($asset['name'], '.min.') !== FALSE) {
$final_assets[] = $asset['name'];
}
}
if(!empty($final_assets)) {
$data['assets'] = $final_assets;
}
$data['location'] = 0;
$data['enabled'] = 1;
}
// remove extra data from the array
unset($data['description']);
unset($data['latest']);
unset($data['filename']);
}
}
public function get_transient_key($key) {
return WP_CDNJS_PLUGIN_SLUG . '_' . sanitize_key($key);
}
/**
*
* Add a settings link to plugins page.
*
* @param $links
* @param $file
*
* @return array
*/
public function plugin_action_links($links, $file) {
if($file == plugin_basename(__FILE__)) {
$settings_link = '<a href="options-general.php?page=' . WP_CDNJS_PLUGIN_SLUG . '" title="' . __(WP_CDNJS_PLUGIN_NAME, 'wp-cdnjs') . '">' . __('Settings', 'wp-cdnjs') . '</a>';
array_unshift($links, $settings_link);
}
return $links;
}
/**
* Enqueue and register JavaScript
*/
public function register_admin_scripts() {
wp_enqueue_script('cdnjs-select2', $this->cdnjs_uri . 'select2/' . $this->select2_version . '/select2.min.js', array('jquery'));
wp_enqueue_script('jquery-ui-sortable');
//wp_enqueue_script('jquery-ui-core');
wp_register_script('wp-cdnjs', WP_CDNJS_DIR_URL . '/assets/js/wp-cdnjs.js');
$translation_array = array(
'add_assets' => __('Add Assets', 'wp-cdnjs'),
'footer' => __('Footer', 'wp-cdnjs'),
'header' => __('Header', 'wp-cdnjs'),
'inc_assets' => __('Included Assets', 'wp-cdnjs'),
'no_addl_assets' => __('Included Assets', 'wp-cdnjs'),
'remove' => __('Remove', 'wp-cdnjs'),
'search_placeholder' => __('Search cdnjs Libraries', 'wp-cdnjs'),
'version' => __('Version', 'wp-cdnjs')
);
wp_localize_script('wp-cdnjs', 'cdnjs_text', $translation_array);
wp_enqueue_script('wp-cdnjs');
}
/**
* Enqueue and register CSS
*/
public function register_admin_styles() {
wp_enqueue_style('cdnjs-select2', $this->cdnjs_uri . 'select2/' . $this->select2_version . '/select2.min.css');
wp_enqueue_style('cdnjs-select2-bootstrap', $this->cdnjs_uri . 'select2/' . $this->select2_version . '/select2-bootstrap.min.css');
wp_enqueue_style('cdnjs-font-awesome', $this->cdnjs_uri . 'font-awesome/' . $this->fa_version . '/css/font-awesome.min.css');
wp_enqueue_style('cdnjs-styles', WP_CDNJS_DIR_URL . '/assets/css/cdnjs-styles.min.css');
}
/**
* CDNJS scripts
*
*/
public function cdnjs_scripts() {
$enabled = $this->get_setting(WP_CDNJS_OPTIONS, 'settings', 'enable_scripts');
// filter to allow script in WordPress Admin area
$show_in_admin = apply_filters('wp_cdnjs_allow_in_admin', FALSE);
// check if output is enabled
if($enabled) {
// only output on frontend, unless $show_in_admin is also TRUE
if((is_admin() && $show_in_admin) || !is_admin()) {
foreach($this->get_setting(WP_CDNJS_OPTIONS, 'settings', 'scripts') as $plugin) {
//var_dump($plugin['assets']);
if($plugin['enabled']) {
foreach($plugin['assets'] as $asset) {
//var_dump($asset.' '.$this->get_file_extension($asset));
$asset_name = sanitize_title($asset);
switch($this->get_file_extension($asset)) {
case 'js':
wp_enqueue_script($asset_name . '-wp-cdnjs', $this->cdnjs_uri . $plugin['name'] . '/' . $plugin['version'] . '/' . $asset, array(), $plugin['version'], (bool) $plugin['location']);
break;
case 'css':
wp_enqueue_style($asset_name . '-wp-cdnjs', $this->cdnjs_uri . $plugin['name'] . '/' . $plugin['version'] . '/' . $asset, array(), $plugin['version']);
break;
}
}
}
}
}
}
}
/**
* Returns the file extension from a given asset.
*
* @param $file_name
*
* @return string
*/
private function get_file_extension($file_name) {
return substr(strrchr($file_name, '.'), 1);
}
}
}
endif;
$wp_cdnjs = new wp_cdnjs();
// for debugging
/*
$my_scripts = $wp_cdnjs->lookup_cdnjs_library('django.js');
if(!$wp_cdnjs->is_script_registered('django.js')) {
if($my_scripts) {
$wp_cdnjs->register_script($my_scripts);
}
}*/