command.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Elementor\Modules\WpCli;
  3. use Elementor\Api;
  4. use Elementor\Plugin;
  5. use Elementor\TemplateLibrary\Source_Local;
  6. use Elementor\Utils;
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit; // Exit if accessed directly
  9. }
  10. /**
  11. * Elementor Page Builder cli tools.
  12. */
  13. class Command extends \WP_CLI_Command {
  14. /**
  15. * Flush the Elementor Page Builder CSS Cache.
  16. *
  17. * [--network]
  18. * Flush CSS Cache for all the sites in the network.
  19. *
  20. * ## EXAMPLES
  21. *
  22. * 1. wp elementor flush-css
  23. * - This will flush the CSS files for elementor page builder.
  24. *
  25. * 2. wp elementor flush-css --network
  26. * - This will flush the CSS files for elementor page builder for all the sites in the network.
  27. *
  28. * @alias flush-css
  29. */
  30. public function flush_css( $args, $assoc_args ) {
  31. $network = ! empty( $assoc_args['network'] ) && is_multisite();
  32. if ( $network ) {
  33. /** @var \WP_Site[] $blogs */
  34. $blogs = get_sites();
  35. foreach ( $blogs as $keys => $blog ) {
  36. // Cast $blog as an array instead of object
  37. $blog_id = $blog->blog_id;
  38. switch_to_blog( $blog_id );
  39. Plugin::$instance->files_manager->clear_cache();
  40. \WP_CLI::success( 'Flushed the Elementor CSS Cache for site - ' . get_option( 'home' ) );
  41. restore_current_blog();
  42. }
  43. } else {
  44. Plugin::$instance->files_manager->clear_cache();
  45. \WP_CLI::success( 'Flushed the Elementor CSS Cache' );
  46. }
  47. }
  48. /**
  49. * Replace old URLs with new URLs in all Elementor pages.
  50. *
  51. * ## EXAMPLES
  52. *
  53. * 1. wp elementor search-replace <old> <new>
  54. * - This will replace all <old> URLs with the <new> URL.
  55. *
  56. * @alias replace-urls
  57. */
  58. public function replace_urls( $args, $assoc_args ) {
  59. if ( empty( $args[0] ) ) {
  60. \WP_CLI::error( 'Please set the `old` URL' );
  61. }
  62. if ( empty( $args[1] ) ) {
  63. \WP_CLI::error( 'Please set the `new` URL' );
  64. }
  65. try {
  66. $results = Utils::replace_urls( $args[0], $args[1] );
  67. \WP_CLI::success( $results );
  68. } catch ( \Exception $e ) {
  69. \WP_CLI::error( $e->getMessage() );
  70. }
  71. }
  72. /**
  73. * Sync Elementor Library.
  74. *
  75. * ## EXAMPLES
  76. *
  77. * 1. wp elementor sync-library
  78. * - This will sync the library with Elementor cloud library.
  79. *
  80. * @alias sync-library
  81. */
  82. public function sync_library( $args, $assoc_args ) {
  83. $data = Api::get_library_data( true );
  84. if ( empty( $data ) ) {
  85. \WP_CLI::error( 'Cannot sync library.' );
  86. }
  87. \WP_CLI::success( 'Library has been synced.' );
  88. }
  89. /**
  90. * Import template files to the Library.
  91. *
  92. * ## EXAMPLES
  93. *
  94. * 1. wp elementor import-library <file-path>
  95. * - This will import a file or a zip of multiple files to the library.
  96. *
  97. * @alias import-library
  98. */
  99. public function import_library( $args, $assoc_args ) {
  100. if ( empty( $args[0] ) ) {
  101. \WP_CLI::error( 'Please set file path.' );
  102. }
  103. if ( ! is_readable( $args[0] ) ) {
  104. \WP_CLI::error( 'Cannot read file.' );
  105. }
  106. /** @var Source_Local $source */
  107. $source = Plugin::$instance->templates_manager->get_source( 'local' );
  108. $imported_items = $source->import_template( basename( $args[0] ), $args[0] );
  109. if ( empty( $imported_items ) ) {
  110. \WP_CLI::error( 'Cannot import.' );
  111. }
  112. \WP_CLI::success( count( $imported_items ) . ' item(s) has been imported.' );
  113. }
  114. }