Skip to content

Commit

Permalink
Merge pull request #163 from PRX/fix_caption
Browse files Browse the repository at this point in the history
Fix Image Caption
  • Loading branch information
gtenaschuk authored Feb 29, 2024
2 parents 776b561 + 511421f commit a8cff1f
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 11 deletions.
4 changes: 4 additions & 0 deletions wp-content/plugins/pri-migration-helper/cli/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
* 4. posts_content_media_fix [limit, comma_separated_post_ids(optional)]
* 5. single_post_content_media_fix [post_id]
* 6. post_tags_fix_duplicate [start_page_number post_per_page]
* 7. image_captions [last_wordpress_id, limit_per_process]
* Both are optional. If not provided, it will start from the beginning by 100 per process.
* 8. _image_captions [wp_image_id]
* If targetting a single image, use this command instead.
*/

// Add 'tw-media-fix-all' command to wp-cli.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,5 +886,168 @@ public function post_tags_fix_duplicate_page( $args ) {
// Return false if no post_tags found.
return count( $a_post_tags ) > 0 ? $a_post_tags[0] : null;
}

/**
* Import extra image fields from Drupal to WordPress.
*
* Example: wp tw-fix image_captions
*
* @param array $a_args
*/
public function image_captions( $args ) {

// Simulate running importer.
global $fgd2wpp;
ob_start();
$fgd2wpp->importer();
ob_get_clean();

// Connect.
WP_CLI::log( 'Connecting to Drupal.' );

$connected = $fgd2wpp->drupal_connect();

// If Drupal connection is established.
if ( $connected ) {

// Show success.
WP_CLI::success( "Drupal connection is established." );

} else {

// Return error.
WP_CLI::error( "Drupal connection is not established." );
}

// Set the minimum post ID
$i_start_from = isset( $args[0] ) ? (int) $args[0] : 0;
$limit = isset( $args[1] ) ? (int) $args[1] : 100;

// Set the last ID.
$this->i_last_id = $i_start_from;

// Get all media ids.
$a_media_ids = $this->get_images_by_asc_id( $limit );

// Start.
WP_CLI::log( 'Processing images..' );

// Loop through all media ids.
do {

// Start.
WP_CLI::log( wp_sprintf( 'Running fix for %s images starting from ID: %s', $limit, $this->i_last_id ) );

if ( ! $a_media_ids ) {
break;
}

// Loop through all media ids.
foreach ( $a_media_ids as $i_media_id ) {

$this->_image_captions( array( $i_media_id, $connected ) );

$this->i_last_id = (int) $i_media_id;
}

// Get all media ids.
$a_media_ids = $this->get_images_by_asc_id( 100 );

} while ( $a_media_ids );
}

/**
* Process images in batches.
*
* @param int $per_process_limit The number of images to process in each batch.
*
* @return array The post ids.
*/
public function get_images_by_asc_id( $per_process_limit ) {

// Query all the images.
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'fields' => 'ids',
'orderby' => 'ID',
'order' => 'ASC',
// 'order' => 'DESC',
'posts_per_page' => $per_process_limit,
'no_found_rows' => true,
);

// Add filter to query.
add_filter( 'posts_where', array( $this, 'image_fix_all_wp_query' ) );

// Do the query.
$query = new WP_Query($args);

// Get the posts.
$post_ids = $query->get_posts();

// Remove filter.
remove_filter( 'posts_where', array( $this, 'image_fix_all_wp_query' ) );

return $post_ids;
}

public function _image_captions( $args ) {

// Get global.
global $fgd2wpp;

// Set args.
$post_id = (int) $args[0];
$connected = (bool) boolval( $args[1] );

// If Drupal connection is established.
if ( ! $connected ) {

// Simulate running importer.
ob_start();
$fgd2wpp->importer();
ob_get_clean();

// Connect.
$fgd2wpp->drupal_connect();
}

// Get drupal nid.
$fid = get_post_meta( $post_id, 'fid', true );

if ( $fid ) {

// Start.
WP_CLI::log( wp_sprintf( 'Processing post media %s.', $post_id ) );

// Get file attributes.
$attributes = pmh_get_file_attributes_images( array( 'fid' => $fid ) );

// If caption is set.
if ( isset( $attributes['caption'] ) && $attributes['caption'] ) {

$caption = wp_strip_all_tags( $attributes['caption'] );

// Update post excerpt.
$post = array(
'ID' => $post_id,
'post_excerpt' => $caption,
);

// Update the post into the database.
$updated = wp_update_post( $post );
// $updated = true;

// Report success.
WP_CLI::log( wp_sprintf(
'- Post media %s update (%s)',
$post_id,
$updated ? 'success' : 'failed',
) );
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ function pmh_get_file_attributes_images( $attributes ) {
fitt.field_file_image_title_text_value AS image_title,
fc.field_credit_value AS credit,
fhi.field_hide_image_value AS hide_image,
ttd.name AS license
ttd.name AS license,
fcap.field_caption_value AS caption
FROM
file_managed AS fm
Expand Down Expand Up @@ -198,6 +199,10 @@ function pmh_get_file_attributes_images( $attributes ) {
taxonomy_term_data AS ttd
ON fl.field_licence_tid = ttd.tid
LEFT JOIN
field_data_field_caption fcap
ON fm.fid = fcap.entity_id
WHERE
fm.fid = {$fid}
Expand All @@ -221,6 +226,7 @@ function pmh_get_file_attributes_images( $attributes ) {
$attributes['credit'] = $results_1[0]['credit'];
$attributes['hide_image'] = $results_1[0]['hide_image'];
$attributes['license'] = $results_1[0]['license'];
$attributes['caption'] = $results_1[0]['caption'];
}

return $attributes;
Expand Down Expand Up @@ -512,17 +518,9 @@ function pmh_extra_image_attributes( $image_attributs, $file ) {
'related_files',
'image_alt',
'image_title',
'caption',
);

/*
Debug
echo "<pre>";
var_dump( 'pmh_extra_image_attributes' );
var_dump( $file );
var_dump( $image_attributs );
echo "</pre>";
*/

foreach ( $extra_attributes as $key ) {

if ( isset( $file[ $key ] ) ) {
Expand Down Expand Up @@ -568,6 +566,8 @@ function pmh_add_external_media_without_import( $url, $attributes = array(), $op
$related_files = isset( $attributes['related_files'] ) ? $attributes['related_files'] : array();
$image_title = isset( $attributes['image_title'] ) ? $attributes['image_title'] : '';
$alt = isset( $attributes['alt'] ) ? $attributes['alt'] : '';
$caption = isset( $attributes['caption'] ) ? $attributes['caption'] : '';

if ( empty( $alt ) ) {
$alt = isset( $attributes['image_alt'] ) ? $attributes['image_alt'] : '';
}
Expand All @@ -585,7 +585,7 @@ function pmh_add_external_media_without_import( $url, $attributes = array(), $op
'post_mime_type' => $mime_type,
'post_title' => sanitize_title( preg_replace( '/\.[^.]+$/', '', $filename ) ),
'post_content' => wp_strip_all_tags( $description ),
'post_excerpt' => wp_strip_all_tags( $image_caption ),
'post_excerpt' => wp_strip_all_tags( $caption ),
);
$attachment = apply_filters( 'fgd2wp_pre_insert_post', $attachment, $attributes );
$attachment_id = wp_insert_attachment( $attachment );
Expand Down

0 comments on commit a8cff1f

Please sign in to comment.