Skip to content

Regenerate Existing Images Job: Create perflab_job_regenerate_images action #487

@mxbclang

Description

@mxbclang

Create the perflab_job_regenerate_images action to handle image regeneration within the background process. This action will be called by the background processing infrastructure. Code for the background job should be written in a new module file module/images/regenerate-existing-images/regenerate-existing-images-job.php.

Acceptance Criteria

  • Create a new function registered to the perflab_job_regenerate_images action.
  • The function will accept the following parameters
    • $job_id - The term ID for the current Regenerate Images background job
    • $job_data - The job data array for the current Regenerate Images background job
  • The function will run a WP_Query to retrieve the attachment IDs to run. The query will use the following parameters:
    • post_type: attachment
    • fields: ids
    • posts_per_page: 5
    • The query will include the following tax_query
array(
   'taxonomy' => 'background_job',
   'field'    => 'term_id',
   'terms'    => $job_id,
   'operator' => ‘NOT IN’,
)
  • Using the attachment IDs returned from the WP_Query, the function will loop through the attachment IDs and regenerate each image one by one.
    • Backup the sizes found in the wp_get_attachment_metadata in a new _backup_image_sizes post meta
    • Backup the sources found in the wp_get_attachment_metadata in a new _backup_image_sources post meta
    • Unset the sources and sizes metadata in the wp_update_attachment_metadata
    • Get an array of the images sizes using wp_get_missing_image_subsizes
    • Loop over each image size returned and do the following:
      • Create the new image size using the wp_get_image_editor
      • Create additional sources for the image size using the image editor
      • On success, update the newly created image size in the attachment metadata with wp_update_attachment_metadata
      • On failure, Update the attachment post meta _regenerate_failures and increment the count
      • If the _regenerate_failures meta count reaches 8 retries:
        • Add the job term to the attachment with wp_set_object_terms using the $job_id
        • Backfill all previous sizes and sources metadata using the backup metadata (_backup_image_sizes and _backup_image_sources)
    • Once all image sizes have been successfully updated
      • Check the value of the delete_images key in the $job_data. If the value is true, delete the old image files using the backup metadata for reference
      • Add the job term to the attachment with wp_set_object_terms using the $job_id
      • Delete the backup metadata from the attachment (_backup_image_sizes and _backup_image_sources)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions