Commit c6b1d2c8 authored by Česlav Przywara's avatar Česlav Przywara

Automatically attach photo/video media to photo page.

Fixes #26.
parent d9d627f5
......@@ -112,3 +112,33 @@ add_filter('wp_insert_attachment_data', function (array $data, array $postarr):
return $data;
}, 10, 2);
/**
* Set post parent ID to post page
*/
add_filter('wp_insert_post_parent', function (int $post_parent, int $post_id, array $post_data, array $raw_data): int {
if ($post_parent !== 0) {
// Only modify post parent, if not set explicitly.
return $post_parent;
}
if ($post_data['post_type'] !== 'attachment') {
// Only modify post parent of attachments.
return $post_parent;
}
if (empty($file = $raw_data['file'])) {
// Without filename, there's nothing we can do here...
return $post_parent;
}
if (empty($pattern = apply_filters('bezirksblaetter/media_filename_regex', ''))) {
// No regex for determination of media files is set by appropriate filter.
return $post_parent;
}
if (preg_match($pattern, basename($file)) !== 1) {
// Filename does not match expected pattern.
return $post_parent;
}
// Ok, set post parent to media parent page.
return get_option(B13R_MEDIA_PARENT_PAGE, 0);
}, 10, 4);
......@@ -52,3 +52,17 @@ add_filter('chesio/static_urls_replace_rules', function (array $replacements): a
}
return $replacements;
}, 10, 1);
/**
* Filter (set) regex for auto-determination of media files.
* @return string
*/
add_filter('bezirksblaetter/media_filename_regex', function (): string {
// Recognize following media files:
// DSC - Sony RX100M2 photo file
// MAH - Sony RX100M2 video file
// IMG - Nexus 5X photo file
// VID - Nexus 5X video file
return '/^(DSC\d{5}|MAH\d{5}|IMG_\d{8}_\d{6}|VID_\d{8}_\d{6})/i';
}, 10, 0);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment