class-fl-builder-admin.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * Main builder admin class.
  4. *
  5. * @since 1.0
  6. */
  7. final class FLBuilderAdmin {
  8. /**
  9. * Initialize hooks.
  10. *
  11. * @since 1.8
  12. * @return void
  13. */
  14. static public function init() {
  15. $basename = plugin_basename( FL_BUILDER_FILE );
  16. // Activation
  17. register_activation_hook( FL_BUILDER_FILE, __CLASS__ . '::activate' );
  18. // Actions
  19. add_action( 'admin_init', __CLASS__ . '::show_activate_notice' );
  20. add_action( 'admin_init', __CLASS__ . '::sanity_checks' );
  21. // Filters
  22. add_filter( 'plugin_action_links_' . $basename, __CLASS__ . '::render_plugin_action_links' );
  23. }
  24. /**
  25. * Called on plugin activation and checks to see if the correct
  26. * WordPress version is installed and multisite is supported. If
  27. * all checks are passed the install method is called.
  28. *
  29. * @since 1.0
  30. * @return void
  31. */
  32. static public function activate() {
  33. global $wp_version;
  34. // Check for WordPress 3.5 and above.
  35. if ( ! version_compare( $wp_version, '3.5', '>=' ) ) {
  36. self::show_activate_error( __( 'The <strong>Page Builder</strong> plugin requires WordPress version 3.5 or greater. Please update WordPress before activating the plugin.', 'fl-builder' ) );
  37. }
  38. // Allow extensions to hook activation.
  39. $activate = apply_filters( 'fl_builder_activate', true );
  40. // Should we continue with activation?
  41. if ( $activate ) {
  42. // Check for multisite.
  43. if ( is_multisite() ) {
  44. $url = FLBuilderModel::get_upgrade_url( array(
  45. 'utm_medium' => 'bb-pro',
  46. 'utm_source' => 'plugins-admin-page',
  47. 'utm_campaign' => 'no-multisite-support',
  48. ) );
  49. self::show_activate_error( sprintf( __( 'This version of the <strong>Page Builder</strong> plugin is not compatible with WordPress Multisite. <a%s>Please upgrade</a> to the Multisite version of this plugin.', 'fl-builder' ), ' href="' . $url . '" target="_blank"' ) );
  50. }
  51. // Success! Run the install.
  52. self::install();
  53. // Trigger the activation notice.
  54. self::trigger_activate_notice();
  55. // Allow add-ons to hook into activation.
  56. do_action( 'fl_builder_activated' );
  57. // Flush the rewrite rules.
  58. flush_rewrite_rules();
  59. }
  60. }
  61. /**
  62. * Show a message if there is an activation error and
  63. * deactivates the plugin.
  64. *
  65. * @since 1.0
  66. * @param string $message The message to show.
  67. * @return void
  68. */
  69. static public function show_activate_error( $message ) {
  70. deactivate_plugins( FLBuilderModel::plugin_basename(), false, is_network_admin() );
  71. die( $message );
  72. }
  73. /**
  74. * @since 2.1.3
  75. */
  76. static public function sanity_checks() {
  77. if ( true !== FL_BUILDER_LITE ) {
  78. // fetch the plugin install folder this should be bb-plugin
  79. $folder = rtrim( FLBuilderModel::plugin_basename(), '/fl-builder.php' );
  80. if ( 'bb-plugin' != $folder ) {
  81. $error = sprintf( __( 'Install Error! We detected that Beaver Builder appears to be installed in a folder called <kbd>%s</kbd>.<br />For automatic updates to work the plugin must be installed in the folder <kbd>bb-plugin</kbd>.', 'fl-builder' ), $folder );
  82. FLBuilderAdminSettings::add_error( $error );
  83. }
  84. }
  85. }
  86. /**
  87. * Sets the transient that triggers the activation notice
  88. * or welcome page redirect.
  89. *
  90. * @since 1.8
  91. * @return void
  92. */
  93. static public function trigger_activate_notice() {
  94. if ( current_user_can( 'delete_users' ) ) {
  95. set_transient( '_fl_builder_activation_admin_notice', true, 30 );
  96. }
  97. }
  98. /**
  99. * Shows the activation success message or redirects to the
  100. * welcome page.
  101. *
  102. * @since 1.0
  103. * @return void
  104. */
  105. static public function show_activate_notice() {
  106. // Bail if no activation transient is set.
  107. if ( ! get_transient( '_fl_builder_activation_admin_notice' ) ) {
  108. return;
  109. }
  110. // Delete the activation transient.
  111. delete_transient( '_fl_builder_activation_admin_notice' );
  112. if ( isset( $_GET['activate-multi'] ) || is_multisite() ) {
  113. // Show the notice if we are activating multiple plugins or on multisite.
  114. add_action( 'admin_notices', __CLASS__ . '::activate_notice' );
  115. add_action( 'network_admin_notices', __CLASS__ . '::activate_notice' );
  116. } else {
  117. // Redirect to the welcome page.
  118. wp_safe_redirect( add_query_arg( array(
  119. 'page' => 'fl-builder-settings',
  120. ), admin_url( 'options-general.php' ) ) );
  121. }
  122. }
  123. /**
  124. * Shows the activation success message.
  125. *
  126. * @since 1.0
  127. * @return void
  128. */
  129. static public function activate_notice() {
  130. if ( FL_BUILDER_LITE !== true ) {
  131. $hash = '#license';
  132. $message = __( 'Page Builder activated! <a%s>Click here</a> to enable remote updates.', 'fl-builder' );
  133. } else {
  134. $hash = '#welcome';
  135. $message = __( 'Page Builder activated! <a%s>Click here</a> to get started.', 'fl-builder' );
  136. }
  137. $url = apply_filters( 'fl_builder_activate_redirect_url', admin_url( '/options-general.php?page=fl-builder-settings' . $hash ) );
  138. echo '<div class="updated" style="background: #d3ebc1;">';
  139. echo '<p><strong>' . sprintf( $message, ' href="' . esc_url( $url ) . '"' ) . '</strong></p>';
  140. echo '</div>';
  141. }
  142. /**
  143. * Installs the builder upon successful activation.
  144. * Currently not used but may be in the future.
  145. *
  146. * @since 1.0
  147. * @return void
  148. */
  149. static public function install() {}
  150. /**
  151. * Uninstalls the builder.
  152. *
  153. * @since 1.0
  154. * @return void
  155. */
  156. static public function uninstall() {
  157. FLBuilderModel::uninstall_database();
  158. }
  159. /**
  160. * Renders the link for the row actions on the plugins page.
  161. *
  162. * @since 1.0
  163. * @param array $actions An array of row action links.
  164. * @return array
  165. */
  166. static public function render_plugin_action_links( $actions ) {
  167. if ( FL_BUILDER_LITE === true ) {
  168. $url = FLBuilderModel::get_upgrade_url( array(
  169. 'utm_medium' => 'bb-lite',
  170. 'utm_source' => 'plugins-admin-page',
  171. 'utm_campaign' => 'plugins-admin-upgrade',
  172. ) );
  173. $actions[] = '<a href="' . $url . '" style="color:#3db634;" target="_blank">' . _x( 'Upgrade', 'Plugin action link label.', 'fl-builder' ) . '</a>';
  174. }
  175. if ( ! FLBuilderModel::is_white_labeled() ) {
  176. $url = FLBuilderModel::get_store_url( 'change-logs', array(
  177. 'utm_medium' => 'bb-pro',
  178. 'utm_source' => 'plugins-admin-page',
  179. 'utm_campaign' => 'plugins-admin-changelog',
  180. ) );
  181. $actions[] = '<a href="' . $url . '" target="_blank">' . _x( 'Change Log', 'Plugin action link label.', 'fl-builder' ) . '</a>';
  182. }
  183. return $actions;
  184. }
  185. /**
  186. * @since 1.0
  187. * @deprecated 1.8
  188. */
  189. static public function init_classes() {
  190. _deprecated_function( __METHOD__, '1.8' );
  191. }
  192. /**
  193. * @since 1.0
  194. * @deprecated 1.8
  195. */
  196. static public function init_settings() {
  197. _deprecated_function( __METHOD__, '1.8' );
  198. }
  199. /**
  200. * @since 1.0
  201. * @deprecated 1.8
  202. */
  203. static public function init_multisite() {
  204. _deprecated_function( __METHOD__, '1.8' );
  205. }
  206. /**
  207. * @since 1.0
  208. * @deprecated 1.8
  209. */
  210. static public function init_templates() {
  211. _deprecated_function( __METHOD__, '1.8' );
  212. }
  213. /**
  214. * @since 1.0
  215. * @deprecated 1.8
  216. */
  217. static public function white_label_plugins_page( $plugins ) {
  218. _deprecated_function( __METHOD__, '1.8', 'FLBuilderWhiteLabel::plugins_page()' );
  219. if ( class_exists( 'FLBuilderWhiteLabel' ) ) {
  220. return FLBuilderWhiteLabel::plugins_page( $plugins );
  221. }
  222. return $plugins;
  223. }
  224. /**
  225. * @since 1.6.4.3
  226. * @deprecated 1.8
  227. */
  228. static public function white_label_themes_page( $themes ) {
  229. _deprecated_function( __METHOD__, '1.8', 'FLBuilderWhiteLabel::themes_page()' );
  230. if ( class_exists( 'FLBuilderWhiteLabel' ) ) {
  231. return FLBuilderWhiteLabel::themes_page( $themes );
  232. }
  233. return $themes;
  234. }
  235. /**
  236. * @since 1.6.4.4
  237. * @deprecated 1.8
  238. */
  239. static public function white_label_theme_gettext( $text ) {
  240. if ( class_exists( 'FLBuilderWhiteLabel' ) ) {
  241. return FLBuilderWhiteLabel::theme_gettext( $text );
  242. }
  243. return $text;
  244. }
  245. }
  246. FLBuilderAdmin::init();