Skip to content

Commit

Permalink
Calculate the script for shortcodes when enqueing scripts, then alway…
Browse files Browse the repository at this point in the history
…s use add_inline_script
  • Loading branch information
otrok7 committed Nov 9, 2024
1 parent 8793e4e commit d017220
Showing 1 changed file with 68 additions and 94 deletions.
162 changes: 68 additions & 94 deletions crouton.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ class Crouton
* @var mixed[]
*/
public $options = array();
public $croutonBlockInitialized = false;
public static $HOUR_IN_SECONDS = 3600;
public $has_handlebars = false;
public $themes = [
"asheboro",
"florida-nights",
Expand All @@ -51,12 +49,10 @@ class Crouton
'timeout' => 60
);
// crouton includes a map, we need to include the JS files and create the croutonMap object.
private $hasMap = false;
public $shortCodeOptions = array(
"root_server" => '',
"service_body" => '',
"service_body_parent" => '',
"jsInFooter" => true,
"venue_types" => '',
"formats" => '',
"has_tabs" => '1',
Expand Down Expand Up @@ -130,7 +126,6 @@ class Crouton
"filter_visible",
"has_common_needs"
];
private $waitMsg = '<div class="bootstrap-bmlt" id="please-wait"><button class="btn btn-lg btn-info"><span class="glyphicon glyphicon-repeat glyphicon-repeat-animate"></span>Fetching...</button></div>';
private MeetingMap\Controller $meetingMapController;
public function __construct()
{
Expand All @@ -146,19 +141,19 @@ public function __construct()
add_action("wp_enqueue_scripts", array(&$this, "enqueueFrontendFiles"));
add_shortcode('init_crouton', array(
&$this,
"initCrouton"
"blank"
));
add_shortcode('bmlt_tabs', array(
&$this,
"tabbedUi"
"replaceShortcodeWithStandardTags"
));
add_shortcode('crouton_map', array(
&$this,
"croutonMap"
"replaceShortcodeWithStandardTags"
));
add_shortcode('bmlt_map', array(
&$this,
"meetingMap"
"replaceShortcodeWithStandardTags"
));
add_shortcode('bmlt_count', array(
&$this,
Expand All @@ -182,26 +177,55 @@ public function __construct()
));
}
}

private function hasShortcode()
/**
*
* @return string
*/
private function croutonInitializationScript()
{
$post_to_check = get_post(get_the_ID());
$post_content = $post_to_check->post_content ?? '';
$tags = ['bmlt_tabs', 'bmlt_map', 'crouton_map', 'bmlt_count', 'meeting_count', 'group_count', 'service_body_names', 'bmlt_handlebar'];
$tags = ['bmlt_tabs', 'bmlt_map', 'crouton_map', 'bmlt_handlebar'];
preg_match_all('/' . get_shortcode_regex($tags) . '/', $post_content, $matches, PREG_SET_ORDER);
if (empty($matches)) {
return false;
return '';
}

foreach ($matches as $shortcode) {
if ($shortcode[2] === 'bmlt_handlebar' ||
$shortcode[2] === 'bmlt_tabs' ||
$shortcode[2] === 'crouton_map' ||
$shortcode[2] === 'bmlt_map') {
$this->hasMap = true;
if (isset($_GET['meeting-id'])) {
$shortcode[2] = 'bmlt_handlebar';
$shortcode[3] = '';
}
switch ($shortcode[2]) {
case 'bmlt_tabs':
$script = $this->renderTable(wp_parse_args($shortcode[3]));
break;
case 'init_crouton':
$script = $this->initCrouton(wp_parse_args($shortcode[3]));
break;
case 'crouton_map':
$script = $this->renderMap(wp_parse_args($shortcode[3]));
break;
case 'bmlt_map':
$atts = wp_parse_args($shortcode[3]);
if (is_array($atts)) {
$atts['has_venues'] = '0';
} else {
$atts = ["has_venues" => "0"];
}
$script = $this->renderMap($atts, false);
break;
case 'bmlt_handlebar':
$script = $this->handlebarFooterScript();
break;
default:
$script = '';
break;
}
if ($script != '') {
return $script;
}
}

return true;
}

Expand Down Expand Up @@ -245,7 +269,8 @@ public function enqueueBackendFiles($hook)
*/
public function enqueueFrontendFiles()
{
if ($this->hasShortcode()) {
$script = $this->croutonInitializationScript();
if ($script !== '') {
wp_enqueue_style("croutoncss", plugin_dir_url(__FILE__) . "croutonjs/dist/crouton-core.min.css", false, filemtime(plugin_dir_path(__FILE__) . "croutonjs/dist/crouton-core.min.css"), false);
if (isset($_GET['croutonjsdebug'])) {
wp_enqueue_script("croutonnocorejs", plugin_dir_url(__FILE__) . "croutonjs/dist/crouton-nocore.js", array('jquery'), filemtime(plugin_dir_path(__FILE__) . "croutonjs/dist/crouton-nocore.js"), true);
Expand All @@ -254,6 +279,7 @@ public function enqueueFrontendFiles()
wp_enqueue_script("croutonjs", plugin_dir_url(__FILE__) . "croutonjs/dist/crouton-core.min.js", array('jquery'), filemtime(plugin_dir_path(__FILE__) . "croutonjs/dist/crouton-core.min.js"), true);
}
$this->meetingMapController->enqueueFrontendFiles();
wp_add_inline_script("croutonjs", $script);
}
}

Expand Down Expand Up @@ -294,45 +320,32 @@ private function testRootServer($root_server)

return '';
}

private function sharedRender()
private function outputTag()
{
$output = "";
$output = '<div class="bootstrap-bmlt" id="please-wait"><button class="btn btn-lg btn-info"><span class="glyphicon glyphicon-repeat glyphicon-repeat-animate"></span>Fetching...</button></div>';
if (isset($_GET['this_title'])) {
$output .= '<div class="bmlt_tabs_title">' . $_GET['this_title'] . '</div>';
}

if (isset($_GET['sub_title'])) {
$output .= '<div class="bmlt_tabs_sub_title">' . $_GET['sub_title'] . '</div>';
}

return $output;
}
private function inlineScript($s)
{
wp_add_inline_script('croutonjs', $s);
}
private function outputScript($s)
{
if ($this->options['jsInFooter']) {
wp_add_inline_script('croutonjs', $s);
$s = "";
} else {
$s = "<script type='text/javascript'>$s</script>";
}
return $this->waitMsg.sprintf('%s<div id="bmlt-tabs" class="bmlt-tabs hide">%s</div>', $this->sharedRender(), $s);
return $output.'<div id="bmlt-tabs" class="bmlt-tabs hide"></div>';
}
public function tabbedUi($atts, $content = null)
{
$this->hasMap = true;
/**
* When we are processing the main shortcodes themselves, we can just insert standard tags, because the difference is
* in how we initialize the JS Crouton object. And we do that when deciding whether to enqueue scripts or not.
*
* @return void
*/
public function replaceShortcodeWithStandardTags() {
if (isset($_GET['meeting-id'])) {
return do_shortcode($this->getDefaultMeetingDetailsPageContents());
}
return $this->outputScript($this->renderTable($atts));
return $this->outputTag();
}
public function bmltHandlebar($atts, $template = null)
{
$this->hasMap = true;
if (!isset($_GET['meeting-id'])) {
return "Meeting-ID not set in query-string";
}
Expand All @@ -343,50 +356,17 @@ public function bmltHandlebar($atts, $template = null)
return '';
}
}
if (!$this->has_handlebars) {
$this->handlebarFooter();
}
$this->has_handlebars = true;
return sprintf('<bmlt-handlebar style="display:none;"><span style="display:none;">%s</span>Fetching...</bmlt-handlebar>', htmlspecialchars($template));
}
public function croutonMap($atts, $content = null)
{
$this->hasMap = true;
if (isset($_GET['meeting-id'])) {
return do_shortcode($this->getDefaultMeetingDetailsPageContents());
}
return $this->outputScript($this->renderMap($atts));
}
public function meetingMap($atts, $content = null)
{
$this->hasMap = true;
if (isset($_GET['meeting-id'])) {
return do_shortcode($this->getDefaultMeetingDetailsPageContents());
}
if (is_array($atts)) {
$atts['has_venues'] = '0';
} else {
$atts = ["has_venues" => "0"];
}
return $this->outputScript($this->renderMap($atts, false));
}
private function getMapInitialization($mapConfig)
{
$className = $this->meetingMapController->className();
if ($this->hasMap) {
return "window.croutonMap = new $className($mapConfig);";
}
return "";
return "window.croutonMap = new $className($mapConfig);";
}
private function getInitializeCroutonBlock($renderCmd, $config, $mapConfig)
{
if (!$this->croutonBlockInitialized) {
$this->croutonBlockInitialized = true;
$croutonMap = $this->getMapInitialization($mapConfig);
return "var crouton;jQuery(document).ready(function() { $croutonMap crouton = new Crouton($config); $renderCmd });";
} else {
return isset($config) ? "jQuery(document).ready(function() { crouton.setConfig($config); $renderCmd });" : "";
}
$croutonMap = $this->getMapInitialization($mapConfig);
return "var crouton;jQuery(document).ready(function() { $croutonMap crouton = new Crouton($config); $renderCmd });";
}

