Skip to content

Commit

Permalink
Committing built version of efe228c
Browse files Browse the repository at this point in the history
  • Loading branch information
10upbot on GitHub committed Jul 13, 2023
1 parent c16e954 commit 4ceb996
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 183 deletions.
89 changes: 45 additions & 44 deletions CHANGELOG.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
Loading

0 comments on commit 4ceb996

Please sign in to comment.