From c1be1fde668924d47b1cd3a78be39c5b8506cbb1 Mon Sep 17 00:00:00 2001 From: Memo Date: Thu, 22 Feb 2024 19:11:54 -0300 Subject: [PATCH 1/2] Add a wp-cli command to fix image caption and include the fix in normal import process --- .../plugins/pri-migration-helper/cli/cli.php | 4 + .../cli/includes/class-pmh-worker.php | 164 ++++++++++++++++++ .../media_filters/media-global-filters.php | 22 +-- 3 files changed, 179 insertions(+), 11 deletions(-) diff --git a/wp-content/plugins/pri-migration-helper/cli/cli.php b/wp-content/plugins/pri-migration-helper/cli/cli.php index f2dc0f277..c1d2a5a01 100644 --- a/wp-content/plugins/pri-migration-helper/cli/cli.php +++ b/wp-content/plugins/pri-migration-helper/cli/cli.php @@ -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. diff --git a/wp-content/plugins/pri-migration-helper/cli/includes/class-pmh-worker.php b/wp-content/plugins/pri-migration-helper/cli/includes/class-pmh-worker.php index 29a02a594..84aeaf757 100644 --- a/wp-content/plugins/pri-migration-helper/cli/includes/class-pmh-worker.php +++ b/wp-content/plugins/pri-migration-helper/cli/includes/class-pmh-worker.php @@ -886,5 +886,169 @@ 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', + 'post_parent' => null, + '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', + ) ); + } + } + } } diff --git a/wp-content/plugins/pri-migration-helper/migration/media_filters/media-global-filters.php b/wp-content/plugins/pri-migration-helper/migration/media_filters/media-global-filters.php index 2d842b053..56e0082e1 100644 --- a/wp-content/plugins/pri-migration-helper/migration/media_filters/media-global-filters.php +++ b/wp-content/plugins/pri-migration-helper/migration/media_filters/media-global-filters.php @@ -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 @@ -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} @@ -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; @@ -512,17 +518,9 @@ function pmh_extra_image_attributes( $image_attributs, $file ) { 'related_files', 'image_alt', 'image_title', + 'caption', ); - /* - Debug - echo "
";
-	var_dump( 'pmh_extra_image_attributes' );
-	var_dump( $file );
-	var_dump( $image_attributs );
-	echo "
"; - */ - foreach ( $extra_attributes as $key ) { if ( isset( $file[ $key ] ) ) { @@ -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'] : ''; } @@ -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 ); From 511421fc283ea404db4e3350b869a3fe21dbff5a Mon Sep 17 00:00:00 2001 From: Memo Date: Mon, 26 Feb 2024 16:29:18 -0300 Subject: [PATCH 2/2] Remove post parent key from wp_query --- .../pri-migration-helper/cli/includes/class-pmh-worker.php | 1 - 1 file changed, 1 deletion(-) diff --git a/wp-content/plugins/pri-migration-helper/cli/includes/class-pmh-worker.php b/wp-content/plugins/pri-migration-helper/cli/includes/class-pmh-worker.php index 84aeaf757..ed2c9dab5 100644 --- a/wp-content/plugins/pri-migration-helper/cli/includes/class-pmh-worker.php +++ b/wp-content/plugins/pri-migration-helper/cli/includes/class-pmh-worker.php @@ -970,7 +970,6 @@ public function get_images_by_asc_id( $per_process_limit ) { 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', - 'post_parent' => null, 'fields' => 'ids', 'orderby' => 'ID', 'order' => 'ASC',