PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.4.0
HTML Forms – Simple WordPress Forms Plugin v1.4.0
trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 All 66 releases
html-forms / src / admin / class-table.php

class-table.php in HTML Forms – Simple WordPress Forms Plugin 1.4.0, at src/admin/class-table.php

307 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'submissions' => __( 'Submissions', 'html-forms' ),
115 );
116 }
117
118 /**
119 * @return array
120 */
121 public function get_hidden_columns() {
122 return array();
123 }
124
125 /**
126 * @return array
127 */
128 public function get_sortable_columns() {
129 return array(
130 'form_name' => array( 'post_title', true ),
131 );
132 }
133
134 /**
135 * @return array
136 */
137 public function get_items() {
138 $args = array();
139
140 if ( ! empty( $_GET['s'] ) ) {
141 $args['s'] = sanitize_text_field( $_GET['s'] );
142 }
143
144 if ( ! empty( $_GET['post_status'] ) ) {
145 $args['post_status'] = sanitize_text_field( $_GET['post_status'] );
146 }
147
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 );
157 }
158
159 /**
160 * @param Form $form
161 *
162 * @return string
163 */
164 public function column_cb( $form ) {
165 return sprintf( '<input type="checkbox" name="forms[]" value="%s" />', $form->ID );
166 }
167
168 /**
169 * @param Form $form
170 *
171 * @return mixed
172 */
173 public function column_ID( Form $form ) {
174 return $form->ID;
175 }
176
177 /**
178 * @param Form $form
179 *
180 * @return string
181 */
182 public function column_form_name( Form $form ) {
183 if ( $this->is_trash ) {
184 return sprintf( '<strong>%s</strong>', esc_html( $form->title ) );
185 }
186
187 $edit_link = admin_url( 'admin.php?page=html-forms&view=edit&form_id=' . $form->ID );
188 $title = '<strong><a class="row-title" href="' . $edit_link . '">' . esc_html( $form->title ) . '</a></strong>';
189
190 $actions = array();
191 $tabs = hf_get_admin_tabs( $form );
192
193 foreach ( $tabs as $tab_slug => $tab_title ) {
194 $actions[ $tab_slug ] = '<a href="' . esc_attr( add_query_arg( array( 'tab' => $tab_slug ), $edit_link ) ) . '">' . $tab_title . '</a>';
195 }
196
197 return $title . $this->row_actions( $actions );
198 }
199
200 /**
201 * @param Form $form
202 *
203 * @return string
204 */
205 public function column_shortcode( Form $form ) {
206 if ( $this->is_trash ) {
207 return '';
208 }
209
210 return sprintf( '<input type="text" onfocus="this.select();" readonly="readonly" value="%s">', esc_attr( '[hf_form slug="' . $form->slug . '"]' ) );
211 }
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
229 /**
230 * The text that is shown when there are no items to show
231 */
232 public function no_items() {
233 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' ) );
234 }
235
236 /**
237 *
238 */
239 public function process_bulk_action() {
240 $action = $this->current_action();
241 if ( empty( $action ) ) {
242 return false;
243 }
244
245 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'bulk-' . $this->_args['plural'] ) ) {
246 return false;
247 }
248
249 $method = 'process_bulk_action_' . $action;
250 $forms = (array) $_REQUEST['forms'];
251 if ( method_exists( $this, $method ) ) {
252 return call_user_func_array( array( $this, $method ), array( $forms ) );
253 }
254
255 return false;
256 }
257
258 public function process_bulk_action_duplicate( $forms ) {
259 foreach ( $forms as $form_id ) {
260 $post = get_post( $form_id );
261 $post_meta = get_post_meta( $form_id );
262
263 $new_post_id = wp_insert_post(
264 array(
265 'post_title' => $post->post_title,
266 'post_content' => $post->post_content,
267 'post_type' => 'html-form',
268 'post_status' => 'publish',
269 )
270 );
271 foreach ( $post_meta as $meta_key => $meta_value ) {
272 $meta_value = maybe_unserialize( $meta_value[0] );
273 update_post_meta( $new_post_id, $meta_key, $meta_value );
274 }
275 }
276 }
277
278 public function process_bulk_action_trash( $forms ) {
279 array_map( 'wp_trash_post', $forms );
280 }
281
282 public function process_bulk_action_delete( $forms ) {
283 array_map( 'wp_delete_post', $forms );
284 }
285
286 public function process_bulk_action_untrash( $forms ) {
287 array_map( 'wp_untrash_post', $forms );
288 }
289
290 /**
291 * Generates content for a single row of the table
292 *
293 * @param Form $form The current item
294 *
295 * @since 3.1.0
296 *
297 */
298 public function single_row( $form ) {
299 echo sprintf( '<tr id="hf-forms-item-%d">', $form->ID );
300 $this->single_row_columns( $form );
301 echo '</tr>';
302 }
303
304 }
305
306 }
307