$value ) { if ( $value ) { $post_types[] = $post_type; } } // set up our WP_Query args to grab anything with legacy data $args = array( 'post_type' => isset( $post_types ) ? $post_types : array(), 'post_status' => 'any', 'posts_per_page' => -1, 'meta_key' => '_attachments', 'suppress_filters' => true, ); $query = new WP_Query( $args ); } $count = 0; // loop through each post if ( $query ) { while( $query->have_posts() ) { // set up postdata $query->the_post(); // let's first decode our Attachments data $existing_attachments = get_post_meta( $query->post->ID, '_attachments', false ); $post_attachments = array(); // check to make sure we've got data if ( is_array( $existing_attachments ) && count( $existing_attachments ) > 0 ) { // loop through each existing attachment foreach( $existing_attachments as $attachment ) { // decode and unserialize the data $data = unserialize( base64_decode( $attachment ) ); array_push( $post_attachments, array( 'id' => stripslashes( $data['id'] ), 'title' => stripslashes( $data['title'] ), 'caption' => stripslashes( $data['caption'] ), 'order' => stripslashes( $data['order'] ) ) ); } // sort attachments if ( count( $post_attachments ) > 1 ) { usort( $post_attachments, 'attachments_cmp' ); } } // we have our Attachments entries // let's check to see if we're migrating after population has taken place $existing_attachments = get_post_meta( $query->post->ID, $this->get_meta_key(), false ); if ( ! isset( $existing_attachments[0] ) ) { $existing_attachments[0] = ''; } $existing_attachments = json_decode( $existing_attachments[0] ); if ( ! is_object( $existing_attachments ) ) { $existing_attachments = new stdClass(); } // we'll loop through the legacy Attachments and save them in the new format foreach ( $post_attachments as $legacy_attachment ) { // convert to the new format $converted_attachment = array( 'id' => $legacy_attachment['id'] ); // fields are technically optional so we'll add those separately // we're also going to encode them in the same way the main class does if ( $title ) { $converted_attachment['fields'][$title] = htmlentities( stripslashes( $legacy_attachment['title'] ), ENT_QUOTES, 'UTF-8' ); } if ( $caption ) { $converted_attachment['fields'][$caption] = htmlentities( stripslashes( $legacy_attachment['caption'] ), ENT_QUOTES, 'UTF-8' ); } // check to see if the existing Attachments have our target instance if ( ! isset( $existing_attachments->$instance ) ) { // the instance doesn't exist so we need to create it $existing_attachments->$instance = array(); } // we need to convert our array to an object $converted_attachment['fields'] = (object) $converted_attachment['fields']; $converted_attachment = (object) $converted_attachment; // append this legacy attachment to the existing instance array_push( $existing_attachments->$instance, $converted_attachment ); } // we're done! let's save everything in our new format $existing_attachments = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $existing_attachments, JSON_UNESCAPED_UNICODE ) : json_encode( $existing_attachments ); // fix potentially encoded Unicode $existing_attachments = str_replace( '\\', '\\\\', $existing_attachments ); // save it to the database update_post_meta( $query->post->ID, 'attachments', $existing_attachments ); // increment our counter $count++; } } return $count; } /** * Step 1 of the migration process. Allows the user to define the target instance and field names. * * @since 3.2 */ function prepare_migration() { if ( ! wp_verify_nonce( $_GET['nonce'], 'attachments-migrate-1') ) { wp_die( __( 'Invalid request', 'attachments' ) ); } ?>
migrate( $_GET['attachments-instance'], $_GET['attachments-title'], $_GET['attachments-caption'] ); if( false == get_option( 'attachments_migrated' ) ) { ?>: .
migrate_pro( $attachments_pro_instance ); } $total_attachments = 0; if ( ! empty( $totals ) ) { foreach ( $totals as $instance_total ) { $total_attachments += $instance_total['total']; } } if ( false == get_option( 'attachments_pro_migrated' ) ) : ?>:
functions.php in order to have Attachments' meta boxes show up where appropriate:", 'attachments' ); ?>
if( !function_exists( 'migrated_pro_attachments' ) ) { function migrated_pro_attachments( $attachments ) { $fields = array( array( 'name' => '', 'type' => '', 'label' => '', 'default' => '', ), ); $args = array( 'label' => '', 'post_type' => array( '' ), 'note' => '', 'fields' => $fields, ); $attachments->register( '', $args ); } } add_action( 'attachments_register', 'migrated_pro_attachments' ); /** * Facilitate Attachments conditional inclusion of meta boxes for existing instances */ if( !function_exists( 'attachments_is_allowed' ) ) { function attachments_is_allowed( $conditions = array(), $match = 'any' ) { global $post; $match = ( $match == 'any' ) ? 'any' : 'all'; $allowed = false; $short_circuit = false; if( is_array( $conditions ) && !empty( $conditions ) ) { foreach( $conditions as $condition ) { if( ( $match == 'all' && !$short_circuit ) || $match == 'any' ) { $parameter = $condition['param']; $operator = $condition['operator']; $limiter = $condition['limiter']; switch( $parameter ) { case 'post_type': if( $operator == 'is' ) { if( $post->post_type == $limiter ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } else { if( $post->post_type != $limiter ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } break; case 'post_format': $post_format = get_post_format( $post->ID ); if( $operator == 'is' ) { if( $post_format == $limiter ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } else { if( $post_format != $limiter ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } break; case 'page': if( $operator == 'is' ) { if( $post->ID == $limiter ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } else { if( $post->ID != $limiter ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } break; case 'role': $current_user = wp_get_current_user(); if( $operator == 'is' ) { if( array_search( strtolower( $limiter ), $current_user->roles ) !== false ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } else { if( array_search( strtolower( $limiter ), $current_user->roles ) === false ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } break; case 'template': $current_template = get_post_meta( $post->ID, '_wp_page_template', true ); if( $operator == 'is' ) { if( $current_template == $limiter ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } else { if( $current_template != $limiter ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } break; case 'parent': if( $operator == 'is' ) { if( $post->post_parent == $limiter ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } else { if( $post->post_parent != $limiter ) { $allowed = true; } elseif( $match == 'all' ) { $short_circuit = true; } } break; default: break; } } } } return ( $allowed && !$short_circuit ) ? true : false; } } function attachments_check_() { $conditions = array( array( 'param' => '', 'operator' => '', 'limiter' => '', ), ); return attachments_is_allowed( $conditions, '' ); } add_filter( "attachments_location_", 'attachments_check_' ); /** * Facilitate Attachments auto-append template parsing */ if( !function_exists( 'attachments_magic_tags_processor' ) ) { function attachments_magic_tags_processor( $in ) { global $attachments_magic_tag_index, $attachments_auto_append_ref; $name = $in[2]; switch( $name ) { case 'index': $value = $attachments_magic_tag_index; break; case 'id': $value = $attachments_auto_append_ref->id(); break; case 'thumb': $value = $attachments_auto_append_ref->image( 'thumbnail' ); break; case 'url': $value = $attachments_auto_append_ref->url(); break; case 'mime': case 'type': $value = $attachments_auto_append_ref->type(); break; case 'subtype': $value = $attachments_auto_append_ref->subtype(); break; case 'filesize': $value = $attachments_auto_append_ref->filesize(); break; default: $value = ''; break; } // we might be dealing with fields if( empty( $value ) ) { if( strpos( $name, 'field_' ) !== false ) { // we are dealing with a field $field = explode( '_', $name ); if( isset( $field[1] ) ) $value = $attachments_auto_append_ref->field( $field[1] ); } } // image details if( empty( $value ) ) { $request = explode( '.', $name ); if( is_array( $request ) && count( $request ) == 2 ) { switch ( $request[1] ) { case 'url': case 'src': $value = $attachments_auto_append_ref->src( $request[0] ); break; case 'width': $value = $attachments_auto_append_ref->width( $request[0] ); break; case 'height': $value = $attachments_auto_append_ref->height( $request[0] ); break; case 'filesize': $value = $attachments_auto_append_ref->filesize( null, $request[0] ); break; default: $value = ''; break; } } } return $value; } } if( !is_admin() ) { function attachments_auto_append_( $content ) { global $attachments_magic_tag_index, $attachments_auto_append_ref; $original = $content; $output = ''; $template = <<deactivate Attachments Pro.", 'attachments' ); ?>
you still need to edit your template files where appropriate. Please see the documentation for more information. The following sample code can be used as a starting point:', 'attachments' ); ?>
<?php $attachments = new Attachments( '' ); ?> <?php if( $attachments->exist() ) : ?> <h3></h3> <p>Total : <?php echo $attachments->total(); ?></p> <ul> <?php while( $attachments->get() ) : ?> <li> ID: <?php echo $attachments->id(); ?><br /> Type: <?php echo $attachments->type(); ?><br /> Subtype: <?php echo $attachments->subtype(); ?><br /> URL: <?php echo $attachments->url(); ?><br /> Image: <?php echo $attachments->image( 'thumbnail' ); ?><br /> Source: <?php echo $attachments->src( 'full' ); ?><br /> Size: <?php echo $attachments->filesize(); ?><br /> : <?php echo $attachments->field( '' ); ?><br /> </li> <?php endwhile; ?> </ul> <?php endif; ?> user_email, '[Attachments] ' . get_bloginfo( 'name' ) . ' - Code Samples', $message ); // we're also going to store them in the database for future reference update_option( 'attachments_pro_functions', $code_for_functionsphp ); update_option( 'attachments_pro_template', $sample_template_code ); ?>!empty( $post_types ) ? $post_types : array( 'post', 'page' ), 'post_status' => 'any', 'posts_per_page' => -1, 'meta_key' => '_attachments_pro', 'suppress_filters' => true, ); $query = new WP_Query( $args ); $count = array( 'instance' => $instance['name'], 'total' => 0 ); if( $query ) { while( $query->have_posts() ) { // set up postdata $query->the_post(); // let's first decode our Attachments Pro data $existing_instances = get_post_meta( $query->post->ID, '_attachments_pro', true ); $existing_instances = isset( $existing_instances['attachments'] ) ? $existing_instances['attachments'] : false; // check to make sure we've got data if( is_array( $existing_instances ) && count( $existing_instances ) > 0 ) { $post_attachments = array(); foreach( $existing_instances as $instance_name => $instance_attachments ) { if( $instance_name == $instance['name'] ) { $post_attachments[$instance_name] = array(); $converted_attachment = array(); foreach( $instance_attachments as $instance_attachment ) { $converted_attachment['id'] = $instance_attachment['id']; if( is_array( $instance_attachment['fields'] ) ) { $converted_attachment['fields'] = array(); foreach( $instance_attachment['fields'] as $instance_attachment_field_key => $instance_attachment_field ) { $destination_field_name = $instance['fields'][$instance_attachment_field_key]['label']; $destination_field_name = str_replace( '-', '_', sanitize_title( $destination_field_name ) ); $converted_attachment['fields'][$destination_field_name] = $instance_attachment_field; } unset( $instance_attachment_field_key ); } unset( $instance_attachment ); $post_attachments[$instance_name][] = $converted_attachment; $count['total']++; } } } if( !empty( $post_attachments ) ) { // before saving the converted data, we need to see if there is existing Attachments data $this->apply_init_filters(); $existing_attachments = get_post_meta( $query->post->ID, $this->get_meta_key(), false ); if( !isset( $existing_attachments[0] ) ) $existing_attachments[0] = ''; $existing_attachments = json_decode( $existing_attachments[0] ); if( !is_object( $existing_attachments ) ) $existing_attachments = new stdClass(); foreach( $post_attachments as $instance => $attachments ) { // check to see if the existing Attachments have our target instance if( !isset( $existing_attachments->$instance ) ) { // the instance doesn't exist so we need to create it $existing_attachments->$instance = array(); } // loop through the instance attachments and integrate new and old foreach( $attachments as $attachment ) { // we need to convert our array to an object $attachment['fields'] = (object) $attachment['fields']; $attachment = (object) $attachment; array_push( $existing_attachments->$instance, $attachment ); } } // now we can save $existing_attachments = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $existing_attachments, JSON_UNESCAPED_UNICODE ) : json_encode( $existing_attachments ); // fix potentially encoded Unicode $existing_attachments = str_replace( '\\', '\\\\', $existing_attachments ); update_post_meta( $query->post->ID, $this->get_meta_key(), $existing_attachments ); } } } } return $count; } }