class-fl-builder-wpcli-command.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * WP Cli commands for Beaver Builder.
  4. */
  5. class FLbuilder_WPCLI_Command extends WP_CLI_Command {
  6. /**
  7. * Deletes preview, draft and live CSS/JS asset cache for all posts.
  8. *
  9. * ## OPTIONS
  10. *
  11. * [--network]
  12. * Clears the page builder cache for all sites on a network.
  13. *
  14. * [--all]
  15. * Clears plugin and bb-theme cache.
  16. *
  17. * ## EXAMPLES
  18. *
  19. * 1. wp beaver clearcache
  20. * - Clears the page builder cache for all the posts on the site.
  21. * 2. wp beaver clearcache --network
  22. * - Clears the page builder cache for all the posts on a network.
  23. */
  24. public function clearcache( $args, $assoc_args ) {
  25. $network = false;
  26. $all = false;
  27. if ( isset( $assoc_args['network'] ) && true == $assoc_args['network'] && is_multisite() ) {
  28. $network = true;
  29. }
  30. if ( isset( $assoc_args['all'] ) ) {
  31. // make sure theme functions are loaded.
  32. if ( class_exists( 'FLCustomizer' ) ) {
  33. $all = true;
  34. } else {
  35. WP_CLI::error( __( '--all switch used but bb-theme is not active. If using multisite bb-theme must be active on the root site.', 'fl-builder' ) );
  36. }
  37. }
  38. if ( class_exists( 'FLBuilderModel' ) ) {
  39. if ( true == $network ) {
  40. if ( function_exists( 'get_sites' ) ) {
  41. $blogs = get_sites();
  42. } else {
  43. $blogs = wp_get_sites();
  44. }
  45. foreach ( $blogs as $keys => $blog ) {
  46. // Cast $blog as an array instead of WP_Site object
  47. if ( is_object( $blog ) ) {
  48. $blog = (array) $blog;
  49. }
  50. $blog_id = $blog['blog_id'];
  51. switch_to_blog( $blog_id );
  52. FLBuilderModel::delete_asset_cache_for_all_posts();
  53. WP_CLI::success( sprintf( _x( 'Cleared the page builder cache for blog %s', 'current blog name', 'fl-builder' ), get_option( 'home' ) ) );
  54. if ( $all ) {
  55. FLCustomizer::refresh_css();
  56. WP_CLI::success( sprintf( _x( 'Rebuilt the theme cache for blog %s', 'current blog name', 'fl-builder' ), get_option( 'home' ) ) );
  57. }
  58. restore_current_blog();
  59. }
  60. } else {
  61. FLBuilderModel::delete_asset_cache_for_all_posts();
  62. WP_CLI::success( __( 'Cleared the page builder cache', 'fl-builder' ) );
  63. if ( $all ) {
  64. FLCustomizer::refresh_css();
  65. WP_CLI::success( __( 'Rebuilt the theme cache', 'fl-builder' ) );
  66. }
  67. }
  68. do_action( 'fl_builder_cache_cleared' );
  69. }
  70. }
  71. /**
  72. * Activate domain using Beaver Builder license key.
  73. *
  74. * ## OPTIONS
  75. *
  76. * [--deactivate]
  77. * Deactivate this domain and remove license.
  78. *
  79. * [--license]
  80. * License key to use.
  81. *
  82. * ## EXAMPLES
  83. *
  84. * 1. wp beaver register --license=01234567890
  85. * - Register this domain using license 01234567890
  86. * 2. wp beaver register --deactivate
  87. * - Removes domain from domain manager and clears saved license info.
  88. * 3. wp beaver register
  89. * - If license is defined in wp-config.php using FL_LICENSE_KEY global.
  90. */
  91. public function register( $args, $assoc_args ) {
  92. $license = '';
  93. if ( isset( $assoc_args['deactivate'] ) ) {
  94. FLUpdater::save_subscription_license( '' );
  95. WP_CLI::success( 'deactivated' );
  96. return false;
  97. }
  98. if ( defined( 'FL_LICENSE_KEY' ) ) {
  99. $license = FL_LICENSE_KEY;
  100. WP_CLI::log( 'Found license using FL_LICENSE_KEY global.' );
  101. }
  102. if ( isset( $assoc_args['license'] ) && '' != $assoc_args['license'] ) {
  103. $license = $assoc_args['license'];
  104. }
  105. if ( ! $license ) {
  106. WP_CLI::error( 'No license info found.' );
  107. }
  108. WP_CLI::log( sprintf( 'Using license [ %s ] to register %s', $license, network_home_url() ) );
  109. $response = FLUpdater::save_subscription_license( $license );
  110. if ( is_object( $response ) && isset( $response->error ) ) {
  111. WP_CLI::error( $response->error );
  112. } else {
  113. WP_CLI::success( $response->success );
  114. }
  115. }
  116. }
  117. WP_CLI::add_command( 'beaver', 'FLbuilder_WPCLI_Command' );