PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.1.5
HTML Forms – Simple WordPress Forms Plugin v1.1.5
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 / Table.php

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

281 lines 8.6 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 $edit_link = admin_url( 'admin.php?page=html-forms&view=edit&form_id=' . $post->ID );
174 $title = '<strong><a class="row-title" href="' . $edit_link . '">' . esc_html( $post->post_title ) . '</a></strong>';
175
176 $actions = array();
177 $tabs = array(
178 'fields' => __( 'Fields', 'html-forms' ),
179 'messages' => __( 'Messages', 'html-forms' ),
180 'settings' => __( 'Settings', 'html-forms' ),
181 'actions' => __( 'Actions', 'html-forms' ),
182 );
183
184 if( $this->settings['save_submissions'] ) {
185 $tabs['submissions'] = __( 'Submissions', 'html-forms' );
186 }
187
188 foreach( $tabs as $tab_slug => $tab_title ) {
189 $actions[$tab_slug] = '<a href="'. esc_attr( add_query_arg( array( 'tab' => $tab_slug ), $edit_link ) ) .'">'. $tab_title . '</a>';
190 }
191
192 return $title . $this->row_actions( $actions );
193 }
194
195 /**
196 * @param WP_Post $post
197 *
198 * @return string
199 */
200 public function column_shortcode( WP_Post $post ) {
201 if( $this->is_trash ) {
202 return '';
203 }
204
205 return sprintf( '<input style="width: 260px;" type="text" onfocus="this.select();" readonly="readonly" value="%s">', esc_attr( '[hf_form slug="' . $post->post_name . '"]' ) );
206 }
207
208 /**
209 * The text that is shown when there are no items to show
210 */
211 public function no_items() {
212 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') );
213 }
214
215 /**
216 *
217 */
218 public function process_bulk_action() {
219 $action = $this->current_action();
220 if( empty( $action ) ) {
221 return false;
222 }
223
224 $method = 'process_bulk_action_' . $action;
225 $forms = (array) $_REQUEST['forms'];
226 if( method_exists( $this, $method ) ) {
227 return call_user_func_array( array( $this, $method ), array( $forms ) );
228 }
229
230 return false;
231 }
232
233 public function process_bulk_action_duplicate( $forms ) {
234 foreach( $forms as $form_id ) {
235 $post = get_post( $form_id );
236 $post_meta = get_post_meta( $form_id );
237
238 $new_post_id = wp_insert_post(
239 array(
240 'post_title' => $post->post_title,
241 'post_content' => $post->post_content,
242 'post_type' => 'html-form',
243 'post_status' => 'publish'
244 )
245 );
246 foreach( $post_meta as $meta_key => $meta_value ) {
247 $meta_value = maybe_unserialize( $meta_value[0] );
248 update_post_meta( $new_post_id, $meta_key, $meta_value );
249 }
250 }
251 }
252
253 public function process_bulk_action_trash( $forms ) {
254 return array_map( 'wp_trash_post', $forms );
255 }
256
257 public function process_bulk_action_delete( $forms ) {
258 return array_map( 'wp_delete_post', $forms );
259 }
260
261 public function process_bulk_action_untrash( $forms ) {
262 return array_map( 'wp_untrash_post', $forms );
263 }
264
265 /**
266 * Generates content for a single row of the table
267 *
268 * @since 3.1.0
269 *
270 * @param object $item The current item
271 */
272 public function single_row( $item ) {
273 echo sprintf( '<tr id="hf-forms-item-%d">',$item->ID );
274 $this->single_row_columns( $item );
275 echo '</tr>';
276 }
277
278 }
279
280 }
281