GFPDFControllerController_Install

Controller_Installer Controls the installation and uninstallation of Gravity PDF.

Defined (1)

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

/src/controller/Controller_Install.php  
  1. class Controller_Install 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_Install $model Our Install Model the controller will manage 
  44. * @param \GFPDF\Helper\Helper_Abstract_Form $gform Our Install View the controller will manage 
  45. * @param \Monolog\Logger|LoggerInterface $log Our logger class 
  46. * @param \GFPDF\Helper\Helper_Notices $notices Our notice class used to queue admin messages and errors 
  47. * @param \GFPDF\Helper\Helper_Data $data Our plugin data store 
  48. * @param \GFPDF\Helper\Helper_Misc $misc Our miscellaneous methods 
  49. * @since 4.0 
  50. */ 
  51. public function __construct( Helper_Abstract_Model $model, Helper_Abstract_Form $gform, LoggerInterface $log, Helper_Notices $notices, Helper_Data $data, Helper_Misc $misc ) { 
  52.  
  53. /** Assign our internal variables */ 
  54. $this->gform = $gform; 
  55. $this->log = $log; 
  56. $this->notices = $notices; 
  57. $this->data = $data; 
  58. $this->misc = $misc; 
  59.  
  60. /** Load our model and view */ 
  61. $this->model = $model; 
  62. $this->model->setController( $this ); 
  63.  
  64. /** 
  65. * Initialise our class defaults 
  66. * @since 4.0 
  67. * @return void 
  68. */ 
  69. public function init() { 
  70. $this->add_actions(); 
  71. $this->add_filters(); 
  72.  
  73. /** 
  74. * Apply any actions needed for the settings page 
  75. * @since 4.0 
  76. * @return void 
  77. */ 
  78. public function add_actions() { 
  79. add_action( 'admin_init', [ $this, 'maybe_uninstall' ] ); 
  80. add_action( 'wp_loaded', [ $this, 'check_install_status' ], 9999 ); 
  81.  
  82. /** rewrite endpoints */ 
  83. add_action( 'init', [ $this->model, 'register_rewrite_rules' ] ); 
  84.  
  85. /** 
  86. * Apply any filters needed for the settings page 
  87. * @since 4.0 
  88. * @return void 
  89. */ 
  90. public function add_filters() { 
  91. /** rewrite filters */ 
  92. add_filter( 'query_vars', [ $this->model, 'register_rewrite_tags' ] ); 
  93.  
  94. /** 
  95. * Set up data related to the plugin setup and installation 
  96. * @return void 
  97. * @since 4.0 
  98. */ 
  99. public function setup_defaults() { 
  100.  
  101. $this->data->is_installed = $this->model->is_installed(); 
  102. $this->data->permalink = $this->model->get_permalink_regex(); 
  103. $this->data->working_folder = $this->model->get_working_directory(); 
  104. $this->data->settings_url = $this->model->get_settings_url(); 
  105.  
  106. $this->data->memory_limit = ini_get( 'memory_limit' ); 
  107.  
  108. $upload_details = $this->misc->get_upload_details(); 
  109. $this->data->upload_dir = $upload_details['path']; 
  110. $this->data->upload_dir_url = $upload_details['url']; 
  111.  
  112. $this->model->setup_template_location(); 
  113. $this->model->setup_multisite_template_location(); 
  114. $this->model->create_folder_structures(); 
  115.  
  116. /** 
  117. * Check the software has been installed on this website before and 
  118. * the version numbers are in sync 
  119. * @return void 
  120. * @since 4.0 
  121. */ 
  122. public function check_install_status() { 
  123.  
  124. if ( ! is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ! current_user_can( 'activate_plugins' ) ) { 
  125. return null; 
  126.  
  127. if ( ! $this->data->is_installed ) { 
  128. $this->model->install_plugin(); 
  129.  
  130. if ( PDF_EXTENDED_VERSION !== get_option( 'gfpdf_current_version' ) ) { 
  131. /** See https://gravitypdf.com/documentation/v4/gfpdf_version_changed/ for more details about this action */ 
  132. do_action( 'gfpdf_version_changed', get_option( 'gfpdf_current_version' ), PDF_EXTENDED_VERSION ); 
  133. update_option( 'gfpdf_current_version', PDF_EXTENDED_VERSION ); 
  134.  
  135. /** 
  136. * Determine if we should be saving the PDF settings 
  137. * @return void 
  138. * @since 4.0 
  139. */ 
  140. public function maybe_uninstall() { 
  141.  
  142. /** check if we should be uninstalling */ 
  143. if ( rgpost( 'gfpdf_uninstall' ) ) { 
  144.  
  145. /** Check Nonce is valid */ 
  146. if ( ! wp_verify_nonce( rgpost( 'gfpdf-uninstall-plugin' ), 'gfpdf-uninstall-plugin' ) ) { 
  147. $this->notices->add_error( esc_html__( 'There was a problem uninstalling Gravity PDF. Please try again.', 'gravity-forms-pdf-extended' ) ); 
  148. $this->log->addWarning( 'Nonce Verification Failed.' ); 
  149.  
  150. return null; 
  151.  
  152. /** 
  153. * Run the uninstaller if the user has the correct permissions 
  154. * If not a multisite any user with the GF uninstaller permission can remove it (usually just admins) 
  155. * If multisite only the super admin can uninstall the software. This is due to how the plugin shares similar directory structures across networked sites 
  156. */ 
  157. if ( ( ! is_multisite() && ! $this->gform->has_capability( 'gravityforms_uninstall' ) ) || 
  158. ( is_multisite() && ! is_super_admin() ) 
  159. ) { 
  160.  
  161. $this->log->addCritical( 'Lack of User Capabilities.', [ 
  162. 'user' => wp_get_current_user(),  
  163. 'user_meta' => get_user_meta( get_current_user_id() ),  
  164. ] ); 
  165.  
  166. wp_die( esc_html__( 'Access Denied' ), 403 ); 
  167.  
  168. $this->model->uninstall_plugin(); 
  169. $this->model->redirect_to_plugins_page(); 
  170.