balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

library.php (4435B)


      1 <?php
      2 namespace Elementor\Modules\WpCli;
      3 
      4 use Elementor\Api;
      5 use Elementor\Plugin;
      6 use Elementor\TemplateLibrary\Source_Local;
      7 
      8 if ( ! defined( 'ABSPATH' ) ) {
      9 	exit; // Exit if accessed directly
     10 }
     11 
     12 /**
     13  * Elementor Page Builder cli tools.
     14  */
     15 class Library extends \WP_CLI_Command {
     16 
     17 	/**
     18 	 * Sync Elementor Library.
     19 	 *
     20 	 * [--network]
     21 	 *      Sync library in all the sites in the network.
     22 	 *
     23 	 * [--force]
     24 	 *      Force sync even if it's looks like that the library is already up to date.
     25 	 *
     26 	 * ## EXAMPLES
     27 	 *
     28 	 *  1. wp elementor library sync
     29 	 *      - This will sync the library with Elementor cloud library.
     30 	 *
     31 	 *  2. wp elementor library sync --force
     32 	 *      - This will sync the library with Elementor cloud even if it's looks like that the library is already up to date.
     33 	 *
     34 	 *  3. wp elementor library sync --network
     35 	 *      - This will sync the library with Elementor cloud library for each site in the network if needed.
     36 	 *
     37 	 * @since 2.8.0
     38 	 * @access public
     39 	 */
     40 	public function sync( $args, $assoc_args ) {
     41 		$network = isset( $assoc_args['network'] ) && is_multisite();
     42 
     43 		if ( $network ) {
     44 			/** @var \WP_Site[] $sites */
     45 			$sites = get_sites();
     46 
     47 			foreach ( $sites as $keys => $blog ) {
     48 				// Cast $blog as an array instead of  object
     49 				$blog_id = $blog->blog_id;
     50 
     51 				switch_to_blog( $blog_id );
     52 
     53 				\WP_CLI::line( 'Site #' . $blog_id . ' - ' . get_option( 'blogname' ) );
     54 
     55 				$this->do_sync( isset( $assoc_args['force'] ) );
     56 
     57 				\WP_CLI::success( 'Done! - ' . get_option( 'home' ) );
     58 
     59 				restore_current_blog();
     60 			}
     61 		} else {
     62 			$this->do_sync( isset( $assoc_args['force'] ) );
     63 			\WP_CLI::success( 'Done!' );
     64 		}
     65 	}
     66 
     67 	/**
     68 	 * Import template files to the Library.
     69 	 *
     70 	 * ## EXAMPLES
     71 	 *
     72 	 *  1. wp elementor library import <file-path>
     73 	 *      - This will import a file or a zip of multiple files to the library.
     74 	 *
     75 	 * @param $args
     76 	 * @param $assoc_args
     77 	 *
     78 	 * @since  2.8.0
     79 	 * @access public
     80 	 */
     81 	public function import( $args ) {
     82 		if ( empty( $args[0] ) ) {
     83 			\WP_CLI::error( 'Please set file path.' );
     84 		}
     85 
     86 		$file = $args[0];
     87 
     88 		/** @var Source_Local $source */
     89 		$source = Plugin::$instance->templates_manager->get_source( 'local' );
     90 
     91 		$imported_items = $source->import_template( basename( $file ), $file );
     92 
     93 		if ( is_wp_error( $imported_items ) ) {
     94 			\WP_CLI::error( $imported_items->get_error_message() );
     95 		}
     96 
     97 		\WP_CLI::success( count( $imported_items ) . ' item(s) has been imported.' );
     98 	}
     99 
    100 	/**
    101 	 * Connect site to Elementor Library.
    102 	 * (Network is not supported)
    103 	 *
    104 	 * --user
    105 	 *      The user to connect <id|login|email>
    106 	 *
    107 	 * --token
    108 	 *      A connect token from Elementor Account Dashboard.
    109 	 *
    110 	 * ## EXAMPLES
    111 	 *
    112 	 *  1. wp elementor library connect --user=admin --token=<connect-cli-token>
    113 	 *      - This will connect the admin to Elementor library.
    114 	 *
    115 	 * @param $args
    116 	 * @param $assoc_args
    117 	 *
    118 	 * @since  2.8.0
    119 	 * @access public
    120 	 */
    121 	public function connect( $args, $assoc_args ) {
    122 		if ( ! get_current_user_id() ) {
    123 			\WP_CLI::error( 'Please set user to connect (--user=<id|login|email>).' );
    124 		}
    125 
    126 		if ( empty( $assoc_args['token'] ) ) {
    127 			\WP_CLI::error( 'Please set connect token.' );
    128 		}
    129 
    130 		$_REQUEST['mode'] = 'cli';
    131 		$_REQUEST['token'] = $assoc_args['token'];
    132 
    133 		$app = $this->get_library_app();
    134 
    135 		$app->action_authorize();
    136 
    137 		$app->action_get_token();
    138 	}
    139 
    140 	/**
    141 	 * Disconnect site from Elementor Library.
    142 	 *
    143 	 * --user
    144 	 *      The user to disconnect <id|login|email>
    145 	 *
    146 	 * ## EXAMPLES
    147 	 *
    148 	 *  1. wp elementor library disconnect --user=admin
    149 	 *      - This will disconnect the admin from Elementor library.
    150 	 *
    151 	 * @param $args
    152 	 * @param $assoc_args
    153 	 *
    154 	 * @since  2.8.0
    155 	 * @access public
    156 	 */
    157 	public function disconnect() {
    158 		if ( ! get_current_user_id() ) {
    159 			\WP_CLI::error( 'Please set user to connect (--user=<id|login|email>).' );
    160 		}
    161 
    162 		$_REQUEST['mode'] = 'cli';
    163 
    164 		$this->get_library_app()->action_disconnect();
    165 	}
    166 
    167 	private function do_sync() {
    168 		$data = Api::get_library_data( true );
    169 
    170 		if ( empty( $data ) ) {
    171 			\WP_CLI::error( 'Cannot sync library.' );
    172 		}
    173 	}
    174 
    175 	/**
    176 	 * @return \Elementor\Core\Common\Modules\Connect\Apps\Library
    177 	 */
    178 	private function get_library_app() {
    179 		$connect = Plugin::$instance->common->get_component( 'connect' );
    180 		$app = $connect->get_app( 'library' );
    181 		// Before init.
    182 		if ( ! $app ) {
    183 			$connect->init();
    184 			$app = $connect->get_app( 'library' );
    185 		}
    186 
    187 		return $app;
    188 	}
    189 }