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

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

270 lines 8.2 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 if( class_exists( 'WP_List_Table' ) ) {
7 /**
8 * Class MC4WP_Forms_Table
9 */
10 class Table extends WP_List_Table {
11
12 /**
13 * @var bool
14 */
15 public $is_trash = false;
16
17 /**
18 * @var array
19 */
20 private $settings = array();
21
22 /**
23 * Constructor
24 */
25 public function __construct( array $settings ) {
26 parent::__construct(
27 array(
28 'singular' => 'form',
29 'plural' => 'forms',
30 'ajax' => false
31 )
32 );
33
34 $this->settings = $settings;
35
36 $columns = $this->get_columns();
37 $sortable = $this->get_sortable_columns();
38 $hidden = array();
39 $this->_column_headers = array( $columns, $hidden, $sortable );
40
41 $this->is_trash = isset( $_REQUEST['post_status'] ) && $_REQUEST['post_status'] === 'trash';
42 $this->process_bulk_action();
43 $this->prepare_items();
44 $this->set_pagination_args(
45 array(
46 'per_page' => 50,
47 'total_items' => count( $this->items )
48 )
49 );
50 }
51
52
53 public function prepare_items() {
54 $this->items = $this->get_items();
55 }
56 /**
57 * Get an associative array ( id => link ) with the list
58 * of views available on this table.
59 *
60 * @since 3.1.0
61 * @access protected
62 *
63 * @return array
64 */
65 public function get_views() {
66
67 $counts = wp_count_posts( 'html-form' );
68 $current = isset( $_GET['post_status'] ) ? $_GET['post_status'] : '';
69
70 $count_any = $counts->publish + $counts->draft + $counts->future + $counts->pending;
71
72 return array(
73 '' => sprintf( '<a href="%s" class="%s">%s</a> (%d)', remove_query_arg( 'post_status' ), $current == '' ? 'current' : '', __( 'All' ), $count_any ),
74 'trash' => sprintf( '<a href="%s" class="%s">%s</a> (%d)', add_query_arg( array( 'post_status' => 'trash' ) ), $current == 'trash' ? 'current' : '', __( 'Trash' ), $counts->trash ),
75 );
76 }
77
78 /**
79 * @return array
80 */
81 public function get_bulk_actions() {
82
83 $actions = array();
84
85 if( $this->is_trash ) {
86 $actions['untrash'] = __( 'Restore' );
87 $actions['delete'] = __( 'Delete Permanently' );
88 return $actions;
89 }
90
91 $actions['trash'] = __( 'Move to Trash' );
92 $actions['duplicate'] = __( 'Duplicate' );
93 return $actions;
94 }
95
96 public function get_default_primary_column_name() {
97 return 'form_name';
98 }
99
100 /**
101 * @return array
102 */
103 public function get_table_classes() {
104 return array( 'widefat', 'fixed', 'striped', 'html-forms-table' );
105 }
106
107 /**
108 * @return array
109 */
110 public function get_columns() {
111 return array(
112 'cb' => '<input type="checkbox" />',
113 'form_name' => __( 'Form', 'html-forms' ),
114 'shortcode' => __( 'Shortcode', 'html-forms' ),
115 );
116 }
117
118 /**
119 * @return array
120 */
121 public function get_sortable_columns() {
122 return array();
123 }
124
125 /**
126 * @return array
127 */
128 public function get_items() {
129 $args = array(
130 'post_type' => 'html-form',
131 'post_status' => array( 'publish', 'draft', 'pending', 'future' )
132 );
133
134 if( ! empty( $_GET['s'] ) ) {
135 $args['s'] = sanitize_text_field( $_GET['s'] );
136 }
137
138 if( ! empty( $_GET['post_status' ] ) ) {
139 $args['post_status'] = sanitize_text_field( $_GET['post_status'] );
140 }
141
142
143 $items = get_posts( $args );
144
145 return $items;
146 }
147
148 /**
149 * @param $item
150 *
151 * @return string
152 */
153 public function column_cb( $item ) {
154 return sprintf( '<input type="checkbox" name="forms[]" value="%s" />', $item->ID );
155 }
156
157 /**
158 * @param WP_Post $post
159 *
160 * @return mixed
161 */
162 public function column_ID( WP_Post $post ) {
163 return $post->ID;
164 }
165
166 /**
167 * @param WP_Post $post
168 * @return string
169 */
170 public function column_form_name( WP_Post $post ) {
171 if( $this->is_trash ) {
172 return sprintf( '<strong>%s</strong>', esc_html( $post->post_title ) );
173 }
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( $this->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
269 }
270