From 8bab9206afe765d005db218fb0be8454fb6add59 Mon Sep 17 00:00:00 2001 From: Pedro de Carvalho Date: Sat, 18 Aug 2018 05:12:24 +0100 Subject: [PATCH 1/6] check if key exists when saving post --- multisite-global-media.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/multisite-global-media.php b/multisite-global-media.php index c0f589a..89068bd 100644 --- a/multisite-global-media.php +++ b/multisite-global-media.php @@ -244,9 +244,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() ); } } From 16a2227a52db005cbc8ff181a65860b76deb43ec Mon Sep 17 00:00:00 2001 From: Pedro de Carvalho Date: Sat, 18 Aug 2018 05:15:10 +0100 Subject: [PATCH 2/6] Check that site id always exists --- multisite-global-media.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/multisite-global-media.php b/multisite-global-media.php index 89068bd..761628d 100644 --- a/multisite-global-media.php +++ b/multisite-global-media.php @@ -294,8 +294,12 @@ 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)) { From fc7bcab54200c9ab0039674d7d0c300ff1496363 Mon Sep 17 00:00:00 2001 From: Pedro de Carvalho Date: Sat, 18 Aug 2018 05:15:35 +0100 Subject: [PATCH 3/6] Use site ID 1 as the default --- multisite-global-media.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/multisite-global-media.php b/multisite-global-media.php index 761628d..ba1ae33 100644 --- a/multisite-global-media.php +++ b/multisite-global-media.php @@ -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. From e9b3c63b9846fbcf6c8d7b0db2aebab7937c67ed Mon Sep 17 00:00:00 2001 From: Pedro de Carvalho Date: Sat, 18 Aug 2018 05:31:34 +0100 Subject: [PATCH 4/6] improve code and clean up --- multisite-global-media.php | 51 +++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/multisite-global-media.php b/multisite-global-media.php index ba1ae33..2b3a092 100644 --- a/multisite-global-media.php +++ b/multisite-global-media.php @@ -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'); } @@ -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 ); } @@ -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 = '

'; - $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. * From 5e8fe0dcad54342adb71ad5fb72b0b76291adf56 Mon Sep 17 00:00:00 2001 From: Pedro de Carvalho Date: Sat, 18 Aug 2018 05:33:30 +0100 Subject: [PATCH 5/6] update version to 0.0.6 --- multisite-global-media.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/multisite-global-media.php b/multisite-global-media.php index 2b3a092..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 From 22f98be69f03f1fc351a5c2ec71bb0b98c40dae6 Mon Sep 17 00:00:00 2001 From: Pedro de Carvalho Date: Sat, 18 Aug 2018 05:36:00 +0100 Subject: [PATCH 6/6] Improve documentation --- readme.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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.