Shipping.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Field_Shipping
  4. */
  5. class NF_Fields_Shipping extends NF_Abstracts_Input
  6. {
  7. protected $_name = 'shipping';
  8. protected $_section = 'pricing';
  9. protected $_icon = 'truck';
  10. protected $_aliases = array();
  11. protected $_type = 'shipping';
  12. protected $_templates = 'shipping';
  13. protected $_test_value = '0.00';
  14. protected $_settings = array( 'shipping_type', 'shipping_cost', 'shipping_options' );
  15. protected $_settings_exclude = array( 'input_limit_set', 'disable_input', 'required' );
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->_nicename = __( 'Shipping', 'ninja-forms' );
  20. add_filter( 'ninja-forms-field-settings-groups', array( $this, 'add_setting_group' ) );
  21. add_filter( 'ninja_forms_merge_tag_value_shipping', array( $this, 'merge_tag_value' ), 10, 2 );
  22. }
  23. public function add_setting_group( $groups )
  24. {
  25. $groups[ 'advanced_shipping' ] = array(
  26. 'id' => 'advanced_shipping',
  27. 'label' => __( 'Advanced Shipping', 'ninja-forms' ),
  28. );
  29. return $groups;
  30. }
  31. public function admin_form_element( $id, $value )
  32. {
  33. $field = Ninja_Forms()->form()->get_field( $id );
  34. $value = $field->get_setting( 'shipping_cost' );
  35. switch( $field->get_setting( 'shipping_type' ) ){
  36. case 'single':
  37. return "<input class='widefat' name='fields[$id]' value='$value' />";
  38. case 'select':
  39. $options = '<option>--</option>';
  40. foreach( $field->get_setting( 'shipping_options' ) as $option ){
  41. $selected = ( $value == $option[ 'value' ] ) ? "selected" : '';
  42. $options .= "<option value='{$option[ 'value' ]}' $selected>{$option[ 'label' ]}</option>";
  43. }
  44. return "<select class='widefat' name='fields[$id]' id=''>$options</select>";
  45. default:
  46. return "";
  47. }
  48. }
  49. public function merge_tag_value( $value, $field )
  50. {
  51. if( isset( $field[ 'shipping_type' ] ) ){
  52. switch( $field[ 'shipping_type' ] ){
  53. case 'single':
  54. $value = $field[ 'shipping_cost' ];
  55. break;
  56. case 'select':
  57. $value = $field[ 'shipping_options' ];
  58. break;
  59. }
  60. }
  61. $value = preg_replace ('/[^\d,\.]/', '', $value );
  62. return $value;
  63. }
  64. }