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

261 lines 7.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
5 use WP_List_Table, WP_Post;
6
7 if ( ! class_exists( 'WP_List_Table', false ) ) {
8 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
9 }
10
11 /**
12 * Class MC4WP_Forms_Table
13 */
14 class Table extends WP_List_Table {
15
16 /**
17 * @var bool
18 */
19 public $is_trash = false;
20
21 /**
22 * Constructor
23 */
24 public function __construct() {
25 parent::__construct(
26 array(
27 'singular' => 'form',
28 'plural' => 'forms',
29 'ajax' => false
30 )
31 );
32
33 $columns = $this->get_columns();
34 $sortable = $this->get_sortable_columns();
35 $hidden = array();
36 $this->is_trash = isset( $_REQUEST['post_status'] ) && $_REQUEST['post_status'] === 'trash';
37
38 $this->process_bulk_action();
39
40 $this->_column_headers = array( $columns, $hidden, $sortable );
41 $this->set_pagination_args(
42 array(
43 'per_page' => 50,
44 'total_items' => count( $this->items )
45 )
46 );
47 }
48
49
50 public function prepare_items() {
51 $this->items = $this->get_items();
52 }
53 /**
54 * Get an associative array ( id => link ) with the list
55 * of views available on this table.
56 *
57 * @since 3.1.0
58 * @access protected
59 *
60 * @return array
61 */
62 public function get_views() {
63
64 $counts = wp_count_posts( 'html-form' );
65 $current = isset( $_GET['post_status'] ) ? $_GET['post_status'] : '';
66
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 );
130
131 if( ! empty( $_GET['s'] ) ) {
132 $args['s'] = sanitize_text_field( $_GET['s'] );
133 }
134
135 if( ! empty( $_GET['post_status' ] ) ) {
136 $args['post_status'] = sanitize_text_field( $_GET['post_status'] );
137 }
138
139
140 $items = get_posts( $args );
141
142 return $items;
143 }
144
145 /**
146 * @param $item
147 *
148 * @return string
149 */
150 public function column_cb( $item ) {
151 return sprintf( '<input type="checkbox" name="forms[]" value="%s" />', $item->ID );
152 }
153
154 /**
155 * @param WP_Post $post
156 *
157 * @return mixed
158 */
159 public function column_ID( WP_Post $post ) {
160 return $post->ID;
161 }
162
163 /**
164 * @param WP_Post $post
165 * @return string
166 */
167 public function column_form_name( WP_Post $post ) {
168 if( $this->is_trash ) {
169 return sprintf( '<strong>%s</strong>', esc_html( $post->post_title ) );
170 }
171
172 $edit_link = admin_url( 'admin.php?page=html-forms&view=edit&form_id=' . $post->ID );
173 $title = '<strong><a class="row-title" href="' . $edit_link . '">' . esc_html( $post->post_title ) . '</a></strong>';
174
175 $actions = array();
176 $tabs = array(
177 'fields' => __( 'Fields', 'html-forms' ),
178 'messages' => __( 'Messages', 'html-forms' ),
179 'settings' => __( 'Settings', 'html-forms' ),
180 'actions' => __( 'Actions', 'html-forms' ),
181 'submissions' => __( 'Submissions', 'html-forms' ),
182 );
183 foreach( $tabs as $tab_slug => $tab_title ) {
184 $actions[$tab_slug] = '<a href="'. esc_attr( add_query_arg( array( 'tab' => $tab_slug ), $edit_link ) ) .'">'. $tab_title . '</a>';
185 }
186
187 return $title . $this->row_actions( $actions );
188 }
189
190 /**
191 * @param WP_Post $post
192 *
193 * @return string
194 */
195 public function column_shortcode( WP_Post $post ) {
196 if( $this->is_trash ) {
197 return '';
198 }
199
200 return sprintf( '<input style="width: 260px;" type="text" onfocus="this.select();" readonly="readonly" value="%s">', esc_attr( '[hf_form slug="' . $post->post_name . '"]' ) );
201 }
202
203 /**
204 * The text that is shown when there are no items to show
205 */
206 public function no_items() {
207 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') );
208 }
209
210 /**
211 *
212 */
213 public function process_bulk_action() {
214 $action = $this->current_action();
215 if( empty( $action ) ) {
216 return false;
217 }
218
219 $method = 'process_bulk_action_' . $action;
220 $forms = (array) $_REQUEST['forms'];
221 if( method_exists( $this, $method ) ) {
222 return call_user_func_array( array( $this, $method ), array( $forms ) );
223 }
224
225 return false;
226 }
227
228 public function process_bulk_action_duplicate( $forms ) {
229 foreach( $forms as $form_id ) {
230 $post = get_post( $form_id );
231 $post_meta = get_post_meta( $form_id );
232
233 $new_post_id = wp_insert_post(
234 array(
235 'post_title' => $post->post_title,
236 'post_content' => $post->post_content,
237 'post_type' => 'html-form',
238 'post_status' => 'publish'
239 )
240 );
241 foreach( $post_meta as $meta_key => $meta_value ) {
242 $meta_value = maybe_unserialize( $meta_value[0] );
243 update_post_meta( $new_post_id, $meta_key, $meta_value );
244 }
245 }
246 }
247
248 public function process_bulk_action_trash( $forms ) {
249 return array_map( 'wp_trash_post', $forms );
250 }
251
252 public function process_bulk_action_delete( $forms ) {
253 return array_map( 'wp_delete_post', $forms );
254 }
255
256 public function process_bulk_action_untrash( $forms ) {
257 return array_map( 'wp_untrash_post', $forms );
258 }
259
260 }
261