Skip to content

Commit

Permalink
Merge pull request #501 from bmlt-enabled/Fix-for-jsInFooter
Browse files Browse the repository at this point in the history
fix for jsInFooter
  • Loading branch information
otrok7 authored Nov 9, 2024
2 parents 0ebb5a6 + f6985da commit 2712ff7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 102 deletions.
161 changes: 69 additions & 92 deletions crouton.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Description: A tabbed based display for showing meeting information.
Author: bmlt-enabled
Author URI: https://bmlt.app
Version: 3.20.4
Version: 3.20.5
*/
/* Disallow direct access to the plugin file */
if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
Expand All @@ -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,43 +320,33 @@ 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;
return $output.'<div id="bmlt-tabs" class="bmlt-tabs hide"></div>';
}
private function inlineScript($s)
{
wp_add_inline_script('croutonjs', $s);
}
private function outputScript($s)
{
if (isset($this->options['jsInFooter'])) {
wp_add_inline_script('croutonjs', $s);
$s = "";
}
return $this->waitMsg.sprintf('%s<div id="bmlt-tabs" class="bmlt-tabs hide">%s</div>', $this->sharedRender(), $s);
}
public function tabbedUi($atts, $content = null)
/**
* 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 string
*/
public function replaceShortcodeWithStandardTags()
{
$this->hasMap = true;
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 @@ -341,50 +357,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 "<script type='text/javascript'>var crouton;jQuery(document).ready(function() { $croutonMap crouton = new Crouton($config); $renderCmd });</script>";
} else {
return isset($config) ? "<script type='text/javascript'>jQuery(document).ready(function() { crouton.setConfig($config); $renderCmd });</script>" : "";
}
$croutonMap = $this->getMapInitialization($mapConfig);
return "var crouton;jQuery(document).ready(function() { $croutonMap crouton = new Crouton($config); $renderCmd });";
}

private function renderTable($atts)
Expand All @@ -407,6 +390,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 @@ -442,7 +430,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 @@ -454,7 +442,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 @@ -519,7 +507,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 @@ -578,9 +565,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 @@ -710,14 +694,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'] == 1 ? '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
6 changes: 2 additions & 4 deletions croutonjs/src/js/crouton-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ function Crouton(config) {
if (showingNow!==null) filteredMeetings = self.meetingData.filter((m) => showingNow.includes(m.id_bigint));
var ids = getUniqueValuesOfKey(filteredMeetings, 'service_body_bigint');
var me = this;
self.getServiceBodies(ids).then(function (service_bodies) {
self.getServiceBodies(ids, false).then(function (service_bodies) {
var n = service_bodies.length;
var names = service_bodies.map((m)=>m['name']);
names.sort();
Expand All @@ -559,9 +559,7 @@ function Crouton(config) {
});
});
}
self.getServiceBodies = function(service_bodies_id) {
const requires_parents = true;

self.getServiceBodies = function(service_bodies_id, requires_parents=true) {
var url = this.config['root_server'] + '/client_interface/jsonp/?switcher=GetServiceBodies'
+ (requires_parents ? '&parents=1' : '') + getServiceBodiesQueryString(service_bodies_id);
return fetchJsonp(url)
Expand Down
10 changes: 5 additions & 5 deletions croutonjs/src/templates/header.hbs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{{startup this}}
{{#if this.config.header}}
{{#if this.config.include_weekday_button}}
<div class="bmlt-button-container"><a id="day" class="btn btn-primary btn-sm">{{getWord 'weekday'}}</a></div>
<div class="bmlt-button-container"><a id="day" tabindex=0 class="btn btn-primary btn-sm">{{getWord 'weekday'}}</a></div>
{{/if}}
{{#each this.config.button_filters}}
<div class="bmlt-button-container"><a id="filterButton_{{this.field}}" data-field="{{this.field}}" class="filterButton filterButtonLogic btn btn-primary btn-sm">{{getWord this.title}}</a></div>
<div class="bmlt-button-container"><a id="filterButton_{{this.field}}" tabindex=0 data-field="{{this.field}}" class="filterButton filterButtonLogic btn btn-primary btn-sm">{{getWord this.title}}</a></div>
{{/each}}
{{#each this.config.button_format_filters}}
<div class="bmlt-button-container"><a id="filterButton_{{this.field}}" data-field="{{this.field}}" class="filterButton filterButtonLogic btn btn-primary btn-sm">{{this.title}}</a></div>
<div class="bmlt-button-container"><a id="filterButton_{{this.field}}"tabindex=0 data-field="{{this.field}}" class="filterButton filterButtonLogic btn btn-primary btn-sm">{{this.title}}</a></div>
{{/each}}
{{#if this.config.map_page}}
<div class="bmlt-button-container"><a id="filterButton_embeddedMapPage" data-field="embeddedMapPage" class="filterButton displayTypeLogic btn btn-primary btn-sm">{{getWord 'map'}}</a></div>
<div class="bmlt-button-container"><a id="displayTypeButton_tablePages" data-field="tablePages" class="filterButton displayTypeLogic btn btn-primary btn-sm hide">{{getWord 'tabular'}}</a></div>
<div class="bmlt-button-container"><a id="filterButton_embeddedMapPage" tabindex=0 data-field="embeddedMapPage" class="filterButton displayTypeLogic btn btn-primary btn-sm">{{getWord 'map'}}</a></div>
<div class="bmlt-button-container"><a id="displayTypeButton_tablePages" tabindex=0 data-field="tablePages" class="filterButton displayTypeLogic btn btn-primary btn-sm hide">{{getWord 'tabular'}}</a></div>
{{/if}}

{{#each this.dropdownData}}
Expand Down
8 changes: 7 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: na, meeting list, meeting finder, maps, recovery, addiction, webservant, b
Requires at least: 4.0
Required PHP: 8.0
Tested up to: 6.6.2
Stable tag: 3.20.4
Stable tag: 3.20.5
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
crouton implements a Tabbed UI for BMLT.
Expand Down Expand Up @@ -36,6 +36,12 @@ https://demo.bmlt.app/crouton

== Changelog ==

= 3.20.5 =
* Fix list_service_bodies including the parent service body.
* Correctly call add_inline_script, and always use it.
* Bug fix for checkbox placing JS in footer
* Accessibility improvements

= 3.20.4 =
* Bug fix for fixing container height when using OSM

Expand Down

0 comments on commit 2712ff7

Please sign in to comment.