From a296420e840a9efa6b796ec0287140f8747a79e9 Mon Sep 17 00:00:00 2001
From: Skylar Ittner
Date: Wed, 16 May 2018 23:03:16 -0600
Subject: [PATCH] Add format_special(), theme functions now always return the
value regardless of $echo
---
lib/themefunctions.php | 109 +++++++++++++++------
public/themes/bootstrap/inc/footer.inc.php | 6 +-
public/themes/hyperspace/home.php | 6 +-
public/themes/massively/inc/footer.inc.php | 6 +-
public/themes/verti/inc/footer.inc.php | 8 +-
5 files changed, 91 insertions(+), 44 deletions(-)
diff --git a/lib/themefunctions.php b/lib/themefunctions.php
index 9cf4ccb..e8a3cd3 100644
--- a/lib/themefunctions.php
+++ b/lib/themefunctions.php
@@ -18,9 +18,8 @@ function get_site_name($echo = true) {
$title = $db->get('sites', "sitename", ["siteid" => getsiteid()]);
if ($echo) {
echo $title;
- } else {
- return $title;
}
+ return $title;
}
/**
@@ -33,9 +32,8 @@ function get_site_url($echo = true) {
$url = formatsiteurl($db->get('sites', "url", ["siteid" => getsiteid()]));
if ($echo) {
echo $url;
- } else {
- return $url;
}
+ return $url;
}
/**
@@ -48,9 +46,8 @@ function get_page_title($echo = true) {
$title = $db->get("pages", "title", ["AND" => ["slug" => getpageslug(), "siteid" => getsiteid()]]);
if ($echo) {
echo $title;
- } else {
- return $title;
}
+ return $title;
}
/**
@@ -62,9 +59,8 @@ function get_page_clean_title($echo = true) {
$title = strip_tags(get_page_title(false));
if ($echo) {
echo $title;
- } else {
- return $title;
}
+ return $title;
}
/**
@@ -75,9 +71,8 @@ function get_page_clean_title($echo = true) {
function get_page_slug($echo = true) {
if ($echo) {
echo getpageslug();
- } else {
- return getpageslug();
}
+ return getpageslug();
}
/**
@@ -97,9 +92,8 @@ function get_page_clean_url($echo = true, $slug = null) {
}
if ($echo) {
echo $url;
- } else {
- return $url;
}
+ return $url;
}
/**
@@ -143,9 +137,8 @@ function get_page_url($echo = true, $slug = null) {
}
if ($echo) {
echo $url;
- } else {
- return $url;
}
+ return $url;
}
/**
@@ -174,9 +167,8 @@ function get_component($name, $context = null, $echo = true, $default = "") {
}
if ($echo) {
echo $content;
- } else {
- return $content;
}
+ return $content;
}
/**
@@ -270,9 +262,8 @@ function get_escaped_json($json, $echo = true) {
$text = htmlspecialchars(json_encode($json), ENT_QUOTES, 'UTF-8');
if ($echo) {
echo $text;
- } else {
- return $text;
}
+ return $text;
}
/**
@@ -290,9 +281,8 @@ function get_url_or_slug($str, $echo = true) {
}
if ($echo) {
echo $url;
- } else {
- return $url;
}
+ return $url;
}
/**
@@ -316,9 +306,8 @@ function get_file_url($file, $echo = true) {
if ($echo) {
echo $url;
- } else {
- return $url;
}
+ return $url;
}
/**
@@ -362,9 +351,8 @@ function get_setting($key, $echo = false) {
}
if ($echo) {
echo $value;
- } else {
- return $value;
}
+ return $value;
}
/**
@@ -382,9 +370,8 @@ function get_theme_url($echo = true) {
}
if ($echo) {
echo $url;
- } else {
- return $url;
}
+ return $url;
}
/**
@@ -407,9 +394,8 @@ function get_theme_color_url($echo = true) {
$url = get_theme_url(false) . "/colors/" . $site["color"];
if ($echo) {
echo $url;
- } else {
- return $url;
}
+ return $url;
}
/**
@@ -480,9 +466,8 @@ function get_fontawesome_js($echo = true) {
$url = "assets/fontawesome-all.min.js";
if ($echo) {
echo $url;
- } else {
- return $url;
}
+ return $url;
}
/**
@@ -494,9 +479,8 @@ function get_fontawesome_css($echo = true) {
$url = "assets/css/fontawesome-all.min.css";
if ($echo) {
echo $url;
- } else {
- return $url;
}
+ return $url;
}
/**
@@ -601,3 +585,64 @@ function get_socialmedia_urls() {
}
return $urls;
}
+
+define("SPECIAL_TYPE_NONE", 0);
+define("SPECIAL_TYPE_PHONE", 1);
+define("SPECIAL_TYPE_EMAIL", 2);
+define("SPECIAL_TYPE_LINEBREAKS", 3);
+define("SPECIAL_TYPE_ADDRESS", 4);
+/**
+ * Take $text, format it according to $type,
+ * replace [[CONTENT]] in $template with it,
+ * and replace [[TITLE]] with $title (or the unchanged $text if $title is null)
+ *
+ * $type may be one of the following:
+ *
+ * - `SPECIAL_TYPE_PHONE`: `tel:1234567890`
+ * - `SPECIAL_TYPE_EMAIL`: `mailto:address@example.com`
+ * - `SPECIAL_TYPE_LINEBREAKS`: Replaces `\n` with `
\n`
+ * - `SPECIAL_TYPE_ADDRESS`: Creates a link to open Google Maps, and runs LINEBREAKS on $title
+ * - `SPECIAL_TYPE_NONE`: Does no text manipulation.
+ *
+ *
+ * @param string $text
+ * @param int $type
+ * @param string $template
+ * @param string $title
+ * @param boolean $echo default true
+ * @param boolean $conditional Act as output_conditional() and not return anything if $text is empty
+ * @return string
+ */
+function format_special($text, $type = SPECIAL_TYPE_NONE, $template = "", $title = null, $echo = true, $conditional = true) {
+ if ($text == "") {
+ return "";
+ }
+ if (is_null($title)) {
+ $title = $text;
+ }
+ $val = "";
+ switch ($type) {
+ case SPECIAL_TYPE_PHONE:
+ $val = "tel:" . preg_replace("/[^0-9+]/", "", $text);
+ break;
+ case SPECIAL_TYPE_EMAIL:
+ $val = "mailto:" . $text;
+ break;
+ case SPECIAL_TYPE_LINEBREAKS:
+ $val = str_replace("\n", "
\n", $text);
+ break;
+ case SPECIAL_TYPE_ADDRESS:
+ $val = "https://www.google.com/maps/dir/?api=1&destination=" . urlencode(str_replace("\n", " ", $text));
+ $title = str_replace("\n", "
\n", $title);
+ break;
+ default:
+ $val = $text;
+ break;
+ }
+ $out = str_replace("[[CONTENT]]", $val, $template);
+ $out = str_replace("[[TITLE]]", $title, $out);
+ if ($echo) {
+ echo $out;
+ }
+ return $out;
+}
diff --git a/public/themes/bootstrap/inc/footer.inc.php b/public/themes/bootstrap/inc/footer.inc.php
index 6ee98dc..40e4d2c 100644
--- a/public/themes/bootstrap/inc/footer.inc.php
+++ b/public/themes/bootstrap/inc/footer.inc.php
@@ -27,9 +27,9 @@
[[VAR]]", get_setting("businessname")); ?>
', preg_replace("/[^0-9+]/", "", get_setting("phone")));
- output_conditional('', str_replace("\n", "
\n", get_setting("address")));
- output_conditional('', get_setting("email"));
+ format_special(get_setting("phone"), SPECIAL_TYPE_PHONE, '');
+ format_special(get_setting("address"), SPECIAL_TYPE_ADDRESS, '');
+ format_special(get_setting("email"), SPECIAL_TYPE_EMAIL, '');
?>
diff --git a/public/themes/hyperspace/home.php b/public/themes/hyperspace/home.php
index 73a0970..2da312e 100644
--- a/public/themes/hyperspace/home.php
+++ b/public/themes/hyperspace/home.php
@@ -183,9 +183,9 @@ include __DIR__ . "/inc/head.inc.php";