Skip to content

Commit

Permalink
improve code and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
LC43 committed Aug 18, 2018
1 parent fc7bcab commit e9b3c63
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions multisite-global-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function enqueue_scripts()
'0.1',
true
);

wp_enqueue_script('global_media');
}

Expand Down Expand Up @@ -147,7 +148,8 @@ function ajax_query_attachments()
$query = isset($_REQUEST['query']) ? (array)$_REQUEST['query'] : array();

if (!empty($query['global_media'])) {
switch_to_blog(get_site_id());

switch_to_blog( get_site_id() );

add_filter('wp_prepare_attachment_for_js', __NAMESPACE__ . '\prepare_attachment_for_js');
}
Expand Down Expand Up @@ -257,15 +259,16 @@ function save_thumbnail_meta($post_id) {
*
* @since 4.6.0
*/
function ajax_get_post_thumbnail_html()
function ajax_get_post_thumbnail_html( $post_id, $thumbnail_id )
{
$id_prefix = get_site_id() . '00000';

if (false !== strpos($thumbnail_id, $id_prefix)) {
$thumbnail_id = str_replace($id_prefix, '', $thumbnail_id); // Unique ID, must be a number.

switch_to_blog(get_site_id());
$return = _wp_post_thumbnail_html( $thumbnail_id, $post_id );

$return = _wp_post_thumbnail_html( $thumbnail_id, $post_ID );
restore_current_blog();

$post = get_post( $post_ID );
Expand All @@ -277,6 +280,7 @@ function ajax_get_post_thumbnail_html()

}
else {

$return = _wp_post_thumbnail_html( $thumbnail_id, $post_ID );
}

Expand All @@ -302,30 +306,43 @@ function admin_post_thumbnail_html ( $content, $post_id, $thumbnail_id ) {

$id_prefix = get_site_id() . '00000';

if (false !== strpos($thumbnail_id, $id_prefix)) {
$thumbnail_id = str_replace($id_prefix, '', $thumbnail_id); // Unique ID, must be a number.
if ( false === strpos( $thumbnail_id, $id_prefix ) ) {
return $content;
}

switch_to_blog($site_id);
$content = _wp_post_thumbnail_html( $thumbnail_id, $thumbnail_id ); //$thumbnail_id is passed instead of post_id to avoid warning messages of nonexistent post object.
restore_current_blog();
$thumbnail_id = str_replace( $id_prefix, '', $thumbnail_id ); // Unique ID, must be a number.

$search = 'value="'.$thumbnail_id.'"';
$replace = 'value="'.$id_prefix.$thumbnail_id.'"';
$content = str_replace($search, $replace, $content);
switch_to_blog( $site_id );

$post = get_post( $post_id );
$post_type_object = get_post_type_object( $post->post_type );
// $thumbnail_id is passed instead of post_id to avoid warning messages of nonexistent post object.
$content = _wp_post_thumbnail_html( $thumbnail_id, $post_id );

$search = '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail"></a></p>';
$replace = '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
$content = str_replace($search, $replace, $content);
restore_current_blog();

$search = 'value="' . $thumbnail_id . '"';
$replace = 'value="' . $id_prefix . $thumbnail_id . '"';
$content = str_replace( $search, $replace, $content );

$post = get_post( $post_id );
$post_type_object = null;

$remove_image_label = _x( 'Remove featured image', 'post' );
if ( $post !== null ) {
$post_type_object = get_post_type_object( $post->post_type );
}
if ( $post_type_object !== null ) {
$remove_image_label = $post_type_object->labels->remove_featured_image;
}

$search = '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail"></a></p>';
$replace = '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $remove_image_label ) . '</a></p>';
$content = str_replace( $search, $replace, $content );

return $content;

}

add_filter( 'post_thumbnail_html', __NAMESPACE__ . '\post_thumbnail_html', 99, 5);

/**
* Filters the post thumbnail HTML.
*
Expand Down

0 comments on commit e9b3c63

Please sign in to comment.