*/ function mulabase_i18n() { return array( 'country' => __( 'Country', 'mulabase' ), 'grossPay' => __( 'Gross pay', 'mulabase' ), 'gross' => __( 'Gross pay', 'mulabase' ), 'period' => __( 'Pay period', 'mulabase' ), 'monthly' => __( 'Monthly', 'mulabase' ), 'yearly' => __( 'Yearly', 'mulabase' ), 'perMonth' => __( 'a month', 'mulabase' ), 'perYear' => __( 'a year', 'mulabase' ), 'takeHome' => __( 'Take-home pay', 'mulabase' ), 'incomeTax' => __( 'Income tax (PAYE)', 'mulabase' ), 'effective' => __( 'Effective tax rate', 'mulabase' ), 'marginal' => __( 'Marginal rate', 'mulabase' ), 'moreOptions' => __( 'More options', 'mulabase' ), 'autoHint' => __( 'Leave blank to estimate from gross pay.', 'mulabase' ), 'amount' => __( 'Amount', 'mulabase' ), 'upTo' => __( 'Tariff covers up to', 'mulabase' ), 'outOfRange' => __( 'That amount is outside this tariff.', 'mulabase' ), 'notAvailable' => __( 'Not available', 'mulabase' ), 'enterAmount' => __( 'Enter an amount, e.g. 50000 or 50k.', 'mulabase' ), 'enterAll' => __( 'Fill in all the fields.', 'mulabase' ), 'bank' => __( 'Your bank', 'mulabase' ), 'cheapest' => __( 'Cheapest banks for this amount', 'mulabase' ), 'loanAmount' => __( 'Loan amount', 'mulabase' ), 'rate' => __( 'Interest rate (% a year)', 'mulabase' ), 'months' => __( 'Months', 'mulabase' ), 'interest' => __( 'Total interest', 'mulabase' ), 'fees' => __( 'Fees', 'mulabase' ), 'totalCost' => __( 'Total cost', 'mulabase' ), 'start' => __( 'Starting amount', 'mulabase' ), 'topUp' => __( 'Monthly top-up', 'mulabase' ), 'years' => __( 'Years', 'mulabase' ), 'yearsShort' => __( 'years', 'mulabase' ), 'fund' => __( 'Or pick a fund', 'mulabase' ), 'after' => __( 'After', 'mulabase' ), 'paidIn' => __( 'You paid in', 'mulabase' ), 'earned' => __( 'Interest earned', 'mulabase' ), 'tax' => __( 'Tax withheld', 'mulabase' ), 'shares' => __( 'Share capital', 'mulabase' ), 'deposits' => __( 'Deposits', 'mulabase' ), 'afterTax' => __( 'Yearly payout after tax, best first', 'mulabase' ), 'checked' => __( 'Rates checked', 'mulabase' ), 'sources' => __( 'Sources', 'mulabase' ), 'credit' => __( 'Data: Mulabase', 'mulabase' ), 'free' => __( 'Free', 'mulabase' ), ); } /** * Classes and inline style shared by every widget. * * @param string $extra Extra classes. * @return string Escaped attribute string. */ function mulabase_wrap_attrs( $extra = '' ) { $s = mulabase_settings(); return 'class="' . esc_attr( trim( 'swp-widget swp-' . $s['theme'] . ' ' . $extra ) ) . '" style="' . esc_attr( '--swp-accent:' . $s['accent'] ) . '"'; } /** * A short notice in place of a widget (shown to editors only; visitors see nothing). * * @param string $message Message. * @return string */ function mulabase_notice( $message ) { if ( ! current_user_can( 'edit_posts' ) ) { return ''; } if ( ! mulabase_connected() ) { $message = __( 'the data has not been downloaded yet. Open Mulabase in the admin menu and click Download the data.', 'mulabase' ); } return '
' . esc_html( 'Mulabase: ' . $message ) . '
'; } /** * An interactive calculator widget. * * @param array $calcs Calculators the visitor can switch between (first is the default). * @param array $opts Starting values and title. * @return string */ function mulabase_widget( $calcs, $opts ) { if ( ! $calcs ) { return mulabase_notice( __( 'no matching calculator. Check the country or slug, or sync in Settings → Mulabase.', 'mulabase' ) ); } mulabase_enqueue_widget( $calcs ); $opts['slugs'] = wp_list_pluck( $calcs, 'slug' ); $first = $calcs[0]; return '

' . esc_html( ! empty( $opts['title'] ) ? $opts['title'] : $first['name'] ) . '

' . esc_html__( 'Loading calculator…', 'mulabase' ) . '

'; } /** * Picks calculators for a "country" attribute: one code/name, a comma list, or "all". * * @param string $type Calculator type. * @param string $country Attribute value. * @return array */ function mulabase_pick( $type, $country ) { $country = trim( (string) $country ); if ( 'all' === strtolower( $country ) ) { $all = mulabase_main_calculators( $type ); // Put the default country first. $default = mulabase_settings()['country']; usort( $all, function ( $a, $b ) use ( $default ) { return ( $b['country'] === $default ) - ( $a['country'] === $default ); } ); return $all; } $out = array(); foreach ( array_filter( array_map( 'trim', explode( ',', '' === $country ? mulabase_settings()['country'] : $country ) ) ) as $one ) { $c = mulabase_main_calculator( $type, mulabase_country_code( $one ) ); if ( $c ) { $out[] = $c; } } return $out; } /** * [mulabase_take_home country="ke" gross="50000" title=""] * * @param array $atts Attributes. * @return string */ function mulabase_sc_take_home( $atts ) { $a = shortcode_atts( array( 'country' => '', 'gross' => '', 'title' => '', ), $atts, 'mulabase_take_home' ); $opts = array_filter( array( 'title' => sanitize_text_field( $a['title'] ), 'gross' => mulabase_amount( $a['gross'] ), ) ); return mulabase_widget( mulabase_pick( 'income-tax', $a['country'] ), $opts ); } /** * [mulabase_mobile_money country="ke" amount="2500" title=""] * * @param array $atts Attributes. * @return string */ function mulabase_sc_mobile_money( $atts ) { $a = shortcode_atts( array( 'country' => '', 'amount' => '', 'title' => '', ), $atts, 'mulabase_mobile_money' ); $opts = array_filter( array( 'title' => sanitize_text_field( $a['title'] ), 'amount' => mulabase_amount( $a['amount'] ), ) ); return mulabase_widget( mulabase_pick( 'tariff', $a['country'] ), $opts ); } /** * [mulabase_calculator slug="kenya-mortgage-loan-calculator" amount="" rate="" months="" title=""] * * @param array $atts Attributes. * @return string */ function mulabase_sc_calculator( $atts ) { $a = shortcode_atts( array( 'slug' => '', 'title' => '', 'amount' => '', 'rate' => '', 'months' => '', 'monthly' => '', 'years' => '', 'shares' => '', 'deposits' => '', 'bank' => '', ), $atts, 'mulabase_calculator' ); $calcs = mulabase_data()['calculators']; $slug = sanitize_title( $a['slug'] ); if ( ! isset( $calcs[ $slug ] ) ) { /* translators: %s: calculator slug. */ return mulabase_notice( sprintf( __( 'no calculator called "%s". Pick one in Settings → Mulabase.', 'mulabase' ), $slug ) ); } $opts = array( 'title' => sanitize_text_field( $a['title'] ) ); foreach ( array( 'amount', 'rate', 'months', 'monthly', 'years', 'shares', 'deposits' ) as $k ) { $v = mulabase_amount( $a[ $k ] ); if ( null !== $v ) { $opts[ $k ] = $v; } } if ( '' !== $a['bank'] ) { $opts['bank'] = sanitize_key( $a['bank'] ); } return mulabase_widget( array( $calcs[ $slug ] ), array_filter( $opts ) ); } /** * The "Rates checked" line, sources and optional credit under a server-rendered card. * * @param array $calc Calculator (or null for rates). * @param string $date ISO date. * @param string $credit_url Page to credit. * @return string */ function mulabase_foot( $calc, $date, $credit_url ) { $html = '
'; if ( $date ) { $html .= '' . esc_html( __( 'Rates checked', 'mulabase' ) . ' ' . date_i18n( get_option( 'date_format' ), strtotime( $date ) ) ) . ''; } if ( $calc && ! empty( $calc['sources'] ) ) { $html .= '
' . esc_html__( 'Sources', 'mulabase' ) . '' . ( ! empty( $calc['caveat'] ) ? '

' . esc_html( $calc['caveat'] ) . '

' : '' ) . '
'; } if ( mulabase_settings()['credit'] ) { $html .= '' . esc_html__( 'Data: Mulabase', 'mulabase' ) . ''; } // Nothing to show (no date, sources or credit): no empty strip. return '
' === $html ? '' : $html . '
'; } /** * [mulabase_fee_table country="ke" columns="send,withdraw" title=""] * The whole tariff, band by band. It updates itself when the provider changes prices. * * @param array $atts Attributes. * @return string */ function mulabase_sc_fee_table( $atts ) { $a = shortcode_atts( array( 'country' => '', 'columns' => '', 'title' => '', ), $atts, 'mulabase_fee_table' ); $list = mulabase_pick( 'tariff', '' === $a['country'] ? mulabase_settings()['country'] : $a['country'] ); if ( ! $list ) { return mulabase_notice( __( 'no mobile money tariff for that country.', 'mulabase' ) ); } $calc = $list[0]; $cfg = $calc['rules']; $want = array_filter( array_map( 'sanitize_key', explode( ',', $a['columns'] ) ) ); $cols = array_values( array_filter( $cfg['columns'], function ( $c ) use ( $want ) { return ! $want || in_array( strtolower( $c['key'] ), array_map( 'strtolower', $want ), true ); } ) ); if ( ! $cols ) { $cols = $cfg['columns']; } wp_enqueue_style( 'mulabase' ); $cur = $calc['currency']; $fmt = function ( $v ) use ( $cur ) { if ( null === $v ) { return '—'; } if ( is_array( $v ) ) { $txt = rtrim( rtrim( number_format_i18n( $v['pct'], 2 ), '0' ), '.' ) . '%'; if ( isset( $v['cap'] ) ) { /* translators: %s: maximum fee. */ $txt .= ' ' . sprintf( __( '(max %s)', 'mulabase' ), mulabase_money( $cur, $v['cap'] ) ); } return esc_html( $txt ); } return 0.0 === (float) $v ? '' . esc_html__( 'Free', 'mulabase' ) . '' : esc_html( mulabase_money( $cur, $v ) ); }; $html = '
'; $html .= '

' . esc_html( '' !== $a['title'] ? $a['title'] : $calc['name'] ) . '

'; $html .= '
'; foreach ( $cols as $c ) { $html .= ''; } $html .= ''; foreach ( $cfg['rows'] as $r ) { $num = function ( $n ) { return number_format_i18n( $n, floor( $n ) == $n ? 0 : 2 ); // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual }; $range = $num( $r['min'] ) . ( null === $r['max'] ? '+' : ' – ' . $num( $r['max'] ) ); $html .= ''; foreach ( $cols as $c ) { $html .= ''; // Escaped in $fmt. } $html .= ''; } $html .= '
' . esc_html( __( 'Amount', 'mulabase' ) . ' (' . $cur . ')' ) . '' . esc_html( $c['label'] ) . '
' . esc_html( $range ) . '' . $fmt( isset( $r['values'][ $c['key'] ] ) ? $r['values'][ $c['key'] ] : null ) . '
' . mulabase_foot( $calc, $calc['verifiedAt'], $calc['url'] ) . '
'; return $html; } /** * [mulabase_rates countries="ke,ug,tz" indicators="policy-rate,inflation" title=""] * * @param array $atts Attributes. * @return string */ function mulabase_sc_rates( $atts ) { $a = shortcode_atts( array( 'countries' => '', 'indicators' => 'policy-rate,inflation', 'title' => '', ), $atts, 'mulabase_rates' ); $data = mulabase_data(); $keys = array_filter( array_map( 'sanitize_key', explode( ',', $a['indicators'] ) ) ); $codes = 'all' === strtolower( trim( $a['countries'] ) ) || '' === trim( $a['countries'] ) ? array() : array_filter( array_map( 'mulabase_country_code', explode( ',', $a['countries'] ) ) ); $by = array(); $latest = ''; foreach ( $data['indicators'] as $s ) { if ( ( $codes && ! in_array( $s['country'], $codes, true ) ) || ! in_array( $s['key'], $keys, true ) || empty( $s['latest'] ) ) { continue; } $by[ $s['country'] ][ $s['key'] ] = $s; $latest = max( $latest, (string) $s['latest']['asOf'] ); } if ( ! $by ) { return mulabase_notice( __( 'no rates for those countries yet.', 'mulabase' ) ); } wp_enqueue_style( 'mulabase' ); $order = $codes ? $codes : array_keys( $by ); if ( ! $codes ) { usort( $order, function ( $x, $y ) use ( $data ) { return strcmp( isset( $data['countries'][ $x ] ) ? $data['countries'][ $x ] : $x, isset( $data['countries'][ $y ] ) ? $data['countries'][ $y ] : $y ); } ); } $html = '
'; $html .= '

' . esc_html( '' !== $a['title'] ? $a['title'] : __( 'Central bank rates and inflation', 'mulabase' ) ) . '

'; foreach ( $order as $code ) { if ( empty( $by[ $code ] ) ) { continue; } $html .= '
' . esc_html( isset( $data['countries'][ $code ] ) ? $data['countries'][ $code ] : strtoupper( $code ) ) . '
'; foreach ( $keys as $k ) { if ( empty( $by[ $code ][ $k ] ) ) { continue; } $s = $by[ $code ][ $k ]; $arrow = ''; if ( 'up' === $s['direction'] ) { $arrow = ' ▲'; } elseif ( 'down' === $s['direction'] ) { $arrow = ' ▼'; } $short = 'policy-rate' === $k ? __( 'Policy rate', 'mulabase' ) : ( 'inflation' === $k ? __( 'Inflation', 'mulabase' ) : $s['label'] ); $html .= '
' . esc_html( $short ) . '' . esc_html( $s['latest']['value'] . '%' ) . $arrow . '
'; $html .= '
' . ( 'policy-rate' === $k ? esc_html( $s['name'] ) . ' · ' : '' ) . esc_html( date_i18n( 'j M Y', strtotime( $s['latest']['asOf'] ) ) ) . ' · ' . esc_html__( 'source', 'mulabase' ) . '
'; } $html .= '
'; } $html .= '
' . mulabase_foot( null, '', 'https://mulabase.com/money-rates/' ) . '
'; return $html; } /** * [mulabase_value type="fee" country="ke" amount="2500" transaction="send"] * [mulabase_value type="rate" country="ke" indicator="inflation"] * [mulabase_value type="take-home" country="ng" gross="6m" period="year"] (also: tax) * One live figure inside your text. Renders as plain text, so it reads naturally. * * @param array $atts Attributes. * @return string */ function mulabase_sc_value( $atts ) { $a = shortcode_atts( array( 'type' => 'fee', 'country' => '', 'amount' => '', 'transaction' => '', 'indicator' => 'policy-rate', 'gross' => '', 'period' => '', 'highlight' => '', ), $atts, 'mulabase_value' ); $code = mulabase_country_code( '' === $a['country'] ? mulabase_settings()['country'] : $a['country'] ); $type = sanitize_key( $a['type'] ); $value = null; if ( 'rate' === $type ) { foreach ( mulabase_data()['indicators'] as $s ) { if ( $s['country'] === $code && $s['key'] === sanitize_key( $a['indicator'] ) && ! empty( $s['latest'] ) ) { $value = rtrim( rtrim( number_format_i18n( $s['latest']['value'], 2 ), '0' ), '.' ) . '%'; } } } elseif ( 'fee' === $type ) { $calc = mulabase_main_calculator( 'tariff', $code ); $amount = mulabase_amount( $a['amount'] ); if ( $calc && null !== $amount ) { $r = mulabase_tariff_fee( $calc['rules'], $amount, sanitize_text_field( $a['transaction'] ) ); if ( $r ) { $value = null === $r['fee'] ? __( 'not available', 'mulabase' ) : ( 0.0 === $r['fee'] ? __( 'free', 'mulabase' ) : mulabase_money( $calc['currency'], $r['fee'] ) ); } } } elseif ( in_array( $type, array( 'take-home', 'net', 'tax' ), true ) ) { $calc = mulabase_main_calculator( 'income-tax', $code ); $gross = mulabase_amount( $a['gross'] ); if ( $calc && null !== $gross ) { $cfg = $calc['rules']; $own = isset( $cfg['period'] ) ? $cfg['period'] : 'month'; $period = in_array( $a['period'], array( 'month', 'year' ), true ) ? $a['period'] : $own; $k = $period === $own ? 1 : ( 'year' === $period ? 1 / 12 : 12 ); $r = mulabase_income_tax( $cfg, $gross * $k ); $value = mulabase_money( $calc['currency'], ( 'tax' === $type ? $r['tax'] : $r['net'] ) / $k ); } } if ( null === $value ) { return mulabase_notice( __( 'figure not available. Check the shortcode attributes.', 'mulabase' ) ); } $hl = '' === $a['highlight'] ? mulabase_settings()['highlight'] : in_array( strtolower( $a['highlight'] ), array( '1', 'yes', 'true' ), true ); wp_enqueue_style( 'mulabase' ); return '' . esc_html( $value ) . ''; } add_shortcode( 'mulabase_take_home', 'mulabase_sc_take_home' ); add_shortcode( 'mulabase_mobile_money', 'mulabase_sc_mobile_money' ); add_shortcode( 'mulabase_calculator', 'mulabase_sc_calculator' ); add_shortcode( 'mulabase_fee_table', 'mulabase_sc_fee_table' ); add_shortcode( 'mulabase_rates', 'mulabase_sc_rates' ); add_shortcode( 'mulabase_value', 'mulabase_sc_value' );