namespace WPForms\Admin\Forms;
* Count forms in different views.
private $show_form_templates;
private function configuration() {
if ( ! empty( $this->views ) ) {
'title' => __( 'All', 'wpforms-lite' ),
'title' => __( 'Trash', 'wpforms-lite' ),
'get_var_value' => 'trash',
'post_status' => 'trash',
$this->show_form_templates = wpforms()->obj( 'forms_overview' )->overview_show_form_templates();
// Add Forms and Templates views if Show Templates setting is enabled.
if ( $this->show_form_templates ) {
$views = wpforms_array_insert(
'title' => __( 'Forms', 'wpforms-lite' ),
'get_var_value' => 'form',
'post_type' => 'wpforms',
'title' => __( 'Templates', 'wpforms-lite' ),
'get_var_value' => 'template',
'post_type' => 'wpforms-template',
// phpcs:disable WPForms.Comments.ParamTagHooks.InvalidParamTagsQuantity
* Filters configuration of the Forms Overview table views.
* Each view is the array with three elements:
* @param string $title View title.
* @param string $get_var URL query variable name.
* @param string $get_var_value URL query variable value.
* @param array $args Additional arguments to be passed to `wpforms()->obj( 'form' )->get()` method.
$this->views = apply_filters( 'wpforms_admin_forms_views_configuration', $views );
// phpcs:enable WPForms.Comments.ParamTagHooks.InvalidParamTagsQuantity
* Determine if the class is allowed to load.
private function allow_load(): bool {
// Load only on the `All Forms` admin page.
return wpforms_is_admin_page( 'overview' );
if ( ! $this->allow_load() ) {
$this->update_current_view();
$this->update_base_url();
private function hooks() {
add_filter( 'wpforms_overview_table_update_count', [ $this, 'update_count' ], 10, 2 );
add_filter( 'wpforms_overview_table_update_count_all', [ $this, 'update_count' ], 10, 2 );
add_filter( 'wpforms_overview_table_prepare_items_args', [ $this, 'prepare_items_args' ], 100 );
add_filter( 'wpforms_overview_row_actions', [ $this, 'row_actions_all' ], 9, 2 );
add_filter( 'wpforms_overview_row_actions', [ $this, 'row_actions_trash' ], PHP_INT_MAX, 2 );
add_filter( 'wpforms_admin_forms_search_search_reset_block_message', [ $this, 'search_reset_message' ], 10, 4 );
* Determine and save current view slug.
private function update_current_view() {
if ( ! is_array( $this->views ) ) {
$this->current_view = 'all';
foreach ( $this->views as $slug => $view ) {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
isset( $_GET[ $view['get_var'] ] ) &&
$view['get_var_value'] === sanitize_key( $_GET[ $view['get_var'] ] )
// phpcs:enable WordPress.Security.NonceVerification.Recommended
$this->current_view = $slug;
private function update_base_url() {
if ( ! is_array( $this->views ) ) {
$get_vars = wp_list_pluck( $this->views, 'get_var' );
$this->base_url = remove_query_arg( $get_vars );
public function get_current_view(): string {
return $this->current_view;
public function get_base_url(): string {
* Get view configuration by slug.
* @param string $slug View slug.
public function get_view_by_slug( string $slug ): array {
return $this->views[ $slug ] ?? []; // phpcs:ignore WPForms.Formatting.EmptyLineBeforeReturn.RemoveEmptyLineBeforeReturnStatement
* @param array $count Number of forms in different views.
* @param array $args Get forms arguments.
public function update_count( $count, $args ) {
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'post_status' => 'publish',
'post_type' => wpforms()->obj( 'form' )::POST_TYPES,
$args = array_merge( $args, $defaults );
$count['all'] = $this->get_all_items_count( $args );
$count['trash'] = $this->get_trashed_forms_count( $args );
// Count forms and templates separately only if Show Templates screen setting is enabled.
if ( $this->show_form_templates ) {
$count['forms'] = $this->get_forms_count( $args );
$count['templates'] = $this->get_form_templates_count( $args );
// Store in class property for further use.
* Get count of all items.
* May include only forms or both forms and form templates, depending on the
* Screen Options settings whether to show form templates or not.
* @param array $args Get forms arguments.
* @return int Number of forms and templates.
private function get_all_items_count( array $args ): int {
if ( ! $this->show_form_templates ) {
$args['post_type'] = 'wpforms';
$all_items = wpforms()->obj( 'form' )->get( '', $args );
return is_array( $all_items ) ? count( $all_items ) : 0;
* @param array $args Get forms arguments.
* @return int Number of published forms.
private function get_forms_count( array $args ): int {
$args['post_type'] = 'wpforms';
$forms = wpforms()->obj( 'form' )->get( '', $args );
return is_array( $forms ) ? count( $forms ) : 0;
* Get count of form templates.
* @param array $args Get forms arguments.
* @return int Number of published templates.
private function get_form_templates_count( array $args ): int {
$args['post_type'] = 'wpforms-template';
$templates = wpforms()->obj( 'form' )->get( '', $args );
return is_array( $templates ) ? count( $templates ) : 0;
* Get count of trashed items.
* May include only forms or both forms and form templates, depending on the
* Screen Options settings whether to show form templates or not.
* @param array $args Get forms arguments.
* @return int Number of trashed forms.
private function get_trashed_forms_count( array $args ): int {
if ( ! $this->show_form_templates ) {
$args['post_type'] = 'wpforms';
$args['post_status'] = 'trash';
$forms = wpforms()->obj( 'form' )->get( '', $args );
return is_array( $forms ) ? count( $forms ) : 0;
* Get counts of forms in different views.
public function get_count(): array {
* Prepare items arguments for list table.
* @param array $args Get multiple forms arguments.
public function prepare_items_args( $args ): array {
$view_args = $this->views[ $this->current_view ]['args'] ?? [];
if ( ! empty( $view_args ) ) {
$args = array_merge( $args, $view_args );
* Get forms from Trash when preparing items for list table.
* @depecated 1.8.8 The `prepare_items_args()` now handles all cases, uses `$this->views`.
* @param array $args Get multiple forms arguments.
public function prepare_items_trash( $args ) {
_deprecated_function( __METHOD__, '1.8.8 of the WPForms plugin' );
public function get_views(): array {
if ( ! is_array( $this->views ) ) {
foreach ( $this->views as $slug => $view ) {
$this->current_view !== 'trash' &&
empty( $this->count[ $slug ] )
$views[ $slug ] = $this->get_view_markup( $slug );
* Filters the Forms Overview table views links.
* @param array $views Views links.
* @param array $count Count forms in different views.
return apply_filters( 'wpforms_admin_forms_views_get_views', $views, $this->count );
* Generate single view item.
* @param string $slug View slug.
private function get_view_markup( string $slug ): string {