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