save'); try { $post = get_post($post_id); // If this is not an image, bail if ( strncasecmp( $post->post_mime_type, 'image', 5) ) { // error_log('Mime type is ' . $post->post_mime_type ); return $post_id; } // Bail out now if POST vars not set if ( ! isset( $_POST[ Add_Img_Maps::name() . '-nonce'] ) ) { return $post_id; } // Bail out now if nonce doesn't verify if ( ! wp_verify_nonce( $_POST[ Add_Img_Maps::name() . '-nonce'], Add_Img_Maps::name() ) ) { return $post_id; } // Start by turning the relevant input forms into an array. $input = array(); foreach ( $_POST as $field => $val ) { /* if the key belongs to this plugin */ if ( ! strncmp( $field, Add_Img_Maps::attr_prefix(), 10) ) { /* Field names translate into a layered associative array - * EG addimgmaps->size->0->shape - and each input field is a * 'leaf' */ $subkeys = explode('-', sanitize_text_field($field) ); $val = sanitize_text_field( $val ); // $subkeys[0] is the prefix, so not part of the array // $subkeys[1] is the image size. // $subkeys[2] is usually a numerical area list, or a flag (eg 'rm') switch ( count($subkeys) ) { case 2: $input[ $subkeys[1] ] = $val; break; case 3: $input[ $subkeys[1] ][ $subkeys[2] ] = $val; break; case 4: $input[ $subkeys[1] ][ $subkeys[2] ][ $subkeys[3] ] = $val; break; case 5: $input[ $subkeys[1] ][ $subkeys[2] ][ $subkeys[3] ][ $subkeys[4] ] = $val; break; default: // This should not happen, and is an apt error log. error_log( "Add_Img_Maps: Unrecognised input option: $field."); } } } /* INPUT now has the form * * $input[$size][$areaNum] = [ shape=>$shape, alt=>$alt, href=>$href, 0,1,2,3...=>[ $x, $y] ] * */ /* No addimgmaps input at all */ if ( 0 == count($input) ) { return; } /* Have we been passed any 'input' values without the 'unchanged' flag? */ if ( 0 == count( array_filter( $input, function($map) { /* If the 'unchanged' flag is either absent or false */ return (! isset($map['unchanged']) or ! $map['unchanged']); }) )) { // error_log('No changes in add_img_maps'); return; } //error_log( 'Parsed $input:' ); //error_log( print_r( $input, true ) ); // Load the previously saved image maps as an array. $maps_metadata = get_post_meta( $post_id, Add_Img_Maps::get_key(), true ); //error_log('Retrieved old post metadata:'); //error_log( print_r( $maps_metadata, true ) ); // If this is an entirely new image map ... if ( ! $maps_metadata ) { $maps_metadata = array (); // If the metadata are storing just one map, it's the one for the full image. } elseif ( $maps_metadata instanceof Add_Img_Maps_Map ) { // (For backwards compatibility with data saved by an earlier dev version.) $fixed_maps = array( 'full' => $maps_metadata ); $maps_metadata = $fixed_maps; } // Which maps are new with this save? $new_maps = array_diff_key( $input, $maps_metadata) ; // Process all the old maps first. //error_log( 'List of key (sizes) of $new_maps (if any):'); //error_log( print_r( $new_maps, true ) ); foreach ( $maps_metadata as $size => $map ) { if( is_numeric($size) ) { throw new Exception("Unexpected size $size. maps_metadata: " . print_r($maps_metadata, true)); } /* If the flag is set to remove this map ... */ if ( isset($input[$size]['rm']) and $input[$size]['rm'] ) { unset ( $maps_metadata[$size] ); /* If the flag is set to say this map isn't changed, it saves us some processing. */ } elseif ( isset($input['size']['unchanged']) and $input[$size]['unchanged']) { /*do nothing */; /* Else the input defines the new map */ } else { //Remove the flags (unset doesn't throw an error if it doesn't exist) unset( $input[$size]['unchanged'], $input[$size]['rm'] ); // Send the rest of it to the constructor. $maps_metadata[$size] = new Add_Img_Maps_Map( $input[$size] ); } } /* New maps are in $input but not maps_metadata; construct them too */ foreach( $new_maps as $size => $map ) { $maps_metadata[$size] = new Add_Img_Maps_Map( $input[$size] ); } /* And update the metadata */ // error_log( 'After updating, maps_metadata are: ' . print_r( $maps_metadata, true ) ); update_post_meta( $post_id, Add_Img_Maps::get_key(), $maps_metadata ); } catch ( Exception $e) { //anything go wrong? ?>
'; error_log ("Plugin Add_Img_Maps caught Exception: $e"); ?>
"%s" ignored.', Add_Img_Maps::name() ), $image_size ); ?>
post_parent ), $post->post_parent ); } else { ?>
this to which you wish to add a map?', // translators: plural 'This image has apparently been used to create others. Is it one of these to which you wish to add a map?', count( $child_images ), Add_Img_Maps::name() ); ?>
'; error_log ("Plugin Add_Img_Maps caught exception during display of metadata box: $e"); ?>