Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beacon Sites #444

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newspack-custom-content-migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@
Command\PublisherSpecific\LinkNYCMigrator::class,
Command\PublisherSpecific\WindyCityMigrator::class,
Command\PublisherSpecific\InjusticeWatchMigrator::class,
Command\PublisherSpecific\BeaconSitesMigrator::class,
)
);
133 changes: 133 additions & 0 deletions src/Command/PublisherSpecific/BeaconSitesMigrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* Migrator for Beacon Sites.
*
* @package NewspackCustomContentMigrator
*/

namespace NewspackCustomContentMigrator\Command\PublisherSpecific;

use NewspackCustomContentMigrator\Logic\Posts;
use NewspackCustomContentMigrator\Command\InterfaceCommand;
use NewspackCustomContentMigrator\Utils\Logger;
use WP_CLI;

/**
* Custom migration scripts for BeaconSites.
*/
class BeaconSitesMigrator implements InterfaceCommand {

/**
* Instance of BeaconSitesMigrator.
*
* @var null|InterfaceCommand
*/
private static $instance = null;

/**
* Posts.
*
* @var Posts
*/
private $posts;

/**
* Logger.
*
* @var Logger
*/
private $logger;

/**
* Constructor.
*/
private function __construct() {
$this->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 or just specific posts.',
'synopsis' => [
[
'type' => 'assoc',
'name' => 'brand-name',
'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,
],
],
]
);
}

/**
* Set brand to all posts.
*
* @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'];
$post_ids = isset( $assoc_args['post-ids-csv'] ) ? explode( ',', $assoc_args['post-ids-csv'] ) : null;

$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,
];

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' );
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.' );
}
}