private function renderTable($atts)
Expand All @@ -409,6 +389,11 @@ public function initCrouton($atts)
return $this->getInitializeCroutonBlock("crouton.renderMeetingCount();", ...$this->getCroutonJsConfig($atts));
}

public function blank()
{
return '';
}

public function meetingCount($atts)
{
if (isset($_GET['meeting-id'])) {
Expand Down Expand Up @@ -444,7 +429,7 @@ public function serviceBodyNames($atts)
}
return "<span id='bmlt_tabs_service_body_names$live'>Fetching...</span>";
}
public function handlebarFooter()
public function handlebarFooterScript()
{
if (!isset($_GET['meeting-id'])) {
return;
Expand All @@ -456,7 +441,7 @@ public function handlebarFooter()
$croutonMap = $this->getMapInitialization($mapConfig);
$ret = "var crouton;"
."jQuery(document).ready(function() { $croutonMap crouton = new Crouton($config); crouton.doHandlebars();})";
$this->inlineScript($ret);
return $ret;
}
/**
* @desc Adds the options sub-panel
Expand Down Expand Up @@ -521,7 +506,6 @@ public function adminOptionsPage()
}
$this->options['root_server'] = $_POST['root_server'];
$this->options['service_body_1'] = $_POST['service_body_1'];
$this->options['jsInFooter'] = isset($_POST['jsInFooter']);
$this->options['time_format'] = $_POST['time_format'];
$this->options['language'] = $_POST['language'];
$this->options['strict_datafields'] = isset($_POST['strict_datafields']);
Expand Down Expand Up @@ -580,9 +564,6 @@ public function adminOptionsPage()
} else {
$this->options['extra_meetings_enabled'] = 1;
}
if (!isset($this->options['jsInFooter'])) {
$this->options['jsInFooter'] = true;
}
?>
<div class="wrap">
<div id="tallyBannerContainer">
Expand Down Expand Up @@ -712,14 +693,7 @@ public function adminOptionsPage()
<input id="custom_query" name="custom_query" size="50" value="<?php echo (isset($this->options['custom_query']) ? $this->options['custom_query'] : ""); ?>" />
</li>
</ul>
</div>
<div style="padding: 0 15px;" class="postbox">
<h3><a id="config-advanced" class="anchor"></a>Advanced Options</h3>
<p>Should the generated Javascript be placed in the footer or in the body.</p>
<div>
<input type="checkbox" name="jsInFooter" value="1" <?php echo ($this->options['jsInFooter'] ? 'checked' : '') ?> />Place Javascript in Footer
</div>
</div>
</div>
</div>
<div id="crouton-ui" class=tab-content>
<div style="padding: 0 15px;" class="postbox">
Expand Down

0 comments on commit d017220

Please sign in to comment.