rty - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of rty.php

  1. <?php
  2. /*
  3.  
  4. Plugin Name: Oppso Unit Converter
  5.  
  6. Plugin URI: http://www.oppso.com/oppso-unit-converter/
  7.  
  8. Description: Simple to use unit converter. It comes as a shortcode that you can use in a post / page or a text widget. Check the settings page for the shortcodes
  9.  
  10. Version: 1.1.1
  11.  
  12. Author: Dan Busuioc
  13.  
  14. Author URI: http://www.oppso.com/
  15.  
  16. */
  17.  
  18.  
  19.  
  20. add_action('admin_menu','oppso_calc_add_menu');
  21.  
  22.  
  23.  
  24. add_filter('widget_text', 'do_shortcode');
  25.  
  26.  
  27.  
  28. add_shortcode('oppso_unit_converter', 'oppso_converter_shortcode');
  29.  
  30.  
  31.  
  32. add_action('wp_enqueue_scripts', 'oppso_scripts');
  33.  
  34.  
  35.  
  36. global $formulae_array ; 
  37.  
  38. $formulae_array = json_decode(file_get_contents(plugins_url("formulae.opp",__FILE__)),"true");
  39.  
  40.  
  41.  
  42.  
  43.  
  44. add_action('wp_head', 'oppso_ajax_url');
  45.  
  46.  
  47.  
  48. function oppso_ajax_url(){
  49.  
  50.         ?>
  51.  
  52.         <script type="text/javascript">
  53.  
  54.         var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
  55.  
  56.         </script>
  57.  
  58.         <?php 
  59.  
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66. function oppso_scripts(){
  67.  
  68.        
  69.  
  70. wp_register_style( 'oppso-converter-style', plugins_url('css/oppso-converter.css', __FILE__) );
  71.  
  72. wp_enqueue_style( 'oppso-converter-style' );
  73.  
  74.         wp_enqueue_script(
  75.  
  76.                         'oppso-converter', 
  77.  
  78.         plugins_url('/js/oppso-converter.js', __FILE__),array('jquery')
  79.  
  80.         );
  81.  
  82. }
  83.  
  84.  
  85.  
  86. function oppso_calc_add_menu(){
  87.  
  88.         add_options_page('Oppso Unit Converter', 'Oppso Unit Converter', 'administrator', __FILE__, 'oppso_converter_show_options');
  89.  
  90. }
  91.  
  92. function oppso_converter_show_options(){
  93.  
  94. global $formulae_array;
  95.  
  96. ?>
  97.  
  98.  
  99.  
  100. <div>
  101.  
  102. <h1>Unit Converter Plugin</h1>
  103.  
  104.         A simple unit converter from <a href="">Oppso.com</a><br />
  105.  
  106.         This is the list of shortcodes that you can use to display the converter. Note that you can use this shortcode both inside a post / page or inside a widget.
  107.  
  108.        
  109.  
  110. </div><br />
  111.  
  112. <table cellpadding="3" cellspacing="3">
  113.  
  114. <tr>
  115.  
  116.         <td><strong>All unit converters</strong>
  117.  
  118.         </td>
  119.  
  120.         <td>[oppso_unit_converter]
  121.  
  122.         </td>
  123.  
  124. </tr>
  125.  
  126.         <?php 
  127.  
  128. foreach($formulae_array['formulae'] as $key=>$value){
  129.  
  130.        
  131.  
  132.         ?>
  133.  
  134.         <tr>
  135.  
  136.         <td><strong><?php echo $value['title']; ?></strong>
  137.  
  138.         </td>
  139.  
  140.         <td>[oppso_unit_converter converter=<?php echo $key ?>]
  141.  
  142.         </td>
  143.  
  144.         </tr>
  145.  
  146.         <?php }?>
  147.  
  148. </table>
  149.  
  150. <!--  
  151.  
  152.  <form style="float:left" action="options.php" method="post">
  153.  
  154.  
  155.  
  156.  <p class="submit">
  157.  
  158.      <input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
  159.  
  160.  </p>
  161.  
  162.             
  163.  
  164.             
  165.  
  166.            
  167.  
  168.            
  169.  
  170.                <div style="float:left" id=""><?php oppso_converter_preview() ?></div>
  171.  
  172.              
  173.  
  174. </form>-->
  175.  
  176.         <?php 
  177.  
  178. }
  179.  
  180. function oppso_do_change(){
  181.  
  182.         global $formulae_array;
  183.  
  184.         $converter = $_POST['converter_type'];
  185.  
  186.        
  187.  
  188.         $formulae = $formulae_array;
  189.  
  190.         $formulae = $formulae['formulae'];
  191.  
  192.        
  193.  
  194.         $converter_data = ($formulae[$converter]);
  195.  
  196.        
  197.  
  198.         $convert_options_arr = $converter_data['select_box'];
  199.  
  200.         foreach($convert_options_arr as $key=>$value){
  201.  
  202.                 $convert_options .= '<option value="'.$key.'">'.$value.'</option>';
  203.  
  204.         }
  205.  
  206.        
  207.  
  208.         $show .= '<div>'.$converter_data['title'].'</div>';
  209.  
  210.         $show .= '<div class="oppso-converter-description">'.$converter_data['description'].'</div>';
  211.  
  212.         $show .= '<div class="oppso-converter-form">
  213.  
  214.        
  215.  
  216.                <input type="hidden" name="oppso_converter_type" value="'.$converter.'" id="oppso_converter_type"/>
  217.  
  218.                 <table cellspacing="3" class="oppso-form-table">
  219.  
  220.                            <tr><td>Value</td><td>  <input  class="oppso-input"  type="text" name="oppso_value" value="" id="oppso_value" />
  221.  
  222.                            </td></tr>
  223.  
  224.                                     <tr><td width="40%">From</td><td>
  225.  
  226.                 <select class="oppso-select" id="oppso_from">'.$convert_options.'</select>
  227.  
  228.                 </td></tr>';
  229.  
  230.         $show .= '<tr><td  width="40%">To</td><td>
  231.  
  232.                         <select class="oppso-select" id="oppso_to">'.$convert_options.'</select></td></tr>
  233.  
  234.                        
  235.  
  236.                 <tr><td><input type="button" name="convert" value="Convert" id="oppso_convert"></td></tr></table>
  237.  
  238.                 <div id="oppso_convert_result" class="oppso-convert-result"></div>
  239.  
  240.        
  241.  
  242.                                
  243.  
  244.                 ';
  245.  
  246.         $show .= '';
  247.  
  248.         echo $show;
  249.  
  250.         die();
  251.  
  252. }
  253.  
  254. function oppso_do_convert(){
  255.  
  256.        
  257.  
  258.         global $formulae_array;
  259.  
  260.         $from = $_POST['from'];
  261.  
  262.         $to = $_POST['to'];
  263.  
  264.         $converter_type = $_POST['converter_type'];
  265.  
  266.         $value = $_POST['oppso_value'];
  267.  
  268.        
  269.  
  270.        
  271.  
  272.         $formulae = $formulae_array;
  273.  
  274.         $formulae = $formulae['formulae'];
  275.  
  276.        
  277.  
  278.         $converter_data = ($formulae[$converter_type]);
  279.  
  280.         $converter_base_unit = $converter_data['base_unit'];
  281.  
  282.         $converter_to = $converter_data['convert_to'];
  283.  
  284.         if($converter_type != 'temperature'){
  285.  
  286.                 $converted_value = $value*$converter_to[$from]*$converter_to[$converter_base_unit]/$converter_to[$to];
  287.  
  288.         }
  289.  
  290.         else{
  291.  
  292.                 $converted_value =     (($value-$converter_to[$from]['base'])/$converter_to[$from]['ratio'])*$converter_to[$to]['ratio'] + $converter_to[$to]['base']; 
  293.  
  294.  
  295.  
  296.         }
  297.  
  298.         echo json_encode(round($converted_value,8));
  299.  
  300.         die();
  301.  
  302.        
  303.  
  304. }
  305.  
  306. function oppso_converter_shortcode($atts){
  307.  
  308.         global $formulae_array;
  309.  
  310.         wp_enqueue_script('jquery');
  311.  
  312.         extract( shortcode_atts( array(
  313.  
  314.                                                 'converter' => '',
  315.  
  316.        
  317.  
  318.        
  319.  
  320.         ), $atts ) );
  321.  
  322.         $formulae = $formulae_array;
  323.  
  324.         $formulae = $formulae['formulae'];
  325.  
  326.         $show.='<div id="oppso-converter-box">';
  327.  
  328.         if($converter==''){
  329.  
  330.                 $options = '';
  331.  
  332.                 $i=0;
  333.  
  334.                 foreach($formulae as $key=>$value){
  335.  
  336.                         if($i==0){
  337.  
  338.                                 $converter = $key;
  339.  
  340.                         }
  341.  
  342.                         $i++;
  343.  
  344.                         $options.='<option value="'.$key.'">'.ucfirst($key).'</option>';
  345.  
  346.                 }
  347.  
  348.                 $show.='<div id="converter-selection"><select class="oppso-select">'.$options.'</select></div>';
  349.  
  350.         }
  351.  
  352.        
  353.  
  354.        
  355.  
  356.         $converter_data = ($formulae[$converter]);
  357.  
  358.        
  359.  
  360.         $convert_options_arr = $converter_data['select_box'];
  361.  
  362.         foreach($convert_options_arr as $key=>$value){
  363.  
  364.                 $convert_options .= '<option value="'.$key.'">'.$value.'</option>';
  365.  
  366.         }
  367.  
  368.        
  369.  
  370.         $show .= '<div id="oppso-converter-type"><div>'.$converter_data['title'].'</div>';
  371.  
  372.         $show .= '<div class="oppso-converter-description">'.$converter_data['description'].'</div>';
  373.  
  374.         $show .= '<div class="oppso-converter-form">
  375.  
  376.  
  377.  
  378.        <input type="hidden" name="oppso_converter_type" value="'.$converter.'" id="oppso_converter_type"/>
  379.  
  380.         <table cellspacing="3" class="oppso-form-table">
  381.  
  382.                    <tr><td>Value</td><td>  <input  class="oppso-input"  type="text" name="oppso_value" value="" id="oppso_value" />
  383.  
  384.                    </td></tr>
  385.  
  386.                             <tr><td width="40%">From</td><td>
  387.  
  388.         <select class="oppso-select" id="oppso_from">'.$convert_options.'</select>
  389.  
  390.         </td></tr>';
  391.  
  392.         $show .= '<tr><td  width="40%">To</td><td>
  393.  
  394.                 <select class="oppso-select" id="oppso_to">'.$convert_options.'</select></td></tr>
  395.  
  396.                
  397.  
  398.         <tr><td><input type="button" name="convert" value="Convert" id="oppso_convert"></td></tr></table>
  399.  
  400.         <div id="oppso_convert_result" class="oppso-convert-result"></div>
  401.  
  402.  
  403.  
  404.                        
  405.  
  406.         ';
  407.  
  408.         $show .= '</div><div>';
  409.  
  410.        
  411.  
  412.         return $show;
  413.  
  414. }
  415.  
  416. function oppso_converter_preview(){
  417.  
  418.         echo oppso_converter_shortcode(array("converter"=>"distance"));
  419.  
  420. }
  421.  
  422.  
  423.  
  424. add_action('wp_ajax_nopriv_oppso_do_convert', 'oppso_do_convert');
  425.  
  426. add_action('wp_ajax_oppso_do_convert', 'oppso_do_convert');
  427.  
  428. add_action('wp_ajax_nopriv_oppso_do_change', 'oppso_do_change');
  429.  
  430. add_action('wp_ajax_oppso_do_change', 'oppso_do_change');
  431.  
  432. ?>
File Description
  • rty
  • PHP Code
  • 09 Aug-2018
  • 7.17 Kb
You can Share it: