| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Runners; |
| 4 |
|
| 5 |
|
| 6 |
class Attachments extends WPContent { |
| 7 |
|
| 8 |
|
| 9 |
public function __construct( $request_params ) { |
| 10 |
parent::__construct( $request_params ); |
| 11 |
$this->attachments_init(); |
| 12 |
} |
| 13 |
|
| 14 |
public function get_name(): string { |
| 15 |
return 'attachments'; |
| 16 |
} |
| 17 |
|
| 18 |
public function get_label(): string { |
| 19 |
return __( 'Attachments', 'templately' ); |
| 20 |
} |
| 21 |
|
| 22 |
public function should_log(): bool { |
| 23 |
return true; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_action(): string { |
| 27 |
return 'updateLog'; |
| 28 |
} |
| 29 |
|
| 30 |
public function log_message(): string { |
| 31 |
if($this->total > 0){ |
| 32 |
return __( 'Importing Attachments: (' . $this->get_processed() . '/' . $this->total . ')', 'templately' ); |
| 33 |
} |
| 34 |
return __( 'Importing Attachment.', 'templately' ); |
| 35 |
} |
| 36 |
|
| 37 |
public function should_run( $data, $imported_data = [] ): bool { |
| 38 |
return !empty($this->manifest['has_attachments']); |
| 39 |
} |
| 40 |
|
| 41 |
public function import( $data, $imported_data ): array { |
| 42 |
$path = $this->dir_path; |
| 43 |
$taxonomies = []; |
| 44 |
$terms = []; |
| 45 |
$results = []; |
| 46 |
|
| 47 |
$this->import_actions(); |
| 48 |
|
| 49 |
$import = $this->_import_type_data( 'attachments', $path, $imported_data, $taxonomies, $terms ); |
| 50 |
$results['attachments'] = $import['summary']['posts']; |
| 51 |
$results['attachments_errors'] = $import['errors']; |
| 52 |
|
| 53 |
$this->import_actions( true ); |
| 54 |
|
| 55 |
return $results; |
| 56 |
} |
| 57 |
|
| 58 |
protected function import_actions( $remove = false ) { |
| 59 |
parent::import_actions( $remove ); |
| 60 |
|
| 61 |
if ( $remove ) { |
| 62 |
remove_filter( 'templately_import_copy_attachment', [ $this, 'copy_attachment_file' ], 10, 4 ); |
| 63 |
} else { |
| 64 |
add_filter( 'templately_import_copy_attachment', [ $this, 'copy_attachment_file' ], 10, 4 ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public function copy_attachment_file( $return, $att_id, $dest_file, $upload_dir ) { |
| 69 |
// @todo check mapping url to new id |
| 70 |
|
| 71 |
if(!empty($att_id)){ |
| 72 |
$file_name = basename($dest_file); |
| 73 |
$path = $this->dir_path . 'attachments' . DIRECTORY_SEPARATOR; |
| 74 |
$wp_filetype = wp_check_filetype( $file_name); |
| 75 |
$ext = $wp_filetype['ext']; |
| 76 |
|
| 77 |
$source_path = "{$path}{$att_id}.{$ext}"; |
| 78 |
|
| 79 |
if(!file_exists($source_path)){ |
| 80 |
return $return; |
| 81 |
} |
| 82 |
|
| 83 |
$move_new_file = copy( $source_path, $dest_file ); |
| 84 |
|
| 85 |
if(!$move_new_file){ |
| 86 |
return $return; |
| 87 |
} |
| 88 |
|
| 89 |
$wp_filetype = wp_check_filetype_and_ext( $source_path, $file_name ); |
| 90 |
|
| 91 |
// Set correct file permissions. |
| 92 |
$stat = stat( dirname( $dest_file ) ); |
| 93 |
$perms = $stat['mode'] & 0000666; |
| 94 |
chmod( $dest_file, $perms ); |
| 95 |
|
| 96 |
return [ |
| 97 |
'file' => $dest_file, |
| 98 |
'url' => $upload_dir['url'] . "/$file_name", |
| 99 |
'type' => $wp_filetype['type'], |
| 100 |
'error' => false, |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
return $return; |
| 106 |
} |
| 107 |
|
| 108 |
public function get_processed() { |
| 109 |
$processed = $this->processed ?? []; |
| 110 |
$succeed = $processed['succeed'] ?? []; |
| 111 |
$failed = $processed['failed'] ?? []; |
| 112 |
|
| 113 |
return count($succeed) + count($failed); |
| 114 |
} |
| 115 |
|
| 116 |
public function post_log( $post, $result ) { |
| 117 |
if ( isset( $post['post_type'] ) ) { |
| 118 |
if ( $post['post_type'] !== 'attachment' ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
// need to assign to processed so log_message() can use get_processed() to calculate progress |
| 123 |
// also bellow code used it to calculate progress |
| 124 |
$this->processed = $result; |
| 125 |
|
| 126 |
$type = $post['post_type']; |
| 127 |
$title = $post['post_title']; |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
if ( empty( $type ) || empty( $title ) ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$progress = $this->total > 0 ? ceil( ( 100 * ( $this->get_processed() ) ) / $this->total ) : 100; |
| 136 |
|
| 137 |
$this->log( $progress); |
| 138 |
} |
| 139 |
|
| 140 |
public function attachments_init() { |
| 141 |
add_filter( 'pre_http_request', [ $this, 'pre_http_request' ], 10, 3 ); |
| 142 |
add_filter( 'http_response', [ $this, 'http_response' ], 10, 3 ); |
| 143 |
// attachment insert hooks |
| 144 |
add_filter( 'wp_insert_attachment_data', [ $this, 'before_insert_attachment' ], 10, 2 ); |
| 145 |
add_action( 'add_attachment', [ $this, 'after_insert_attachment' ], 10, 1 ); |
| 146 |
add_filter( 'wp_update_attachment_metadata', [ $this, 'wp_update_attachment_metadata' ], 99999, 2 ); |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
public function pre_http_request( $preempt, $parsed_args, $url ) { |
| 151 |
// error_log(print_r([$preempt, $parsed_args, $url], true)); |
| 152 |
|
| 153 |
$this->sse_log( 'attachments', 'Before downloading attachment: ' . $url, 1, 'eventLog' ); |
| 154 |
|
| 155 |
return $preempt; |
| 156 |
} |
| 157 |
|
| 158 |
public function http_response( $response, $parsed_args, $url ) { |
| 159 |
// error_log(print_r([$response, $parsed_args, $url], true)); |
| 160 |
$this->sse_log( 'attachments', 'After downloading attachment: ' . $url, 1, 'eventLog' ); |
| 161 |
return $response; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Log before inserting attachment. |
| 166 |
*/ |
| 167 |
public function before_insert_attachment( $data, $postarr ) { |
| 168 |
$this->sse_log( 'attachments', 'Before inserting attachment: ' . ( $data['post_title'] ?? '' ), 1, 'eventLog' ); |
| 169 |
return $data; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Log after attachment is inserted. |
| 174 |
*/ |
| 175 |
public function after_insert_attachment( $post_ID ) { |
| 176 |
$post = get_post( $post_ID ); |
| 177 |
$this->sse_log( 'attachments', 'After inserting attachment: ' . ( $post->post_title ?? '' ), 1, 'eventLog' ); |
| 178 |
} |
| 179 |
|
| 180 |
public function wp_update_attachment_metadata( $metadata, $attachment_id ) { |
| 181 |
$this->sse_log( 'attachments', 'Updated attachment metadata: ' . $attachment_id, 1, 'eventLog' ); |
| 182 |
// $this->sse_message([ |
| 183 |
// "action" => "eventLog", |
| 184 |
// "type" => "attachments", |
| 185 |
// "progress" => 1, |
| 186 |
// "message" => "Updated attachment metadata => " . $attachment_id, |
| 187 |
// "backtrace" => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 30), |
| 188 |
// ]); |
| 189 |
return $metadata; |
| 190 |
} |
| 191 |
} |