<?php
/**
 * Provides an example of the filters provided by the IPTC/EXIF and Custom Field mapping features
 *
 * In this example the Title and ALT Text values are cleaned up, replacing dashes and underscores with spaces.
 *
 * @package MLA Simple Mapping Hooks Example
 * @version 1.00
 */

/*
Plugin Name: MLA Simple Mapping Hooks Example
Plugin URI: http://fairtradejudaica.org/media-library-assistant-a-wordpress-plugin/
Description: Provides an example of the filters provided by the IPTC/EXIF and Custom Field mapping features.
Author: David Lingren
Version: 1.00
Author URI: http://fairtradejudaica.org/our-story/staff/

Copyright 2014 David Lingren

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You can get a copy of the GNU General Public License by writing to the
	Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/

/**
 * Class MLA Simple Mapping Hooks Example hooks all of the filters provided by the IPTC/EXIF and Custom Field mapping features
 *
 * Call it anything you want, but give it an unlikely and hopefully unique name. Hiding everything
 * else inside a class means this is the only name you have to worry about.
 *
 * @package MLA Simple Mapping Hooks Example
 * @since 1.00
 */
class MLASimpleMappingHooksExample {
	/**
	 * Initialization function, similar to __construct()
	 *
	 * Installs filters and actions that handle the MLA hooks for uploading and mapping.
	 *
	 * @since 1.00
	 *
	 * @return	void
	 */
	public static function initialize() {
		/*
		 * The filters are only useful in the admin section; exit if in the "front-end" posts/pages. 
		 */
		if ( ! is_admin() )
			return;

		/*
		 * add_filter parameters:
		 * $tag - name of the hook you're filtering; defined by [mla_gallery]
		 * $function_to_add - function to be called when [mla_gallery] applies the filter
		 * $priority - default 10; lower runs earlier, higher runs later
		 * $accepted_args - number of arguments your function accepts
		 *
		 * Comment out the filters you don't need; save them for future use
		 */
		//add_filter( 'mla_upload_prefilter', 'MLASimpleMappingHooksExample::mla_upload_prefilter_filter', 10, 2 );
		//add_filter( 'mla_upload_filter', 'MLASimpleMappingHooksExample::mla_upload_filter_filter', 10, 2 );
		
		//add_action( 'mla_add_attachment', 'MLASimpleMappingHooksExample::mla_add_attachment_action', 10, 1 );
		
		//add_filter( 'mla_update_attachment_metadata_options', 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_options_filter', 10, 3 );
		//add_filter( 'mla_update_attachment_metadata_prefilter', 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_prefilter_filter', 10, 3 );
		//add_filter( 'mla_update_attachment_metadata_postfilter', 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_postfilter_filter', 10, 3 );
		
		//add_filter( 'mla_mapping_settings', 'MLASimpleMappingHooksExample::mla_mapping_settings_filter', 10, 4 );
		//add_filter( 'mla_mapping_rule', 'MLASimpleMappingHooksExample::mla_mapping_rule_filter', 10, 4 );
		//add_filter( 'mla_mapping_custom_value', 'MLASimpleMappingHooksExample::mla_mapping_custom_value_filter', 10, 5 );
		//add_filter( 'mla_mapping_iptc_value', 'MLASimpleMappingHooksExample::mla_mapping_iptc_value_filter', 10, 5 );
		//add_filter( 'mla_mapping_exif_value', 'MLASimpleMappingHooksExample::mla_mapping_exif_value_filter', 10, 5 );
		add_filter( 'mla_mapping_updates', 'MLASimpleMappingHooksExample::mla_mapping_updates_filter', 10, 5 );

		//add_filter( 'mla_get_options_tablist', 'MLASimpleMappingHooksExample::mla_get_options_tablist_filter', 10, 3 );
	}

	/**
	 * Save the original image metadata when a file is first uploaded
	 *
	 * Array elements are:
	 * 		'post_id' => 0,
	 *		'mla_iptc_metadata' => array(),
	 *		'mla_exif_metadata' => array(),
	 *		'wp_image_metadata' => array(),
	 *
	 * @since 1.00
	 *
	 * @var	array
	 */
	private static $image_metadata = array();
	
