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

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