page_id = 'fields';
$this->section_id = 'billing';
$this->tabs = array( 'fields' => 'Checkout Fields', 'advanced_settings' => 'Advanced Settings');
$this->sections = array('billing' => 'Billing Fields', 'shipping' => 'Shipping Fields', 'additional' => 'Additional Fields');
}
public static function instance() {
if(is_null(self::$_instance)){
self::$_instance = new self();
}
return self::$_instance;
}
public function define_admin_hooks(){
// Show in order details page
add_action('woocommerce_admin_order_data_after_order_details', array($this, 'order_data_after_order_details'), 20, 1);
add_action('woocommerce_admin_order_data_after_billing_address', array($this, 'order_data_after_billing_address'), 20, 1);
add_action('woocommerce_admin_order_data_after_shipping_address', array($this, 'order_data_after_shipping_address'), 20, 1);
}
public function init(){
$this->field_form = new THWCFD_Admin_Form_Field();
$this->field_form_props = $this->field_form->get_field_form_props();
$this->render_page();
}
public function reset_to_default() {
delete_option('wc_fields_billing');
delete_option('wc_fields_shipping');
delete_option('wc_fields_additional');
return $this->print_notices('Checkout fields successfully reset', 'updated', true);
}
public function render_page(){
$this->output_tabs();
$this->output_sections();
$this->output_content();
}
public function render_checkout_fields_heading_row(){
?>
field_form->output_field_forms();
}
public function get_property_set_json($name, $field){
$json = '';
if(is_array($field)){
foreach($field as $pname => $pvalue){
$pvalue = is_array($pvalue) ? implode(',', $pvalue) : $pvalue;
$pvalue = is_string($pvalue) ? esc_attr($pvalue) : $pvalue;
$field[$pname] = $pvalue;
}
$field['name'] = $name;
$json = json_encode($field);
}
return $json;
}
private function save_or_update_field($section, $action) {
try {
$result = false;
$fields = THWCFD_Utils::get_fields($section);
$field = $this->prepare_field_from_posted_data($_POST);
$name = isset($field['name']) ? $field['name'] : false;
if($name){
if($action === 'new'){
$priority = THWCFD_Utils::prepare_field_priority($fields, false, true);
$field['custom'] = 1;
$field['priority'] = $priority;
}else{
$oname = isset($_POST['i_oname']) ? trim(stripslashes($_POST['i_oname'])) : false;
if($name && $oname && $name !== $oname ){
unset($fields[$oname]);
}
}
$fields[$name] = $field;
}
$result = THWCFD_Utils::update_fields($section, $fields);
if($result == true) {
$this->print_notices('Your changes were saved.', 'updated');
}else {
$this->print_notices('Your changes were not saved due to an error (or you made none!).', 'error');
}
} catch (Exception $e) {
$this->print_notices('Your changes were not saved due to an error.', 'error');
}
}
private function save_fields($section) {
try {
$f_names = !empty( $_POST['f_name'] ) ? $_POST['f_name'] : array();
if(empty($f_names)){
$this->print_notices('Your changes were not saved due to no fields found.', 'error');
return;
}
$f_order = !empty( $_POST['f_order'] ) ? $_POST['f_order'] : array();
$f_deleted = !empty( $_POST['f_deleted'] ) ? $_POST['f_deleted'] : array();
$f_enabled = !empty( $_POST['f_enabled'] ) ? $_POST['f_enabled'] : array();
$fields = THWCFD_Utils::get_fields($section);
$max = max( array_map( 'absint', array_keys( $f_names ) ) );
for($i = 0; $i <= $max; $i++) {
$name = $f_names[$i];
if(isset($fields[$name])){
$is_deleted = isset($f_deleted[$i]) && $f_deleted[$i] ? true : false;
if($is_deleted){
unset($fields[$name]);
continue;
}
$order = isset($f_order[$i]) ? trim(stripslashes($f_order[$i])) : 0;
$enabled = isset($f_enabled[$i]) ? trim(stripslashes($f_enabled[$i])) : 0;
$priority = THWCFD_Utils::prepare_field_priority($fields, $order, false);
$field = $fields[$name];
$field['priority'] = $priority;
$field['enabled'] = $enabled;
$fields[$name] = $field;
}
}
$fields = THWCFD_Utils::sort_fields($fields);
$result = THWCFD_Utils::update_fields($section, $fields);
if($result == true) {
$this->print_notices('Your changes were saved.', 'updated');
}else {
$this->print_notices('Your changes were not saved due to an error (or you made none!).', 'error');
}
} catch (Exception $e) {
$this->print_notices('Your changes were not saved due to an error.', 'error');
}
}
private function prepare_field_from_posted_data($posted){
$field_props = $this->field_form_props;
$field = array();
foreach ($field_props as $pname => $prop) {
$iname = 'i_'.$pname;
$pvalue = '';
if($prop['type'] === 'checkbox'){
$pvalue = isset($posted[$iname]) && $posted[$iname] ? 1 : 0;
}else if(isset($posted[$iname])){
//$pvalue = is_array($posted[$iname]) ? implode(',', $posted[$iname]) : trim(stripslashes($posted[$iname]));
$pvalue = is_array($posted[$iname]) ? $posted[$iname] : trim(stripslashes($posted[$iname]));
}
if($pname === 'class'){
//$pvalue = is_string($pvalue) ? array_map('trim', explode(',', $pvalue)) : $pvalue;
$pvalue = is_string($pvalue) ? preg_split('/(\s*,*\s*)*,+(\s*,*\s*)*/', $pvalue) : $pvalue;
$pvalue = is_array($pvalue) ? $pvalue : array();
}
$field[$pname] = $pvalue;
}
$type = isset($field['type']) ? $field['type'] : '';
if(!$type){
$type = isset($posted['i_otype']) ? trim(stripslashes($posted['i_otype'])) : '';
$field['type'] = $type;
}
$name = isset($field['name']) ? $field['name'] : '';
if(!$name){
$field['name'] = isset($posted['i_oname']) ? trim(stripslashes($posted['i_oname'])) : '';
}
if($type === 'select'){
$field['validate'] = '';
}else if($type === 'radio'){
$field['validate'] = '';
$field['placeholder'] = '';
}
if($type === 'select' || $type === 'radio'){
$options_json = isset($posted['i_options_json']) ? trim(stripslashes($posted['i_options_json'])) : '';
$options_arr = THWCFD_Utils::prepare_options_array($options_json);
//$field['options_json'] = $options_json;
$field['options'] = $options_arr;
}else{
$field['options'] = '';
}
$field['autocomplete'] = isset($posted['i_autocomplete']) ? $posted['i_autocomplete'] : '';
$field['priority'] = isset($posted['i_priority']) ? $posted['i_priority'] : '';
//$field['custom'] = isset($posted['i_custom']) ? $posted['i_custom'] : '';
$field['custom'] = isset($posted['i_custom']) && $posted['i_custom'] ? 1 : 0;
return $field;
}
/******* Display & Update Field Values *******/
/*********************************************/
public function order_data_after_order_details($order){
$fields = THWCFD_Utils::get_fields('additional');
$this->display_fields_in_admin_order($order, $fields, '
');
}
public function order_data_after_billing_address($order){
$fields = THWCFD_Utils::get_fields('billing');
$this->display_fields_in_admin_order($order, $fields, '');
}
public function order_data_after_shipping_address($order){
$fields = THWCFD_Utils::get_fields('shipping');
$this->display_fields_in_admin_order($order, $fields, '');
}
public function display_fields_in_admin_order($order, $fields, $prefix_html = ''){
if(is_array($fields)){
$html = '';
$order_id = THWCFD_Utils::get_order_id($order);
foreach($fields as $name => $field){
if(THWCFD_Utils::is_active_custom_field($field) && isset($field['show_in_order']) && $field['show_in_order']){
$value = get_post_meta( $order_id, $name, true );
if(!empty($value)){
$value = THWCFD_Utils::get_option_text($field, $value);
$label = isset($field['label']) && $field['label'] ? THWCFD_Utils::t($field['label']) : $name;
$html .= '
'. $label .':
'. wptexturize($value) .'
';
}
}
}
if($html){
echo $prefix_html.$html;
}
}
}
/******* TABS & SECTIONS *******/
/*******************************/
public function get_current_tab(){
return isset( $_GET['tab'] ) ? esc_attr( $_GET['tab'] ) : 'fields';
}
public function get_current_section(){
$tab = $this->get_current_tab();
$section = '';
if($tab === 'fields'){
$section = isset( $_GET['section'] ) ? esc_attr( $_GET['section'] ) : 'billing';
}
return $section;
}
public function output_tabs(){
$current_tab = $this->get_current_tab();
if(empty($this->tabs)){
return;
}
echo '
';
foreach( $this->tabs as $id => $label ){
$active = ( $current_tab == $id ) ? 'nav-tab-active' : '';
$label = __($label, 'woo-checkout-field-editor-pro');
echo ''.$label.'';
}
echo '
';
}
public function output_sections() {
$result = false;
if(isset($_POST['reset_fields']))
$result = $this->reset_to_default();
$current_tab = $this->get_current_tab();
$current_section = $this->get_current_section();
if(empty($this->sections)){
return;
}
$array_keys = array_keys( $this->sections );
echo '
';
foreach( $this->sections as $id => $label ){
$label = __($label, 'woo-checkout-field-editor-pro');
$url = $this->get_admin_url($current_tab, sanitize_title($id));
echo '- '. $label .' '. (end( $array_keys ) == $id ? '' : '|') .'
';
}
echo '
';
if($result){
echo $result;
}
}
public function get_admin_url($tab = false, $section = false){
$url = 'admin.php?page=checkout_form_designer';
if($tab && !empty($tab)){
$url .= '&tab='. $tab;
}
if($section && !empty($section)){
$url .= '§ion='. $section;
}
return admin_url($url);
}
}
endif;