PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/ninja-forms/includes/Fields/Product.php

https://bitbucket.org/iroshbrianskye/genteelfinal
PHP | 217 lines | 122 code | 57 blank | 38 comment | 33 complexity | f50ef2941e14af3b274301cef6ff70e2 MD5 | raw file
Possible License(s): GPL-3.0, MIT, Apache-2.0
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Fields_Product
  4. */
  5. class NF_Fields_Product extends NF_Abstracts_Input
  6. {
  7. protected $_name = 'product';
  8. protected $_section = 'pricing';
  9. protected $_icon = 'tag';
  10. protected $_aliases = array();
  11. protected $_type = 'product';
  12. protected $_templates = array( 'product', 'textbox', 'hidden', 'listselect' );
  13. protected $_test_value = '0';
  14. protected $processing_fields = array( 'quantity', 'modifier', 'shipping', 'tax', 'total' );
  15. protected $_settings = array( 'product_use_quantity', 'product_price', 'product_type', 'product_type' );
  16. protected $_settings_exclude = array( 'input_limit_set', 'disable_input', 'required' );
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->_nicename = __( 'Product', 'ninja-forms' );
  21. $this->_settings[ 'product_price' ][ 'width' ] = 'full';
  22. add_filter( 'ninja_forms_merge_tag_value_product', array( $this, 'merge_tag_value' ), 10, 2 );
  23. add_filter( 'ninja_forms_custom_columns', array( $this, 'custom_columns' ), 10, 3 );
  24. add_filter( 'ninja_forms_merge_tag_calc_value_product', array( $this, 'merge_tag_value' ), 10, 2 );
  25. add_filter( 'ninja_forms_localize_field_' . $this->_name, array( $this, 'filter_required_setting' ) );
  26. add_filter( 'ninja_forms_localize_field_' . $this->_name . '_preview', array( $this, 'filter_required_setting' ) );
  27. }
  28. public function process( $product, $data )
  29. {
  30. $related = array();
  31. foreach( $data[ 'fields' ] as $key => $field ){
  32. if( ! isset ( $field[ 'type' ] ) || ! in_array( $field[ 'type' ], $this->processing_fields ) ) continue;
  33. $type = $field[ 'type' ];
  34. if( ! isset( $field[ 'product_assignment' ] ) ) continue;
  35. if( $product[ 'id' ] != $field[ 'product_assignment' ] ) continue;
  36. $related[ $type ] = &$data[ 'fields' ][ $key ]; // Assign by reference
  37. }
  38. //TODO: Does not work in non-English locales
  39. $total = str_replace( array( ',', '$' ), '', $product[ 'product_price' ] );
  40. $total = floatval( $total );
  41. if( isset( $related[ 'quantity' ][ 'value' ] ) && $related[ 'quantity' ][ 'value' ] ){
  42. $total = $total * $related[ 'quantity' ][ 'value' ];
  43. } elseif( $product[ 'product_use_quantity'] && $product[ 'value' ] ){
  44. $total = $total * $product[ 'value' ];
  45. }
  46. if( isset( $related[ 'modifier' ] ) ){
  47. //TODO: Handle multiple modifiers.
  48. }
  49. $data[ 'product_totals' ][] = number_format( $total, 2 );
  50. $data[ 'extra' ][ 'product_fields' ][ $product[ 'id' ] ][ 'product_price' ] = $product[ 'settings' ][ 'product_price' ];
  51. return $data;
  52. }
  53. /**
  54. * Validate
  55. *
  56. * @param $field
  57. * @param $data
  58. * @return array $errors
  59. */
  60. public function validate( $field, $data )
  61. {
  62. $errors = array();
  63. if( isset( $field[ 'product_use_quantity' ] ) && 1 == $field[ 'product_use_quantity' ] ){
  64. // Required check.
  65. if( isset( $field['required'] ) && 1 == $field['required'] && ! trim( $field['value'] ) ){
  66. $errors[] = 'Field is required.';
  67. }
  68. }
  69. return $errors;
  70. }
  71. public function filter_required_setting( $field )
  72. {
  73. if( ! isset( $field[ 'settings' ][ 'product_use_quantity' ] ) || 1 != $field[ 'settings' ][ 'product_use_quantity' ] ) {
  74. $field[ 'settings' ][ 'required' ] = 0;
  75. }
  76. return $field;
  77. }
  78. public function merge_tag_value( $value, $field )
  79. {
  80. // TODO: Replaced this to fix English locales.
  81. // Other locales are still broken and will need to be addressed in refactor.
  82. // $product_price = preg_replace ('/[^\d,\.]/', '', $field[ 'product_price' ] );
  83. $product_price = str_replace( array( ',', '$' ), '', $field[ 'product_price' ] );
  84. $product_quantity = ( isset( $field[ 'product_use_quantity' ] ) && 1 == $field[ 'product_use_quantity' ] ) ? $value : 1;
  85. return number_format( $product_price * $product_quantity, 2 );
  86. }
  87. public function custom_columns( $value, $field, $sub_id )
  88. {
  89. if ( ! $field->get_setting( 'product_use_quantity' ) ) return $value;
  90. if ( 0 == absint( $_REQUEST[ 'form_id' ] ) ) return $value;
  91. $form_id = absint( $_REQUEST[ 'form_id' ] );
  92. /*
  93. * Check to see if we have a stored "price" setting for this field.
  94. * This lets us track what the value was when the user submitted so that total isn't incorrect if the user changes the price after a submission.
  95. */
  96. $sub = Ninja_Forms()->form()->get_sub( $sub_id );
  97. $product_fields = $sub->get_extra_value( 'product_fields' );
  98. if( is_array( $product_fields ) && isset ( $product_fields[ $field->get_id() ] ) ) {
  99. $price = $product_fields[ $field->get_id() ][ 'product_price' ];
  100. } else {
  101. $price = $field->get_setting( 'product_price' );
  102. }
  103. /*
  104. * Get our currency marker setting. First, we check the form, then plugin settings.
  105. */
  106. $currency = Ninja_Forms()->form( $form_id )->get()->get_setting( 'currency' );
  107. if ( empty( $currency ) ) {
  108. /*
  109. * Check our plugin currency.
  110. */
  111. $currency = Ninja_Forms()->get_setting( 'currency' );
  112. }
  113. $currency_symbols = Ninja_Forms::config( 'CurrencySymbol' );
  114. $currency_symbol = html_entity_decode( $currency_symbols[ $currency ] );
  115. // @todo Update to use the locale of the form.
  116. global $wp_locale;
  117. $price = str_replace( array( $wp_locale->number_format[ 'thousands_sep' ], $currency_symbol ), '', $price );
  118. $price = floatval( $price );
  119. $value = intval( $value );
  120. $total = number_format_i18n( $price * $value, 2 );
  121. $output = $currency_symbol . $total . ' ( ' . $value . ' ) ';
  122. return $output;
  123. }
  124. public function admin_form_element( $id, $value )
  125. {
  126. $form_id = get_post_meta( absint( $_GET[ 'post' ] ), '_form_id', true );
  127. $field = Ninja_Forms()->form( $form_id )->get_field( $id );
  128. /*
  129. * Check to see if we have a stored "price" setting for this field.
  130. * This lets us track what the value was when the user submitted so that total isn't incorrect if the user changes the price after a submission.
  131. */
  132. $sub = Ninja_Forms()->form()->get_sub( absint( $_REQUEST[ 'post' ] ) );
  133. $product_fields = $sub->get_extra_value( 'product_fields' );
  134. if( is_array( $product_fields ) && isset ( $product_fields[ $id ] ) ) {
  135. $price = $product_fields[ $id ][ 'product_price' ];
  136. } else {
  137. $price = $field->get_setting( 'product_price' );
  138. }
  139. /*
  140. * Get our currency marker setting. First, we check the form, then plugin settings.
  141. */
  142. $currency = Ninja_Forms()->form( $form_id )->get()->get_setting( 'currency' );
  143. if ( empty( $currency ) ) {
  144. /*
  145. * Check our plugin currency.
  146. */
  147. $currency = Ninja_Forms()->get_setting( 'currency' );
  148. }
  149. $currency_symbols = Ninja_Forms::config( 'CurrencySymbol' );
  150. $currency_symbol = html_entity_decode( $currency_symbols[ $currency ] );
  151. // @todo Update to use the locale of the form.
  152. global $wp_locale;
  153. $price = str_replace( array( $wp_locale->number_format[ 'thousands_sep' ], $currency_symbol ), '', $price );
  154. $price = floatval( $price );
  155. $value = intval( $value );
  156. $total = number_format_i18n( $price * $value, 2 );
  157. $price = number_format_i18n( $price, 2 );
  158. return "Price: <strong>" . $currency_symbol . $price . "</strong> X Quantity: <input name='fields[$id]' type='number' value='" . $value . "'> = " . $currency_symbol . $total;
  159. }
  160. }