Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 6f01f79829de3577c4f74899bb2eedd2470f915f Mon Sep 17 00:00:00 2001
- From: Hari Karam Singh <harikaram@regallygraceful.com>
- Date: Wed, 26 Oct 2011 18:21:27 +0100
- Subject: [PATCH] Add auto-tagging from IPTC keywords on upload including
- setting.
- ---
- mediatags_admin.php | 67 +++++++++++++++++++++++++++++++++++++++++++++--
- mediatags_settings.php | 28 ++++++++++++++++++++
- 2 files changed, 92 insertions(+), 3 deletions(-)
- diff --git a/mediatags_admin.php b/mediatags_admin.php
- index b077f1f..0d97eb6 100755
- --- a/mediatags_admin.php
- +++ b/mediatags_admin.php
- @@ -27,6 +27,9 @@ function mediatags_admin_init()
- // Handle Export/Import interaction
- add_action('export_wp', 'mediatags_wp_export_metadata');
- add_action('import_post_meta', 'mediatags_wp_import_metadata', 10, 3);
- +
- + // Auto-tagging
- + add_filter( 'added_post_meta', 'mediatags_autotag_from_iptc_keywords', 10, 4);
- $mediatag_admin_bulk_library = get_option('mediatag_admin_bulk_library', 'yes');
- $mediatag_admin_bulk_inline = get_option('mediatag_admin_bulk_inline', 'yes');
- @@ -113,7 +116,7 @@ function mediatags_admin_init()
- //wp_enqueue_style( 'mediatags-stylesheet', $mediatags->plugindir_url .'/css/mediatags_style_admin.css',
- // false, $mediatags->plugin_version);
- //wp_enqueue_script('mediatags', $mediatags->plugindir_url .'/js/mediatags.js',
- - // array('jquery'), $mediatags->plugin_version);
- + // array('jquery'), $mediatags->plugin_version);
- // }
- if (function_exists('mediatags_settings_api_init'))
- @@ -474,8 +477,8 @@ function mediatags_show_fields_to_edit($form_fields, $post)
- else
- $post_media_tags_fields = "<br />". __('Enter media tags in the space above.
- Enter multiple tags separated with comma.', MEDIA_TAGS_I18N_DOMAIN);
- -
- - $form_fields['media-meta'] = array(
- +
- + $form_fields['media-meta'] = array(
- 'label' => __('Media-Tags:', MEDIA_TAGS_I18N_DOMAIN),
- 'input' => 'html',
- 'html' => "<input type='text' name='attachments[$post->ID][media_tags_input]'
- @@ -1048,3 +1051,61 @@ function mediatags_edit_tags_fixes($parent_file)
- return $parent_file;
- }
- }
- +
- +
- +// Auto tag (and save) images from the IPTC keywords
- +function mediatags_autotag_from_iptc_keywords( $meta_id, $attachment_id, $meta_key, $meta_value )
- +{
- + // Check that it's an upload and that our setting is on
- + // Do the action when _wp_attachment_metadata is first added in order to catch each attachment
- + if ( !$_POST['Upload'] || $meta_key != '_wp_attachment_metadata' ) return;
- + if ( 'yes' !== get_option( 'mediatags_autotag_from_iptc_keywords', 'yes' ) ) return;
- +
- + // GRAB THE TAGS FROM THE IPTC KEYWORDS
- + // First get the supported image types
- + $image_file_types = apply_filters(
- + 'wp_read_image_metadata_types',
- + array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM )
- + );
- +
- + // Get the file and info
- + $media_file = get_attached_file( $attachment_id );
- + list( , , $source_image_type ) = getimagesize( $media_file, $info );
- +
- + // Check that it's an image type and IPTC is available
- + if ( !in_array( $source_image_type, $image_file_types )
- + || !function_exists('iptcparse') ) {
- + return;
- + }
- +
- + $iptc = iptcparse( $info['APP13'] );
- + $keywords = $iptc['2#025'];
- +
- +
- + // SAVE THE KEYWORDS
- + // Create the slugs and insert terms not in the db
- + $media_tags_array = array();
- + if ($keywords) {
- + foreach($keywords as $tag) {
- + $tag_slug = sanitize_title_with_dashes($tag);
- +
- + if ( ! ($id = term_exists( $tag_slug, MEDIA_TAGS_TAXONOMY ) ) )
- + wp_insert_term($tag_val, MEDIA_TAGS_TAXONOMY, array('slug' => $tag_slug));
- +
- + $media_tags_array[] = $tag_slug;
- + }
- + }
- +
- + // Now relate them to the attachment post
- + $media_tags_slugs = array();
- + if ($media_tags_array) {
- + $media_tags_slugs = array();
- + foreach($media_tags_array as $media_tags_item) {
- + $media_tags_slugs[$media_tags_item] = sprintf("%s", $media_tags_item); // ??
- + }
- +
- + wp_set_object_terms( $attachment_id, $media_tags_slugs, MEDIA_TAGS_TAXONOMY );
- + } else {
- + wp_set_object_terms( $attachment_id, "", MEDIA_TAGS_TAXONOMY ); // ?
- + }
- +}
- \ No newline at end of file
- diff --git a/mediatags_settings.php b/mediatags_settings.php
- index a1eb8cc..3ff64e2 100755
- --- a/mediatags_settings.php
- +++ b/mediatags_settings.php
- @@ -122,6 +122,17 @@ function mediatags_settings_panel()
- update_option( 'mediatag_rss_feed', $mediatag_rss_feed );
- $update_message = _x("Media-Tags Settings have been updated.", 'update message', MEDIA_TAGS_I18N_DOMAIN);
- }
- +
- + if (isset($_REQUEST['mediatag_autotag_on_upload']))
- + {
- + if (strtolower($_REQUEST['mediatag_autotag_on_upload']) == strtolower("yes"))
- + $mediatag_autotag_on_upload = "yes";
- + else
- + $mediatag_autotag_on_upload = "no";
- +
- + update_option( 'mediatag_autotag_on_upload', $mediatag_autotag_on_upload );
- + $update_message = _x("Media-Tags Settings have been updated.", 'update message', MEDIA_TAGS_I18N_DOMAIN);
- + }
- }
- $title = _x('Media-Tags Settings', 'settings panel title', MEDIA_TAGS_I18N_DOMAIN);
- ?>
- @@ -207,6 +218,23 @@ function mediatags_settings_panel()
- <?php mediatag_settings_boxfooter(false); ?>
- + <?php
- + mediatag_settings_boxheader('mediatag-options-auto-tagging',
- + __('Auto-Tag Settings for Uploaded Media', MEDIA_TAGS_I18N_DOMAIN));
- +
- + $mediatag_autotag_on_upload = get_option('mediatag_autotag_on_upload', 'yes');?>
- +
- + <p><?php _e("Media-Tags can extract the IPTC keywords for media files and auto-save them to newly uploaded media.", MEDIA_TAGS_I18N_DOMAIN); ?></p>
- +
- + <select id="mediatag_autotag_on_upload" name="mediatag_autotag_on_upload">
- + <option selected="selected" value="yes"><?php
- + echo _x('On', 'select option', MEDIA_TAGS_I18N_DOMAIN); ?></option>
- + <option <?php if ($mediatag_autotag_on_upload == "no"){ echo ' selected="selected" ';} ?> value="no"><?php
- + echo _x('Off', 'select option', MEDIA_TAGS_I18N_DOMAIN); ?></option>
- + </select>
- + <label for="mediatag_autotag_on_upload"><?php _e('Turn Auto-Tagging of Uploaded Files On/Off',
- + MEDIA_TAGS_I18N_DOMAIN); ?></label>
- + <?php mediatag_settings_boxfooter(false); ?>
- --
- 1.7.5.1
Add Comment
Please, Sign In to add comment