Club 15CC

Advanced ~ 15CC Code

Task Code
Objective-C Add Ivars & Properties to an Objective-C Category via Runtime »
/// An sort-of hack (not that bad really) for adding ivars and properies to Categories 

// .h file
@interface UIView (Marshmallows)
@property (nonatomic) CGFloat rotation;
@end

// .m file
#import <objc/runtime.h>

/// Key for objc_*AssociatedObject functions
static char _rotation;

@implementation UIView (Marshmallows)

- (CGFloat)rotation
{
    // Convert from object
    NSNumber *n = objc_getAssociatedObject(self, &_rotation);
    return n.floatValue;
}

- (void)setRotation:(CGFloat)rotation
{
    CGFloat delta = rotation - _rotation;
    self.transform = CGAffineTransformMakeRotation(delta);

    // Must convert to an object for this trick to work
    objc_setAssociatedObject(self, &_rotation, @(rotation), OBJC_ASSOCIATION_COPY);
}

@end
WordPress Add a Form Parameter to Media Uploads in WordPress »
////
// Say you want to get a form parameter to pass to the media uploader so you can test for it in the $_POST on your wp_handle_upload filter…
////

/**
 * Add checkbox to set autocreate on upload
 */
function fifteenccgal_autocreate_post_on_image_upload_form_tweaks()
{
    ?>
    <label style="display: inline-block; padding: 5px 8px; border: 3px solid lightgrey; border-radius: 1em">
        <input  type="checkbox" 
                id="fifcc_autocreate" 
                name="fifcc_autocreate" 
                value="1" 
                <?php echo ((FIFTEENCCGAL_AUTOCREATE_DEFAULT_STATE == 1) ? 'checked="checked"' : '')?>" />
         Auto-create a Photo Post for each uploaded image?
    </label><br/><br/>
    <script type="text/javascript">
        jQuery(function ($) {
            $('#fifcc_autocreate').change(function (e) {
                uploader.settings.multipart_params.fifcc_autocreate = ($('#fifcc_autocreate:checked').val() ? 1 : 0);
            })
        });
    </script>
    <?php
}
add_action('post-upload-ui', 'fifteenccgal_autocreate_post_on_image_upload_form_tweaks');

////
// You might also wish to set a default value on load. WordPress provides a suitable hook…
////

/**
 * Initialise the value to reflect the checkbox default
 */
function fifteenccgal_autocreate_post_on_image_upload_post_params_init($params)
{
    $params['fifcc_autocreate'] = FIFTEENCCGAL_AUTOCREATE_DEFAULT_STATE;
    return $params;
}
add_filter('upload_post_params', 'fifteenccgal_autocreate_post_on_image_upload_post_params_init');
WordPress Add IPTC Keywords and Category Metadata to Uploaded Images in WordPress »
function fifteenccgal_add_additional_image_meta( $meta, $file, $sourceImageType ) 
{
    $image_file_types = apply_filters( 
        'wp_read_image_metadata_types', 
        array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM )
    );

    // Check that it's an image type
    if (  in_array( $sourceImageType, $image_file_types ) 
          && function_exists('iptcparse')  ) {

        getimagesize($file, $info);
        $iptc = iptcparse($info['APP13']);
        $meta['category'] = $iptc['2#015'];
        $meta['keywords'] = $iptc['2#025'];
    }

    return $meta;
}
add_filter('wp_read_image_metadata', 'fifteenccgal_add_additional_image_meta', 10, 3);