| 1 |
<?php |
| 2 |
/** |
| 3 |
* Abstract Shortcode |
| 4 |
* |
| 5 |
* @category Shortcode |
| 6 |
* @author Benjamin J DeLong <ben@bozdoz.com> |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Abstract Shortcode Class |
| 11 |
* |
| 12 |
* Use with add_shortcode('leaflet-map', array ('Leaflet_Shortcode', 'shortcode')) |
| 13 |
* extend and manipulate __construct |
| 14 |
*/ |
| 15 |
abstract class Leaflet_Shortcode |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @var Leaflet_Map |
| 19 |
*/ |
| 20 |
protected $LM; |
| 21 |
|
| 22 |
/** |
| 23 |
* Generate HTML from the shortcode |
| 24 |
* Maybe won't always be required |
| 25 |
* |
| 26 |
* @param array $atts string |
| 27 |
* @param string $content Optional |
| 28 |
* |
| 29 |
* @since 2.8.2 |
| 30 |
* |
| 31 |
* @return string (typically, return a script tag with Leaflet logic) |
| 32 |
*/ |
| 33 |
abstract protected function getHTML($atts='', $content=null); |
| 34 |
|
| 35 |
public static function getClass() |
| 36 |
{ |
| 37 |
return function_exists('get_called_class') ? get_called_class() : __CLASS__; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Instantiate class and get HTML for shortcode |
| 42 |
* |
| 43 |
* @param array|string|null $atts string|array |
| 44 |
* @param string|null $content Optional |
| 45 |
* |
| 46 |
* @return string HTML |
| 47 |
*/ |
| 48 |
public static function shortcode($atts = '', $content = null) |
| 49 |
{ |
| 50 |
$class = self::getClass(); |
| 51 |
$instance = new $class(); |
| 52 |
|
| 53 |
// swap sequential array with associative array |
| 54 |
// this enables assumed-boolean attributes, |
| 55 |
// like: [leaflet-marker draggable svg] |
| 56 |
// meaning draggable=1 svg=1 |
| 57 |
// and: [leaflet-marker !doubleClickZoom !boxZoom] |
| 58 |
// meaning doubleClickZoom=0 boxZoom=0 |
| 59 |
if (!empty($atts)) { |
| 60 |
foreach($atts as $k => $v) { |
| 61 |
if ( |
| 62 |
is_numeric($k) && |
| 63 |
!key_exists($v, $atts) && |
| 64 |
!!$v |
| 65 |
) { |
| 66 |
// false if starts with !, else true |
| 67 |
if ($v[0] === '!') { |
| 68 |
$k = substr($v, 1); |
| 69 |
$v = 0; |
| 70 |
} else { |
| 71 |
$k = $v; |
| 72 |
$v = 1; |
| 73 |
} |
| 74 |
$atts[$k] = $v; |
| 75 |
} |
| 76 |
// change hyphens to underscores for `extract()` |
| 77 |
if (strpos($k, '-')) { |
| 78 |
$k = str_replace('-', '_', $k); |
| 79 |
$atts[$k] = $v; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return $instance->getHTML($atts, $content); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Wrap Javascript output with common function and tags |
| 89 |
* |
| 90 |
* @param string $script JavaScript |
| 91 |
* @param string $name string name of function |
| 92 |
* |
| 93 |
* @return string JavaScript |
| 94 |
*/ |
| 95 |
protected function wrap_script($script, $name="") |
| 96 |
{ |
| 97 |
ob_start(); |
| 98 |
?><script> |
| 99 |
window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || []; |
| 100 |
window.WPLeafletMapPlugin.push(function <?php echo $name; ?>() {<?php echo $script; ?>});</script><?php |
| 101 |
return ob_get_clean(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Create an LM variable for each shortcode class |
| 106 |
* instance |
| 107 |
*/ |
| 108 |
protected function __construct() |
| 109 |
{ |
| 110 |
$this->LM = Leaflet_Map::init(); |
| 111 |
} |
| 112 |
} |