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

Change media handling to only process featured image by default #184

Merged
merged 14 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from 11 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
5 changes: 4 additions & 1 deletion assets/css/admin-settings.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.media-handling {
margin: 0;
}

.license-wrap.valid:after {
content: "\f147";
font-family: dashicons;
Expand All @@ -9,7 +13,6 @@
color: #49da49;
}


.license-wrap.invalid:after {
content: "\f335";
font-family: dashicons;
Expand Down
2 changes: 1 addition & 1 deletion dist/css/admin-settings.min.css

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

2 changes: 1 addition & 1 deletion dist/css/admin-settings.min.css.map

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

Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,20 @@ public function pull( $items ) {
}

if ( ! empty( $post_array['media'] ) ) {
\Distributor\Utils\set_media( $new_post, $post_array['media'] );

/**
* Allow bypassing of all media processing.
*
* @param bool true If Distributor should set the post media.
* @param int $new_post The newly created post ID.
* @param array $post_array['media'] List of media items attached to the post, formatted by {@see \Distributor\Utils\prepare_media()}.
* @param int $item_array['remote_post_id'] The original post ID.
* @param array $post_array The arguments passed into wp_insert_post.
* @param ExternalConnection $this The distributor connection being pulled from.
*/
if ( apply_filters( 'dt_pull_post_media', true, $new_post, $post_array['media'], $item_array['remote_post_id'], $post_array, $this ) ) {
\Distributor\Utils\set_media( $new_post, $post_array['media'] );
}
}

/**
Expand Down
29 changes: 21 additions & 8 deletions includes/classes/InternalConnections/NetworkSiteConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ public function push( $post_id, $args = array() ) {
\Distributor\Utils\set_taxonomy_terms( $new_post_id, $terms );

/**
* Allow plugins to override the default {@see \Distributor\Utils\set_media()} function.
* Allow bypassing of all media processing.
*
* @param bool true If Distributor should set the post media.
* @param int $new_post_id The newly created post ID.
* @param array $media List of media items attached to the post, formatted by {@see \Distributor\Utils\prepare_media()}.
* @param int $post_id The original post ID.
* @param array $args The arguments passed into wp_insert_post.
* @param ExternalConnection $this The distributor connection being pushed to.
* @param bool true If Distributor should set the post media.
* @param int $new_post_id The newly created post ID.
* @param array $media List of media items attached to the post, formatted by {@see \Distributor\Utils\prepare_media()}.
* @param int $post_id The original post ID.
* @param array $args The arguments passed into wp_insert_post.
* @param NetworkSiteConnection $this The distributor connection being pushed to.
*/
if ( apply_filters( 'dt_push_post_media', true, $new_post_id, $media, $post_id, $args, $this ) ) {
\Distributor\Utils\set_media( $new_post_id, $media );
Expand Down Expand Up @@ -206,7 +206,20 @@ public function pull( $items ) {

\Distributor\Utils\set_meta( $new_post_id, $post->meta );
\Distributor\Utils\set_taxonomy_terms( $new_post_id, $post->terms );
\Distributor\Utils\set_media( $new_post_id, $post->media );

/**
* Allow bypassing of all media processing.
*
* @param bool true If Distributor should set the post media.
* @param int $new_post_id The newly created post ID.
* @param array $post->media List of media items attached to the post, formatted by {@see \Distributor\Utils\prepare_media()}.
* @param int $item_array['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.
*/
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 );
};
}

