Forminator Guestbook
<?php | |
class GuestBook { | |
private $form_id = '1035'; // gunakan ID form Anda | |
private $page_slug = 'buku-tamu'; // pastikan sesuai dengan slug halaman Anda | |
public function __construct(){ | |
add_action( 'wp_ajax_guestbook_entries', [$this, 'get_guestbook_entries' ] ); | |
add_action( 'wp_ajax_nopriv_guestbook_entries', [$this, 'get_guestbook_entries' ] ); | |
add_action( 'wp_enqueue_scripts', [$this, 'set_styling' ], 999 ); | |
add_action( 'forminator_after_form_render', [$this, 'custom_script_after_form_render' ], 10, 5 ); | |
add_shortcode( 'guestbook_entries', [$this, 'solusipress_guestbook_entries' ] ); | |
} | |
public function set_styling() { | |
if( is_page( $this->page_slug ) ) { | |
?> | |
<style type="text/css"> | |
.guestbook-entries ul, .guestbook-entries ul li { | |
margin:0; | |
list-style: none; | |
} | |
.guestbook-entries ul li { | |
margin-bottom: 10px; | |
border-bottom: 1px solid #ccc; | |
} | |
.guestbook-entries blockquote { | |
padding: 10px; | |
margin: 0px; | |
} | |
.guestbook-entries .entry-time, | |
.guestbook-entries .entry-sender { | |
font-size: 0.90em; | |
} | |
</style> | |
<?php | |
} | |
} | |
function solusipress_guestbook_entries( $atts ) { | |
$html = '<div class="guestbook-entries">'; | |
$html .= $this->get_guestbook_entries(true); | |
$html .= '</div>'; | |
return $html; | |
} | |
function get_guestbook_entries( $in_shortcode=false ) { | |
if( class_exists( 'Forminator_Form_Entry_Model' ) ) { | |
$total_entries = Forminator_Form_Entry_Model::count_entries( $this->form_id ); | |
$count = 0; | |
$pagination = [ | |
'page' => !isset( $_GET['page'] ) ? 1:$_GET['page'], | |
'more' => false | |
]; | |
$page = intval( $pagination['page'] ); | |
$offset = 0; | |
$limit = 5; | |
if( $page > 1 ) { | |
$offset = ($page - 1) * $limit; | |
} | |
$total_rendered = $page * $limit; | |
if( $total_entries > $total_rendered ) { | |
$pagination['more'] = true; | |
} | |
$args = [ | |
'form_id' => $this->form_id, | |
'per_page' => $limit, | |
'offset' => $offset, | |
]; | |
$entries = Forminator_Form_Entry_Model::query_entries($args, $count); | |
ob_start(); ?> | |
<ul> | |
<?php foreach( $entries as $entry ): ?> | |
<li> | |
<span class="entry-time"><?php echo $entry->time_created; ?></span> | |
<div class="entry-content"> | |
<blockquote><?php echo $entry->meta_data['textarea-1']['value'] ?></blockquote> | |
<div class="entry-sender"> | |
<?php echo $entry->meta_data['name-1']['value'] ?>, <?php echo $entry->meta_data['text-1']['value'] ?> | |
</div> | |
</div> | |
</li> | |
<?php endforeach; ?> | |
</ul> | |
<?php | |
if( $pagination['more'] ) { | |
echo '<a href="#" id="guestbook-pagination" data-page="' . ($pagination['page']+1) . '">Load More...</a>'; | |
} | |
$html = ob_get_contents(); | |
ob_end_clean(); | |
if( $in_shortcode ) | |
return $html; | |
else { | |
echo str_replace( "\n", "", str_replace( "\t", "", $html ) ); | |
die(); | |
} | |
} | |
} | |
public function custom_script_after_form_render( $form_id, $form_type, $post_id, $form_fields, $form_settings ){ | |
if( $form_id == $this->form_id ): | |
?> | |
<script type="text/javascript"> | |
( function( $ ){ | |
$(document).ready( function(){ | |
$( 'body' ).on( 'forminator:form:submit:success', function( formData ) { | |
$.ajax( { | |
'url':'<?php echo admin_url( 'admin-ajax.php' ); ?>?action=guestbook_entries', | |
'type':'POST', | |
'dataType': 'html', | |
'success': function(response) { | |
$( '.guestbook-entries' ).html( response ); | |
} | |
} ); | |
} ); | |
$( 'body' ).on( 'click', '#guestbook-pagination', function(evt){ | |
evt.preventDefault(); | |
var page = $(this).attr('data-page'); | |
$( '#guestbook-pagination' ).html( "Loading..." ); | |
$.ajax( { | |
'url':'<?php echo admin_url( 'admin-ajax.php' ); ?>?action=guestbook_entries&page=' + page, | |
'type':'POST', | |
'dataType': 'html', | |
'success': function(response) { | |
$( '#guestbook-pagination' ).remove(); | |
$( '.guestbook-entries' ).append( response ); | |
} | |
} ); | |
} ); | |
} ); | |
} )(jQuery); | |
</script> | |
<?php | |
endif; | |
} | |
} | |
new GuestBook(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment