GFPDFControllerController_PDF

Controller_PDF Handles the PDF display and authentication.

Defined (1)

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

/src/controller/Controller_PDF.php  
  1. class Controller_PDF extends Helper_Abstract_Controller implements Helper_Interface_Actions, Helper_Interface_Filters { 
  2.  
  3. /** 
  4. * Holds the abstracted Gravity Forms API specific to Gravity PDF 
  5. * @var \GFPDF\Helper\Helper_Form 
  6. * @since 4.0 
  7. */ 
  8. protected $gform; 
  9.  
  10. /** 
  11. * Holds our log class 
  12. * @var \Monolog\Logger|LoggerInterface 
  13. * @since 4.0 
  14. */ 
  15. protected $log; 
  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. * Setup our class by injecting all our dependancies 
  27. * @param Helper_Abstract_Model|\GFPDF\Model\Model_PDF $model Our PDF Model the controller will manage 
  28. * @param Helper_Abstract_View|\GFPDF\View\View_PDF $view Our PDF View the controller will manage 
  29. * @param \GFPDF\Helper\Helper_Abstract_Form $gform Our abstracted Gravity Forms helper functions 
  30. * @param \Monolog\Logger|LoggerInterface $log Our logger class 
  31. * @param \GFPDF\Helper\Helper_Misc $misc Our miscellaneous class 
  32. * @since 4.0 
  33. */ 
  34. public function __construct( Helper_Abstract_Model $model, Helper_Abstract_View $view, Helper_Abstract_Form $gform, LoggerInterface $log, Helper_Misc $misc ) { 
  35.  
  36. /** Assign our internal variables */ 
  37. $this->gform = $gform; 
  38. $this->log = $log; 
  39. $this->misc = $misc; 
  40.  
  41. /** Load our model and view */ 
  42. $this->model = $model; 
  43. $this->model->setController( $this ); 
  44.  
  45. $this->view = $view; 
  46. $this->view->setController( $this ); 
  47.  
  48. /** 
  49. * Initialise our class defaults 
  50. * @since 4.0 
  51. * @return void 
  52. */ 
  53. public function init() { 
  54. /** 
  55. * Tell Gravity Forms to add our form PDF settings pages 
  56. */ 
  57. $this->add_actions(); 
  58. $this->add_filters(); 
  59.  
  60. /** Add scheduled tasks */ 
  61. if ( ! wp_next_scheduled( 'gfpdf_cleanup_tmp_dir' ) ) { 
  62. wp_schedule_event( time(), 'daily', 'gfpdf_cleanup_tmp_dir' ); 
  63.  
  64. /** 
  65. * Apply any actions needed for the settings page 
  66. * @since 4.0 
  67. * @return void 
  68. */ 
  69. public function add_actions() { 
  70. /** Process PDF if needed */ 
  71. add_action( 'parse_request', [ $this, 'process_legacy_pdf_endpoint' ] ); /** legacy PDF endpoint */ 
  72. add_action( 'parse_request', [ $this, 'process_pdf_endpoint' ] ); /** new PDF endpoint */ 
  73.  
  74. /** Display PDF links in Gravity Forms Admin Area */ 
  75. add_action( 'gform_entries_first_column_actions', [ $this->model, 'view_pdf_entry_list' ], 10, 4 ); 
  76. add_action( 'gform_entry_info', [ $this->model, 'view_pdf_entry_detail' ], 10, 2 ); 
  77.  
  78. /** Add save PDF filter */ 
  79. add_action( 'gform_after_submission', [ $this->model, 'maybe_save_pdf' ], 10, 2 ); 
  80.  
  81. /** Clean-up actions */ 
  82. add_action( 'gform_after_submission', [ $this->model, 'cleanup_pdf' ], 9999, 2 ); 
  83. add_action( 'gform_after_update_entry', [ $this->model, 'cleanup_pdf_after_submission' ], 9999, 2 ); 
  84. add_action( 'gfpdf_cleanup_tmp_dir', [ $this->model, 'cleanup_tmp_dir' ] ); 
  85.  
  86. /** 
  87. * Apply any filters needed for the settings page 
  88. * @since 4.0 
  89. * @return void 
  90. */ 
  91. public function add_filters() { 
  92. /** PDF authentication middleware */ 
  93. add_filter( 'gfpdf_pdf_middleware', [ $this->model, 'middle_public_access' ], 10, 3 ); 
  94. add_filter( 'gfpdf_pdf_middleware', [ $this->model, 'middle_active' ], 20, 3 ); 
  95. add_filter( 'gfpdf_pdf_middleware', [ $this->model, 'middle_conditional' ], 30, 3 ); 
  96. add_filter( 'gfpdf_pdf_middleware', [ $this->model, 'middle_owner_restriction' ], 40, 3 ); 
  97. add_filter( 'gfpdf_pdf_middleware', [ $this->model, 'middle_logged_out_timeout' ], 50, 3 ); 
  98. add_filter( 'gfpdf_pdf_middleware', [ $this->model, 'middle_auth_logged_out_user' ], 60, 3 ); 
  99. add_filter( 'gfpdf_pdf_middleware', [ $this->model, 'middle_user_capability' ], 70, 3 ); 
  100.  
  101. /** Tap into GF notifications */ 
  102. add_filter( 'gform_notification', [ 
  103. $this->model,  
  104. 'notifications',  
  105. ], 9999, 3 ); /** ensure Gravity PDF is one of the last filters to be applied */ 
  106.  
  107. /** Modify mPDF's path locations */ 
  108. add_filter( 'mpdf_tmp_path', [ $this->model, 'mpdf_tmp_path' ] ); 
  109. add_filter( 'mpdf_fontdata_path', [ $this->model, 'mpdf_tmp_font_path' ] ); 
  110.  
  111. /** Change mPDF settings */ 
  112. add_filter( 'mpdf_current_font_path', [ $this->model, 'set_current_pdf_font' ], 10, 2 ); 
  113. add_filter( 'mpdf_font_data', [ $this->model, 'register_custom_font_data_with_mPDF' ] ); 
  114. add_filter( 'mpdf_font_data', [ $this->model, 'add_unregistered_fonts_to_mPDF' ], 20 ); 
  115.  
  116. /** Process mergetags and shortcodes in PDF */ 
  117. add_filter( 'gfpdf_pdf_html_output', [ $this->gform, 'process_tags' ], 10, 3 ); 
  118. add_filter( 'gfpdf_pdf_html_output', 'do_shortcode' ); 
  119.  
  120. add_filter( 'gfpdf_pdf_core_template_html_output', [ $this->gform, 'process_tags' ], 10, 3 ); 
  121.  
  122. /** Backwards compatibility for our Tier 2 plugin */ 
  123. add_filter( 'gfpdfe_pre_load_template', [ 'PDFRender', 'prepare_ids' ], 1, 8 ); 
  124.  
  125. /** Pre-process our template arguments and automatically render them in PDF */ 
  126. add_filter( 'gfpdf_template_args', [ $this->model, 'preprocess_template_arguments' ] ); 
  127. add_filter( 'gfpdf_pdf_html_output', [ $this->view, 'autoprocess_core_template_options' ], 5, 4 ); 
  128.  
  129. /** Cleanup filters */ 
  130. add_filter( 'gform_before_resend_notifications', [ $this->model, 'resend_notification_pdf_cleanup' ], 10, 2 ); 
  131.  
  132. /** 
  133. * Determines if we should process the PDF at this stage 
  134. * Fires just before the main WP_Query is executed (we don't need it) 
  135. * @since 4.0 
  136. * @return void 
  137. */ 
  138. public function process_pdf_endpoint() { 
  139.  
  140. /** exit early if all the required URL parameters aren't met */ 
  141. if ( empty( $GLOBALS['wp']->query_vars['gpdf'] ) || empty( $GLOBALS['wp']->query_vars['pid'] ) || empty( $GLOBALS['wp']->query_vars['lid'] ) ) { 
  142. return null; 
  143.  
  144. $pid = $GLOBALS['wp']->query_vars['pid']; 
  145. $lid = (int) $GLOBALS['wp']->query_vars['lid']; 
  146. $action = ( ( isset( $GLOBALS['wp']->query_vars['action'] ) ) && $GLOBALS['wp']->query_vars['action'] == 'download' ) ? 'download' : 'view'; 
  147.  
  148. $this->log->addNotice( 'Processing PDF endpoint.', [ 
  149. 'pid' => $pid,  
  150. 'lid' => $lid,  
  151. 'action' => $action,  
  152. ] ); 
  153.  
  154. /** Send to our model to handle validation / authentication */ 
  155. $results = $this->model->process_pdf( $pid, $lid, $action ); 
  156.  
  157. /** if error, display to user */ 
  158. if ( is_wp_error( $results ) ) { 
  159. $this->pdf_error( $results ); 
  160.  
  161. /** 
  162. * Determines if we should process the legacy PDF endpoint at this stage (the one with $_GET variables) 
  163. * Fires just before the main WP_Query is executed (we don't need it) 
  164. * @since 4.0 
  165. * @return void 
  166. */ 
  167. public function process_legacy_pdf_endpoint() { 
  168.  
  169. /** exit early if all our required parameters aren't met */ 
  170. if ( empty( $_GET['gf_pdf'] ) || empty( $_GET['fid'] ) || empty( $_GET['lid'] ) || empty( $_GET['template'] ) ) { 
  171. return null; 
  172.  
  173. $config = [ 
  174. 'lid' => $_GET['lid'],  
  175. 'fid' => (int) $_GET['fid'],  
  176. 'aid' => ( isset( $_GET['aid'] ) ) ? (int) $_GET['aid'] : false,  
  177. 'template' => substr( $_GET['template'], 0, -4 ), /** strip .php from the template name */ 
  178. 'action' => ( isset( $_GET['download'] ) ) ? 'download' : 'view',  
  179. ]; 
  180.  
  181. $this->log->addNotice( 'Processing Legacy PDF endpoint.', [ 
  182. 'config' => $config,  
  183. ] ); 
  184.  
  185. /** Attempt to find a valid config */ 
  186. $pid = $this->model->get_legacy_config( $config ); 
  187.  
  188. if ( is_wp_error( $pid ) ) { 
  189. return $this->pdf_error( $pid ); 
  190.  
  191. /** Store our ids in the WP query_vars object */ 
  192. $GLOBALS['wp']->query_vars['gpdf'] = 1; 
  193. $GLOBALS['wp']->query_vars['pid'] = $pid; 
  194. $GLOBALS['wp']->query_vars['lid'] = $config['lid']; 
  195.  
  196. /** Send to our model to handle validation / authentication */ 
  197. $results = $this->model->process_pdf( $pid, $config['lid'], $config['action'] ); 
  198.  
  199. /** if error, display to user */ 
  200. if ( is_wp_error( $results ) ) { 
  201. $this->pdf_error( $results ); 
  202.  
  203. /** 
  204. * Output PDF error to user 
  205. * @param Object $error The WP_Error object 
  206. * @return void 
  207. * @since 4.0 
  208. */ 
  209. private function pdf_error( $error ) { 
  210.  
  211. $this->log->addError( 'PDF Generation Error.', [ 
  212. 'WP_Error_Message' => $error->get_error_message(),  
  213. 'WP_Error_Code' => $error->get_error_code(),  
  214. ] ); 
  215.  
  216. /** only display detailed error to admins */ 
  217. $whitelist_errors = [ 'timeout_expired', 'access_denied' ]; 
  218. if ( $this->gform->has_capability( 'gravityforms_view_settings' ) || in_array( $error->get_error_code(), $whitelist_errors ) ) { 
  219. wp_die( $error->get_error_message() ); 
  220. } else { 
  221. wp_die( esc_html__( 'There was a problem generating your PDF', 'gravity-forms-pdf-extended' ) );