switch_to_blog( $this->site->blog_id );
Expand Down
36 changes: 36 additions & 0 deletions includes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ function setup_fields_sections() {

add_settings_field( 'override_author_byline', esc_html__( 'Override Author Byline', 'distributor' ), __NAMESPACE__ . '\override_author_byline_callback', 'distributor', 'dt-section-1' );

add_settings_field( 'media_handling', esc_html__( 'Media Handling', 'distributor' ), __NAMESPACE__ . '\media_handling_callback', 'distributor', 'dt-section-1' );

if ( false === DT_IS_NETWORK ) {
add_settings_field( 'registation_key', esc_html__( 'Registration Key', 'distributor' ), __NAMESPACE__ . '\license_key_callback', 'distributor', 'dt-section-1' );
}
Expand Down Expand Up @@ -217,6 +219,33 @@ function license_key_callback() {
<?php
}

/**
* Output media handling options.
*
* @since 1.2.2
*/
function media_handling_callback() {
$settings = Utils\get_settings();
?>

<ul class="media-handling">
<li>
<input <?php checked( $settings['media_handling'], 'featured' ); ?> type="radio" value="featured" name="dt_settings[media_handling]">
<span class="description">
helen marked this conversation as resolved.
Show resolved Hide resolved
<?php esc_html_e( 'Process the featured image only (default).', 'distributor' ); ?>
</span>
</li>
<li>
<input <?php checked( $settings['media_handling'], 'attached' ); ?> type="radio" value="attached" name="dt_settings[media_handling]">
<span class="description">
<?php esc_html_e( 'Process the featured image and any attached images.', 'distributor' ); ?>
</span>
</li>
</ul>

<?php
}

/**
* Register settings for options table
*
Expand All @@ -234,6 +263,7 @@ function register_settings() {
function admin_menu() {
add_submenu_page( 'distributor', esc_html__( 'Settings', 'distributor' ), esc_html__( 'Settings', 'distributor' ), 'manage_options', 'distributor-settings', __NAMESPACE__ . '\settings_screen' );
}

/**
* Output network setting menu option
*
Expand Down Expand Up @@ -360,6 +390,12 @@ function sanitize_settings( $settings ) {
$new_settings['override_author_byline'] = true;
}

if ( ! isset( $settings['media_handling'] ) || ! in_array( $settings['media_handling'], array( 'featured', 'attached' ), true ) ) {
$new_settings['media_handling'] = 'featured';
} else {
$new_settings['media_handling'] = sanitize_text_field( $settings['media_handling'] );
}

if ( isset( $settings['license_key'] ) ) {
$new_settings['license_key'] = sanitize_text_field( $settings['license_key'] );
}
Expand Down
10 changes: 10 additions & 0 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function is_using_gutenberg() {
function get_settings() {
$defaults = [
'override_author_byline' => true,
'media_handling' => 'featured',
'email' => '',
'license_key' => '',
'valid_license' => null,
Expand Down Expand Up @@ -379,6 +380,7 @@ function set_taxonomy_terms( $post_id, $taxonomy_terms ) {
* @since 1.0
*/
function set_media( $post_id, $media ) {
$settings = get_settings(); // phpcs:ignore
$current_media_posts = get_attached_media( get_allowed_mime_types(), $post_id );
$current_media = [];

Expand All @@ -390,6 +392,14 @@ function set_media( $post_id, $media ) {

$found_featured_image = false;

// If we only want to process the featured image, remove all other media
if ( 'featured' === $settings['media_handling'] ) {
$featured_keys = wp_list_pluck( $media, 'featured' );
$featured_key = array_search( true, $featured_keys, true );

$media = ( false !== $featured_key ) ? array( $media[ $featured_key ] ) : array();
}

foreach ( $media as $media_item ) {

// Delete duplicate if it exists (unless filter says otherwise)
Expand Down
21 changes: 21 additions & 0 deletions tests/php/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,19 @@ public function test_set_media() {
$attached_media_post->post_excerpt = 'excerpt';
$attached_media_post->post_mime_type = 'image/png';

\WP_Mock::userFunction(
'Distributor\Utils\get_settings', [
'times' => 1,
'return' => [
'override_author_byline' => true,
'media_handling' => 'featured',
'email' => '',
'license_key' => '',
'valid_license' => null,
],
]
);

\WP_Mock::userFunction(
'get_attached_media', [
'times' => 1,
Expand All @@ -470,6 +483,14 @@ public function test_set_media() {
]
);

\WP_Mock::userFunction(
'wp_list_pluck', [
'times' => 1,
'args' => [ [ $media_item ], 'featured' ],
'return' => [ 0 => true ],
]
);

\WP_Mock::userFunction(
'Distributor\Utils\process_media', [
'times' => 1,
Expand Down