PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.20
HTML Forms – Simple WordPress Forms Plugin v1.3.20
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.3.20, at src/admin/class-table.php

286 lines 6.8 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'] = __( 'Move to Trash', '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 $method = 'process_bulk_action_' . $action;
229 $forms = (array) $_REQUEST['forms'];
230 if ( method_exists( $this, $method ) ) {
231 return call_user_func_array( array( $this, $method ), array( $forms ) );
232 }
233
234 return false;
235 }
236
237 public function process_bulk_action_duplicate( $forms ) {
238 foreach ( $forms as $form_id ) {
239 $post = get_post( $form_id );
240 $post_meta = get_post_meta( $form_id );
241
242 $new_post_id = wp_insert_post(
243 array(
244 'post_title' => $post->post_title,
245 'post_content' => $post->post_content,
246 'post_type' => 'html-form',
247 'post_status' => 'publish',
248 )
249 );
250 foreach ( $post_meta as $meta_key => $meta_value ) {
251 $meta_value = maybe_unserialize( $meta_value[0] );
252 update_post_meta( $new_post_id, $meta_key, $meta_value );
253 }
254 }
255 }
256
257 public function process_bulk_action_trash( $forms ) {
258 return array_map( 'wp_trash_post', $forms );
259 }
260
261 public function process_bulk_action_delete( $forms ) {
262 return array_map( 'wp_delete_post', $forms );
263 }
264
265 public function process_bulk_action_untrash( $forms ) {
266 return array_map( 'wp_untrash_post', $forms );
267 }
268
269 /**
270 * Generates content for a single row of the table
271 *
272 * @param Form $form The current item
273 *
274 * @since 3.1.0
275 *
276 */
277 public function single_row( $form ) {
278 echo sprintf( '<tr id="hf-forms-item-%d">', $form->ID );
279 $this->single_row_columns( $form );
280 echo '</tr>';
281 }
282
283 }
284
285 }
286