container
* and adds a JS script at the bottom of a page that adds the map to the created container
*
* @param $atts
* Attributes from the shortcode
*
* @return string
* String that replaces the [mapsvg id="xxx"] shortcode
*/
function renderShortcode( $atts ){
if(!isset($atts['id'])){
return 'Error: no ID in mapsvg shortcode.';
}
$mapsRepo = new MapsRepository();
$map = $mapsRepo->findById($atts['id']);
if (!$map){
return 'Map not found, please check "id" parameter in your shortcode.';
}
if(version_compare($map->version, '3.0.0', '<')){
return $this->renderShortcodeV2($atts);
}
$googleMapsApiKey = Options::get("google_api_key");
$map->withSchema();
$map->withRegions();
if(isset($map->options["database"]["loadOnStart"]) && $map->options["database"]["loadOnStart"] === true){
$map->withObjects();
}
// Check if map settings need to be upgraded
$updater = new MapUpdater();
$updater->maybeUpdate($map);
// Load JS/CSS files
static::addJsCss();
do_action('mapsvg_shortcode');
$js_mapsvg_options = json_encode($map, JSON_UNESCAPED_UNICODE);
$no_double_render = !empty($atts['no_double_render']) ? true : false;
// Prepare MapSVG container (short
$container_id = $no_double_render ? $map->id : $this->generateContainerId($map->id);
$data = '
';
$script = "';
$this->mapScripts[$container_id] = $script;
// Load MapSVG execution script at the bottom of the page
add_action('wp_footer', [$this,'outputJsScript'], 9998);
return $data;
}
function renderShortcodeV2( $atts ){
$db = Database::get();
static::addJsCssV2();
$res = $db->get_results(
$db->prepare("select * from $db->posts WHERE ID = %d", (int)$atts['id'])
);
$post = $res && isset($res[0]) ? $res[0] : array();
if (empty($post->ID))
return 'Map not found, please check "id" parameter in your shortcode.';
$data = '
';
$script = '';
}else{
$script .= 'jQuery("#mapsvg-'.$post->ID.'").mapSvg2('.$post->post_content.');';
}
$this->mapScripts[$post->ID] = $script;
// Load MapSVG execution script at the bottom of the page
add_action('wp_footer', [$this,'outputJsScript'], 9998);
return $data;
}
/**
* Generate container ID for the map
*/
function generateContainerId($mapId, $iteration = 0){
$iteration_str = '';
if($iteration !== 0){
$iteration_str = '-'.$iteration;
}
if(isset($this->mapScripts[$mapId.$iteration_str])){
$iteration++;
return $this->generateContainerId($mapId, $iteration);
} else {
return $mapId.$iteration_str;
}
}
/**
* Output MapSVG scripts
*/
function outputJsScript(){
foreach($this->mapScripts as $m){
echo $m;
}
}
}