Skip to content

Instantly share code, notes, and snippets.

@congthien
Created August 21, 2018 12:35
Show Gist options
  • Save congthien/1183d8902e97ddeac34c7313f30b42b3 to your computer and use it in GitHub Desktop.
Save congthien/1183d8902e97ddeac34c7313f30b42b3 to your computer and use it in GitHub Desktop.
<?php
class Customify_Builder_Item_Contact {
public $id;
public $label;
public $prefix;
public $selector;
public $section;
public $panel = 'header_settings';
/**
* Optional construct
*/
function __construct()
{
$this->id = 'header_contact';
$this->label = __( 'Contact Info', 'customify-pro' );
$this->prefix = 'header_contact';
$this->selector = '.header--row:not(.header--transparent) #header_contact';
$this->section = 'header_contact';
add_filter( 'customify/icon_used', array( $this, 'used_icon' ) );
}
function used_icon( $list = array() ){
$list[ $this->id ] = 1;
return $list;
}
function item(){
return array(
'name' => $this->label,
'id' => $this->id,
'width' => '2',
'section' => $this->section // Customizer section to focus when click settings
);
}
function customize() {
$section = $this->section;
$fn = array( $this, 'render' );
$config = array(
array(
'name' => $section,
'type' => 'section',
'panel' => $this->panel,
'theme_supports' => '',
'title' => $this->label,
),
array(
'name' => $this->prefix . '_items',
'type' => 'repeater',
'section' => $section,
'selector' => $this->selector,
'render_callback' => $fn,
'title' => __( 'Contact Information', 'customify-pro' ),
'live_title_field' => 'title',
'default' => array(
array(
'title' => _x( '123 Street City', 'Default info', 'customify-pro' ),
'icon' => array(
'type' => 'font-awesome',
'icon' => 'fa fa-map-marker',
),
'type' => 'text',
),
array(
'title' => '1.800.123.4567',
'icon' => array(
'type' => 'font-awesome',
'icon' => 'fa fa-phone',
),
'type' => 'phone',
),
array(
'title' => 'email@example.com',
'icon' => array(
'icon' => 'fa fa-envelope-o',
'type' => 'font-awesome',
),
'type' => 'email',
)
),
'fields' => array(
array(
'name' => 'title',
'type' => 'text',
'label' => __( 'Title', 'customify-pro' ),
),
array(
'name' => 'icon',
'type' => 'icon',
'label' => __( 'Icon', 'customify-pro' ),
),
array(
'name' => 'type',
'type' => 'select',
'default' => 'text',
'label' => __( 'Field Type', 'customify-pro' ),
'choices' => array(
'text' => __( 'Text', 'customify-pro' ),
'phone' => __( 'Phone', 'customify-pro' ),
'email' => __( 'Email', 'customify-pro' ),
'url' => __( 'URL', 'customify-pro' ),
)
),
array(
'name' => 'url',
'type' => 'text',
'label' => __( 'Link', 'customify-pro' ),
'required' => array( 'type', '=', 'url' ),
),
)
),
array(
'name' => $this->prefix . '_typo',
'type' => 'typography',
'section' => $section,
'label' => __('Item Typography', 'customify-pro'),
'selector' => "{$this->selector} li",
'css_format' => 'typography',
),
array(
'name' => $this->prefix . '_styling',
'type' => 'styling',
'section' => $section,
'title' => __('Item Styling', 'customify-pro'),
'selector' => array(
'normal' => "{$this->selector} li",
'normal_margin' => "{$this->selector} li",
'normal_text_color' => "{$this->selector} li, {$this->selector} li a",
'hover' => "{$this->selector} li:hover, {$this->selector} li a:hover",
'hover_text_color' => "{$this->selector} {$this->selector} li:hover, {$this->selector} li a:hover",
),
'css_format' => 'styling',
'fields' => array(
'tabs' => array(
'normal' => __('Normal', 'customify-pro'),
'hover' => __('Hover', 'customify-pro'),
),
'normal_fields' => array(
//'padding' => false // disable for special field.
'link_color' => false,
'bg_cover' => false,
'bg_image' => false,
'bg_repeat' => false,
'bg_attachment' => false,
'bg_position' => false,
),
'hover_fields' => array(
'link_color' => false,
'bg_cover' => false,
'bg_image' => false,
'bg_repeat' => false,
'bg_attachment' => false,
'bg_position' => false,
), // disable hover tab and all fields inside.
)
),
array(
'name' => $this->prefix . '_icon_size',
'type' => 'slider',
'device_settings' => true,
'section' => $section,
'min' => 10,
'step' => 1,
'max' => 100,
//'selector' => "$selector li a",
'selector' => "format",
'css_format' => "{$this->selector} li .c-icon { font-size: {{value}}; }",
'label' => __( 'Icon Size', 'customify-pro' ),
),
array(
'name' => $this->prefix . '_padding',
'type' => 'slider',
'device_settings' => true,
'section' => $section,
'min' => .1,
'step' => .1,
'max' => 5,
'selector' => "{$this->selector} li .c-icon",
'unit' => 'em',
'css_format' => 'padding: {{value_no_unit}}em;',
'label' => __( 'Icon Padding', 'customify-pro' ),
),
array(
'name' => $this->prefix . '_icon_styling',
'type' => 'styling',
'section' => $section,
'title' => __('Item Icon Styling', 'customify-pro'),
'selector' => array(
'normal' => "{$this->selector} li .c-icon",
'hover' => "{$this->selector} li:hover .c-icon, {$this->selector} li a:hover .c-icon"
),
'css_format' => 'styling',
'fields' => array(
'tabs' => array(
'normal' => __('Normal', 'customify-pro'),
'hover' => __('Hover', 'customify-pro'),
),
'normal_fields' => array(
//'padding' => false // disable for special field.
'link_color' => false,
'padding' => false,
'bg_cover' => false,
'bg_image' => false,
'bg_repeat' => false,
'bg_attachment' => false,
'bg_position' => false,
'box_shadow' => false,
),
'hover_fields' => array(
'link_color' => false,
'bg_cover' => false,
'bg_image' => false,
'bg_repeat' => false,
'bg_attachment' => false,
'bg_position' => false,
'box_shadow' => false,
), // disable hover tab and all fields inside.
)
),
array(
'name' => $this->prefix . '_display_align',
'type' => 'select',
'device_settings' => true,
'section' => $section,
'devices' => array('desktop', 'mobile'),
'selector' => "{$this->selector}",
'render_callback' => $fn,
'label' => __( 'Display Align', 'customify-pro' ),
'choices' => array(
'horizontal' => __( 'Horizontal', 'customify-pro' ),
'vertical' => __( 'Vertical', 'customify-pro' )
),
),
);
if ( $this->id == 'header_contact' ) {
$config[] = array(
'name' => $this->prefix . '_vertical_on_sidebar',
'type' => 'checkbox',
'section' => $section,
'selector' => "{$this->selector}",
'render_callback' => $fn,
'checkbox_label' => __('Display as vertical in menu sidebar', 'customify-pro'),
);
}
// Item Layout
return array_merge( $config, customify_header_layout_settings( $this->id, $section ) );
}
/**
* @see https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/
*/
function render(){
$classes = array();
$classes[] = 'builder-contact--item';
$items = Customify()->get_setting( $this->prefix.'_items' );
$align_desktop = Customify()->get_setting( $this->prefix.'_display_align', 'desktop' );
$align_mobile = Customify()->get_setting( $this->prefix.'_display_align', 'mobile' );
$v_in_sidebar = Customify()->get_setting( $this->prefix.'_vertical_on_sidebar' );
if ( $v_in_sidebar ) {
$classes[] = 'hca-sidebar-vertical';
}
$classes[] = 'cont-desktop-'.$align_desktop;
$classes[] = 'cont-mobile-'.$align_mobile;
echo '<ul id="'.esc_attr( $this->id ).'" class="'.esc_attr( join( ' ', $classes ) ).'">';
foreach( ( array ) $items as $item ) {
$item = wp_parse_args( $item, array(
'title' => '',
'icon' => '',
'url' => '',
'type' => '',
'_visibility' => ''
) );
$item['icon'] = wp_parse_args( $item['icon'], array(
'icon' => ''
) );
if ( $item['_visibility'] != 'hidden' ) {
$icon_html = '';
if ( $item['icon']['icon'] ) {
$icon_html = '<i class="c-icon '.esc_attr( $item['icon']['icon'] ).'"></i>';
}
if ( $item['type'] == 'email' ) {
$item['title'] = antispambot( $item['title'] );
}
if ( is_rtl() ) {
$text = $item['title'] .$icon_html;
} else {
$text = $icon_html. $item['title'];
};
echo '<li class="c-type-'.esc_attr( $item['type'] ).'">';
switch( $item['type'] ) {
case 'url':
printf( '<a href="%1$s">%2$s</a>', esc_url( $item['url'] ), $text );
break;
case 'email':
$email_link = sprintf( 'mailto:%s', $item['title'] );
printf( '<a href="%1$s">%2$s</a>', esc_url( $email_link, array( 'mailto' ) ), $text );
break;
case 'phone':
printf( '<a href="%1$s">%2$s</a>', esc_attr( 'tel:'.$item['title'], array( 'tel' ) ), esc_html( $item['title'] ) );
break;
default:
echo '<span>'.$text.'</span>';
}
echo '</li>';
}
}
echo '</ul>';
}
}
class Customify_Builder_Footer_Item_Contact extends Customify_Builder_Item_Contact{
public $id;
public $label;
public $prefix;
public $selector;
public $section;
public $panel;
/**
* Optional construct
*/
function __construct()
{
parent::__construct();
$this->id = 'footer_contact';
$this->prefix = 'footer_contact';
$this->selector = '#footer_contact';
$this->section = 'footer_contact';
$this->panel = 'footer_settings';
}
}
Customify_Customize_Layout_Builder()->register_item('header', new Customify_Builder_Item_Contact() );
Customify_Customize_Layout_Builder()->register_item('footer', new Customify_Builder_Footer_Item_Contact() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment