GFPDFModelModel_Actions

Model_Actions.

Defined (1)

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

/src/model/Model_Actions.php  
  1. class Model_Actions extends Helper_Abstract_Model { 
  2.  
  3. /** 
  4. * Holds our Helper_Data object 
  5. * which we can autoload with any data needed 
  6. * @var \GFPDF\Helper\Helper_Data 
  7. * @since 4.0 
  8. */ 
  9. protected $data; 
  10.  
  11. /** 
  12. * Holds our Helper_Abstract_Options / Helper_Options_Fields object 
  13. * Makes it easy to access global PDF settings and individual form PDF settings 
  14. * @var \GFPDF\Helper\Helper_Options_Fields 
  15. * @since 4.0 
  16. */ 
  17. protected $options; 
  18.  
  19. /** 
  20. * Holds our Helper_Notices object 
  21. * which we can use to queue up admin messages for the user 
  22. * @var \GFPDF\Helper\Helper_Notices 
  23. * @since 4.0 
  24. */ 
  25. protected $notices; 
  26.  
  27. /** 
  28. * Setup our class by injecting all our dependancies 
  29. * @param \GFPDF\Helper\Helper_Data $data Our plugin data store 
  30. * @param \GFPDF\Helper\Helper_Abstract_Options $options Our options class which allows us to access any settings 
  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_Data $data, Helper_Abstract_Options $options, Helper_Notices $notices ) { 
  35.  
  36. /** Assign our internal variables */ 
  37. $this->data = $data; 
  38. $this->options = $options; 
  39. $this->notices = $notices; 
  40.  
  41. /** 
  42. * Check if the current notice has already been dismissed 
  43. * @param string $type The current notice ID 
  44. * @return boolean True if dismissed, false otherwise 
  45. * @since 4.0 
  46. */ 
  47. public function is_notice_already_dismissed( $type ) { 
  48.  
  49. $dismissed_notices = $this->options->get_option( 'action_dismissal', [] ); 
  50.  
  51. if ( isset( $dismissed_notices[ $type ] ) ) { 
  52. return true; 
  53.  
  54. return false; 
  55.  
  56. /** 
  57. * Mark the current notice as being dismissed 
  58. * @param string $type The current notice ID 
  59. * @return void 
  60. * @since 4.0 
  61. */ 
  62. public function dismiss_notice( $type ) { 
  63.  
  64. $dismissed_notices = $this->options->get_option( 'action_dismissal', [] ); 
  65. $dismissed_notices[ $type ] = $type; 
  66. $this->options->update_option( 'action_dismissal', $dismissed_notices ); 
  67.  
  68. /** 
  69. * Check if our review notice condition has been met 
  70. * A review will only display if more than 100 PDFs have been generated 
  71. * @return boolean 
  72. * @since 4.0 
  73. */ 
  74. public function review_condition() { 
  75.  
  76. /** Check we are on a Gravity Forms page and we have more than 100 PDFs */ 
  77. if ( GFForms::is_gravity_page() && 100 < (int) $this->options->get_option( 'pdf_count', 0 ) ) { 
  78. return true; 
  79.  
  80. return false; 
  81.  
  82. /** 
  83. * Check if our v3 configuration file exists 
  84. * @return boolean 
  85. * @since 4.0 
  86. */ 
  87. public function migration_condition() { 
  88.  
  89. /** Check standard installation */ 
  90. if ( ! is_multisite() && is_file( $this->data->template_location . 'configuration.php' ) ) { 
  91. return true; 
  92.  
  93. /** Check multisite installation */ 
  94. if ( is_multisite() && is_super_admin() ) { 
  95. if ( is_file( $this->data->multisite_template_location . 'configuration.php' ) ) { 
  96. return true; 
  97. } else { 
  98. /** Check other multisites for a config file */ 
  99. $sites = ( function_exists( 'get_sites' ) ) ? get_sites() : wp_get_sites(); 
  100.  
  101. foreach ( $sites as $site ) { 
  102. $site = (array) $site; /** Back-compat: ensure the new site object introduced in 4.6 gets converted back to an array */ 
  103. if ( is_file( $this->data->template_location . '/' . $site['blog_id'] . '/configuration.php' ) ) { 
  104. return true; 
  105.  
  106. return false; 
  107.  
  108. /** 
  109. * Process our v3 to v4 migration 
  110. * @return boolean 
  111. * @since 4.0 
  112. */ 
  113. public function begin_migration() { 
  114.  
  115. if ( is_multisite() ) { 
  116.  
  117. /** Verify we have a site to migrate */ 
  118. $sites = ( function_exists( 'get_sites' ) ) ? get_sites() : wp_get_sites(); 
  119. $found = false; 
  120.  
  121. foreach ( $sites as $site ) { 
  122. $site = (array) $site; /** Back-compat: ensure the new site object introduced in 4.6 gets converted back to an array */ 
  123. $site_config = $this->data->template_location . '/' . $site['blog_id'] . '/'; 
  124.  
  125. if ( is_file( $site_config . 'configuration.php' ) ) { 
  126. $found = true; 
  127. break; 
  128.  
  129. if ( $found ) { 
  130. /** Remove all notices to prevent any messages showing up on the migration screen */ 
  131. remove_all_actions( 'network_admin_notices' ); 
  132. remove_all_actions( 'admin_notices' ); 
  133. remove_all_actions( 'all_admin_notices' ); 
  134.  
  135. /** We need a user interface so queue this right before the admin page runs */ 
  136. add_action( 'all_admin_notices', [ $this, 'handle_multisite_migration' ] ); 
  137.  
  138. /** Add our migration script */ 
  139. wp_enqueue_script( 'gfpdf_js_v3_migration' ); 
  140.  
  141. } else if ( is_file( $this->data->template_location . 'configuration.php' ) ) { 
  142. $this->migrate_v3( $this->data->template_location ); 
  143.  
  144. /** 
  145. * Does the migration and notice clearing (if unsuccessful) 
  146. * @param string $path Path to the current site's template directory 
  147. * @return boolean 
  148. * @since 4.0 
  149. */ 
  150. private function migrate_v3( $path ) { 
  151.  
  152. $migration = new Helper_Migration( 
  153. GPDFAPI::get_form_class(),  
  154. GPDFAPI::get_log_class(),  
  155. GPDFAPI::get_data_class(),  
  156. GPDFAPI::get_options_class(),  
  157. GPDFAPI::get_misc_class(),  
  158. GPDFAPI::get_notice_class(),  
  159. GPDFAPI::get_templates_class() 
  160. ); 
  161.  
  162. if ( $migration->begin_migration() ) { 
  163.  
  164. /** 
  165. * Migration Successful. 
  166. * If there was a problem removing the configuration file we'll automatically prevent the migration message displaying again 
  167. */ 
  168. if ( is_file( $path . 'configuration.php' ) ) { 
  169. $this->dismiss_notice( 'migrate_v3_to_v4' ); 
  170.  
  171. return true; 
  172.  
  173. return false; 
  174.  
  175. /** 
  176. * Handles the multsite migration 
  177. * Use AJAX query to process each multisite individually 
  178. * @since 4.0 
  179. */ 
  180. public function handle_multisite_migration() { 
  181. $controller = $this->getController(); 
  182.  
  183. $args = [ 
  184. 'multisite_ids' => $this->get_multisite_ids_with_v3_config(),  
  185. 'current_page_url' => add_query_arg( null, null ),  
  186. 'gf_forms_url' => admin_url( 'admin.php?page=gf_edit_forms' ),  
  187. ]; 
  188. $controller->view->begin_multisite_migration( $args ); 
  189. $controller->view->end_multisite_migration(); 
  190.  
  191. /** 
  192. * Return an array of mulitsite blog IDs which have a v3 config 
  193. * @return array 
  194. * @since 4.0 
  195. */ 
  196. private function get_multisite_ids_with_v3_config() { 
  197. $sites = ( function_exists( 'get_sites' ) ) ? get_sites() : wp_get_sites(); 
  198. $blog_ids = []; 
  199.  
  200. foreach ( $sites as $site ) { 
  201. $site = (array) $site; /** Back-compat: ensure the new site object introduced in 4.6 gets converted back to an array */ 
  202. $site_config = $this->data->template_location . $site['blog_id'] . '/'; 
  203.  
  204. if ( is_file( $site_config . 'configuration.php' ) ) { 
  205. $blog_ids[] = $site['blog_id']; 
  206.  
  207. return $blog_ids; 
  208.  
  209. /** 
  210. * AJAX Endpoint for migrating each multisite to our v4 config 
  211. * @internal param $_POST ['nonce'] a valid nonce 
  212. * @internal param $_POST ['blog_id'] a valid site ID 
  213. * @since 4.0 
  214. */ 
  215. public function ajax_multisite_v3_migration() { 
  216.  
  217. /** @todo Dependacy inject these when we move all AJAX calls to their own class */ 
  218. $log = GPDFAPI::get_log_class(); 
  219. $misc = GPDFAPI::get_misc_class(); 
  220.  
  221. /** Ensure multisite website */ 
  222. if( ! is_multisite() ) { 
  223. /** Unauthorized response */ 
  224. wp_die( '401', 401 ); 
  225.  
  226. /** User / CORS validation */ 
  227. $misc->handle_ajax_authentication( 'Multisite v3 to v4 config', 'manage_sites', 'gfpdf_multisite_migration' ); 
  228.  
  229. /** Check there's a configuration file to migrate */ 
  230. $blog_id = ( isset( $_POST['blog_id'] ) ) ? (int) $_POST['blog_id'] : 0; 
  231.  
  232. /** Check if we have a config file that should be migrated */ 
  233. $path = $this->data->template_location . $blog_id . '/'; 
  234.  
  235. if ( ! is_file( $path . 'configuration.php' ) ) { 
  236.  
  237. $return = [ 
  238. 'error' => sprintf( esc_html__( 'No configuration.php file found for site #%s', 'gravity-forms-pdf-extended' ), $blog_id ),  
  239. ]; 
  240.  
  241. $log->addError( 'AJAX Endpoint Failed', $return ); 
  242.  
  243. echo json_encode( [ 'results' => $return ] ); 
  244. wp_die(); 
  245.  
  246. /** Setup correct migration settings */ 
  247. switch_to_blog( $blog_id ); 
  248. $this->data->multisite_template_location = $path; 
  249.  
  250. /** Do migration */ 
  251. if ( $this->migrate_v3( $path ) ) { 
  252. echo json_encode( [ 'results' => 'complete' ] ); 
  253. wp_die(); 
  254. } else { 
  255.  
  256. $return = [ 
  257. 'error' => sprintf( esc_html__( 'Database import problem for site #%s', 'gravity-forms-pdf-extended' ), $blog_id ),  
  258. ]; 
  259.  
  260. $log->addError( 'AJAX Endpoint Failed', $return ); 
  261.  
  262. echo json_encode( [ 'results' => $return ] ); 
  263. wp_die(); 
  264.  
  265. $log->addError( 'AJAX Endpoint Failed' ); 
  266.  
  267. /** Internal Server Error */ 
  268. wp_die( '500', 500 );