Нема описа

class-overview-table.php 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. /**
  3. * Generate the table on the plugin overview page.
  4. *
  5. * @since 1.0.0
  6. */
  7. class WPForms_Overview_Table extends WP_List_Table {
  8. /**
  9. * Number of forms to show per page.
  10. *
  11. * @since 1.0.0
  12. *
  13. * @var int
  14. */
  15. public $per_page;
  16. /**
  17. * Primary class constructor.
  18. *
  19. * @since 1.0.0
  20. */
  21. public function __construct() {
  22. // Utilize the parent constructor to build the main class properties.
  23. parent::__construct(
  24. array(
  25. 'singular' => 'form',
  26. 'plural' => 'forms',
  27. 'ajax' => false,
  28. )
  29. );
  30. // Default number of forms to show per page.
  31. $this->per_page = (int) apply_filters( 'wpforms_overview_per_page', 20 );
  32. }
  33. /**
  34. * Retrieve the table columns.
  35. *
  36. * @since 1.0.0
  37. *
  38. * @return array $columns Array of all the list table columns.
  39. */
  40. public function get_columns() {
  41. $columns = array(
  42. 'cb' => '<input type="checkbox" />',
  43. 'form_name' => esc_html__( 'Name', 'wpforms-lite' ),
  44. 'shortcode' => esc_html__( 'Shortcode', 'wpforms-lite' ),
  45. 'created' => esc_html__( 'Created', 'wpforms-lite' ),
  46. );
  47. return apply_filters( 'wpforms_overview_table_columns', $columns );
  48. }
  49. /**
  50. * Render the checkbox column.
  51. *
  52. * @since 1.0.0
  53. *
  54. * @param WP_Post $form Form.
  55. *
  56. * @return string
  57. */
  58. public function column_cb( $form ) {
  59. return '<input type="checkbox" name="form_id[]" value="' . absint( $form->ID ) . '" />';
  60. }
  61. /**
  62. * Render the columns.
  63. *
  64. * @since 1.0.0
  65. *
  66. * @param WP_Post $form Form.
  67. * @param string $column_name Column Name.
  68. *
  69. * @return string
  70. */
  71. public function column_default( $form, $column_name ) {
  72. switch ( $column_name ) {
  73. case 'id':
  74. $value = $form->ID;
  75. break;
  76. case 'shortcode':
  77. $value = '[wpforms id="' . $form->ID . '"]';
  78. break;
  79. case 'created':
  80. $value = get_the_date( get_option( 'date_format' ), $form );
  81. break;
  82. case 'modified':
  83. $value = get_post_modified_time( get_option( 'date_format' ), false, $form );
  84. break;
  85. case 'author':
  86. $author = get_userdata( $form->post_author );
  87. $value = $author->display_name;
  88. break;
  89. case 'php':
  90. $value = '<code style="display:block;font-size:11px;">if( function_exists( \'wpforms_get\' ) ){ wpforms_get( ' . $form->ID . ' ); }</code>';
  91. break;
  92. default:
  93. $value = '';
  94. }
  95. return apply_filters( 'wpforms_overview_table_column_value', $value, $form, $column_name );
  96. }
  97. /**
  98. * Render the form name column with action links.
  99. *
  100. * @since 1.0.0
  101. *
  102. * @param WP_Post $form Form.
  103. *
  104. * @return string
  105. */
  106. public function column_form_name( $form ) {
  107. // Build the row action links and return the value.
  108. return $this->get_column_form_name_title( $form ) . $this->get_column_form_name_row_actions( $form );
  109. }
  110. /**
  111. * Get the form name HTML for the form name column.
  112. *
  113. * @since 1.5.8
  114. *
  115. * @param WP_Post $form Form object.
  116. *
  117. * @return string
  118. */
  119. protected function get_column_form_name_title( $form ) {
  120. $title = ! empty( $form->post_title ) ? $form->post_title : $form->post_name;
  121. $name = sprintf(
  122. '<span><strong>%s</strong></span>',
  123. esc_html( $title )
  124. );
  125. if ( wpforms_current_user_can( 'view_form_single', $form->ID ) ) {
  126. $name = sprintf(
  127. '<a href="%s" title="%s" class="row-title" target="_blank" rel="noopener noreferrer"><strong>%s</strong></a>',
  128. esc_url( wpforms_get_form_preview_url( $form->ID ) ),
  129. esc_attr__( 'View preview', 'wpforms-lite' ),
  130. esc_html( $title )
  131. );
  132. }
  133. if ( wpforms_current_user_can( 'view_entries_form_single', $form->ID ) ) {
  134. $name = sprintf(
  135. '<a href="%s" title="%s"><strong>%s</strong></a>',
  136. esc_url(
  137. add_query_arg(
  138. array(
  139. 'view' => 'list',
  140. 'form_id' => $form->ID,
  141. ),
  142. admin_url( 'admin.php?page=wpforms-entries' )
  143. )
  144. ),
  145. esc_attr__( 'View entries', 'wpforms-lite' ),
  146. esc_html( $title )
  147. );
  148. }
  149. if ( wpforms_current_user_can( 'edit_form_single', $form->ID ) ) {
  150. $name = sprintf(
  151. '<a href="%s" title="%s"><strong>%s</strong></a>',
  152. esc_url(
  153. add_query_arg(
  154. array(
  155. 'view' => 'fields',
  156. 'form_id' => $form->ID,
  157. ),
  158. admin_url( 'admin.php?page=wpforms-builder' )
  159. )
  160. ),
  161. esc_attr__( 'Edit This Form', 'wpforms-lite' ),
  162. esc_html( $title )
  163. );
  164. }
  165. return $name;
  166. }
  167. /**
  168. * Get the row actions HTML for the form name column.
  169. *
  170. * @since 1.5.8
  171. *
  172. * @param WP_Post $form Form object.
  173. *
  174. * @return string
  175. */
  176. protected function get_column_form_name_row_actions( $form ) {
  177. // Build all of the row action links.
  178. $row_actions = array();
  179. // Edit.
  180. if ( wpforms_current_user_can( 'edit_form_single', $form->ID ) ) {
  181. $row_actions['edit'] = sprintf(
  182. '<a href="%s" title="%s">%s</a>',
  183. esc_url(
  184. add_query_arg(
  185. array(
  186. 'view' => 'fields',
  187. 'form_id' => $form->ID,
  188. ),
  189. admin_url( 'admin.php?page=wpforms-builder' )
  190. )
  191. ),
  192. esc_attr__( 'Edit This Form', 'wpforms-lite' ),
  193. esc_html__( 'Edit', 'wpforms-lite' )
  194. );
  195. }
  196. // Entries.
  197. if ( wpforms_current_user_can( 'view_entries_form_single', $form->ID ) ) {
  198. $row_actions['entries'] = sprintf(
  199. '<a href="%s" title="%s">%s</a>',
  200. esc_url(
  201. add_query_arg(
  202. array(
  203. 'view' => 'list',
  204. 'form_id' => $form->ID,
  205. ),
  206. admin_url( 'admin.php?page=wpforms-entries' )
  207. )
  208. ),
  209. esc_attr__( 'View entries', 'wpforms-lite' ),
  210. esc_html__( 'Entries', 'wpforms-lite' )
  211. );
  212. }
  213. // Preview.
  214. if ( wpforms_current_user_can( 'view_form_single', $form->ID ) ) {
  215. $row_actions['preview_'] = sprintf(
  216. '<a href="%s" title="%s" target="_blank" rel="noopener noreferrer">%s</a>',
  217. esc_url( wpforms_get_form_preview_url( $form->ID ) ),
  218. esc_attr__( 'View preview', 'wpforms-lite' ),
  219. esc_html__( 'Preview', 'wpforms-lite' )
  220. );
  221. }
  222. // Duplicate.
  223. if ( wpforms_current_user_can( 'create_forms' ) && wpforms_current_user_can( 'view_form_single', $form->ID ) ) {
  224. $row_actions['duplicate'] = sprintf(
  225. '<a href="%s" title="%s">%s</a>',
  226. esc_url(
  227. wp_nonce_url(
  228. add_query_arg(
  229. array(
  230. 'action' => 'duplicate',
  231. 'form_id' => $form->ID,
  232. ),
  233. admin_url( 'admin.php?page=wpforms-overview' )
  234. ),
  235. 'wpforms_duplicate_form_nonce'
  236. )
  237. ),
  238. esc_attr__( 'Duplicate this form', 'wpforms-lite' ),
  239. esc_html__( 'Duplicate', 'wpforms-lite' )
  240. );
  241. }
  242. // Delete.
  243. if ( wpforms_current_user_can( 'delete_form_single', $form->ID ) ) {
  244. $row_actions['delete'] = sprintf(
  245. '<a href="%s" title="%s">%s</a>',
  246. esc_url(
  247. wp_nonce_url(
  248. add_query_arg(
  249. array(
  250. 'action' => 'delete',
  251. 'form_id' => $form->ID,
  252. ),
  253. admin_url( 'admin.php?page=wpforms-overview' )
  254. ),
  255. 'wpforms_delete_form_nonce'
  256. )
  257. ),
  258. esc_attr__( 'Delete this form', 'wpforms-lite' ),
  259. esc_html__( 'Delete', 'wpforms-lite' )
  260. );
  261. }
  262. return $this->row_actions( apply_filters( 'wpforms_overview_row_actions', $row_actions, $form ) );
  263. }
  264. /**
  265. * Define bulk actions available for our table listing.
  266. *
  267. * @since 1.0.0
  268. *
  269. * @return array
  270. */
  271. public function get_bulk_actions() {
  272. $actions = array();
  273. if ( wpforms_current_user_can( 'delete_entries' ) ) {
  274. $actions = array(
  275. 'delete' => esc_html__( 'Delete', 'wpforms-lite' ),
  276. );
  277. }
  278. return $actions;
  279. }
  280. /**
  281. * Message to be displayed when there are no forms.
  282. *
  283. * @since 1.0.0
  284. */
  285. public function no_items() {
  286. printf(
  287. wp_kses( /* translators: %s - WPForms Builder page. */
  288. __( 'Whoops, you haven\'t created a form yet. Want to <a href="%s">give it a go</a>?', 'wpforms-lite' ),
  289. array(
  290. 'a' => array(
  291. 'href' => array(),
  292. ),
  293. )
  294. ),
  295. esc_url( admin_url( 'admin.php?page=wpforms-builder' ) )
  296. );
  297. }
  298. /**
  299. * Fetch and setup the final data for the table.
  300. *
  301. * @since 1.0.0
  302. */
  303. public function prepare_items() {
  304. // Setup the columns.
  305. $columns = $this->get_columns();
  306. // Hidden columns (none).
  307. $hidden = array();
  308. // Define which columns can be sorted - form name, date.
  309. $sortable = array(
  310. 'form_name' => array( 'title', false ),
  311. 'created' => array( 'date', false ),
  312. );
  313. // Set column headers.
  314. $this->_column_headers = array( $columns, $hidden, $sortable );
  315. // Get forms.
  316. if ( wpforms_current_user_can( 'wpforms_view_others_forms' ) ) {
  317. $total = wp_count_posts( 'wpforms' )->publish;
  318. } else {
  319. $total = count_user_posts( get_current_user_id(), 'wpforms', true );
  320. }
  321. // phpcs:disable WordPress.Security.NonceVerification.Recommended
  322. $page = $this->get_pagenum();
  323. $order = isset( $_GET['order'] ) && strtoupper( sanitize_text_field( wp_unslash( $_GET['order'] ) ) ) === 'ASC' ? 'ASC' : 'DESC';
  324. $orderby = isset( $_GET['orderby'] ) ? sanitize_key( $_GET['orderby'] ) : 'ID';
  325. $per_page = $this->get_items_per_page( 'wpforms_forms_per_page', $this->per_page );
  326. // phpcs:enable WordPress.Security.NonceVerification.Recommended
  327. $args = array(
  328. 'orderby' => $orderby,
  329. 'order' => $order,
  330. 'nopaging' => false,
  331. 'posts_per_page' => $per_page,
  332. 'paged' => $page,
  333. 'no_found_rows' => false,
  334. 'post_status' => 'publish',
  335. );
  336. $data = wpforms()->form->get( '', $args );
  337. // Giddy up.
  338. $this->items = $data;
  339. // Finalize pagination.
  340. $this->set_pagination_args(
  341. array(
  342. 'total_items' => $total,
  343. 'per_page' => $per_page,
  344. 'total_pages' => ceil( $total / $per_page ),
  345. )
  346. );
  347. }
  348. /**
  349. * Extending the `display_rows()` method in order to add hooks.
  350. *
  351. * @since 1.5.6
  352. */
  353. public function display_rows() {
  354. do_action( 'wpforms_admin_overview_before_rows', $this );
  355. parent::display_rows();
  356. do_action( 'wpforms_admin_overview_after_rows', $this );
  357. }
  358. }