-
Notifications
You must be signed in to change notification settings - Fork 7
feat(content-distribution): add CLI command for distribute post #159
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
Merged
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
9997fd5
feat: content distribution class
miguelpeixe 7a5153d
feat: insert linked post
miguelpeixe 4230692
refactor: use site url instead of ID
miguelpeixe 0561d77
chore: lint
miguelpeixe e1aee3f
feat: return errors on post insert
miguelpeixe 6d5e931
chore: lint
miguelpeixe 34dcbf3
feat: support webhooks request priority
miguelpeixe 5e47bbe
feat: unlink functionality and unit tests
miguelpeixe ca10658
chore: lint
miguelpeixe 136ed6a
feat: introduce 'linked_post_inserted' hook and listener
miguelpeixe 7738de3
chore: lint
miguelpeixe 6095800
fix: listener hook name
miguelpeixe 699228a
fix: typo
miguelpeixe 0bf9c37
chore: better docblocks
miguelpeixe 20d5dc0
chore: remove redundant code
miguelpeixe fb501d4
test: persist post hash
miguelpeixe 548a571
test: remove unnecessary assertion
miguelpeixe b0c0a0d
refactor: distribute_post() to use handle_post_updated() method
miguelpeixe 2d3f9ca
refactor: post hash is now network post id
miguelpeixe 78c7f01
chore: update comment
miguelpeixe 491bf9f
chore: Add CLI command for distribute
naxoc 4f2641a
feat: content distribution class (#155)
miguelpeixe a397989
Merge branch 'epic/content-distribution' into feat/distribute-post-cli
naxoc cd4339d
feat(content-distribution): prevent older content updating linked pos…
miguelpeixe f2de08c
feat(content-distribution): handle post thumbnail (#157)
miguelpeixe db14c66
Add network util class
naxoc 49936b4
Almost done except for a few todos
naxoc 31d6744
refactor: OOP for distributed and linked posts (#160)
miguelpeixe 568a1f2
Merge branch 'epic/content-distribution' into feat/distribute-post-cli
naxoc cf2a369
Merge branch 'epic/content-distribution' into feat/network-validate-urls
naxoc 8e222e5
chore: Require php 8.1
naxoc 36a8807
Merge pull request #161 from Automattic/feat/network-validate-urls
naxoc 0724460
Merge branch 'epic/content-distribution' into feat/distribute-post-cli
naxoc 43634ee
chore: Update to use util class
naxoc 00be82b
fix: Include correct classname
naxoc cbd1704
chore: Add validation of outgoing post
naxoc 7352967
chore: phpcs
naxoc 0e3bec4
Merge pull request #164 from Automattic/fix/content-distribution-clas…
naxoc 20b6996
Merge branch 'epic/content-distribution' into feat/distribute-post-cli
naxoc 47bd6b6
fix(content-distribution): debug post update and remove deprecated co…
miguelpeixe 9722f56
Merge branch 'epic/content-distribution' into feat/distribute-post-cli
naxoc aa12aea
fix: Don't return payload on all posts
naxoc 745754f
fix: post handling
naxoc c64f15b
chore: move try-catch
naxoc b0c5f5d
chore: Move check for networked urls
naxoc 6e33778
Move more
naxoc 8427f77
Merge branch 'trunk' into feat/distribute-post-cli
naxoc 7146907
Move things again
naxoc 587e668
chore: Add a mock networked node
naxoc aeb190b
chore: add one more node to the mock nodes
naxoc 17c4359
Add a test
naxoc d0b0743
Merge branch 'trunk' into feat/distribute-post-cli
naxoc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php | ||
| /** | ||
| * Network Content Distribution commands. | ||
| * | ||
| * @package Newspack | ||
| */ | ||
|
|
||
| namespace Newspack_Network\Content_Distribution; | ||
|
|
||
| use Newspack_Network\Content_Distribution; | ||
| use Newspack_Network\Utils\Network; | ||
| use WP_CLI; | ||
| use WP_CLI\ExitException; | ||
|
|
||
| /** | ||
| * Class Distribution. | ||
| */ | ||
| class CLI { | ||
| /** | ||
| * Initialize this class and register hooks | ||
| * | ||
| * @return void | ||
| */ | ||
| public static function init(): void { | ||
| if ( defined( 'WP_CLI' ) && WP_CLI ) { | ||
| add_action( 'init', [ __CLASS__, 'register_commands' ] ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Callback to register the WP-CLI commands. | ||
| * | ||
| * @return void | ||
| * @throws \Exception If something goes wrong. | ||
| */ | ||
| public static function register_commands(): void { | ||
| WP_CLI::add_command( | ||
| 'newspack network distribute post', | ||
| [ __CLASS__, 'cmd_distribute_post' ], | ||
| [ | ||
| 'shortdesc' => __( 'Distribute a post to all the network or the specified sites' ), | ||
| 'synopsis' => [ | ||
| [ | ||
| 'type' => 'positional', | ||
| 'name' => 'post-id', | ||
| 'description' => sprintf( | ||
| 'The ID of the post to distribute. Supported post types are: %s', | ||
| implode( | ||
| ', ', | ||
| Content_Distribution::get_distributed_post_types() | ||
| ) | ||
| ), | ||
| 'optional' => false, | ||
| 'repeating' => false, | ||
| ], | ||
| [ | ||
| 'type' => 'assoc', | ||
| 'name' => 'sites', | ||
| 'description' => __( "Networked site url(s) comma separated to distribute the post to – or 'all' to distribute to all sites in the network." ), | ||
| 'optional' => false, | ||
| ], | ||
| ], | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Callback for the `newspack-network distribute post` command. | ||
| * | ||
| * @param array $pos_args Positional arguments. | ||
| * @param array $assoc_args Associative arguments. | ||
| * | ||
| * @throws ExitException If something goes wrong. | ||
| */ | ||
| public function cmd_distribute_post( array $pos_args, array $assoc_args ): void { | ||
| $post_id = $pos_args[0]; | ||
| if ( ! is_numeric( $post_id ) ) { | ||
| WP_CLI::error( 'Post ID must be a number.' ); | ||
| } | ||
|
|
||
| if ( 'all' === $assoc_args['sites'] ) { | ||
| $sites = Network::get_networked_urls(); | ||
| } else { | ||
| $sites = array_map( | ||
| fn( $site ) => untrailingslashit( trim( $site ) ), | ||
| explode( ',', $assoc_args['sites'] ) | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| $outgoing_post = Content_Distribution::get_distributed_post( $post_id ) ?? new Outgoing_Post( $post_id ); | ||
| $config = $outgoing_post->set_config( $sites ); | ||
| if ( is_wp_error( $config ) ) { | ||
| WP_CLI::error( $config->get_error_message() ); | ||
| } | ||
|
|
||
| Content_Distribution::distribute_post( $outgoing_post ); | ||
| WP_CLI::success( sprintf( 'Post with ID %d is distributed to %d sites: %s', $post_id, count( $config['site_urls'] ), implode( ', ', $config['site_urls'] ) ) ); | ||
|
|
||
| } catch ( \Exception $e ) { | ||
| WP_CLI::error( $e->getMessage() ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,9 @@ | |
| namespace Newspack_Network\Content_Distribution; | ||
|
|
||
| use Newspack_Network\Content_Distribution; | ||
| use WP_Post; | ||
| use Newspack_Network\Utils\Network; | ||
| use WP_Error; | ||
| use WP_Post; | ||
|
|
||
| /** | ||
| * Outgoing Post Class. | ||
|
|
@@ -40,6 +41,11 @@ public function __construct( $post ) { | |
| throw new \InvalidArgumentException( esc_html( __( 'Invalid post.', 'newspack-network' ) ) ); | ||
| } | ||
|
|
||
| if ( ! in_array( $post->post_type, Content_Distribution::get_distributed_post_types() ) ) { | ||
| /* translators: unsupported post type for content distribution */ | ||
| throw new \InvalidArgumentException( esc_html( sprintf( __( 'Post type %s is not supported as a distributed outgoing post.', 'newspack-network' ), $post->post_type ) ) ); | ||
| } | ||
|
|
||
| $this->post = $post; | ||
| } | ||
|
|
||
|
|
@@ -57,9 +63,27 @@ public function get_post() { | |
| * | ||
| * @param int[] $site_urls Array of site URLs to distribute the post to. | ||
| * | ||
| * @return void|WP_Error Void on success, WP_Error on failure. | ||
| * @return array|WP_Error Config array on success, WP_Error on failure. | ||
| */ | ||
| public function set_config( $site_urls = [] ) { | ||
| public function set_config( $site_urls ) { | ||
| if ( empty( $site_urls ) ) { | ||
| return new WP_Error( 'config_no_site_urls', __( 'No site URLs provided.', 'newspack-network' ) ); | ||
| } | ||
|
|
||
| $networked_urls = Network::get_networked_urls(); | ||
| $urls_not_in_network = array_diff( $site_urls, $networked_urls ); | ||
| if ( ! empty( $urls_not_in_network ) ) { | ||
| return new WP_Error( | ||
| 'config_non_networked_urls', | ||
| sprintf( | ||
| /* translators: %s: list of non-networked URLs */ | ||
| __( 'Non-networked URLs were passed to config: %s', 'newspack-network' ), | ||
| implode( ', ', $urls_not_in_network ) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| $config = get_post_meta( $this->post->ID, self::DISTRIBUTED_POST_META, true ); | ||
| if ( ! is_array( $config ) ) { | ||
| $config = []; | ||
|
|
@@ -68,8 +92,18 @@ public function set_config( $site_urls = [] ) { | |
| if ( empty( $config['network_post_id'] ) ) { | ||
| $config['network_post_id'] = md5( $this->post->ID . get_bloginfo( 'url' ) ); | ||
| } | ||
| $config['site_urls'] = $site_urls; | ||
|
|
||
| if ( empty( $config['site_urls'] ) ) { | ||
| $config['site_urls'] = []; | ||
| } | ||
|
|
||
| // If there are urls not already in the config, add them. Note that we don't support | ||
| // removing urls from the config. | ||
| $config['site_urls'] = array_unique( array_merge( $config['site_urls'], $site_urls ) ); | ||
|
Comment on lines
+100
to
+102
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we unit test this new thing?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep! I added a test 👍 |
||
|
|
||
| update_post_meta( $this->post->ID, self::DISTRIBUTED_POST_META, $config ); | ||
|
|
||
| return $config; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