Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/ninja-fo.../includes/Fields
File: Product.php
<?php if ( ! defined( 'ABSPATH' ) ) exit;
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Class NF_Fields_Product
[3] Fix | Delete
*/
[4] Fix | Delete
class NF_Fields_Product extends NF_Abstracts_Input
[5] Fix | Delete
{
[6] Fix | Delete
protected $_name = 'product';
[7] Fix | Delete
[8] Fix | Delete
protected $_section = '';
[9] Fix | Delete
[10] Fix | Delete
protected $_icon = 'tag';
[11] Fix | Delete
[12] Fix | Delete
protected $_aliases = array();
[13] Fix | Delete
[14] Fix | Delete
protected $_type = 'product';
[15] Fix | Delete
[16] Fix | Delete
protected $_templates = array( 'product', 'textbox', 'hidden', 'listselect' );
[17] Fix | Delete
[18] Fix | Delete
protected $_test_value = '0';
[19] Fix | Delete
[20] Fix | Delete
protected $processing_fields = array( 'quantity', 'modifier', 'shipping', 'tax', 'total' );
[21] Fix | Delete
[22] Fix | Delete
protected $_settings = array( 'product_use_quantity', 'product_price', 'product_type', 'product_type' );
[23] Fix | Delete
[24] Fix | Delete
protected $_settings_exclude = array( 'input_limit_set', 'disable_input', 'required' );
[25] Fix | Delete
[26] Fix | Delete
public function __construct()
[27] Fix | Delete
{
[28] Fix | Delete
parent::__construct();
[29] Fix | Delete
[30] Fix | Delete
$this->_nicename = esc_html__( 'Product', 'ninja-forms' );
[31] Fix | Delete
[32] Fix | Delete
$this->_settings[ 'product_price' ][ 'width' ] = 'full';
[33] Fix | Delete
[34] Fix | Delete
add_filter( 'ninja_forms_merge_tag_value_product', array( $this, 'merge_tag_value' ), 10, 2 );
[35] Fix | Delete
[36] Fix | Delete
add_filter( 'ninja_forms_custom_columns', array( $this, 'custom_columns' ), 10, 3 );
[37] Fix | Delete
add_filter( 'ninja_forms_merge_tag_calc_value_product', array( $this, 'merge_tag_value' ), 10, 2 );
[38] Fix | Delete
[39] Fix | Delete
add_filter( 'ninja_forms_localize_field_' . $this->_name, array( $this, 'filter_required_setting' ) );
[40] Fix | Delete
add_filter( 'ninja_forms_localize_field_' . $this->_name . '_preview', array( $this, 'filter_required_setting' ) );
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
public function process( $product, $data )
[44] Fix | Delete
{
[45] Fix | Delete
$related = array();
[46] Fix | Delete
[47] Fix | Delete
foreach( $data[ 'fields' ] as $key => $field ){
[48] Fix | Delete
[49] Fix | Delete
[50] Fix | Delete
if( ! isset ( $field[ 'type' ] ) || ! in_array( $field[ 'type' ], $this->processing_fields ) ) continue;
[51] Fix | Delete
[52] Fix | Delete
$type = $field[ 'type' ];
[53] Fix | Delete
[54] Fix | Delete
if( ! isset( $field[ 'product_assignment' ] ) ) continue;
[55] Fix | Delete
[56] Fix | Delete
if( $product[ 'id' ] != $field[ 'product_assignment' ] ) continue;
[57] Fix | Delete
[58] Fix | Delete
$related[ $type ] = &$data[ 'fields' ][ $key ]; // Assign by reference
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
//TODO: Does not work in non-English locales
[62] Fix | Delete
$total = str_replace( array( ',', '$' ), '', $product[ 'product_price' ] );
[63] Fix | Delete
$total = floatval( $total );
[64] Fix | Delete
[65] Fix | Delete
if( isset( $related[ 'quantity' ][ 'value' ] ) && $related[ 'quantity' ][ 'value' ] ){
[66] Fix | Delete
$total = $total * $related[ 'quantity' ][ 'value' ];
[67] Fix | Delete
} elseif( $product[ 'product_use_quantity'] && $product[ 'value' ] ){
[68] Fix | Delete
$total = $total * $product[ 'value' ];
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
if( isset( $related[ 'modifier' ] ) ){
[72] Fix | Delete
//TODO: Handle multiple modifiers.
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
$data[ 'product_totals' ][] = number_format( $total, 2 );
[76] Fix | Delete
$data[ 'extra' ][ 'product_fields' ][ $product[ 'id' ] ][ 'product_price' ] = $product[ 'settings' ][ 'product_price' ];
[77] Fix | Delete
[78] Fix | Delete
return $data;
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Validate
[83] Fix | Delete
*
[84] Fix | Delete
* @param $field
[85] Fix | Delete
* @param $data
[86] Fix | Delete
* @return array $errors
[87] Fix | Delete
*/
[88] Fix | Delete
public function validate( $field, $data )
[89] Fix | Delete
{
[90] Fix | Delete
$errors = array();
[91] Fix | Delete
[92] Fix | Delete
if( isset( $field[ 'product_use_quantity' ] ) && 1 == $field[ 'product_use_quantity' ] ){
[93] Fix | Delete
[94] Fix | Delete
// Required check.
[95] Fix | Delete
if( isset( $field['required'] ) && 1 == $field['required'] && ! trim( $field['value'] ) ){
[96] Fix | Delete
$errors[] = 'Field is required.';
[97] Fix | Delete
}
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
return $errors;
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
public function filter_required_setting( $field )
[104] Fix | Delete
{
[105] Fix | Delete
if( ! isset( $field[ 'settings' ][ 'product_use_quantity' ] ) || 1 != $field[ 'settings' ][ 'product_use_quantity' ] ) {
[106] Fix | Delete
$field[ 'settings' ][ 'required' ] = 0;
[107] Fix | Delete
}
[108] Fix | Delete
return $field;
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
public function merge_tag_value( $value, $field )
[112] Fix | Delete
{
[113] Fix | Delete
// TODO: Replaced this to fix English locales.
[114] Fix | Delete
// Other locales are still broken and will need to be addressed in refactor.
[115] Fix | Delete
// $product_price = preg_replace ('/[^\d,\.]/', '', $field[ 'product_price' ] );
[116] Fix | Delete
$product_price = (float) str_replace( array( ',', $this->getCurrencySymbol() ), '', $field[ 'product_price' ] );
[117] Fix | Delete
[118] Fix | Delete
$product_quantity = ( isset( $field[ 'product_use_quantity' ] ) && 1 == $field[ 'product_use_quantity' ] ) ?(float) $value : 1;
[119] Fix | Delete
[120] Fix | Delete
return number_format( $product_price * $product_quantity, 2 );
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
public function custom_columns( $value, $field, $sub_id )
[124] Fix | Delete
{
[125] Fix | Delete
if ( ! $field->get_setting( 'product_use_quantity' ) ) return $value;
[126] Fix | Delete
if ( 0 == absint( $_REQUEST[ 'form_id' ] ) ) return $value;
[127] Fix | Delete
[128] Fix | Delete
$form_id = absint( $_REQUEST[ 'form_id' ] );
[129] Fix | Delete
[130] Fix | Delete
/*
[131] Fix | Delete
* Check to see if we have a stored "price" setting for this field.
[132] Fix | Delete
* 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.
[133] Fix | Delete
*/
[134] Fix | Delete
$sub = Ninja_Forms()->form()->get_sub( $sub_id );
[135] Fix | Delete
$product_fields = $sub->get_extra_value( 'product_fields' );
[136] Fix | Delete
[137] Fix | Delete
if( is_array( $product_fields ) && isset ( $product_fields[ $field->get_id() ] ) ) {
[138] Fix | Delete
$price = $product_fields[ $field->get_id() ][ 'product_price' ];
[139] Fix | Delete
} else {
[140] Fix | Delete
$price = $field->get_setting( 'product_price' );
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
/*
[144] Fix | Delete
* Get our currency marker setting. First, we check the form, then plugin settings.
[145] Fix | Delete
*/
[146] Fix | Delete
$currency = Ninja_Forms()->form( $form_id )->get()->get_setting( 'currency' );
[147] Fix | Delete
[148] Fix | Delete
if ( empty( $currency ) ) {
[149] Fix | Delete
/*
[150] Fix | Delete
* Check our plugin currency.
[151] Fix | Delete
*/
[152] Fix | Delete
$currency = Ninja_Forms()->get_setting( 'currency' );
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
$currency_symbols = Ninja_Forms::config( 'CurrencySymbol' );
[156] Fix | Delete
$currency_symbol = html_entity_decode( $currency_symbols[ $currency ] );
[157] Fix | Delete
[158] Fix | Delete
// @todo Update to use the locale of the form.
[159] Fix | Delete
global $wp_locale;
[160] Fix | Delete
$price = str_replace( array( $wp_locale->number_format[ 'thousands_sep' ], $currency_symbol ), '', $price );
[161] Fix | Delete
$price = floatval( $price );
[162] Fix | Delete
$value = intval( $value );
[163] Fix | Delete
[164] Fix | Delete
$total = number_format_i18n( $price * $value, 2 );
[165] Fix | Delete
[166] Fix | Delete
$output = $currency_symbol . $total . ' ( ' . $value . ' ) ';
[167] Fix | Delete
return $output;
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
public function admin_form_element( $id, $value )
[171] Fix | Delete
{
[172] Fix | Delete
$form_id = get_post_meta( absint( $_GET[ 'post' ] ), '_form_id', true );
[173] Fix | Delete
[174] Fix | Delete
$field = Ninja_Forms()->form( $form_id )->get_field( $id );
[175] Fix | Delete
[176] Fix | Delete
/*
[177] Fix | Delete
* Check to see if we have a stored "price" setting for this field.
[178] Fix | Delete
* 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.
[179] Fix | Delete
*/
[180] Fix | Delete
$sub = Ninja_Forms()->form()->get_sub( absint( $_REQUEST[ 'post' ] ) );
[181] Fix | Delete
$product_fields = $sub->get_extra_value( 'product_fields' );
[182] Fix | Delete
[183] Fix | Delete
if( is_array( $product_fields ) && isset ( $product_fields[ $id ] ) ) {
[184] Fix | Delete
$price = $product_fields[ $id ][ 'product_price' ];
[185] Fix | Delete
} else {
[186] Fix | Delete
$price = $field->get_setting( 'product_price' );
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/*
[190] Fix | Delete
* Get our currency marker setting. First, we check the form, then plugin settings.
[191] Fix | Delete
*/
[192] Fix | Delete
$currency = Ninja_Forms()->form( $form_id )->get()->get_setting( 'currency' );
[193] Fix | Delete
[194] Fix | Delete
if ( empty( $currency ) ) {
[195] Fix | Delete
/*
[196] Fix | Delete
* Check our plugin currency.
[197] Fix | Delete
*/
[198] Fix | Delete
$currency = Ninja_Forms()->get_setting( 'currency' );
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
$currency_symbols = Ninja_Forms::config( 'CurrencySymbol' );
[202] Fix | Delete
$currency_symbol = html_entity_decode( $currency_symbols[ $currency ] );
[203] Fix | Delete
[204] Fix | Delete
// @todo Update to use the locale of the form.
[205] Fix | Delete
global $wp_locale;
[206] Fix | Delete
$price = str_replace( array( $wp_locale->number_format[ 'thousands_sep' ], $currency_symbol ), '', $price );
[207] Fix | Delete
$price = floatval( $price );
[208] Fix | Delete
$value = intval( $value );
[209] Fix | Delete
[210] Fix | Delete
$total = number_format_i18n( $price * $value, 2 );
[211] Fix | Delete
$price = number_format_i18n( $price, 2 );
[212] Fix | Delete
[213] Fix | Delete
return "Price: <strong>" . $currency_symbol . $price . "</strong> X Quantity: <input name='fields[$id]' type='number' value='" . $value . "'> = " . $currency_symbol . $total;
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
/**
[217] Fix | Delete
* Determine currency symbol
[218] Fix | Delete
*
[219] Fix | Delete
* @return string
[220] Fix | Delete
*/
[221] Fix | Delete
protected function getCurrencySymbol(): string
[222] Fix | Delete
{
[223] Fix | Delete
$currencySymbolLookup = Ninja_Forms::config('CurrencySymbol');
[224] Fix | Delete
$currency = Ninja_Forms()->get_setting('currency');
[225] Fix | Delete
[226] Fix | Delete
if (!is_string($currency)) {
[227] Fix | Delete
return '';
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
$return = isset($currencySymbolLookup[$currency]) ? $currencySymbolLookup[$currency] : '';
[231] Fix | Delete
[232] Fix | Delete
return $return;
[233] Fix | Delete
}
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function