Skip to content

Commit

Permalink
Merge branch 'develop' into trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwilsoncc committed Jul 13, 2023
2 parents 6d9b679 + 0839663 commit efe228c
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 88 deletions.
89 changes: 45 additions & 44 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion CREDITS.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions distributor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin URI: https://github.com/10up/distributor
* Update URI: https://distributorplugin.com
* Description: Makes it easy to distribute and reuse content across your websites, whether inside of a multisite or across the web.
* Version: 2.0.0-beta1
* Version: 2.0.0-beta2
* Requires at least: 5.7
* Requires PHP: 7.4
* Author: 10up Inc.
Expand All @@ -28,7 +28,7 @@
exit; // Exit if accessed directly.
}

define( 'DT_VERSION', '2.0.0-beta1' );
define( 'DT_VERSION', '2.0.0-beta2' );
define( 'DT_PLUGIN_FILE', preg_replace( '#^.*plugins/(.*)$#i', '$1', __FILE__ ) );
define( 'DT_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'DT_PLUGIN_FULL_FILE', __FILE__ );
Expand Down
93 changes: 56 additions & 37 deletions includes/classes/InternalConnections/NetworkSiteConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,28 @@ public function push( $post, $args = array() ) {
update_post_meta( $new_post_id, 'dt_syndicate_time', absint( time() ) );

/**
* Allow bypassing of all meta processing.
* Allow bypassing of all media processing.
*
* @hook dt_push_post_meta
* @hook dt_push_post_media
*
* @param {bool} true If Distributor should push the post meta.
* @param {bool} true If Distributor should push the post media.
* @param {int} $new_post_id The newly created post ID.
* @param {array} $post_meta Meta attached to the post, formatted by {@link \Distributor\Utils\prepare_meta()}.
* @param {array} $post_media List of media items attached to the post, formatted by {@link \Distributor\Utils\prepare_media()}.
* @param {int} $post_id The original post ID.
* @param {array} $args The arguments passed into wp_insert_post.
* @param {Connection} $this The distributor connection being pushed to.
*
* @return {bool} If Distributor should push the post meta.
* @return {bool} If Distributor should push the post media.
*/
if ( apply_filters( 'dt_push_post_meta', true, $new_post_id, $post_meta, $post_id, $args, $this ) ) {
Utils\set_meta( $new_post_id, $post_meta );
if ( apply_filters( 'dt_push_post_media', true, $new_post_id, $post_media, $post_id, $args, $this ) ) {
Utils\set_media( $new_post_id, $post_media, [ 'use_filesystem' => true ] );
};

$media_errors = get_transient( 'dt_media_errors_' . $new_post_id );

if ( $media_errors ) {
$output['push-errors'] = $media_errors;
delete_transient( 'dt_media_errors_' . $new_post_id );
}

/**
Expand All @@ -201,28 +208,22 @@ public function push( $post, $args = array() ) {
}

/**
* Allow bypassing of all media processing.
* Allow bypassing of all meta processing.
*
* @hook dt_push_post_media
* @hook dt_push_post_meta
*
* @param {bool} true If Distributor should push the post media.
* @param {bool} true If Distributor should push the post meta.
* @param {int} $new_post_id The newly created post ID.
* @param {array} $post_media List of media items attached to the post, formatted by {@link \Distributor\Utils\prepare_media()}.
* @param {array} $post_meta Meta attached to the post, formatted by {@link \Distributor\Utils\prepare_meta()}.
* @param {int} $post_id The original post ID.
* @param {array} $args The arguments passed into wp_insert_post.
* @param {Connection} $this The distributor connection being pushed to.
*
* @return {bool} If Distributor should push the post media.
* @return {bool} If Distributor should push the post meta.
*/
if ( apply_filters( 'dt_push_post_media', true, $new_post_id, $post_media, $post_id, $args, $this ) ) {
Utils\set_media( $new_post_id, $post_media, [ 'use_filesystem' => true ] );
};

$media_errors = get_transient( 'dt_media_errors_' . $new_post_id );

if ( $media_errors ) {
$output['push-errors'] = $media_errors;
delete_transient( 'dt_media_errors_' . $new_post_id );
if ( apply_filters( 'dt_push_post_meta', true, $new_post_id, $post_meta, $post_id, $args, $this ) ) {
$post_meta = $this->exclude_additional_meta_data( $post_meta );
Utils\set_meta( $new_post_id, $post_meta );
}

/** This filter is documented in includes/classes/InternalConnections/NetworkSiteConnection.php */
Expand Down Expand Up @@ -306,22 +307,22 @@ public function pull( $items ) {
update_post_meta( $new_post_id, 'dt_original_post_url', wp_slash( sanitize_url( $new_post_args['meta_input']['dt_original_post_url'] ) ) );

/**
* Allow bypassing of all meta processing.
* Allow bypassing of all media processing.
*
* @hook dt_pull_post_meta
* @hook dt_pull_post_media
*
* @param {bool} true If Distributor should set the post meta.
* @param {bool} true If Distributor should set the post media.
* @param {int} $new_post_id The newly created post ID.
* @param {array} $post_meta List of meta items attached to the post, formatted by {@link \Distributor\Utils\prepare_meta()}.
* @param {array} $post_media List of media items attached to the post, formatted by {@link \Distributor\Utils\prepare_media()}.
* @param {int} $remote_post_id The original post ID.
* @param {array} $post_array The arguments passed into wp_insert_post.
* @param {NetworkSiteConnection} $this The Distributor connection being pulled from.
*
* @return {bool} If Distributor should set the post meta.
* @return {bool} If Distributor should set the post media.
*/
if ( apply_filters( 'dt_pull_post_meta', true, $new_post_id, $post['meta'], $item_array['remote_post_id'], $post_array, $this ) ) {
\Distributor\Utils\set_meta( $new_post_id, $post['meta'] );
}
if ( apply_filters( 'dt_pull_post_media', true, $new_post_id, $post['media'], $item_array['remote_post_id'], $post_array, $this ) ) {
\Distributor\Utils\set_media( $new_post_id, $post['media'], [ 'use_filesystem' => true ] );
};

/**
* Allow bypassing of all terms processing.
Expand All @@ -342,22 +343,23 @@ public function pull( $items ) {
}

/**
* Allow bypassing of all media processing.
* Allow bypassing of all meta processing.
*
* @hook dt_pull_post_media
* @hook dt_pull_post_meta
*
* @param {bool} true If Distributor should set the post media.
* @param {bool} true If Distributor should set the post meta.
* @param {int} $new_post_id The newly created post ID.
* @param {array} $post_media List of media items attached to the post, formatted by {@link \Distributor\Utils\prepare_media()}.
* @param {array} $post_meta List of meta items attached to the post, formatted by {@link \Distributor\Utils\prepare_meta()}.
* @param {int} $remote_post_id The original post ID.
* @param {array} $post_array The arguments passed into wp_insert_post.
* @param {NetworkSiteConnection} $this The Distributor connection being pulled from.
*
* @return {bool} If Distributor should set the post media.
* @return {bool} If Distributor should set the post meta.
*/
if ( apply_filters( 'dt_pull_post_media', true, $new_post_id, $post['media'], $item_array['remote_post_id'], $post_array, $this ) ) {
\Distributor\Utils\set_media( $new_post_id, $post['media'], [ 'use_filesystem' => true ] );
};
if ( apply_filters( 'dt_pull_post_meta', true, $new_post_id, $post['meta'], $item_array['remote_post_id'], $post_array, $this ) ) {
$post_meta = $this->exclude_additional_meta_data( $post['meta'] );
\Distributor\Utils\set_meta( $new_post_id, $post_meta );
}
}

switch_to_blog( $this->site->blog_id );
Expand Down Expand Up @@ -1106,4 +1108,21 @@ public function update_content_via_rest( $new_post_id ) {
);
}
}

/**
* Exclude additional meta data for network distributions
*
* In network connections the featured image is set prior to the meta data.
* Excluding the `_thumbnail_id` meta from distribution prevents the meta
* data from referring to the attachment ID of the original site.
*
* @since 2.0.0
*
* @param string[] $post_meta Array of meta to include in the distribution.
* @return string[] Array of meta to include in the distribution after filtering out excluded meta.
*/
public static function exclude_additional_meta_data( $post_meta ) {
unset( $post_meta['_thumbnail_id'] );
return $post_meta;
}
}
54 changes: 54 additions & 0 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ function is_dt_debug() {
* @param array $meta Array of meta as key => value
*/
function set_meta( $post_id, $meta ) {
/**
* Fires before Distributor sets post meta.
*
* All sent meta is included in the `$meta` array, including excluded keys.
* Any excluded keys returned in this filter will be subsequently removed
* from the saved meta data.
*
* @since 2.0.0
* @hook dt_before_set_meta
*
* @param {array} $meta All received meta for the post
* @param {int} $post_id Post ID
*/
$meta = apply_filters( 'dt_before_set_meta', $meta, $post_id );

$existing_meta = get_post_meta( $post_id );
$excluded_meta = excluded_meta();

Expand Down Expand Up @@ -448,6 +463,23 @@ function prepare_meta( $post_id ) {
}
}

/**
* Filter prepared meta for consumption.
*
* Modify meta data before it is sent for consumption by a distributed
* post. The prepared meta data should not include any excluded meta.
* see `excluded_meta()`.
*
* @since 2.0.0
* @hook dt_prepared_meta
*
* @param {array} $prepared_meta Prepared meta.
* @param {int} $post_id Post ID.
*
* @return {array} Prepared meta.
*/
$prepared_meta = apply_filters( 'dt_prepared_meta', $prepared_meta, $post_id );

return $prepared_meta;
}

Expand Down Expand Up @@ -510,6 +542,23 @@ function prepare_taxonomy_terms( $post_id, $args = array() ) {
$taxonomy_terms[ $taxonomy ] = wp_get_object_terms( $post_id, $taxonomy );
}

/**
* Filters the taxonomy terms for consumption.
*
* Modify taxonomies and terms prior to distribution. The array should be
* keyed by taxonomy. The returned data by filters should only return
* taxonomies permitted for distribution. See the `dt_syncable_taxonomies` hook.
*
* @since 2.0.0
* @hook dt_prepared_taxonomy_terms
*
* @param {array} $taxonomy_terms Associative array of terms keyed by taxonomy.
* @param {int} $post_id Post ID.
*
* @param {array} $args Modified array of terms keyed by taxonomy.
*/
$taxonomy_terms = apply_filters( 'dt_prepared_taxonomy_terms', $taxonomy_terms, $post_id );

return $taxonomy_terms;
}

