Guest User

Untitled

a guest
Feb 20th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 6.84 KB | None | 0 0
  1. From 6f01f79829de3577c4f74899bb2eedd2470f915f Mon Sep 17 00:00:00 2001
  2. From: Hari Karam Singh <harikaram@regallygraceful.com>
  3. Date: Wed, 26 Oct 2011 18:21:27 +0100
  4. Subject: [PATCH] Add auto-tagging from IPTC keywords on upload including
  5.  setting.
  6.  
  7. ---
  8. mediatags_admin.php    |   67 +++++++++++++++++++++++++++++++++++++++++++++--
  9.  mediatags_settings.php |   28 ++++++++++++++++++++
  10.  2 files changed, 92 insertions(+), 3 deletions(-)
  11.  
  12. diff --git a/mediatags_admin.php b/mediatags_admin.php
  13. index b077f1f..0d97eb6 100755
  14. --- a/mediatags_admin.php
  15. +++ b/mediatags_admin.php
  16. @@ -27,6 +27,9 @@ function mediatags_admin_init()
  17.     // Handle Export/Import interaction
  18.     add_action('export_wp',                         'mediatags_wp_export_metadata');
  19.     add_action('import_post_meta',                  'mediatags_wp_import_metadata', 10, 3);
  20. +    
  21. +    // Auto-tagging
  22. +    add_filter( 'added_post_meta',                  'mediatags_autotag_from_iptc_keywords', 10, 4);
  23.  
  24.     $mediatag_admin_bulk_library                    = get_option('mediatag_admin_bulk_library', 'yes');
  25.     $mediatag_admin_bulk_inline                     = get_option('mediatag_admin_bulk_inline', 'yes');
  26. @@ -113,7 +116,7 @@ function mediatags_admin_init()
  27.         //wp_enqueue_style( 'mediatags-stylesheet', $mediatags->plugindir_url .'/css/mediatags_style_admin.css',
  28.         //  false, $mediatags->plugin_version);
  29.         //wp_enqueue_script('mediatags', $mediatags->plugindir_url .'/js/mediatags.js',
  30. -       //  array('jquery'), $mediatags->plugin_version);          
  31. +       //      array('jquery'), $mediatags->plugin_version);
  32.  // }
  33.  
  34.     if (function_exists('mediatags_settings_api_init'))
  35. @@ -474,8 +477,8 @@ function mediatags_show_fields_to_edit($form_fields, $post)
  36.         else
  37.             $post_media_tags_fields = "<br />". __('Enter media tags in the space above.
  38.             Enter multiple tags separated with comma.', MEDIA_TAGS_I18N_DOMAIN);
  39. -  
  40. -       $form_fields['media-meta'] = array(
  41. +
  42. +        $form_fields['media-meta'] = array(
  43.                 'label' => __('Media-Tags:', MEDIA_TAGS_I18N_DOMAIN),
  44.                 'input' => 'html',
  45.                 'html' => "<input type='text' name='attachments[$post->ID][media_tags_input]'
  46. @@ -1048,3 +1051,61 @@ function mediatags_edit_tags_fixes($parent_file)
  47.         return $parent_file;
  48.     }
  49.  }
  50. +
  51. +
  52. +// Auto tag (and save) images from the IPTC keywords
  53. +function mediatags_autotag_from_iptc_keywords( $meta_id, $attachment_id, $meta_key, $meta_value )
  54. +{
  55. +    // Check that it's an upload and that our setting is on
  56. +    // Do the action when _wp_attachment_metadata is first added in order to catch each attachment
  57. +    if ( !$_POST['Upload'] || $meta_key != '_wp_attachment_metadata' ) return;
  58. +    if ( 'yes' !== get_option( 'mediatags_autotag_from_iptc_keywords', 'yes' ) ) return;
  59. +    
  60. +    // GRAB THE TAGS FROM THE IPTC KEYWORDS
  61. +    // First get the supported image types
  62. +    $image_file_types = apply_filters(
  63. +        'wp_read_image_metadata_types',
  64. +        array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM )
  65. +    );
  66. +        
  67. +    // Get the file and info
  68. +    $media_file = get_attached_file( $attachment_id );
  69. +    list( , , $source_image_type ) = getimagesize( $media_file, $info );
  70. +
  71. +    // Check that it's an image type and IPTC is available
  72. +    if (  !in_array( $source_image_type, $image_file_types )
  73. +          || !function_exists('iptcparse')  ) {
  74. +        return;  
  75. +    }
  76. +
  77. +    $iptc = iptcparse( $info['APP13'] );
  78. +    $keywords = $iptc['2#025'];
  79. +
  80. +
  81. +    // SAVE THE KEYWORDS
  82. +    // Create the slugs and insert terms not in the db
  83. +    $media_tags_array = array();
  84. +    if ($keywords) {
  85. +        foreach($keywords as $tag) {
  86. +            $tag_slug = sanitize_title_with_dashes($tag);
  87. +            
  88. +            if ( ! ($id = term_exists( $tag_slug, MEDIA_TAGS_TAXONOMY ) ) )
  89. +                wp_insert_term($tag_val, MEDIA_TAGS_TAXONOMY, array('slug' => $tag_slug));
  90. +            
  91. +            $media_tags_array[] = $tag_slug;
  92. +        }
  93. +    }
  94. +
  95. +    // Now relate them to the attachment post
  96. +    $media_tags_slugs = array();
  97. +    if ($media_tags_array) {
  98. +        $media_tags_slugs = array();
  99. +        foreach($media_tags_array as $media_tags_item) {
  100. +            $media_tags_slugs[$media_tags_item] = sprintf("%s", $media_tags_item); // ??
  101. +        }
  102. +        
  103. +        wp_set_object_terms( $attachment_id, $media_tags_slugs, MEDIA_TAGS_TAXONOMY );          
  104. +    } else {
  105. +        wp_set_object_terms( $attachment_id, "", MEDIA_TAGS_TAXONOMY ); // ?              
  106. +    }
  107. +}
  108. \ No newline at end of file
  109. diff --git a/mediatags_settings.php b/mediatags_settings.php
  110. index a1eb8cc..3ff64e2 100755
  111. --- a/mediatags_settings.php
  112. +++ b/mediatags_settings.php
  113. @@ -122,6 +122,17 @@ function mediatags_settings_panel()
  114.             update_option( 'mediatag_rss_feed', $mediatag_rss_feed );
  115.             $update_message = _x("Media-Tags Settings have been updated.", 'update message', MEDIA_TAGS_I18N_DOMAIN);
  116.         }
  117. +
  118. +       if (isset($_REQUEST['mediatag_autotag_on_upload']))
  119. +       {
  120. +           if (strtolower($_REQUEST['mediatag_autotag_on_upload']) == strtolower("yes"))
  121. +               $mediatag_autotag_on_upload = "yes";
  122. +           else
  123. +               $mediatag_autotag_on_upload = "no";
  124. +
  125. +           update_option( 'mediatag_autotag_on_upload', $mediatag_autotag_on_upload );
  126. +           $update_message = _x("Media-Tags Settings have been updated.", 'update message', MEDIA_TAGS_I18N_DOMAIN);
  127. +       }
  128.     }
  129.     $title = _x('Media-Tags Settings', 'settings panel title', MEDIA_TAGS_I18N_DOMAIN);
  130.     ?>
  131. @@ -207,6 +218,23 @@ function mediatags_settings_panel()
  132.                         <?php mediatag_settings_boxfooter(false); ?>
  133.  
  134.  
  135. +                        <?php
  136. +                        mediatag_settings_boxheader('mediatag-options-auto-tagging',
  137. +                                __('Auto-Tag Settings for Uploaded Media', MEDIA_TAGS_I18N_DOMAIN));
  138. +
  139. +                        $mediatag_autotag_on_upload = get_option('mediatag_autotag_on_upload', 'yes');?>
  140. +                        
  141. +                        <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>
  142. +                        
  143. +                        <select id="mediatag_autotag_on_upload" name="mediatag_autotag_on_upload">
  144. +                            <option selected="selected" value="yes"><?php
  145. +                                echo _x('On', 'select option', MEDIA_TAGS_I18N_DOMAIN); ?></option>
  146. +                            <option <?php if ($mediatag_autotag_on_upload == "no"){ echo ' selected="selected" ';} ?> value="no"><?php
  147. +                                echo _x('Off', 'select option', MEDIA_TAGS_I18N_DOMAIN); ?></option>
  148. +                        </select>
  149. +                        <label for="mediatag_autotag_on_upload"><?php _e('Turn Auto-Tagging of Uploaded Files On/Off',
  150. +                            MEDIA_TAGS_I18N_DOMAIN); ?></label>
  151. +                        <?php mediatag_settings_boxfooter(false); ?>
  152.  
  153.  
  154.  
  155. --
  156. 1.7.5.1
Add Comment
Please, Sign In to add comment