forked from roots/sage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register Starter ACF Fields with Corresponding Bootstrap Components (#7)
* Setup fields builder and front-page, page and component field templates * Fix bug in Sage ACF Blocks MWDelaney/sage-acf-wp-blocks#52 (comment) * Update accordion and tab components and blocks * Add blocks config file * Add automatic registration of blocks connected to component fields * Add correct version of sage blocks plugin * Add additional components and refactor existing * Add options page * Update filters, add display_sidebar * Add default BS4 gravity forms styling * Include baseline swiper styles * Refactor default layouts and templates, add kitchen sink * Update swiper language to represent carousels/sliders * Update components and kitchen sink * Add link for sage acf block fix * Remove duplicate footer element * Add CLI stubs * Add CLI script for insering dummy data into kitchen sink template * Add default card imge style * Update swiper styles * Update layout of kitchen sink page * Remove $test variable * Refactor card field structure * Programatically add a menu via demo CLI script * Add alert component and refactor quicklinks * Remove block registration and specify details in comment See #11 for more info * Update default page settings
- Loading branch information
1 parent
7e90b59
commit fadb90b
Showing
53 changed files
with
1,222 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
use Sober\Controller\Controller; | ||
|
||
class TemplateKitchenSink extends Controller | ||
{ | ||
protected $acf = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
|
||
if (function_exists('acf_add_options_page')) : | ||
/** | ||
* Add an ACF options page | ||
* | ||
* @link https://www.advancedcustomfields.com/resources/options-page/ | ||
*/ | ||
\acf_add_options_page([ | ||
'page_title' => 'Theme General Settings', | ||
'menu_title' => 'Theme Settings', | ||
'menu_slug' => 'theme-general-settings', | ||
'capability' => 'edit_posts', | ||
'redirect' => false | ||
]); | ||
endif; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use WP_CLI; | ||
|
||
class CLI | ||
{ | ||
protected $kitchenSinkId; | ||
|
||
/** | ||
* Runs the demo setup | ||
* | ||
* @return void | ||
*/ | ||
public function demo() | ||
{ | ||
|
||
$this->kitchenSinkId = wp_insert_post([ | ||
'post_title' => 'Kitchen Sink', | ||
'post_status' => 'publish', | ||
'post_type' => 'page', | ||
]); | ||
|
||
$this->setPageTemplate(); | ||
$this->setQuicklinkData(); | ||
$this->setCardData(); | ||
$this->setSliderData(); | ||
$this->setAccordionTabData(); | ||
$this->addMenu(); | ||
|
||
// Present link to page | ||
$url = get_permalink($this->kitchenSinkId); | ||
WP_CLI::line( | ||
'Done! See the kitchen sink page at ' . WP_CLI::colorize("%B$url%n") | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Adds a menu to the primary_navigation theme location with the | ||
* Kitchen Sink page | ||
* | ||
* @link https://wordpress.stackexchange.com/a/44739/65403 | ||
* | ||
* @return void | ||
*/ | ||
protected function addMenu() | ||
{ | ||
$menuName = 'Main Menu'; | ||
$menuExists = wp_get_nav_menu_object($menuName); | ||
if ($menuExists) return; | ||
|
||
$menuId = wp_create_nav_menu($menuName); | ||
|
||
wp_update_nav_menu_item($menuId, 0, array( | ||
'menu-item-title' => 'Kitchen Sink', | ||
'menu-item-object' => 'page', | ||
'menu-item-object-id' => $this->kitchenSinkId, | ||
'menu-item-type' => 'post_type', | ||
'menu-item-status' => 'publish' | ||
)); | ||
|
||
if (!has_nav_menu('primary_navigation')) { | ||
$locations = get_theme_mod('nav_menu_locations'); | ||
$locations['primary_navigation'] = $menuId; | ||
set_theme_mod('nav_menu_locations', $locations); | ||
} | ||
} | ||
/** | ||
* Sets the data for the quicklinks field | ||
* | ||
* @return void | ||
*/ | ||
protected function setQuicklinkData(): void | ||
{ | ||
$data = []; | ||
for ($i = 0; $i < 3; $i++) { | ||
$data[] = [ | ||
'link' => [ | ||
'title' => 'Visit Link', | ||
'url' => '#' | ||
] | ||
]; | ||
} | ||
|
||
update_field('quicklinks', $data, $this->kitchenSinkId); | ||
} | ||
|
||
/** | ||
* Sets the data for the single and mult-card fields | ||
* | ||
* @return void | ||
*/ | ||
protected function setCardData(): void | ||
{ | ||
$multiCardData = []; | ||
for ($i = 0; $i < 3; $i++) { | ||
$multiCardData[] = $this->generateCardData(); | ||
} | ||
update_field('multi_card', $multiCardData, $this->kitchenSinkId); | ||
update_field('single_card', $multiCardData[0], $this->kitchenSinkId); | ||
} | ||
|
||
/** | ||
* Sets the data for the slider and carousel fields | ||
* | ||
* @return void | ||
*/ | ||
protected function setSliderData(): void | ||
{ | ||
$data = []; | ||
for ($i = 0; $i < 3; $i++) { | ||
$data[] = [ | ||
'background_image' => $this->saveRandomImage(), | ||
'title' => 'Test', | ||
'description' => 'Test', | ||
'link' => [ | ||
'title' => 'Visit Link', | ||
'url' => '#' | ||
] | ||
]; | ||
} | ||
|
||
update_field('slider', ['slides' => $data], $this->kitchenSinkId); | ||
update_field('carousel', ['slides' => $data], $this->kitchenSinkId); | ||
} | ||
|
||
/** | ||
* Sets the data for the accordion and tab fields | ||
* | ||
* @return void | ||
*/ | ||
public function setAccordionTabData(): void | ||
{ | ||
for ($i = 1; $i < 5; $i++) { | ||
$data[] = [ | ||
'title' => "Test $i", | ||
'content' => 'Test Content', | ||
]; | ||
} | ||
|
||
update_field('tabs', $data, $this->kitchenSinkId); | ||
update_field('accordion_items', $data, $this->kitchenSinkId); | ||
} | ||
|
||
/** | ||
* Set the page template to the kitchen sink template | ||
* | ||
* @return void | ||
*/ | ||
protected function setPageTemplate(): void | ||
{ | ||
update_post_meta($this->kitchenSinkId, '_wp_page_template', 'views/template-kitchen-sink.blade.php'); | ||
} | ||
|
||
/** | ||
* Generate data for a single card | ||
* | ||
* @return array | ||
*/ | ||
protected function generateCardData(): array | ||
{ | ||
return [ | ||
'image' => $this->saveRandomImage(), | ||
'title' => 'Lorem Ipsum', | ||
'content' => 'Test', | ||
'link' => [ | ||
'title' => 'Visit Link', | ||
'url' => '#' | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* Download a random image from Unsplash, save and | ||
* add to media library | ||
* | ||
* @return integer attachment ID | ||
*/ | ||
protected function saveRandomImage(): int | ||
{ | ||
$url = 'https://source.unsplash.com/random/800x600'; | ||
$filename = uniqid() . '.jpg'; | ||
$uploaddir = wp_upload_dir(); | ||
$uploadfile = $uploaddir['path'] . '/' . $filename; | ||
|
||
$contents = file_get_contents($url); | ||
$savefile = fopen($uploadfile, 'w'); | ||
fwrite($savefile, $contents); | ||
fclose($savefile); | ||
|
||
$wp_filetype = wp_check_filetype(basename($filename), null); | ||
|
||
$attachment = array( | ||
'post_mime_type' => $wp_filetype['type'], | ||
'post_title' => $filename, | ||
'post_content' => '', | ||
'post_status' => 'inherit' | ||
); | ||
|
||
$attach_id = wp_insert_attachment($attachment, $uploadfile); | ||
|
||
$imagenew = get_post($attach_id); | ||
$fullsizepath = get_attached_file($imagenew->ID); | ||
$attach_data = wp_generate_attachment_metadata($attach_id, $fullsizepath); | ||
wp_update_attachment_metadata($attach_id, $attach_data); | ||
|
||
return $imagenew->ID; | ||
} | ||
} | ||
|
||
add_action('cli_init', function () { | ||
$instance = new CLI(); | ||
WP_CLI::add_command('slab', $instance); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use StoutLogic\AcfBuilder\FieldsBuilder; | ||
|
||
$fields = new FieldsBuilder('accordion'); | ||
|
||
$fields | ||
->addRepeater('accordion_items') | ||
->addText('title')->setRequired() | ||
->addWysiwyg('content')->setRequired() | ||
->endRepeater(); | ||
|
||
return $fields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use StoutLogic\AcfBuilder\FieldsBuilder; | ||
|
||
$fields = new FieldsBuilder('alert'); | ||
|
||
$config = ['wrapper' => ['width' => '25%']]; | ||
|
||
$fields | ||
->addSelect('type', [ | ||
'choices' => [ | ||
'info' => 'Info', | ||
'success' => 'Success', | ||
'danger' => 'Danger', | ||
'warning' => 'Warning' | ||
], | ||
'default_value' => 'info', | ||
'wrapper' => $config['wrapper'] | ||
]) | ||
->addImage('icon', [ | ||
'return_format' => 'ID', | ||
'wrapper' => $config['wrapper'] | ||
]) | ||
->addText('text', $config)->setRequired() | ||
->addLink('link', $config); | ||
|
||
return $fields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use StoutLogic\AcfBuilder\FieldsBuilder; | ||
|
||
$fields = new FieldsBuilder('button'); | ||
|
||
$fields | ||
->addImage('icon', [ | ||
'return_format' => 'ID' | ||
]) | ||
->addLink('link')->setRequired(); | ||
|
||
return $fields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use StoutLogic\AcfBuilder\FieldsBuilder; | ||
|
||
$fields = new FieldsBuilder('card'); | ||
|
||
$fields | ||
->addSelect('layout', [ | ||
'choices' => [ | ||
'vertical' => 'Vertical', | ||
'horizontal' => 'Horizontal', | ||
], | ||
'default_value' => 'vertical', | ||
]) | ||
->addImage('image', [ | ||
'return_format' => 'ID' | ||
]) | ||
->addText('title') | ||
->addTextarea('content') | ||
->addLink('link'); | ||
|
||
return $fields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use StoutLogic\AcfBuilder\FieldsBuilder; | ||
|
||
$fields = new FieldsBuilder('quicklinks'); | ||
|
||
$fields | ||
->addRepeater('quicklinks') | ||
->addFields(get_field_partial('components.button')) | ||
->endRepeater(); | ||
|
||
return $fields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use StoutLogic\AcfBuilder\FieldsBuilder; | ||
|
||
$fields = new FieldsBuilder('slider'); | ||
|
||
$fields | ||
->addRepeater('slides') | ||
->addImage('background_image', [ | ||
'required' => true, | ||
'return_format' => 'ID' | ||
]) | ||
->addText('title') | ||
->addTextarea('description') | ||
->addLink('link') | ||
->endRepeater(); | ||
|
||
return $fields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use StoutLogic\AcfBuilder\FieldsBuilder; | ||
|
||
$fields = new FieldsBuilder('tabs'); | ||
|
||
$fields | ||
->addRepeater('tabs') | ||
->addText('title')->setRequired() | ||
->addWysiwyg('content')->setRequired() | ||
->endRepeater(); | ||
|
||
return $fields; |
Oops, something went wrong.