Plugin Directory

Changeset 3273472


Ignore:
Timestamp:
04/15/2025 11:36:03 AM (11 months ago)
Author:
codeisartnet
Message:

update

Location:
codeart-units-converter/trunk/include/converter
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • codeart-units-converter/trunk/include/converter/area/area.php

    r3273038 r3273472  
    11<?php
    2 // http://localhost/wordpress/wp-json/CodeArtUnitsConverter/v1/area/?u=ares&v=22.55
    3 
    4 function webservice_codeart_units_converter_area_register_rest_fields(WP_REST_Request $request)
    5 {
    6     $unit  = $request->get_param('u');
    7     $value = $request->get_param('v');
    8 
    9     // Validate unit and value
    10     if (! $unit || ! $value) {
    11         return new WP_Error('invalid_params', 'Missing parameters', ['status' => 400]);
    12     }
    13 
    14     $conversion_map = [
    15         'square_meter'      => $value,                     // m²
    16         'are'               => $value * 100,               // 1 are = 100 m²
    17         'hectare'           => $value * 10000,             // 1 ha = 10,000 m²
    18         'square_kilometer'  => $value * 1e6,               // 1 km² = 1,000,000 m²
    19         'square_centimeter' => $value / 10000,             // 10,000 cm² = 1 m²
    20         'square_decimeter'  => $value / 100,               // 100 dm² = 1 m²
    21         'square_millimeter' => $value / 1e6,               // 1,000,000 mm² = 1 m²
    22         'square_micrometer' => $value / 1e12,              // 1e12 µm² = 1 m²
    23         'square_nanometer'  => $value / 1e18,              // 1e18 nm² = 1 m²
    24         'square_inch'       => $value * 0.00064516,        // 1 in² = 0.00064516 m²
    25         'square_foot'       => $value * 0.09290304,        // 1 ft² = 0.09290304 m²
    26         'square_yard'       => $value * 0.83612736,        // 1 yd² = 0.83612736 m²
    27         'square_rod'        => $value * 25.29285264,       // 1 square rod ≈ 25.29285264 m²
    28         'rood'              => $value * 1011.7141056,      // 1 rood = 1011.7141056 m²
    29         'acre'              => $value * 4046.8564224,      // 1 acre = 4046.8564224 m²
    30         'homestead'         => $value * 647497.027584,     // 1 homestead = 160 acres = 647497.027584 m²
    31         'square_mile'       => $value * 2.589988110336e6,  // 1 mi² = 2,589,988.110336 m²
    32         'township'          => $value * 9.3239931972096e7, // 1 township = 36 mi² = 93,239,931.972096 m²
    33     ];
    34 
    35     // Convert square meters to all units
    36     function m2_to_all($val)
    37     {
    38         return [
    39             "results" => [
    40                 "are"               => round($val * 0.01, 5),
    41                 "square_centimeter" => round($val * 10000, 5),
    42                 "square_decimeter"  => round($val * 100, 5),
    43                 "square_foot"       => round($val * 10.7639, 5),
    44                 "hectare"           => round($val * 0.0001, 5),
    45                 "square_inch"       => round($val * 1550, 5),
    46                 "square_kilometer"  => round($val * 1e-6, 5),
    47                 "square_millimeter" => round($val * 1000000, 5),
    48                 "square_nanometer"  => round($val * 1e+18, 5),
    49                 "square_yard"       => round($val * 1.19599, 5),
    50                 "square_micrometer" => round($val * 1e+12, 5),
    51                 "square_rod"        => round($val * 0.0395369, 5),
    52                 "rood"              => round($val * 0.001, 5),
    53                 "acre"              => round($val * 0.000247105, 5),
    54                 "homestead"         => round($val * 1.5444015444015E-6, 5),
    55                 "square_mile"       => round($val * 3.86102e-7, 5),
    56                 "township"          => round($val * 1.0725e-8, 5),
    57                 "square_meter"      => round($val, 5),
    58             ],
    59         ]
    60         ;
    61     }
    62 
    63     // Check if unit exists in the conversion map
    64     if (! array_key_exists($unit, $conversion_map)) {
    65         return new WP_Error('invalid_unit', 'Unit not supported', ['status' => 400]);
    66     }
    67 
    68     // Convert to square meters using the function
    69     $m2     = $conversion_map[$unit];
    70     $result = m2_to_all($m2);
    71 
    72     return rest_ensure_response($result);
    73 }
     2// Example: http://localhost/wordpress/wp-json/CodeArtUnitsConverter/v1/area/?u=are&v=22.55
    743
    754add_action('rest_api_init', function () {
    765    register_rest_route('CodeArtUnitsConverter/v1', '/area', [
    776        'methods'  => 'GET',
    78         'callback' => 'webservice_codeart_units_converter_area_register_rest_fields',
     7        'callback' => 'api_codeart_units_converter_area',
    798    ]);
    809});
     10
     11function api_codeart_units_converter_area(WP_REST_Request $request)
     12{
     13    $unit  = strtolower($request->get_param('u'));
     14    $value = floatval($request->get_param('v'));
     15
     16    if (empty($unit) || !is_numeric($value)) {
     17        return new WP_Error('invalid_params', 'Missing or invalid parameters', ['status' => 400]);
     18    }
     19
     20    // Conversion factors to square meters
     21    $to_m2 = [
     22        'square_meter'      => 1,
     23        'are'               => 100,
     24        'hectare'           => 10000,
     25        'square_kilometer'  => 1e6,
     26        'square_centimeter' => 0.0001,
     27        'square_decimeter'  => 0.01,
     28        'square_millimeter' => 1e-6,
     29        'square_micrometer' => 1e-12,
     30        'square_nanometer'  => 1e-18,
     31        'square_inch'       => 0.00064516,
     32        'square_foot'       => 0.09290304,
     33        'square_yard'       => 0.83612736,
     34        'square_rod'        => 25.29285264,
     35        'rood'              => 1011.7141056,
     36        'acre'              => 4046.8564224,
     37        'homestead'         => 647497.027584,
     38        'square_mile'       => 2.589988110336e6,
     39        'township'          => 9.3239931972096e7,
     40    ];
     41
     42    if (!isset($to_m2[$unit])) {
     43        return new WP_Error('invalid_unit', 'Unsupported unit', ['status' => 400]);
     44    }
     45
     46    // Convert to square meters
     47    $m2 = $value * $to_m2[$unit];
     48
     49    // Convert square meters to all other units
     50    $results = [];
     51    foreach ($to_m2 as $target_unit => $factor) {
     52        $results[$target_unit] = round($m2 / $factor, 5);
     53    }
     54
     55    return rest_ensure_response(['results' => $results]);
     56}
  • codeart-units-converter/trunk/include/converter/speed/speed.php

    r3273038 r3273472  
    11<?php
    2 //http://localhost/wordpress/wp-json/CodeArtUnitsConverter/v1/speed/?u=Kilometres-per-hour&v=22.55
    3 
    4 function webservice_codeart_units_converter_speed_register_rest_fields(WP_REST_Request $request)
    5 {
    6     $unit  = $request->get_param('u');
    7     $value = $request->get_param('v');
    8 
    9     // Define unit conversions in an array
    10 
    11     $conversions = [
    12         "kilometer_per_hour"    => $value,
    13         "kilometer_per_second"  => $value * 3600,         // 1 km/s = 3600 km/h
    14         "meter_per_second"      => $value * 3.6,          // 1 m/s = 3.6 km/h
    15         "millimeter_per_second" => $value / 277.777778,   // 1 mm/s ≈ 0.0036 km/h
    16         "centimeter_per_second" => $value / 27.777778,    // 1 cm/s ≈ 0.036 km/h
    17         "meter_per_hour"        => $value / 1000,         // 1000 m/h = 1 km/h
    18         "centimeter_per_hour"   => $value / 100000,       // 100000 cm/h = 1 km/h
    19         "mile_per_hour"         => $value / 0.6213711922, // mph to km/h
    20         "mile_per_second"       => $value / 0.0001726031, // mi/s to km/h
    21         "knot"                  => $value / 0.5399568035, // knots to km/h
    22         "foot_per_second"       => $value / 0.9113444153, // ft/s to km/h
    23         "yard_per_second"       => $value / 0.3037814718, // yd/s to km/h
    24         "speed_of_light"        => $value * 1079252848.8, // c to km/h
    25         "speed_of_sound"        => $value * 1234.8,       // Mach (at 20°C dry air) to km/h
    26     ];
    27 
    28     // Convert Kilometres per hour to all values
    29     function kmh_to_all($value)
    30     {
    31         return [
    32             "results" => [
    33                 "kilometer_per_hour"    => round($value, 5),                // Original value
    34                 "kilometer_per_second"  => round($value / 3600, 5),         // Converted to km/s
    35                 "meter_per_second"      => round($value / 3.6, 5),          // Converted to m/s
    36                 "millimeter_per_second" => round($value * 277.777778, 5),   // Converted to mm/s
    37                 "centimeter_per_second" => round($value * 27.777778, 5),    // Converted to cm/s
    38                 "meter_per_hour"        => round($value * 1000, 5),         // Converted to m/h
    39                 "centimeter_per_hour"   => round($value * 100000, 5),       // Converted to cm/h
    40                 "mile_per_hour"         => round($value * 0.6213711922, 5), // Converted to mph
    41                 "mile_per_second"       => round($value * 0.0001726031, 5), // Converted to mi/s
    42                 "knot"                  => round($value * 0.5399568035, 5), // Converted to knots
    43                 "foot_per_second"       => round($value * 0.9113444153, 5), // Converted to ft/s
    44                 "yard_per_second"       => round($value * 0.3037814718, 5), // Converted to yd/s
    45                 "speed_of_light"        => round($value / 1079252848.8, 5), // Converted to fraction of speed of light
    46                 "speed_of_sound"        => round($value / 1234.8, 5),       // Converted to fraction of speed of sound
    47             ],
    48         ];
    49     }
    50 
    51     // Check if the unit exists in the conversions array
    52     if (! isset($conversions[$unit])) {
    53         return new WP_REST_Response('Invalid unit provided', 400); // Return error if unit is not found
    54     }
    55 
    56     $kmh   = $conversions[$unit];             // Get the converted value to km/h
    57     $array = kmh_to_all($kmh);                // Convert km/h to all other units
    58     return new WP_REST_Response($array, 200); // Return response with converted values
    59 }
     2// Example: http://localhost/wordpress/wp-json/CodeArtUnitsConverter/v1/speed/?u=kilometer_per_hour&v=22.55
    603
    614add_action('rest_api_init', function () {
    625    register_rest_route('CodeArtUnitsConverter/v1', '/speed', [
    636        'methods'  => 'GET',
    64         'callback' => 'webservice_codeart_units_converter_speed_register_rest_fields',
     7        'callback' => 'api_codeart_units_converter_speed',
    658    ]);
    669});
     10
     11function api_codeart_units_converter_speed(WP_REST_Request $request)
     12{
     13    $unit  = strtolower(str_replace('-', '_', $request->get_param('u')));
     14    $value = floatval($request->get_param('v'));
     15
     16    if (empty($unit) || !is_numeric($value)) {
     17        return new WP_Error('invalid_params', 'Missing or invalid parameters', ['status' => 400]);
     18    }
     19
     20    // Conversion factors to km/h
     21    $to_kmh = [
     22        "kilometer_per_hour"    => 1,
     23        "kilometer_per_second"  => 3600,
     24        "meter_per_second"      => 3.6,
     25        "millimeter_per_second" => 0.0036,
     26        "centimeter_per_second" => 0.036,
     27        "meter_per_hour"        => 0.001,
     28        "centimeter_per_hour"   => 0.00001,
     29        "mile_per_hour"         => 1.609344,
     30        "mile_per_second"       => 5793.6384,
     31        "knot"                  => 1.852,
     32        "foot_per_second"       => 1.09728,
     33        "yard_per_second"       => 3.29184,
     34        "speed_of_light"        => 1.0792528488e9,
     35        "speed_of_sound"        => 1234.8,
     36    ];
     37
     38    if (!isset($to_kmh[$unit])) {
     39        return new WP_Error('invalid_unit', 'Unsupported unit', ['status' => 400]);
     40    }
     41
     42    // Convert to km/h
     43    $kmh = $value * $to_kmh[$unit];
     44
     45    // Convert km/h to all other units
     46    $results = [];
     47    foreach ($to_kmh as $target_unit => $factor) {
     48        $results[$target_unit] = round($kmh / $factor, 5);
     49    }
     50
     51    return rest_ensure_response(['results' => $results]);
     52}
  • codeart-units-converter/trunk/include/converter/weight/weight.php

    r3273038 r3273472  
    11<?php
    2 // Example: http://localhost/wordpress/wp-json/CodeArtUnitsConverter/v1/weight/?u=Grams&v=22.55
    3 
    4 function webservice_codeart_units_converter_weight_register_rest_fields(WP_REST_Request $request)
    5 {
    6     $unit  = $request->get_param('u');
    7     $value = $request->get_param('v');
    8 
    9     // Define conversion to Kilos
    10     $to_kilos = [
    11         "centigram"       => 1e-5,            // 1 cg = 0.00001 kg
    12         "carat"           => 0.0002,          // 1 carat = 0.0002 kg
    13         "dram"            => 0.0017718451953, // 1 dram ≈ 0.0017718452 kg
    14         "gram"            => 0.001,           // 1 g = 0.001 kg
    15         "grain"           => 6.479891e-5,     // 1 grain ≈ 0.00006479891 kg
    16         "troy_grain"      => 6.479891e-5,     // same as grain
    17         "hectogram"       => 0.1,             // 1 hg = 0.1 kg
    18         "kilogram"        => 1,               // base unit
    19         "kilonewton_mass" => 101.9716212978,  // 1 kN ≈ 101.97162 kg
    20         "pound"           => 0.45359237,      // 1 lb = 0.45359237 kg (exact)
    21         "troy_pound"      => 0.3732417216,    // 1 troy lb = 0.3732417216 kg
    22         "long_ton"        => 1016.0469088,    // 1 long ton = 1016.0469088 kg
    23         "milligram"       => 1e-6,            // 1 mg = 0.000001 kg
    24         "nanogram"        => 1e-12,           // 1 ng = 0.000000000001 kg
    25         "ounce"           => 0.028349523125,  // 1 oz = 0.028349523125 kg
    26         "troy_ounce"      => 0.0311034768,    // 1 troy oz = 0.0311034768 kg
    27         "short_ton"       => 907.18474,       // 1 short ton = 907.18474 kg
    28         "stone"           => 6.35029318,      // 1 stone = 6.35029318 kg
    29         "ton"             => 907.18474,       // same as short ton
    30         "troy_carat"      => 0.0002051967,    // 1 troy carat ≈ 0.0002051967 kg
    31         "microgram"       => 1e-9,            // 1 µg = 0.000000001 kg
    32     ];
    33 
    34     function Kilos_to_all($val)
    35     {
    36         return [
    37             "results" => [
    38                 "centigram"       => round($val * 100000, 5),              // 1 kg = 100,000 cg
    39                 "carat"           => round($val * 5000, 5),                // 1 kg = 5000 carats
    40                 "dram"            => round($val * 564.38339119329, 5),     // 1 kg = ~564.383 drams
    41                 "gram"            => round($val * 1000, 5),                // 1 kg = 1000 g
    42                 "grain"           => round($val * 15432.358352941, 5),     // 1 kg = ~15432.358 grains
    43                 "troy_grain"      => round($val * 15432.358352941, 5),     // same as grains
    44                 "hectogram"       => round($val * 10, 5),                  // 1 kg = 10 hg
    45                 "kilogram"        => $val,                                 // base
    46                 "kilonewton_mass" => round($val * 0.00980665, 5),          // 1 kg ≈ 0.00980665 kN
    47                 "pound"           => round($val * 2.20462262185, 5),       // 1 kg = 2.20462262185 lb
    48                 "troy_pound"      => round($val * 2.679228880719, 5),      // 1 kg ≈ 2.679228880719 troy lb
    49                 "long_ton"        => round($val * 0.00098420652761106, 5), // 1 kg ≈ 0.0009842065 long tons
    50                 "milligram"       => round($val * 1000000, 5),             // 1 kg = 1,000,000 mg
    51                 "nanogram"        => round($val * 1e+12, 5),               // 1 kg = 1,000,000,000,000 ng
    52                 "ounce"           => round($val * 35.27396194958, 5),      // 1 kg = 35.27396194958 oz
    53                 "troy_ounce"      => round($val * 32.15074656863, 5),      // 1 kg ≈ 32.15074656863 troy oz
    54                 "short_ton"       => round($val * 0.0011023113109244, 5),  // 1 kg ≈ 0.0011023113 short tons
    55                 "stone"           => round($val * 0.15747304441777, 5),    // 1 kg = ~0.157473 stones
    56                 "ton"             => round($val * 0.0011023113109244, 5),  // alias of short tons
    57                 "troy_carat"      => round($val * 4877.5510204082, 5),     // 1 kg ≈ 4877.551 troy carats
    58                 "microgram"       => round($val * 1e+9, 5),                // 1 kg = 1,000,000,000 µg
    59             ],
    60         ];
    61     }
    62 
    63     if (! isset($to_kilos[$unit])) {
    64         return new WP_REST_Response('Invalid unit provided', 400);
    65     }
    66 
    67     $kg    = $value * $to_kilos[$unit];
    68     $array = Kilos_to_all($kg);
    69 
    70     return new WP_REST_Response($array, 200);
    71 }
     2// Example: http://localhost/wordpress/wp-json/CodeArtUnitsConverter/v1/weight/?u=gram&v=22.55
    723
    734add_action('rest_api_init', function () {
    745    register_rest_route('CodeArtUnitsConverter/v1', '/weight', [
    756        'methods'  => 'GET',
    76         'callback' => 'webservice_codeart_units_converter_weight_register_rest_fields',
     7        'callback' => 'api_codeart_units_converter_weight',
    778    ]);
    789});
     10
     11function api_codeart_units_converter_weight(WP_REST_Request $request)
     12{
     13    $unit  = strtolower(str_replace('-', '_', $request->get_param('u')));
     14    $value = floatval($request->get_param('v'));
     15
     16    if (empty($unit) || !is_numeric($value)) {
     17        return new WP_Error('invalid_params', 'Missing or invalid parameters', ['status' => 400]);
     18    }
     19
     20    // Conversion factors to kilograms
     21    $to_kg = [
     22        "centigram"       => 1e-5,
     23        "carat"           => 0.0002,
     24        "dram"            => 0.0017718451953,
     25        "gram"            => 0.001,
     26        "grain"           => 6.479891e-5,
     27        "troy_grain"      => 6.479891e-5,
     28        "hectogram"       => 0.1,
     29        "kilogram"        => 1,
     30        "kilonewton_mass" => 101.9716212978,
     31        "pound"           => 0.45359237,
     32        "troy_pound"      => 0.3732417216,
     33        "long_ton"        => 1016.0469088,
     34        "milligram"       => 1e-6,
     35        "nanogram"        => 1e-12,
     36        "ounce"           => 0.028349523125,
     37        "troy_ounce"      => 0.0311034768,
     38        "short_ton"       => 907.18474,
     39        "stone"           => 6.35029318,
     40        "ton"             => 907.18474,
     41        "troy_carat"      => 0.0002051967,
     42        "microgram"       => 1e-9,
     43    ];
     44
     45    if (!isset($to_kg[$unit])) {
     46        return new WP_Error('invalid_unit', 'Unsupported unit', ['status' => 400]);
     47    }
     48
     49    // Convert input to kilograms
     50    $kg = $value * $to_kg[$unit];
     51
     52    // Convert kilograms to all other units
     53    $results = [];
     54    foreach ($to_kg as $target_unit => $factor) {
     55        $results[$target_unit] = round($kg / $factor, 5);
     56    }
     57
     58    return rest_ensure_response(['results' => $results]);
     59}
Note: See TracChangeset for help on using the changeset viewer.