*/
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 = '