Last active
April 30, 2021 06:25
-
-
Save webgurus/d187c383c2c92155fdf8f753bff588bc to your computer and use it in GitHub Desktop.
Sage9 - ACF Flexible page builder controller
This file contains hidden or 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
<?php | |
namespace App\Controllers; | |
use Sober\Controller\Controller; | |
use WP_Query; | |
class App extends Controller | |
{ | |
// Add all acf fields into the view | |
protected $acf = true; | |
public function siteName() | |
{ | |
return get_bloginfo('name'); | |
} | |
public static function title() | |
{ | |
if (is_home()) { | |
if ($home = get_option('page_for_posts', true)) { | |
return get_the_title($home); | |
} | |
return __('Latest Posts', 'sage'); | |
} | |
if (is_archive()) { | |
return get_the_archive_title(); | |
} | |
if (is_search()) { | |
return sprintf(__('Search Results for %s', 'sage'), get_search_query()); | |
} | |
if (is_404()) { | |
return __('Not Found', 'sage'); | |
} | |
return get_the_title(); | |
} | |
/** | |
* Get 3 news item query | |
* @return array | |
*/ | |
public static function blogPostsLoop($post_ids = array()) { | |
$blogposts_query = array( | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'posts_per_page' => '3', | |
'showposts' => '3', | |
'post__in' => $post_ids, | |
// 'ignore_sticky_posts' => 1 | |
); | |
$blogposts_query = new WP_Query( $blogposts_query ); | |
return $blogposts_query; | |
} | |
// Create page builder for ACF Flexible Blocks | |
public function pageBuilder() { | |
$page_builder = get_field('content_blocks'); | |
$data = []; | |
$counters = []; // for counting same layout blocks on a page | |
if ($page_builder) { | |
foreach ($page_builder as $block) { | |
// update counter | |
if( !isset($counters[ $block['acf_fc_layout'] ] ) ) { | |
// initialize counter | |
$counters[ $block['acf_fc_layout'] ] = 1; | |
} | |
else { | |
// increase existing counter | |
$counters[ $block['acf_fc_layout'] ]++; | |
} | |
$this_block = [ | |
'block_type' => $block['acf_fc_layout'], | |
'block_counter' => $counters[$block['acf_fc_layout']], | |
]; | |
unset($block['acf_fc_layout']); | |
$this_block = (object) array_merge($this_block, $block); | |
array_push($data, $this_block); | |
} | |
$data = (object) $data; | |
return $data; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment