PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.2
HTML Forms – Simple WordPress Forms Plugin v1.3.2
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 / trunk / src / Admin / Table.php

Table.php in HTML Forms – Simple WordPress Forms Plugin 1.3.2, at trunk/src/Admin/Table.php

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