diff --git a/multisite-global-media.php b/multisite-global-media.php index c0f589a..2897538 100644 --- a/multisite-global-media.php +++ b/multisite-global-media.php @@ -5,7 +5,7 @@ * Description: Multisite Global Media is a WordPress plugin which shares media across the Multisite network. * Network: true * Plugin URI: https://github.com/bueltge/multisite-global-media - * Version: 0.0.5 + * Version: 0.0.6 * Author: Dominik Schilling, Frank Bültge * License: MIT * License URI: ./LICENSE @@ -37,7 +37,7 @@ * @var integer * @since 2015-01-22 */ -const SITE_ID = 3; +const SITE_ID = 1; /** * Return the ID of site that store the media files. @@ -71,6 +71,7 @@ function enqueue_scripts() '0.1', true ); + wp_enqueue_script('global_media'); } @@ -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'); } @@ -244,9 +246,9 @@ function ajax_get_attachment() function save_thumbnail_meta($post_id) { $id_prefix = get_site_id() . '00000'; - if ($_POST['_thumbnail_id'] && false !== strpos($_POST['_thumbnail_id'], $id_prefix)) { - update_post_meta($post_id, '_thumbnail_id', $_POST['_thumbnail_id']); - update_post_meta($post_id, 'global_media_site_id', get_site_id()); + if ( ! empty( $_POST['_thumbnail_id'] ) && false !== strpos( $_POST['_thumbnail_id'], $id_prefix ) ) { + update_post_meta( $post_id, '_thumbnail_id', intval( $_POST['_thumbnail_id'] ) ); + update_post_meta( $post_id, 'global_media_site_id', get_site_id() ); } } @@ -257,7 +259,7 @@ 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'; @@ -265,7 +267,8 @@ function ajax_get_post_thumbnail_html() $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 ); @@ -277,6 +280,7 @@ function ajax_get_post_thumbnail_html() } else { + $return = _wp_post_thumbnail_html( $thumbnail_id, $post_ID ); } @@ -294,34 +298,51 @@ function ajax_get_post_thumbnail_html() */ function admin_post_thumbnail_html ( $content, $post_id, $thumbnail_id ) { -// var_dump(get_post_meta( $post_id)); + $site_id = get_post_meta( $post_id, 'global_media_site_id', true ); + if ( empty( $site_id ) ) { + $site_id = get_site_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 = '

'; - $replace = '

' . esc_html( $post_type_object->labels->remove_featured_image ) . '

'; - $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 = '

'; + $replace = '

' . esc_html( $remove_image_label ) . '

'; + $content = str_replace( $search, $replace, $content ); + return $content; } - add_filter( 'post_thumbnail_html', __NAMESPACE__ . '\post_thumbnail_html', 99, 5); + /** * Filters the post thumbnail HTML. * diff --git a/readme.md b/readme.md index 98d4378..9cb3aa2 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ _Multisite Global Media_ is a WordPress plugin which shares media across the Multisite network. ## Description -This small plugin adds a new tab to the media modal which gives you the opportunity to share media from one site to all the other sites of the network. The `multisite-global-media.php` file uses the ID of the site that will store the global media. Currently the Site ID is set at `const SITE_ID = 3`. Change this value to set one of the other sites as the default for storing global media. You can also set/change this Site ID via filter hook `global_media.site_id`, like +This small plugin adds a new tab to the media modal which gives you the opportunity to share media from one site to all the other sites of the network. The `multisite-global-media.php` file uses the ID of the site that will store the global media. Currently the Site ID is set at `const SITE_ID = 1`. Change this value to set one of the other sites as the default for storing global media. You can also set/change this Site ID via filter hook `global_media.site_id`, like add_filter( 'global_media.site_id', function() { return 1234; @@ -23,13 +23,15 @@ To get Global Media to work one has to follow these steps: * @var integer * @since 2015-01-22 */ - const SITE_ID = 3; + const SITE_ID = 1; ``` Normally you should not change the source. It is much easier for maintenance and other points. So if you are familiar with code in the WordPress context, use the hook below to change the default Site ID of the plugin with a small custom plugin. #### Hook for Site ID -The plugin defines the hook `global_media.site_id` to set an ID for the network Site, that store the media files, like `add_filter( 'global_media.site_id', 1234 );`. +The plugin defines the hook `global_media.site_id` to set an ID for the network Site, that store the media files, like `add_filter( 'global_media.site_id', function() { + return 1234; + } );`. ### Installation * Download the plugin as zip, use a clone of the repo or use Composer, see below @@ -46,7 +48,7 @@ The plugin is also available as [Composer package](https://packagist.org/package ![Media Modal](./assets/screenshot-1.png) ![Usage in Featured Image](./assets/screenshot-2.png) - + ## Other Notes ### Crafted by [Inpsyde](https://inpsyde.com) · Engineering the web since 2006.