Skip to content

Commit

Permalink
Merge pull request #26 from LC43/fix/missing-site-id
Browse files Browse the repository at this point in the history
Fix missing site id, warnings and sets new default site id
  • Loading branch information
bueltge authored Aug 20, 2018
2 parents d511288 + 22f98be commit e5cfb81
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
67 changes: 44 additions & 23 deletions multisite-global-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down 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 @@ -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() );
}

}
Expand All @@ -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 @@ -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 = '<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
10 changes: 6 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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) &middot; Engineering the web since 2006.
Expand Down

0 comments on commit e5cfb81

Please sign in to comment.