Skip to content

Instantly share code, notes, and snippets.

@fritids
Forked from cyberwani/publish-to-network.php
Created February 10, 2014 15:52
Show Gist options
  • Save fritids/8918318 to your computer and use it in GitHub Desktop.
Save fritids/8918318 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Publish to Network
* Plugin URI: https://gist.github.com/brasofilo/7036415
* Description: Meta box that allows publishing a post to another site of the network
* Version: 1.0
* Author: Rodolfo Buaiz
* Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
* Text Domain: sepw
* Domain Path: /languages
* License: GPLv2 or later
*/
/*
Publish to Network
Copyright (C) 2013 Rodolfo Buaiz
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
add_action(
'plugins_loaded',
array ( B5F_Publish_To_Network::get_instance(), 'plugin_setup' )
);
class B5F_Publish_To_Network
{
/**
* Plugin instance.
*
* @see get_instance()
* @type object
*/
protected static $instance = NULL;
/**
* URL to this plugin's directory.
*
* @type string
*/
public $plugin_url = '';
/**
* Path to this plugin's directory.
*
* @type string
*/
public $plugin_path = '';
/**
* Meta data name.
*
* @type string
*/
private $plugin_meta = '_network_published';
/**
* Constructor. Intentionally left empty and public.
*
* @see plugin_setup()
* @since 2012.09.12
*/
public function __construct() {}
/**
* Access this plugin’s working instance
*
* @wp-hook plugins_loaded
* @since 2012.09.13
* @return object of this class
*/
public static function get_instance()
{
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}
/**
* Used for regular plugin work.
*
* @wp-hook plugins_loaded
* @since 2012.09.10
* @return void
*/
public function plugin_setup()
{
$this->plugin_url = plugins_url( '/', __FILE__ );
$this->plugin_path = plugin_dir_path( __FILE__ );
$this->load_language( 'plugin_unique_name' );
if( is_multisite() )
{
add_action( 'save_post', array( $this, 'cross_publish' ), 10, 2 );
add_action( 'add_meta_boxes_post', array( $this, 'add_custom_box' ) );
} else
add_action( 'after_plugin_row_publish-to-network/publish-to-network.php', array( $this, 'plugin_row' ) );
}
/**
* Warning when activated on single sites
*/
public function plugin_row()
{
echo '<tr class="plugin-update-tr"><td colspan="3" class="plugin-update colspanchange"><div class="update-message" style="font-size: 1.3em; color:#f00">This plugin only works in Multisite! And does nothing in single sites.</div></td></tr>';
}
/**
* Add meta boxes.
*
* @wp-hook add_meta_boxes_post
* @since 2013.09.09
* @return void
*/
public function add_custom_box()
{
global $post;
$termid = get_post_meta( $post->ID, $this->plugin_meta, true );
if( empty( $termid ) || 'none' == $termid )
{
add_meta_box(
'sectionid',
__( 'Post to other site of the network' ),
array( $this, 'inner_custom_box' ),
'post',
'side',
'high'
);
}
elseif( 'donotshow' != $termid )
{
$details = get_blog_details( $termid['bid'] );
$site = sprintf(
'<a href="%s" target="_blank" style="font-size:1.2em">%s</a>',
$details->siteurl,
$details->blogname
);
add_meta_box(
'sectionid',
__( 'Posted to: ' ) . $site,
array( $this, 'inner_posted_box' ),
'post',
'side',
'high'
);
}
}
/**
* Meta box for publishing post
*
* @wp-hook add_meta_box
* @param object $post
* @since 2013.09.09
* @return void
*/
public function inner_custom_box( $post )
{
$blogs = $this->get_blogs();
echo '<select name="my_blog_id" id="my_blog_id">';
echo "<option value='0'>- Don't publish -</option>";
foreach ( $blogs as $site )
{
if( get_current_blog_id() != $site->blog_id )
{
$details = get_blog_details( $site->blog_id );
printf(
'<option value="%s" title="%s">%s</option>',
$site->blog_id,
$site->domain,
$details->blogname
);
}
}
echo '</select>
<div id="feedback" class="misc-pub-section"><small>Destination Site:</small><br><label for="my_blog_id">&nbsp;</label></div>';
echo '<label><input type="checkbox" name="disable_cross_publish" /> Remove this box</label>';
wp_nonce_field( plugin_basename( __FILE__ ), 'B5F_Publish_To_Network' );
$this->print_script();
}
/**
* Meta box for published posts
*
* @wp-hook add_meta_box
* @param object $post
* @since 2013.09.09
* @return void
*/
public function inner_posted_box( $post )
{
$termid = get_post_meta( $post->ID, $this->plugin_meta, true );
if( !empty( $termid ) )
{
$details = get_blog_details( $termid['bid'] );
switch_to_blog( $termid['bid'] );
$po = get_post( $termid['pid'] );
$perma = get_edit_post_link( $termid['pid'] );
restore_current_blog();
printf(
'<p><a href="%s" target="_blank">%s</a></p>',
$perma,
$po->post_title
);
}
}
/**
* jQuery for live Checkbox
*
* @since 2013.09.09
* @return void
*/
private function print_script()
{
echo <<<HTML
<script type="text/javascript">
jQuery(document).ready( function($)
{
$('#feedback label').html( $('#my_blog_id').children("option:selected").attr('title') );
$('#my_blog_id').live('change', function()
{
var site_domain = $(this).children("option:selected").attr('title');
var site_name = $(this).children("option:selected").text();
$('#feedback label').html(site_domain);
});
});
</script>
HTML;
}
/**
* Save post action.
*
* @wp-hook save_post
* @param integer $post_id
* @param object $post_object
* @since 2013.09.09
* @return void
*/
public function cross_publish( $post_id, $post_object )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;
if ( !wp_verify_nonce( $_POST['B5F_Publish_To_Network'], plugin_basename( __FILE__ ) ) )
return;
# Block auto-drafts and revisions
if( in_array( $post_object->post_status, array( 'auto-draft', 'inherit' ) ) )
return;
$termid = get_post_meta( $post_id, $this->plugin_meta, true );
if( isset( $_POST['disable_cross_publish'] ) )
{
update_post_meta( $post_id, $this->plugin_meta, 'donotshow' );
return;
}
# It's a new post
if( empty( $termid ) && isset( $_POST['my_blog_id'] ) )
{
if( '0' == $_POST['my_blog_id'] )
{
update_post_meta( $post_id, $this->plugin_meta, 'none' );
return;
}
remove_action( 'save_post', array( $this, 'cross_publish' ) );
switch_to_blog( $_POST['my_blog_id'] );
$my_post = array(
'post_title' => $post_object->post_title,
'post_content' => $post_object->post_content,
'post_status' => 'publish',
'post_author' => $post_object->post_author,
);
$new_post = wp_insert_post( $my_post );
restore_current_blog();
add_action( 'save_post', array( $this, 'cross_publish' ), 10, 2 );
update_post_meta(
$post_id,
$this->plugin_meta,
array(
'pid' => $new_post,
'bid' => $_POST['my_blog_id']
)
);
}
}
/**
* Get blogs list
*
* @return array List of blog ids
*/
private function get_blogs()
{
// get blog list from cache
$blogs = get_site_transient( 'multisite_blog_list' );
if ( FALSE === $blogs ) {
global $wpdb;
$blogs = $wpdb->get_results(
$wpdb->prepare( "
SELECT blog_id, domain
FROM $wpdb->blogs
WHERE site_id = %d
AND public = '1'
AND archived = '0'
AND mature = '0'
AND spam = '0'
AND deleted = '0'
ORDER BY registered ASC
", $wpdb->siteid ) );
// Set the Transient cache
set_site_transient( 'multisite_blog_list', $blogs, 60*60 );
}
return $blogs;
}
/**
* Loads translation file.
*
* Accessible to other classes to load different language files (admin and
* front-end for example).
*
* @wp-hook init
* @param string $domain
* @since 2012.09.11
* @return void
*/
public function load_language( $domain )
{
load_plugin_textdomain(
$domain,
FALSE,
$this->plugin_path . 'languages'
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment