11<?php
22/**
33 * Jacobin Core Register Shortcodes
4- *
4+ *
55 * @package Jacobin_Core
66 * @subpackage Jacobin_Core\Includes
77 * @since 0.1.3
@@ -27,7 +27,7 @@ function __construct() {
2727 add_action ( 'init ' , array ( $ this , 'detect_shortcode_ui ' ) );
2828 add_action ( 'init ' , array ( $ this , 'register_shortcodes ' ) );
2929 add_action ( 'init ' , array ( $ this , 'register_shortcode_ui ' ) );
30-
30+
3131 }
3232
3333 /**
@@ -70,12 +70,14 @@ public function shortcode_ui_notices() {
7070 public function register_shortcodes () {
7171 add_shortcode ( 'embed-timeline ' , array ( $ this , 'embed_timeline_shortcode ' ) );
7272 add_shortcode ( 'embed-chart ' , array ( $ this , 'embed_chart_shortcode ' ) );
73+ add_shortcode ( 'embed-script ' , array ( $ this , 'embed_script_shortcode ' ) );
7374 }
7475
7576 /**
7677 * Register Shortcode with Shortcode UI
7778 *
7879 * @since 0.1.3
80+ * @since 0.4.14
7981 *
8082 * @param string $shortcode_tag
8183 * @param function $shortcode_function
@@ -84,6 +86,39 @@ public function register_shortcodes() {
8486 public function register_shortcode_ui () {
8587 add_action ( 'register_shortcode_ui ' , array ( $ this , 'embed_timeline_shortcode_ui ' ) );
8688 add_action ( 'register_shortcode_ui ' , array ( $ this , 'embed_chart_shortcode_ui ' ) );
89+ add_action ( 'register_shortcode_ui ' , array ( $ this , 'embed_script_shortcode_ui ' ) );
90+ }
91+
92+ /**
93+ * Callback for the `embed_script` shortcode
94+ * It renders the shortcode based on supplied attributes.
95+ *
96+ * @example `[embed-script src={url} defer=defer]`
97+ *
98+ * @since 0.4.14
99+ *
100+ * @param string $atts
101+ * @param string $content
102+ * @param string $shortcode_tag
103+ */
104+ public function embed_script_shortcode ( $ atts , $ content , $ shortcode_tag ) {
105+
106+ $ atts = shortcode_atts (
107+ array (
108+ 'src ' => '' ,
109+ 'defer ' => 'defer ' ,
110+ 'type ' => 'text/javascript ' ,
111+ ),
112+ $ atts ,
113+ $ shortcode_tag
114+ );
115+
116+ return sprintf ( '<script scr="%s" defer="%s" type="%s"></script> ' ,
117+ esc_url ( $ atts ['src ' ] ),
118+ esc_attr ( $ atts ['defer ' ] ),
119+ esc_attr ( $ atts ['type ' ] )
120+ );
121+
87122 }
88123
89124 /**
@@ -108,7 +143,7 @@ public function embed_timeline_shortcode( $attr, $content, $shortcode_tag ) {
108143
109144 // Output buffering here.
110145 ob_start ();
111-
146+
112147 include_once ( 'views/embed-timeline.php ' );
113148
114149 return ob_get_clean ();
@@ -136,7 +171,7 @@ public function embed_chart_shortcode( $attr, $content, $shortcode_tag ) {
136171
137172 // Output buffering here.
138173 ob_start ();
139-
174+
140175 include_once ( 'views/embed-chart.php ' );
141176
142177 return ob_get_clean ();
@@ -201,6 +236,34 @@ public function embed_chart_shortcode_ui() {
201236 shortcode_ui_register_for_shortcode ( 'embed-chart ' , $ shortcode_ui_args );
202237 }
203238
239+ /**
240+ * Embed Script Shortcode UI
241+ *
242+ * @since 0.4.14
243+ *
244+ */
245+ public function embed_script_shortcode_ui () {
246+ $ fields = array (
247+ array (
248+ 'label ' => esc_html__ ( 'Script URL ' , 'jacobin-core ' ),
249+ 'attr ' => 'src ' ,
250+ 'type ' => 'url ' ,
251+ ),
252+ );
253+
254+ /*
255+ * Define the Shortcode UI arguments.
256+ */
257+ $ shortcode_ui_args = array (
258+ 'label ' => esc_html__ ( 'Embed Script ' , 'jacobin-core ' ),
259+ 'listItemImage ' => 'dashicons-editor-code ' ,
260+ 'post_type ' => array ( 'post ' ),
261+ 'attrs ' => $ fields ,
262+ );
263+
264+ shortcode_ui_register_for_shortcode ( 'embed-script ' , $ shortcode_ui_args );
265+ }
266+
204267}
205268
206269new Jacobin_Register_Shortcodes ();
0 commit comments