	/**
	 * MLA Mapping Upload Prefilter
	 *
	 * This filter gives you an opportunity to record the original IPTC, EXIF and
	 * WordPress image_metadata before the file is stored in the Media Library.
	 * You can also modify the file name that will be used in the Media Library.
	 *
	 * Many plugins and image editing functions alter or destroy this information,
	 * so this may be your last change to preserve it.
	 *
	 * @since 1.00
	 *
	 * @param	array	the file name, type and location
	 * @param	array	the IPTC, EXIF and WordPress image_metadata
	 *
	 * @return	array	updated file name and other information
	 */
	public static function mla_upload_prefilter_filter( $file, $image_metadata ) {
		/*
		 * Uncomment the error_log statements in any of the filters to see what's passed in
		 */
		//error_log( 'MLASimpleMappingHooksExample::mla_upload_prefilter_filter $file = ' . var_export( $file, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_upload_prefilter_filter $image_metadata = ' . var_export( $image_metadata, true ), 0 );
		
		/*
		 * Save the information for use in the later filters
		 */
		self::$image_metadata = $image_metadata;
		self::$image_metadata['preload_file'] = $file;
		
		return $file;
	} // mla_upload_prefilter_filter
	
	/**
	 * MLA Mapping Upload Filter
	 *
	 * This filter gives you an opportunity to record some additional metadata
	 * for audio and video media after the file is stored in the Media Library.
	 *
	 * Many plugins and other functions alter or destroy this information,
	 * so this may be your last change to preserve it.
	 *
	 * @since 1.00
	 *
	 * @param	array	the file name, type and location
	 * @param	array	the ID3 metadata for audio and video files
	 *
	 * @return	array	updated file name, type and location
	 */
	public static function mla_upload_filter_filter( $file, $id3_data ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_upload_filter_filter $file = ' . var_export( $file, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_upload_filter_filter $id3_data = ' . var_export( $id3_data, true ), 0 );
		
		/*
		 * Save the information for use in the later filters
		 */
		self::$image_metadata['postload_file'] = $file;
		self::$image_metadata['id3_metadata'] = $id3_data;

