Skip to content

Commit

Permalink
Merge pull request #49 from threadi/feature/useFileDates
Browse files Browse the repository at this point in the history
added option to use external file dates instead of the actual (solves…
  • Loading branch information
threadi authored Nov 23, 2024
2 parents 25d1699 + 3b60cda commit 6d3511e
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 29 deletions.
31 changes: 30 additions & 1 deletion app/ExternalFiles/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function init(): void {
add_action( 'eml_http_directory_import_file_before_to_list', array( $this, 'check_runtime' ), 10, 2 );
add_action( 'eml_sftp_directory_import_file_before_to_list', array( $this, 'check_runtime' ), 10, 2 );
add_filter( 'eml_help_tabs', array( $this, 'add_help' ), 20 );
add_filter( 'eml_file_import_attachment', array( $this, 'add_file_date' ), 10, 3 );

// add admin actions.
add_action( 'admin_action_eml_reset_thumbnails', array( $this, 'reset_thumbnails_by_request' ) );
Expand Down Expand Up @@ -420,7 +421,7 @@ public function add_url( string $url, bool $add_to_queue = false ): bool {
*
* @since 2.0.0 Available since 2.0.0
*
* @param string $post_array The attachment settings.
* @param array $post_array The attachment settings.
* @param string $url The requested external URL.
* @param array $file_data List of file settings detected by importer.
*/
Expand Down Expand Up @@ -1783,4 +1784,32 @@ public function check_srcset_meta( array $image_meta, array $size_array, string
// return resulting meta.
return $image_meta;
}

/**
* Add file date to post array to set the date of the external file.
*
* @param array $post_array The attachment settings.
* @param string $url The requested external URL.
* @param array $file_data List of file settings detected by importer.
*
* @return array
* @noinspection PhpUnusedParameterInspection
*/
public function add_file_date( array $post_array, string $url, array $file_data ): array {
// bail if setting is disabled.
if ( 1 !== absint( get_option( 'eml_use_file_dates' ) ) ) {
return $post_array;
}

// bail if no last-modified is given.
if ( empty( $file_data['last-modified'] ) ) {
return $post_array;
}

// add the last-modified date.
$post_array['post_date'] = gmdate( 'Y-m-d H:i:s', $file_data['last-modified'] );

// return the resulting array.
return $post_array;
}
}
20 changes: 13 additions & 7 deletions app/ExternalFiles/Protocols/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ public function get_url_infos(): array {
private function get_url_info( string $file_path ): array {
// initialize the file infos array.
$results = array(
'title' => basename( $file_path ),
'filesize' => 0,
'mime-type' => '',
'tmp-file' => '',
'local' => true,
'url' => $file_path,
'title' => basename( $file_path ),
'filesize' => 0,
'mime-type' => '',
'tmp-file' => '',
'local' => true,
'url' => $file_path,
'last-modified' => '',
);

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

// get the last modified date.
$results['last-modified'] = $wp_filesystem->mtime( $file_path );

$response_headers = array();
/**
* Filter the data of a single file during import.
*
* @since 1.1.0 Available since 1.1.0
*
* @param array $results List of detected file settings.
* @param string $url The requested external URL.
* @param array $response_headers The response header.
*/
return apply_filters( 'eml_external_file_infos', $results, $file_path );
return apply_filters( 'eml_external_file_infos', $results, $file_path, $response_headers );
}

/**
Expand Down
20 changes: 13 additions & 7 deletions app/ExternalFiles/Protocols/Ftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,13 @@ public function get_url_infos(): array {
private function get_url_info( string $file_path, WP_Filesystem_FTPext $ftp_connection ): array {
// initialize the file infos array.
$results = array(
'title' => basename( $file_path ),
'filesize' => 0,
'mime-type' => '',
'tmp-file' => '',
'local' => true,
'url' => $file_path,
'title' => basename( $file_path ),
'filesize' => 0,
'mime-type' => '',
'tmp-file' => '',
'local' => true,
'url' => $file_path,
'last-modified' => '',
);

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

// get the last modified date.
$results['last-modified'] = $ftp_connection->mtime( $file_path );

$response_headers = array();
/**
* Filter the data of a single file during import.
*
* @since 1.1.0 Available since 1.1.0
*
* @param array $results List of detected file settings.
* @param string $url The requested external URL.
* @param array $response_headers The response header.
*/
return apply_filters( 'eml_external_file_infos', $results, $file_path );
return apply_filters( 'eml_external_file_infos', $results, $file_path, $response_headers );
}

/**
Expand Down
22 changes: 16 additions & 6 deletions app/ExternalFiles/Protocols/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,12 @@ public function get_url_info( string $url ): array {

// initialize basic array for file data.
$results = array(
'title' => basename( $url ),
'filesize' => 0,
'mime-type' => '',
'local' => false,
'url' => $url,
'title' => basename( $url ),
'filesize' => 0,
'mime-type' => '',
'local' => false,
'url' => $url,
'last-modified' => '',
);

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

// set last modified, if given.
if ( ! empty( $response_headers['last-modified'] ) ) {
$last_modified = strtotime( $response_headers['last-modified'] );
if ( $last_modified ) {
$results['last-modified'] = $last_modified;
}
}

// download file as temporary file for further analyses.
add_filter( 'http_request_args', array( $this, 'set_download_url_header' ) );
$results['tmp-file'] = download_url( $url );
Expand All @@ -496,10 +505,11 @@ public function get_url_info( string $url ): array {
*
* @param array $results List of detected file settings.
* @param string $url The requested external URL.
* @param array $response_headers The response header.
*
* @noinspection PhpConditionAlreadyCheckedInspection
*/
return apply_filters( 'eml_external_file_infos', $results, $url );
return apply_filters( 'eml_external_file_infos', $results, $url, $response_headers );
}

/**
Expand Down
20 changes: 13 additions & 7 deletions app/ExternalFiles/Protocols/Sftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,13 @@ public function get_url_infos(): array {
private function get_url_info( string $file_path, WP_Filesystem_SSH2 $ssh_connection ): array {
// initialize the file infos array.
$results = array(
'title' => basename( $file_path ),
'filesize' => 0,
'mime-type' => '',
'tmp-file' => '',
'local' => true,
'url' => $file_path,
'title' => basename( $file_path ),
'filesize' => 0,
'mime-type' => '',
'tmp-file' => '',
'local' => true,
'url' => $file_path,
'last-modified' => '',
);

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

// get the last modified date.
$results['last-modified'] = $ssh_connection->mtime( $file_path );

$response_headers = array();
/**
* Filter the data of a single file during import.
*
* @since 1.1.0 Available since 1.1.0
*
* @param array $results List of detected file settings.
* @param string $url The requested external URL.
* @param array $response_headers The response header.
*/
return apply_filters( 'eml_external_file_infos', $results, $file_path );
return apply_filters( 'eml_external_file_infos', $results, $file_path, $response_headers );
}

