<?php
/**
* Optin post meta
*
* @package cartflows
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Meta Boxes setup
*/
class Cartflows_Pro_Optin_Meta_Helper {
/**
* Instance
*
* @var $instance
*/
private static $instance;
/**
* Meta Option
*
* @var $meta_option
*/
private static $meta_option = null;
/**
* Initiator
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
public function __construct() {
}
/**
* Fetch default width of checkout fields by key.
*
* @param string $field_key field key.
* @return int
*/
public function get_default_optin_field_width( $field_key ) {
$default_width = 100;
switch ( $field_key ) {
case 'billing_first_name':
case 'billing_last_name':
$default_width = 50;
break;
default:
$default_width = 100;
break;
}
return $default_width;
}
/**
* Prepare HTML data for billing and shipping fields.
*
* @param string $field checkout field key.
* @param string $field_data checkout field object.
* @param integer $post_id chcekout post id.
* @param string $type checkout field type.
* @return array
*/
public function prepare_field_arguments( $field, $field_data, $post_id, $type ) {
$field_name = '';
if ( isset( $field_data['label'] ) ) {
$field_name = $field_data['label'];
}
if ( isset( $field_data['width'] ) ) {
$width = $field_data['width'];
} else {
$width = $this->get_default_optin_field_width( $field );
}
if ( isset( $field_data['enabled'] ) ) {
$is_enabled = true === $field_data['enabled'] ? 'yes' : 'no';
} else {
$is_enabled = 'yes';
}
$field_args = array(
'type' => ( isset( $field_data['type'] ) && ! empty( $field_data['type'] ) ) ? $field_data['type'] : '',
'label' => $field_name,
'name' => 'wcf-' . $field,
'placeholder' => isset( $field_data['placeholder'] ) ? $field_data['placeholder'] : '',
'width' => $width,
'enabled' => $is_enabled,
'after' => 'Enable',
'section' => $type,
'default' => isset( $field_data['default'] ) ? $field_data['default'] : '',
'required' => ( isset( $field_data['required'] ) && true == $field_data['required'] ) ? 'yes' : 'no',
'optimized' => ( isset( $field_data['optimized'] ) && true == $field_data['optimized'] ) ? 'yes' : 'no',
'options' => ( isset( $field_data['options'] ) && ! empty( $field_data['options'] ) ) ? implode( ',', $field_data['options'] ) : '',
);
if ( 'billing' === $type ) {
if ( isset( $field_data['custom'] ) && $field_data['custom'] ) {
$field_args['after_html'] = '<span class="wcf-cpf-actions" data-type="billing" data-key="' . $field . '">';
$field_args['after_html'] .= '<a class="wcf-pro-custom-field-remove wp-ui-text-notification">' . __( 'Remove', 'cartflows-pro' ) . '</a>';
$field_args['after_html'] .= '</span>';
}
}
return $field_args;
}
}
/**
* Kicking this off by calling 'get_instance()' method
*/
Cartflows_Pro_Optin_Meta_Helper::get_instance();