Skip to content

Commit 4f72411

Browse files
committed
Helper improvements
1 parent b97f558 commit 4f72411

File tree

1 file changed

+214
-22
lines changed

1 file changed

+214
-22
lines changed

inc/Helpers/Waymark_Helper.php

+214-22
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,16 @@ static public function get_map_meta($Map, $context = 'map_single') {
141141

142142
//Select
143143
if (in_array($setting_meta['meta_type'], array('select')) && array_key_exists('meta_options', $setting_meta)) {
144-
$options_array = Waymark_Helper::comma_string_to_array($setting_meta['meta_options']);
144+
$options_array = self::comma_string_to_array($setting_meta['meta_options']);
145145

146146
//Only if option exists
147147
if (isset($options_array[$Map->data[$meta_key]])) {
148148
$data['meta_value'] = $options_array[$Map->data[$meta_key]];
149149
}
150150
//Multi-Select
151151
} elseif (in_array($setting_meta['meta_type'], array('select_multi')) && array_key_exists('meta_options', $setting_meta)) {
152-
$options_array = Waymark_Helper::comma_string_to_array($setting_meta['meta_options']);
153-
$values_array = Waymark_Helper::array_string_to_array($Map->data[$meta_key]);
152+
$options_array = self::comma_string_to_array($setting_meta['meta_options']);
153+
$values_array = self::array_string_to_array($Map->data[$meta_key]);
154154

155155
//Build output
156156
$data['meta_value'] = '';
@@ -199,13 +199,13 @@ static public function get_map_meta($Map, $context = 'map_single') {
199199
$map_meta['export_data'] = array(
200200
'meta_key' => 'export_data',
201201
'meta_title' => esc_html__('Export', 'waymark'),
202-
'meta_value' => Waymark_Helper::map_export_html($Map),
202+
'meta_value' => self::map_export_html($Map),
203203
'meta_info' => '<a data-title="' . esc_attr__('This will download the Overlays currently displayed by the Map in the selected format.', 'waymark') . '" href="#" onclick="return false;" class="waymark-tooltip">?</a>',
204204
'meta_group' => '',
205205
);
206206
}
207207

208-
// Waymark_Helper::debug($map_meta);
208+
// self::debug($map_meta);
209209

210210
return $map_meta;
211211
}
@@ -228,13 +228,13 @@ static public function get_collection_meta($Collection, $context = 'shortcode')
228228
$map_meta['export_data'] = array(
229229
'meta_key' => 'export_data',
230230
'meta_title' => esc_html__('Export', 'waymark'),
231-
'meta_value' => Waymark_Helper::collection_export_html($Collection),
231+
'meta_value' => self::collection_export_html($Collection),
232232
'meta_info' => '<a data-title="' . esc_attr__('This will download the Overlays currently displayed by the Map in the selected format.', 'waymark') . '" href="#" onclick="return false;" class="waymark-tooltip">?</a>',
233233
'meta_group' => '',
234234
);
235235
}
236236

237-
// Waymark_Helper::debug($map_meta);
237+
// self::debug($map_meta);
238238

