class-fl-builder-multisite.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Multisite helper for the page builder.
  4. *
  5. * @since 1.0
  6. */
  7. final class FLBuilderMultisite {
  8. /**
  9. * Initializes builder multisite support.
  10. *
  11. * @since 1.0
  12. * @return void
  13. */
  14. static public function init() {
  15. add_action( 'wpmu_new_blog', __CLASS__ . '::install_for_new_blog', 10, 6 );
  16. add_filter( 'wpmu_drop_tables', __CLASS__ . '::uninstall_on_delete_blog' );
  17. add_filter( 'fl_builder_activate', __CLASS__ . '::activate' );
  18. add_filter( 'fl_builder_uninstall', __CLASS__ . '::uninstall' );
  19. }
  20. /**
  21. * Short circuit activation in favor of multisite activation.
  22. *
  23. * @since 1.8
  24. * @return void
  25. */
  26. static public function activate( $activate ) {
  27. if ( is_network_admin() ) {
  28. FLBuilderMultisite::install();
  29. } else {
  30. FLBuilderAdmin::install();
  31. }
  32. FLBuilderAdmin::trigger_activate_notice();
  33. return false;
  34. }
  35. /**
  36. * Runs the install method for each site on the network.
  37. *
  38. * @since 1.0
  39. * @return void
  40. */
  41. static public function install() {
  42. global $blog_id;
  43. global $wpdb;
  44. $original_blog_id = $blog_id;
  45. $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
  46. foreach ( $blog_ids as $id ) {
  47. switch_to_blog( $id );
  48. FLBuilderAdmin::install();
  49. }
  50. switch_to_blog( $original_blog_id );
  51. }
  52. /**
  53. * Runs the install for a newly created site.
  54. *
  55. * @since 1.0
  56. * @param int $blog_id
  57. * @param int $user_id
  58. * @param string $domain
  59. * @param string $path
  60. * @param int $site_id
  61. * @param array $meta
  62. * @return void
  63. */
  64. static public function install_for_new_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
  65. global $wpdb;
  66. if ( is_plugin_active_for_network( FLBuilderModel::plugin_basename() ) ) {
  67. switch_to_blog( $blog_id );
  68. FLBuilderAdmin::install();
  69. restore_current_blog();
  70. }
  71. }
  72. /**
  73. * Short circuit the default uninstall and run
  74. * the uninstall for each site on the network.
  75. *
  76. * @since 1.0
  77. * @return void
  78. */
  79. static public function uninstall( $uninstall ) {
  80. global $blog_id;
  81. global $wpdb;
  82. $original_blog_id = $blog_id;
  83. $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
  84. foreach ( $blog_ids as $id ) {
  85. switch_to_blog( $id );
  86. FLBuilderAdmin::uninstall();
  87. }
  88. switch_to_blog( $original_blog_id );
  89. return false;
  90. }
  91. /**
  92. * Runs the uninstall method when a site is deleted.
  93. *
  94. * @since 1.0
  95. * @return array
  96. */
  97. static public function uninstall_on_delete_blog( $tables ) {
  98. return $tables;
  99. }
  100. /**
  101. * Checks if a blog on the network exists.
  102. *
  103. * @since 1.5.7
  104. * @param $blog_id The blog ID to check.
  105. * @return bool
  106. */
  107. static public function blog_exists( $blog_id ) {
  108. global $wpdb;
  109. if ( method_exists( $wpdb, 'esc_like' ) ) {
  110. $like = esc_sql( $wpdb->esc_like( $blog_id ) );
  111. } else {
  112. $like = like_escape( esc_sql( $blog_id ) );
  113. }
  114. return $wpdb->get_row( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE blog_id = '%s'", $like ) );
  115. }
  116. }
  117. FLBuilderMultisite::init();