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