$b ) { return 1; } else { return 0; } } /** * Creates the markup for the WordPress admin options page * * @return void * @author Jonathan Christopher */ function attachments_options() { ?>

Attachments Options

checked="checked" /> Users can only see their own attachments

'; echo ' var attachments_base = "' . WP_PLUGIN_URL . '/attachments"; '; echo ' var attachments_media = ""; '; echo ''; } /** * Fired when Post or Page is saved. Serializes all attachment data and saves to post_meta * * @param int $post_id The ID of the current post * @return void * @author Jonathan Christopher * @author JR Tashjian */ function attachments_save($post_id) { // verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times if ( !wp_verify_nonce( $_POST['attachments_nonce'], plugin_basename(__FILE__) )) { return $post_id; } // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want // to do anything if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id; // Check permissions if ( 'page' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id ) ) return $post_id; } else { if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id; } // OK, we're authenticated: we need to find and save the data // delete all current attachments meta // moved outside conditional, else we can never delete all attachments delete_post_meta($post_id, '_attachments'); // Since we're allowing Attachments to be sortable, we can't simply increment a counter // we need to keep track of the IDs we're given $attachment_ids = array(); // We'll build our array of attachments foreach($_POST as $key => $data) { // Arbitrarily using the id if( substr($key, 0, 14) == 'attachment_id_' ) { array_push( $attachment_ids, substr( $key, 14, strlen( $key ) ) ); } } // If we have attachments, there's work to do if( !empty( $attachment_ids ) ) { foreach ( $attachment_ids as $i ) { if( !empty( $_POST['attachment_id_' . $i] ) ) { $attachment_details = array( 'id' => $_POST['attachment_id_' . $i], 'title' => $_POST['attachment_title_' . $i], 'caption' => $_POST['attachment_caption_' . $i], 'order' => $_POST['attachment_order_' . $i] ); // serialize data and encode $attachment_serialized = base64_encode( serialize( $attachment_details ) ); // add individual attachment add_post_meta( $post_id, '_attachments', $attachment_serialized ); } } } } /** * Retrieves all Attachments for provided Post or Page * * @param int $post_id (optional) ID of target Post or Page, otherwise pulls from global $post * @return array $post_attachments * @author Jonathan Christopher * @author JR Tashjian */ function attachments_get_attachments( $post_id=null ) { global $post; if( $post_id==null ) { $post_id = $post->ID; } // get all attachments $existing_attachments = get_post_meta( $post_id, '_attachments', false ); $legacy_existing_attachments = unserialize( $existing_attachments[0] ); // Check for legacy attachments if( is_array( $legacy_existing_attachments ) ) { $tmp_legacy_attachments = array(); // Legacy attachments (single serialized record) foreach ( $legacy_existing_attachments as $legacy_attachment ) { array_push( $tmp_legacy_attachments, base64_encode( serialize( $legacy_attachment ) ) ); } $existing_attachments = $tmp_legacy_attachments; } // We can now proceed as normal, all legacy data should now be upgraded if( is_array( $existing_attachments ) && count( $existing_attachments ) > 0 ) { $post_attachments = array(); foreach ($existing_attachments as $attachment) { // decode and unserialize the data $data = unserialize( base64_decode( $attachment ) ); array_push( $post_attachments, array( 'id' => stripslashes( $data['id'] ), 'name' => stripslashes( get_the_title( $data['id'] ) ), 'mime' => stripslashes( get_post_mime_type( $data['id'] ) ), 'title' => stripslashes( $data['title'] ), 'caption' => stripslashes( $data['caption'] ), 'location' => stripslashes( wp_get_attachment_url( $data['id'] ) ), 'order' => stripslashes( $data['order'] ) )); } // sort attachments if( count( $post_attachments ) > 1 ) { usort( $post_attachments, "attachments_cmp" ); } } return $post_attachments; } /** * This is the main initialization function, it will invoke the necessary meta_box * * @return void * @author Jonathan Christopher */ function attachments_init() { wp_enqueue_style('attachments', WP_PLUGIN_URL . '/attachments/css/attachments.css'); wp_enqueue_script('attachments', WP_PLUGIN_URL . '/attachments/js/attachments.js'); attachments_meta_box(); }