Exemple #1
0
function eme_replace_locations_placeholders($format, $location = "", $target = "html", $do_shortcode = 1, $lang = '')
{
    // first we do the custom attributes, since these can contain other placeholders
    preg_match_all("/#(ESC|URL)?_ATT\\{.+?\\}(\\{.+?\\})?/", $format, $results);
    foreach ($results[0] as $resultKey => $result) {
        $need_escape = 0;
        $need_urlencode = 0;
        $orig_result = $result;
        if (strstr($result, '#ESC')) {
            $result = str_replace("#ESC", "#", $result);
            $need_escape = 1;
        } elseif (strstr($result, '#URL')) {
            $result = str_replace("#URL", "#", $result);
            $need_urlencode = 1;
        }
        $replacement = "";
        //Strip string of placeholder and just leave the reference
        $attRef = substr(substr($result, 0, strpos($result, '}')), 6);
        if (isset($location['location_attributes'][$attRef])) {
            $replacement = $location['location_attributes'][$attRef];
        }
        if (trim($replacement) == '' && isset($results[2][$resultKey]) && $results[2][$resultKey] != '') {
            //Check to see if we have a second set of braces;
            $replacement = substr($results[2][$resultKey], 1, strlen(trim($results[2][$resultKey])) - 2);
        }
        if ($need_escape) {
            $replacement = eme_sanitize_request(eme_sanitize_html(preg_replace('/\\n|\\r/', '', $replacement)));
        }
        if ($need_urlencode) {
            $replacement = rawurlencode($replacement);
        }
        $format = str_replace($orig_result, $replacement, $format);
    }
    preg_match_all("/#(ESC|URL)?@?_?[A-Za-z0-9_]+/", $format, $placeholders);
    // make sure we set the largest matched placeholders first, otherwise if you found e.g.
    // #_LOCATION, part of #_LOCATIONPAGEURL would get replaced as well ...
    usort($placeholders[0], 'sort_stringlenth');
    foreach ($placeholders[0] as $result) {
        $need_escape = 0;
        $need_urlencode = 0;
        $orig_result = $result;
        $found = 1;
        if (strstr($result, '#ESC')) {
            $result = str_replace("#ESC", "#", $result);
            $need_escape = 1;
        } elseif (strstr($result, '#URL')) {
            $result = str_replace("#URL", "#", $result);
            $need_urlencode = 1;
        }
        $replacement = "";
        // echo "RESULT: $result <br>";
        // matches alla fields placeholder
        if (preg_match('/#_MAP/', $result)) {
            $replacement = eme_single_location_map($location);
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_PASTEVENTS/', $result)) {
            $replacement = eme_events_in_location_list($location, "past");
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_NEXTEVENTS/', $result)) {
            $replacement = eme_events_in_location_list($location);
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_ALLEVENTS/', $result)) {
            $replacement = eme_events_in_location_list($location, "all");
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_(ADDRESS|TOWN)/', $result)) {
            $field = "location_" . ltrim(strtolower($result), "#_");
            if (isset($location[$field])) {
                $replacement = $location[$field];
            }
            $replacement = eme_trans_sanitize_html($replacement, $lang);
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_(NAME|LOCATIONNAME|LOCATION)$/', $result)) {
            $field = "location_name";
            if (isset($location[$field])) {
                $replacement = $location[$field];
            }
            $replacement = eme_trans_sanitize_html($replacement, $lang);
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_LOCATIONID/', $result)) {
            $field = "location_id";
            $replacement = $location[$field];
            $replacement = eme_trans_sanitize_html($replacement, $lang);
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_(IMAGE|LOCATIONIMAGE)$/', $result)) {
            if (!empty($location['location_image_id'])) {
                $location['location_image_url'] = wp_get_attachment_url($location['location_image_id']);
            }
            if (!empty($location['location_image_url'])) {
                $replacement = "<img src='" . $location['location_image_url'] . "' alt='" . eme_trans_sanitize_html($location['location_name'], $lang) . "'/>";
                if ($target == "html") {
                    $replacement = apply_filters('eme_general', $replacement);
                } elseif ($target == "rss") {
                    $replacement = apply_filters('eme_general_rss', $replacement);
                } else {
                    $replacement = apply_filters('eme_text', $replacement);
                }
            }
        } elseif (preg_match('/#_(IMAGEURL|LOCATIONIMAGEURL)/', $result)) {
            if (!empty($location['location_image_id'])) {
                $location['location_image_url'] = wp_get_attachment_url($location['location_image_id']);
            }
            if ($location['location_image_url'] != '') {
                $replacement = $location['location_image_url'];
                if ($target == "html") {
                    $replacement = apply_filters('eme_general', $replacement);
                } elseif ($target == "rss") {
                    $replacement = apply_filters('eme_general_rss', $replacement);
                } else {
                    $replacement = apply_filters('eme_text', $replacement);
                }
            }
        } elseif (preg_match('/#_LOCATIONIMAGETHUMB$/', $result)) {
            if (!empty($location['location_image_id'])) {
                $thumb_array = image_downsize($location['location_image_id'], get_option('eme_thumbnail_size'));
                $thumb_url = $thumb_array[0];
                $thumb_width = $thumb_array[1];
                $thumb_height = $thumb_array[2];
                $replacement = "<img width='{$thumb_width}' height='{$thumb_height}' src='" . $thumb_url . "' alt='" . eme_trans_sanitize_html($location['location_name'], $lang) . "'/>";
                if ($target == "html") {
                    $replacement = apply_filters('eme_general', $replacement);
                } elseif ($target == "rss") {
                    $replacement = apply_filters('eme_general_rss', $replacement);
                } else {
                    $replacement = apply_filters('eme_text', $replacement);
                }
            }
        } elseif (preg_match('/#_LOCATIONIMAGETHUMBURL/', $result)) {
            if (!empty($location['location_image_id'])) {
                $thumb_array = image_downsize($location['location_image_id'], get_option('eme_thumbnail_size'));
                $thumb_url = $thumb_array[0];
                $replacement = $thumb_url;
                if ($target == "html") {
                    $replacement = apply_filters('eme_general', $replacement);
                } elseif ($target == "rss") {
                    $replacement = apply_filters('eme_general_rss', $replacement);
                } else {
                    $replacement = apply_filters('eme_text', $replacement);
                }
            }
        } elseif (preg_match('/#_LOCATIONPAGEURL/', $result)) {
            $replacement = eme_location_url($location, $lang);
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_LATITUDE/', $result)) {
            $field = "location_latitude";
            $replacement = $location[$field];
            $replacement = eme_trans_sanitize_html($replacement, $lang);
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_LONGITUDE/', $result)) {
            $field = "location_longitude";
            $replacement = $location[$field];
            $replacement = eme_trans_sanitize_html($replacement, $lang);
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_DIRECTIONS/', $result)) {
            $replacement = eme_add_directions_form($location);
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_CATEGORIES|#_LOCATIONCATEGORIES/', $result) && get_option('eme_categories_enabled')) {
            $categories = eme_get_location_category_names($location['location_id']);
            $replacement = eme_trans_sanitize_html(join(", ", $categories), $lang);
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_LOCATIONCATEGORYDESCRIPTIONS/', $result) && get_option('eme_categories_enabled')) {
            $categories = eme_get_location_category_descriptions($location['location_id']);
            $replacement = eme_trans_sanitize_html(join(", ", $categories), $lang);
            if ($target == "html") {
                $replacement = apply_filters('eme_general', $replacement);
            } elseif ($target == "rss") {
                $replacement = apply_filters('eme_general_rss', $replacement);
            } else {
                $replacement = apply_filters('eme_text', $replacement);
            }
        } elseif (preg_match('/#_EDITLOCATIONLINK/', $result)) {
            if (current_user_can(get_option('eme_cap_edit_locations')) || current_user_can(get_option('eme_cap_author_locations')) && $location['location_author'] == $current_userid) {
                $replacement = "<a href=' " . admin_url("admin.php?page=eme-locations&amp;eme_admin_action=edit_location&amp;location_id=" . $location['location_id']) . "'>" . __('Edit') . "</a>";
            }
        } elseif (preg_match('/#_EDITLOCATIONURL/', $result)) {
            if (current_user_can(get_option('eme_cap_edit_locations')) || current_user_can(get_option('eme_cap_author_locations')) && $location['location_author'] == $current_userid) {
                $replacement = admin_url("admin.php?page=eme-locations&amp;eme_admin_action=edit_location&amp;location_id=" . $location['location_id']);
            }
        } elseif (preg_match('/#_IS_SINGLE_LOC/', $result)) {
            if (eme_is_single_location_page()) {
                $replacement = 1;
            } else {
                $replacement = 0;
            }
        } elseif (preg_match('/#_IS_LOGGED_IN/', $result)) {
            if (is_user_logged_in()) {
                $replacement = 1;
            } else {
                $replacement = 0;
            }
        } elseif (preg_match('/#_IS_ADMIN_PAGE/', $result)) {
            if (is_admin()) {
                $replacement = 1;
            } else {
                $replacement = 0;
            }
        } else {
            $found = 0;
        }
        if ($found) {
            if ($need_escape) {
                $replacement = eme_sanitize_request(eme_sanitize_html(preg_replace('/\\n|\\r/', '', $replacement)));
            }
            if ($need_urlencode) {
                $replacement = rawurlencode($replacement);
            }
            $format = str_replace($orig_result, $replacement, $format);
        }
    }
    # we handle DESCRIPTION the last, so no placeholder replacement happens accidentaly in the text of #_DESCRIPTION
    if (preg_match('/#_DESCRIPTION|#_LOCATIONDETAILS/', $format, $placeholders)) {
        $result = $placeholders[0];
        $need_escape = 0;
        $need_urlencode = 0;
        $orig_result = $result;
        if (strstr($result, '#ESC')) {
            $result = str_replace("#ESC", "#", $result);
            $need_escape = 1;
        } elseif (strstr($result, '#URL')) {
            $result = str_replace("#URL", "#", $result);
            $need_urlencode = 1;
        }
        $replacement = "";
        $field = "location_description";
        if (isset($location[$field])) {
            $replacement = $location[$field];
        }
        // no real sanitizing needed, but possible translation
        // this is the same as for an event in fact
        $replacement = eme_translate($replacement);
        if ($target == "html") {
            $replacement = apply_filters('eme_notes', $replacement);
        } elseif ($target == "rss") {
            $replacement = apply_filters('eme_notes_rss', $replacement);
            $replacement = apply_filters('the_content_rss', $replacement);
        } else {
            $replacement = apply_filters('eme_text', $replacement);
        }
        if ($need_escape) {
            $replacement = eme_sanitize_request(eme_sanitize_html(preg_replace('/\\n|\\r/', '', $replacement)));
        }
        if ($need_urlencode) {
            $replacement = rawurlencode($replacement);
        }
        $format = str_replace($orig_result, $replacement, $format);
    }
    // now, replace any language tags found
    $format = eme_translate($format, $lang);
    // and now replace any shortcodes, if wanted
    if ($do_shortcode) {
        return do_shortcode($format);
    } else {
        return $format;
    }
}
function eme_general_head()
{
    $extra_html_header = get_option('eme_html_header');
    $extra_html_header = trim(preg_replace('/\\r\\n/', "\n", $extra_html_header));
    if (!empty($extra_html_header)) {
        echo $extra_html_header . "\n";
    }
    if (eme_is_single_event_page()) {
        $event = eme_get_event(get_query_var('event_id'));
        // I don't know if the canonical rel-link is needed, but since WP adds it by default ...
        $canon_url = eme_event_url($event);
        echo "<link rel=\"canonical\" href=\"{$canon_url}\" />\n";
        $extra_headers_format = get_option('eme_event_html_headers_format');
        if (!empty($extra_headers_format)) {
            $extra_headers_lines = explode("\n", $extra_headers_format);
            foreach ($extra_headers_lines as $extra_header_format) {
                # the text format already removes most of html code, so let's use that
                $extra_header = strip_shortcodes(eme_replace_placeholders($extra_header_format, $event, "text", 0));
                # the text format converts \n to \r\n but we want one line only
                $extra_header = trim(preg_replace('/\\r\\n/', "", $extra_header));
                if ($extra_header != "") {
                    echo $extra_header . "\n";
                }
            }
        }
    } elseif (eme_is_single_location_page()) {
        $location = eme_get_location(get_query_var('location_id'));
        $canon_url = eme_location_url($location);
        echo "<link rel=\"canonical\" href=\"{$canon_url}\" />\n";
        $extra_headers_format = get_option('eme_location_html_headers_format');
        if (!empty($extra_headers_format)) {
            $extra_headers_lines = explode("\n", $extra_headers_format);
            foreach ($extra_headers_lines as $extra_header_format) {
                # the text format already removes most of html code, so let's use that
                $extra_header = strip_shortcodes(eme_replace_locations_placeholders($extra_header_format, $location, "text", 0));
                # the text format converts \n to \r\n but we want one line only
                $extra_header = trim(preg_replace('/\\r\\n/', "", $extra_header));
                if (!empty($extra_header)) {
                    echo $extra_header . "\n";
                }
            }
        }
    }
    $gmap_is_active = get_option('eme_gmap_is_active');
    $load_js_in_header = get_option('eme_load_js_in_header');
    if ($gmap_is_active && $load_js_in_header) {
        echo "<script type='text/javascript' src='//maps.google.com/maps/api/js?v=3.16&amp;sensor=false'></script>\n";
        echo "<script type='text/javascript' src='" . EME_PLUGIN_URL . "js/eme_location_map.js'></script>\n";
    }
}