Easy Property Listings 3.5.15 Code Reference
  • Package
  • Class
  • Tree
  • Todo
  • Hook Reference

Packages

  • EPL
    • Admin
      • Actions
      • Classes
        • ContactsTable
        • ReportsGraph
        • Welcome
      • Contacts
      • ContactsActions
      • ContactsFunctions
      • Elements
      • Functions
      • Help
      • HelpSingle
      • Menus
      • MenusAddons
      • MenusExtensions
      • MenusLicenses
      • Plugins
      • Reports
      • ReportsGraphing
      • User
    • Assets
      • ScriptsStyles
      • SVG
    • Classes
      • AuthorLoader
      • AuthorMeta
      • Contact
      • Cron
      • CustomPostType
      • Forms
      • License
      • ListingAdvanced
      • ListingElements
      • MetaboxesCustomFields
      • Pagination
      • PropertyMeta
      • RenderFields
      • RestAPI
      • Search
      • Session
      • Updater
    • Compatibility
      • Extensions
      • Functions
      • Shortcodes
    • Functions
      • Actions
      • ConditionalTags
      • ErrorTracking
      • Formatting
      • Front
      • Global
      • Install
      • Pagination
      • Settings
      • Templates
    • Hooks
      • EnergyCertificate
      • ExternalLinks
      • FloorPlan
      • Map
      • ReadMore
      • WebLink
    • Meta
      • InitCustomFields
      • Meta
    • PostTypes
      • Business
      • Commercial
      • CommercialLand
      • Contact
      • Functions
      • Land
      • Property
      • Rental
      • Rural
    • Shortcode
      • CommercialListingSearch
      • ContactForm
      • Listing
      • ListingAdvanced
      • ListingAuction
      • ListingCategory
      • ListingFeature
      • ListingLocation
      • ListingMetaDoc
      • ListingOpen
      • ListingResults
      • ListingSearch
      • Map
    • Taxonomy
      • BusinessCategories
      • ContactTag
      • Features
      • Location
    • Templates
      • Themes
        • iThemes
        • iThemesBuilder
    • Widget
      • Admin
        • Dashboard
      • Classes
        • Author
        • ContactCapture
        • Gallery
        • Listing
        • Search
      • Functions
  • None
  • WordPress
    • Session

Classes

  • EPL_Advanced_Shortcode_Listing
  • EPL_Author
  • EPL_Author_Loader
  • EPL_Author_Meta
  • EPL_Contact
  • EPL_Contact_Reports_Table
  • EPL_CPT
  • EPL_Cron
  • EPL_FORM_BUILDER
  • EPL_Graph
  • EPL_License
  • EPL_Listing_Elements
  • EPL_METABOX
  • EPL_Pagination_Call
  • EPL_Property_Meta
  • EPL_Render_Fields
  • EPL_Rest_API
  • EPL_SEARCH
  • EPL_Search_Fields
  • EPL_Session
  • EPL_SL_Plugin_Updater
  • EPL_Welcome
  • EPL_Widget_Author
  • EPL_Widget_Contact_Capture
  • EPL_Widget_Property_Gallery
  • EPL_Widget_Property_Search
  • EPL_Widget_Recent_Property

Functions

  • EPL
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 
<?php
/**
 * Formatting functions for taking care of proper number formats and such
 * Adapted from Easy Digital Downloads
 *
 * @package     EPL
 * @subpackage  Functions/Formatting
 * @copyright   Copyright (c) 2020, Merv Barrett
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       1.0
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Sanitize Amount
 *
 * Returns a sanitized amount by stripping out thousands separators.
 *
 * @since 1.0
 * @param string $amount Price amount to format.
 * @return string $amount Newly sanitized amount
 */
