__( 'Entry', 'admin-optimizer' ), 'plural' => __( 'Entries', 'admin-optimizer' ), 'ajax' => false, ] ); $this->table_name = $wpdb->prefix . Form_Builder_Entries::TABLE_NAME; } /** * Get columns. * * @return array */ public function get_columns() { $columns = [ 'cb' => '', 'form_id' => __( 'Form', 'admin-optimizer' ), 'entry_data' => __( 'Entry Data', 'admin-optimizer' ), 'ip_address' => __( 'IP Address', 'admin-optimizer' ), 'date_created' => __( 'Date', 'admin-optimizer' ), 'actions' => __( 'Action', 'admin-optimizer' ), ]; if ( ! current_user_can( 'manage_options' ) ) { unset( $columns['cb'] ); } return $columns; } /** * Get sortable columns. * * @return array */ protected function get_sortable_columns() { $sortable_columns = [ 'form_id' => [ 'form_id', false ], 'date_created' => [ 'date_created', true ], ]; return $sortable_columns; } /** * Get bulk actions. * * @return array */ protected function get_bulk_actions() { if ( ! current_user_can( 'manage_options' ) ) { return []; } return [ 'bulk_delete' => __( 'Delete', 'admin-optimizer' ), ]; } /** * Define default column content. * * @param array|object $item Data item. * @param string $column_name Column name. * * @return string */ protected function column_default( $item, $column_name ) { switch ( $column_name ) { case 'ip_address': case 'date_created': return esc_html( $item[ $column_name ] ); default: return isset( $item[ $column_name ] ) ? esc_html( is_scalar( $item[ $column_name ] ) ? (string) $item[ $column_name ] : wp_json_encode( $item[ $column_name ] ) ) : ''; } } /** * Method for form_id column. * * @param array $item Data item. * * @return string */ protected function column_form_id( $item ) { $form_post = get_post( $item['form_id'] ); $form_name = $form_post ? $form_post->post_title : __( 'Deleted Form', 'admin-optimizer' ); return sprintf( '%s', esc_url( get_edit_post_link( $item['form_id'] ) ), esc_html( $form_name ) ); } /** * Method for entry_data column. * * @param array $item Data item. * * @return string */ protected function column_entry_data( $item ) { $data = json_decode( $item['entry_data'], true ); if ( ! is_array( $data ) ) { return esc_html( $item['entry_data'] ); } $output = []; foreach ( $data as $key => $value ) { $output[] = sprintf( '%s: %s', esc_html( $key ), $this->render_entry_value( $value, (string) $key, $item ) ); } return implode( '
', $output ); } /** * Render an entry value. * * @param mixed $value Entry value. * @param string $key Field name. * @param array $item Entry row. * * @return string */ protected function render_entry_value( $value, $key = '', array $item = [] ) { if ( is_array( $value ) ) { return implode( ', ', array_map( function ( $single_value ) use ( $key, $item ) { return $this->render_entry_value( $single_value, $key, $item ); }, $value ) ); } $attachment_id = Form_Builder_Entries::get_entry_attachment_id( $value, $key, $item ); $attachment_url = $attachment_id ? wp_get_attachment_url( $attachment_id ) : ''; if ( $attachment_url ) { return sprintf( '%2$s', esc_url( $attachment_url ), esc_html__( 'View file', 'admin-optimizer' ) ); } $value = is_scalar( $value ) ? (string) $value : ''; if ( '' === $value ) { return ''; } if ( $this->is_file_url( $value ) ) { return sprintf( '%2$s', esc_url( $value ), esc_html__( 'View file', 'admin-optimizer' ) ); } return esc_html( $value ); } /** * Check whether a value is a URL to a stored file. * * @param string $value Entry value. * * @return bool */ private function is_file_url( $value ) { if ( ! wp_http_validate_url( $value ) ) { return false; } $filetype = wp_check_filetype( (string) wp_parse_url( $value, PHP_URL_PATH ) ); return ! empty( $filetype['ext'] ); } /** * Method for actions column. * * @param array $item Data item. * * @return string */ protected function column_actions( $item ) { $entry_id = absint( $item['id'] ); $args = [ 'post_type' => 'adminoptim-form', 'page' => 'adminoptim-form-entries', ]; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_REQUEST['form_id'] ) && absint( $_REQUEST['form_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $args['form_id'] = absint( $_REQUEST['form_id'] ); } $base_url = add_query_arg( $args, admin_url( 'edit.php' ) ); $view_url = add_query_arg( 'entry_id', $entry_id, $base_url ); if ( ! current_user_can( 'manage_options' ) ) { return '' . esc_html__( 'View', 'admin-optimizer' ) . ''; } $delete_url = wp_nonce_url( add_query_arg( [ 'action' => 'delete_entry', 'entry_id' => $entry_id, ], $base_url ), 'aofb_delete_entry_' . $entry_id ); return sprintf( '%2$s | %5$s', esc_url( $view_url ), esc_html__( 'View', 'admin-optimizer' ), esc_url( $delete_url ), esc_js( __( 'Are you sure you want to delete this entry?', 'admin-optimizer' ) ), esc_html__( 'Delete', 'admin-optimizer' ) ); } /** * Method for cb column. * * @param array $item Data item. * * @return string */ protected function column_cb( $item ) { return sprintf( '', absint( $item['id'] ) ); } /** Filter choices use the same object checks as the data query. */ protected function extra_tablenav( $which ) { if ( 'top' !== $which ) { return; } $selected = isset( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended echo '
'; submit_button( __( 'Filter', 'admin-optimizer' ), '', 'filter_action', false ); echo '
'; } /** * Prepare items for the table. */ public function prepare_items() { global $wpdb; $per_page = 20; $columns = $this->get_columns(); $hidden = []; $sortable = $this->get_sortable_columns(); $this->_column_headers = [ $columns, $hidden, $sortable ]; $current_page = $this->get_pagenum(); $offset = ( $current_page - 1 ) * $per_page; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $orderby = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ) : 'date_created'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $order = isset( $_REQUEST['order'] ) && 'asc' === strtolower( sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ) ) ? 'ASC' : 'DESC'; $valid_orderbys = array_keys( $this->get_sortable_columns() ); if ( ! in_array( $orderby, $valid_orderbys, true ) ) { $orderby = 'date_created'; } $ids = Form_Builder::accessible_form_ids(); $where = 'form_id IN (' . implode( ',', array_map( 'absint', $ids ?: [ 0 ] ) ) . ')'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_REQUEST['form_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $form_id = absint( $_REQUEST['form_id'] ); if ( $form_id > 0 ) { $where .= $wpdb->prepare( ' AND form_id = %d', $form_id ); } } // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching // phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter $total_items = $wpdb->get_var( "SELECT COUNT(id) FROM {$this->table_name} WHERE {$where}" ); $query = $wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE {$where} ORDER BY {$orderby} {$order} LIMIT %d OFFSET %d", $per_page, $offset ); // phpcs:ignore $this->items = $wpdb->get_results( $query, ARRAY_A ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery // phpcs:enable WordPress.DB.DirectDatabaseQuery.NoCaching $this->set_pagination_args( [ 'total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => ceil( $total_items / $per_page ), ] ); } }