Plugin Directory

Changeset 2349030


Ignore:
Timestamp:
07/30/2020 11:14:30 AM (6 years ago)
Author:
ankgne
Message:

1.1.0 export functionality added

Location:
add-entries-functionality-to-wpforms
Files:
14 edited
1 copied

Legend:

Unmodified
Added
Removed
  • add-entries-functionality-to-wpforms/tags/1.1.0/add-wpform-entries.php

    r2345376 r2349030  
    77  Author URI:http://aaivatech.com/
    88  COntributors:
    9   Version: 1.0.0
     9  Version: 1.1.0
    1010  @copyright   Copyright (c) 2020, Ankur Khurana
    1111  @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
     
    2121     * @var string
    2222     */
    23     public $ank_wpforms_entry_version = '1.0.0';
     23    public $ank_wpforms_entry_version = '1.1.0';
    2424
    2525    /**
     
    115115        include_once ANK_WPFORM_ENTRY_BASE_DIR . '/includes/admin/entry/entries-db.php';
    116116        include_once ANK_WPFORM_ENTRY_BASE_DIR . '/includes/admin/entry/entries-page.php';
     117        include_once ANK_WPFORM_ENTRY_BASE_DIR . '/includes/exporter/export-csv.php';
    117118    }
    118119
     
    270271//main call starts from here
    271272ank_wpforms_entry();
     273
     274/*
     275 *  Displays update information for a plugin.
     276 */
     277function ank_wpforms_entry_update_message( $data, $response )
     278{
     279    var_dump("test");
     280    if(isset( $data['upgrade_notice']))
     281    {
     282        printf(
     283            '<div class="update-message ">%s</div>',
     284            wpautop ($data['upgrade_notice'])
     285        );
     286    }
     287}
     288add_action( 'in_plugin_update_message-add-wpform-entries/add-wpform-entries.php', 'ank_wpforms_entry_update_message', 10, 2 );
  • add-entries-functionality-to-wpforms/tags/1.1.0/includes/admin/entry/entries-db.php

    r2345376 r2349030  
    236236     *
    237237     */
    238     public function get_entries( $form_id, $page, $per_page ) {
     238    public function get_entries( $form_id, $page = '', $per_page = '' ) {
    239239
    240240        $temp_entry = array();
    241241
    242         $entries = parent::get_all_records( $form_id, $page, $per_page);
     242        $entries = parent::get_all_records( $form_id, $page, $per_page );
    243243
    244244        foreach ( $entries as $entry ) {
  • add-entries-functionality-to-wpforms/tags/1.1.0/includes/admin/entry/entries-page.php

    r2345376 r2349030  
    1717    const SLUG = 'ank-wpforms-entries';
    1818
     19    const EXPORT_BUTTON = 'Export Entries';
     20
    1921    /**
    2022     * Primary class constructor.
     
    2628        // Maybe load entries page.
    2729        add_action( 'admin_init', array( $this, 'init' ) );
     30
     31        // Maybe load entries page.
     32        add_action( 'admin_init', array( $this, 'catch_export_request' ) );
    2833
    2934        // Setup screen options. Needs to be here as admin_init hook it too late.
     
    6368
    6469    /**
     70     * Determine if the request has come exporting entries.
     71     *
     72     * @since 1.1.0
     73     */
     74    public function catch_export_request() {
     75        // Check what page we are on.
     76        $page = isset( $_GET['page'] ) ? \sanitize_key( \wp_unslash( $_GET['page'] ) ) : '';
     77
     78        // Export only if we are on right page and export is requested.
     79        if ( self::SLUG === $page && $_GET['export'] === self::EXPORT_BUTTON ) {
     80            $user_can_export = ank_wpforms_entries_user_permission();
     81
     82            if ( $user_can_export ) {
     83                //core processing on CSV export happens here
     84                $export = new Ank_WPForms_Export();
     85                $export->process_export();
     86            } else {
     87                if ( is_admin() ) {
     88                    //Admin notice to mention about permissions required for exporting
     89                    add_action( 'admin_notices', array( $this, 'export_lack_of_permission_admin_notice' ) );
     90                } else {
     91                    wp_redirect( wp_login_url() );
     92                }
     93            }
     94
     95        } else {
     96            return;
     97        }
     98
     99    }
     100
     101    /**
     102     * Admin notice to show if the user does not have access to export
     103     *
     104     * @since 1.1.0
     105     */
     106    public function export_lack_of_permission_admin_notice() {
     107        echo '<div class="notice notice-error"><p>' . __( 'By default, admin  are given access to export WPForm entries. ', 'ank-wpforms-entry' ) . '</p></div>';
     108    }
     109
     110
     111    /**
    65112     * Add per-page screen option to the Forms table.
    66113     *
     
    77124            'per_page',
    78125            array(
    79                 'label'   => esc_html__( 'Number of entries per page:', 'ank-wpforms-entry'),
     126                'label'   => esc_html__( 'Number of entries per page:', 'ank-wpforms-entry' ),
    80127                'option'  => 'ank_entries_per_page',
    81128                'default' => apply_filters( 'ank_entries_per_page', 20 ),
     
    123170        <div id="wpforms-overview" class="wrap wpforms-admin-wrap">
    124171
    125             <?php
    126             $entries_table = new Ank_WPForms_Entries_Table();
    127             ?>
     172            <?php
     173            $entries_table = new Ank_WPForms_Entries_Table();
     174            ?>
    128175            <h1 class="page-title">
    129                 <?php esc_html_e( 'Contact form entries' , 'ank-wpforms-entry' ); ?>
    130                 <?php
    131                 if($entries_table->default_form_title ){
    132                     esc_html_e( " for " . $entries_table->default_form_title);
     176                <?php esc_html_e( 'Contact form entries', 'ank-wpforms-entry' ); ?>
     177                <?php
     178                if ( $entries_table->default_form_title ) {
     179                    esc_html_e( " for " . $entries_table->default_form_title );
    133180                }
    134181                ?>
     
    137184            <?php
    138185            //prepare entries only if the fields/columns for selected form are defined
    139             //no items message is displayed if no fields are present for the form
     186            //no items message is displayed if no fields are present for the form
    140187            if ( $entries_table->fields ) {
    141188                $entries_table->prepare_items();
  • add-entries-functionality-to-wpforms/tags/1.1.0/includes/admin/entry/entries-table.php

    r2345376 r2349030  
    8181     */
    8282    public function get_columns() {
    83         ;
     83
    8484        $columns = array();
    8585
     
    232232                    echo $output;
    233233                    submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
     234                    submit_button( __( Ank_WPForms_Entries::EXPORT_BUTTON ), 'primary', 'export', false, array( 'id' => 'export-request-submit' ) );
    234235                }
    235236            }
     
    257258     */
    258259    protected function get_form_fields() {
    259         //TODO: Add option to restrict columns to certain form fields only
     260        //TODO: Add option to restrict columns to certain form fields only
    260261        $fields       = wpforms_get_form_fields( $this->default_form_id );
    261262        $this->fields = $fields;
     
    399400        do_action( 'ank_wpforms_entries_admin_overview_after_rows', $this );
    400401    }
     402
    401403}
    402404
  • add-entries-functionality-to-wpforms/tags/1.1.0/includes/class-db.php

    r2345376 r2349030  
    9898     *
    9999     */
    100     public function get_all_records( $form_id, $page, $per_page ) {
    101 
    102         global $wpdb;
    103 
    104         $pgstrt = absint( ( $page - 1 ) * $per_page ) . ', ';
    105         $limits = ' LIMIT ' . $pgstrt . $per_page;
     100    public function get_all_records( $form_id, $page='', $per_page='' ) {
     101
     102        global $wpdb;
     103        $limits = "";
     104
     105        //If the page is coming as blank then do not set limit
     106        if ( ! empty( $page ) ) {
     107            $pgstrt = absint( ( $page - 1 ) * $per_page ) . ', ';
     108            $limits = ' LIMIT ' . $pgstrt . $per_page;
     109        }
    106110
    107111        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
     
    123127     *
    124128     */
    125     public function get_count_all_records ( $form_id ) {
     129    public function get_count_all_records( $form_id ) {
    126130
    127131        global $wpdb;
  • add-entries-functionality-to-wpforms/tags/1.1.0/includes/functions.php

    r2345376 r2349030  
    5454        $data = '';
    5555    } else {
    56         $data= json_decode( $decoded, true );
     56        $data = json_decode( $decoded, true );
    5757    }
    5858
    5959    return $data;
    6060}
     61
     62/**
     63 * Validate user for permissions
     64 *
     65 *
     66 * @since 1.1.0
     67 *
     68 */
     69function ank_wpforms_entries_user_permission() {
     70
     71    // Check if user has rights to export
     72    $current_user        = wp_get_current_user();
     73    $current_user->roles = apply_filters( 'ank_wpforms_entries_add_user_roles', $current_user->roles );
     74    $current_user->roles = array_unique( $current_user->roles );
     75
     76    $user_can_export              = false;
     77    $roles_with_export_permission = apply_filters( 'ank_wpforms_entries_user_export_permission_roles', array( 'administrator' ) );
     78
     79    if ( $current_user instanceof WP_User ) {
     80        $can_users = array_intersect( $roles_with_export_permission, $current_user->roles );
     81        if ( ! empty( $can_users ) || is_super_admin( $current_user->ID ) ) {
     82            $user_can_export = true;
     83        }
     84    }
     85
     86    return $user_can_export;
     87}
  • add-entries-functionality-to-wpforms/tags/1.1.0/readme.txt

    r2345396 r2349030  
    11=== Add entries functionality to WPForms ===
    22Contributors: ankgne
    3 Tags: entries, wprforms, entry, wpform
     3Tags: entries, wprforms, entry, wpform, export entries, export WPForm entries in CSV, CSV, export
    44Requires at least: 1.0.0
    55Tested up to: 5.4.2
    66Requires PHP: 7.0.33
     7Stable tag: 1.1.0
    78License: GNU General Public License v2.0 or later
    89
     
    39402. Entries are also accessible form WPForms overview page
    4041
     42
     43== Upgrade Notice ==
     44
     45= 1.1.0 =
     46* Added functionality of exporting entries of selected WPForm in CSV format
     47
  • add-entries-functionality-to-wpforms/trunk/add-wpform-entries.php

    r2345376 r2349030  
    77  Author URI:http://aaivatech.com/
    88  COntributors:
    9   Version: 1.0.0
     9  Version: 1.1.0
    1010  @copyright   Copyright (c) 2020, Ankur Khurana
    1111  @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
     
    2121     * @var string
    2222     */
    23     public $ank_wpforms_entry_version = '1.0.0';
     23    public $ank_wpforms_entry_version = '1.1.0';
    2424
    2525    /**
     
    115115        include_once ANK_WPFORM_ENTRY_BASE_DIR . '/includes/admin/entry/entries-db.php';
    116116        include_once ANK_WPFORM_ENTRY_BASE_DIR . '/includes/admin/entry/entries-page.php';
     117        include_once ANK_WPFORM_ENTRY_BASE_DIR . '/includes/exporter/export-csv.php';
    117118    }
    118119
     
    270271//main call starts from here
    271272ank_wpforms_entry();
     273
     274/*
     275 *  Displays update information for a plugin.
     276 */
     277function ank_wpforms_entry_update_message( $data, $response )
     278{
     279    var_dump("test");
     280    if(isset( $data['upgrade_notice']))
     281    {
     282        printf(
     283            '<div class="update-message ">%s</div>',
     284            wpautop ($data['upgrade_notice'])
     285        );
     286    }
     287}
     288add_action( 'in_plugin_update_message-add-wpform-entries/add-wpform-entries.php', 'ank_wpforms_entry_update_message', 10, 2 );
  • add-entries-functionality-to-wpforms/trunk/includes/admin/entry/entries-db.php

    r2345376 r2349030  
    236236     *
    237237     */
    238     public function get_entries( $form_id, $page, $per_page ) {
     238    public function get_entries( $form_id, $page = '', $per_page = '' ) {
    239239
    240240        $temp_entry = array();
    241241
    242         $entries = parent::get_all_records( $form_id, $page, $per_page);
     242        $entries = parent::get_all_records( $form_id, $page, $per_page );
    243243
    244244        foreach ( $entries as $entry ) {
  • add-entries-functionality-to-wpforms/trunk/includes/admin/entry/entries-page.php

    r2345376 r2349030  
    1717    const SLUG = 'ank-wpforms-entries';
    1818
     19    const EXPORT_BUTTON = 'Export Entries';
     20
    1921    /**
    2022     * Primary class constructor.
     
    2628        // Maybe load entries page.
    2729        add_action( 'admin_init', array( $this, 'init' ) );
     30
     31        // Maybe load entries page.
     32        add_action( 'admin_init', array( $this, 'catch_export_request' ) );
    2833
    2934        // Setup screen options. Needs to be here as admin_init hook it too late.
     
    6368
    6469    /**
     70     * Determine if the request has come exporting entries.
     71     *
     72     * @since 1.1.0
     73     */
     74    public function catch_export_request() {
     75        // Check what page we are on.
     76        $page = isset( $_GET['page'] ) ? \sanitize_key( \wp_unslash( $_GET['page'] ) ) : '';
     77
     78        // Export only if we are on right page and export is requested.
     79        if ( self::SLUG === $page && $_GET['export'] === self::EXPORT_BUTTON ) {
     80            $user_can_export = ank_wpforms_entries_user_permission();
     81
     82            if ( $user_can_export ) {
     83                //core processing on CSV export happens here
     84                $export = new Ank_WPForms_Export();
     85                $export->process_export();
     86            } else {
     87                if ( is_admin() ) {
     88                    //Admin notice to mention about permissions required for exporting
     89                    add_action( 'admin_notices', array( $this, 'export_lack_of_permission_admin_notice' ) );
     90                } else {
     91                    wp_redirect( wp_login_url() );
     92                }
     93            }
     94
     95        } else {
     96            return;
     97        }
     98
     99    }
     100
     101    /**
     102     * Admin notice to show if the user does not have access to export
     103     *
     104     * @since 1.1.0
     105     */
     106    public function export_lack_of_permission_admin_notice() {
     107        echo '<div class="notice notice-error"><p>' . __( 'By default, admin  are given access to export WPForm entries. ', 'ank-wpforms-entry' ) . '</p></div>';
     108    }
     109
     110
     111    /**
    65112     * Add per-page screen option to the Forms table.
    66113     *
     
    77124            'per_page',
    78125            array(
    79                 'label'   => esc_html__( 'Number of entries per page:', 'ank-wpforms-entry'),
     126                'label'   => esc_html__( 'Number of entries per page:', 'ank-wpforms-entry' ),
    80127                'option'  => 'ank_entries_per_page',
    81128                'default' => apply_filters( 'ank_entries_per_page', 20 ),
     
    123170        <div id="wpforms-overview" class="wrap wpforms-admin-wrap">
    124171
    125             <?php
    126             $entries_table = new Ank_WPForms_Entries_Table();
    127             ?>
     172            <?php
     173            $entries_table = new Ank_WPForms_Entries_Table();
     174            ?>
    128175            <h1 class="page-title">
    129                 <?php esc_html_e( 'Contact form entries' , 'ank-wpforms-entry' ); ?>
    130                 <?php
    131                 if($entries_table->default_form_title ){
    132                     esc_html_e( " for " . $entries_table->default_form_title);
     176                <?php esc_html_e( 'Contact form entries', 'ank-wpforms-entry' ); ?>
     177                <?php
     178                if ( $entries_table->default_form_title ) {
     179                    esc_html_e( " for " . $entries_table->default_form_title );
    133180                }
    134181                ?>
     
    137184            <?php
    138185            //prepare entries only if the fields/columns for selected form are defined
    139             //no items message is displayed if no fields are present for the form
     186            //no items message is displayed if no fields are present for the form
    140187            if ( $entries_table->fields ) {
    141188                $entries_table->prepare_items();
  • add-entries-functionality-to-wpforms/trunk/includes/admin/entry/entries-table.php

    r2345376 r2349030  
    8181     */
    8282    public function get_columns() {
    83         ;
     83
    8484        $columns = array();
    8585
     
    232232                    echo $output;
    233233                    submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
     234                    submit_button( __( Ank_WPForms_Entries::EXPORT_BUTTON ), 'primary', 'export', false, array( 'id' => 'export-request-submit' ) );
    234235                }
    235236            }
     
    257258     */
    258259    protected function get_form_fields() {
    259         //TODO: Add option to restrict columns to certain form fields only
     260        //TODO: Add option to restrict columns to certain form fields only
    260261        $fields       = wpforms_get_form_fields( $this->default_form_id );
    261262        $this->fields = $fields;
     
    399400        do_action( 'ank_wpforms_entries_admin_overview_after_rows', $this );
    400401    }
     402
    401403}
    402404
  • add-entries-functionality-to-wpforms/trunk/includes/class-db.php

    r2345376 r2349030  
    9898     *
    9999     */
    100     public function get_all_records( $form_id, $page, $per_page ) {
    101 
    102         global $wpdb;
    103 
    104         $pgstrt = absint( ( $page - 1 ) * $per_page ) . ', ';
    105         $limits = ' LIMIT ' . $pgstrt . $per_page;
     100    public function get_all_records( $form_id, $page='', $per_page='' ) {
     101
     102        global $wpdb;
     103        $limits = "";
     104
     105        //If the page is coming as blank then do not set limit
     106        if ( ! empty( $page ) ) {
     107            $pgstrt = absint( ( $page - 1 ) * $per_page ) . ', ';
     108            $limits = ' LIMIT ' . $pgstrt . $per_page;
     109        }
    106110
    107111        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
     
    123127     *
    124128     */
    125     public function get_count_all_records ( $form_id ) {
     129    public function get_count_all_records( $form_id ) {
    126130
    127131        global $wpdb;
  • add-entries-functionality-to-wpforms/trunk/includes/functions.php

    r2345376 r2349030  
    5454        $data = '';
    5555    } else {
    56         $data= json_decode( $decoded, true );
     56        $data = json_decode( $decoded, true );
    5757    }
    5858
    5959    return $data;
    6060}
     61
     62/**
     63 * Validate user for permissions
     64 *
     65 *
     66 * @since 1.1.0
     67 *
     68 */
     69function ank_wpforms_entries_user_permission() {
     70
     71    // Check if user has rights to export
     72    $current_user        = wp_get_current_user();
     73    $current_user->roles = apply_filters( 'ank_wpforms_entries_add_user_roles', $current_user->roles );
     74    $current_user->roles = array_unique( $current_user->roles );
     75
     76    $user_can_export              = false;
     77    $roles_with_export_permission = apply_filters( 'ank_wpforms_entries_user_export_permission_roles', array( 'administrator' ) );
     78
     79    if ( $current_user instanceof WP_User ) {
     80        $can_users = array_intersect( $roles_with_export_permission, $current_user->roles );
     81        if ( ! empty( $can_users ) || is_super_admin( $current_user->ID ) ) {
     82            $user_can_export = true;
     83        }
     84    }
     85
     86    return $user_can_export;
     87}
  • add-entries-functionality-to-wpforms/trunk/readme.txt

    r2345396 r2349030  
    11=== Add entries functionality to WPForms ===
    22Contributors: ankgne
    3 Tags: entries, wprforms, entry, wpform
     3Tags: entries, wprforms, entry, wpform, export entries, export WPForm entries in CSV, CSV, export
    44Requires at least: 1.0.0
    55Tested up to: 5.4.2
    66Requires PHP: 7.0.33
     7Stable tag: 1.1.0
    78License: GNU General Public License v2.0 or later
    89
     
    39402. Entries are also accessible form WPForms overview page
    4041
     42
     43== Upgrade Notice ==
     44
     45= 1.1.0 =
     46* Added functionality of exporting entries of selected WPForm in CSV format
     47
Note: See TracChangeset for help on using the changeset viewer.