		return $file;
	} // mla_upload_filter_filter
	
	/**
	 * MLA Add Attachment Action
	 *
	 * This filter is called at the end of the wp_insert_attachment() function,
	 * after the file is in place and the post object has been created in the database.
	 *
	 * By this time, other plugins have probably run their own 'add_attachment' filters
	 * and done their work/damage to metadata, etc.
	 *
	 * @since 1.00
	 *
	 * @param	integer	The Post ID of the new attachment
	 *
	 * @return	void
	 */
	public static function mla_add_attachment_action( $post_id ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_add_attachment_action $post_id = ' . var_export( $post_id, true ), 0 );
		
		/*
		 * Save the information for use in the later filters
		 */
		self::$image_metadata['post_id'] = $post_id;
	} // mla_add_attachment_action
	
	/**
	 * MLA Update Attachment Metadata Options
	 *
	 * This filter lets you inspect or change the processing options that will
	 * control the MLA mapping rules in the update_attachment_metadata filter.
	 *
	 * The options are:
	 *		is_upload - true if this is part of the original file upload process
	 *		enable_iptc_exif_mapping - true to apply IPTC/EXIF mapping to file uploads
	 *		enable_custom_field_mapping - true to apply custom field mapping to file uploads
	 *		enable_iptc_exif_update - true to apply IPTC/EXIF mapping to updates
	 *		enable_custom_field_update - true to apply custom field mapping to updates
	 *
	 * @since 1.00
	 *
	 * @param	array	Processing options, e.g., 'is_upload'
	 * @param	array	attachment metadata
	 * @param	integer	The Post ID of the new/updated attachment
	 *
	 * @return	array	updated processing options
	 */
	public static function mla_update_attachment_metadata_options_filter( $options, $data, $post_id ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_options_filter $options = ' . var_export( $options, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_options_filter $data = ' . var_export( $data, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_options_filter $post_id = ' . var_export( $post_id, true ), 0 );
		
		return $options;
	} // mla_update_attachment_metadata_prefilter_filter
	
	/**
	 * MLA Update Attachment Metadata Prefilter
	 *
	 * This filter is called at the end of the wp_update_attachment_metadata() function,
	 * BEFORE any MLA mapping rules are applied. The prefilter gives you an
	 * opportunity to record or update the metadata before the mapping.
	 *
	 * The wp_update_attachment_metadata() function is called at the end of the file upload process and at
	 * several later points, such as when an image attachment is edited or by
	 * plugins that alter the attachment file.
	 *
	 * @since 1.00
	 *
	 * @param	array	attachment metadata
	 * @param	integer	The Post ID of the new/updated attachment
	 * @param	array	Processing options, e.g., 'is_upload'
	 *
	 * @return	array	updated attachment metadata
	 */
	public static function mla_update_attachment_metadata_prefilter_filter( $data, $post_id, $options ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_prefilter_filter $data = ' . var_export( $data, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_prefilter_filter $post_id = ' . var_export( $post_id, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_prefilter_filter $options = ' . var_export( $options, true ), 0 );
		
		return $data;
	} // mla_update_attachment_metadata_prefilter_filter
	
	/**
	 * MLA Update Attachment Metadata Postfilter
	 *
	 * This filter is called AFTER MLA mapping rules are applied during
	 * wp_update_attachment_metadata() processing. The postfilter gives you
	 * an opportunity to record or update the metadata after the mapping.
	 *
	 * @since 1.00
	 *
	 * @param	array	attachment metadata
	 * @param	integer	The Post ID of the new/updated attachment
	 * @param	array	Processing options, e.g., 'is_upload'
	 *
	 * @return	array	updated attachment metadata
	 */
	public static function mla_update_attachment_metadata_postfilter_filter( $data, $post_id, $options ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_postfilter_filter $data = ' . var_export( $data, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_postfilter_filter $post_id = ' . var_export( $post_id, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_update_attachment_metadata_postfilter_filter $options = ' . var_export( $options, true ), 0 );

		return $data;
	} // mla_update_attachment_metadata_postfilter_filter
	
	/**
	 * MLA Mapping Settings Filter
	 *
	 * This filter is called before any mapping rules are executed.
	 * You can add, change or delete rules from the array.
	 *
	 * @since 1.00
	 *
	 * @param	array 	mapping rules
	 * @param	integer post ID to be evaluated
	 * @param	string 	category/scope to evaluate against, e.g., custom_field_mapping or single_attachment_mapping
	 * @param	array 	attachment_metadata, default NULL
	 *
	 * @return	array	updated mapping rules
	 */
	public static function mla_mapping_settings_filter( $settings, $post_id, $category, $attachment_metadata ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_settings_filter $settings = ' . var_export( $settings, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_settings_filter $post_id = ' . var_export( $post_id, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_settings_filter $category = ' . var_export( $category, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_settings_filter $attachment_metadata = ' . var_export( $attachment_metadata, true ), 0 );
		
		/*
		 * $category gives the context in which the rule is applied:
		 *		'custom_field_mapping' - mapping custom fields for ALL attachments
		 *		'single_attachment_mapping' - mapping custom fields for ONE attachments
		 *
		 *		'iptc_exif_mapping' - mapping ALL IPTC/EXIF rules
		 *		'iptc_exif_standard_mapping' - mapping standard field rules
		 *		'iptc_exif_taxonomy_mapping' - mapping taxonomy term rules
		 *		'iptc_exif_custom_mapping' - mapping IPTC/EXIF custom field rules
		 *
		 * NOTE: 'iptc_exif_mapping' will never be passed to the 'mla_mapping_rule' filter.
		 * There, one of the three more specific values will be passed.
		 */

		/*
		 * For Custom Field Mapping, $settings is an array indexed by
		 * the custom field name.
		 * Each array element is a mapping rule; an array containing:
		 *		'name' => custom field name
		 *		'data_source' => 'none', 'meta', 'template' or data source name
		 *		'keep_existing' => boolean; true to preserve existing content
		 *		'format' => 'native', 'commas', 'raw'
		 *		'mla_column' => boolean; not used
		 *		'quick_edit' => boolean; not used
		 *		'bulk_edit' => boolean; not used
		 *		'meta_name' => attachment metadata element name or content template
		 *		'option' => 'text', 'single', 'export', 'array', 'multi'
		 *		'no_null' => boolean; true to delete empty custom field values
		 *
		 * For IPTC/EXIF Mapping, $settings is an array indexed by
		 * the mapping category; 'standard', 'taxonomy' and 'custom'.
		 * Each category is an array of rules, with slightly different formats.
		 *
		 * Each 'standard' category array element is a rule (array) containing:
		 *		'name' => field slug; 'post_title', 'post_name', 'image_alt', 'post_excerpt', 'post_content'
		 *		'iptc_value' => IPTC Identifier or friendly name
		 *		'exif_value' => EXIF element name
		 *		'iptc_first' => boolean; true to prefer IPTC value over EXIF value
		 *		'keep_existing' => boolean; true to preserve existing content
		 *
		 * Each 'taxonomy' category array element is a rule (array) containing:
		 *		'name' => taxonomy slug, e.g., 'post_tag', 'attachment_category'
		 *		'hierarchical' => boolean; true for hierarchical taxonomies
		 *		'iptc_value' => IPTC Identifier or friendly name
		 *		'exif_value' => EXIF element name
		 *		'iptc_first' => boolean; true to prefer IPTC value over EXIF value
		 *		'keep_existing' => boolean; true to preserve existing content
		 *		'parent' => zero for none or the term_id of the parent 
		 *		'delimiters' => term separator(s), e.g., ',;'
		 *
		 * Each 'custom' category array element is a rule (array) containing:
		 *		'name' => custom field name
		 *		'iptc_value' => IPTC Identifier or friendly name
		 *		'exif_value' => EXIF element name
		 *		'iptc_first' => boolean; true to prefer IPTC value over EXIF value
		 *		'keep_existing' => boolean; true to preserve existing content
		 */
		return $settings;
	} // mla_mapping_settings_filter
	
	/**
	 * MLA Mapping Rule Filter
	 *
	 * This filter is called once for each mapping rule, before the rule
	 * is evaluated. You can change the rule parameters, or prevent rule
	 * evaluation by returning $setting_value['data_source'] = 'none'; 
	 *
	 * @since 1.00
	 *
	 * @param	array 	custom_field_mapping rule
	 * @param	integer post ID to be evaluated
	 * @param	string 	category/scope to evaluate against: custom_field_mapping or single_attachment_mapping
	 * @param	array 	attachment_metadata, default NULL
	 *
	 * @return	array	updated custom_field_mapping rule
	 */
	public static function mla_mapping_rule_filter( $setting_value, $post_id, $category, $attachment_metadata ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_rule_filter $setting_value = ' . var_export( $setting_value, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_rule_filter $post_id = ' . var_export( $post_id, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_rule_filter $category = ' . var_export( $category, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_rule_filter $attachment_metadata = ' . var_export( $attachment_metadata, true ), 0 );

		/*
		 * $setting_value is an array containing a mapping rule; see above
		 * To stop this rule's evaluation and mapping, return NULL
		 */
		return $setting_value;
	} // mla_mapping_rule_filter
	
	/**
	 * MLA Mapping Custom Field Value Filter
	 *
	 * This filter is called once for each custom field mapping rule, after the rule
	 * is evaluated. You can change the new value produced by the rule.
	 *
	 * @since 1.00
	 *
	 * @param	mixed 	value returned by the rule
	 * @param	array 	custom_field_mapping rule
	 * @param	integer post ID to be evaluated
	 * @param	string 	category/scope to evaluate against: custom_field_mapping or single_attachment_mapping
	 * @param	array 	attachment_metadata, default NULL
	 *
	 * @return	array	updated rule value
	 */
	public static function mla_mapping_custom_value_filter( $new_text, $setting_value, $post_id, $category, $attachment_metadata ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_custom_value_filter $new_text = ' . var_export( $new_text, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_custom_value_filter $setting_value = ' . var_export( $setting_value, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_custom_value_filter $post_id = ' . var_export( $post_id, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_custom_value_filter $category = ' . var_export( $category, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_custom_value_filter $attachment_metadata = ' . var_export( $attachment_metadata, true ), 0 );

		/*
		 * You can use MLAOptions::mla_get_data_source() to get anything available;
		 * for example:
		 */
		$my_setting = array(
			'data_source' => 'size_names',
			'option' => 'array'
		);
		//$size_names = MLAOptions::mla_get_data_source($post_id, $category, $my_setting, $attachment_metadata);
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_custom_value_filter $size_names = ' . var_export( $size_names, true ), 0 );

		/*
		 * For "empty" values, return ' '.
		 */
		return $new_text;
	} // mla_mapping_custom_value_filter

	/**
	 * MLA Mapping IPTC Value Filter
	 *
	 * This filter is called once for each IPTC/EXIF mapping rule, after the IPTC 
	 * portion of the rule is evaluated. You can change the new value produced by
	 * the rule.
	 *
	 * @since 1.00
	 *
	 * @param	mixed 	IPTC value returned by the rule
	 * @param	array 	custom_field_mapping rule
	 * @param	integer post ID to be evaluated
	 * @param	string 	category/scope to evaluate against: custom_field_mapping or single_attachment_mapping
	 * @param	array 	attachment_metadata, default NULL
	 *
	 * @return	array	updated rule IPTC value
	 */
	public static function mla_mapping_iptc_value_filter( $iptc_value, $setting_value, $post_id, $category, $attachment_metadata ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_iptc_value_filter $iptc_value = ' . var_export( $iptc_value, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_iptc_value_filter $setting_value = ' . var_export( $setting_value, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_iptc_value_filter $post_id = ' . var_export( $post_id, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_iptc_value_filter $category = ' . var_export( $category, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_iptc_value_filter $attachment_metadata = ' . var_export( $attachment_metadata, true ), 0 );

		/*
		 * You can use MLAOptions::mla_get_data_source() to get anything available;
		 * for example:
		 */
		$my_setting = array(
			'data_source' => 'template',
			'meta_name' => '([+iptc:keywords+])',
			'option' => 'array'
		);
		//$keywords = MLAOptions::mla_get_data_source($post_id, $category, $my_setting, $attachment_metadata);
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_iptc_value_filter $keywords = ' . var_export( $keywords, true ), 0 );

		/*
		 * For "empty" values, return ''.
		 */
		return $iptc_value;
	} // mla_mapping_iptc_value_filter

	/**
	 * MLA Mapping EXIF Value Filter
	 *
	 * This filter is called once for each IPTC/EXIF mapping rule, after the EXIF 
	 * portion of the rule is evaluated. You can change the new value produced by
	 * the rule.
	 *
	 * @since 1.00
	 *
	 * @param	mixed 	EXIF/Template value returned by the rule
	 * @param	array 	custom_field_mapping rule
	 * @param	integer post ID to be evaluated
	 * @param	string 	category/scope to evaluate against: custom_field_mapping or single_attachment_mapping
	 * @param	array 	attachment_metadata, default NULL
	 *
	 * @return	array	updated rule EXIF/Template value
	 */
	public static function mla_mapping_exif_value_filter( $exif_value, $setting_value, $post_id, $category, $attachment_metadata ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_exif_value_filter $exif_value = ' . var_export( $exif_value, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_exif_value_filter $setting_value = ' . var_export( $setting_value, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_exif_value_filter $post_id = ' . var_export( $post_id, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_exif_value_filter $category = ' . var_export( $category, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_exif_value_filter $attachment_metadata = ' . var_export( $attachment_metadata, true ), 0 );

		/*
		 * You can use MLAOptions::mla_get_data_source() to get anything available;
		 * for example:
		 */
		$my_setting = array(
			'data_source' => 'template',
			'meta_name' => '([+exif:Copyright+])',
			'option' => 'array'
		);
		//$copyright = MLAOptions::mla_get_data_source($post_id, $category, $my_setting, $attachment_metadata);
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_exif_value_filter $copyright = ' . var_export( $copyright, true ), 0 );

		/*
		 * For "empty" 'text' values, return ''.
		 * For "empty" 'array' values, return NULL.
		 */
		return $exif_value;
	} // mla_mapping_exif_value_filter
	
	/**
	 * MLA Mapping Updates Filter
	 *
	 * This filter is called AFTER all mapping rules are applied.
	 * You can add, change or remove updates for the attachment's
	 * standard fields, taxonomies and/or custom fields.
	 *
	 * @since 1.00
	 *
	 * @param	array	updates for the attachment's standard fields, taxonomies and/or custom fields
	 * @param	integer post ID to be evaluated
	 * @param	string 	category/scope to evaluate against: custom_field_mapping or single_attachment_mapping
	 * @param	array 	mapping rules
	 * @param	array 	attachment_metadata, default NULL
	 *
	 * @return	array	updated attachment's updates
	 */
	public static function mla_mapping_updates_filter( $updates, $post_id, $category, $settings, $attachment_metadata ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_updates_filter $updates = ' . var_export( $updates, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_updates_filter $post_id = ' . var_export( $post_id, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_updates_filter $category = ' . var_export( $category, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_updates_filter $settings = ' . var_export( $settings, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_mapping_updates_filter $attachment_metadata = ' . var_export( $attachment_metadata, true ), 0 );

		/*
		 * If $updates[ 'post_title' ] is set, some mapping rule
		 * has been set up, so we respect the result. If not,
		 * use whatever the current Title value is.
		 */
		if ( isset( $updates[ 'post_title' ] ) ) {
			$old_value = $updates[ 'post_title' ];
		} else {
			$post = get_post( $post_id );
			$old_value = $post->post_title;
		}

		/*
		 * Clean up the Title value. If the cleanup has changed the value,
		 * put the new value in the $updates array.
		 */
		$new_title = str_replace( array( '-', '_', '.' ), ' ', $old_value );
		if ( $old_value != $new_title ) {
			$updates[ 'post_title' ] = $new_title;
		}
		
		// Find the current ALT Text value
		if ( isset( $updates[ 'image_alt' ] ) ) {
			$old_value = $updates[ 'image_alt' ];
		} else {
			$old_value = get_metadata( 'post', $post_id, '_wp_attachment_image_alt', true );
		}

		// Replace the ALT Text value with the clean Title
		if ( $old_value != $new_title ) {
			$updates[ 'image_alt' ] = $new_title;
		}
		
		/*
		 * To stop this rule's updates, return an empty array, i.e., return array();
		 */
		return $updates;
	} // mla_mapping_updates_filter

	/**
	 * MLA Mapping Updates Filter
	 *
	 * This filter is called AFTER all mapping rules are applied.
	 * You can add, change or remove updates for the attachment's
	 * standard fields, taxonomies and/or custom fields.
	 *
	 * @since 1.02
	 *
	 * @param	array|false	The entire tablist ( $tab = NULL ), a single tab entry or false if not found/not allowed.
	 * @param	array		The entire tablist
	 * @param	string|NULL	tab slug for single-element return or NULL to return entire tablist
	 *
	 * @return	array	updated attachment's updates
	 */
	public static function mla_get_options_tablist_filter( $results, $mla_tablist, $tab ) {
		//error_log( 'MLASimpleMappingHooksExample::mla_get_options_tablist_filter $results = ' . var_export( $results, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_get_options_tablist_filter $mla_tablist = ' . var_export( $mla_tablist, true ), 0 );
		//error_log( 'MLASimpleMappingHooksExample::mla_get_options_tablist_filter $tab = ' . var_export( $tab, true ), 0 );

		/*
		 * Return an updated $mla_tablist ( $tab = NULL ), an updated single element or false
		 */
		return $results;
	} // mla_get_options_tablist_filter
} //MLASimpleMappingHooksExample

/*
 * Install the filters at an early opportunity
 */
add_action('init', 'MLASimpleMappingHooksExample::initialize');
?>