| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\WordPress; |
| 4 |
|
| 5 |
use Averta\WordPress\Utility\JSON; |
| 6 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 7 |
use Depicter\Services\AssetsAPIService; |
| 8 |
|
| 9 |
class WPCliService |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Add import sub command to depicter cli command |
| 13 |
* |
| 14 |
* @example wp depicter import --template-ids:1,2,3 --v=1 --directory=2 --site-id=5 |
| 15 |
* @param $args |
| 16 |
* @param $assoc_args |
| 17 |
*` |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public function import( $args, $assoc_args ) { |
| 21 |
|
| 22 |
if ( empty( $assoc_args['template-ids'] ) ) { |
| 23 |
\WP_CLI::error( __( '--template-ids param is required.', 'depicter' ) ); |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
if ( is_multisite() && ! empty( $assoc_args['site-id'] ) ) { |
| 28 |
switch_to_blog( $assoc_args['site-id'] ); |
| 29 |
} |
| 30 |
|
| 31 |
\Depicter::client()->getRefreshToken(); |
| 32 |
|
| 33 |
$templateIDs = explode( ',', $assoc_args['template-ids'] ); |
| 34 |
$endpointVersion = !empty( $assoc_args['v'] ) ? (int) $assoc_args['v'] : 1; |
| 35 |
$directory = !empty( $assoc_args['directory'] ) ? (int) $assoc_args['directory'] : 2; |
| 36 |
|
| 37 |
foreach( $templateIDs as $templateID ){ |
| 38 |
try { |
| 39 |
$result = AssetsAPIService::getDocumentTemplateData( $templateID, [ 'v' => $endpointVersion, 'directory' => $directory ] ); |
| 40 |
if ( !empty( $result->errors ) ){ |
| 41 |
foreach( $result->errors as $error ) { |
| 42 |
\WP_CLI::error( $error ); |
| 43 |
} |
| 44 |
} elseif ( !empty( $result->hits ) ) { |
| 45 |
$editorData = JSON::encode( $result->hits ); |
| 46 |
$editorData = preg_replace( '/"activeBreakpoint":".+?"/', '"activeBreakpoint":"default"', $editorData ); |
| 47 |
$document = \Depicter::documentRepository()->create(); |
| 48 |
|
| 49 |
$updateData = [ 'content' => $editorData ]; |
| 50 |
if( ! empty( $result->title ) ){ |
| 51 |
$updateData['name'] = $result->title . ' ' . $document->getID(); |
| 52 |
} |
| 53 |
if( ! empty( $result->image ) ){ |
| 54 |
$imageRequest = wp_remote_get( $result->image ); |
| 55 |
$previewImage = wp_remote_retrieve_body( $imageRequest ); |
| 56 |
\Depicter::storage()->filesystem()->write( \Depicter::documentRepository() |
| 57 |
->getPreviewImagePath( $document->getID() ), $previewImage ); |
| 58 |
} |
| 59 |
|
| 60 |
\Depicter::documentRepository()->update( $document->getID(), $updateData ); |
| 61 |
\Depicter::media()->importDocumentAssets( $editorData ); |
| 62 |
|
| 63 |
if( \Depicter::options()->get( 'use_google_fonts', 'on' ) === 'save_locally' ){ |
| 64 |
$documentModel = \Depicter::document()->getModel( $document->getID() )->prepare(); |
| 65 |
\Depicter::googleFontsService()->download( $documentModel->getFontsLink() ); |
| 66 |
} |
| 67 |
|
| 68 |
if ( is_multisite() && empty( $assoc_args['site-id'] ) ) { |
| 69 |
\WP_CLI::warning( __( 'slider imported on the main site. To import template on specific site please provide the --site-id param.', 'depicter' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
/* translators: Imported slider id */ |
| 73 |
\WP_CLI::success(sprintf( __( 'Slider with ID of %s imported successfully', 'depicter' ), $templateID ) ); |
| 74 |
} |
| 75 |
} catch( GuzzleException $e ) { |
| 76 |
\WP_CLI::error( $e->getMessage() ); |
| 77 |
} catch( \Exception $e ){ |
| 78 |
\WP_CLI::error( $e->getMessage() ); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|