<?php
/**
 * @package Test_Crop_Filter
 * @version 0.01
 */
/*
Plugin Name: Test Cropping Filter
Plugin URI: http://buzz-media.com
Description: Plugin to exercise the proposed image resizing crop filter
Author: Matt White
Version: 0.1
Author URI: http://buzz-media.com
*/

//
// FILTER BEING USED
//

add_filter('image_crop_location','buzzmedia_crop_control',10,6); // image_crop_area is not in core wp, you need to patch media.php
function buzzmedia_crop_control ($orig_w,$orig_h,$crop_w,$crop_h,$s_x,$s_y)
{
	if (!($options=get_option('bm_crop_control'))) return array($s_x,$s_y);
	
	
	switch ($options['crop_control']) {
		case 'left_top';
			$s_x = 0;
			$s_y = 0;
		break;
		
		case 'center_top';
			$s_y = 0;
		break;
		
		case 'center_middle';
			// wordpress default - leave it be
		break;
	}
	return array($s_x,$s_y); // return the coordinates of the top left hand corner of the crop
		
}




//
// JUST SOME ADMIN TO ALLOW US TO CONFIGURE THE TEST FILTER
//


add_action('admin_menu', 'buzzmedia_crop_menu');
function buzzmedia_crop_menu() {
	add_options_page('BM Crop Menu Options', 'BM Crop Control', 'manage_options', 'buzzmedia-crop-control', 'buzzmedia_crop_options');
}


add_action('admin_init','bm_crop_control_init');
function bm_crop_control_init () {
	register_setting('bm_crop_control_options','bm_crop_control');
}


function buzzmedia_crop_options() {
	if (!current_user_can('manage_options'))  {
		wp_die( __('You do not have sufficient permissions to access this page.') );
	}
	?>
	<div class="wrap">
	<div id="icon-options-general" class="icon32"><br /></div> 
	<h2>BM Crop Control Settings</h2>
	<p>This plugin exists only as a way to test the proposed image_crop_location filter added to media.php.</p>
	<p>If you haven't applied the patch in ticket #15989, then don't expect much to happen.</p>
	<p>If you <i>have</i> applied the patch <i>and</i> you have hard cropping selected in media settings, you can test as follows</p>
	<p>
		<ol>
			<li>Choose how you would like your image to be cropped from the dropdown in this page</li>
			<li>Save :)</li>
			<li>Edit a post and upload images (probably want to try some wide, long, and square variants</li>
			<li>Put a gallery shortcode in the post</li>
			<li>Save, then view the post - marvel at the thumbnails</li>
			<li>Come back to this page, change the settings - rinse and repeat til happy</li>
		</ol>
	</p>
	
	<form method="post" action="options.php">
	<?php settings_fields('bm_crop_control_options'); ?>
    <?php $options = get_option('bm_crop_control'); ?>
    <p><b>Current Setting:<?=$options['crop_control']?></b></p>
	
	<b>Crop Location:</b>&nbsp;
	<select name="bm_crop_control[crop_control]">
		<option value="left_top">Left Top</option>
		<option value="center_top">Center Top</option>
		<option value="center_middle">Center Middle (wp default)</option>	
	</select>
	<p class="submit"><input type="submit" name="submit" id="submit" class="button-primary" value="Save Changes"  /></p>
	</div>
	<?php	
}