GFPDFControllerController_Actions

Controller_Actions Controller to trigger anything that requires a one-time user interaction Examples include a configuration importer, or a promo.

Defined (1)

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

/src/controller/Controller_Actions.php  
  1. class Controller_Actions extends Helper_Abstract_Controller implements Helper_Interface_Actions { 
  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. * Setup our class by injecting all our dependancies 
  27. * @param Helper_Abstract_Model|\GFPDF\Model\Model_Actions $model Our Actions Model the controller will manage 
  28. * @param Helper_Abstract_View|\GFPDF\View\View_Actions $view Our Actions 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_Notices $notices Our notice class used to queue admin messages and errors 
  32. * @since 4.0 
  33. */ 
  34. public function __construct( Helper_Abstract_Model $model, Helper_Abstract_View $view, Helper_Abstract_Form $gform, LoggerInterface $log, Helper_Notices $notices ) { 
  35.  
  36. /** Assign our internal variables */ 
  37. $this->gform = $gform; 
  38. $this->log = $log; 
  39. $this->notices = $notices; 
  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. $this->add_actions(); 
  55.  
  56. /** 
  57. * Apply any actions 
  58. * @since 4.0 
  59. * @return void 
  60. */ 
  61. public function add_actions() { 
  62. add_action( 'admin_init', [ $this, 'route' ] ); 
  63. add_action( 'admin_init', [ $this, 'route_notices' ], 20 ); /** Run later than our route check */ 
  64.  
  65. /** Add AJAX endpoints */ 
  66. add_action( 'wp_ajax_multisite_v3_migration', [ $this->model, 'ajax_multisite_v3_migration' ] ); 
  67.  
  68. /** 
  69. * Holds our one-time action routes 
  70. * Routes should contain the following keys: action, action_text, condition, process, view, capability 
  71. * action: The action ID to be processed 
  72. * action_text: The text used in our main button 
  73. * condition: The function or method to call to determine if a notice should be displayed (Boolean) 
  74. * process: The function to handle a successful action. On success the disable_route() method should be called 
  75. * view: The function used to display the notice content 
  76. * @return array 
  77. * @since 4.0 
  78. */ 
  79. public function get_routes() { 
  80.  
  81. $routes = [ 
  82. 'action' => 'review_plugin',  
  83. 'action_text' => esc_html__( 'Review Submitted', 'gravity-forms-pdf-extended' ),  
  84. 'condition' => [ $this->model, 'review_condition' ],  
  85. 'process' => [ $this->model, 'dismiss_notice' ],  
  86. 'view' => [ $this->view, 'review_plugin' ],  
  87. 'capability' => 'gravityforms_view_settings',  
  88. 'view_class' => 'gfpdf-alert-mascot',  
  89. ],  
  90.  
  91. 'action' => 'migrate_v3_to_v4',  
  92. 'action_text' => esc_html__( 'Begin Migration', 'gravity-forms-pdf-extended' ),  
  93. 'condition' => [ $this->model, 'migration_condition' ],  
  94. 'process' => [ $this->model, 'begin_migration' ],  
  95. 'view' => [ $this->view, 'migration' ],  
  96. 'capability' => 'update_plugins',  
  97. ],  
  98. ]; 
  99.  
  100. /** See https://gravitypdf.com/documentation/v4/gfpdf_one_time_action_routes/ for more details about this filter */ 
  101.  
  102. return apply_filters( 'gfpdf_one_time_action_routes', $routes ); 
  103.  
  104. /** 
  105. * Setup our route notices, if they should be enabled 
  106. * @return void 
  107. * @since 4.0 
  108. */ 
  109. public function route_notices() { 
  110.  
  111. /** Prevent actions being displayed on our welcome pages */ 
  112. if ( ! is_admin() || 
  113. ( rgget( 'page' ) == 'gfpdf-getting-started' ) || ( rgget( 'page' ) == 'gfpdf-update' ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 
  114. ) { 
  115. return null; 
  116.  
  117. foreach ( $this->get_routes() as $route ) { 
  118.  
  119. /** Before displaying check the user has the correct capabilities, the notice isn't already been dismissed and the route condition has been met */ 
  120. if ( $this->gform->has_capability( $route['capability'] ) && 
  121. ! $this->model->is_notice_already_dismissed( $route['action'] ) && 
  122. call_user_func( $route['condition'] ) 
  123. ) { 
  124.  
  125. $this->log->addNotice( 'Trigger Action Notification.', [ 
  126. 'route' => $route,  
  127. ] ); 
  128.  
  129. $class = ( isset( $route['view_class'] ) ) ? $route['view_class'] : ''; 
  130. $this->notices->add_notice( call_user_func( $route['view'], $route['action'], $route['action_text'] ), $class ); 
  131.  
  132. /** 
  133. * Run approprate events 
  134. * @return void 
  135. * @since 4.0 
  136. */ 
  137. public function route() { 
  138.  
  139. foreach ( $this->get_routes() as $route ) { 
  140.  
  141. /** Check we have a valid action and the display condition is true */ 
  142. if ( rgpost( 'gfpdf_action' ) == 'gfpdf_' . $route['action'] && call_user_func( $route['condition'] ) ) { 
  143.  
  144. /** Check user capability */ 
  145. if ( ! $this->gform->has_capability( $route['capability'] ) ) { 
  146.  
  147. $this->log->addCritical( 'Lack of User Capabilities.', [ 
  148. 'user' => wp_get_current_user(),  
  149. 'user_meta' => get_user_meta( get_current_user_id() ),  
  150. ] ); 
  151.  
  152. wp_die( esc_html__( 'You do not have permission to access this page', 'gravity-forms-pdf-extended' ) ); 
  153.  
  154. /** Check nonce is valid */ 
  155. if ( ! wp_verify_nonce( rgpost( 'gfpdf_action_' . $route['action'] ), 'gfpdf_action_' . $route['action'] ) ) { 
  156.  
  157. $this->log->addWarning( 'Nonce Verification Failed.' ); 
  158. $this->notices->add_error( esc_html__( 'There was a problem processing the action. Please try again.', 'gravity-forms-pdf-extended' ) ); 
  159.  
  160. continue; 
  161.  
  162. /** Check if the user wants to dismiss the notice, otherwise process the route */ 
  163. if ( isset( $_POST['gfpdf-dismiss-notice'] ) ) { 
  164. $this->log->addNotice( 'Dismiss Action.', [ 
  165. 'route' => $route,  
  166. ] ); 
  167.  
  168. $this->model->dismiss_notice( $route['action'] ); 
  169. } else { 
  170. $this->log->addNotice( 'Trigger Action Process.', [ 
  171. 'route' => $route,  
  172. ] ); 
  173.  
  174. call_user_func( $route['process'], $route['action'], $route );