Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created February 23, 2021 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/b60b51a7dfd5830b89409c9bb3d643cd to your computer and use it in GitHub Desktop.
Save wpmudev-sls/b60b51a7dfd5830b89409c9bb3d643cd to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: [Forminator] - User Submissions
* Plugin URI: https://premium.wpmudev.org/
* Description: Adds action link in users admin page to fetch submissions per user id. It requires a hidden field to be set and it should have the `{USER_ID}` as custom value
* Task: SLS-1682
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}
add_action(
'plugins_loaded',
function() {
if ( ! class_exists( 'WPMUDEV_Forminator_User_Submissions' ) ) {
class WPMUDEV_Forminator_User_Submissions {
private $user_id_field = 'hidden-1';
private $form_post_type = 'forminator_forms';
private static $_instance = null;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
private function first_form_id() {
return get_posts(
array(
'post_type' => $this->form_post_type,
'posts_per_page' => 1,
'order' => 'ASC',
'fields' => 'ids',
)
)[0];
}
public function __construct() {
add_filter( 'user_row_actions', array( $this, 'add_submissions_row_action' ), 10, 2 );
add_filter( 'forminator_field_hidden_field_value', array( $this, 'inject_user_id' ), 10, 4 );
add_filter( 'forminator_query_entries_where', array( $this, 'filter_entries_query' ), 10, 2 );
}
public function filter_entries_query( $where, $arg ) {
if ( isset( $_GET[ 'user_id' ] ) && is_numeric( $_GET[ 'user_id' ] ) ) {
$user_id = filter_input( INPUT_GET, 'user_id' , FILTER_VALIDATE_INT );
$where = preg_replace( '/AND entries.form_id = \d+ AND/', 'AND', $where );
$where .= " AND metas.meta_key=\"{$this->user_id_field}\" AND metas.meta_value=\"{$user_id}\"";
}
return $where;
}
public function inject_user_id( $value, $saved_value, $field, $hidden_object ) {
$user_id = is_user_logged_in() ? get_current_user_id() : '';
$value = str_replace( '{USER_ID}', $user_id, $value );
return $value;
}
public function add_submissions_row_action( $actions, $user ) {
// We need a form id to use in our link to the netries page.
// If the entries page does not have a form id it will not continue further to the query.
$form_id = $this->first_form_id();
$submissions_url = admin_url( "admin.php?page=forminator-entries&form_type=forminator_forms&form_id={$form_id}&user_id={$user->ID}" );
$actions['forminator-submissions'] = "<a class=\"forminator-submissions\" href=\"{$submissions_url}\">Submissions</a>";
return $actions;
}
}
WPMUDEV_Forminator_User_Submissions::get_instance();
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment