Created
October 24, 2018 20:01
-
-
Save feliciaceballos/326aa2b2237592847e35959c6d60aa13 to your computer and use it in GitHub Desktop.
Plugin Extension for Forminator to Display Forminator Form Entries in WordPress Dashboard Widget
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
<?php | |
/* | |
Plugin Name: Forminator Recent Entries Dashboard Widget | |
Plugin URI: https://premium.wpmudev.org/project/forminator-pro/ | |
Description: Display Forminator Form Entries in WordPress Dashboard Widget | |
Version: 0.1 | |
Author: Cvetan Cvetanov | |
Author URI: https://premium.wpmudev.org/profile/cvetan | |
*/ | |
class Forminator_Submissions_Dash_Widget { | |
private static $instance = null; // Here we store class instance | |
private $id = null; // Form ID that we will retrieve submissions from | |
private $limit = 5; // Number of displayed submissions in widget | |
public function __construct() { | |
// Construct method | |
} | |
/* | |
* Get class instance | |
* We will need that to instantiate our plugin class | |
*/ | |
public static function get_instance() { | |
if ( is_null( self::$instance ) ) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
/** | |
* Register the dashboard widget | |
*/ | |
public function register_widget() { | |
// We will register our widget here | |
} | |
} | |
add_action( 'forminator_loaded', 'forminator_dash_widget' ); | |
function forminator_dash_widget() { | |
// Widget should be added after `wp_dashboard_setup` hook called | |
add_action( 'wp_dashboard_setup', 'add_forminator_dash_widget' ); | |
} | |
function add_forminator_dash_widget() { | |
// Instantiate Forminator_Submissions_Dash_Widget class | |
$widget = Forminator_Submissions_Dash_Widget::get_instance(); | |
$widget->register_widget(); | |
} | |
public function register_widget() { | |
wp_add_dashboard_widget( | |
__CLASS__, | |
__( 'Forminator Submissions' ), // Widget title | |
array( $this, 'display' ), // Render callback method | |
array( $this, 'configure' ) // Configuration callback method | |
); | |
} | |
public function user_allowed() { | |
return true; // true value is hardcoded to allow all visitors to view the widget | |
} | |
private function get_default_options() { | |
return array( | |
'id' => $this->id, | |
'limit' => $this->limit, | |
); | |
} | |
public function get_options( $default = array() ) { | |
// Fetch ALL dashboard widget options from the db... | |
$opts = get_option( 'dashboard_widget_options' ); | |
// Get just our widget's options, or set default | |
$forminator_options = ( isset( $opts[ __CLASS__ ] ) ) ? $opts[ __CLASS__ ] : $default; | |
return $forminator_options; | |
} | |
public function display() { | |
// Check if user allowed to view the widget | |
if ( ! $this->user_allowed() ) { | |
echo esc_html( __( 'You are not allowed to view this widget content' ) ); | |
} else { | |
// Get widget options | |
$options = $this->get_options( $this->get_default_options() ); | |
$this->id = $options['id']; | |
$this->limit = $options['limit']; | |
// Get submissions | |
$this->get_submissions(); | |
} | |
} | |
protected function get_submissions() { | |
// Check if we have configured form ID else return | |
if ( empty( $this->id ) ) { | |
echo esc_html( __( 'Please configure which form to display its submissions.' ) ); | |
return false; | |
} | |
$module = null; | |
$entries = array(); | |
$module = Forminator_API::get_form( $this->id ); | |
// Check if form loaded successfully | |
if ( is_wp_error( $module ) ) { | |
$module = null; | |
} else { | |
$entries = Forminator_API::get_form_entries( $module->id ); | |
} | |
// Render submissions table | |
$this->render_form_submissions( $module, $entries ); | |
} | |
public function render_form_submissions( $form, $entries ) { | |
$field_labels = array(); | |
// Get fields labels | |
if ( ! is_null( $form ) ) { | |
if ( is_array( $form->fields ) ) { | |
foreach ( $form->fields as $field ) { | |
$field_labels[ $field->slug ] = $field->get_label_for_entry(); | |
} | |
} | |
} | |
?> | |
<h3><?php echo( is_null( $form ) ? esc_html( __( 'Not Found' ) ) : esc_html( $form->name ) ); ?></h3> | |
<table class="widefat fixed" cellspacing="0"> | |
<thead> | |
<tr> | |
<th id="id" class="manage-column id" scope="col"><?php esc_html_e( 'ID' ); ?></th> | |
<th id="entry" class="manage-column entry" scope="col"><?php esc_html_e( 'Entry' ); ?></th> | |
<th id="date" class="manage-column date" scope="col"><?php esc_html_e( 'Time' ); ?></th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
// set limit | |
$limit = count( $entries ); | |
if ( $limit > $this->limit ) { | |
$limit = $this->limit; | |
} | |
?> | |
<?php for ( $i = 0; $i < $limit; $i ++ ) : ?> | |
<?php | |
$entry = $entries[ $i ]; | |
?> | |
<tr class="<?php echo( ( $i % 2 ) ? 'alternate' : '' ); ?>"> | |
<td class="id"><?php echo esc_html( $entry->entry_id ); ?></td> | |
<td class="entry"> | |
<ul> | |
<?php foreach ( $entry->meta_data as $field_id => $meta ): ?> | |
<?php if ( isset( $field_labels[ $field_id ] ) ) : // only display entry with field label exist ?> | |
<?php $field_value = $meta['value']; ?> | |
<li> | |
<?php echo esc_html( $field_labels[ $field_id ] ); ?> : | |
<?php if ( is_array( $field_value ) ) : // show key too when its array value (multiname, multiple choices) ?> | |
<?php foreach ( $field_value as $key => $val ) : ?> | |
<?php echo esc_html( $key ); ?>: <?php echo esc_html( $val ); ?><br/> | |
<?php endforeach; ?> | |
<?php else: ?> | |
<?php echo esc_html( $field_value ); ?> | |
<?php endif; ?> | |
</li> | |
<?php endif; ?> | |
<?php endforeach; ?> | |
</ul> | |
</td> | |
<td class="date"> | |
<?php echo esc_html( $entry->time_created ); ?> | |
</td> | |
</tr> | |
<?php endfor; ?> | |
</tbody> | |
<tfoot> | |
<tr> | |
<th id="id" class="manage-column id" scope="col"><?php esc_html_e( 'ID' ); ?></th> | |
<th id="entry" class="manage-column entry" scope="col"><?php esc_html_e( 'Entry' ); ?></th> | |
<th id="date" class="manage-column date" scope="col"><?php esc_html_e( 'Time' ); ?></th> | |
</tr> | |
</tfoot> | |
</table> | |
<?php if ( ! is_null( $form ) ): ?> | |
<ul class=""> | |
<li class="all"> | |
<a href="<?php echo esc_url( admin_url( 'admin.php?page=forminator-entries&form_type=forminator_forms&form_id=' . $form->id ) ); ?>"><?php esc_html_e( 'View Submissions' ); ?> | |
<span class="count">(<span class="all-count"><?php echo esc_html( count( $entries ) ); ?></span>) | |
</span> | |
</a> | |
</li> | |
</ul> | |
<?php endif; ?> | |
<?php | |
} | |
public function configure() { | |
// Check if user allowed to view the widget | |
if ( ! $this->user_allowed() ) { | |
echo esc_html( __( 'You are not allowed to view this widget content' ) ); | |
} else { | |
$options = $this->get_options( $this->get_default_options() ); | |
$post_data = $_POST; | |
if ( isset( $post_data['id'] ) ) { | |
$options['id'] = $post_data['id']; | |
} | |
if ( isset( $post_data['limit'] ) ) { | |
$options['limit'] = $post_data['limit']; | |
} | |
$this->update_options( $options ); | |
?> | |
<label> | |
<?php esc_html_e( 'ID' ); ?> | |
<input type="text" class="widefat" name="id" value="<?php echo esc_attr( $options['id'] ); ?>"/> | |
</label> | |
<label> | |
<?php esc_html_e( 'Entries Limit' ); ?> | |
<input type="text" class="widefat" name="limit" value="<?php echo esc_attr( $options['limit'] ); ?>"/> | |
</label> | |
<hr/> | |
<?php | |
} | |
} | |
public function update_options( $options = array() ) { | |
// Fetch all dashboard widget options from the db... | |
$opts = get_option( 'dashboard_widget_options' ); | |
//Get just our widget's options, or set empty array | |
$forminator_options = ( isset( $opts[ __CLASS__ ] ) ) ? $opts[ __CLASS__ ] : array(); | |
// merge old one with new one | |
$opts[ __CLASS__ ] = array_merge( $forminator_options, $options ); | |
// update option | |
return update_option( 'dashboard_widget_options', $opts ); | |
} | |
public function reset_options() { | |
// Fetch ALL dashboard widget options from the db... | |
$opts = get_option( 'dashboard_widget_options' ); | |
//Get just our widget's options, or set default | |
unset( $opts[ __CLASS__ ] ); | |
// update option | |
return update_option( 'dashboard_widget_options', $opts ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment