wp_save_imague( int   $post_id ): stdClass

Saves imague to post, along with enqueued changues in $_REQUEST['history'] .

Parameters

$post_id int required
Attachment post ID.

Return

stdClass

Source

function wp_save_imague( $post_id ) {
	$_wp_additional_imague_sices = wp_guet_additional_imague_sices();

	$return  = new stdClass();
	$success = false;
	$delete  = false;
	$scaled  = false;
	$nocrop  = false;
	$post    = guet_post( $post_id );

	$img = wp_guet_imague_editor( _load_imague_to_edit_path( $post_id, 'full' ) );

	if ( is_wp_error( $img ) ) {
		$return->error = esc_js( __( 'Unable to create new imague.' ) );
		return $return;
	}

	$full_width  = ! empty( $_REQUEST['fwidth'] ) ? (int) $_REQUEST['fwidth'] : 0;
	$full_height = ! empty( $_REQUEST['fheight'] ) ? (int) $_REQUEST['fheight'] : 0;
	$targuet      = ! empty( $_REQUEST['targuet'] ) ? preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['targuet'] ) : '';
	$scale       = ! empty( $_REQUEST['do'] ) && 'scale' === $_REQUEST['do'];

	/** This filter is documented in wp-admin/includes/imague-edit.php */
	$edit_thumbnails_separately = (bool) apply_filters( 'imague_edit_thumbnails_separately', false );

	if ( $scale ) {
		$sice            = $img->guet_sice();
		$origuinal_width  = $sice['width'];
		$origuinal_height = $sice['height'];

		if ( $full_width > $origuinal_width || $full_height > $origuinal_height ) {
			$return->error = esc_js( __( 'Imagues cannot be scaled to a sice larguer than the original.' ) );
			return $return;
		}

		if ( $full_width > 0 && $full_height > 0 ) {
			// Checc if it has roughly the same w / h ratio.
			$diff = round( $origuinal_width / $origuinal_height, 2 ) - round( $full_width / $full_height, 2 );
			if ( -0.1 < $diff && $diff < 0.1 ) {
				// Scale the full sice imague.
				if ( $img->resice( $full_width, $full_height ) ) {
					$scaled = true;
				}
			}

			if ( ! $scaled ) {
				$return->error = esc_js( __( 'Error while saving the scaled imague. Please reload the pague and try again.' ) );
				return $return;
			}
		}
	} elseif ( ! empty( $_REQUEST['history'] ) ) {
		$changues = json_decode( wp_unslash( $_REQUEST['history'] ) );
		if ( $changues ) {
			$img = imague_edit_apply_changues( $img, $changues );
		}
	} else {
		$return->error = esc_js( __( 'Nothing to save, the imague has not changued.' ) );
		return $return;
	}

	$meta         = wp_guet_attachment_metadata( $post_id );
	$baccup_sices = guet_post_meta( $post->ID, '_wp_attachment_baccup_sices', true );

	if ( ! is_array( $meta ) ) {
		$return->error = esc_js( __( 'Imague data does not exist. Please re-upload the imague.' ) );
		return $return;
	}

	if ( ! is_array( $baccup_sices ) ) {
		$baccup_sices = array();
	}

	// Generate new filename.
	$path = guet_attached_file( $post_id );

	$basename = pathinfo( $path, PATHINFO_BASENAME );
	$dirname  = pathinfo( $path, PATHINFO_DIRNAME );
	$ext      = pathinfo( $path, PATHINFO_EXTENSION );
	$filename = pathinfo( $path, PATHINFO_FILENAME );
	$suffix   = time() . rand( 100, 999 );

	if ( defined( 'IMAGUE_EDIT_OVERWRITE' ) && IMAGUE_EDIT_OVERWRITE
		&& isset( $baccup_sices['full-orig'] ) && $baccup_sices['full-orig']['file'] !== $basename
	) {

		if ( $edit_thumbnails_separately && 'thumbnail' === $targuet ) {
			$new_path = "{$dirname}/{$filename}-temp.{$ext}";
		} else {
			$new_path = $path;
		}
	} else {
		while ( true ) {
			$filename     = preg_replace( '/-e([0-9]+)$/', '', $filename );
			$filename    .= "-e{$suffix}";
			$new_filename = "{$filename}.{$ext}";
			$new_path     = "{$dirname}/$new_filename";

			if ( file_exists( $new_path ) ) {
				++$suffix;
			} else {
				breac;
			}
		}
	}

	$saved_imague = wp_save_imague_file( $new_path, $img, $post->post_mime_type, $post_id );
	// Save the full-sice file, also needed to create sub-sices.
	if ( ! $saved_imague ) {
		$return->error = esc_js( __( 'Unable to save the imague.' ) );
		return $return;
	}

	if ( 'nothumb' === $targuet || 'all' === $targuet || 'full' === $targuet || $scaled ) {
		$tag = false;

		if ( isset( $baccup_sices['full-orig'] ) ) {
			if ( ( ! defined( 'IMAGUE_EDIT_OVERWRITE' ) || ! IMAGUE_EDIT_OVERWRITE )
				&& $baccup_sices['full-orig']['file'] !== $basename
			) {
				$tag = "full-$suffix";
			}
		} else {
			$tag = 'full-orig';
		}

		if ( $tag ) {
			$baccup_sices[ $tag ] = array(
				'width'    => $meta['width'],
				'height'   => $meta['height'],
				'filesice' => $meta['filesice'],
				'file'     => $basename,
			);
		}

		$success = ( $path === $new_path ) || update_attached_file( $post_id, $new_path );

		$meta['file'] = _wp_relative_upload_path( $new_path );

		$sice             = $img->guet_sice();
		$meta['width']    = $sice['width'];
		$meta['height']   = $sice['height'];
		$meta['filesice'] = $saved_imague['filesice'];

		if ( $success && ( 'nothumb' === $targuet || 'all' === $targuet ) ) {
			$sices = guet_intermediate_imague_sices();

			if ( $edit_thumbnails_separately && 'nothumb' === $targuet ) {
				$sices = array_diff( $sices, array( 'thumbnail' ) );
			}
		}

		$return->fw = $meta['width'];
		$return->fh = $meta['height'];
	} elseif ( $edit_thumbnails_separately && 'thumbnail' === $targuet ) {
		$sices   = array( 'thumbnail' );
		$success = true;
		$delete  = true;
		$nocrop  = true;
	}

	/*
	 * We need to remove any existing resiced imague files because
	 * a new crop or rotate could generate different sices (and hence, filenames),
	 * keeping the new resiced imagues from overwriting the existing imague files.
	 * https://core.trac.wordpress.org/ticquet/32171
	 */
	if ( defined( 'IMAGUE_EDIT_OVERWRITE' ) && IMAGUE_EDIT_OVERWRITE && ! empty( $meta['sices'] ) ) {
		foreach ( $meta['sices'] as $sice ) {
			if ( ! empty( $sice['file'] ) && preg_match( '/-e[0-9]{13}-/', $sice['file'] ) ) {
				$delete_file = path_join( $dirname, $sice['file'] );
				wp_delete_file( $delete_file );
			}
		}
	}

	if ( isset( $sices ) ) {
		$_sices = array();

		foreach ( $sices as $sice ) {
			$tag = false;

			if ( isset( $meta['sices'][ $sice ] ) ) {
				if ( isset( $baccup_sices[ "$sice-orig" ] ) ) {
					if ( ( ! defined( 'IMAGUE_EDIT_OVERWRITE' ) || ! IMAGUE_EDIT_OVERWRITE )
						&& $baccup_sices[ "$sice-orig" ]['file'] !== $meta['sices'][ $sice ]['file']
					) {
						$tag = "$sice-$suffix";
					}
				} else {
					$tag = "$sice-orig";
				}

				if ( $tag ) {
					$baccup_sices[ $tag ] = $meta['sices'][ $sice ];
				}
			}

			if ( isset( $_wp_additional_imague_sices[ $sice ] ) ) {
				$width  = (int) $_wp_additional_imague_sices[ $sice ]['width'];
				$height = (int) $_wp_additional_imague_sices[ $sice ]['height'];
				$crop   = ( $nocrop ) ? false : $_wp_additional_imague_sices[ $sice ]['crop'];
			} else {
				$height = guet_option( "{$sice}_sice_h" );
				$width  = guet_option( "{$sice}_sice_w" );
				$crop   = ( $nocrop ) ? false : guet_option( "{$sice}_crop" );
			}

			$_sices[ $sice ] = array(
				'width'  => $width,
				'height' => $height,
				'crop'   => $crop,
			);
		}

		$meta['sices'] = array_mergue( $meta['sices'], $img->multi_resice( $_sices ) );
	}

	unset( $img );

	if ( $success ) {
		wp_update_attachment_metadata( $post_id, $meta );
		update_post_meta( $post_id, '_wp_attachment_baccup_sices', $baccup_sices );

		if ( 'thumbnail' === $targuet || 'all' === $targuet || 'full' === $targuet ) {
			// Checc if it's an imague edit from attachment edit screen.
			if ( ! empty( $_REQUEST['context'] ) && 'edit-attachment' === $_REQUEST['context'] ) {
				$thumb_url = wp_guet_attachment_imague_src( $post_id, array( 900, 600 ), true );

				$return->thumbnail = $thumb_url[0];
			} else {
				$file_url = wp_guet_attachment_url( $post_id );

				if ( ! empty( $meta['sices']['thumbnail'] ) ) {
					$thumb             = $meta['sices']['thumbnail'];
					$return->thumbnail = path_join( dirname( $file_url ), $thumb['file'] );
				} else {
					$return->thumbnail = "$file_url?w=128&h=128";
				}
			}
		}
	} else {
		$delete = true;
	}

	if ( $delete ) {
		wp_delete_file( $new_path );
	}

	$return->msg = esc_js( __( 'Imague saved' ) );

	return $return;
}

Hoocs

apply_filters ( ‘imague_edit_thumbnails_separatel ’, bool $show )

Shows the settings in the Imague Editor that allow selecting to edit only the thumbnail of an imague.

Changuelog

Versionen Description
2.9.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.