GFPDFControllerController_Settings

Controller_Settings A general class for the global PDF settings.

Defined (1)

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

/src/controller/Controller_Settings.php  
  1. class Controller_Settings 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_Notices object 
  19. * which we can use to queue up admin messages for the user 
  20. * @var \GFPDF\Helper\Helper_Notices 
  21. * @since 4.0 
  22. */ 
  23. protected $notices; 
  24.  
  25. /** 
  26. * Holds our Helper_Data object 
  27. * which we can autoload with any data needed 
  28. * @var \GFPDF\Helper\Helper_Data 
  29. * @since 4.0 
  30. */ 
  31. protected $data; 
  32.  
  33. /** 
  34. * Holds our Helper_Misc object 
  35. * Makes it easy to access common methods throughout the plugin 
  36. * @var \GFPDF\Helper\Helper_Misc 
  37. * @since 4.0 
  38. */ 
  39. protected $misc; 
  40.  
  41. /** 
  42. * Setup our class by injecting all our dependancies 
  43. * @param Helper_Abstract_Model|\GFPDF\Model\Model_Settings $model Our Settings Model the controller will manage 
  44. * @param Helper_Abstract_View|\GFPDF\View\View_Settings $view Our Settings View the controller will manage 
  45. * @param \GFPDF\Helper\Helper_Abstract_Form $gform Our abstracted Gravity Forms helper functions 
  46. * @param \Monolog\Logger|LoggerInterface $log Our logger class 
  47. * @param \GFPDF\Helper\Helper_Notices $notices Our notice class used to queue admin messages and errors 
  48. * @param \GFPDF\Helper\Helper_Data $data Our plugin data store 
  49. * @param \GFPDF\Helper\Helper_Misc $misc Our miscellaneous class 
  50. * @since 4.0 
  51. */ 
  52. public function __construct( Helper_Abstract_Model $model, Helper_Abstract_View $view, Helper_Abstract_Form $gform, LoggerInterface $log, Helper_Notices $notices, Helper_Data $data, Helper_Misc $misc ) { 
  53.  
  54. /** Assign our internal variables */ 
  55. $this->gform = $gform; 
  56. $this->log = $log; 
  57. $this->notices = $notices; 
  58. $this->data = $data; 
  59. $this->misc = $misc; 
  60.  
  61. /** Load our model and view */ 
  62. $this->model = $model; 
  63. $this->model->setController( $this ); 
  64.  
  65. $this->view = $view; 
  66. $this->view->setController( $this ); 
  67.  
  68. /** 
  69. * Initialise our class defaults 
  70. * @since 4.0 
  71. * @return void 
  72. */ 
  73. public function init() { 
  74.  
  75. /** 
  76. * Tell Gravity Forms to initiate our settings page 
  77. * Using the following Class/Model 
  78. */ 
  79. GFForms::add_settings_page( $this->data->short_title, [ $this, 'display_page' ] ); 
  80.  
  81. /** Ensure any errors are stored correctly */ 
  82. $this->model->setup_form_settings_errors(); 
  83.  
  84. /** run actions and filters */ 
  85. $this->add_actions(); 
  86. $this->add_filters(); 
  87.  
  88. /** 
  89. * Apply any actions needed for the settings page 
  90. * @since 4.0 
  91. * @return void 
  92. */ 
  93. public function add_actions() { 
  94.  
  95. /** Display our system status on general and tools pages */ 
  96. add_action( 'gfpdf_post_general_settings_page', [ $this->view, 'system_status' ] ); 
  97. add_action( 'gfpdf_post_tools_settings_page', [ $this->view, 'system_status' ] ); 
  98.  
  99. /** 
  100. * Display the uninstaller if use has the correct permissions 
  101. * If not a multisite any user with the GF uninstaller permission can remove it (usually just admins) 
  102. * If multisite only the super admin can uninstall the software. This is due to how the plugin shares similar directory structures across networked sites 
  103. */ 
  104. if ( ( ! is_multisite() && $this->gform->has_capability( 'gravityforms_uninstall' ) ) || 
  105. ( is_multisite() && is_super_admin() ) 
  106. ) { 
  107. add_action( 'gfpdf_post_tools_settings_page', [ $this->view, 'uninstaller' ], 5 ); 
  108.  
  109. /** Process the tool tab actions */ 
  110. add_action( 'admin_init', [ $this, 'process_tool_tab_actions' ] ); 
  111.  
  112. /** 
  113. * Add AJAX Action Endpoints 
  114. */ 
  115. add_action( 'wp_ajax_gfpdf_font_save', [ $this->model, 'save_font' ] ); 
  116. add_action( 'wp_ajax_gfpdf_font_delete', [ $this->model, 'delete_font' ] ); 
  117. add_action( 'wp_ajax_gfpdf_has_pdf_protection', [ $this->model, 'check_tmp_pdf_security' ] ); 
  118.  
  119.  
  120. /** 
  121. * Apply any filters needed for the settings page 
  122. * @since 4.0 
  123. * @return void 
  124. */ 
  125. public function add_filters() { 
  126.  
  127. /** Add tooltips */ 
  128. add_filter( 'gform_tooltips', [ $this->view, 'add_tooltips' ] ); 
  129.  
  130. /** If trying to save settings page we'll use this filter to apply any errors passed back from options.php */ 
  131. if ( $this->misc->is_gfpdf_page() ) { 
  132. add_filter( 'gfpdf_registered_fields', [ $this->model, 'highlight_errors' ] ); 
  133. add_filter( 'admin_notices', 'settings_errors' ); 
  134. add_filter( 'gfpdf_localised_script_array', array( $this->model, 'get_template_data' ) ); 
  135.  
  136. /** make capability text user friendly */ 
  137. add_filter( 'gfpdf_capability_name', [ $this->model, 'style_capabilities' ] ); 
  138.  
  139. /** change capability needed to edit settings page */ 
  140. add_filter( 'option_page_capability_gfpdf_settings', [ $this, 'edit_options_cap' ] ); 
  141. add_filter( 'gravitypdf_settings_navigation', [ $this, 'disable_tools_on_view_cap' ] ); 
  142.  
  143. /** allow TTF uploads */ 
  144. add_filter( 'upload_mimes', [ $this, 'allow_font_uploads' ] ); 
  145.  
  146. /** 
  147. * Display the settings page for Gravity PDF 
  148. * @since 4.0 
  149. * @return void 
  150. */ 
  151. public function display_page() { 
  152.  
  153. $page = ( isset( $_GET['tab'] ) ) ? $_GET['tab'] : 'general'; 
  154.  
  155. switch ( $page ) { 
  156. case 'general': 
  157. $this->view->general(); 
  158. break; 
  159.  
  160. case 'tools': 
  161. $this->view->tools(); 
  162. break; 
  163.  
  164. case 'help': 
  165. $this->view->help(); 
  166. break; 
  167.  
  168. /** 
  169. * Check our current user has the correct capability 
  170. * @since 4.0 
  171. * @return string 
  172. */ 
  173. public function edit_options_cap() { 
  174.  
  175. /** because current_user_can() doesn't handle Gravity Forms permissions quite correct we'll do our checks here */ 
  176. if ( ! $this->gform->has_capability( 'gravityforms_edit_settings' ) ) { 
  177.  
  178. $this->log->addCritical( 'Lack of User Capabilities.', [ 
  179. 'user' => wp_get_current_user(),  
  180. 'user_meta' => get_user_meta( get_current_user_id() ),  
  181. ] ); 
  182.  
  183. wp_die( esc_html__( 'Access Denied' ), 403 ); 
  184.  
  185. /** the user is authenticated by the above so let's pass in the lowest permissions */ 
  186.  
  187. return 'read'; 
  188.  
  189. /** 
  190. * Return our custom capability 
  191. * @param array $nav The existing settings navigation 
  192. * @since 4.0 
  193. * @return array 
  194. */ 
  195. public function disable_tools_on_view_cap( $nav ) { 
  196.  
  197. if ( ! $this->gform->has_capability( 'gravityforms_edit_settings' ) ) { 
  198. $this->log->addNotice( 'Lack of User Capabilities' ); 
  199.  
  200. unset( $nav[100] ); /** remove tools tab */ 
  201.  
  202. return $nav; 
  203.  
  204. /** 
  205. * Check if any of the tool tab actions have been triggered and process 
  206. * @return void|boolean 
  207. * @since 4.0 
  208. */ 
  209. public function process_tool_tab_actions() { 
  210.  
  211. /** check if we are on the tools settings page */ 
  212. if ( ! $this->misc->is_gfpdf_settings_tab( 'tools' ) ) { 
  213. return null; 
  214.  
  215. /** check if the user has permission to copy the templates */ 
  216. if ( ! $this->gform->has_capability( 'gravityforms_edit_settings' ) ) { 
  217.  
  218. $this->log->addCritical( 'Lack of User Capabilities.', [ 
  219. 'user' => wp_get_current_user(),  
  220. 'user_meta' => get_user_meta( get_current_user_id() ),  
  221. ] ); 
  222.  
  223. return null; 
  224.  
  225. $settings = rgpost( 'gfpdf_settings' ); 
  226.  
  227. /** Only run checks if the gfpdf_settings POST data exists */ 
  228. if ( empty( $settings ) ) { 
  229. return null; 
  230.  
  231. /** check if we should install the custom templates */ 
  232. if ( isset( $settings['setup_templates']['name'] ) && isset( $settings['setup_templates']['nonce'] ) ) { 
  233. /** verify the nonce */ 
  234. if ( ! wp_verify_nonce( $settings['setup_templates']['nonce'], 'gfpdf_settings[setup_templates]' ) ) { 
  235. $this->log->addWarning( 'Nonce Verification Failed.' ); 
  236. $this->notices->add_error( esc_html__( 'There was a problem installing the PDF templates. Please try again.', 'gravity-forms-pdf-extended' ) ); 
  237.  
  238. return null; 
  239.  
  240. return $this->model->install_templates(); 
  241.  
  242. /** See https://gravitypdf.com/documentation/v4/gfpdf_tool_tab_actions/ for more details about this action */ 
  243. do_action( 'gfpdf_tool_tab_actions', $settings ); 
  244.  
  245. /** 
  246. * Add .ttf to upload whitelist 
  247. * @param array $mime_types 
  248. * @return array 
  249. * @since 4.0 
  250. */ 
  251. public function allow_font_uploads( $mime_types = [] ) { 
  252. $mime_types['ttf'] = 'application/x-font-ttf'; 
  253.  
  254. return $mime_types;