2022-09-27 v.1.6.1
+2022-09-26 v.1.6.0
+v.1.5.5-1.5.7
+2022-08-21 v.1.5.4
+2022-08-16 v.1.5.2, v.1.5.3
+2022-08-05 v.1.5.0
You can use file size [MB]
parameter in formulas.
You can use {field_name:size}
variable in formulas to get uploaded image file size in [MiB]
+ +
+ 10 and the hook must be wp_enqueue_scripts +add_action( 'wp_enqueue_scripts', 'wck_remove_default_price_block', 20); + +// woocommerce_before_add_to_cart_form - is an example +add_action('woocommerce_before_add_to_cart_form', 'wck_add_custom_price_block'); +``` \ No newline at end of file diff --git a/readme.md b/readme.md index f042da3..db77411 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ Tags: woocommerce custom fields, woocommerce product price, woocommerce product fields, woocommerce custom price field, woocommerce personalized product, woocommerce custom product fields, product fields, custom product price, price calculation, price formula Requires at least: 5.0 Tested up to: 6.0.1 -Stable tag: 1.5.7 +Stable tag: 1.6.1 Requires PHP: 5.6 License: GNU GPLv2 Donate link: https://www.paypal.com/donate/?hosted_button_id=5DNZK72H5YCBY @@ -149,6 +149,22 @@ You can use `acf('field_name')` function to get ACF field value in custom price Full documentation at: [www.wckalkulator.com](https://wckalkulator.com) == Changelog == +2022-09-27 v.1.6.1 +- fixed issue with color picker + +2022-09-26 v.1.6.0 +- image file upload bug fixes +- conditional visibility works with static fields (html, paragraph) +- added support for {image:size} in HTML field's content (for example: {={image:size}} MB) +- display calculated product price in the cart widget (cart popup) +- new parameter: product_is_on_sale to use in formula +- added parameters to js dynamic formula in HTML field +- added placeholder for select and dropdown fields +- bug fixed: incorrect value of the image swatch in the cart + +v.1.5.5-1.5.7 +- bug fixes + 2022-08-21 v.1.5.4 - add option to show Price Block before or after "Add to cart" button - bug fixes diff --git a/src/Ajax.php b/src/Ajax.php index b9e8e72..e613ecf 100644 --- a/src/Ajax.php +++ b/src/Ajax.php @@ -58,6 +58,8 @@ public static function enqueue_scripts() { $fieldset = FieldsetProduct::getInstance(); if ($fieldset->has_fieldset('current')/* && $fieldset->has_expression('current')*/) { + $fieldset->init(); + $formula_parameters = $fieldset->set_additional_input_variables(true); wp_enqueue_script( 'wck-ajax-script', @@ -76,7 +78,8 @@ public static function enqueue_scripts() '_wck_i18n_required' => __('You should check at least one option.', 'wc-kalkulator'), '_wck_i18n_maxfilesize' => __('This file is too big!', 'wc-kalkulator'), 'form' => Settings::get('form_css_selector'), - '_wck_visibility_rules' => $fieldset->visibility_rules() + '_wck_visibility_rules' => $fieldset->visibility_rules(), + '_wck_additional_parameters' => ($formula_parameters) ) ) . ';' ); diff --git a/src/Cron.php b/src/Cron.php index ccd63e4..3615c85 100644 --- a/src/Cron.php +++ b/src/Cron.php @@ -115,6 +115,10 @@ public static function delete_customer_uploads() //$upload_path = wp_upload_dir()['basedir'] . $customer_dir; $upload_path = Settings::get('upload_customer_data_dir'); + if(!file_exists($upload_path)) { + return; + } + $ext = array('jpg', 'jpeg', 'png', 'gif'); $dir = new \RecursiveDirectoryIterator($upload_path); $files = new \RecursiveIteratorIterator($dir); diff --git a/src/Fields/AbstractField.php b/src/Fields/AbstractField.php index 591043d..8df5951 100644 --- a/src/Fields/AbstractField.php +++ b/src/Fields/AbstractField.php @@ -9,256 +9,242 @@ * * @package WCKalkulator */ -abstract class AbstractField -{ - protected $groups; - - public final function __construct() - { - $required = array("parameters", "data", "default_data", "type", "admin_title", "use_expression", "group"); - - foreach ($required as $property) { - if (!property_exists($this, $property)) - throw new \LogicException(get_class($this) . ' must have a "' . $property . '" property.'); - } - - $this->groups = array( - 'input' => __('Input Fields', 'wc-kalkulator'), - 'select' => __('Select Fields', 'wc-kalkulator'), - 'picker' => __('Picker Fields', 'wc-kalkulator'), - 'upload' => __('Upload Fields', 'wc-kalkulator'), - 'static' => __('Static Fields', 'wc-kalkulator'), - 'special' => __('Special Fields', 'wc-kalkulator'), - 'other' => __('Other Fields', 'wc-kalkulator') - ); - - if (!array_key_exists($this->group, $this->groups)) { - throw new \LogicException(get_class($this) . ' has unknown group property.'); - } - - } - - /** - * @param $data - */ - public function fromArray($data) - { - $this->data = $data; - $this->default_data(); - } - - protected function default_data() - { - $data_keys = array_keys($this->data); - foreach ($this->default_data as $param => $default_value) { - if (!in_array($param, $data_keys)) { - $this->data[$param] = $default_value; - } - } - } - - /** - * @return array - */ - public function prepared_data() - { - return array( - 'type' => $this->type(), - 'title' => $this->data("title"), - 'before_title' => $this->data("before_title"), - 'after_title' => $this->data("after_title"), - 'hint' => $this->html_hint(), - 'name' => "wck[" . $this->data("name") . "]", - 'id' => 'wck_' . $this->data("name"), - 'css_class' => $this->data("css_class"), - 'required' => ($this->is_required() || $this->is_required_when_visible() ? ' required' : ''), // add "required" attribute - 'is_required' => $this->is_required() ? '1' : '0', // is always required - 'show_required_asterisk' => $this->is_required() || $this->is_required_when_visible() - ); - } - - /** - * @return string - */ - public function type() - { - return $this->type; - } - - /** - * @param string $key - * @return array|string|null - */ - public function data($key = '') - { - if ($key === '') { - return $this->data; - } - - if (!empty($this->data[$key])) { - return $this->data[$key]; - } - - return; - } - - /** - * @return string - */ - public function html_hint() - { - if ($this->data("hint") != '') - return ''; - return ''; - } - - /** - * Returns true if the field is always required - * - * @return mixed - */ - public function is_required() - { - return !($this->group() === 'static' || $this->type() === 'formula') && $this->data["required"] === '1'; - } - - /** - * Returns true if the field is required when visible - * - * @return mixed - * @since 1.5.0 - */ - public function is_required_when_visible() - { - return $this->data["required"] === '2'; - } - - /** - * @param $json - */ - public function fromJSON($json) - { - $this->data = json_decode($json, true); - $this->default_data(); - } - - /** - * @return false|string - */ - public function toJSON() - { - return wp_json_encode($this->data); - } - - /** - * @param $type - * @return bool - */ - public function is_type($type) - { - return is_array($type) ? in_array($this->type, $type) : $this->type === $type; - } - - /** - * @param string $param - * @return string - */ - public function render_for_admin($param = '') - { - return View::render('fields/admin', array( - 'admin_fields' => $this->admin_fields(), - 'title' => $this->admin_title(), - 'type' => $this->type(), - 'use_expression' => $this->use_expression() ? 'true' : 'false', - 'group' => $this->group(), - 'show_title' => $this->show_title() - )); - } - - abstract public function admin_fields($param = ''); - - abstract public function order_item_value($value); - - /** - * Checks if we need to hide "Title" field on admin page - * - * @return bool - */ - public function show_title() - { - /** - * If property exists return its value - */ - if (property_exists($this, 'show_title')) { - return $this->show_title; - } - - /** - * If field's group is not static, return true (Title field should be visible) - */ - if ($this->group() !== 'static') { - return true; - } - - /** - * In other case (static group) return false (Title field should be hidden) - */ - return false; - } - - /** - * @return string - */ - public function admin_title() - { - return $this->admin_title; - } - - /** - * @return mixed - */ - public function use_expression() - { - return $this->use_expression; - } - - abstract public function render_for_product($param = ''); - - abstract public function render_for_cart($param = ''); - - abstract public function validate($value); - - /** - * @return mixed - */ - public function group() - { - return $this->group; - } - - public function group_title() - { - return $this->groups[$this->group]; - } - - /** - * @return mixed - */ - public function icon() - { - return $this->icon; - } - - public function enqueue_scripts() - { - } - - /** - * @return array - */ - public function localize_script() - { - return array(); - } +abstract class AbstractField { + protected $groups; + + public final function __construct() { + $required = array( "parameters", "data", "default_data", "type", "admin_title", "use_expression", "group" ); + + foreach ( $required as $property ) { + if ( ! property_exists( $this, $property ) ) { + throw new \LogicException( get_class( $this ) . ' must have a "' . $property . '" property.' ); + } + } + + $this->groups = array( + 'input' => __( 'Input Fields', 'wc-kalkulator' ), + 'select' => __( 'Select Fields', 'wc-kalkulator' ), + 'picker' => __( 'Picker Fields', 'wc-kalkulator' ), + 'upload' => __( 'Upload Fields', 'wc-kalkulator' ), + 'static' => __( 'Static Fields', 'wc-kalkulator' ), + 'special' => __( 'Special Fields', 'wc-kalkulator' ), + 'other' => __( 'Other Fields', 'wc-kalkulator' ) + ); + + if ( ! array_key_exists( $this->group, $this->groups ) ) { + throw new \LogicException( get_class( $this ) . ' has unknown group property.' ); + } + + } + + /** + * @param $data + */ + public function fromArray( $data ) { + $this->data = $data; + $this->default_data(); + } + + protected function default_data() { + $data_keys = array_keys( $this->data ); + foreach ( $this->default_data as $param => $default_value ) { + if ( ! in_array( $param, $data_keys ) ) { + $this->data[ $param ] = $default_value; + } + } + } + + /** + * @return array + */ + public function prepared_data() { + return array( + 'type' => $this->type(), + 'title' => $this->data( "title" ), + 'before_title' => $this->data( "before_title" ), + 'after_title' => $this->data( "after_title" ), + 'hint' => $this->html_hint(), + 'name' => "wck[" . $this->data( "name" ) . "]", + 'id' => 'wck_' . $this->data( "name" ), + 'css_class' => $this->data( "css_class" ), + 'required' => ( $this->is_required() || $this->is_required_when_visible() ? ' required' : '' ), + // add "required" attribute + 'is_required' => $this->is_required() ? '1' : '0', + // is always required + 'show_required_asterisk' => $this->is_required() || $this->is_required_when_visible() + ); + } + + /** + * @return string + */ + public function type() { + return $this->type; + } + + /** + * @param string $key + * + * @return array|string|null + */ + public function data( $key = '' ) { + if ( $key === '' ) { + return $this->data; + } + + if ( ! empty( $this->data[ $key ] ) ) { + return $this->data[ $key ]; + } + + return; + } + + /** + * @return string + */ + public function html_hint() { + if ( $this->data( "hint" ) != '' ) { + return ''; + } + + return ''; + } + + /** + * Returns true if the field is always required + * + * @return mixed + */ + public function is_required() { + return ! ( $this->group() === 'static' || $this->type() === 'formula' ) && $this->data["required"] === '1'; + } + + /** + * Returns true if the field is required when visible + * + * @return mixed + * @since 1.5.0 + */ + public function is_required_when_visible() { + return $this->data["required"] === '2'; + } + + /** + * @param $json + */ + public function fromJSON( $json ) { + $this->data = json_decode( $json, true ); + $this->default_data(); + } + + /** + * @return false|string + */ + public function toJSON() { + return wp_json_encode( $this->data ); + } + + /** + * @param $type + * + * @return bool + */ + public function is_type( $type ) { + return is_array( $type ) ? in_array( $this->type, $type ) : $this->type === $type; + } + + /** + * @param string $param + * + * @return string + */ + public function render_for_admin( $param = '' ) { + return View::render( 'fields/admin', array( + 'admin_fields' => $this->admin_fields(), + 'title' => $this->admin_title(), + 'type' => $this->type(), + 'use_expression' => $this->use_expression() ? 'true' : 'false', + 'group' => $this->group(), + 'show_title' => $this->show_title() + ) ); + } + + abstract public function admin_fields( $param = '' ); + + abstract public function order_item_value( $value ); + + /** + * Checks if we need to hide "Title" field on admin page + * + * @return bool + */ + public function show_title() { + /** + * If property exists return its value + */ + if ( property_exists( $this, 'show_title' ) ) { + return $this->show_title; + } + + /** + * If field's group is not static, return true (Title field should be visible) + */ + if ( $this->group() !== 'static' ) { + return true; + } + + /** + * In other case (static group) return false (Title field should be hidden) + */ + return false; + } + + /** + * @return string + */ + public function admin_title() { + return $this->admin_title; + } + + /** + * @return mixed + */ + public function use_expression() { + return $this->use_expression; + } + + abstract public function render_for_product( $param = '' ); + + abstract public function render_for_cart( $param = '' ); + + abstract public function validate( $value ); + + /** + * @return mixed + */ + public function group() { + return $this->group; + } + + public function group_title() { + return $this->groups[ $this->group ]; + } + + /** + * @return mixed + */ + public function icon() { + return $this->icon; + } + + public function enqueue_scripts() { + } + + /** + * @return array + */ + public function localize_script() { + return array(); + } } \ No newline at end of file diff --git a/src/Fields/AttachmentField.php b/src/Fields/AttachmentField.php index 83aceef..b6e75d0 100644 --- a/src/Fields/AttachmentField.php +++ b/src/Fields/AttachmentField.php @@ -8,59 +8,64 @@ * Class LinkField * @package WCKalkulator */ -class AttachmentField extends HtmlField -{ - protected $parameters = array("type", "name", "content", "title", "target"); - protected $default_data = array("required" => false, "content" => "", "target" => "_blank"); - protected $data; - protected $type = "attachment"; - protected $admin_title; - protected $use_expression = false; - protected $group = "static"; - protected $show_title = true; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("File Attachment", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - $args = $this->prepared_data(); - $args['content'] = $this->data('content'); - return View::render('fields/front/' . $this->type, $args); - } - - /** - * No need to show hidden field in the user's cart - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - return; - } - - /** - * No need to validate hidden static field - * @param $value - * @return bool - */ - public function validate($value) - { - return true; - - } - +class AttachmentField extends HtmlField { + protected $parameters = array( "type", "name", "content", "title", "target" ); + protected $default_data = array( "required" => false, "content" => "", "target" => "_blank" ); + protected $data; + protected $type = "attachment"; + protected $admin_title; + protected $use_expression = false; + protected $group = "static"; + protected $show_title = true; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "File Attachment", "wc-kalkulator" ); + + return View::render( 'fields/admin/' . $this->type ); + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + $args = $this->prepared_data(); + $args['content'] = $this->data( 'content' ); + + return View::render( 'fields/front/' . $this->type, $args ); + } + + /** + * No need to show hidden field in the user's cart + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + return; + } + + /** + * No need to validate hidden static field + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + return true; + + } + } \ No newline at end of file diff --git a/src/Fields/CheckboxField.php b/src/Fields/CheckboxField.php index 3bae831..e71a9da 100644 --- a/src/Fields/CheckboxField.php +++ b/src/Fields/CheckboxField.php @@ -8,76 +8,82 @@ * Class CheckboxField * @package WCKalkulator */ -class CheckboxField extends AbstractField -{ - protected $parameters = array("type", "name", "title", "hint", "css_class", "required", "default_state", "price"); - protected $default_data = array("css_class" => "", "required" => false, "default_state" => "", "hint" => ""); - protected $data; - protected $type = "checkbox"; - protected $admin_title; - protected $use_expression = false; - protected $group = "select"; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Checkbox", "wc-kalkulator"); - return View::render('fields/admin/checkbox'); - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = 0) - { - $value = (int)$value; - $args = $this->prepared_data(); - $args['value'] = $value === 1; - $args['default_state'] = $this->data["default_state"]; - $args['checked'] = ($value === 1) || ($value === 0 && (int)$this->data["default_state"] === 1); - return View::render('fields/front/checkbox', $args); - } - - /** - * Output HTML for User's cart nad order meta - * @param $value - * @return string - */ - public function render_for_cart($value = 0) - { - $checked = (int)$value === 1; - return View::render('fields/cart', array( - 'title' => $this->data['title'], - 'value' => ($checked ? __('yes', 'wc-kalkulator') : __('no', 'wc-kalkulator')) - )); - } - - /** - * Run all validation tests - * @param $value - * @return bool - */ - public function validate($value) - { - return empty($value) || (int)$value === 1; - } - - /** - * Display value of the field in order line item at backend - * - * @param $value - * @return string - * @since 1.2.0 - */ - public function order_item_value($value) - { - return (int)$value === 1 ? __('yes', 'wc-kalkulator') : __('no', 'wc-kalkulator'); - } - +class CheckboxField extends AbstractField { + protected $parameters = array( "type", "name", "title", "hint", "css_class", "required", "default_state", "price" ); + protected $default_data = array( "css_class" => "", "required" => false, "default_state" => "", "hint" => "" ); + protected $data; + protected $type = "checkbox"; + protected $admin_title; + protected $use_expression = false; + protected $group = "select"; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Checkbox", "wc-kalkulator" ); + + return View::render( 'fields/admin/checkbox' ); + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = 0 ) { + $value = (int) $value; + $args = $this->prepared_data(); + $args['value'] = $value === 1; + $args['default_state'] = $this->data["default_state"]; + $args['checked'] = ( $value === 1 ) || ( $value === 0 && (int) $this->data["default_state"] === 1 ); + + return View::render( 'fields/front/checkbox', $args ); + } + + /** + * Output HTML for User's cart nad order meta + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = 0 ) { + $checked = (int) $value === 1; + + return View::render( 'fields/cart', array( + 'title' => $this->data['title'], + 'value' => ( $checked ? __( 'yes', 'wc-kalkulator' ) : __( 'no', 'wc-kalkulator' ) ) + ) ); + } + + /** + * Run all validation tests + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + return empty( $value ) || (int) $value === 1; + } + + /** + * Display value of the field in order line item at backend + * + * @param $value + * + * @return string + * @since 1.2.0 + */ + public function order_item_value( $value ) { + return (int) $value === 1 ? __( 'yes', 'wc-kalkulator' ) : __( 'no', 'wc-kalkulator' ); + } + } \ No newline at end of file diff --git a/src/Fields/CheckboxgroupField.php b/src/Fields/CheckboxgroupField.php index 96b8c8e..9d41a74 100644 --- a/src/Fields/CheckboxgroupField.php +++ b/src/Fields/CheckboxgroupField.php @@ -8,108 +8,119 @@ * Class CheckboxgroupField * @package WCKalkulator */ -class CheckboxgroupField extends SelectField -{ - protected $type = "checkboxgroup"; - protected $default_data = array("css_class" => "", "required" => false, "default_value" => "", "hint" => "", "select_limit" => 0); - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Multi Checkbox", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } - - /** - * Output HTML for User's cart nad order meta - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - return View::render('fields/cart', array( - 'title' => $this->data['title'], - 'value' => $this->order_item_value($value) - )); - } - - /** - * Output HTML for product page - * @param $selected_name - * @return string - */ - public function render_for_product($selected_name = "") - { - if ($selected_name === "") { - $selected_name = $this->data["default_value"]; - } - $args = $this->prepared_data(); - $args['value'] = $selected_name; - $args['options_name'] = $this->data['options_name']; - $args['options_title'] = $this->data['options_title']; - $args['select_limit'] = absint($this->data('select_limit')); - - return View::render('fields/front/' . $this->type, $args); - } - - /** - * Run validation tests - * @param $value - * @return bool - */ - public function validate($value) - { - if (!$this->is_required() && empty($value)) { - return true; - } - - if (is_array($value)) { - - if (((int)$this->data('select_limit') > 0 && count($value) > $this->data('select_limit')) || ($this->is_required() && count($value) === 0)) { - return false; - } - - foreach ($value as $val) { - if (!in_array($val, $this->data["options_name"])) { - return false; - } - return true; - } - } - - /** - * if $value is not an array - */ - - if ($this->is_required() && empty($value)) { - return false; - } - - return in_array($value, $this->data["options_name"]); - } - - - /** - * Display value of the field in order line item at backend - * - * @param $value - * @return string - * @since 1.2.0 - */ - public function order_item_value($value) - { - if (is_array($value)) { - foreach ($value as $key => $val) { - $value[$key] = $this->get_option_title($val); - } - $value = join(", ", $value); - } else { - $value = $this->get_option_title($value); - } - return $value; - } +class CheckboxgroupField extends SelectField { + protected $type = "checkboxgroup"; + protected $default_data = array( "css_class" => "", + "required" => false, + "default_value" => "", + "hint" => "", + "select_limit" => 0 + ); + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Multi Checkbox", "wc-kalkulator" ); + + return View::render( 'fields/admin/' . $this->type ); + } + + /** + * Output HTML for User's cart nad order meta + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + return View::render( 'fields/cart', array( + 'title' => $this->data['title'], + 'value' => $this->order_item_value( $value ) + ) ); + } + + /** + * Output HTML for product page + * + * @param $selected_name + * + * @return string + */ + public function render_for_product( $selected_name = "" ) { + if ( $selected_name === "" ) { + $selected_name = $this->data["default_value"]; + } + $args = $this->prepared_data(); + $args['value'] = $selected_name; + $args['options_name'] = $this->data['options_name']; + $args['options_title'] = $this->data['options_title']; + $args['select_limit'] = absint( $this->data( 'select_limit' ) ); + + return View::render( 'fields/front/' . $this->type, $args ); + } + + /** + * Run validation tests + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + if ( ! $this->is_required() && empty( $value ) ) { + return true; + } + + if ( is_array( $value ) ) { + + if ( ( (int) $this->data( 'select_limit' ) > 0 && count( $value ) > $this->data( 'select_limit' ) ) || ( $this->is_required() && count( $value ) === 0 ) ) { + return false; + } + + foreach ( $value as $val ) { + if ( ! in_array( $val, $this->data["options_name"] ) ) { + return false; + } + + return true; + } + } + + /** + * if $value is not an array + */ + + if ( $this->is_required() && empty( $value ) ) { + return false; + } + + return in_array( $value, $this->data["options_name"] ); + } + + + /** + * Display value of the field in order line item at backend + * + * @param $value + * + * @return string + * @since 1.2.0 + */ + public function order_item_value( $value ) { + if ( is_array( $value ) ) { + foreach ( $value as $key => $val ) { + $value[ $key ] = $this->get_option_title( $val ); + } + $value = join( ", ", $value ); + } else { + $value = $this->get_option_title( $value ); + } + + return $value; + } } \ No newline at end of file diff --git a/src/Fields/ColorpickerField.php b/src/Fields/ColorpickerField.php index e63c2f9..b3e54dd 100644 --- a/src/Fields/ColorpickerField.php +++ b/src/Fields/ColorpickerField.php @@ -9,80 +9,86 @@ * Class ColorpickerField * @package WCKalkulator */ -class ColorpickerField extends TextField -{ - protected $type = "colorpicker"; - protected $group = "picker"; - - /** - * Called in enqueue_scripts action - */ - public function enqueue_scripts() - { - wp_enqueue_style('wp-color-picker'); - wp_enqueue_script( - 'iris', - admin_url('js/iris.min.js'), - array('jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch'), - false, - 1 - ); - wp_enqueue_script( - 'wp-color-picker', - admin_url('js/color-picker.min.js'), - array('iris', 'wp-i18n'), - false, - 1 - ); - wp_enqueue_script( - 'wck-color-picker', - Plugin::url() . '/assets/js/colorpicker.min.js', - array('wp-color-picker'), - Plugin::VERSION, - 1 - ); - } - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Color Picker", "wc-kalkulator"); - return View::render('fields/admin/colorpicker'); - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - $args = $args = $this->prepared_data(); - $args['value'] = $value; - return View::render('fields/front/colorpicker', $args); - } - - /** - * Run validation tests - * @param $value - * @return bool - */ - public function validate($value) - { - if(!$this->is_required() && empty($value)) { - return true; - } - if (strlen($value) === 7) { - if ($value[0] === '#') { - $hex = substr($value, 1); - return ctype_xdigit($hex); - } - } - return false; - } - + +class ColorpickerField extends TextField { + protected $type = "colorpicker"; + protected $group = "picker"; + + /** + * Called in enqueue_scripts action + */ + public function enqueue_scripts() { + wp_enqueue_style( 'wp-color-picker' ); + wp_enqueue_script( + 'iris', + admin_url( 'js/iris.min.js' ), + array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), + false, + 1 + ); + wp_enqueue_script( + 'wp-color-picker', + admin_url( 'js/color-picker.min.js' ), + array( 'iris', 'wp-i18n' ), + false, + 1 + ); + wp_enqueue_script( + 'wck-color-picker', + Plugin::url() . '/assets/js/colorpicker.min.js', + array( 'wp-color-picker' ), + Plugin::VERSION, + 1 + ); + } + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Color Picker", "wc-kalkulator" ); + + return View::render( 'fields/admin/colorpicker' ); + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + $args = $args = $this->prepared_data(); + $args['value'] = $value; + + return View::render( 'fields/front/colorpicker', $args ); + } + + /** + * Run validation tests + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + if ( ! $this->is_required() && empty( $value ) ) { + return true; + } + if ( strlen( $value ) === 7 ) { + if ( $value[0] === '#' ) { + $hex = substr( $value, 1 ); + + return ctype_xdigit( $hex ); + } + } + + return false; + } + } \ No newline at end of file diff --git a/src/Fields/ColorswatchesField.php b/src/Fields/ColorswatchesField.php index 6044bd1..408a8a7 100644 --- a/src/Fields/ColorswatchesField.php +++ b/src/Fields/ColorswatchesField.php @@ -8,68 +8,89 @@ * Class ColorswatchesField * @package WCKalkulator */ -class ColorswatchesField extends SelectField -{ - protected $parameters = array("type", "name", "title", "hint", "options_name", "options_title", "options_image", "css_class", "required", "default_value"); - protected $default_data = array("css_class" => "", "required" => false, "default_value" => "", "hint" => "", "image_size" => 40); - protected $type = "colorswatches"; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Color Swatches", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } - - /** - * Output HTML for product page - * @param $selected_name - * @return string - */ - public function render_for_product($selected_name = "") - { - if ($selected_name === "") { - $selected_name = $this->data["default_value"]; - } - $args = $this->prepared_data(); - $args['value'] = $selected_name; - $args['options_name'] = $this->data['options_name']; - $args['options_title'] = $this->data['options_title']; - $args['size'] = $this->data('image_size'); - - return View::render('fields/front/' . $this->type, $args); - } - - /** - * Output HTML for User's cart nad order meta - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - $value = $this->get_option_title($value); - - return View::render('fields/cart', array( - 'title' => $this->data['title'], - 'colorswatch' => $value - )); - } - - /** - * Display value of the field in order line item at backend - * - * @param $value - * @return string - * @since 1.2.0 - */ - public function order_item_value($value) - { - $color = $this->get_option_title($value); - return '' . $color . ''; - } +class ColorswatchesField extends SelectField { + protected $parameters = array( + "type", + "name", + "title", + "hint", + "options_name", + "options_title", + "options_image", + "css_class", + "required", + "default_value" + ); + protected $default_data = array( + "css_class" => "", + "required" => false, + "default_value" => "", + "hint" => "", + "image_size" => 40 + ); + protected $type = "colorswatches"; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Color Swatches", "wc-kalkulator" ); + + return View::render( 'fields/admin/' . $this->type ); + } + + /** + * Output HTML for product page + * + * @param $selected_name + * + * @return string + */ + public function render_for_product( $selected_name = "" ) { + if ( $selected_name === "" ) { + $selected_name = $this->data["default_value"]; + } + $args = $this->prepared_data(); + $args['value'] = $selected_name; + $args['options_name'] = $this->data['options_name']; + $args['options_title'] = $this->data['options_title']; + $args['size'] = $this->data( 'image_size' ); + + return View::render( 'fields/front/' . $this->type, $args ); + } + + /** + * Output HTML for User's cart nad order meta + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + $value = $this->get_option_title( $value ); + + return View::render( 'fields/cart', array( + 'title' => $this->data['title'], + 'colorswatch' => $value + ) ); + } + + /** + * Display value of the field in order line item at backend + * + * @param $value + * + * @return string + * @since 1.2.0 + */ + public function order_item_value( $value ) { + $color = $this->get_option_title( $value ); + + return '' . $color . ''; + } } \ No newline at end of file diff --git a/src/Fields/DatepickerField.php b/src/Fields/DatepickerField.php index 848476b..ea0f9d0 100644 --- a/src/Fields/DatepickerField.php +++ b/src/Fields/DatepickerField.php @@ -9,123 +9,141 @@ * Class DatepickerField * @package WCKalkulator */ -class DatepickerField extends TextField -{ - protected $parameters = array("type", "name", "title", "hint", "css_class", "required", "disallow_past_date", "price"); - protected $default_data = array("css_class" => "", "required" => false, "disallow_past_date" => false, "hint" => ""); - protected $type = "datepicker"; - protected $group = "picker"; - /** - * Called in enqueue_scripts action - */ - public function enqueue_scripts() - { - wp_enqueue_style( - 'wck-jquery-ui-css', - Plugin::url() . '/assets/css/jquery-ui.min.css' - ); - wp_enqueue_script('jquery-ui-datepicker'); - wp_enqueue_script( - 'wck-date-picker', - Plugin::url() . '/assets/js/datepicker.min.js', - array('jquery-ui-datepicker'), - Plugin::VERSION, - 1 - ); - } - - /** - * @return array - */ - public function localize_script() - { - $options = array( - 'dateFormat' => 'yy-mm-dd', - 'minDate' => $this->data('disallow_past_date') ? '0' : null - ); - - return array( - 'script' => 'wck-date-picker', - 'field_name' => $this->data("name"), - 'options' => $options - ); - } - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Date Picker", "wc-kalkulator"); - return View::render('fields/admin/datepicker'); - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - $args = $this->prepared_data(); - $args['value'] = $value; - $args['disallow_past_date'] = $this->data["disallow_past_date"]; - - return View::render('fields/front/datepicker', $args); - } - - /** - * Run validation tests - * @param $value - * @return bool - */ - public function validate($value) - { - if (!$this->is_required() && empty($value)) { - return true; - } - //yyyy-mm-dd - $is_valid = false; - $date_arr = explode('-', $value); - if (count($date_arr) === 3) { - $year = $date_arr[0]; - $month = $date_arr[1]; - $day = $date_arr[2]; - $is_valid = checkdate($month, $day, $year); - } - if (!$is_valid) { - error_log($this->data("name") . " - date is incorrect " . $value); - } - - if ($is_valid && $this->data("disallow_past_date")) { - try { - $date = new \DateTime($value); - $date->setTime(0, 0, 0); - $now = new \DateTime(); - $now->setTime(0, 0, 0); - } catch(\Exception $e) { - error_log($this->data("name") . " - DateTime exception"); - return false; - } - $is_valid = $date >= $now; - if (!$is_valid) { - error_log($this->data("name") . " - past date is not allowed"); - } - } - return $is_valid; - } - - /** - * Display value of the field in order line item at backend - * - * @param $value - * @return string - * @since 1.2.0 - */ - public function order_item_value($value) - { - return $this->get_option_title($value); - } +class DatepickerField extends TextField { + protected $parameters = array( + "type", + "name", + "title", + "hint", + "css_class", + "required", + "disallow_past_date", + "price" + ); + protected $default_data = array( + "css_class" => "", + "required" => false, + "disallow_past_date" => false, + "hint" => "" + ); + protected $type = "datepicker"; + protected $group = "picker"; + + /** + * Called in enqueue_scripts action + */ + public function enqueue_scripts() { + wp_enqueue_style( + 'wck-jquery-ui-css', + Plugin::url() . '/assets/css/jquery-ui.min.css' + ); + wp_enqueue_script( 'jquery-ui-datepicker' ); + wp_enqueue_script( + 'wck-date-picker', + Plugin::url() . '/assets/js/datepicker.min.js', + array( 'jquery-ui-datepicker' ), + Plugin::VERSION, + 1 + ); + } + + /** + * @return array + */ + public function localize_script() { + $options = array( + 'dateFormat' => 'yy-mm-dd', + 'minDate' => $this->data( 'disallow_past_date' ) ? '0' : null + ); + + return array( + 'script' => 'wck-date-picker', + 'field_name' => $this->data( "name" ), + 'options' => $options + ); + } + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Date Picker", "wc-kalkulator" ); + + return View::render( 'fields/admin/datepicker' ); + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + $args = $this->prepared_data(); + $args['value'] = $value; + $args['disallow_past_date'] = $this->data["disallow_past_date"]; + + return View::render( 'fields/front/datepicker', $args ); + } + + /** + * Run validation tests + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + if ( ! $this->is_required() && empty( $value ) ) { + return true; + } + //yyyy-mm-dd + $is_valid = false; + $date_arr = explode( '-', $value ); + if ( count( $date_arr ) === 3 ) { + $year = $date_arr[0]; + $month = $date_arr[1]; + $day = $date_arr[2]; + $is_valid = checkdate( $month, $day, $year ); + } + if ( ! $is_valid ) { + error_log( $this->data( "name" ) . " - date is incorrect " . $value ); + } + + if ( $is_valid && $this->data( "disallow_past_date" ) ) { + try { + $date = new \DateTime( $value ); + $date->setTime( 0, 0, 0 ); + $now = new \DateTime(); + $now->setTime( 0, 0, 0 ); + } catch ( \Exception $e ) { + error_log( $this->data( "name" ) . " - DateTime exception" ); + + return false; + } + $is_valid = $date >= $now; + if ( ! $is_valid ) { + error_log( $this->data( "name" ) . " - past date is not allowed" ); + } + } + + return $is_valid; + } + + /** + * Display value of the field in order line item at backend + * + * @param $value + * + * @return string + * @since 1.2.0 + */ + public function order_item_value( $value ) { + return $this->get_option_title( $value ); + } } \ No newline at end of file diff --git a/src/Fields/DropdownField.php b/src/Fields/DropdownField.php index 1fcee3b..234b9d9 100644 --- a/src/Fields/DropdownField.php +++ b/src/Fields/DropdownField.php @@ -8,81 +8,96 @@ * Class DropdownField * @package WCKalkulator */ -class DropdownField extends AbstractField -{ - protected $parameters = array("type", "name", "title", "hint", "options_title", "css_class", "required", "default_value", "price"); - protected $default_data = array("css_class" => "", "required" => false, "default_value" => "", "hint" => ""); - protected $data; - protected $type = "dropdown"; - protected $admin_title; - protected $use_expression = false; - protected $group = "select"; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Dropdown", "wc-kalkulator"); - return View::render('fields/admin/dropdown'); - } - - /** - * Output HTML for product page - * @param $selected_name - * @return string - */ - public function render_for_product($selected_name = "") - { - if ($selected_name === "") { - $selected_name = $this->data["default_value"]; - } - $args = $this->prepared_data(); - $args['value'] = $selected_name; - $args['options_title'] = $this->data['options_title']; - - return View::render('fields/front/dropdown', $args); - - } - - /** - * Output HTML for User's cart nad order meta - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - return View::render('fields/cart', array( - 'title' => $this->data['title'], - 'value' => $value - )); - } - - /** - * Run validation tests - * @param $value - * @return bool - */ - public function validate($value) - { - if (!$this->is_required() && empty($value)) { - return true; - } - return in_array($value, $this->data["options_title"]); - } - - /** - * Display value of the field in order line item at backend - * - * @param $value - * @return string - * @since 1.2.0 - */ - public function order_item_value($value) - { - return $value; - } - +class DropdownField extends AbstractField { + protected $parameters = array( + "type", + "name", + "title", + "hint", + "options_title", + "css_class", + "required", + "default_value", + "price" + ); + protected $default_data = array( "css_class" => "", "required" => false, "default_value" => "", "hint" => "" ); + protected $data; + protected $type = "dropdown"; + protected $admin_title; + protected $use_expression = false; + protected $group = "select"; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Dropdown", "wc-kalkulator" ); + + return View::render( 'fields/admin/dropdown' ); + } + + /** + * Output HTML for product page + * + * @param $selected_name + * + * @return string + */ + public function render_for_product( $selected_name = "" ) { + if ( $selected_name === "" ) { + $selected_name = $this->data["default_value"]; + } + $args = $this->prepared_data(); + $args['value'] = $selected_name; + $args['options_title'] = $this->data['options_title']; + + return View::render( 'fields/front/dropdown', $args ); + + } + + /** + * Output HTML for User's cart nad order meta + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + return View::render( 'fields/cart', array( + 'title' => $this->data['title'], + 'value' => $value + ) ); + } + + /** + * Run validation tests + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + if ( ! $this->is_required() && empty( $value ) ) { + return true; + } + + return in_array( $value, $this->data["options_title"] ); + } + + /** + * Display value of the field in order line item at backend + * + * @param $value + * + * @return string + * @since 1.2.0 + */ + public function order_item_value( $value ) { + return $value; + } + } \ No newline at end of file diff --git a/src/Fields/EmailField.php b/src/Fields/EmailField.php index 6820553..1ff2582 100644 --- a/src/Fields/EmailField.php +++ b/src/Fields/EmailField.php @@ -8,48 +8,50 @@ * Class EmailField * @package WCKalkulator */ -class EmailField extends TextField -{ - protected $type = "email"; - protected $group = "input"; - - /** - * @param string $value - * @return string - */ - public function admin_fields($value = '') - { - $html = parent::admin_fields($value); - $this->admin_title = __("E-mail Field", "wc-kalkulator"); - return $html; - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - $args = $this->prepared_data(); - $args['placeholder'] = esc_html($this->data["default_value"]); - $args['min_length'] = $this->data["min"]; - $args['max_length'] = $this->data["max"]; - $args['value'] = $value; - - return View::render('fields/front/email', $args); - } - - /** - * Run all validation tests - * @param $value - * @return bool - */ - public function validate($value) - { - $is_email_valid = empty($value) ? true : sanitize_email($value); - - return parent::validate($value) && $is_email_valid; - } - +class EmailField extends TextField { + protected $type = "email"; + protected $group = "input"; + + /** + * @param string $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $html = parent::admin_fields( $value ); + $this->admin_title = __( "E-mail Field", "wc-kalkulator" ); + + return $html; + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + $args = $this->prepared_data(); + $args['placeholder'] = esc_html( $this->data["default_value"] ); + $args['min_length'] = $this->data["min"]; + $args['max_length'] = $this->data["max"]; + $args['value'] = $value; + + return View::render( 'fields/front/email', $args ); + } + + /** + * Run all validation tests + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + $is_email_valid = empty( $value ) ? true : sanitize_email( $value ); + + return parent::validate( $value ) && $is_email_valid; + } + } \ No newline at end of file diff --git a/src/Fields/EmptyField.php b/src/Fields/EmptyField.php index 9a6ff7e..3a2d48c 100644 --- a/src/Fields/EmptyField.php +++ b/src/Fields/EmptyField.php @@ -8,69 +8,68 @@ * Class EmptyField * @package WCKalkulator */ -class EmptyField extends AbstractField -{ - protected $parameters = array("type", "name", "content"); - protected $default_data = array("required" => false); - protected $data; - protected $type = "empty"; - protected $admin_title; - protected $use_expression = false; - protected $group = "static"; +class EmptyField extends AbstractField { + protected $parameters = array( "type", "name", "content" ); + protected $default_data = array( "required" => false ); + protected $data; + protected $type = "empty"; + protected $admin_title; + protected $use_expression = false; + protected $group = "static"; - /** - * Output HTML for fields at backend. - * - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Empty", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Empty", "wc-kalkulator" ); - /** - * Output HTML for product page - * - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - return ''; - } + return View::render( 'fields/admin/' . $this->type ); + } - /** - * No need to show static field in the user's cart - * - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - return ''; - } + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + return ''; + } - /** - * No need to validate static field - * - * @param $value - * @return bool - */ - public function validate($value) - { - return true; - } + /** + * No need to show static field in the user's cart + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + return ''; + } - /** - * No need to display the field in order line item - * - * @param $value - */ - public function order_item_value($value) - { - return; - } + /** + * No need to validate static field + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + return true; + } + + /** + * No need to display the field in order line item + * + * @param $value + */ + public function order_item_value( $value ) { + return; + } } \ No newline at end of file diff --git a/src/Fields/FileuploadField.php b/src/Fields/FileuploadField.php index f9f99d5..27816bc 100644 --- a/src/Fields/FileuploadField.php +++ b/src/Fields/FileuploadField.php @@ -5,97 +5,185 @@ use WCKalkulator\View; /** - * - * - * - * !!!!! This class is not finished yet !!!! - * - * - * - * - * - * Class FileuploadFiled + * Class FileuploadField * @package WCKalkulator */ -class FileuploadField extends AbstractField -{ - protected $parameters = array("type", "name", "title", "hint", "css_class", "required", "max_file_count", "max_file_size", "allowed_extensions"); - protected $default_data = array("css_class" => "", "required" => false, "default_value" => "", "hint" => ""); - protected $data; - protected $type = "fileupload"; - protected $admin_title; - protected $use_expression = false; - protected $group = "upload"; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("File Upload", "wc-kalkulator"); - return View::render('fields/admin/fileupload'); - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - $args = $this->prepared_data(); - $args['max_file_count'] = $this->data('max_file_count'); - $args['max_file_size'] = $this->data('max_file_size'); - $args['allowed_extensions'] = $this->data('allowed_extensions'); - return View::render('fields/front/fileupload', $args); - } - - /** - * Output HTML for User's cart nad order meta - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - return View::render('fields/cart', array( - 'title' => $this->data['title'], - 'value' => $value - )); - } - - /** - * Run all validation tests - * @param $value - * @return bool - */ - public function validate($value) - { - if(!$this->is_required() && empty($value)) { - return true; - } - - $is_required_and_nonempty = true; - if ($this->data['required']) { - if (empty($value) || $value === "") { - $is_required_and_nonempty = false; - } - } - - return $is_required_and_nonempty; - } - - /** - * Display value of the field in order line item at backend - * - * @param $value - * @return string - * @since 1.2.0 - */ - public function order_item_value($value) - { - return $value; - } - +class FileuploadField extends AbstractField { + protected $parameters = array( + "type", + "name", + "title", + "hint", + "css_class", + "required", + "max_file_size", + "allowed_extensions" + ); + protected $default_data = array( "css_class" => "", "required" => false, "default_value" => "", "hint" => "" ); + protected $data; + protected $type = "fileupload"; + protected $admin_title; + protected $use_expression = true; + protected $group = "upload"; + protected $mimes = array( + "pdf" => "application/pdf", + "jpg" => "image/jpeg", + "jpeg" => "image/jpeg", + "png" => "image/png", + "gif" => "image/gif" + ); + protected $ext = array( "pdf", "jpg", "jpeg", "png", "gif" ); + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "File Upload", "wc-kalkulator" ); + + return View::render( 'fields/admin/' . $this->type ); + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + $args = $this->prepared_data(); + $args['max_file_size'] = $this->data( 'max_file_size' ); + if ( ! empty( $this->data( 'allowed_extensions' ) ) ) { + $args['allowed_extensions'] = $this->data( 'allowed_extensions' ); + $args['accept'] = '.' . str_replace( '|', ', .', $this->data( 'allowed_extensions' ) ); + } else { + $args['allowed_extensions'] = 'pdf|jpg|jpeg|png|gif'; + $args['accept'] = '.pdf, .jpg, .jpeg, .png, .gif'; + } + + return View::render( 'fields/front/' . $this->type, $args ); + } + + /** + * Output HTML for User's cart nad order meta + * + * @param $file + * + * @return string + */ + public function render_for_cart( $file = '' ) { + if ( is_array( $file ) ) { + return View::render( 'fields/cart', array( + 'title' => $this->data['title'], + 'value' => esc_html( $file['original_name'] ) . ' (' . round( $file['size'] / 1000000, 2 ) . ' MB)' + ) ); + } + } + + /** + * Run all validation tests + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + if ( ! $this->is_required() && empty( $value ) ) { + return true; + } + + if ( $this->data['required'] && empty( $value ) ) { + return false; + } + + if ( is_array( $value ) ) { + if ( is_uploaded_file( $value['tmp_name'] ) ) { + $allowed_size = $this->data( 'max_file_size' ) * 1000000; //Bytes + $allowed_ext = array(); + + foreach ( $this->ext as $ext ) { + if ( strpos( $this->data( 'allowed_extensions' ), $ext ) !== false ) { + $allowed_ext[] = $ext; + } + } + + if ( empty( $allowed_ext ) ) { + $allowed_ext = $this->ext; + } + + $mimes = array(); + + foreach ( $allowed_ext as $ext ) { + $mimes[ $ext ] = $this->mimes[ $ext ]; + } + + if ( $value['size'] > 0 && $value['size'] <= $allowed_size && filesize( $value['tmp_name'] ) <= $allowed_size ) { + $fileinfo = wp_check_filetype_and_ext( $value['tmp_name'], $value['original_name'], $mimes ); + if ( $fileinfo['type'] !== false && in_array( $fileinfo['type'], + $mimes ) && in_array( $fileinfo['ext'], $allowed_ext ) ) { + return true; + } + } + } + } + + return false; + } + + /** + * Display value of the field in order line item at backend + * + * @param $value + * + * @return string + * @since 1.2.0 + */ + public function order_item_value( $value ) { + return $value; + } + + /** + * Handles file upload. Copy file from temp location to the customer directory + * + * @param $data + * + * @return bool + * @since 1.3.0 + */ + public function upload( $data ) { + if ( ! file_exists( $data['upload_tmp'] ) ) { + return false; + } + + $dir = dirname( $data['upload_path'] ); + if ( ! file_exists( $dir ) ) { + mkdir( $dir, 0755, true ); + file_put_contents( $dir . '/index.html', '' ); + } + + return rename( $data['upload_tmp'], $data['upload_path'] ); + } + + /** + * Handles file upload from POST to the temp directory + * + * @param $data + * + * @return bool + * @since 1.3.0 + */ + public function upload_temp( $data ) { + if ( move_uploaded_file( $data['tmp_name'], $data['upload_tmp'] ) ) { + chmod( $data['upload_tmp'], 0644 ); + + return true; + } + + return false; + } + } \ No newline at end of file diff --git a/src/Fields/FormulaField.php b/src/Fields/FormulaField.php index cd1dc94..0915992 100644 --- a/src/Fields/FormulaField.php +++ b/src/Fields/FormulaField.php @@ -8,62 +8,67 @@ * Class FormulaField * @package WCKalkulator */ -class FormulaField extends HtmlField -{ - protected $parameters = array("type", "name", "content", "title"); - protected $default_data = array("required" => false, "content" => ""); - protected $data; - protected $type = "formula"; - protected $admin_title; - protected $use_expression = false; - protected $group = "special"; - protected $show_title = true; +class FormulaField extends HtmlField { + protected $parameters = array( "type", "name", "content", "title" ); + protected $default_data = array( "required" => false, "content" => "" ); + protected $data; + protected $type = "formula"; + protected $admin_title; + protected $use_expression = false; + protected $group = "special"; + protected $show_title = true; - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Value of Formula", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Value of Formula", "wc-kalkulator" ); - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - return ""; - } + return View::render( 'fields/admin/' . $this->type ); + } - /** - * No need to show hidden field in the user's cart - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - if($this->data('display_on_user_cart') === '1') { - return View::render('fields/cart', array( - 'title' => $this->data['title'], - 'value' => $value - )); - } - return ""; - } + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + return ""; + } - /** - * No need to validate - * @param $value - * @return bool - */ - public function validate($value) - { - return true; - } + /** + * No need to show hidden field in the user's cart + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + if ( $this->data( 'display_on_user_cart' ) === '1' ) { + return View::render( 'fields/cart', array( + 'title' => $this->data['title'], + 'value' => $value + ) ); + } + + return ""; + } + + /** + * No need to validate + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + return true; + } } \ No newline at end of file diff --git a/src/Fields/HeadingField.php b/src/Fields/HeadingField.php index cbfa5a8..f5f29a1 100644 --- a/src/Fields/HeadingField.php +++ b/src/Fields/HeadingField.php @@ -8,37 +8,40 @@ * Class StaticHtmlField * @package WCKalkulator */ -class HeadingField extends HtmlField -{ - protected $parameters = array("type", "name", "content", "level"); - protected $default_data = array("required" => false, "content" => "", "level" => "1"); - protected $data; - protected $type = "heading"; - protected $admin_title; - protected $use_expression = false; - protected $group = "static"; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Heading", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - return View::render('fields/front/' . $this->type, array( - 'content' => $this->data('content'), - 'level' => $this->data('level') - )); - } +class HeadingField extends HtmlField { + protected $parameters = array( "type", "name", "content", "level" ); + protected $default_data = array( "required" => false, "content" => "", "level" => "1" ); + protected $data; + protected $type = "heading"; + protected $admin_title; + protected $use_expression = false; + protected $group = "static"; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Heading", "wc-kalkulator" ); + + return View::render( 'fields/admin/' . $this->type ); + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + return View::render( 'fields/front/' . $this->type, array( + 'name' => $this->data( 'name' ), + 'content' => $this->data( 'content' ), + 'level' => $this->data( 'level' ) + ) ); + } } \ No newline at end of file diff --git a/src/Fields/HiddenField.php b/src/Fields/HiddenField.php index 08468e9..92438a7 100644 --- a/src/Fields/HiddenField.php +++ b/src/Fields/HiddenField.php @@ -8,59 +8,64 @@ * Class HiddenField * @package WCKalkulator */ -class HiddenField extends HtmlField -{ - protected $parameters = array("type", "name", "content", "title"); - protected $default_data = array("required" => false, "content" => ""); - protected $data; - protected $type = "hidden"; - protected $admin_title; - protected $use_expression = false; - protected $group = "static"; - protected $show_title = true; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Hidden", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - $args = $this->prepared_data(); - $args['content'] = $this->data('content'); - return View::render('fields/front/' . $this->type, $args); - } - - /** - * No need to show hidden field in the user's cart - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - return; - } - - /** - * No need to validate hidden static field - * @param $value - * @return bool - */ - public function validate($value) - { - return stripslashes($value) === ($this->data('title') . ': '. $this->data('content')); +class HiddenField extends HtmlField { + protected $parameters = array( "type", "name", "content", "title" ); + protected $default_data = array( "required" => false, "content" => "" ); + protected $data; + protected $type = "hidden"; + protected $admin_title; + protected $use_expression = false; + protected $group = "static"; + protected $show_title = true; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Hidden", "wc-kalkulator" ); + + return View::render( 'fields/admin/' . $this->type ); + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + $args = $this->prepared_data(); + $args['content'] = $this->data( 'content' ); + + return View::render( 'fields/front/' . $this->type, $args ); + } + + /** + * No need to show hidden field in the user's cart + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + return; + } + + /** + * No need to validate hidden static field + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + return stripslashes( $value ) === ( $this->data( 'title' ) . ': ' . $this->data( 'content' ) ); + + } - } - } \ No newline at end of file diff --git a/src/Fields/HtmlField.php b/src/Fields/HtmlField.php index 092558d..7a8ab56 100644 --- a/src/Fields/HtmlField.php +++ b/src/Fields/HtmlField.php @@ -8,76 +8,78 @@ * Class HtmlField * @package WCKalkulator */ -class HtmlField extends AbstractField -{ - protected $parameters = array("type", "name", "content"); - protected $default_data = array("required" => false, "content"); - protected $data; - protected $type = "html"; - protected $admin_title; - protected $use_expression = false; - protected $group = "static"; +class HtmlField extends AbstractField { + protected $parameters = array( "type", "name", "content" ); + protected $default_data = array( "required" => false, "content" ); + protected $data; + protected $type = "html"; + protected $admin_title; + protected $use_expression = false; + protected $group = "static"; - /** - * Output HTML for fields at backend. - * - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("HTML", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "HTML", "wc-kalkulator" ); - /** - * Output HTML for product page - * - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - $content = $this->data('content'); - preg_match('/{=(.+)}/m', $content, $matches); - if(!empty($matches)) { - $content = str_replace($matches[0], '', $content); - } - return View::render('fields/front/' . $this->type, array( - 'content' => $content - )); - } + return View::render( 'fields/admin/' . $this->type ); + } - /** - * No need to show static field in the user's cart - * - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - return; - } + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + $content = $this->data( 'content' ); + preg_match( '/{=(.+)}/m', $content, $matches ); + if ( ! empty( $matches ) ) { + $content = str_replace( $matches[0], '', + $content ); + } + + return View::render( 'fields/front/' . $this->type, array( + 'name' => $this->data( 'name' ), + 'content' => $content + ) ); + } + + /** + * No need to show static field in the user's cart + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + return; + } + + /** + * No need to validate static field + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + return true; + } + + /** + * No need to display the field in order line item + * + * @param $value + */ + public function order_item_value( $value ) { + return; + } - /** - * No need to validate static field - * - * @param $value - * @return bool - */ - public function validate($value) - { - return true; - } - - /** - * No need to display the field in order line item - * - * @param $value - */ - public function order_item_value($value) - { - return; - } - } \ No newline at end of file diff --git a/src/Fields/ImageselectField.php b/src/Fields/ImageselectField.php index ca8e27e..447ea9c 100644 --- a/src/Fields/ImageselectField.php +++ b/src/Fields/ImageselectField.php @@ -8,82 +8,102 @@ * Class ImageselectField * @package WCKalkulator */ -class ImageselectField extends SelectField -{ - protected $parameters = array("type", "name", "title", "hint", "options_name", "options_title", "options_image", "css_class", "required", "default_value"); - protected $default_data = array("css_class" => "", "required" => false, "default_value" => "", "hint" => "", "image_size" => 60); - protected $type = "imageselect"; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Select Image", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } - - /** - * Output HTML for User's cart nad order meta - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - $value = $this->get_option_image($value); - - return View::render('fields/cart', array( - 'title' => $this->data['title'], - 'value' => $this->get_option_title($value), - 'image' => $value, - - )); - } - - /** - * Return option title based on option value from select field - * @param $value - * @return string - */ - public function get_option_image($value) - { - $id = array_search($value, $this->data['options_name']); - return wp_get_attachment_image_url( $this->data['options_image'][ $id ] ); - } - - /** - * Output HTML for product page - * @param $selected_name - * @return string - */ - public function render_for_product($selected_name = "") - { - if ($selected_name === "") { - $selected_name = $this->data["default_value"]; - } - $args = $this->prepared_data(); - $args['value'] = $selected_name; - $args['options_name'] = $this->data['options_name']; - $args['options_title'] = $this->data['options_title']; - $args['options_image'] = $this->data['options_image']; - $args['size'] = $this->data("image_size"); - - return View::render('fields/front/' . $this->type, $args); - } - - /** - * Display value of the field in order line item at backend - * - * @param $value - * @return string - * @since 1.2.0 - */ - public function order_item_value($value) - { - $img = $this->get_option_image($value); - $title = $this->get_option_title($value); - return $title . ' '; - } +class ImageselectField extends SelectField { + protected $parameters = array( + "type", + "name", + "title", + "hint", + "options_name", + "options_title", + "options_image", + "css_class", + "required", + "default_value" + ); + protected $default_data = array( + "css_class" => "", + "required" => false, + "default_value" => "", + "hint" => "", + "image_size" => 60 + ); + protected $type = "imageselect"; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Select Image", "wc-kalkulator" ); + + return View::render( 'fields/admin/' . $this->type ); + } + + /** + * Output HTML for User's cart nad order meta + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + return View::render( 'fields/cart', array( + 'title' => $this->data['title'], + 'value' => $this->get_option_title( $value ), + 'image' => $this->get_option_image( $value ), + ) ); + } + + /** + * Return option title based on option value from select field + * + * @param $value + * + * @return string + */ + public function get_option_image( $value ) { + $id = array_search( $value, $this->data['options_name'] ); + + return wp_get_attachment_image_url( $this->data['options_image'][ $id ] ); + } + + /** + * Output HTML for product page + * + * @param $selected_name + * + * @return string + */ + public function render_for_product( $selected_name = "" ) { + if ( $selected_name === "" ) { + $selected_name = $this->data["default_value"]; + } + $args = $this->prepared_data(); + $args['value'] = $selected_name; + $args['options_name'] = $this->data['options_name']; + $args['options_title'] = $this->data['options_title']; + $args['options_image'] = $this->data['options_image']; + $args['size'] = $this->data( "image_size" ); + + return View::render( 'fields/front/' . $this->type, $args ); + } + + /** + * Display value of the field in order line item at backend + * + * @param $value + * + * @return string + * @since 1.2.0 + */ + public function order_item_value( $value ) { + $img = $this->get_option_image( $value ); + $title = $this->get_option_title( $value ); + + return $title . ' '; + } } \ No newline at end of file diff --git a/src/Fields/ImageswatchesField.php b/src/Fields/ImageswatchesField.php index 20f8763..33c6859 100644 --- a/src/Fields/ImageswatchesField.php +++ b/src/Fields/ImageswatchesField.php @@ -8,19 +8,20 @@ * Class ImageswatchesField * @package WCKalkulator */ -class ImageswatchesField extends ImageselectField -{ - protected $type = "imageswatches"; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Image Swatches", "wc-kalkulator"); - return View::render('fields/admin/imageselect'); - } - +class ImageswatchesField extends ImageselectField { + protected $type = "imageswatches"; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Image Swatches", "wc-kalkulator" ); + + return View::render( 'fields/admin/imageselect' ); + } + } \ No newline at end of file diff --git a/src/Fields/ImageuploadField.php b/src/Fields/ImageuploadField.php index 62c71f2..fad7231 100644 --- a/src/Fields/ImageuploadField.php +++ b/src/Fields/ImageuploadField.php @@ -8,155 +8,180 @@ * Class ImageuploadField * @package WCKalkulator */ -class ImageuploadField extends AbstractField -{ - protected $parameters = array("type", "name", "title", "hint", "css_class", "required", "max_file_size", "allowed_extensions"); - protected $default_data = array("css_class" => "", "required" => false, "default_value" => "", "hint" => ""); - protected $data; - protected $type = "imageupload"; - protected $admin_title; - protected $use_expression = true; - protected $group = "upload"; - protected $mimes = array("jpg" => "image/jpeg", "jpeg" => "image/jpeg", "png" => "image/png", "gif" => "image/gif"); - protected $ext = array("jpg", "jpeg", "png", "gif"); - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Image Upload", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - $args = $this->prepared_data(); - $args['max_file_size'] = $this->data('max_file_size'); - if (!empty($this->data('allowed_extensions'))) { - $args['allowed_extensions'] = $this->data('allowed_extensions'); - $args['accept'] = '.' . str_replace('|', ', .', $this->data('allowed_extensions')); - } else { - $args['allowed_extensions'] = 'jpg|jpeg|png|gif'; - $args['accept'] = '.jpg, .jpeg, .png, .gif'; - } - return View::render('fields/front/' . $this->type, $args); - } - - /** - * Output HTML for User's cart nad order meta - * @param $file - * @return string - */ - public function render_for_cart($file = '') - { - if (is_array($file)) { - return View::render('fields/cart', array( - 'title' => $this->data['title'], - 'value' => esc_html($file['original_name']) . ' (' . round($file['size'] / 1000000, 2) . ' MB)' - )); - } - } - - /** - * Run all validation tests - * @param $value - * @return bool - */ - public function validate($value) - { - if (!$this->is_required() && empty($value)) - return true; - - if ($this->data['required'] && empty($value)) - return false; - - if (is_array($value)) { - if (is_uploaded_file($value['tmp_name'])) { - $allowed_size = $this->data('max_file_size') * 1000000; //Bytes - $allowed_ext = array(); - - foreach ($this->ext as $ext) { - if (strpos($this->data('allowed_extensions'), $ext) !== false) { - $allowed_ext[] = $ext; - } - } - - if (empty($allowed_ext)) - $allowed_ext = $this->ext; - - $mimes = array(); - - foreach ($allowed_ext as $ext) - $mimes[$ext] = $this->mimes[$ext]; - - if ($value['size'] > 0 && $value['size'] <= $allowed_size && filesize($value['tmp_name']) <= $allowed_size) { - $image_mime = wp_get_image_mime($value['tmp_name']); - if ($image_mime !== false && in_array($image_mime, $mimes)) { - return true; - } - } - } - } - - return false; - } - - /** - * Display value of the field in order line item at backend - * - * @param $value - * @return string - * @since 1.2.0 - */ - public function order_item_value($value) - { - return $value; - } - - /** - * Handles file upload. Copy file from temp location to the customer directory - * - * @param $data - * @return bool - * @since 1.3.0 - */ - public function upload($data) - { - if (!file_exists($data['upload_tmp'])) { - return false; - } - - $dir = dirname($data['upload_path']); - if (!file_exists($dir)) { - mkdir($dir, 0755, true); - file_put_contents($dir . '/index.html', ''); - } - - return rename($data['upload_tmp'], $data['upload_path']); - } - - /** - * Handles file upload from POST to the temp directory - * - * @param $data - * @return bool - * @since 1.3.0 - */ - public function upload_temp($data) - { - if (move_uploaded_file($data['tmp_name'], $data['upload_tmp'])) { - chmod($data['upload_tmp'], 0644); - return true; - } - return false; - } +class ImageuploadField extends AbstractField { + protected $parameters = array( + "type", + "name", + "title", + "hint", + "css_class", + "required", + "max_file_size", + "allowed_extensions" + ); + protected $default_data = array( "css_class" => "", "required" => false, "default_value" => "", "hint" => "" ); + protected $data; + protected $type = "imageupload"; + protected $admin_title; + protected $use_expression = true; + protected $group = "upload"; + protected $mimes = array( + "jpg" => "image/jpeg", + "jpeg" => "image/jpeg", + "png" => "image/png", + "gif" => "image/gif" + ); + protected $ext = array( "jpg", "jpeg", "png", "gif" ); + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Image Upload", "wc-kalkulator" ); + + return View::render( 'fields/admin/' . $this->type ); + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + $args = $this->prepared_data(); + $args['max_file_size'] = $this->data( 'max_file_size' ); + if ( ! empty( $this->data( 'allowed_extensions' ) ) ) { + $args['allowed_extensions'] = $this->data( 'allowed_extensions' ); + $args['accept'] = '.' . str_replace( '|', ', .', $this->data( 'allowed_extensions' ) ); + } else { + $args['allowed_extensions'] = 'jpg|jpeg|png|gif'; + $args['accept'] = '.jpg, .jpeg, .png, .gif'; + } + + return View::render( 'fields/front/' . $this->type, $args ); + } + + /** + * Output HTML for User's cart nad order meta + * + * @param $file + * + * @return string + */ + public function render_for_cart( $file = '' ) { + if ( is_array( $file ) ) { + return View::render( 'fields/cart', array( + 'title' => $this->data['title'], + 'value' => esc_html( $file['original_name'] ) . ' (' . round( $file['size'] / 1000000, 2 ) . ' MB)' + ) ); + } + } + + /** + * Run all validation tests + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + if ( ! $this->is_required() && empty( $value ) ) { + return true; + } + + if ( $this->data['required'] && empty( $value ) ) { + return false; + } + + if ( is_array( $value ) ) { + if ( is_uploaded_file( $value['tmp_name'] ) ) { + $allowed_size = $this->data( 'max_file_size' ) * 1000000; //Bytes + $allowed_ext = array(); + + foreach ( $this->ext as $ext ) { + if ( strpos( $this->data( 'allowed_extensions' ), $ext ) !== false ) { + $allowed_ext[] = $ext; + } + } + + if ( empty( $allowed_ext ) ) { + $allowed_ext = $this->ext; + } + + $mimes = array(); + + foreach ( $allowed_ext as $ext ) { + $mimes[ $ext ] = $this->mimes[ $ext ]; + } + + if ( $value['size'] > 0 && $value['size'] <= $allowed_size && filesize( $value['tmp_name'] ) <= $allowed_size ) { + $image_mime = wp_get_image_mime( $value['tmp_name'] ); + if ( $image_mime !== false && in_array( $image_mime, $mimes ) ) { + return true; + } + } + } + } + + return false; + } + + /** + * Display value of the field in order line item at backend + * + * @param $value + * + * @return string + * @since 1.2.0 + */ + public function order_item_value( $value ) { + return $value; + } + + /** + * Handles file upload. Copy file from temp location to the customer directory + * + * @param $data + * + * @return bool + * @since 1.3.0 + */ + public function upload( $data ) { + if ( ! file_exists( $data['upload_tmp'] ) ) { + return false; + } + + $dir = dirname( $data['upload_path'] ); + if ( ! file_exists( $dir ) ) { + mkdir( $dir, 0755, true ); + file_put_contents( $dir . '/index.html', '' ); + } + + return rename( $data['upload_tmp'], $data['upload_path'] ); + } + + /** + * Handles file upload from POST to the temp directory + * + * @param $data + * + * @return bool + * @since 1.3.0 + */ + public function upload_temp( $data ) { + if ( move_uploaded_file( $data['tmp_name'], $data['upload_tmp'] ) ) { + chmod( $data['upload_tmp'], 0644 ); + + return true; + } + + return false; + } } \ No newline at end of file diff --git a/src/Fields/LinkField.php b/src/Fields/LinkField.php index f749a44..1e14d07 100644 --- a/src/Fields/LinkField.php +++ b/src/Fields/LinkField.php @@ -8,60 +8,65 @@ * Class LinkField * @package WCKalkulator */ -class LinkField extends HtmlField -{ - protected $parameters = array("type", "name", "content", "title", "target"); - protected $default_data = array("required" => false, "content" => "", "target" => "_blank"); - protected $data; - protected $type = "link"; - protected $admin_title; - protected $use_expression = false; - protected $group = "static"; - protected $show_title = true; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Link / URL", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - $args = $this->prepared_data(); - $args['content'] = $this->data('content'); - $args['target'] = $this->data('target'); - return View::render('fields/front/' . $this->type, $args); - } - - /** - * No need to show hidden field in the user's cart - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - return; - } - - /** - * No need to validate hidden static field - * @param $value - * @return bool - */ - public function validate($value) - { - return true; - - } - +class LinkField extends HtmlField { + protected $parameters = array( "type", "name", "content", "title", "target" ); + protected $default_data = array( "required" => false, "content" => "", "target" => "_blank" ); + protected $data; + protected $type = "link"; + protected $admin_title; + protected $use_expression = false; + protected $group = "static"; + protected $show_title = true; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Link / URL", "wc-kalkulator" ); + + return View::render( 'fields/admin/' . $this->type ); + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + $args = $this->prepared_data(); + $args['content'] = $this->data( 'content' ); + $args['target'] = $this->data( 'target' ); + + return View::render( 'fields/front/' . $this->type, $args ); + } + + /** + * No need to show hidden field in the user's cart + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + return; + } + + /** + * No need to validate hidden static field + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + return true; + + } + } \ No newline at end of file diff --git a/src/Fields/NumberField.php b/src/Fields/NumberField.php index 90c9133..9365b66 100644 --- a/src/Fields/NumberField.php +++ b/src/Fields/NumberField.php @@ -8,88 +8,103 @@ * Class NumberField * @package WCKalkulator */ -class NumberField extends AbstractField -{ - protected $parameters = array("type", "name", "title", "hint", "min", "max", "css_class", "required", "default_value"); - protected $default_data = array("css_class" => "", "required" => true, "default_value" => "", "hint" => ""); - protected $data; - protected $type = "number"; - protected $admin_title; - protected $use_expression = true; - protected $group = "input"; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = "") - { - $this->admin_title = __("Number Field", "wc-kalkulator"); - return View::render('fields/admin/number'); - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - if ($value === "") { - $value = $this->data["default_value"]; - if ($value === "") { - $value = $this->data["min"]; - } - } - - $args = $this->prepared_data(); - $args['min'] = $this->data["min"]; - $args['max'] = $this->data["max"]; - $args['value'] = $value; - - return View::render('fields/front/number', $args); - } - - /** - * Output HTML for User's cart nad order meta - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - return View::render('fields/cart', array( - 'title' => $this->data['title'], - 'value' => $value - )); - } - - /** - * Run all validation tests - * @param $value - * @return bool - */ - public function validate($value) - { - if (!$this->is_required() && empty($value)) { - return true; - } - $is_greater_than_min = $value >= $this->data["min"]; - $is_less_than_max = $value <= $this->data["max"]; - $is_numeric = is_numeric($value); - return $is_greater_than_min && $is_less_than_max && $is_numeric; - } - - /** - * Display value of the field in order line item at backend - * - * @param $value - * @return string - * @since 1.2.0 - */ - public function order_item_value($value) - { - return $value; - } - +class NumberField extends AbstractField { + protected $parameters = array( + "type", + "name", + "title", + "hint", + "min", + "max", + "css_class", + "required", + "default_value" + ); + protected $default_data = array( "css_class" => "", "required" => true, "default_value" => "", "hint" => "" ); + protected $data; + protected $type = "number"; + protected $admin_title; + protected $use_expression = true; + protected $group = "input"; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = "" ) { + $this->admin_title = __( "Number Field", "wc-kalkulator" ); + + return View::render( 'fields/admin/number' ); + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + if ( $value === "" ) { + $value = $this->data["default_value"]; + if ( $value === "" ) { + $value = $this->data["min"]; + } + } + + $args = $this->prepared_data(); + $args['min'] = $this->data["min"]; + $args['max'] = $this->data["max"]; + $args['value'] = $value; + + return View::render( 'fields/front/number', $args ); + } + + /** + * Output HTML for User's cart nad order meta + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + return View::render( 'fields/cart', array( + 'title' => $this->data['title'], + 'value' => $value + ) ); + } + + /** + * Run all validation tests + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + if ( ! $this->is_required() && empty( $value ) ) { + return true; + } + $is_greater_than_min = $value >= $this->data["min"]; + $is_less_than_max = $value <= $this->data["max"]; + $is_numeric = is_numeric( $value ); + + return $is_greater_than_min && $is_less_than_max && $is_numeric; + } + + /** + * Display value of the field in order line item at backend + * + * @param $value + * + * @return string + * @since 1.2.0 + */ + public function order_item_value( $value ) { + return $value; + } + } \ No newline at end of file diff --git a/src/Fields/ParagraphField.php b/src/Fields/ParagraphField.php index 1ccaf5e..d0002c2 100644 --- a/src/Fields/ParagraphField.php +++ b/src/Fields/ParagraphField.php @@ -8,25 +8,26 @@ * Class ParagraphField * @package WCKalkulator */ -class ParagraphField extends HtmlField -{ - protected $parameters = array("type", "name", "content"); - protected $default_data = array("required" => false, "content"); - protected $data; - protected $type = "paragraph"; - protected $admin_title; - protected $use_expression = false; - protected $group = "static"; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Paragraph", "wc-kalkulator"); - return View::render('fields/admin/' . $this->type); - } - +class ParagraphField extends HtmlField { + protected $parameters = array( "type", "name", "content" ); + protected $default_data = array( "required" => false, "content" ); + protected $data; + protected $type = "paragraph"; + protected $admin_title; + protected $use_expression = false; + protected $group = "static"; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Paragraph", "wc-kalkulator" ); + + return View::render( 'fields/admin/' . $this->type ); + } + } \ No newline at end of file diff --git a/src/Fields/RadioField.php b/src/Fields/RadioField.php index a17c577..2127202 100644 --- a/src/Fields/RadioField.php +++ b/src/Fields/RadioField.php @@ -8,38 +8,40 @@ * Class RadioField * @package WCKalkulator */ -class RadioField extends SelectField -{ - protected $type = "radio"; - protected $group = "select"; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Radio", "wc-kalkulator"); - return View::render('fields/admin/select'); - } - - /** - * Output HTML for product page - * @param $selected_name - * @return string - */ - public function render_for_product($selected_name = "") - { - if ($selected_name === "") { - $selected_name = $this->data["default_value"]; - } - $args = $this->prepared_data(); - $args['value'] = $selected_name; - $args['options_name'] = $this->data['options_name']; - $args['options_title'] = $this->data['options_title']; - - return View::render('fields/front/radio', $args); - } - +class RadioField extends SelectField { + protected $type = "radio"; + protected $group = "select"; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Radio", "wc-kalkulator" ); + + return View::render( 'fields/admin/select' ); + } + + /** + * Output HTML for product page + * + * @param $selected_name + * + * @return string + */ + public function render_for_product( $selected_name = "" ) { + if ( $selected_name === "" ) { + $selected_name = $this->data["default_value"]; + } + $args = $this->prepared_data(); + $args['value'] = $selected_name; + $args['options_name'] = $this->data['options_name']; + $args['options_title'] = $this->data['options_title']; + + return View::render( 'fields/front/radio', $args ); + } + } \ No newline at end of file diff --git a/src/Fields/RangedatepickerField.php b/src/Fields/RangedatepickerField.php index c217c4b..f194759 100644 --- a/src/Fields/RangedatepickerField.php +++ b/src/Fields/RangedatepickerField.php @@ -9,173 +9,181 @@ * Class DatepickerField * @package WCKalkulator */ -class RangedatepickerField extends DatepickerField -{ - protected $type = "rangedatepicker"; - protected $group = "picker"; - - /** - * Called in enqueue_scripts action - */ - public function enqueue_scripts() - { - wp_enqueue_style( - 'wck-jquery-ui-css', - Plugin::url() . '/assets/css/jquery-ui.min.css' - ); - wp_enqueue_script('jquery-ui-datepicker'); - wp_enqueue_script( - 'wck-range-date-picker', - Plugin::url() . '/assets/js/rangedatepicker.min.js', - array('jquery-ui-datepicker'), - Plugin::VERSION, - 1 - ); - } - - /** - * @return array - */ - public function localize_script() - { - $options = array( - 'dateFormat' => 'yy-mm-dd', - 'minDate' => $this->data('disallow_past_date') ? '0' : null - ); - - return array( - 'script' => 'wck-range-date-picker', - 'field_name' => $this->data("name"), - 'options' => $options - ); - } - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $html = parent::admin_fields(); - $this->admin_title = __("Range Date Picker", "wc-kalkulator"); - return $html; - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = array()) - { - $args = $this->prepared_data(); - $args['value_from'] = isset($value['from']) ? $value['from'] : ''; - $args['value_to'] = isset($value['to']) ? $value['to'] : ''; - $args['disallow_past_date'] = $this->data["disallow_past_date"]; - - return View::render('fields/front/rangedatepicker', $args); - } - - /** - * Output HTML for User's cart nad order meta - * @param $value - * @return string - */ - public function render_for_cart($value = array()) - { - return View::render('fields/cart', array( - 'title' => $this->data['title'], - 'value' => $value["from"] . ' - ' . $value["to"] - )); - } - - /** - * @param $value - * @return bool - * @throws \Exception - */ - public function validate($value) - { - if (!isset($value["from"]) || !isset($value["to"]) || count($value) != 2) { - error_log($this->data("name") . " does not exists or is not an array of 2 items"); - return false; - } - - if ($this->is_required()) { - if (empty($value["from"]) || empty($value["to"])) { - error_log($this->data("name") . " is required and has empty values"); - return false; - } - } else { - if (empty($value["from"]) xor empty($value["to"])) { - error_log($this->data("name") . " is not required, but has incorrect value"); - return false; - } - if (empty($value["from"]) && empty($value["to"])) { - return true; - } - } - - //1. Check correct date - $count_valid = 0; - foreach ($value as $date) { - $date_arr = explode('-', $date); - if (count($date_arr) === 3) { - $year = $date_arr[0]; - $month = $date_arr[1]; - $day = $date_arr[2]; - $count_valid += checkdate($month, $day, $year) ? 1 : 0; - } - } - $is_valid = $count_valid === 2; - if (!$is_valid) { - error_log($this->data("name") . " - date is incorrect " . print_r($value, true) . " = " . $count_valid); - } - //2. Check past date option - if ($is_valid) { - foreach ($value as $date) { - if ($this->data("disallow_past_date")) { - try { - $datetime = new \DateTime($date); - $datetime->setTime(0, 0, 0); - $now = new \DateTime(); - $now->setTime(0, 0, 0); - } catch (\Exception $e) { - error_log($this->data("name") . " - DateTime exception"); - return false; - } - if ($datetime < $now) { - error_log($this->data("name") . " - past date is not allowed"); - return false; - } - } - } - //3. Check date "from" is earlier than date "to" - $date_from = new \DateTime($value["from"]); - $date_from->setTime(0, 0, 0); - $date_to = new \DateTime($value["to"]); - $date_to->setTime(0, 0, 0); - - if ($date_from > $date_to) { - error_log($this->data("name") . " date A is greater than date B"); - return false; - } - - } - - return $is_valid; - } - - /** - * Display value of the field in order line item at backend - * - * @param $value - * @return string - * @since 1.2.0 - */ - public function order_item_value($value) - { - return $value["from"] . ' - ' . $value["to"]; - } +class RangedatepickerField extends DatepickerField { + protected $type = "rangedatepicker"; + protected $group = "picker"; + + /** + * Called in enqueue_scripts action + */ + public function enqueue_scripts() { + wp_enqueue_style( + 'wck-jquery-ui-css', + Plugin::url() . '/assets/css/jquery-ui.min.css' + ); + wp_enqueue_script( 'jquery-ui-datepicker' ); + wp_enqueue_script( + 'wck-range-date-picker', + Plugin::url() . '/assets/js/rangedatepicker.min.js', + array( 'jquery-ui-datepicker' ), + Plugin::VERSION, + 1 + ); + } + + /** + * @return array + */ + public function localize_script() { + $options = array( + 'dateFormat' => 'yy-mm-dd', + 'minDate' => $this->data( 'disallow_past_date' ) ? '0' : null + ); + + return array( + 'script' => 'wck-range-date-picker', + 'field_name' => $this->data( "name" ), + 'options' => $options + ); + } + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $html = parent::admin_fields(); + $this->admin_title = __( "Range Date Picker", "wc-kalkulator" ); + + return $html; + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = array() ) { + $args = $this->prepared_data(); + $args['value_from'] = isset( $value['from'] ) ? $value['from'] : ''; + $args['value_to'] = isset( $value['to'] ) ? $value['to'] : ''; + $args['disallow_past_date'] = $this->data["disallow_past_date"]; + + return View::render( 'fields/front/rangedatepicker', $args ); + } + + /** + * Output HTML for User's cart nad order meta + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = array() ) { + return View::render( 'fields/cart', array( + 'title' => $this->data['title'], + 'value' => $value["from"] . ' - ' . $value["to"] + ) ); + } + + /** + * @param $value + * + * @return bool + * @throws \Exception + */ + public function validate( $value ) { + if ( ! isset( $value["from"] ) || ! isset( $value["to"] ) || count( $value ) != 2 ) { + error_log( $this->data( "name" ) . " does not exists or is not an array of 2 items" ); + + return false; + } + + if ( $this->is_required() ) { + if ( empty( $value["from"] ) || empty( $value["to"] ) ) { + error_log( $this->data( "name" ) . " is required and has empty values" ); + + return false; + } + } else { + if ( empty( $value["from"] ) xor empty( $value["to"] ) ) { + error_log( $this->data( "name" ) . " is not required, but has incorrect value" ); + + return false; + } + if ( empty( $value["from"] ) && empty( $value["to"] ) ) { + return true; + } + } + + //1. Check correct date + $count_valid = 0; + foreach ( $value as $date ) { + $date_arr = explode( '-', $date ); + if ( count( $date_arr ) === 3 ) { + $year = $date_arr[0]; + $month = $date_arr[1]; + $day = $date_arr[2]; + $count_valid += checkdate( $month, $day, $year ) ? 1 : 0; + } + } + $is_valid = $count_valid === 2; + if ( ! $is_valid ) { + error_log( $this->data( "name" ) . " - date is incorrect " . print_r( $value, + true ) . " = " . $count_valid ); + } + //2. Check past date option + if ( $is_valid ) { + foreach ( $value as $date ) { + if ( $this->data( "disallow_past_date" ) ) { + try { + $datetime = new \DateTime( $date ); + $datetime->setTime( 0, 0, 0 ); + $now = new \DateTime(); + $now->setTime( 0, 0, 0 ); + } catch ( \Exception $e ) { + error_log( $this->data( "name" ) . " - DateTime exception" ); + + return false; + } + if ( $datetime < $now ) { + error_log( $this->data( "name" ) . " - past date is not allowed" ); + + return false; + } + } + } + //3. Check date "from" is earlier than date "to" + $date_from = new \DateTime( $value["from"] ); + $date_from->setTime( 0, 0, 0 ); + $date_to = new \DateTime( $value["to"] ); + $date_to->setTime( 0, 0, 0 ); + + if ( $date_from > $date_to ) { + error_log( $this->data( "name" ) . " date A is greater than date B" ); + + return false; + } + + } + + return $is_valid; + } + + /** + * Display value of the field in order line item at backend + * + * @param $value + * + * @return string + * @since 1.2.0 + */ + public function order_item_value( $value ) { + return $value["from"] . ' - ' . $value["to"]; + } } \ No newline at end of file diff --git a/src/Fields/SelectField.php b/src/Fields/SelectField.php index 08401e0..90bc650 100644 --- a/src/Fields/SelectField.php +++ b/src/Fields/SelectField.php @@ -15,7 +15,7 @@ class SelectField extends AbstractField protected $data; protected $type = "select"; protected $admin_title; - protected $use_expression = true; + protected $use_expression = true; protected $group = "select"; /** diff --git a/src/Fields/TextField.php b/src/Fields/TextField.php index 12bb249..11d9940 100644 --- a/src/Fields/TextField.php +++ b/src/Fields/TextField.php @@ -8,101 +8,125 @@ * Class TextField * @package WCKalkulator */ -class TextField extends AbstractField -{ - protected $parameters = array("type", "name", "title", "hint", "css_class", "required", "default_value", "min", "max", "price", "pattern"); - protected $default_data = array("css_class" => "", "required" => false, "default_value" => "", "hint" => "", "pattern" => ""); - protected $data; - protected $type = "text"; - protected $admin_title; - protected $use_expression = true; - protected $group = "input"; - - /** - * Output HTML for fields at backend. - * @param $value - * @return string - */ - public function admin_fields($value = '') - { - $this->admin_title = __("Text", "wc-kalkulator"); - return View::render('fields/admin/text'); - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - $args = $this->prepared_data(); - $args['placeholder'] = esc_html($this->data["default_value"]); - $args['min_length'] = $this->data["min"]; - $args['max_length'] = $this->data["max"]; - $args['value'] = $value; - $args['pattern'] = $this->data('pattern'); - - return View::render('fields/front/text', $args); - } - - /** - * Output HTML for User's cart nad order meta - * @param $value - * @return string - */ - public function render_for_cart($value = '') - { - return View::render('fields/cart', array( - 'title' => $this->data['title'], - 'value' => $value - )); - } - - /** - * Run all validation tests - * @param $value - * @return bool - */ - public function validate($value) - { - if(!$this->is_required() && empty($value)) { - return true; - } - - $length = strlen(esc_html($value)); - - if (intval($this->data["min"]) > 0) - $is_longer_than_min = $length >= $this->data["min"]; - else - $is_longer_than_min = true; - - if (intval($this->data["max"]) > 0) - $is_shorter_than_max = $length <= $this->data["max"]; - else - $is_shorter_than_max = true; - - - $is_required_and_nonempty = true; - if ($this->data['required']) { - if (empty($value) || $value === "") { - $is_required_and_nonempty = false; - } - } - - return $is_longer_than_min && $is_shorter_than_max && $is_required_and_nonempty; - } - - /** - * Display value of the field in order line item at backend - * - * @param $value - * @return string - * @since 1.2.0 - */ - public function order_item_value($value) - { - return $value; - } - +class TextField extends AbstractField { + protected $parameters = array( + "type", + "name", + "title", + "hint", + "css_class", + "required", + "default_value", + "min", + "max", + "price", + "pattern" + ); + protected $default_data = array( + "css_class" => "", + "required" => false, + "default_value" => "", + "hint" => "", + "pattern" => "" + ); + protected $data; + protected $type = "text"; + protected $admin_title; + protected $use_expression = true; + protected $group = "input"; + + /** + * Output HTML for fields at backend. + * + * @param $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $this->admin_title = __( "Text", "wc-kalkulator" ); + + return View::render( 'fields/admin/text' ); + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + $args = $this->prepared_data(); + $args['placeholder'] = esc_html( $this->data["default_value"] ); + $args['min_length'] = $this->data["min"]; + $args['max_length'] = $this->data["max"]; + $args['value'] = $value; + $args['pattern'] = $this->data( 'pattern' ); + + return View::render( 'fields/front/text', $args ); + } + + /** + * Output HTML for User's cart nad order meta + * + * @param $value + * + * @return string + */ + public function render_for_cart( $value = '' ) { + return View::render( 'fields/cart', array( + 'title' => $this->data['title'], + 'value' => $value + ) ); + } + + /** + * Run all validation tests + * + * @param $value + * + * @return bool + */ + public function validate( $value ) { + if ( ! $this->is_required() && empty( $value ) ) { + return true; + } + + $length = strlen( esc_html( $value ) ); + + if ( intval( $this->data["min"] ) > 0 ) { + $is_longer_than_min = $length >= $this->data["min"]; + } else { + $is_longer_than_min = true; + } + + if ( intval( $this->data["max"] ) > 0 ) { + $is_shorter_than_max = $length <= $this->data["max"]; + } else { + $is_shorter_than_max = true; + } + + + $is_required_and_nonempty = true; + if ( $this->data['required'] ) { + if ( empty( $value ) || $value === "" ) { + $is_required_and_nonempty = false; + } + } + + return $is_longer_than_min && $is_shorter_than_max && $is_required_and_nonempty; + } + + /** + * Display value of the field in order line item at backend + * + * @param $value + * + * @return string + * @since 1.2.0 + */ + public function order_item_value( $value ) { + return $value; + } + } \ No newline at end of file diff --git a/src/Fields/TextareaField.php b/src/Fields/TextareaField.php index 775652a..f24c623 100644 --- a/src/Fields/TextareaField.php +++ b/src/Fields/TextareaField.php @@ -8,35 +8,36 @@ * Class TextField * @package WCKalkulator */ -class TextareaField extends TextField -{ - protected $type = "textarea"; - protected $group = "input"; - - /** - * @param string $value - * @return string - */ - public function admin_fields($value = '') - { - $html = parent::admin_fields($value); - $this->admin_title = __("Textarea", "wc-kalkulator"); - return $html; - } - - /** - * Output HTML for product page - * @param $value - * @return string - */ - public function render_for_product($value = "") - { - $args = $this->prepared_data(); - $args['placeholder'] = esc_html($this->data["default_value"]); - $args['min_length'] = $this->data["min"]; - $args['max_length'] = $this->data["max"]; - $args['value'] = $value; - - return View::render('fields/front/textarea', $args); - } +class TextareaField extends TextField { + protected $type = "textarea"; + protected $group = "input"; + + /** + * @param string $value + * + * @return string + */ + public function admin_fields( $value = '' ) { + $html = parent::admin_fields( $value ); + $this->admin_title = __( "Textarea", "wc-kalkulator" ); + + return $html; + } + + /** + * Output HTML for product page + * + * @param $value + * + * @return string + */ + public function render_for_product( $value = "" ) { + $args = $this->prepared_data(); + $args['placeholder'] = esc_html( $this->data["default_value"] ); + $args['min_length'] = $this->data["min"]; + $args['max_length'] = $this->data["max"]; + $args['value'] = $value; + + return View::render( 'fields/front/textarea', $args ); + } } \ No newline at end of file diff --git a/src/FieldsetProduct.php b/src/FieldsetProduct.php index 2434cc2..6cc3c7c 100644 --- a/src/FieldsetProduct.php +++ b/src/FieldsetProduct.php @@ -360,6 +360,22 @@ public function field($name) return false; } + public function set_default_input() + { + $data = array(); + foreach ($this->fields() as $name => $field) { + $field = (object) $field->data(); + if ((int)$field->use_expression === 1) { + if($field->type === 'checkboxgroup') { + $data[$name] = array($field->default_value); + } else { + $data[$name] = $field->default_value; + } + } + } + $this->user_input = $data; + } + /** * Gets user input from $_POST. Sanitize input * @@ -368,7 +384,6 @@ public function field($name) */ public function get_user_input() { - $user_input = array(); $allowed_names = $this->fields_names(); //if (isset($_POST['wck']) && is_array($_POST['wck'])) { @@ -387,17 +402,6 @@ public function get_user_input() $user_input = Sanitizer::sanitize($filtered_post, 'array'); //} - foreach ($allowed_names as $name) { - if (isset($_POST['wck'][$name])) { - $filtered_post[$name] = $_POST['wck'][$name]; - } else { - /* Set Default values if the field is not in POST data */ - if ($this->field($name)['type'] === 'checkboxgroup') { - $filtered_post[$name] = array(); - } - } - } - $user_input['_files'] = array(); /* Since v.1.3.1 upload path is defined in WCK Settings @@ -613,36 +617,37 @@ public function validate_values() /** * Add field's static price to $user_input * - * @param $input - * @return void + * @param $return + * @return void|array * @since 1.2.0 */ - public function set_additional_input_variables() + public function set_additional_input_variables($return = false) { - if (!is_array($this->user_input)) { + if (!$return && !is_array($this->user_input)) { return; } - $field_names = array_keys($this->user_input); + if(!$return) { + $field_names = array_keys($this->user_input); + foreach ($this->fields() as $field) { + $name = $field->data("name"); - foreach ($this->fields() as $field) { - $name = $field->data("name"); + // Check if field has price paramter and its name is in user input + if ($field->data("price") !== null && in_array($name, $field_names)) { + $static_price = Sanitizer::sanitize($field->data("price"), "price"); - // Check if field has price paramter and its name is in user input - if ($field->data("price") !== null && in_array($name, $field_names)) { - $static_price = Sanitizer::sanitize($field->data("price"), "price"); + if ($field->type() === "checkbox") { + $static_price = intval((int)$this->user_input[$name] === 1) * $static_price; + } - if ($field->type() === "checkbox") { - $static_price = intval((int)$this->user_input[$name] === 1) * $static_price; + if (empty($this->user_input[$field->data("name")])) { + $static_price = 0; + } + $this->user_input[$name] = $static_price; } - if (empty($this->user_input[$field->data("name")])) { - $static_price = 0; + if ($field->group() !== 'static' && isset($this->user_input[$name])) { + $this->set_additional_field_parameters($field, $this->user_input[$name]); } - $this->user_input[$name] = $static_price; - } - - if ($field->group() !== 'static' && isset($this->user_input[$name])) { - $this->set_additional_field_parameters($field, $this->user_input[$name]); } } @@ -659,6 +664,7 @@ public function set_additional_input_variables() $this->user_input["product_height"] = $product_helper->get_height(); $this->user_input["product_length"] = $product_helper->get_length(); $this->user_input["product_regular_price"] = $product_helper->regular_price(); + $this->user_input["product_is_on_sale"] = (bool)$product_helper->is_on_sale(); } } @@ -690,6 +696,10 @@ public function set_additional_input_variables() "variation_id" => $this->variation_id //lowest priority )); + if($return) { + return $this->user_input; + } + } /** @@ -724,7 +734,13 @@ public function set_additional_field_parameters($field, $input) $this->user_input[$name . ':text'] = sanitize_text_field($input); break; case 'imageupload': - $this->user_input[$name . ':size'] = round(absint($input) / 1000000, 2); + case 'fileupload': + if (is_array($input)) { + $size = $input['size']; + } else { + $size = $input; + } + $this->user_input[$name . ':size'] = round(absint($size) / 1000000, 2); break; } } @@ -772,7 +788,7 @@ public function calculate() if (isset($result['value']) && $result['is_error'] === false) { $this->user_input['total_price'] = $result['value'] * $this->user_input["quantity"]; } - return $parser->execute(); + return $result; } else { return Ajax::response('error', $parser->error); } @@ -781,6 +797,21 @@ public function calculate() } } + /** + * Calculate minimum price amount to display in price block + * + * @return void + * @since 1.6.0 + */ + public function calculate_minimum() + { + $parser = new ExpressionParser($this->expression(), $this->user_input); + if ($parser->is_ready()) { + return $parser->execute(); + } + return -1; + } + /** * Calculates the value of formula fields * diff --git a/src/Woocommerce/Cart.php b/src/Woocommerce/Cart.php index b9a5232..5a159d5 100644 --- a/src/Woocommerce/Cart.php +++ b/src/Woocommerce/Cart.php @@ -18,17 +18,18 @@ class Cart * @var Cart */ protected static $instance; - + /** * @var array */ protected $fieldsets; - + private function __construct() { add_action('woocommerce_cart_loaded_from_session', array($this, 'check_cart')); + add_filter('woocommerce_widget_cart_item_quantity', array($this, 'widget_cart_item_quantity'), 10, 3); } - + /** * Get instance of a singleton * @@ -40,10 +41,10 @@ public static function getInstance(): Cart if (self::$instance === null) { self::$instance = new static(); } - + return self::$instance; } - + /** * Check cart items for outdated fieldsets. Removes products with outdated fieldsets */ @@ -54,7 +55,7 @@ public function check_cart() foreach ($cart_items as $cart_item_key => $cart_item) { $fieldset_version_hash = isset($cart_item['wckalkulator_fieldset_version_hash']) ? $cart_item['wckalkulator_fieldset_version_hash'] : ''; $fieldset_id = isset($cart_item['wckalkulator_fieldset_id']) ? $cart_item['wckalkulator_fieldset_id'] : 0; - + if ($this->is_fieldset_outdated($fieldset_id, $fieldset_version_hash)) { wc_add_notice( sprintf(__('The product "%s" has been changed by shop manager and removed from the cart. ', 'woocommerce_kalkulator'), @@ -65,7 +66,25 @@ public function check_cart() } } } - + + /** + * Show the calculated product price in cart widget (cart popup) + * + * @param $html + * @param $cart_item + * @param $cart_item_key + * @return mixed|string + * @since 1.6.0 + */ + public function widget_cart_item_quantity($html, $cart_item, $cart_item_key) + { + if (isset($cart_item['wckalkulator_price'])) { + return '' . sprintf('%s × %s', $cart_item['quantity'], strip_tags(wc_price($cart_item['wckalkulator_price']))) . ''; + } else { + return $html; + } + } + /** * Check if fieldset has been updated by admin/manager. * @@ -84,10 +103,10 @@ private function is_fieldset_outdated($id, $hash) if (!isset($this->fielsets[$id])) { $this->fieldsets[$id] = FieldsetProduct::getInstance()->get_data($id); } - + return $hash !== $this->fieldsets[$id]->version_hash; } - + private function __clone() { } diff --git a/src/Woocommerce/Product.php b/src/Woocommerce/Product.php index a55d0fe..79d5b7f 100644 --- a/src/Woocommerce/Product.php +++ b/src/Woocommerce/Product.php @@ -19,6 +19,8 @@ */ class Product { + private static $default_price = 0; + /** * Add hooks and filters * @@ -43,8 +45,29 @@ public static function init() PriceFilter::getInstance(); Cart::getInstance(); + + //remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10); + //add_filter('woocommerce_structured_data_product_offer', array(__CLASS__, 'override_structured_data'), 10, 2); } + /** + * Change product price in structured data + * + * @param $markup_offer + * @param $product + * @return array + * @since 1.6.0 + */ + /*public static function override_structured_data($markup_offer, $product) + { + if(self::$default_price > 0) { + $formated_price = wc_format_decimal(self::$default_price, wc_get_price_decimals()); + $markup_offer['price'] = $formated_price; + $markup_offer['priceSpecification']['price'] = $formated_price; + } + return $markup_offer; + }*/ + /** * Add styles and scripts to the product page * @@ -64,6 +87,20 @@ public static function enqueue_scripts() $fieldset->add_action_price_block(); } + /*$default_price = 0; + $fieldset = FieldsetProduct::getInstance(); + $fieldset->init(); + $fieldset->set_default_input(); + if ($fieldset->validate(true)) { + try { + $default_price = $fieldset->calculate_minimum(); + $default_price = $default_price['value']; + } catch (\Exception $e) { + $default_price = 0; + } + } + self::$default_price = $default_price;*/ + /* * "Price.min.css" forces price block to be hidden. * Price blocks should be visible if the product is type of "variable" and user has selected 'variation_prices_visible' option. @@ -227,7 +264,9 @@ public static function validate_fields_on_product_page($validation, $product_id, */ public static function price_block() { - echo View::render('woocommerce/price_block'); + echo View::render('woocommerce/price_block', array( + 'default_price' => self::$default_price + )); } /** diff --git a/views/admin/expression.php b/views/admin/expression.php index 6b90393..6608e67 100644 --- a/views/admin/expression.php +++ b/views/admin/expression.php @@ -128,6 +128,7 @@ class="first-selected"> + diff --git a/views/fields/admin/fileupload.php b/views/fields/admin/fileupload.php index 9266a99..9fce47b 100644 --- a/views/fields/admin/fileupload.php +++ b/views/fields/admin/fileupload.php @@ -2,7 +2,9 @@ if (!defined('ABSPATH')) { exit; } + use WCKalkulator\Helper; + ?> +