forked from newcity/newcity-wp-shortcodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-local-scripts-shortcode.php
97 lines (80 loc) · 2.76 KB
/
class-local-scripts-shortcode.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
<?php
/**
*
* Defines custom TimberPost extensions
*
* @since 0.1.0
*
* @package NewCityCustom
* @author Jesse Janowiak <[email protected]>
*/
class NewCityLocalScriptsShortcode {
public function __construct() {
add_shortcode( 'local_script', array( 'NewCityLocalScriptsShortcode', 'local_script' ), 7 );
add_action( 'register_shortcode_ui', array($this, 'shortcode_ui_local_script') );
}
public static function safe_path( $string, $dir = true ) {
// Remove leading and trailing whitespace, then remove
// any periods or slashes at the beginning or end
$path = trim(trim($string), '/.');
// Remove any double slashes (`//`). This prevents attempts
// to inject full URLs
$path = preg_replace('/\/\/+/', '', $path);
if (! $dir ) {
// Slashes are allowed in directory values, but not in
// file names
$path = str_replace('/', '', $path);
}
return $path;
}
public static function local_script( $attr ) {
$default_script_path = 'js';
$options = get_option('newcity_shortcodes_options', false);
$attr = wp_parse_args(
$attr, array(
'script' => '',
'path' => $options['script_path'],
'dependencies' => 'jquery',
)
);
$path = $attr['path'] ? $attr['path'] : $default_script_path;
$path = self::safe_path($path);
$script = self::safe_path($attr['script'], false);
$full_path = get_site_url( 1, '/wp-content/themes/' . basename( get_stylesheet_directory() ) ) . '/' . $path . '/' . $script;
$full_path = esc_attr($full_path);
if (! file_exists($full_path)) {
$full_path = get_site_url( 1, '/wp-content/themes/' . basename( get_template_directory() ) ) . '/' . $path . '/' . $script;
$full_path = esc_attr($full_path);
}
wp_enqueue_script( str_replace('/', '_', $full_path ), esc_attr($full_path . '.js'), $attr['dependencies'], false, true );
return '';
}
function shortcode_ui_local_script() {
$fields = array(
array(
'label' => esc_html__( 'Script Name (without .js)', 'shortcode-ui-local-script', 'shortcode-ui' ),
'attr' => 'script',
'type' => 'text',
'encode' => true
),
);
$shortcode_ui_args = array(
/*
* How the shortcode should be labeled in the UI. Required argument.
*/
'label' => esc_html__( 'Enqueue Local Script File', 'shortcode-ui-local-script', 'shortcode-ui' ),
/*
* Include an icon with your shortcode. Optional.
* Use a dashicon, or full HTML (e.g. <img src="/path/to/your/icon" />).
*/
'listItemImage' => 'dashicons-editor-code',
/*
* Define the UI for attributes of the shortcode. Optional.
*
* See above, to where the the assignment to the $fields variable was made.
*/
'attrs' => $fields,
);
shortcode_ui_register_for_shortcode( 'local_script', $shortcode_ui_args );
}
}