Skip to content

Instantly share code, notes, and snippets.

@darthink
Created July 4, 2019 15:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darthink/10fd1b70007d0847b6469ae545415f66 to your computer and use it in GitHub Desktop.
Save darthink/10fd1b70007d0847b6469ae545415f66 to your computer and use it in GitHub Desktop.
<?php foreach ($controller->getItems() as $item) : ?>
<p><?php echo $item['title']; ?></p>
<?php endforeach; ?>
<?php
namespace App;
use Roots\Sage\Container;
use Roots\Sage\Assets\JsonManifest;
use Roots\Sage\Template\Blade;
use Roots\Sage\Template\BladeProvider;
use App\Blocks\Testimonial\Testimonial;
/**
* Theme assets
*/
add_action('wp_enqueue_scripts', function () {
wp_enqueue_style('material-icons', 'https://fonts.googleapis.com/icon?family=Material+Icons', false, null);
wp_enqueue_style('sage/main.css', asset_path('styles/main.css'), ['material-icons'], false, null);
// wp_enqueue_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js', ['jquery'], null, true);
wp_enqueue_script('sage/main.js', asset_path('scripts/main.js'), ['jquery'], null, true);
if (is_single() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply');
}
}, 100);
/**
* Theme setup
*/
add_action('after_setup_theme', function () {
/**
* Enable features from Soil when plugin is activated
* @link https://roots.io/plugins/soil/
*/
add_theme_support('soil-clean-up');
add_theme_support('soil-jquery-cdn');
add_theme_support('soil-nav-walker');
add_theme_support('soil-nice-search');
add_theme_support('soil-relative-urls');
/**
* Enable plugins to manage the document title
* @link https://developer.wordpress.org/reference/functions/add_theme_support/#title-tag
*/
add_theme_support('title-tag');
/**
* Register navigation menus
* @link https://developer.wordpress.org/reference/functions/register_nav_menus/
*/
register_nav_menus([
'primary_navigation' => __('Primary Navigation', 'sage')
]);
/**
* Enable post thumbnails
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
*/
add_theme_support('post-thumbnails');
/**
* Enable HTML5 markup support
* @link https://developer.wordpress.org/reference/functions/add_theme_support/#html5
*/
add_theme_support('html5', ['caption', 'comment-form', 'comment-list', 'gallery', 'search-form']);
/**
* Enable selective refresh for widgets in customizer
* @link https://developer.wordpress.org/themes/advanced-topics/customizer-api/#theme-support-in-sidebars
*/
add_theme_support('customize-selective-refresh-widgets');
/**
* Use main stylesheet for visual editor
* @see resources/assets/styles/layouts/_tinymce.scss
*/
add_editor_style(asset_path('styles/main.css'));
}, 20);
/**
* Register sidebars
*/
add_action('widgets_init', function () {
$config = [
'before_widget' => '<section class="widget %1$s %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3>',
'after_title' => '</h3>'
];
register_sidebar([
'name' => __('Primary', 'sage'),
'id' => 'sidebar-primary'
] + $config);
register_sidebar([
'name' => __('Footer', 'sage'),
'id' => 'sidebar-footer'
] + $config);
});
/**
* Updates the `$post` variable on each iteration of the loop.
* Note: updated value is only available for subsequently loaded views, such as partials
*/
add_action('the_post', function ($post) {
sage('blade')->share('post', $post);
});
/**
* Setup Sage options
*/
add_action('after_setup_theme', function () {
/**
* Add JsonManifest to Sage container
*/
sage()->singleton('sage.assets', function () {
return new JsonManifest(config('assets.manifest'), config('assets.uri'));
});
/**
* Add Blade to Sage container
*/
sage()->singleton('sage.blade', function (Container $app) {
$cachePath = config('view.compiled');
if (!file_exists($cachePath)) {
wp_mkdir_p($cachePath);
}
(new BladeProvider($app))->register();
return new Blade($app['view']);
});
/**
* Create @asset() Blade directive
*/
sage('blade')->compiler()->directive('asset', function ($asset) {
return "<?= " . __NAMESPACE__ . "\\asset_path({$asset}); ?>";
});
});
/**
* Register ACF Gutenblocks
*/
add_filter('acf_gutenblocks/blocks', function (array $blocks): array {
$new_blocks = [
Testimonial::class,
];
return array_merge($blocks, $new_blocks);
});
<?php
declare(strict_types=1);
namespace App\Blocks\Testimonial;
use Itineris\AcfGutenblocks\AbstractBlock;
class Testimonial extends AbstractBlock
{
public function __construct()
{
parent::__construct([
'title' => __('Testimonial', 'fabric'),
'description' => __('Testimonial description', 'fabric'),
'category' => 'formatting',
// Other valid acf_register_block() settings
]);
}
public function getItems(): array
{
$items = [];
foreach (get_field('list_items') as $item) {
if ($item['enabled']) {
$items[] = $item['list_item'];
}
}
return $items;
}
protected function registerFields(): array
{
$testimonial = new FieldsBuilder('testimonial');
$testimonial
->setLocation('block', '==', 'acf/testimonial');
$testimonial
->addText('quote')
->addText('cite')
->addRepeater('list_items')
->addText('list_item')
->addTrueFalse('enabled', [
'ui' => 1,
'default_value' => 1,
])
->endRepeater();
return $testimonial->build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment