Нет описания

class-install.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. use WPForms\Tasks\Meta;
  3. /**
  4. * Handle plugin installation upon activation.
  5. *
  6. * @since 1.0.0
  7. */
  8. class WPForms_Install {
  9. /**
  10. * Primary class constructor.
  11. *
  12. * @since 1.0.0
  13. */
  14. public function __construct() {
  15. // When activated, trigger install method.
  16. register_activation_hook( WPFORMS_PLUGIN_FILE, array( $this, 'install' ) );
  17. register_deactivation_hook( WPFORMS_PLUGIN_FILE, array( $this, 'deactivate' ) );
  18. // Watch for new multisite blogs.
  19. add_action( 'wpmu_new_blog', array( $this, 'new_multisite_blog' ), 10, 6 );
  20. // Watch for delayed admin install.
  21. add_action( 'admin_init', array( $this, 'admin' ) );
  22. }
  23. /**
  24. * Perform certain actions on plugin activation.
  25. *
  26. * @since 1.0.0
  27. *
  28. * @param bool $network_wide Whether to enable the plugin for all sites in the network
  29. * or just the current site. Multisite only. Default is false.
  30. */
  31. public function install( $network_wide = false ) {
  32. // Check if we are on multisite and network activating.
  33. if ( is_multisite() && $network_wide ) {
  34. // Multisite - go through each subsite and run the installer.
  35. $sites = get_sites(
  36. [
  37. 'fields' => 'ids',
  38. 'number' => 0,
  39. ]
  40. );
  41. foreach ( $sites as $blog_id ) {
  42. switch_to_blog( $blog_id );
  43. $this->run();
  44. restore_current_blog();
  45. }
  46. } else {
  47. // Normal single site.
  48. $this->run();
  49. }
  50. set_transient( 'wpforms_just_activated', wpforms()->pro ? 'pro' : 'lite', 60 );
  51. // Abort so we only set the transient for single site installs.
  52. if ( isset( $_GET['activate-multi'] ) || is_network_admin() ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  53. return;
  54. }
  55. // Add transient to trigger redirect to the Welcome screen.
  56. set_transient( 'wpforms_activation_redirect', true, 30 );
  57. }
  58. /**
  59. * Run manual installation.
  60. *
  61. * @since 1.5.4.2
  62. *
  63. * @param bool $silent Silent install, disables welcome page.
  64. */
  65. public function manual( $silent = false ) {
  66. $this->install( is_plugin_active_for_network( plugin_basename( WPFORMS_PLUGIN_FILE ) ) );
  67. if ( $silent ) {
  68. delete_transient( 'wpforms_activation_redirect' );
  69. }
  70. }
  71. /**
  72. * Perform certain actions on plugin deactivation.
  73. *
  74. * @since 1.5.9
  75. */
  76. public function deactivate() {
  77. // Unschedule all ActionScheduler actions by group.
  78. wpforms()->get( 'tasks' )->cancel_all();
  79. }
  80. /**
  81. * Watch for delayed install procedure from WPForms admin.
  82. *
  83. * @since 1.5.4.2
  84. */
  85. public function admin() {
  86. if ( ! is_admin() ) {
  87. return;
  88. }
  89. $install = get_option( 'wpforms_install', false );
  90. if ( empty( $install ) ) {
  91. return;
  92. }
  93. $this->manual( true );
  94. delete_option( 'wpforms_install' );
  95. }
  96. /**
  97. * Run the actual installer.
  98. *
  99. * @since 1.5.4.2
  100. */
  101. protected function run() {
  102. $meta = new Meta();
  103. // Create the table if it doesn't exist.
  104. if ( ! $meta->table_exists() ) {
  105. $meta->create_table();
  106. }
  107. // Hook for Pro users.
  108. do_action( 'wpforms_install' );
  109. /*
  110. * Set current version, to be referenced in future updates.
  111. */
  112. // Used by Pro migrations.
  113. update_option( 'wpforms_version', WPFORMS_VERSION );
  114. // Used by Lite migrations.
  115. update_option( 'wpforms_version_lite', WPFORMS_VERSION );
  116. // Store the date when the initial activation was performed.
  117. $type = class_exists( 'WPForms_Lite', false ) ? 'lite' : 'pro';
  118. $activated = get_option( 'wpforms_activated', array() );
  119. if ( empty( $activated[ $type ] ) ) {
  120. $activated[ $type ] = time();
  121. update_option( 'wpforms_activated', $activated );
  122. }
  123. }
  124. /**
  125. * When a new site is created in multisite, see if we are network activated,
  126. * and if so run the installer.
  127. *
  128. * @since 1.3.0
  129. *
  130. * @param int $blog_id Blog ID.
  131. * @param int $user_id User ID.
  132. * @param string $domain Site domain.
  133. * @param string $path Site path.
  134. * @param int $site_id Site ID. Only relevant on multi-network installs.
  135. * @param array $meta Meta data. Used to set initial site options.
  136. */
  137. public function new_multisite_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
  138. if ( is_plugin_active_for_network( plugin_basename( WPFORMS_PLUGIN_FILE ) ) ) {
  139. switch_to_blog( $blog_id );
  140. $this->run();
  141. restore_current_blog();
  142. }
  143. }
  144. }
  145. new WPForms_Install();