GFPDFHelperHelper_PDF_List_Table

A simple abstract class controlers can extent to share similar variables.

Defined (1)

The class is defined in the following location(s).

/src/helper/Helper_PDF_List_Table.php  
  1. class Helper_PDF_List_Table extends WP_List_Table { 
  2.  
  3. /** 
  4. * Our Gravity Form array 
  5. * @var array 
  6. * @since 4.0 
  7. */ 
  8. public $form; 
  9.  
  10. /** 
  11. * Holds the abstracted Gravity Forms API specific to Gravity PDF 
  12. * @var \GFPDF\Helper\Helper_Form 
  13. * @since 4.0 
  14. */ 
  15. protected $gform; 
  16.  
  17. /** 
  18. * Holds our Helper_Misc object 
  19. * Makes it easy to access common methods throughout the plugin 
  20. * @var \GFPDF\Helper\Helper_Misc 
  21. * @since 4.0 
  22. */ 
  23. protected $misc; 
  24.  
  25. /** 
  26. * Holds our Helper_Templates object 
  27. * used to ease access to our PDF templates 
  28. * @var \GFPDF\Helper\Helper_Templates 
  29. * @since 4.0 
  30. */ 
  31. protected $templates; 
  32.  
  33. /** 
  34. * Setup our class with appropriate data 
  35. * @param array $form The Gravity Forms object 
  36. * @param \GFPDF\Helper\Helper_Abstract_Form $gform Our abstracted Gravity Forms API 
  37. * @param \GFPDF\Helper\Helper_Misc $misc 
  38. * @param \GFPDF\Helper\Helper_Templates $templates 
  39. * @since 4.0 
  40. */ 
  41. public function __construct( $form, Helper_Abstract_Form $gform, Helper_Misc $misc, Helper_Templates $templates ) { 
  42.  
  43. /** Assign our internal variables */ 
  44. $this->form = $form; 
  45. $this->gform = $gform; 
  46. $this->misc = $misc; 
  47. $this->templates = $templates; 
  48.  
  49. /** Cache column header internally so we don't have to work with the global get_column_headers() function */ 
  50. $this->_column_headers = [ $this->get_columns(), [], [] ]; 
  51.  
  52. parent::__construct(); 
  53.  
  54. /** 
  55. * Return the columns that should be used in the list table 
  56. * @return array 
  57. */ 
  58. public function get_columns() { 
  59.  
  60. $columns = [ 
  61. 'cb' => '',  
  62. 'name' => esc_html__( 'Name', 'gravity-forms-pdf-extended' ),  
  63. 'shortcode' => esc_html__( 'Download Shortcode', 'gravity-forms-pdf-extended' ),  
  64. 'template' => esc_html__( 'Template', 'gravity-forms-pdf-extended' ),  
  65. 'notifications' => esc_html__( 'Notifications', 'gravity-forms-pdf-extended' ),  
  66. ]; 
  67.  
  68. /** See https://gravitypdf.com/documentation/v4/gfpdf_pdf_list_columns/ for more details about this filter */ 
  69. $columns = apply_filters( 'gfpdf_pdf_list_columns', $columns ); 
  70.  
  71. return $columns; 
  72.  
  73. /** 
  74. * Get the name of the default primary column. 
  75. * @return string Name of the default primary column, in this case, 'name' 
  76. * @since 4.0 
  77. */ 
  78. protected function get_default_primary_column_name() { 
  79. return 'name'; 
  80.  
  81. /** 
  82. * Prepares the list of items for displaying. 
  83. * @since 4.0 
  84. */ 
  85. public function prepare_items() { 
  86. $this->items = ( isset( $this->form['gfpdf_form_settings'] ) ) ? $this->form['gfpdf_form_settings'] : []; 
  87.  
  88. /** 
  89. * Display our table 
  90. * @since 4.0 
  91. */ 
  92. public function display() { 
  93.  
  94. $singular = rgar( $this->_args, 'singular' ); 
  95. ?> 
  96.  
  97. <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0"> 
  98. <thead> 
  99. <tr> 
  100. <?php $this->print_column_headers(); ?> 
  101. </tr> 
  102. </thead> 
  103.  
  104. <tbody id="the-list" <?php if ( $singular ) { 
  105. echo " class='list:$singular'"; 
  106. } ?>> 
  107. <?php $this->display_rows_or_placeholder(); ?> 
  108. </tbody> 
  109.  
  110. <tfoot> 
  111. <tr> 
  112. <?php $this->print_column_headers( false ); ?> 
  113. </tr> 
  114. </tfoot> 
  115.  
  116. </table> 
  117.  
  118. <?php 
  119.  
  120. /** 
  121. * Output the single table row 
  122. * @param object $item The table row being processed 
  123. * @since 4.0 
  124. */ 
  125. public function single_row( $item ) { 
  126. static $row_class = ''; 
  127.  
  128. $row_class = ( $row_class == '' ? 'class="alternate"' : $row_class ); 
  129.  
  130. echo '<tr id="gfpdf-' . $item['id'] . '" ' . $row_class . '>'; 
  131. $this->single_row_columns( $item ); 
  132. echo '</tr>'; 
  133.  
  134. /** 
  135. * Default column handler 
  136. * Used when no public method exists for the column being processed 
  137. * For developers who want to include additional columns using the `gfpdf_pdf_list_columns` filter 
  138. * there's also an action you can tap into to output the correct column information 
  139. * @param array $item The table row being processed 
  140. * @param string $column 
  141. * @since 4.0 
  142. */ 
  143. public function column_default( $item, $column ) { 
  144.  
  145. $action = 'gfpdf_pdf_list_column_' . $column; 
  146.  
  147. if ( has_action( $action ) ) { 
  148. /** See https://gravitypdf.com/documentation/v4/gfpdf_pdf_list_column_id/ for more details about this action */ 
  149. do_action( $action, $item ); 
  150. } else { 
  151. echo rgar( $item, $column ); 
  152.  
  153. /** 
  154. * Custom public function for displaying the 'cb' column 
  155. * Used to handle active / inactive PDFs 
  156. * @param array $item The table row being processed 
  157. * @since 4.0 
  158. */ 
  159. public function column_cb( $item ) { 
  160.  
  161. $is_active = isset( $item['active'] ) ? $item['active'] : true; 
  162. $form_id = rgget( 'id' ); 
  163. $state_nonce = wp_create_nonce( "gfpdf_state_nonce_{$form_id}_{$item['id']}" ); 
  164. ?> 
  165.  
  166. <img data-id="<?php echo $item['id'] ?>" data-nonce="<?php echo $state_nonce; ?>" 
  167. data-fid="<?php echo $form_id; ?>" 
  168. src="<?php echo $this->gform->get_plugin_url() ?>/images/active<?php echo intval( $is_active ) ?>.png" 
  169. style="cursor: pointer;margin:-1px 0 0 8px;" 
  170. alt="<?php $is_active ? esc_attr__( 'Active', 'gravity-forms-pdf-extended' ) : esc_attr__( 'Inactive', 'gravity-forms-pdf-extended' ); ?>" 
  171. title="<?php echo $is_active ? esc_attr__( 'Active', 'gravity-forms-pdf-extended' ) : esc_attr__( 'Inactive', 'gravity-forms-pdf-extended' ); ?>"/> 
  172.  
  173. <?php 
  174.  
  175. /** 
  176. * Custom public function for displaying the 'notifications' column 
  177. * Display comma separated list of active notifications, otherwise display 'None' 
  178. * @param array $item The table row being processed 
  179. * @since 4.0 
  180. */ 
  181. public function column_notifications( $item ) { 
  182. if ( ! isset( $item['notification'] ) || sizeof( $item['notification'] ) == 0 ) { 
  183. esc_html_e( 'None', 'gravity-forms-pdf-extended' ); 
  184.  
  185. return; 
  186.  
  187. /** Convert our IDs to names */ 
  188. $notification_names = []; 
  189. foreach ( $this->form['notifications'] as $notification ) { 
  190. if ( in_array( $notification['id'], $item['notification'] ) ) { 
  191. $notification_names[] = $notification['name']; 
  192.  
  193. echo implode( ', ', $notification_names ); 
  194.  
  195. /** 
  196. * Translates the template raw name to a user-friendly name 
  197. * @param array $item The table row being processed 
  198. * @since 4.0 
  199. */ 
  200. public function column_shortcode( $item ) { 
  201.  
  202. /** 
  203. * While esc_attr() used below will ensure no display issues when copied the double quote will cause shortcode parse issues 
  204. * We'll prevent this by removing them before hand 
  205. */ 
  206. $name = str_replace( '"', '', $item['name'] ); 
  207.  
  208. /** Prepare our shortcode sample */ 
  209. $shortcode = '[gravitypdf name="' . esc_attr( $name ) . '" id="' . esc_attr( $item['id'] ) . '" text="' . esc_attr__( 'Download PDF', 'gravity-forms-pdf-extended' ) . '"]'; 
  210.  
  211. /** Display in a readonly field */ 
  212. echo '<input type="text" class="gravitypdf_shortcode" value="' . esc_attr( $shortcode ) . '" readonly="readonly" onfocus="jQuery(this).select();" onclick="jQuery(this).select();" />'; 
  213.  
  214. /** 
  215. * Translates the template raw name to a user-friendly name 
  216. * @param array $item The table row being processed 
  217. * @since 4.0 
  218. */ 
  219. public function column_template( $item ) { 
  220.  
  221. $template = $this->templates->get_template_info_by_id( $item['template'] ); 
  222.  
  223. if ( isset( $template['template'] ) ) { 
  224. $template_group = $template['group']; 
  225. $template_name = $this->templates->maybe_add_template_compatibility_notice( $template['template'], $template['required_pdf_version'] ); 
  226.  
  227. ?> 
  228. <strong><?php echo $template_group; ?></strong> <?php echo $template_name; ?> 
  229. <?php 
  230.  
  231. /** 
  232. * Add column actions to allow edit, duplication and deletion 
  233. * @param array $item The table row being processed 
  234. * @since 4.0 
  235. */ 
  236. public function column_name( $item ) { 
  237. $edit_url = add_query_arg( [ 'pid' => $item['id'] ] ); 
  238. $form_id = rgget( 'id' ); 
  239. $duplicate_nonce = wp_create_nonce( "gfpdf_duplicate_nonce_{$form_id}_{$item['id']}" ); 
  240. $delete_nonce = wp_create_nonce( "gfpdf_delete_nonce_{$form_id}_{$item['id']}" ); 
  241.  
  242. $actions = [ 
  243. 'edit' => '<a title="' . esc_attr__( 'Edit this PDF', 'gravity-forms-pdf-extended' ) . '" href="' . $edit_url . '">' . esc_html__( 'Edit', 'gravity-forms-pdf-extended' ) . '</a>',  
  244. 'duplicate' => '<a title="' . esc_attr__( 'Duplicate this PDF', 'gravity-forms-pdf-extended' ) . '" data-id="' . $item['id'] . '" class="submitduplicate" data-nonce="' . $duplicate_nonce . '" data-fid="' . $form_id . '">' . esc_html__( 'Duplicate', 'gravity-forms-pdf-extended' ) . '</a>',  
  245. 'delete' => '<a title="' . esc_attr__( 'Delete this PDF', 'gravity-forms-pdf-extended' ) . '" class="submitdelete" data-id="' . $item['id'] . '" data-nonce="' . $delete_nonce . '" data-fid="' . $form_id . '">' . esc_html__( 'Delete', 'gravity-forms-pdf-extended' ) . '</a>',  
  246. ]; 
  247.  
  248. /** See https://gravitypdf.com/documentation/v4/gfpdf_pdf_actions/ for more details about this filter */ 
  249. $actions = apply_filters( 'gfpdf_pdf_actions', $actions, $item ); 
  250.  
  251. ?> 
  252.  
  253. <a href="<?php echo $edit_url; ?>"><strong><?php echo rgar( $item, 'name' ); ?></strong></a> 
  254. <div class="row-actions"> 
  255.  
  256. <?php 
  257. if ( is_array( $actions ) && ! empty( $actions ) ) { 
  258. $keys = array_keys( $actions ); 
  259. $last_key = array_pop( $keys ); 
  260. foreach ( $actions as $key => $html ) { 
  261. $divider = $key == $last_key ? '' : ' | '; 
  262. ?> 
  263. <span class="<?php echo $key; ?>"> 
  264. <?php echo $html . $divider; ?> 
  265. </span> 
  266. <?php 
  267. ?> 
  268.  
  269. </div> 
  270.  
  271. <?php 
  272.  
  273. /** 
  274. * Copy to display when no PDF configuration options exist 
  275. * @since 4.0 
  276. */ 
  277. public function no_items() { 
  278. printf( esc_html__( "This form doesn't have any PDFs. Let's go %screate one%s.", 'gravity-forms-pdf-extended' ), "<a href='" . add_query_arg( [ 'pid' => 0 ] ) . "'>", '</a>' );