Defaults. */ function acsfc_default_settings() { return array( 'default_region' => 'kanto', 'default_vehicle' => 'medium', 'title_text' => '', 'tax_rate' => 10, 'show_credit' => false, ); } /** * Create the option on activation. * * @return void */ function acsfc_activate() { if ( false === get_option( 'acsfc_settings' ) ) { add_option( 'acsfc_settings', acsfc_default_settings() ); } } register_activation_hook( __FILE__, 'acsfc_activate' ); /** * Get the plugin settings merged with the defaults. * * @return array Settings. */ function acsfc_get_settings() { $settings = wp_parse_args( get_option( 'acsfc_settings', array() ), acsfc_default_settings() ); if ( '' === $settings['title_text'] ) { $settings['title_text'] = __( 'Truck freight rate calculator', 'acs-freight-calculator' ); } $settings['tax_rate'] = max( 0, min( 100, (int) $settings['tax_rate'] ) ); $settings['show_credit'] = ! empty( $settings['show_credit'] ); return $settings; } /** * Transport bureaus covered by the bundled rate tables. * * @return array Region id => label. */ function acsfc_get_regions() { return array( 'hokkaido' => __( 'Hokkaido District Transport Bureau', 'acs-freight-calculator' ), 'tohoku' => __( 'Tohoku District Transport Bureau', 'acs-freight-calculator' ), 'kanto' => __( 'Kanto District Transport Bureau', 'acs-freight-calculator' ), 'hokuriku' => __( 'Hokuriku-Shinetsu District Transport Bureau', 'acs-freight-calculator' ), 'chubu' => __( 'Chubu District Transport Bureau', 'acs-freight-calculator' ), 'kinki' => __( 'Kinki District Transport Bureau', 'acs-freight-calculator' ), 'chugoku' => __( 'Chugoku District Transport Bureau', 'acs-freight-calculator' ), 'shikoku' => __( 'Shikoku District Transport Bureau', 'acs-freight-calculator' ), 'kyushu' => __( 'Kyushu District Transport Bureau', 'acs-freight-calculator' ), 'okinawa' => __( 'Okinawa General Bureau', 'acs-freight-calculator' ), ); } /** * Vehicle classes covered by the bundled rate tables. * * @return array Vehicle id => label. */ function acsfc_get_vehicles() { return array( 'small' => __( 'Light truck (2 t)', 'acs-freight-calculator' ), 'medium' => __( 'Medium truck (4 t)', 'acs-freight-calculator' ), 'large' => __( 'Heavy truck (10 t)', 'acs-freight-calculator' ), 'trailer' => __( 'Trailer (20 t)', 'acs-freight-calculator' ), ); } /** * Labels for the special vehicle surcharges defined in the bundled data. * * @return array Key => label. */ function acsfc_get_special_vehicle_labels() { return array( 'refrigerated' => __( 'Refrigerated or frozen vehicle', 'acs-freight-calculator' ), 'container' => __( 'Marine container trailer', 'acs-freight-calculator' ), 'cement' => __( 'Cement bulk carrier', 'acs-freight-calculator' ), 'dump' => __( 'Dump truck', 'acs-freight-calculator' ), 'mixer' => __( 'Concrete mixer truck', 'acs-freight-calculator' ), 'petroleum' => __( 'Petroleum tank truck', 'acs-freight-calculator' ), 'chemical' => __( 'Chemical tank truck', 'acs-freight-calculator' ), 'highpressure' => __( 'High-pressure gas transport vehicle', 'acs-freight-calculator' ), ); } /** * Read one of the bundled JSON data files. * * @param string $name File name without extension. * @return array|null Decoded data, or null when unavailable. */ function acsfc_load_data_file( $name ) { $file = ACSFC_PLUGIN_DIR . 'inc/data/' . sanitize_file_name( $name ) . '.json'; if ( ! file_exists( $file ) ) { return null; } $data = wp_json_file_decode( $file, array( 'associative' => true ) ); return is_array( $data ) ? $data : null; } /** * All bundled rate tables, keyed by region id. * * @return array> Region data. */ function acsfc_get_all_region_data() { $all = array(); foreach ( array_keys( acsfc_get_regions() ) as $region_id ) { $data = acsfc_load_data_file( $region_id ); if ( null !== $data ) { $all[ $region_id ] = $data; } } return $all; } /** * Strings handed to the front-end script. * * All source strings are English so that they can be translated on * translate.wordpress.org. * * @return array Key => translated string. */ function acsfc_get_script_strings() { return array( /* translators: %s: formatted amount. */ 'currency' => __( '¥%s', 'acs-freight-calculator' ), 'rowDistanceFare' => __( 'Distance-based fare', 'acs-freight-calculator' ), 'rowTimeFare' => __( 'Time-based fare', 'acs-freight-calculator' ), 'rowLoading' => __( 'Loading charge', 'acs-freight-calculator' ), 'rowUnloading' => __( 'Unloading charge', 'acs-freight-calculator' ), 'rowWaiting' => __( 'Waiting time charge', 'acs-freight-calculator' ), 'rowNight' => __( 'Night and early-morning surcharge', 'acs-freight-calculator' ), 'rowHoliday' => __( 'Holiday surcharge', 'acs-freight-calculator' ), 'rowWinter' => __( 'Winter surcharge', 'acs-freight-calculator' ), 'rowHighway' => __( 'Expressway tolls', 'acs-freight-calculator' ), 'rowFuel' => __( 'Fuel surcharge', 'acs-freight-calculator' ), 'rowSubtotal' => __( 'Subtotal (excluding tax)', 'acs-freight-calculator' ), /* translators: %s: tax rate in percent. */ 'rowTax' => __( 'Consumption tax (%s%%)', 'acs-freight-calculator' ), 'rowTotal' => __( 'Total (including tax)', 'acs-freight-calculator' ), /* translators: %s: distance in kilometres. */ 'detailOneWay' => __( 'One-way distance: %s km', 'acs-freight-calculator' ), /* translators: %s: formatted amount. */ 'detailDistanceFare' => __( 'Distance-based fare = %s', 'acs-freight-calculator' ), /* translators: %s: formatted amount. */ 'detailTimeBase' => __( 'Base fare (8-hour service): %s', 'acs-freight-calculator' ), /* translators: 1: hours, 2: hourly rate, 3: total amount. */ 'detailTimeOver' => __( 'Extra hours: %1$s h x %2$s = %3$s', 'acs-freight-calculator' ), /* translators: 1: kilometres, 2: rate per 10 km, 3: total amount. */ 'detailTimeOverKm' => __( 'Extra distance: %1$s km x %2$s per 10 km = %3$s', 'acs-freight-calculator' ), /* translators: %s: formatted amount. */ 'detailTimeFare' => __( 'Time-based fare = %s', 'acs-freight-calculator' ), /* translators: 1: minutes, 2: number of 30-minute slots. */ 'detailCargoHead' => __( '%1$s min = %2$s slot(s) of 30 minutes', 'acs-freight-calculator' ), /* translators: 1: minutes counted so far, 2: rate description, 3: amount. */ 'detailSlot' => __( 'Cumulative %1$s min: %2$s rate %3$s', 'acs-freight-calculator' ), 'rateStandard' => __( 'standard', 'acs-freight-calculator' ), 'rateOverTwoHours' => __( 'over two hours', 'acs-freight-calculator' ), /* translators: 1: minutes counted so far, 2: minutes. */ 'detailCargoCarry' => __( '%1$s min, continuing from %2$s min already counted', 'acs-freight-calculator' ), /* translators: 1: waiting minutes, 2: free minutes. */ 'detailWaitFree' => __( 'Waiting time %1$s min is within the free allowance of %2$s min', 'acs-freight-calculator' ), /* translators: %s: free minutes. */ 'detailWaitHead' => __( 'First %s min: free', 'acs-freight-calculator' ), /* translators: 1: base fare, 2: percentage, 3: amount. */ 'detailPercentOf' => __( 'Base fare %1$s x %2$s%% = %3$s', 'acs-freight-calculator' ), /* translators: 1: kilometres, 2: distance fare, 3: percentage, 4: amount. */ 'detailNight' => __( '%1$s km gives a distance-based fare of %2$s; %3$s%% of that is %4$s', 'acs-freight-calculator' ), /* translators: %s: formatted amount. */ 'detailActual' => __( 'Actual cost: %s', 'acs-freight-calculator' ), /* translators: %s: base diesel price per litre. */ 'detailFuelNone' => __( 'No surcharge: the diesel price is at or below the base price of %s per litre.', 'acs-freight-calculator' ), /* translators: 1: lower bound, 2: upper bound, 3: midpoint. */ 'detailFuelBand' => __( 'Price band %1$s to %2$s per litre, midpoint %3$s per litre', 'acs-freight-calculator' ), /* translators: %s: amount added per litre. */ 'detailFuelAdd' => __( 'Added amount: %s per litre', 'acs-freight-calculator' ), /* translators: 1: kilometres, 2: fuel efficiency, 3: litres. */ 'detailFuelLitres' => __( '%1$s km / %2$s km per litre = %3$s litres', 'acs-freight-calculator' ), /* translators: 1: litres, 2: amount per litre, 3: total amount. */ 'detailFuelTotal' => __( '%1$s litres x %2$s = %3$s, rounded up', 'acs-freight-calculator' ), /* translators: %s: fuel efficiency in km per litre. */ 'fuelHint' => __( 'Guide value: %s km/L', 'acs-freight-calculator' ), 'selectSpecial' => __( 'Select a special vehicle', 'acs-freight-calculator' ), /* translators: 1: vehicle label, 2: percentage. */ 'specialOption' => __( '%1$s (%2$s%% surcharge)', 'acs-freight-calculator' ), /* translators: %s: vehicle label. */ 'specialRow' => __( '%s surcharge', 'acs-freight-calculator' ), ); } /** * Register the front-end assets. * * @return void */ function acsfc_register_assets() { wp_register_style( 'acsfc-frontend', ACSFC_PLUGIN_URL . 'assets/css/frontend.css', array(), ACSFC_VERSION ); wp_register_script( 'acsfc-calculator', ACSFC_PLUGIN_URL . 'assets/js/calculator.js', array(), ACSFC_VERSION, true ); } add_action( 'init', 'acsfc_register_assets' ); /** * Enqueue the front-end assets and hand the rate tables to the script. * * Called only while a shortcode or block is being rendered, so the assets * never load on pages that do not contain the calculator. * * @return void */ function acsfc_enqueue_frontend_assets() { if ( wp_script_is( 'acsfc-calculator', 'enqueued' ) ) { return; } $settings = acsfc_get_settings(); wp_enqueue_style( 'acsfc-frontend' ); wp_enqueue_script( 'acsfc-calculator' ); wp_localize_script( 'acsfc-calculator', 'acsfcData', array( 'regions' => acsfc_get_all_region_data(), 'surcharges' => acsfc_load_data_file( 'surcharges' ), 'labels' => array( 'special' => acsfc_get_special_vehicle_labels(), ), 'settings' => array( 'taxRate' => (int) $settings['tax_rate'], ), 'i18n' => acsfc_get_script_strings(), ) ); } /** * Add a settings link to the plugin row. * * @param string[] $links Existing action links. * @return string[] Action links. */ function acsfc_plugin_action_links( $links ) { $settings_link = sprintf( '%s', esc_url( admin_url( 'options-general.php?page=acsfc-settings' ) ), esc_html__( 'Settings', 'acs-freight-calculator' ) ); array_unshift( $links, $settings_link ); return $links; } add_filter( 'plugin_action_links_' . ACSFC_PLUGIN_BASENAME, 'acsfc_plugin_action_links' ); /** * Load the bundled translations from the languages folder. * * @return void */ function acsfc_load_textdomain() { load_plugin_textdomain( 'acs-freight-calculator', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); } add_action( 'init', 'acsfc_load_textdomain', 0 );