| 1 |
<?php |
| 2 |
|
| 3 |
namespace HTML_Forms\Admin; |
| 4 |
use HTML_Forms\Form; |
| 5 |
use WP_List_Table, WP_Post; |
| 6 |
|
| 7 |
// Check if WP Core class exists so that we can keep testing rest of HTML Forms in isolation.. |
| 8 |
if( class_exists( 'WP_List_Table' ) ) { |
| 9 |
|
| 10 |
class Table extends WP_List_Table { |
| 11 |
|
| 12 |
/** |
| 13 |
* @var bool |
| 14 |
*/ |
| 15 |
public $is_trash = false; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var array |
| 19 |
*/ |
| 20 |
private $settings = array(); |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructor |
| 24 |
*/ |
| 25 |
public function __construct( array $settings ) { |
| 26 |
parent::__construct( |
| 27 |
array( |
| 28 |
'singular' => 'form', |
| 29 |
'plural' => 'forms', |
| 30 |
'ajax' => false |
| 31 |
) |
| 32 |
); |
| 33 |
|
| 34 |
$this->settings = $settings; |
| 35 |
$this->process_bulk_action(); |
| 36 |
|
| 37 |
$columns = $this->get_columns(); |
| 38 |
$sortable = $this->get_sortable_columns(); |
| 39 |
$hidden = array(); |
| 40 |
$this->_column_headers = array( $columns, $hidden, $sortable ); |
| 41 |
$this->is_trash = isset( $_REQUEST['post_status'] ) && $_REQUEST['post_status'] === 'trash'; |
| 42 |
$this->items = $this->get_items(); |
| 43 |
$this->set_pagination_args( |
| 44 |
array( |
| 45 |
'per_page' => 50, |
| 46 |
'total_items' => count( $this->items ) |
| 47 |
) |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get an associative array ( id => link ) with the list |
| 53 |
* of views available on this table. |
| 54 |
* |
| 55 |
* @since 3.1.0 |
| 56 |
* @access protected |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public function get_views() { |
| 61 |
$counts = wp_count_posts( 'html-form' ); |
| 62 |
$current = isset( $_GET['post_status'] ) ? $_GET['post_status'] : ''; |
| 63 |
$count_any = $counts->publish + $counts->draft + $counts->future + $counts->pending; |
| 64 |
|
| 65 |
return array( |
| 66 |
'' => sprintf( '<a href="%s" class="%s">%s</a> (%d)', remove_query_arg( 'post_status' ), $current == '' ? 'current' : '', __( 'All' ), $count_any ), |
| 67 |
'trash' => sprintf( '<a href="%s" class="%s">%s</a> (%d)', add_query_arg( array( 'post_status' => 'trash' ) ), $current == 'trash' ? 'current' : '', __( 'Trash' ), $counts->trash ), |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
public function get_bulk_actions() { |
| 75 |
|
| 76 |
$actions = array(); |
| 77 |
|
| 78 |
if( $this->is_trash ) { |
| 79 |
$actions['untrash'] = __( 'Restore' ); |
| 80 |
$actions['delete'] = __( 'Delete Permanently' ); |
| 81 |
return $actions; |
| 82 |
} |
| 83 |
|
| 84 |
$actions['trash'] = __( 'Move to Trash' ); |
| 85 |
$actions['duplicate'] = __( 'Duplicate' ); |
| 86 |
return $actions; |
| 87 |
} |
| 88 |
|
| 89 |
public function get_default_primary_column_name() { |
| 90 |
return 'form_name'; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
public function get_table_classes() { |
| 97 |
return array( 'widefat', 'fixed', 'striped', 'html-forms-table' ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
public function get_columns() { |
| 104 |
return array( |
| 105 |
'cb' => '<input type="checkbox" />', |
| 106 |
'form_name' => __( 'Form', 'html-forms' ), |
| 107 |
'shortcode' => __( 'Shortcode', 'html-forms' ), |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
public function get_sortable_columns() { |
| 115 |
return array(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
public function get_items() { |
| 122 |
|
| 123 |
$args = array(); |
| 124 |
|
| 125 |
if( ! empty( $_GET['s'] ) ) { |
| 126 |
$args['s'] = sanitize_text_field( $_GET['s'] ); |
| 127 |
} |
| 128 |
|
| 129 |
if( ! empty( $_GET['post_status' ] ) ) { |
| 130 |
$args['post_status'] = sanitize_text_field( $_GET['post_status'] ); |
| 131 |
} |
| 132 |
|
| 133 |
$items = hf_get_forms($args); |
| 134 |
return $items; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @param Form $form |
| 139 |
* @return string |
| 140 |
*/ |
| 141 |
public function column_cb($form) { |
| 142 |
return sprintf( '<input type="checkbox" name="forms[]" value="%s" />', $form->ID ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* @param Form $form |
| 147 |
* |
| 148 |
* @return mixed |
| 149 |
*/ |
| 150 |
public function column_ID( Form $form ) { |
| 151 |
return $form->ID; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @param Form $form |
| 156 |
* @return string |
| 157 |
*/ |
| 158 |
public function column_form_name( Form $form ) { |
| 159 |
if( $this->is_trash ) { |
| 160 |
return sprintf( '<strong>%s</strong>', esc_html( $form->title ) ); |
| 161 |
} |
| 162 |
|
| 163 |
$edit_link = admin_url( 'admin.php?page=html-forms&view=edit&form_id=' . $form->ID ); |
| 164 |
$title = '<strong><a class="row-title" href="' . $edit_link . '">' . esc_html( $form->title ) . '</a></strong>'; |
| 165 |
|
| 166 |
$actions = array(); |
| 167 |
$tabs = array( |
| 168 |
'fields' => __( 'Fields', 'html-forms' ), |
| 169 |
'messages' => __( 'Messages', 'html-forms' ), |
| 170 |
'settings' => __( 'Settings', 'html-forms' ), |
| 171 |
'actions' => __( 'Actions', 'html-forms' ), |
| 172 |
); |
| 173 |
|
| 174 |
if( $form->settings['save_submissions'] ) { |
| 175 |
$tabs['submissions'] = __( 'Submissions', 'html-forms' ); |
| 176 |
} |
| 177 |
|
| 178 |
foreach( $tabs as $tab_slug => $tab_title ) { |
| 179 |
$actions[$tab_slug] = '<a href="'. esc_attr( add_query_arg( array( 'tab' => $tab_slug ), $edit_link ) ) .'">'. $tab_title . '</a>'; |
| 180 |
} |
| 181 |
|
| 182 |
return $title . $this->row_actions( $actions ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @param Form $form |
| 187 |
* @return string |
| 188 |
*/ |
| 189 |
public function column_shortcode(Form $form) { |
| 190 |
if( $this->is_trash ) { |
| 191 |
return ''; |
| 192 |
} |
| 193 |
|
| 194 |
return sprintf( '<input style="width: 260px;" type="text" onfocus="this.select();" readonly="readonly" value="%s">', esc_attr( '[hf_form slug="' . $form->slug . '"]' ) ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* The text that is shown when there are no items to show |
| 199 |
*/ |
| 200 |
public function no_items() { |
| 201 |
echo sprintf( __( 'No forms found. <a href="%s">Would you like to create one now</a>?', 'html-forms' ), admin_url( 'admin.php?page=html-forms-add-form') ); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* |
| 206 |
*/ |
| 207 |
public function process_bulk_action() { |
| 208 |
$action = $this->current_action(); |
| 209 |
if( empty( $action ) ) { |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
$method = 'process_bulk_action_' . $action; |
| 214 |
$forms = (array) $_REQUEST['forms']; |
| 215 |
if( method_exists( $this, $method ) ) { |
| 216 |
return call_user_func_array( array( $this, $method ), array( $forms ) ); |
| 217 |
} |
| 218 |
|
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
public function process_bulk_action_duplicate( $forms ) { |
| 223 |
foreach( $forms as $form_id ) { |
| 224 |
$post = get_post( $form_id ); |
| 225 |
$post_meta = get_post_meta( $form_id ); |
| 226 |
|
| 227 |
$new_post_id = wp_insert_post( |
| 228 |
array( |
| 229 |
'post_title' => $post->post_title, |
| 230 |
'post_content' => $post->post_content, |
| 231 |
'post_type' => 'html-form', |
| 232 |
'post_status' => 'publish' |
| 233 |
) |
| 234 |
); |
| 235 |
foreach( $post_meta as $meta_key => $meta_value ) { |
| 236 |
$meta_value = maybe_unserialize( $meta_value[0] ); |
| 237 |
update_post_meta( $new_post_id, $meta_key, $meta_value ); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
public function process_bulk_action_trash( $forms ) { |
| 243 |
return array_map( 'wp_trash_post', $forms ); |
| 244 |
} |
| 245 |
|
| 246 |
public function process_bulk_action_delete( $forms ) { |
| 247 |
return array_map( 'wp_delete_post', $forms ); |
| 248 |
} |
| 249 |
|
| 250 |
public function process_bulk_action_untrash( $forms ) { |
| 251 |
return array_map( 'wp_untrash_post', $forms ); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Generates content for a single row of the table |
| 256 |
* |
| 257 |
* @since 3.1.0 |
| 258 |
* |
| 259 |
* @param Form $form The current item |
| 260 |
*/ |
| 261 |
public function single_row($form) { |
| 262 |
echo sprintf( '<tr id="hf-forms-item-%d">', $form->ID ); |
| 263 |
$this->single_row_columns($form); |
| 264 |
echo '</tr>'; |
| 265 |
} |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
} |
| 270 |
|