*/
abstract class Fields extends Base {
public function hooks() {
if( did_action( "cx-plugin_{$this->config['id']}_loaded" ) ) return;
do_action( "cx-plugin_{$this->config['id']}_loaded" );
$this->action( 'admin_head', 'callback_head', 99 );
}
public function enqueue_scripts() {
wp_enqueue_media();
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker' );
wp_register_script( 'select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js' );
wp_register_style( 'select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css' );
wp_register_script( 'chosen', 'https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js' );
wp_register_style( 'chosen', 'https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css' );
if( $this->has_select2() ) {
wp_enqueue_style( 'select2' );
wp_enqueue_script( 'select2' );
}
if( $this->has_chosen() ) {
wp_enqueue_style( 'chosen' );
wp_enqueue_script( 'chosen' );
}
wp_enqueue_style( 'codexpert-product-fields', plugins_url( 'assets/css/fields.css', __FILE__ ), [], '' );
wp_enqueue_script( 'codexpert-product-fields', plugins_url( 'assets/js/fields.js', __FILE__ ), [ 'jquery' ], '', true );
}
public function callback_head() {
?>
config;
$scope = $metabox == [] ? 'option' : 'post';
echo '
';
if( $scope == 'option' ) :
$icon = $this->generate_icon( $config['icon'] );
echo "
{$icon} {$config['title']}
";
endif;
do_action( 'cx-settings-heading', $config );
if( !isset( $this->sections ) || count( $this->sections ) <= 0 ) return;
$tab_position = isset( $config['tab_position'] ) ? $config['tab_position'] : 'left';
echo "
";
$sections = $this->sections;
// nav tabs
$display = count( $sections ) > 1 ? 'block' : 'none';
echo '
';
foreach ( $sections as $section ) {
$icon = $this->generate_icon( $section['icon'] );
$color = isset( $section['color'] ) ? $section['color'] : '#23282d';
echo "- {$icon} {$section['label']}
";
}
echo '
';
// form areas
echo '
';
foreach ( $sections as $section ) {
$icon = $this->generate_icon( $section['icon'] );
$color = isset( $section['color'] ) ? $section['color'] : '#23282d';
$submit_button = isset( $section['submit_button'] ) ? $section['submit_button'] : __( 'Save Settings' );
$reset_button = isset( $section['reset_button'] ) ? $section['reset_button'] : __( 'Reset Default' );
$_nonce = wp_create_nonce();
echo "
";
echo "
";
do_action( 'cx-settings-before-title', $section );
echo "{$icon} {$section['label']}";
do_action( 'cx-settings-after-title', $section );
echo "
";
if( isset( $section['desc'] ) && $section['desc'] != '' ) {
echo "
{$section['desc']}
";
}
do_action( 'cx-settings-before-form', $section );
$fields = isset( $section['fields'] ) ? $section['fields'] : [];
$fields = apply_filters( 'cx-settings-fields', $fields, $section );
$show_form = isset( $section['hide_form'] ) && $section['hide_form'] ? false : true;
$show_form = apply_filters( 'cx-settigns-show-form', $show_form, $section );
if( $scope == 'option' && $show_form ) :
$page_load = isset( $section['page_load'] ) && $section['page_load'] ? 1 : 0;
echo "
';
endif; // if( $show_form ) :
do_action( 'cx-settings-after-form', $section );
echo "
";
}
echo '
';
if( isset( $config['css'] ) && $config['css'] != '' ) {
echo "";
}
}
/**
* Populates all fields under a section or tab
*/
public function populate_fields( $fields, $section, $scope ) {
$html = '';
if( count( $fields ) > 0 ) :
foreach ( $fields as $field ) {
$_show_label = isset( $field['label'] ) && $field['type'] != 'tabs';
if( isset( $field['type'] ) && $field['type'] == 'divider' ) {
$style = isset( $field['style'] ) ? $field['style'] : '';
$html .= "{$field['label']}
";
}
else {
$field_display = isset( $field['condition'] ) && is_array( $field['condition'] ) ? 'none' : '';
$html .= "
";
if( $_show_label ) {
$html .= "
";
do_action( 'cx-settings-before-label', $field, $section );
$html .= "";
do_action( 'cx-settings-after-label', $field, $section );
$html .= "
";
}
$_label_class = $_show_label ? '' : 'cx-field-wrap-nolabel';
$html .= "
";
do_action( 'cx-settings-before-field', $field, $section );
if( isset( $field['template'] ) && $field['template'] != '' ) $html .= $field['template'];
if( isset( $field['type'] ) && $field['type'] != '' ) $html .= $this->populate( $field, $section, $scope );
do_action( 'cx-settings-after-field', $field, $section );
if( isset( $field['desc'] ) && $field['desc'] != '' ) {
$html .= "
{$field['desc']}
";
}
do_action( 'cx-settings-after-description', $field, $section );
$html .= "
";
}
}
endif; // if( count( $fields ) > 0 ) :
return $html;
}
/**
* Populates a single input field
*/
public function populate( $field, $section, $scope = 'option' ) {
if ( in_array( $field['type'], [ 'text', 'number', 'email', 'url', 'password', 'color', 'range', 'date', 'time' ] ) ) {
$callback_fn = 'field_text';
}
else {
$callback_fn = "field_{$field['type']}";
}
if( method_exists( $this, $callback_fn ) ) {
return $this->$callback_fn( $field, $section, $scope );
}
return __( 'Invalid field type', 'cx-plugin' );
}
public function get_value( $field, $section, $default = '', $scope = 'option' ) {
if( isset( $field['value'] ) ) return $field['value'];
if( $scope == 'option' ) {
$section_values = get_option( $section['id'] );
}
else {
global $post;
$section_values = get_post_meta( $post->ID, $section['id'], true );
}
if( isset( $section_values[ $field['id'] ] ) ) {
return $section_values[ $field['id'] ];
}
return $default;
}
public function field_text( $field, $section, $scope ) {
$default = isset( $field['default'] ) ? $field['default'] : '';
$value = $this->esc_str( $this->get_value( $field, $section, $default, $scope ) );
$type = $field['type'];
$name = $scope == 'option' ? $field['id'] : "{$section['id']}[{$field['id']}]";
$label = $field['label'];
$id = "{$section['id']}-{$field['id']}";
$class = "cx-field cx-field-{$field['type']}";
$class .= isset( $field['class'] ) ? $field['class'] : '';
$placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
$required = isset( $field['required'] ) && $field['required'] ? " required" : "";
$readonly = isset( $field['readonly'] ) && $field['readonly'] ? " readonly" : "";
$disabled = isset( $field['disabled'] ) && $field['disabled'] ? " disabled" : "";
$min = isset( $field['min'] ) && $field['min'] ? " min='{$field['min']}'" : "";
$max = isset( $field['max'] ) && $field['max'] ? " max='{$field['max']}'" : "";
$step = isset( $field['step'] ) && $field['step'] ? " step='{$field['step']}'" : "";
if( $type == 'color' ) {
$class .= ' cx-color-picker';
$type = 'text';
}
$html = "";
return $html;
}
public function field_textarea( $field, $section, $scope ) {
$default = isset( $field['default'] ) ? $field['default'] : '';
$value = $this->esc_str( $this->get_value( $field, $section, $default, $scope ) );
$name = $scope == 'option' ? $field['id'] : "{$section['id']}[{$field['id']}]";
$label = $field['label'];
$id = "{$section['id']}-{$field['id']}";
$class = "cx-field cx-field-{$field['type']}";
$class .= isset( $field['class'] ) ? $field['class'] : '';
$placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
$required = isset( $field['required'] ) && $field['required'] ? " required" : "";
$readonly = isset( $field['readonly'] ) && $field['readonly'] ? " readonly" : "";
$disabled = isset( $field['disabled'] ) && $field['disabled'] ? " disabled" : "";
$rows = isset( $field['rows'] ) ? $field['rows'] : 5;
$cols = isset( $field['cols'] ) ? $field['cols'] : 3;
$html = "";
return $html;
}
public function field_radio( $field, $section, $scope ) {
$default = isset( $field['default'] ) ? $field['default'] : '';
$value = $this->get_value( $field, $section, $default, $scope );
$name = $scope == 'option' ? $field['id'] : "{$section['id']}[{$field['id']}]";
$label = $field['label'];
$id = "{$section['id']}-{$field['id']}";
$class = "cx-field cx-field-{$field['type']}";
$class .= isset( $field['class'] ) ? $field['class'] : '';
$placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
$required = isset( $field['required'] ) && $field['required'] ? " required" : "";
$readonly = isset( $field['readonly'] ) && $field['readonly'] ? " readonly" : "";
$disabled = isset( $field['disabled'] ) && $field['disabled'] ? " disabled" : "";
$options = isset( $field['options'] ) ? $field['options'] : [];
$html = '';
foreach ( $options as $key => $title ) {
$html .= "";
$html .= "
";
}
return $html;
}
public function field_checkbox( $field, $section, $scope ) {
$default = isset( $field['default'] ) ? $field['default'] : '';
$value = $this->get_value( $field, $section, $default, $scope );
$name = $scope == 'option' ? $field['id'] : "{$section['id']}[{$field['id']}]";
$label = $field['label'];
$id = "{$section['id']}-{$field['id']}";
$class = "cx-field cx-field-{$field['type']}";
$class .= isset( $field['class'] ) ? $field['class'] : '';
$placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
$required = isset( $field['required'] ) && $field['required'] ? " required" : "";
$disabled = isset( $field['disabled'] ) && $field['disabled'] ? " disabled" : "";
$multiple = isset( $field['multiple'] ) && $field['multiple'];
$options = isset( $field['options'] ) ? $field['options'] : [];
$html = '';
if( $multiple ) {
foreach ( $options as $key => $title ) {
$html .= "
";
}
}
else {
$html .= "";
}
return $html;
}
public function field_switch( $field, $section, $scope ) {
$default = isset( $field['default'] ) ? $field['default'] : '';
$value = $this->get_value( $field, $section, $default, $scope );
$name = $scope == 'option' ? $field['id'] : "{$section['id']}[{$field['id']}]";
$label = $field['label'];
$id = "{$section['id']}-{$field['id']}";
$class = "cx-field cx-field-{$field['type']}";
$class .= isset( $field['class'] ) ? $field['class'] : '';
$placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
$required = isset( $field['required'] ) && $field['required'] ? " required" : "";
$disabled = isset( $field['disabled'] ) && $field['disabled'] ? " disabled" : "";
$multiple = isset( $field['multiple'] ) && $field['multiple'];
$options = isset( $field['options'] ) ? $field['options'] : [];
$html = '';
if( $multiple ) {
foreach ( $options as $key => $title ) {
$html .= "
";
}
}
else {
$html .= "
";
}
return $html;
}
public function field_select( $field, $section, $scope ) {
$default = isset( $field['default'] ) ? $field['default'] : '';
$value = $this->get_value( $field, $section, $default, $scope );
$name = $scope == 'option' ? $field['id'] : "{$section['id']}[{$field['id']}]";
$label = $field['label'];
$id = "{$section['id']}-{$field['id']}";
$class = "cx-field cx-field-{$field['type']}";
$class .= isset( $field['class'] ) ? $field['class'] : '';
$class .= isset( $field['select2'] ) && $field['select2'] ? ' cx-select2' : '';
$class .= isset( $field['chosen'] ) && $field['chosen'] ? ' cx-chosen' : '';
$placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
$required = isset( $field['required'] ) && $field['required'] ? " required" : "";
$disabled = isset( $field['disabled'] ) && $field['disabled'] ? " disabled" : "";
$multiple = isset( $field['multiple'] ) && $field['multiple'] ? 'multiple' : false;
$options = isset( $field['options'] ) ? $field['options'] : [];
$html = '';
if( $multiple ) {
$html .= "';
}
else {
$html .= "';
}
return $html;
}
public function field_file( $field, $section, $scope ) {
$default = isset( $field['default'] ) ? $field['default'] : '';
$value = $this->esc_str( $this->get_value( $field, $section, $default, $scope ) );
$type = $field['type'];
$name = $scope == 'option' ? $field['id'] : "{$section['id']}[{$field['id']}]";
$label = $field['label'];
$id = "{$section['id']}-{$field['id']}";
$class = "cx-field cx-field-{$field['type']}";
$class .= isset( $field['class'] ) ? $field['class'] : '';
$placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
$required = isset( $field['required'] ) && $field['required'] ? " required" : "";
$readonly = isset( $field['readonly'] ) && $field['readonly'] ? " readonly" : "";
$disabled = isset( $field['disabled'] ) && $field['disabled'] ? " disabled" : "";
$upload_button = isset( $field['upload_button'] ) ? $field['upload_button'] : __( 'Choose File' );
$select_button = isset( $field['select_button'] ) ? $field['select_button'] : __( 'Select' );
$html = '';
$html .= "";
$html .= "";
return $html;
}
public function field_wysiwyg( $field, $section, $scope ) {
$default = isset( $field['default'] ) ? $field['default'] : '';
$value = stripslashes( $this->get_value( $field, $section, $default, $scope ) );
$name = $scope == 'option' ? $field['id'] : "{$section['id']}[{$field['id']}]";
$label = $field['label'];
$id = "{$section['id']}-{$field['id']}";
$class = "cx-field cx-field-{$field['type']}";
$class .= isset( $field['class'] ) ? $field['class'] : '';
$placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
$readonly = isset( $field['readonly'] ) && $field['readonly'] ? " readonly" : "";
$disabled = isset( $field['disabled'] ) && $field['disabled'] ? " disabled" : "";
$teeny = isset( $field['teeny'] ) && $field['teeny'];
$text_mode = isset( $field['text_mode'] ) && $field['text_mode'];
$media_buttons = isset( $field['media_buttons'] ) && $field['media_buttons'];
$rows = isset( $field['rows'] ) ? $field['rows'] : 10;
$html = '';
$settings = [
'teeny' => $teeny,
'textarea_name' => $name,
'textarea_rows' => $rows,
'quicktags' => $text_mode,
'media_buttons' => $media_buttons,
];
if ( isset( $field['options'] ) && is_array( $field['options'] ) ) {
$settings = array_merge( $settings, $field['options'] );
}
ob_start();
wp_editor( $value, $id, $settings );
$html .= ob_get_contents();
ob_end_clean();
return $html;
}
public function field_divider( $field, $section, $scope ) {
return $field['label'];
}
public function field_group( $field, $section, $scope ) {
$items = $field['items'];
$html = '';
foreach ( $items as $item ) {
$item['class'] = ' cx-field-group';
$html .= $this->populate( $item, $section, $scope );
}
return $html;
}
public function field_tabs( $field, $section, $scope ) {
$tabs = $field['items'];
$html = $buttons = $content = '';
$count = 0;
foreach ( $tabs as $id => $tab ) {
$btn_active = $count == 0 ? 'cx-tab-active' : '';
$cnt_display = $count == 0 ? '' : 'none';
$buttons .= "{$tab['label']}";
$content .= "";
$content .= $this->populate_fields( $tab['fields'], $section, $scope );
$content .= "
";
$count++;
}
$style = "";
$html .= '';
$html .= $content;
$html .= $style;
return $html;
}
public function field_repeater( $field, $section, $scope ) {
$items = $field['items'];
$html = '';
$values = $this->get_value( $field, $section, [], $scope ) ? : [];
$count = 0;
for( $i = 0; $i < ( is_array( reset( $values ) ) ? count( reset( $values ) ) : 1 ); $i++ ) {
$html .= '';
foreach ( $items as $item ) {
$item['class'] = ' cx-field-group';
$item['default'] = isset( $item['default'] ) ? $item['default'] : '';
$item['value'] = isset( $values[ $item['id'] ][ $count ] ) ? $values[ $item['id'] ][ $count ] : $item['default'];
$item['id'] = "{$field['id']}[{$item['id']}][]";
$html .= $this->populate( $item, $section, $scope );
}
$html .= '';
$html .= '';
$html .= '
';
$count++;
}
return $html;
}
public function generate_icon( $value ) {
if( $value == '' ) return '';
if( strpos( $value, '://' ) !== false ) {
return "
";
}
return "";
}
public function esc_str( $string ) {
return stripslashes( esc_attr( $string ) );
}
public function deep_key_exists( $arr, $key ) {
if ( array_key_exists( $key, $arr ) && $arr[ $key ] == true ) return true;
foreach( $arr as $element ) {
if( is_array( $element ) && $this->deep_key_exists( $element, $key ) ) {
return true;
}
}
return false;
}
public function has_select2() {
return $this->deep_key_exists( $this->config, 'select2' );
}
public function has_chosen() {
return $this->deep_key_exists( $this->config, 'chosen' );
}
}