|  |  | 
|  | <?php
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | function wpf_entries_table( $atts ) {
 | 
|  |   
 | 
|  |     
 | 
|  |     $atts = shortcode_atts(
 | 
|  |         [
 | 
|  |             'id'     => '',
 | 
|  |             'user'   => '',
 | 
|  |             'fields' => '',
 | 
|  |             'number' => '',
 | 
|  |             'type'   => 'all', 
 | 
|  |             'sort'   => '',
 | 
|  |             'order'  => 'asc',
 | 
|  |         ],
 | 
|  |         $atts
 | 
|  |     );
 | 
|  |   
 | 
|  |     
 | 
|  |     
 | 
|  |     if ( empty( $atts[ 'id' ] ) || ! function_exists( 'wpforms' ) ) {
 | 
|  |         return;
 | 
|  |     }
 | 
|  |   
 | 
|  |     
 | 
|  |     $form = wpforms()->form->get( absint( $atts[ 'id' ] ) );
 | 
|  |   
 | 
|  |     
 | 
|  |     if ( empty( $form ) ) {
 | 
|  |         return;
 | 
|  |     }
 | 
|  |   
 | 
|  |     
 | 
|  |     $form_data = ! empty( $form->post_content ) ? wpforms_decode( $form->post_content ) : '';
 | 
|  |   
 | 
|  |     
 | 
|  |     $form_field_ids = isset( $atts[ 'fields' ] ) && $atts[ 'fields' ] !== '' ? explode( ',', str_replace( ' ', '', $atts[ 'fields' ] ) ) : [];
 | 
|  |   
 | 
|  |     
 | 
|  |     if ( empty( $form_field_ids ) ) {
 | 
|  |         $form_fields = $form_data[ 'fields' ];
 | 
|  |     } else {
 | 
|  |         $form_fields = [];
 | 
|  |         foreach ( $form_field_ids as $field_id ) {
 | 
|  |             if ( isset( $form_data[ 'fields' ][ $field_id ] ) ) {
 | 
|  |                 $form_fields[ $field_id ] = $form_data[ 'fields' ][ $field_id ];
 | 
|  |             }
 | 
|  |         }
 | 
|  |     }
 | 
|  |   
 | 
|  |     if ( empty( $form_fields ) ) {
 | 
|  |         return;
 | 
|  |     }
 | 
|  |   
 | 
|  |     
 | 
|  |     
 | 
|  |     $form_fields_disallow = apply_filters( 'wpforms_frontend_entries_table_disallow', [ 'divider', 'html', 'pagebreak', 'captcha' ] );
 | 
|  |   
 | 
|  |     
 | 
|  |     foreach ( $form_fields as $field_id => $form_field ) {
 | 
|  |         if ( in_array( $form_field[ 'type' ], $form_fields_disallow, true ) ) {
 | 
|  |             unset( $form_fields[ $field_id ] );
 | 
|  |         }
 | 
|  |     }
 | 
|  |   
 | 
|  |     $entries_args = [
 | 
|  |         'form_id' => absint( $atts[ 'id' ] ),
 | 
|  |     ];
 | 
|  |   
 | 
|  |     
 | 
|  |     if ( ! empty( $atts[ 'user' ] ) ) {
 | 
|  |         if ( $atts[ 'user' ] === 'current' && is_user_logged_in() ) {
 | 
|  |             $entries_args[ 'user_id' ] = get_current_user_id();
 | 
|  |         } else {
 | 
|  |             $entries_args[ 'user_id' ] = absint( $atts[ 'user' ] );
 | 
|  |         }
 | 
|  |     }
 | 
|  |   
 | 
|  |     
 | 
|  |     if ( ! empty( $atts[ 'number' ] ) ) {
 | 
|  |         $entries_args[ 'number' ] = absint( $atts[ 'number' ] );
 | 
|  |     }
 | 
|  |   
 | 
|  |     
 | 
|  |     if ( $atts[ 'type' ] === 'unread' ) {
 | 
|  |         $entries_args[ 'viewed' ] = '0';
 | 
|  |     } elseif( $atts[ 'type' ] === 'read' ) {
 | 
|  |         $entries_args[ 'viewed' ] = '1';
 | 
|  |     } elseif ( $atts[ 'type' ] === 'starred' ) {
 | 
|  |         $entries_args[ 'starred' ] = '1';
 | 
|  |     }
 | 
|  |   
 | 
|  |     
 | 
|  |     
 | 
|  |     
 | 
|  |     $entries = json_decode(json_encode(wpforms()->entry->get_entries( $entries_args )), true);
 | 
|  |   
 | 
|  |     if ( empty( $entries ) ) {
 | 
|  |         return '<p>No entries found.</p>';
 | 
|  |     }
 | 
|  |      
 | 
|  |     foreach($entries as $key => $entry) {
 | 
|  |         $entries[$key][ 'fields' ] = json_decode($entry[ 'fields' ], true);
 | 
|  |         $entries[$key][ 'meta' ] = json_decode($entry[ 'meta' ], true);
 | 
|  |     }
 | 
|  |      
 | 
|  |     if ( !empty($atts[ 'sort' ]) && isset($entries[0][ 'fields' ][$atts[ 'sort' ]] ) ) {
 | 
|  |         if ( strtolower($atts[ 'order' ]) == 'asc' ) {
 | 
|  |             usort($entries, function ($entry1, $entry2) use ($atts) {
 | 
|  |                 return strcmp($entry1[ 'fields' ][$atts[ 'sort' ]][ 'value' ], $entry2[ 'fields' ][$atts[ 'sort' ]][ 'value' ]);
 | 
|  |             });         
 | 
|  |         } elseif ( strtolower($atts[ 'order' ]) == 'desc' ) {
 | 
|  |             usort($entries, function ($entry1, $entry2) use ($atts) {
 | 
|  |                 return strcmp($entry2[ 'fields' ][$atts[ 'sort' ]][ 'value' ], $entry1[ 'fields' ][$atts[ 'sort' ]]['value']);
 | 
|  |             });
 | 
|  |         }
 | 
|  |     }
 | 
|  |      
 | 
|  |     ob_start();
 | 
|  |   
 | 
|  |     echo '<table class="wpforms-frontend-entries">';
 | 
|  |   
 | 
|  |         echo '<thead><tr>';
 | 
|  |   
 | 
|  |             
 | 
|  |             
 | 
|  |             foreach ( $form_fields as $form_field ) {
 | 
|  |   
 | 
|  |                 
 | 
|  |                 echo '<th>';
 | 
|  |                     echo esc_html( sanitize_text_field( $form_field[ 'label' ] ) );
 | 
|  |                 echo '</th>';
 | 
|  |             }
 | 
|  |   
 | 
|  |         echo '</tr></thead>';
 | 
|  |   
 | 
|  |         echo '<tbody>';
 | 
|  |   
 | 
|  |             
 | 
|  |             foreach ( $entries as $entry ) {
 | 
|  |   
 | 
|  |                 echo '<tr>';
 | 
|  |   
 | 
|  |                 $entry_fields = $entry[ 'fields' ];
 | 
|  |   
 | 
|  |                 foreach ( $form_fields as $form_field ) {
 | 
|  |   
 | 
|  |                     echo '<td>';
 | 
|  |   
 | 
|  |                         foreach ( $entry_fields as $entry_field ) {
 | 
|  |                             if ( absint( $entry_field[ 'id' ] ) === absint( $form_field[ 'id' ] ) ) {
 | 
|  |                                 echo apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $entry_field[ 'value' ] ), $entry_field, $form_data, 'entry-frontend-table' );
 | 
|  |                                 break;
 | 
|  |                             }
 | 
|  |                         }
 | 
|  |   
 | 
|  |                     echo '</td>';
 | 
|  |                 }
 | 
|  |   
 | 
|  |                 echo '</tr>';
 | 
|  |             }
 | 
|  |   
 | 
|  |         echo '</tbody>';
 | 
|  |   
 | 
|  |     echo '</table>';
 | 
|  |   
 | 
|  |     $output = ob_get_clean();
 | 
|  |   
 | 
|  |     return $output;
 | 
|  | }
 | 
|  | add_shortcode( 'wpforms_entries_table', 'wpf_entries_table' );
 | 
|  |  | 
|  |  | 
Comments