From 8c59422132860d75ff8b5e8b98f1e9b102cad5eb Mon Sep 17 00:00:00 2001 From: Ivan Uravic Date: Fri, 23 Feb 2024 15:06:15 +0100 Subject: [PATCH 1/3] Beacon Sites, set brand command. --- newspack-custom-content-migrator.php | 1 + .../PublisherSpecific/BeaconSitesMigrator.php | 123 ++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/Command/PublisherSpecific/BeaconSitesMigrator.php diff --git a/newspack-custom-content-migrator.php b/newspack-custom-content-migrator.php index 32ebdb1d..4b4a6e9e 100644 --- a/newspack-custom-content-migrator.php +++ b/newspack-custom-content-migrator.php @@ -87,5 +87,6 @@ Command\PublisherSpecific\LinkNYCMigrator::class, Command\PublisherSpecific\WindyCityMigrator::class, Command\PublisherSpecific\InjusticeWatchMigrator::class, + Command\PublisherSpecific\BeaconSitesMigrator::class, ) ); diff --git a/src/Command/PublisherSpecific/BeaconSitesMigrator.php b/src/Command/PublisherSpecific/BeaconSitesMigrator.php new file mode 100644 index 00000000..1eae674c --- /dev/null +++ b/src/Command/PublisherSpecific/BeaconSitesMigrator.php @@ -0,0 +1,123 @@ +posts = new Posts(); + $this->logger = new Logger(); + } + + /** + * Singleton get_instance(). + * + * @return InterfaceCommand|null + */ + public static function get_instance() { + $class = get_called_class(); + if ( null === self::$instance ) { + self::$instance = new $class(); + } + + return self::$instance; + } + + /** + * See InterfaceCommand::register_commands. + */ + public function register_commands() { + WP_CLI::add_command( + 'newspack-content-migrator beacon-sites-set-brands-on-posts', + [ $this, 'cmd_set_brands_on_posts' ], + [ + 'shortdesc' => 'Sets a brand to all the posts.', + 'synopsis' => [ + [ + 'type' => 'assoc', + 'name' => 'brand-name', + 'optional' => false, + 'repeating' => false, + ], + ], + ] + ); + } + + /** + * Extract the old avatars from Starbox options and migrate them to Simple Local Avatars. + * + * @param array $pos_args Positional arguments. + * @param array $assoc_args Associative arguments. + */ + public function cmd_set_brands_on_posts( $pos_args, $assoc_args ) { + $brand_name = $assoc_args['brand-name']; + + $log_file = 'beacon-sites-set-brands-on-posts.log'; + + if ( ! taxonomy_exists( 'brand' ) ) { + WP_CLI::error( 'Brand taxonomy does not exist. Best to temporariliy install and activate newspack-multibranded-site so that this command can run.' ); + } + + // Get brand ID. + $brand_term = get_term_by( 'name', $brand_name, 'brand' ); + if ( ! $brand_term ) { + $brand_term = wp_insert_term( $brand_name, 'brand' ); + if ( is_wp_error( $brand_term ) ) { + WP_CLI::error( sprintf( 'Error getting/creating brand `%s`, err: %s', $brand_name, $brand_term->get_error_message() ) ); + } + } + $brand_ids = [ + $brand_term->term_id, + ]; + + $post_ids = $this->posts->get_all_posts_ids(); + foreach ( $post_ids as $key_post_id => $post_id ) { + WP_CLI::line( sprintf( '%d/%d %d', $key_post_id + 1, count( $post_ids ), $post_id ) ); + $set = wp_set_post_terms( $post_id, $brand_ids, 'brand' ); + if ( ! $set || is_wp_error( $set ) ) { + $this->logger->log( $log_file, sprintf( 'Error setting brand %s to post %d, err.msg: %s', $brand_name, $post_id, is_wp_error( $set ) ? $set->get_error_message() : 'n/a' ), 'error', true ); + } + $this->logger->log( $log_file, sprintf( 'Setting brand %s for post %d', $brand_name, $post_id ) ); + } + + WP_CLI::success( 'Done.' ); + } +} From 61f3cfbcdd618d42e82ff764aff1ea6e6a55b736 Mon Sep 17 00:00:00 2001 From: Ivan Uravic Date: Fri, 23 Feb 2024 15:07:18 +0100 Subject: [PATCH 2/3] Comment --- src/Command/PublisherSpecific/BeaconSitesMigrator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/PublisherSpecific/BeaconSitesMigrator.php b/src/Command/PublisherSpecific/BeaconSitesMigrator.php index 1eae674c..4b620502 100644 --- a/src/Command/PublisherSpecific/BeaconSitesMigrator.php +++ b/src/Command/PublisherSpecific/BeaconSitesMigrator.php @@ -82,7 +82,7 @@ public function register_commands() { } /** - * Extract the old avatars from Starbox options and migrate them to Simple Local Avatars. + * Set brand to all posts. * * @param array $pos_args Positional arguments. * @param array $assoc_args Associative arguments. From 493df3535c9bfe54f3b2cb4fc4297961020f2d68 Mon Sep 17 00:00:00 2001 From: Ivan Uravic Date: Fri, 23 Feb 2024 15:47:28 +0100 Subject: [PATCH 3/3] Optional post IDs --- .../PublisherSpecific/BeaconSitesMigrator.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Command/PublisherSpecific/BeaconSitesMigrator.php b/src/Command/PublisherSpecific/BeaconSitesMigrator.php index 4b620502..d54c11d3 100644 --- a/src/Command/PublisherSpecific/BeaconSitesMigrator.php +++ b/src/Command/PublisherSpecific/BeaconSitesMigrator.php @@ -68,7 +68,7 @@ public function register_commands() { 'newspack-content-migrator beacon-sites-set-brands-on-posts', [ $this, 'cmd_set_brands_on_posts' ], [ - 'shortdesc' => 'Sets a brand to all the posts.', + 'shortdesc' => 'Sets a brand to all or just specific posts.', 'synopsis' => [ [ 'type' => 'assoc', @@ -76,6 +76,13 @@ public function register_commands() { 'optional' => false, 'repeating' => false, ], + [ + 'type' => 'assoc', + 'name' => 'post-ids-csv', + 'description' => 'If provided, only these IDs will be affected, otherwise all posts will.', + 'optional' => true, + 'repeating' => false, + ], ], ] ); @@ -89,6 +96,7 @@ public function register_commands() { */ public function cmd_set_brands_on_posts( $pos_args, $assoc_args ) { $brand_name = $assoc_args['brand-name']; + $post_ids = isset( $assoc_args['post-ids-csv'] ) ? explode( ',', $assoc_args['post-ids-csv'] ) : null; $log_file = 'beacon-sites-set-brands-on-posts.log'; @@ -108,7 +116,9 @@ public function cmd_set_brands_on_posts( $pos_args, $assoc_args ) { $brand_term->term_id, ]; - $post_ids = $this->posts->get_all_posts_ids(); + if ( is_null( $post_ids ) ) { + $post_ids = $this->posts->get_all_posts_ids(); + } foreach ( $post_ids as $key_post_id => $post_id ) { WP_CLI::line( sprintf( '%d/%d %d', $key_post_id + 1, count( $post_ids ), $post_id ) ); $set = wp_set_post_terms( $post_id, $brand_ids, 'brand' );