function epl_sanitize_amount( $amount ) {
    $thousands_sep = epl_get_thousands_separator();
    $decimal_sep   = epl_get_decimal_separator();

    // Sanitize the amount.
    if ( ',' === $decimal_sep && false !== ( strpos( $amount, $decimal_sep ) ) ) {
        if ( '.' === $thousands_sep && false !== ( strpos( $amount, $thousands_sep ) ) ) {
            $amount = str_replace( $thousands_sep, '', $amount );
        } elseif ( empty( $thousands_sep ) && false !== ( strpos( $amount, '.' ) ) ) {
            $amount = str_replace( '.', '', $amount );
        }

        $amount = str_replace( $decimal_sep, '.', $amount );
    } elseif ( ',' === $thousands_sep && false !== ( strpos( $amount, $thousands_sep ) ) ) {
        $amount = str_replace( $thousands_sep, '', $amount );
    }

    $decimals = apply_filters( 'epl_sanitize_amount_decimals', 2, $amount );
    $amount   = number_format( $amount, $decimals, '.', '' );

    return apply_filters( 'epl_sanitize_amount', $amount );
}

/**
 * Returns a nicely formatted amount.
 *
 * @param string $amount          Price amount to format.
 * @param bool   $decimals        Whether or not to use decimals. Useful when set to false for non-currency numbers.
 * @param bool   $preserve_format Whether to preserve amount format ie int / floats.
 *
 * @return string $amount Newly formatted amount or Price Not Available
 *
 * @since 1.0.0
 * @since 3.4.28 $preserve_format param added.
 * @since 3.4.44 Fixed warning for strpos when amount is empty.
 */
function epl_format_amount( $amount, $decimals = false, $preserve_format = false ) {
    $thousands_sep = epl_get_thousands_separator();
    $decimal_sep   = epl_get_decimal_separator();
    $formatted     = '';

        if ( empty( $amount ) ) {
                return $amount;
        }

    // Format the amount.
    $sep_found = strpos( $amount, $decimal_sep );
    if ( ',' === $decimal_sep && false !== $sep_found ) {
        $whole  = substr( $amount, 0, $sep_found );
        $part   = substr( $amount, $sep_found + 1, ( strlen( $amount ) - 1 ) );
        $amount = $whole . '.' . $part;
    }

    // Strip , from the amount (if set as the thousands separator).
    if ( ',' === $thousands_sep && false !== ( strpos( $amount, $thousands_sep ) ) ) {
        $amount = str_replace( ',', '', $amount );
    }

    if ( empty( $amount ) ) {
        $amount = 0;
    }
    if ( $preserve_format && false === ( strpos( $amount, $decimal_sep ) ) ) {
        $decimals = false;
    }

    $decimals = apply_filters( 'epl_format_amount_decimals', $decimals ? 2 : 0, $amount );
    if ( is_numeric( $amount ) ) {

        $formatted = number_format( $amount, $decimals, $decimal_sep, $thousands_sep );
    }

    return apply_filters( 'epl_format_amount', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep );
}

/**
 * Formats the currency display
 *
 * @param string $price Price.
 *
 * @return string $currency Currencies displayed correctly
 * @since 1.0
 * @since 3.4.44 fixed the appended dash sign for field sliders.
 */