239239
return $map_meta;
240240
}
@@ -250,7 +250,7 @@ static public function get_meta_groups() {
250250
static public function group_meta($meta_array, $meta_groups = []) {
251251
if (!$meta_groups) {
252252
//Get Groups
253-
$meta_groups = Waymark_Helper::get_meta_groups();
253+
$meta_groups = self::get_meta_groups();
254254
}
255255

256256
//Sort into groups
@@ -275,10 +275,10 @@ static public function map_meta_html($meta_array = array()) {
275275
//Do we have data?
276276
if (is_array($meta_array) && sizeof($meta_array)) {
277277
//Get Meta Groups
278-
$meta_groups = Waymark_Helper::get_meta_groups();
278+
$meta_groups = self::get_meta_groups();
279279

280280
//Sort into groups
281-
$meta_grouped = Waymark_Helper::group_meta($meta_array, $meta_groups);
281+
$meta_grouped = self::group_meta($meta_array, $meta_groups);
282282

283283
//Container
284284
$out = '<!-- START Parameter Container -->' . "\n";
@@ -533,7 +533,7 @@ static public function add_map_link_to_description($map_id = null, $map_title =
533533
}
534534
$desc_append .= '</b>.</div>';
535535

536-
return Waymark_Helper::set_map_data_property($map_data, 'description', $desc_append, true);
536+
return self::set_map_data_property($map_data, 'description', $desc_append, true);
537537
}
538538

539539
public static function convert_single_value_to_array($value_in) {
@@ -547,7 +547,7 @@ public static function convert_single_value_to_array($value_in) {
547547
$count = 0;
548548
foreach ($multi as $m) {
549549
$array_out[$count][$key] = $m;
550-
// Waymark_Helper::debug($m, false);
550+
// self::debug($m, false);
551551

552552
$count++;
553553
}
@@ -677,7 +677,7 @@ static public function map_export_html($Map) {
677677

678678
$element = (is_admin()) ? 'div' : 'form';
679679

680-
$out = '<' . $element . ' action="' . Waymark_Helper::http_url() . '" method="post" id="waymark-map-export-' . $Map->post_id . '" class="waymark-map-export" data-map_id="' . $Map->post_id . '" data-map_slug="' . sanitize_title($Map->post_title) . '">' . "\n";
680+
$out = '<' . $element . ' action="' . self::http_url() . '" method="post" id="waymark-map-export-' . $Map->post_id . '" class="waymark-map-export" data-map_id="' . $Map->post_id . '" data-map_slug="' . sanitize_title($Map->post_title) . '">' . "\n";
681681
$out .= ' <select name="export_format">' . "\n";
682682
$out .= ' <option value="gpx">GPX</option>' . "\n";
683683
$out .= ' <option value="kml">KML</option>' . "\n";
@@ -694,15 +694,15 @@ static public function map_export_html($Map) {
694694
}
695695

696696
static public function collection_export_html($Collection) {
697-
// Waymark_Helper::debug($Collection);
697+
// self::debug($Collection);
698698

699699
if (!isset($Collection->collection_id)) {
700700
return false;
701701
}
702702

703703
$element = (is_admin()) ? 'div' : 'form';
704704

705-
$out = '<' . $element . ' action="' . Waymark_Helper::http_url() . '" method="post" id="waymark-map-export-' . $Collection->collection_id . '" class="waymark-map-export" data-collection_id="' . $Collection->collection_id . '" data-collection_slug="' . $Collection->slug . '">' . "\n";
705+
$out = '<' . $element . ' action="' . self::http_url() . '" method="post" id="waymark-map-export-' . $Collection->collection_id . '" class="waymark-map-export" data-collection_id="' . $Collection->collection_id . '" data-collection_slug="' . $Collection->slug . '">' . "\n";
706706
$out .= ' <select name="export_format">' . "\n";
707707
$out .= ' <option value="gpx">GPX</option>' . "\n";
708708
$out .= ' <option value="kml">KML</option>' . "\n";
@@ -740,7 +740,7 @@ static public function repeatable_setting_option_array($tab, $section, $key) {
740740
//If exists
741741
if (array_key_exists($key, $s)) {
742742
//Add as option
743-
$options_array[Waymark_Helper::make_key($s[$key])] = $s[$key];
743+
$options_array[self::make_key($s[$key])] = $s[$key];
744744
}
745745
}
746746

@@ -973,9 +973,20 @@ static function allowable_file($ext = '', $mime = false, $file_image = 'file') {
973973
return false;
974974
}
975975

976+
/**
977+
* build_icon_data
978+
*
979+
* Builds the data necessary to render an icon
980+
*
981+
* @param array $type Type data
982+
* @return array Icon data
983+
* @since 2024.1
984+
* @access public
985+
* @static
986+
*/
976987
static public function build_icon_data(Array $type = []) {
977988

978-
// Waymark_helper::debug($type);
989+
// self::debug($type);
979990

980991
// If Type key not set
981992
if (!isset($type['type_key'])) {
@@ -1085,9 +1096,18 @@ static public function build_icon_data(Array $type = []) {
10851096
return $icon_data;
10861097
}
10871098

1099+
/**
1100+
* build_icon_html
1101+
* Builds the HTML for an icon
1102+
* @param array $icon_data Icon data
1103+
* @return string HTML
1104+
* @since 2024.1
1105+
* @access public
1106+
* @static
1107+
*/
10881108
static public function build_icon_html(Array $icon_data = []) {
10891109

1090-
// Waymark_helper::debug($icon_data);
1110+
// self::debug($icon_data);
10911111

10921112
$icon_html = '<div class="' . $icon_data['className'] . '">';
10931113

@@ -1102,6 +1122,17 @@ static public function build_icon_html(Array $icon_data = []) {
11021122

11031123
}
11041124

1125+
/**
1126+
*
1127+
* Converts old background options to new ones
1128+
*
1129+
* @param string $colour Colour
1130+
* @return string Colour
1131+
* @since 2024.1
1132+
* @access public
1133+
* @static
1134+
*
1135+
*/
11051136
public static function get_marker_background(String $colour = '') {
11061137
$old_background_options = [
11071138
'red',
@@ -1159,15 +1190,28 @@ public static function get_marker_background(String $colour = '') {
11591190
return $colour;
11601191
}
11611192

1193+
/**
1194+
*
1195+
* Builds the HTML content for an overlay
1196+
*
1197+
* @param array $feature Feature array
1198+
* @param string $feature_type Feature type
1199+
* @param array $type_data Type data
1200+
* @return string HTML content
1201+
* @since 2024.1
1202+
* @access public
1203+
* @static
1204+
*
1205+
*/
11621206
public static function build_overlay_content(Array $feature = [], String $feature_type = 'marker', Array $type_data = []) {
11631207
$content = '<div class="waymark-overlay-content waymark-overlay-' . $feature_type . '">' . "\n";
11641208

11651209
// If we don't have type data
11661210
if (empty($type_data)) {
11671211
// Get Type Data
1168-
$type_data = Waymark_Helper::get_type_data($feature_type, $feature['properties']['type_key']);
1212+
$type_data = self::get_type_data($feature_type, $feature['properties']['type_key']);
11691213

1170-
Waymark_Helper::debug($type_data);
1214+
self::debug($type_data);
11711215

11721216
}
11731217

@@ -1204,7 +1248,7 @@ public static function build_overlay_content(Array $feature = [], String $featur
12041248
// break;
12051249
// }
12061250

1207-
$content .= Waymark_Helper::type_to_text($feature_type, $type_data, 'small');
1251+
$content .= self::type_to_text($feature_type, $type_data, 'small');
12081252

12091253
break;
12101254

@@ -1256,7 +1300,21 @@ public static function build_overlay_content(Array $feature = [], String $featur
12561300
return $content;
12571301
}
12581302

1259-
//Represent Type as text
1303+
/**
1304+
*
1305+
* Represent Type as text
1306+
*
1307+
* Outputs a textual representation of a type, coloured according to the type's colour Settings
1308+
* https://www.waymark.dev/docs/settings/
1309+
*
1310+
* @param string $feature_type - marker, line, shape
1311+
* @param array $type_data - array of type data, must have these keys: type_key, type_title, marker_colour, icon_colour
1312+
* @param string $ele - HTML element to use to wrap the output
1313+
* @return string
1314+
* @since 2024.1
1315+
* @access public
1316+
* @static
1317+
*/
12601318
public static function type_to_text(String $feature_type = '', Array $type_data = [], String $ele = 'span') {
12611319
$preview_class = 'waymark-type-text waymark-' . $feature_type . '-type';
12621320
$preview_style = '';
@@ -1279,4 +1337,138 @@ public static function type_to_text(String $feature_type = '', Array $type_data
12791337

12801338
return '<' . $ele . ' class="' . $preview_class . '" style="' . $preview_style . '">' . $type_data[$feature_type . '_title'] . '</' . $ele . '>';
12811339
}
1340+
1341+
/**
1342+
*
1343+
* Creates the HTML strucuture for the overlays list
1344+
*
1345+
* $overlays must have one of these keys: markers, lines, shapes containing an array of overlays
1346+
* Waymark_GeoJSON::features_by_overlay_type() is a good way to get this data
1347+
*
1348+
* Markers/Lines/Shapes are displayed separately and divided into types
1349+
* Types are displayed as a header with a count
1350+
* Each overlay is displayed as a list item
1351+
* Each overlay has a title, description and image
1352+
*
1353+
* @param array $overlays An array containing 'marker' => [ $markers ], 'line' => [ $lines ], 'shape' => [ $shapes ]
1354+
* @return string
1355+
* @since 2024.1
1356+
* @access public
1357+
* @static
1358+
*
1359+
*/
1360+
public static function overlays_list_html(Array $overlays = []) {
1361+
$out = '';
1362+
1363+
if (!sizeof($overlays)) {
1364+
return $out;
1365+
}
1366+
1367+
// $overlays must have one of these keys: markers, lines, shapes
1368+
if (!array_key_exists('markers', $overlays) && !array_key_exists('lines', $overlays) && !array_key_exists('shapes', $overlays)) {
1369+
return $out;
1370+
}
1371+
1372+
// self::debug($overlays);
1373+
1374+
foreach ($overlays as $overlay_type => $overlay) {
1375+
// $overlay must be an array
1376+
if (!is_array($overlay)) {
1377+
continue;
1378+
}
1379+
1380+
$out .= '<div class="waymark-overlays-list">' . "\n";
1381+
1382+
switch ($overlay_type) {
1383+
case 'markers':
1384+
// Wrapper
1385+
$out .= '<div class="waymark-overlay-list waymark-overlay-markers">' . "\n";
1386+
$out .= ' <div class="waymark-title">' . __('Markers', 'waymark') . '</div>' . "\n";
1387+
$out .= ' <div class="waymark-overlays">' . "\n";
1388+
1389+
// Every marker type
1390+
foreach ($overlay as $marker_type => $markers) {
1391+
// Ensure we have markers
1392+
if (!sizeof($markers)) {
1393+
continue;
1394+
}
1395+
1396+
// Get type data
1397+
$type_data = self::get_type_data('marker', $marker_type);
1398+
1399+
if (!$type_data) {
1400+
continue;
1401+
}
1402+
1403+
// Wrapper for Type
1404+
$out .= ' <div class="waymark-type waymark-type-' . $marker_type . '">' . "\n";
1405+
1406+
//Output Title, Icon and count
1407+
$icon_data = self::build_icon_data($type_data);
1408+
$icon_html = self::build_icon_html($icon_data);
1409+
1410+
$out .= ' <div class="waymark-header" style="background-color:' . $type_data['marker_colour'] . ';color:' . $type_data['icon_colour'] . ';">' . "\n";
1411+
$out .= ' <div class="waymark-icon">' . $icon_html . '</div>' . "\n";
1412+
$out .= ' <div class="waymark-title">' . $type_data['marker_title'] . '</div>' . "\n";
1413+
$out .= ' <div class="waymark-count">' . sizeof($markers) . '</div>' . "\n";
1414+
$out .= ' </div>' . "\n";
1415+
1416+
// Iterate over markers
1417+
foreach ($markers as $marker) {
1418+
$out .= self::build_overlay_content($marker, 'marker', $type_data);
1419+
}
1420+
1421+
$out .= ' </div>' . "\n";
1422+
}
1423+
1424+
$out .= ' </div>' . "\n";
1425+
$out .= '</div>' . "\n";
1426+
1427+
break;
1428+
case 'lines':
1429+
// Get valid line types
1430+
$line_types = self::get_overlay_types('line', 'line_title');
1431+
1432+
// Wrapper
1433+
$out .= '<div class="waymark-overlays waymark-lines">' . "\n";
1434+
1435+
// Every line type
1436+
foreach ($overlay as $line_type => $lines) {
1437+
// Ensure is valid line type
1438+
if (!array_key_exists($line_type, $line_types)) {
1439+
continue;
1440+
}
1441+
1442+
$out .= '<li>' . $line_type . ' (' . sizeof($lines) . ')</li>' . "\n";
1443+
}
1444+
$out .= '</div>' . "\n";
1445+
1446+
break;
1447+
case 'shapes':
1448+
// Get valid shape types
1449+
$shape_types = self::get_overlay_types('shape', 'shape_title');
1450+
1451+
// Wrapper
1452+
$out .= '<div class="waymark-overlays waymark-shapes">' . "\n";
1453+
1454+
// Every shape type
1455+
foreach ($overlay as $shape_type => $shapes) {
1456+
1457+
// Ensure is valid shape type
1458+
if (!array_key_exists($shape_type, $shape_types)) {
1459+
continue;
1460+
}
1461+
1462+
$out .= '<li>' . $shape_type . ' (' . sizeof($shapes) . ')</li>' . "\n";
1463+
}
1464+
$out .= '</div>' . "\n";
1465+
1466+
break;
1467+
}
1468+
}
1469+
1470+
$out .= '</div>' . "\n";
1471+
1472+
return $out;
1473+
}
12821474
}

0 commit comments

Comments
 (0)