Expand Down Expand Up @@ -924,6 +973,11 @@ function process_media( $url, $post_id, $args = [] ) {
// Default for external or if a local file copy failed.
if ( $download_url ) {

// Set the scheme to http: if a relative URL is specified.
if ( str_starts_with( $url, '//' ) ) {
$url = 'http:' . $url;
}

// Allows to pull media from local IP addresses
// Uses a "magic number" for priority so we only unhook our call, just in case.
add_filter( 'http_request_host_is_external', '__return_true', 88 );
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "distributor",
"version": "2.0.0-beta.1",
"version": "2.0.0-beta.2",
"description": "Distributor is a WordPress plugin that makes it easy to distribute and reuse content across your websites — whether in a single multisite or across the web.",
"author": {
"name": "10up",
Expand Down
5 changes: 5 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ There are two connection types: `internal` and `external`.
* External connections are external websites, connected by the JSON REST API. External connections can be added in the WordPress admin dashboard under `Distributor` > `External Connections`. Administrators can decide which user roles are allowed to distribute content to and from that connection (Editors and Administrators by default). All users with those roles will inherit the permissions of the user account used to establish the remote connection.
* External connections require HTTP Basic Authentication or [WordPress.com OAuth2](https://developer.wordpress.com/docs/oauth2/) (must be on VIP) be set up on the remote website. For Basic Auth, we recommend the [Application Passwords](https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/#Getting-Credentials) feature built in to WordPress.
* For external connections, Distributor needs to be installed on BOTH sides of the connection.

== Upgrade Notice ==

= 2.0.0 =
**Distributor now requires PHP 7.4 or later and WordPress 5.7 or later.**
2 changes: 1 addition & 1 deletion tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
WP_Mock::bootstrap();

define( 'DT_PLUGIN_PATH', dirname( __DIR__, 2 ) );
define( 'DT_VERSION', '2.0.0-beta1' );
define( 'DT_VERSION', '2.0.0-beta2' );

require_once __DIR__ . '/includes/common.php';
require_once __DIR__ . '/includes/TestCase.php';
Expand Down

0 comments on commit efe228c

Please sign in to comment.