Skip to content

Instantly share code, notes, and snippets.

@webdevid
Created December 11, 2020 03:51
Show Gist options
  • Save webdevid/b568555302ee5b2c8d7797e86080a4f6 to your computer and use it in GitHub Desktop.
Save webdevid/b568555302ee5b2c8d7797e86080a4f6 to your computer and use it in GitHub Desktop.
contoh block dace dengan pagination
/**
* Block Dace update
* 8/20/2019, 10:44:12 AM
*/
Block::make( __( 'Block Dace List' ) )
->add_tab( __( 'Block Dace List' ), array(
Field::make( 'text', 'block_title', __( 'Title' ) )->set_default_value('Open position'),
Field::make( 'rich_text', 'block_desc', __( 'Description' ) ),
Field::make( 'select', 'block_column', __( 'Column' ) )
->set_options( array(
'' => '-',
'2' => '2',
'3' => '3',
'4' => '4',
) )->set_width( 30 ),
) )
->add_tab( __( 'Data Static' ),
array(
Field::make( 'checkbox', 'enable_custome', 'use this data' ),
Field::make( 'complex', 'block_custome_source' ,' ')
->add_fields( 'source_custome',
array(
Field::make( 'checkbox', 'source_enable', __( 'Show this content' ) )->set_option_value( 'false' )->set_width( 50 ),
Field::make( 'select', 'content_type', __( 'Content type' ) )
->set_options( array(
'' => '-',
'publication' => 'Publication',
'blog' => 'Blog',
'video' => 'Video',
'photo' => 'Photo',
'podcast' => 'Podcast',
'presentation' => 'Presentation',
'project' => 'Project',
'partner' => 'Partner',
) )->set_width( 50 ),
Field::make( 'image', 'source_image','Background image' )->set_value_type( 'url' ),
Field::make( 'text', 'source_title', 'Title' ),
Field::make( 'textarea', 'source_desc', 'Description' ),
Field::make( 'text', 'source_cat', 'Category' )->set_width( 30 ),
Field::make( 'text', 'source_author', 'Author' )->set_width( 30 ),
Field::make( 'text', 'source_year', 'Year' )->set_width( 30 ),
Field::make( 'text', 'source_link', 'Url/link' )
)
)
->set_header_template( '
<% if (source_title) { %>
static <%- source_title %>
<% }else{ %>
Source Custome
<% } %>
' ),
)
)
->add_tab( __( 'Dace filter' ), array(
Field::make( 'text', 'filter_keyword', __( 'Keyword' ) ),
Field::make( 'text', 'filter_topic', __( 'Topic' ) )->set_width( 50 ),
Field::make( 'text', 'filter_team', __( 'Team' ) )->set_width( 50 ),
Field::make( 'text', 'filter_pmo', __( 'Project code' ) )->set_width( 50 ),
Field::make( 'select', 'the_region', __( 'Region' ) )->set_options( region_list() )->set_width( 100 ),
Field::make( 'select', 'content_type', __( 'Content type' ) )
->set_options( array(
'' => '-',
'publication' => 'Publication',
'blog' => 'Blog',
'video' => 'Video',
'photo' => 'Photo',
'podcast' => 'Podcast',
'presentation' => 'Presentation',
'project' => 'Project',
'partner' => 'Partner',
) )->set_width( 30 ),
Field::make( 'select', 'dc_type', __( 'Dace type' ) )
->set_options( array(
'' => '-',
'Strategy documents' => 'Strategy documents',
'Brochure' => 'Brochure',
'Factsheet' => 'Factsheet',
'Annual Report' => 'Annual Report',
'Financial Statements OR Financial Statement' => 'Financial Statements',
'Corporate Publication' => 'Corporate Publication',
) )->set_width( 30 ),
Field::make( 'select', 'layout', __( 'Layout' ) )
->set_options( array(
'' => '-',
'grid' => 'grid',
'list' => 'list',
) )->set_width( 30 ),
Field::make( 'select', 'dc_lang', __( 'Dace Language' ) )
->set_options( array(
'' => '-',
'English' => 'English',
'Spanish' => 'Spanish',
'Indonesian'=>'Indonesian',
'French'=>'French',
) )->set_width( 50 ),
Field::make( 'text', 'author', __( 'Author' ) )->set_width( 50 ),
Field::make( 'text', 'filter_ids', __( 'Dace Origin IDs' ) )->set_width( 50 ),
Field::make( 'text', 'order_by', __( 'Order by' ) )->set_width( 50 ),
Field::make( 'checkbox', 'hide_filter', __( 'Hide filter sort' ) )->set_option_value( 'false' )->set_width( 30 ),
))
->add_tab( __( 'Link More' ), array(
Field::make( 'text', 'block_link_title', __( 'Title' ) )->set_default_value('Read more'),
Field::make( 'text', 'block_link_url', __( 'URL/Link' ) )->set_default_value('https://www.cifor.org'),
))
->add_tab( __( 'Style' ), array(
Field::make( 'text', 'block_id', __( 'Block ID CSS' ) ),
Field::make( 'text', 'block_class', __( 'Block Class CSS' ) ),
Field::make( 'textarea', 'block_css_code', __( 'CSS Code' ) ),
))
->set_category( 'cifor-block', __( 'CIFOR Dace List' ), '' )
->set_render_callback( function ( $block ) {
wp_reset_postdata();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
if(isset($block['the_region'])){
$arg['region'] = $block['the_region'];
}
$arg = array();
if($block['content_type']!=''){
$arg['content_type'] = $block['content_type'];
}
if($block['dc_type']!=''){
$arg['dc_type'] = urlencode($block['dc_type']);
}
if(isset($block['author'])){
if($block['author']!=''){
$arg['author']=urlencode($block['author']);
}
}
if(isset($block['filter_keyword'])){
if($block['filter_keyword']!=''){
$keywords = explode(",",$block['filter_keyword']);
if(count($keywords) > 1){
$keywords = implode(" OR ",$keywords);
}else{
$keywords = $block['filter_keyword'];
}
$arg['keywords']= $keywords;
}
}
if(isset($block['filter_ids'])){
if($block['filter_ids']!=''){
$ids = explode(",",$block['filter_ids']);
if(count($ids) > 1){
$ids = implode(" OR ",$ids);
}else{
}
$arg['origin_id']= $ids;
}
}
if(isset($block['filter_topic'])){
$arg['topic'] = urlencode($block['filter_topic']);
}
if(isset($block['filter_team'])){
$arg['team'] = urlencode($block['filter_team']);
}
if(isset($block['filter_pmo'])){
$arg['pmo_code'] = $block['filter_pmo'];
}
if(isset($block['filter_region'])){
$arg['region'] = $block['filter_region'];
}
if(isset($_GET['sort'])){
$arg['order_by'] = $_GET['sort'];
}else{
$arg['order_by'] = '-date';
}
if(isset($block['dc_lang'])){
$arg['language'] = $block['dc_lang'];
}
if(isset($paged)){
$paged = $paged;
$page = $paged;
if ( empty( $page ) ) {
$page = '0';
} else {
$page = ( $paged - 1 ) * 12;//get_option( 'posts_per_page' );
}
$row = 12;//get_option( 'posts_per_page' );
$arg['limit'] = $row;
$arg['offset'] = $page;
}
$row = isset($_GET['display']) ? $_GET['display'] : 15;
$arg['limit'] = $row;
if(isset($_GET['sort'])) {
$sort = $_GET['sort'];
}else{
$sort = '-output';
}
if($block['content_type']=='partner'){
$partner=0;
if($block['filter_team']!='') {
$partner = $block['filter_team'];
}
$region='';
if(isset($block['the_region'])) {
if($block['the_region']!='') {
$region = '/'.$block['the_region'];
}
}
$url = DACE_BASE_URI.'output/partner/'.$partner.$region.'?rows=200&start=0&sort='.$sort;
$file = file_get_contents($url);
$json = json_decode($file);
foreach($json as $j) {
$donor =getPartnerOne($j->partner_id[0]);
if($donor!=null) {
$p[] = (object) [
'id'=>$donor->slug,
'origin_id'=>[$donor->slug],
'title'=>$donor->title,
'order_date'=>[$donor->date],
'published_date'=>[$donor->date],
'thumbnail'=>[$donor->image],
'content_type'=>['partner'],
];
}
}
$this_page = max( 1, get_query_var('paged'));
$display=15;
if(isset($_GET['display'])) {
$display=$_GET['display'];
}
$start_page =($this_page-1)*$display;
$result = [
'type'=>"online",
'step'=>"step-on",
'url'=>$url,
'result'=> (object) [
'response'=> (object) [
'numFound'=>count($p),
'start'=>$start_page,
'docs'=> array_slice($p,$start_page,$display)
]
]
];
}else{
if(isset($block['the_region'])){
$arg['the_region'] = $block['the_region'];
}
$result = dace_json($arg);
}
$baseUrl = home_url();
$totalResults = $result['result']->response->numFound;
$resultsPerPage = $row;
$totalPages = ceil($totalResults/$resultsPerPage);
$block_id = isset($block['block_id']) ? $block['block_id'] : '';
$block_class = isset($block['block_class']) ? $block['block_class'] : '';
$css_code = isset($block['block_css_code']) ? $block['block_css_code'] : '';
$block_link_url = isset($block['block_link_url']) ? $block['block_link_url'] : '';
echo '<style>
.dace-list .col-md-4{ margin-bottom:30px;}
'.$css_code.'
</style>';
$parent_id = wp_get_post_parent_id(get_the_ID());
$display = 'Displaying page '.$paged.' of '.$totalPages;
echo '<div id="' .$block_id. '" class="' .$block_class. ' m-section m-margin-bottom-50 dace-data-query">';
if($block['block_title']){
//echo '<h2 class="m-weight-700 lead-34">' .$block['block_title']. '</h2>';
echo '
<div class="row middle-md m-margin-bottom-20">';
if($block_link_url){
$block_link_title = isset($block['block_link_title']) ? $block['block_link_title'] : 'View all';
echo '<div class="col-md-6 col-xs-7">
<h2 class="h-dace m-weight-700 has-icon m-margin-bottom-0">
<span>'.$block['block_title'].'</span>
</h2>
</div>';
echo '
<div class="col-md-6 col-xs-5">
<div class="text-right">
<a class="h-dace-link link-has-border-dark m-weight-700 text--sm" href="'.$block['block_link_url'].'">'.$block_link_title.'</a>
</div>
</div>';
}else{
echo '
<div class="col-md-6 col-xs-7">
<h2 class="h-dace m-weight-700 has-icon m-margin-bottom-0">
<span>'.$block['block_title'].'</span>
</h2>
</div>
';
}
echo'
</div>';
}
if(!isset($block['hide_filter'])){
?>
<div class="columns-10">
<div class="col-5">
<p class="m-weight-700 lead-12"><?php echo $display;?></p>
</div>
<?php
if($block['content_type']=='partner') {
echo get_template_part('templates/_parts/element/_list','search-partner');
} else {
echo get_template_part('templates/_parts/element/_list','search');
}
?>
</div>
<?php
}
$numOfCols = isset($block['block_column']) ? $block['block_column'] : 3;
$numOfCols = $numOfCols > 0? $numOfCols : 3;
$rowCount = 0;
$bootstrapColWidth = 12 / $numOfCols;
$block_column = $bootstrapColWidth;
if(isset($_GET['layout'])){
if($_GET['layout']=='list'){
echo '<div class="dace-list content-type-list">';
}elseif($_GET['layout']=='grid'){
echo '<div class="dace-list row m-margin-bottom-50">';
};
}else{
echo '<div class="dace-list row m-margin-bottom-50">';
}
foreach ($result['result']->response->docs as $row){
$row = dace_single_loop($row);
(isset($row->project_start_date))? $project_start_date = $row->project_start_date: $project_start_date=null;
(isset($row->country))? $country= $row->country: $country=null;
$image = dace_image(strtolower($row->content_type),$row->thumbnail,$row->files);
$link = dace_url(strtolower($row->content_type),$row->origin_id);
$target = '';
$arg = array(
'id'=>$row->origin_id,
'title'=>$row->title,
'link'=>$link,
'image'=>$image,
'year'=> $row->published_year,
'type'=> strtolower($row->content_type),
'date'=> $row->published_date,
'authors'=> $row->authors,
'topic'=> $row->topic,
'country'=>$country,
'project_status'=>$row->project_status,
'project_start_date'=> $project_start_date,
'col'=>$block_column,
);
if(isset($_GET['layout'])){
if($_GET['layout']=='list'){
echo dace_template_type_loop_list(strtolower($row->content_type),$arg);
$rowCount++;
}elseif($_GET['layout']=='grid'){
echo dace_template_type_loop(strtolower($row->content_type),$arg);
$rowCount++;
if($rowCount % $numOfCols == 0){
//echo '</div><div class="row m-margin-bottom-50">';
}
}
}else{
echo dace_template_type_loop(strtolower($row->content_type),$arg);
$rowCount++;
if($rowCount % $numOfCols == 0){
//echo '</div><div class="row m-margin-bottom-50">';
}
}
}// end foreach
//
// handle static content
//
if($block['enable_custome']){
$data_static = '';
$data_static_row = 0;
foreach($block['block_custome_source'] as $ds){
$arg = dataStaticArr($ds,$block_column);
$data_static_row++;
if($_GET['layout']=='list'){
$data_static .= dace_template_type_loop_list(strtolower($ds['content_type']),$arg);
}else{
$data_static .= dace_template_type_loop(strtolower($ds['content_type']),$arg);
}
}
echo $data_static;
$rowCount++;
if($rowCount % $numOfCols == 0){
//echo '</div><div class="row m-margin-bottom-50">';
}
}
//
// end handle static content
//
?>
</div>
<?php
/**
* 8/22/2019, 12:47:21 PM
* pagination use wp default pagination
*/
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $totalPages
) );
echo '</div>';
wp_reset_postdata();
});
// =======
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment