Skip to content

Commit

Permalink
#449 added new option for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
planv committed Mar 26, 2020
1 parent 9e37ab0 commit 54473a0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
9 changes: 8 additions & 1 deletion lib/classes/class-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,14 @@ public function action_stateless_process_image() {

// If replace file with wildcards
if ( false !== $fullsizepath && '1' == $use_wildcards ) {
$fullsizepath_original = wp_get_original_image_path( $image->ID );
$image_meta = wp_get_attachment_metadata( $image->ID );
$image_file = get_attached_file( $image->ID );

if ( empty( $image_meta['original_image'] ) ) {
$fullsizepath_original = $image_file;
} else {
$fullsizepath_original = path_join( dirname( $image_file ), $image_meta['original_image'] );
}
$fullsizepath_wildcard = apply_filters( 'wp_stateless_file_name', basename($fullsizepath_original), true, "", "", $use_wildcards);

//copy original image
Expand Down
5 changes: 4 additions & 1 deletion lib/cli/class-sm-cli-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ public function __construct( $args = array(), $assoc_args = array() ) {
* wp stateless sync files --fix
* : Run process and trying to fix previously failed items
*
* @synopsis [<type>] [--start=<val>] [--limit=<val>] [--end=<val>] [--batch=<val>] [--batches=<val>] [--b] [--log] [--o] [--force] [--continue] [--fix] [--order]
* wp stateless sync files --use_wildcards
* : Run process with using wildcards for file path
*
* @synopsis [<type>] [--start=<val>] [--limit=<val>] [--end=<val>] [--batch=<val>] [--batches=<val>] [--b] [--log] [--o] [--force] [--continue] [--fix] [--use_wildcards] [--order]
* @param $args
* @param $assoc_args
*/
Expand Down
60 changes: 51 additions & 9 deletions lib/cli/class-sm-cli-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,13 @@ public function stateless_process_image( $id ) {
throw new \Exception( sprintf( __( 'Failed resize: %s is an invalid image ID.', ud_get_stateless_media()->domain ), esc_html( $id ) ) );

$fullsizepath = get_attached_file( $image->ID );
$upload_dir = wp_upload_dir();

// If no file found
if ( false === $fullsizepath || ! file_exists( $fullsizepath ) ) {
$upload_dir = wp_upload_dir();

// Try get it and save
$result_code = ud_get_stateless_media()->get_client()->get_media( apply_filters( 'wp_stateless_file_name', str_replace( trailingslashit( $upload_dir[ 'basedir' ] ), '', $fullsizepath ), true, $id ), true, $fullsizepath );
$result_code = ud_get_stateless_media()->get_client()->get_media( apply_filters( 'wp_stateless_file_name', str_replace( trailingslashit( $upload_dir[ 'basedir' ] ), '', $fullsizepath ), true, $id, "", $this->use_wildcards), true, $fullsizepath );

if ( $result_code !== 200 ) {
if(!Utility::sync_get_attachment_if_exist($image->ID, $fullsizepath)){ // Save file to local from proxy.
Expand All @@ -338,6 +338,31 @@ public function stateless_process_image( $id ) {
}
}

// If replace file with wildcards
if ( false !== $fullsizepath && '1' == $this->use_wildcards ) {
$image_meta = wp_get_attachment_metadata( $image->ID );
$image_file = get_attached_file( $image->ID );

if ( empty( $image_meta['original_image'] ) ) {
$fullsizepath_original = $image_file;
} else {
$fullsizepath_original = path_join( dirname( $image_file ), $image_meta['original_image'] );
}
$fullsizepath_wildcard = apply_filters( 'wp_stateless_file_name', basename($fullsizepath_original), true, "", "", $this->use_wildcards);

//copy original image
copy( $fullsizepath_original, $upload_dir[ 'basedir' ] . '/' . $fullsizepath_wildcard);
//copy scaled image
copy( $fullsizepath, $upload_dir[ 'basedir' ] . '/' . apply_filters( 'wp_stateless_file_name', basename($fullsizepath), true, "", "", $this->use_wildcards));

//removing old file
//ud_get_stateless_media()->get_client()->remove_media($fullsizepath, $id);
//unlink( $fullsizepath );

update_attached_file( $image->ID, apply_filters( 'wp_stateless_file_name', basename($fullsizepath), true, "", "", $this->use_wildcards) );
$fullsizepath = $upload_dir[ 'basedir' ] . '/' . $fullsizepath_wildcard;
}

@set_time_limit( -1 );

//
Expand Down Expand Up @@ -387,8 +412,9 @@ public function stateless_process_file( $id ) {
$fullsizepath = get_attached_file( $file->ID );
$local_file_exists = file_exists( $fullsizepath );

$upload_dir = wp_upload_dir();

if ( false === $fullsizepath || ! $local_file_exists ) {
$upload_dir = wp_upload_dir();

// Try get it and save
$result_code = ud_get_stateless_media()->get_client()->get_media( str_replace( trailingslashit( $upload_dir[ 'basedir' ] ), '', $fullsizepath ), true, $fullsizepath );
Expand All @@ -407,6 +433,20 @@ public function stateless_process_file( $id ) {
}
}

// If replace file with wildcards
if ( false !== $fullsizepath && '1' == $this->use_wildcards ) {

$fullsizepath_wildcard = apply_filters( 'wp_stateless_file_name', basename($fullsizepath), true, "", "", $this->use_wildcards);

//copy file
copy( $fullsizepath, $upload_dir[ 'basedir' ] . '/' . $fullsizepath_wildcard);

update_attached_file( $file->ID, apply_filters( 'wp_stateless_file_name', basename($fullsizepath), true, "", "", $this->use_wildcards) );
$fullsizepath = $upload_dir[ 'basedir' ] . '/' . $fullsizepath_wildcard;

$local_file_exists = file_exists( $fullsizepath );
}

if($local_file_exists){
$upload_dir = wp_upload_dir();

Expand Down Expand Up @@ -448,12 +488,14 @@ private function _prepare() {
if( isset( $args[ 'b' ] ) ) {
WP_CLI::error( 'Invalid parameter --b. Command must not be run directly with --b parameter.' );
}
$this->start = isset( $args[ 'start' ] ) && is_numeric( $args[ 'start' ] ) ? $args[ 'start' ] : 0;
$this->limit = isset( $args[ 'limit' ] ) && is_numeric( $args[ 'limit' ] ) ? $args[ 'limit' ] : 100;
$this->force = isset( $args[ 'force' ] ) ? true : false;
$this->continue = isset( $args[ 'continue' ] ) ? true : false;
$this->fix = isset( $args[ 'fix' ] ) ? true : false;
$this->order = isset( $args[ 'order' ]) && $args[ 'order' ] === 'ASC' ? 'ASC' : 'DESC';
$this->start = isset( $args[ 'start' ] ) && is_numeric( $args[ 'start' ] ) ? $args[ 'start' ] : 0;
$this->limit = isset( $args[ 'limit' ] ) && is_numeric( $args[ 'limit' ] ) ? $args[ 'limit' ] : 100;
$this->force = isset( $args[ 'force' ] ) ? true : false;
$this->continue = isset( $args[ 'continue' ] ) ? true : false;
$this->fix = isset( $args[ 'fix' ] ) ? true : false;
$this->use_wildcards = isset( $args[ 'use_wildcards' ] ) ? true : false;
$_REQUEST['use_wildcards'] = $this->use_wildcards;
$this->order = isset( $args[ 'order' ]) && $args[ 'order' ] === 'ASC' ? 'ASC' : 'DESC';
if( isset( $args[ 'batch' ] ) ) {
if( !is_numeric( $args[ 'batch' ] ) || $args[ 'batch' ] <= 0 ) {
WP_CLI::error( 'Invalid parameter --batch' );
Expand Down
2 changes: 1 addition & 1 deletion static/views/settings_interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<p class="description"><strong ng-bind="sm.showNotice('mode')" ></strong></p>
<?php if(is_network_admin()): ?>
<p class="sm-mode">
<label for="sm_mode_disabled"><input ng-model="sm.mode" id="sm_mode_not_override" type="radio" name="sm[mode]" value="" ng-checked="sm.mode == ''" ng-disabled="sm.readonly.mode"><?php _e( 'Don\'t override', ud_get_stateless_media()->domain ); ?><small class="description"><?php _e( 'Don\'t override.', ud_get_stateless_media()->domain ); ?></small></label>
<label for="sm_mode_not_override"><input ng-model="sm.mode" id="sm_mode_not_override" type="radio" name="sm[mode]" value="" ng-checked="sm.mode == ''" ng-disabled="sm.readonly.mode"><?php _e( 'Don\'t override', ud_get_stateless_media()->domain ); ?><small class="description"><?php _e( 'Don\'t override.', ud_get_stateless_media()->domain ); ?></small></label>
</p>
<?php endif; ?>
<p class="sm-mode">
Expand Down

0 comments on commit 54473a0

Please sign in to comment.