Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Custom Fields
*
* @package CoreFunctionality
* @author Bill Erickson
* @since 1.0.0
* @license GPL-2.0+
**/
use Carbon_Fields\Container;
use Carbon_Fields\Field;
/**
* Register Fields
*
*/
function ea_register_custom_fields() {
Container::make( 'theme_options', 'Site Options' )
->add_tab( 'Site Footer', array(
Field::make( 'text', 'ea_privacy_url', 'Privacy Page URL' ),
Field::make( 'text', 'ea_youtube', 'YouTube URL' ),
Field::make( 'text', 'ea_linkedin', 'LinkedIn URL' )
));
Container::make( 'post_meta', 'Modules' )
->where( 'post_type', '=', 'page' )
->where( 'post_template', '=', 'templates/modules.php' )
->add_fields( array(
Field::make( 'complex', 'ea_modules', 'Modules' )
->set_collapsed( true )
->setup_labels( array( 'singular_name' => 'Module', 'plural_name' => 'Modules' ) )
->add_fields( 'header', 'Header', array(
Field::make( 'rich_text', 'content' ),
Field::make( 'image', 'image' ),
Field::make( 'select', 'content_alignment', 'Display content on' )
->add_options( array(
'left' => 'Left',
'right' => 'Right',
))
))
->add_fields( 'content', 'Content', array(
Field::make( 'text', 'title' ),
Field::make( 'select', 'bg', 'Background' )
->set_options( 'ea_module_bg_options' ),
Field::make( 'rich_text', 'content' )
))
->set_header_template( ea_metabox_header_template( 'title', 'Content' ) )
->add_fields( 'content_image', 'Content & Image', array(
Field::make( 'text', 'title' ),
Field::make( 'select', 'bg', 'Background' )
->set_options( 'ea_module_bg_options' ),
Field::make( 'rich_text', 'content' ),
Field::make( 'image', 'image' ),
Field::make( 'select', 'content_alignment', 'Display content on' )
->add_options( array(
'left' => 'Left',
'right' => 'Right',
))
))
->set_header_template( ea_metabox_header_template( 'title', 'Content & Image' ) )
->add_fields( 'content_icons', 'Content & Icons', array(
Field::make( 'text', 'title' ),
Field::make( 'select', 'bg', 'Background' )
->set_options( 'ea_module_bg_options' ),
Field::make( 'rich_text', 'content' ),
Field::make( 'complex', 'icons' )
->set_collapsed( true )
->setup_labels( array( 'singular_name' => 'Icon', 'plural_name' => 'Icons' ) )
->add_fields( array(
Field::make( 'text', 'title' ),
Field::make( 'select', 'icon' )
->set_options( 'ea_module_icon_options' )
))
->set_header_template( ea_metabox_header_template( 'title', 'Icon' ) )
))
->set_header_template( ea_metabox_header_template( 'title', 'Content & Icons' ) )
->add_fields( 'resources', 'Resources', array(
Field::make( 'text', 'title' ),
Field::make( 'select', 'bg', 'Background' )
->set_options( 'ea_module_bg_options' ),
Field::make( 'rich_text', 'content' ),
Field::make( 'complex', 'resources' )
->set_collapsed( true )
->setup_labels( array( 'singular_name' => 'Resource', 'plural_name' => 'Resources' ) )
->add_fields( array(
Field::make( 'text', 'title' ),
Field::make( 'text', 'url', 'URL' ),
Field::make( 'checkbox', 'new_tab', 'Open link in new tab' ),
Field::make( 'select', 'icon' )
->set_options( 'ea_module_icon_options' )
))
->set_header_template( ea_metabox_header_template( 'title', 'Resource' ) )
))
->set_header_template( ea_metabox_header_template( 'title', 'Resources' ) )
->add_fields( 'videos', 'Videos', array(
Field::make( 'text', 'title' ),
Field::make( 'select', 'bg', 'Background' )
->set_options( 'ea_module_bg_options' ),
Field::make( 'rich_text', 'content' ),
Field::make( 'complex', 'videos' )
->set_collapsed( true )
->setup_labels( array( 'singular_name' => 'Video', 'plural_name' => 'Videos' ) )
->add_fields( array(
Field::make( 'text', 'title' ),
Field::make( 'text', 'video_url', 'YouTube Video URL' ),
))
->set_header_template( ea_metabox_header_template( 'title', 'Video' ) )
))
->set_header_template( ea_metabox_header_template( 'title', 'Videos' ) )
->add_fields( 'features', 'Features', array(
Field::make( 'text', 'title' ),
Field::make( 'select', 'bg', 'Background' )
->set_options( 'ea_module_bg_options' ),
Field::make( 'rich_text', 'content' ),
Field::make( 'complex', 'features' )
->set_collapsed( true )
->setup_labels( array( 'singular_name' => 'Feature', 'plural_name' => 'Features' ) )
->add_fields( array(
Field::make( 'select', 'icon' )
->set_options( 'ea_module_icon_options' ),
Field::make( 'rich_text', 'content' ),
))
))
->set_header_template( ea_metabox_header_template( 'title', 'Features' ) )
->add_fields( 'testimonials', 'Testimonials', array(
Field::make( 'text', 'button_text', 'Button Text' )->set_width( 50 ),
Field::make( 'text', 'button_url', 'Button URL' )->set_width( 50 ),
Field::make( 'text', 'title' ),
Field::make( 'select', 'bg', 'Background' )
->set_options( 'ea_module_bg_options' ),
Field::make( 'image', 'image' ),
Field::make( 'complex', 'testimonials' )
->set_collapsed( true )
->setup_labels( array( 'singular_name' => 'Testimonial', 'plural_name' => 'Testimonials' ) )
->add_fields( array(
Field::make( 'textarea', 'quote' ),
Field::make( 'text', 'byline' )
))
->set_header_template( ea_metabox_header_template( 'byline', 'Testimonial' ) )
))
->set_header_template( ea_metabox_header_template( 'title', 'Testimonials' ) )
));
}
add_action( 'carbon_fields_register_fields', 'ea_register_custom_fields' );
/**
* Metabox Header Template
*
*/
function ea_metabox_header_template( $key = '', $title = '' ) {
return $title . '<% if (' . $key . ') { %>: <%- ' . $key . ' %><% } %>';
}
/**
* Module Background Options
*
*/
function ea_module_bg_options() {
return array(
'' => 'White',
'grey' => 'Grey',
'light-blue' => 'Light Blue',
'dark-blue' => 'Dark Blue',
);
}
/**
* Module Icon Options
*
*/
function ea_module_icon_options() {
$output = array();
$icons = ea_get_theme_icons();
foreach( $icons as $icon )
$output[$icon] = $icon;
return $output;
}
/**
* Theme Icons
*
*/
function ea_get_theme_icons() {
$icons = get_option( 'ea_theme_icons' );
if( empty( $icons ) ) {
$icons = scandir( get_stylesheet_directory() . '/assets/icons/' );
$icons = array_diff( $icons, array( '..', '.' ) );
$icons = array_values( $icons );
if( empty( $icons ) )
return $icons;
// remove the .svg
foreach( $icons as $i => $icon ) {
$icons[ $i ] = substr( $icon, 0, -4 );
}
update_option( 'ea_theme_icons', $icons );
}
return $icons;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment