Skip to content

Commit 6d3511e

Browse files
authored
Merge pull request #49 from threadi/feature/useFileDates
added option to use external file dates instead of the actual (solves…
2 parents 25d1699 + 3b60cda commit 6d3511e

File tree

7 files changed

+104
-29
lines changed

7 files changed

+104
-29
lines changed

app/ExternalFiles/Files.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function init(): void {
115115
add_action( 'eml_http_directory_import_file_before_to_list', array( $this, 'check_runtime' ), 10, 2 );
116116
add_action( 'eml_sftp_directory_import_file_before_to_list', array( $this, 'check_runtime' ), 10, 2 );
117117
add_filter( 'eml_help_tabs', array( $this, 'add_help' ), 20 );
118+
add_filter( 'eml_file_import_attachment', array( $this, 'add_file_date' ), 10, 3 );
118119

119120
// add admin actions.
120121
add_action( 'admin_action_eml_reset_thumbnails', array( $this, 'reset_thumbnails_by_request' ) );
@@ -420,7 +421,7 @@ public function add_url( string $url, bool $add_to_queue = false ): bool {
420421
*
421422
* @since 2.0.0 Available since 2.0.0
422423
*
423-
* @param string $post_array The attachment settings.
424+
* @param array $post_array The attachment settings.
424425
* @param string $url The requested external URL.
425426
* @param array $file_data List of file settings detected by importer.
426427
*/
@@ -1783,4 +1784,32 @@ public function check_srcset_meta( array $image_meta, array $size_array, string
17831784
// return resulting meta.
17841785
return $image_meta;
17851786
}
1787+
1788+
/**
1789+
* Add file date to post array to set the date of the external file.
1790+
*
1791+
* @param array $post_array The attachment settings.
1792+
* @param string $url The requested external URL.
1793+
* @param array $file_data List of file settings detected by importer.
1794+
*
1795+
* @return array
1796+
* @noinspection PhpUnusedParameterInspection
1797+
*/
1798+
public function add_file_date( array $post_array, string $url, array $file_data ): array {
1799+
// bail if setting is disabled.
1800+
if ( 1 !== absint( get_option( 'eml_use_file_dates' ) ) ) {
1801+
return $post_array;
1802+
}
1803+
1804+
// bail if no last-modified is given.
1805+
if ( empty( $file_data['last-modified'] ) ) {
1806+
return $post_array;
1807+
}
1808+
1809+
// add the last-modified date.
1810+
$post_array['post_date'] = gmdate( 'Y-m-d H:i:s', $file_data['last-modified'] );
1811+
1812+
// return the resulting array.
1813+
return $post_array;
1814+
}
17861815
}

app/ExternalFiles/Protocols/File.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,13 @@ public function get_url_infos(): array {
159159
private function get_url_info( string $file_path ): array {
160160
// initialize the file infos array.
161161
$results = array(
162-
'title' => basename( $file_path ),
163-
'filesize' => 0,
164-
'mime-type' => '',
165-
'tmp-file' => '',
166-
'local' => true,
167-
'url' => $file_path,
162+
'title' => basename( $file_path ),
163+
'filesize' => 0,
164+
'mime-type' => '',
165+
'tmp-file' => '',
166+
'local' => true,
167+
'url' => $file_path,
168+
'last-modified' => '',
168169
);
169170

170171
// bail if file does not exist.
@@ -200,15 +201,20 @@ private function get_url_info( string $file_path ): array {
200201
// get the size.
201202
$results['filesize'] = wp_filesize( $temp_file );
202203

204+
// get the last modified date.
205+
$results['last-modified'] = $wp_filesystem->mtime( $file_path );
206+
207+
$response_headers = array();
203208
/**
204209
* Filter the data of a single file during import.
205210
*
206211
* @since 1.1.0 Available since 1.1.0
207212
*
208213
* @param array $results List of detected file settings.
209214
* @param string $url The requested external URL.
215+
* @param array $response_headers The response header.
210216
*/
211-
return apply_filters( 'eml_external_file_infos', $results, $file_path );
217+
return apply_filters( 'eml_external_file_infos', $results, $file_path, $response_headers );
212218
}
213219

214220
/**

app/ExternalFiles/Protocols/Ftp.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,13 @@ public function get_url_infos(): array {
296296
private function get_url_info( string $file_path, WP_Filesystem_FTPext $ftp_connection ): array {
297297
// initialize the file infos array.
298298
$results = array(
299-
'title' => basename( $file_path ),
300-
'filesize' => 0,
301-
'mime-type' => '',
302-
'tmp-file' => '',
303-
'local' => true,
304-
'url' => $file_path,
299+
'title' => basename( $file_path ),
300+
'filesize' => 0,
301+
'mime-type' => '',
302+
'tmp-file' => '',
303+
'local' => true,
304+
'url' => $file_path,
305+
'last-modified' => '',
305306
);
306307

307308
// get the file contents.
@@ -332,15 +333,20 @@ private function get_url_info( string $file_path, WP_Filesystem_FTPext $ftp_conn
332333
// get the size.
333334
$results['filesize'] = wp_filesize( $temp_file );
334335

336+
// get the last modified date.
337+
$results['last-modified'] = $ftp_connection->mtime( $file_path );
338+
339+
$response_headers = array();
335340
/**
336341
* Filter the data of a single file during import.
337342
*
338343
* @since 1.1.0 Available since 1.1.0
339344
*
340345
* @param array $results List of detected file settings.
341346
* @param string $url The requested external URL.
347+
* @param array $response_headers The response header.
342348
*/
343-
return apply_filters( 'eml_external_file_infos', $results, $file_path );
349+
return apply_filters( 'eml_external_file_infos', $results, $file_path, $response_headers );
344350
}
345351

346352
/**

app/ExternalFiles/Protocols/Http.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,12 @@ public function get_url_info( string $url ): array {
449449

450450
// initialize basic array for file data.
451451
$results = array(
452-
'title' => basename( $url ),
453-
'filesize' => 0,
454-
'mime-type' => '',
455-
'local' => false,
456-
'url' => $url,
452+
'title' => basename( $url ),
453+
'filesize' => 0,
454+
'mime-type' => '',
455+
'local' => false,
456+
'url' => $url,
457+
'last-modified' => '',
457458
);
458459

459460
// set file size in result-array.
@@ -474,6 +475,14 @@ public function get_url_info( string $url ): array {
474475
$results['local'] = $this->url_should_be_saved_local( $url, $results['mime-type'] );
475476
}
476477

478+
// set last modified, if given.
479+
if ( ! empty( $response_headers['last-modified'] ) ) {
480+
$last_modified = strtotime( $response_headers['last-modified'] );
481+
if ( $last_modified ) {
482+
$results['last-modified'] = $last_modified;
483+
}
484+
}
485+
477486
// download file as temporary file for further analyses.
478487
add_filter( 'http_request_args', array( $this, 'set_download_url_header' ) );
479488
$results['tmp-file'] = download_url( $url );
@@ -496,10 +505,11 @@ public function get_url_info( string $url ): array {
496505
*
497506
* @param array $results List of detected file settings.
498507
* @param string $url The requested external URL.
508+
* @param array $response_headers The response header.
499509
*
500510
* @noinspection PhpConditionAlreadyCheckedInspection
501511
*/
502-
return apply_filters( 'eml_external_file_infos', $results, $url );
512+
return apply_filters( 'eml_external_file_infos', $results, $url, $response_headers );
503513
}
504514

505515
/**

app/ExternalFiles/Protocols/Sftp.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,13 @@ public function get_url_infos(): array {
220220
private function get_url_info( string $file_path, WP_Filesystem_SSH2 $ssh_connection ): array {
221221
// initialize the file infos array.
222222
$results = array(
223-
'title' => basename( $file_path ),
224-
'filesize' => 0,
225-
'mime-type' => '',
226-
'tmp-file' => '',
227-
'local' => true,
228-
'url' => $file_path,
223+
'title' => basename( $file_path ),
224+
'filesize' => 0,
225+
'mime-type' => '',
226+
'tmp-file' => '',
227+
'local' => true,
228+
'url' => $file_path,
229+
'last-modified' => '',
229230
);
230231

231232
// bail if file does not exist.
@@ -263,15 +264,20 @@ private function get_url_info( string $file_path, WP_Filesystem_SSH2 $ssh_connec
263264
// get the size.
264265
$results['filesize'] = wp_filesize( $temp_file );
265266

267+
// get the last modified date.
268+
$results['last-modified'] = $ssh_connection->mtime( $file_path );
269+
270+
$response_headers = array();
266271
/**
267272
* Filter the data of a single file during import.
268273
*
269274
* @since 1.1.0 Available since 1.1.0
270275
*
271276
* @param array $results List of detected file settings.
272277
* @param string $url The requested external URL.
278+
* @param array $response_headers The response header.
273279
*/
274-
return apply_filters( 'eml_external_file_infos', $results, $file_path );
280+
return apply_filters( 'eml_external_file_infos', $results, $file_path, $response_headers );
275281
}
276282

277283
/**

app/Plugin/Settings.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,20 @@ public function add_settings(): void {
306306
$setting->set_default( 0 );
307307
$setting->set_help( '<p>' . $field->get_description() . '</p>' );
308308

309+
// add setting.
310+
$setting = $settings_obj->add_setting( 'eml_use_file_dates' );
311+
$setting->set_section( $advanced_tab_advanced );
312+
$setting->set_field(
313+
array(
314+
'type' => 'Checkbox',
315+
'title' => __( 'Use external file dates', 'external-files-in-media-library' ),
316+
'description' => __( 'If this option is enabled all external files will be saved in media library with the date set by the external location. If the external location does not set any date the actual date will be used.', 'external-files-in-media-library' ),
317+
)
318+
);
319+
$setting->set_type( 'integer' );
320+
$setting->set_default( 0 );
321+
$setting->set_help( '<p>' . $field->get_description() . '</p>' );
322+
309323
// get user roles.
310324
$user_roles = array();
311325
if ( function_exists( 'wp_roles' ) && ! empty( wp_roles()->roles ) ) {

readme.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Tested up to: 6.7
66
Requires PHP: 8.0
77
License: GPL-2.0-or-later
88
License URI: https://www.gnu.org/licenses/gpl-2.0.html
9-
Stable tag: 2.0.1
9+
Stable tag: 2.0.2
1010

1111
Add external files to your media library to link or embed them in your website. They will be integrated as if they were locally available.
1212

@@ -199,3 +199,7 @@ Yes, there are many options on WP CLI, see [our documentation](https://github.co
199199
* Fixed update handler for WordPress 6.7
200200
* Fixed setting of capabilities for Playground
201201
* Fixed setting of capabilities on update
202+
203+
= 2.0.2 =
204+
* Added option to use the external file date instead of the import date
205+
* Fixed hook documentations

0 commit comments

Comments
 (0)