function epl_currency_filter( $price ) {
    $currency          = epl_get_currency();
    $currency_position = epl_get_currency_position();

    $position = ( isset( $currency_position ) && ! empty( $currency_position ) ) ? $currency_position : 'before';

    $negative = floatval( $price ) < 0;

    if ( $negative ) {
        $price = substr( $price, 1 ); // Remove proceeding "-".
    }
    //phpcs:disable
    if ( 'before' === $position ) :
        switch ( $currency ) :
            case 'GBP':
                $formatted = '&pound;' . $price;
                break;
            case 'BRL':
                $formatted = 'R&#36;' . $price;
                break;
            case 'EUR':
                $formatted = '&euro;' . $price;
                break;
            case 'USD':
            case 'AUD':
            case 'NZD':
            case 'CAD':
            case 'HKD':
            case 'MXN':
            case 'SGD':
                $formatted = '&#36;' . $price;
                break;
            case 'JPY':
                $formatted = '&yen;' . $price;
                break;
            case 'ILS':
                $formatted = '&#8362;' . $price;
                break;
            case 'THB':
                $formatted = '&#3647;' . $price;
                break;
            case 'INR':
                $formatted = '&#8377;' . $price;
                break;
            case 'TRY':
                $formatted = '&#8378;' . $price;
                break;
            case 'RIAL':
                $formatted = '&#65020;' . $price;
                break;
            case 'UAH':
                $formatted = '&#8372;' . $price;
                break;
            case 'VND':
                $formatted = '&#8363;' . $price;
                break;
            case 'ZAR':
                $formatted = 'R' . $price;
                break;
            case 'CZK':
                $formatted = 'K&#269;' . $price;
                break;
            default:
                $formatted = $currency . ' ' . $price;
                break;
        endswitch;
        $formatted = apply_filters( 'epl_' . strtolower( $currency ) . '_currency_filter_before', $formatted, $currency, $price );
        else :
            switch ( $currency ) :
                case 'GBP':
                    $formatted = $price . '&pound;';
                    break;
                case 'BRL':
                    $formatted = $price . 'R&#36;';
                    break;
                case 'EUR':
                    $formatted = $price . '&euro;';
                    break;
                case 'USD':
                case 'AUD':
                case 'NZD':
                case 'CAD':
                case 'HKD':
                case 'MXN':
                case 'SGD':
                    $formatted = $price . '&#36;';
                    break;
                case 'JPY':
                    $formatted = $price . '&yen;';
                    break;
                case 'ILS':
                    $formatted = $price . '&#8362;';
                    break;
                case 'THB':
                    $formatted = $price . '&#3647;';
                    break;
                case 'INR':
                    $formatted = $price . '&#8377;';
                    break;
                case 'TRY':
                    $formatted = $price . '&#8378;';
                    break;
                case 'RIAL':
                    $formatted = $price . '&#65020;';
                    break;
                case 'UAH':
                    $formatted = $price . '&#8372;';
                    break;
                case 'VND':
                    $formatted = $price . '&#8363;';
                    break;
                case 'ZAR':
                    $formatted = $price . 'R';
                    break;
                case 'CZK':
                    $formatted = $price . ' K&#269;';
                    break;
                default:
                    $formatted = $price . ' ' . $currency;
                    break;
            endswitch;
            $formatted = apply_filters( 'epl_' . strtolower( $currency ) . '_currency_filter_after', $formatted, $currency, $price );
    endif;

        if ( $negative ) {
            // Prepend the minus sign before the currency sign.
            $formatted = '-' . $formatted;
        }

        return $formatted;
        //phpcs:enable
}

/**
 * Set the number of decimal places per currency
 *
 * @since 1.0
 * @param int $decimals Number of decimal places.
 * @return int $decimals
 */
function epl_currency_decimal_filter( $decimals = 2 ) {
    $currency = epl_get_currency();

    switch ( $currency ) {
        case 'RIAL':
        case 'JPY':
        case 'TWD':
            $decimals = 0;
            break;
    }

    return $decimals;
}
add_filter( 'epl_sanitize_amount_decimals', 'epl_currency_decimal_filter' );
add_filter( 'epl_format_amount_decimals', 'epl_currency_decimal_filter' );

/**
 * Apply formatting to epl_the_excerpt
 *
 * @param string $content The content.
 * @since 3.1
 * @return int $decimals
 */
function epl_format_the_excerpt( $content ) {
    $content = '<div class="epl-excerpt-content">' . wpautop( $content ) . '</div>';
    return $content;
}
add_filter( 'epl_the_excerpt', 'epl_format_the_excerpt' );

/**
 * Sanitizes a string key for EPL
 *
 * Keys are used as internal identifiers. Alphanumeric characters, dashes, underscores, stops, colons and slashes are allowed
 *
 * @since  3.3.5
 * @param  string $key String key.
 * @return string Sanitized key
 */
function epl_sanitize_key( $key ) {

    $raw_key = $key;
    $key     = preg_replace( '/[^a-zA-Z0-9_\-\.\:\/]/', '', $key );
    return apply_filters( 'epl_sanitize_key', $key, $raw_key );
}
Easy Property Listings 3.5.15 Code Reference API documentation generated by ApiGen