'
);
add_menu_page( __( 'Mulabase', 'mulabase' ), __( 'Mulabase', 'mulabase' ), 'manage_options', 'mulabase', 'mulabase_admin_page', $icon, 81 );
}
add_action( 'admin_menu', 'mulabase_admin_menu' );
/**
* "Settings" link on the Plugins screen.
*
* @param array $links Links.
* @return array
*/
function mulabase_action_links( $links ) {
array_unshift( $links, '' . esc_html__( 'Settings', 'mulabase' ) . '' );
return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( MULABASE_FILE ), 'mulabase_action_links' );
/**
* Settings registration.
*/
function mulabase_register_settings() {
register_setting(
'mulabase',
'mulabase_settings',
array(
'type' => 'array',
'sanitize_callback' => 'mulabase_sanitize_settings',
'default' => array(),
)
);
}
add_action( 'admin_init', 'mulabase_register_settings' );
/**
* Sanitizes the settings form.
*
* @param mixed $in Submitted values.
* @return array
*/
function mulabase_sanitize_settings( $in ) {
$in = is_array( $in ) ? $in : array();
return array(
'country' => isset( $in['country'] ) ? sanitize_key( $in['country'] ) : 'ke',
'accent' => isset( $in['accent'] ) && preg_match( '/^#[0-9a-f]{6}$/i', $in['accent'] ) ? strtolower( $in['accent'] ) : '#11794a',
'theme' => isset( $in['theme'] ) && in_array( $in['theme'], array( 'light', 'dark', 'auto' ), true ) ? $in['theme'] : 'light',
'credit' => ! empty( $in['credit'] ),
'highlight' => ! empty( $in['highlight'] ),
);
}
/**
* "Sync now" button.
*/
function mulabase_handle_sync() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do that.', 'mulabase' ) );
}
check_admin_referer( 'mulabase_sync' );
$r = mulabase_sync();
wp_safe_redirect( add_query_arg( 'mulabase_synced', is_wp_error( $r ) ? '0' : '1', admin_url( 'admin.php?page=mulabase' ) ) );
exit;
}
add_action( 'admin_post_mulabase_sync', 'mulabase_handle_sync' );
/**
* "Download the data" button: the administrator's explicit go-ahead to contact
* Mulabase. Turns on the twice-daily sync and downloads straight away.
*/
function mulabase_handle_connect() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do that.', 'mulabase' ) );
}
check_admin_referer( 'mulabase_connect' );
update_option( 'mulabase_connected', 1, false );
mulabase_schedule();
$r = mulabase_sync();
wp_safe_redirect( add_query_arg( 'mulabase_synced', is_wp_error( $r ) ? '0' : '1', admin_url( 'admin.php?page=mulabase' ) ) );
exit;
}
add_action( 'admin_post_mulabase_connect', 'mulabase_handle_connect' );
/**
* "Stop downloading" link: turns the sync off. The last copy stays so widgets keep working.
*/
function mulabase_handle_disconnect() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do that.', 'mulabase' ) );
}
check_admin_referer( 'mulabase_disconnect' );
delete_option( 'mulabase_connected' );
wp_clear_scheduled_hook( 'mulabase_sync_event' );
wp_safe_redirect( admin_url( 'admin.php?page=mulabase' ) );
exit;
}
add_action( 'admin_post_mulabase_disconnect', 'mulabase_handle_disconnect' );
/**
* On the Plugins screen only: a notice pointing to the Mulabase screen until
* downloads are turned on.
*/
function mulabase_setup_notice() {
$screen = get_current_screen();
if ( ! $screen || 'plugins' !== $screen->id || mulabase_connected() || ! current_user_can( 'manage_options' ) ) {
return;
}
printf(
'',
esc_html__( 'Mulabase needs your OK before it downloads the rates it shows.', 'mulabase' ),
esc_url( admin_url( 'admin.php?page=mulabase' ) ),
esc_html__( 'Set it up', 'mulabase' )
);
}
add_action( 'admin_notices', 'mulabase_setup_notice' );
/**
* Live preview for the builder: renders one shortcode.
*/
function mulabase_ajax_preview() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( null, 403 );
}
check_ajax_referer( 'mulabase_preview' );
$allowed = array( 'mulabase_take_home', 'mulabase_mobile_money', 'mulabase_calculator', 'mulabase_fee_table', 'mulabase_rates', 'mulabase_value' );
$tag = isset( $_POST['tag'] ) ? sanitize_key( wp_unslash( $_POST['tag'] ) ) : '';
if ( ! in_array( $tag, $allowed, true ) ) {
wp_send_json_error( null, 400 );
}
$atts = array();
foreach ( array( 'country', 'countries', 'slug', 'amount', 'gross', 'title', 'type', 'transaction', 'indicator', 'indicators', 'columns' ) as $k ) {
if ( isset( $_POST[ $k ] ) && '' !== $_POST[ $k ] ) {
$atts[ $k ] = sanitize_text_field( wp_unslash( $_POST[ $k ] ) );
}
}
$fn = 'mulabase_sc_' . substr( $tag, strlen( 'mulabase_' ) );
wp_send_json_success( array( 'html' => call_user_func( $fn, $atts ) ) );
}
add_action( 'wp_ajax_mulabase_preview', 'mulabase_ajax_preview' );
/**
* Assets for the Mulabase screen only.
*
* @param string $hook Current screen.
*/
function mulabase_admin_assets( $hook ) {
if ( 'toplevel_page_mulabase' !== $hook ) {
return;
}
wp_enqueue_style( 'mulabase-admin', MULABASE_URL . 'assets/admin.css', array(), MULABASE_VERSION );
mulabase_enqueue_widget( array_values( mulabase_data()['calculators'] ) );
wp_add_inline_script( 'mulabase', 'window.MulabaseData.editor=true;', 'before' );
wp_enqueue_script( 'mulabase-admin', MULABASE_URL . 'assets/admin.js', array( 'mulabase' ), MULABASE_VERSION, true );
wp_localize_script(
'mulabase-admin',
'MulabaseAdmin',
array(
'ajax' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'mulabase_preview' ),
'copied' => __( 'Copied', 'mulabase' ),
'copy' => __( 'Copy', 'mulabase' ),
'feeFor' => __( 'Fee for', 'mulabase' ),
)
);
}
add_action( 'admin_enqueue_scripts', 'mulabase_admin_assets' );
/**
* Human time since the last sync.
*
* @param int $ts Timestamp.
* @return string
*/
function mulabase_since( $ts ) {
/* translators: %s: time, e.g. "2 hours". */
return $ts ? sprintf( __( '%s ago', 'mulabase' ), human_time_diff( $ts ) ) : __( 'never', 'mulabase' );
}
/**
* The Mulabase screen.
*/
function mulabase_admin_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$data = mulabase_data();
$s = mulabase_settings();
$calcs = $data['calculators'];
$paye = mulabase_main_calculators( 'income-tax' );
$momo = mulabase_main_calculators( 'tariff' );
$error = get_option( 'mulabase_sync_error', '' );
$synced = isset( $_GET['mulabase_synced'] ) ? sanitize_key( wp_unslash( $_GET['mulabase_synced'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$others = array_filter(
$calcs,
function ( $c ) {
return in_array( $c['type'], array( 'loan', 'savings', 'dividend', 'bank-fees' ), true );
}
);
$fresh = 0;
foreach ( $calcs as $c ) {
$fresh = max( $fresh, $c['verifiedAt'] ? strtotime( $c['verifiedAt'] ) : 0 );
}
$opt = function ( $value, $label, $current ) {
return '';
};
?>
Mulabase
↗
[mulabase_take_home country="ke"]
[mulabase_mobile_money country="ke" amount="2500"]
[mulabase_fee_table country="ke"]
[mulabase_rates countries="ke,ug,tz"]
[mulabase_calculator slug="kenya-mortgage-loan-calculator"]
[mulabase_value type="fee" country="ke" amount="2500"]