/**
Expand Down
14 changes: 14 additions & 0 deletions app/Plugin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,20 @@ public function add_settings(): void {
$setting->set_default( 0 );
$setting->set_help( '<p>' . $field->get_description() . '</p>' );

// add setting.
$setting = $settings_obj->add_setting( 'eml_use_file_dates' );
$setting->set_section( $advanced_tab_advanced );
$setting->set_field(
array(
'type' => 'Checkbox',
'title' => __( 'Use external file dates', 'external-files-in-media-library' ),
'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' ),
)
);
$setting->set_type( 'integer' );
$setting->set_default( 0 );
$setting->set_help( '<p>' . $field->get_description() . '</p>' );

// get user roles.
$user_roles = array();
if ( function_exists( 'wp_roles' ) && ! empty( wp_roles()->roles ) ) {
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Tested up to: 6.7
Requires PHP: 8.0
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Stable tag: 2.0.1
Stable tag: 2.0.2

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.

Expand Down Expand Up @@ -199,3 +199,7 @@ Yes, there are many options on WP CLI, see [our documentation](https://github.co
* Fixed update handler for WordPress 6.7
* Fixed setting of capabilities for Playground
* Fixed setting of capabilities on update

= 2.0.2 =
* Added option to use the external file date instead of the import date
* Fixed hook documentations

0 comments on commit 6d3511e

Please sign in to comment.