| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
if ( ! class_exists( 'WP_List_Table' ) ) { |
| 5 |
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 6 |
} |
| 7 |
|
| 8 |
class Accua_Forms_Fields_List_Table extends WP_List_Table { |
| 9 |
|
| 10 |
private $field_types = array(); |
| 11 |
|
| 12 |
function __construct() { |
| 13 |
parent::__construct( array( |
| 14 |
'singular' => 'field', |
| 15 |
'plural' => 'fields', |
| 16 |
'ajax' => false, |
| 17 |
) ); |
| 18 |
$this->field_types = accua_forms_fields_get_types(); |
| 19 |
} |
| 20 |
|
| 21 |
function get_columns() { |
| 22 |
return array( |
| 23 |
'cb' => '<input type="checkbox" />', |
| 24 |
'name' => __( 'Label', 'contact-forms' ), |
| 25 |
'slug' => __( 'Slug', 'contact-forms' ), |
| 26 |
'type' => __( 'Type', 'contact-forms' ), |
| 27 |
'forms' => __( 'Forms', 'contact-forms' ), |
| 28 |
'description' => __( 'Description', 'contact-forms' ), |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
function get_sortable_columns() { |
| 33 |
return array( |
| 34 |
'name' => array( 'name', false ), |
| 35 |
'slug' => array( 'slug', false ), |
| 36 |
'type' => array( 'type', false ), |
| 37 |
'forms' => array( 'forms', false ), |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
function get_bulk_actions() { |
| 42 |
return array( |
| 43 |
'delete' => __( 'Delete', 'contact-forms' ), |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
function column_cb( $item ) { |
| 48 |
return sprintf( |
| 49 |
'<input type="checkbox" name="fields[]" value="%s" />', |
| 50 |
esc_attr( $item['slug'] ) |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
function column_default( $item, $column_name ) { |
| 55 |
if ( isset( $item[ $column_name ] ) ) { |
| 56 |
return esc_html( $item[ $column_name ] ); |
| 57 |
} |
| 58 |
return ''; |
| 59 |
} |
| 60 |
|
| 61 |
function column_name( $item ) { |
| 62 |
$edit_url = admin_url( 'admin.php?page=accua_forms_fields&edit-fid=' . urlencode( $item['slug'] ) ); |
| 63 |
$delete_url = wp_nonce_url( |
| 64 |
admin_url( 'admin.php?page=accua_forms_fields&delete-fid=' . urlencode( $item['slug'] ) ), |
| 65 |
'delete_form_field_' . $item['slug'] |
| 66 |
); |
| 67 |
$actions = array( |
| 68 |
'edit' => sprintf( |
| 69 |
'<a href="%s">%s</a>', |
| 70 |
esc_url( $edit_url ), |
| 71 |
esc_html__( 'Edit', 'contact-forms' ) |
| 72 |
), |
| 73 |
'delete' => sprintf( |
| 74 |
'<a href="%s" class="delete-tag" onclick="return confirm(\'%s\');">%s</a>', |
| 75 |
esc_url( $delete_url ), |
| 76 |
esc_js( __( 'Are you sure you want to delete this field?', 'contact-forms' ) ), |
| 77 |
esc_html__( 'Delete', 'contact-forms' ) |
| 78 |
), |
| 79 |
); |
| 80 |
return sprintf( |
| 81 |
'<strong><a class="row-title" href="%s">%s</a></strong>%s', |
| 82 |
esc_url( $edit_url ), |
| 83 |
esc_html( $item['name'] ), |
| 84 |
$this->row_actions( $actions ) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
function column_type( $item ) { |
| 89 |
$type_key = $item['type']; |
| 90 |
if ( isset( $this->field_types[ $type_key ] ) ) { |
| 91 |
return esc_html( $this->field_types[ $type_key ] ); |
| 92 |
} |
| 93 |
return esc_html( $type_key ); |
| 94 |
} |
| 95 |
|
| 96 |
function column_forms( $item ) { |
| 97 |
$count = $item['forms']; |
| 98 |
if ( $count > 0 ) { |
| 99 |
return '<span>' . (int) $count . '</span>'; |
| 100 |
} |
| 101 |
return '0'; |
| 102 |
} |
| 103 |
|
| 104 |
function prepare_items() { |
| 105 |
$columns = $this->get_columns(); |
| 106 |
$hidden = array(); |
| 107 |
$sortable = $this->get_sortable_columns(); |
| 108 |
|
| 109 |
$this->_column_headers = array( $columns, $hidden, $sortable ); |
| 110 |
|
| 111 |
$avail_fields = get_option( 'accua_forms_avail_fields', array() ); |
| 112 |
$saved_forms = get_option( 'accua_forms_saved_forms', array() ); |
| 113 |
|
| 114 |
// Count how many forms use each field |
| 115 |
$field_usage = array(); |
| 116 |
foreach ( $saved_forms as $form ) { |
| 117 |
if ( ! empty( $form['fields'] ) && is_array( $form['fields'] ) ) { |
| 118 |
foreach ( array_keys( $form['fields'] ) as $field_slug ) { |
| 119 |
if ( ! isset( $field_usage[ $field_slug ] ) ) { |
| 120 |
$field_usage[ $field_slug ] = 0; |
| 121 |
} |
| 122 |
$field_usage[ $field_slug ]++; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
$data = array(); |
| 128 |
foreach ( $avail_fields as $id => $field ) { |
| 129 |
$data[] = array( |
| 130 |
'slug' => $id, |
| 131 |
'name' => $field['name'] ?? $field['label'] ?? $id, |
| 132 |
'type' => $field['type'] ?? '', |
| 133 |
'forms' => $field_usage[ $id ] ?? 0, |
| 134 |
'description' => $field['description'] ?? '', |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
// Sorting |
| 139 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only sort parameter |
| 140 |
$orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : 'slug'; |
| 141 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only sort parameter |
| 142 |
$order = isset( $_GET['order'] ) ? sanitize_text_field( wp_unslash( $_GET['order'] ) ) : 'asc'; |
| 143 |
|
| 144 |
$valid_columns = array( 'name', 'slug', 'type', 'forms' ); |
| 145 |
if ( ! in_array( $orderby, $valid_columns, true ) ) { |
| 146 |
$orderby = 'slug'; |
| 147 |
} |
| 148 |
$order_asc = ( 'desc' !== strtolower( $order ) ); |
| 149 |
|
| 150 |
usort( $data, function ( $a, $b ) use ( $orderby, $order_asc ) { |
| 151 |
if ( $orderby === 'forms' ) { |
| 152 |
$result = $a['forms'] - $b['forms']; |
| 153 |
} else { |
| 154 |
$result = strcasecmp( $a[ $orderby ], $b[ $orderby ] ); |
| 155 |
} |
| 156 |
return $order_asc ? $result : -$result; |
| 157 |
} ); |
| 158 |
|
| 159 |
$this->items = $data; |
| 160 |
} |
| 161 |
|
| 162 |
protected function get_default_primary_column_name() { |
| 163 |
return 'name'; |
| 164 |
} |
| 165 |
} |
| 166 |
|