Created
November 20, 2014 05:56
-
-
Save bappi-d-great/aa858d11dec838f0da80 to your computer and use it in GitHub Desktop.
Sharing site info across the network
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Share across network | |
Plugin URI: http://premium.wpmudev.org/ | |
Description: A very simple widget plugin to share info across the network | |
Version: 1.0.1 | |
Author: Ashok (Incsub) | |
Author URI: http://bappi-d-great.com | |
License: GNU General Public License (Version 2 - GPLv2) | |
Network: true | |
*/ | |
/* | |
* Translation is not ready | |
*/ | |
namespace shareinfo; | |
use \WP_Widget as WP_Widget; | |
if ( ! defined( 'ABSPATH' ) ) exit; | |
if( ! class_exists( 'share_info' ) ){ | |
class share_info{ | |
public function __construct() { | |
add_action( 'network_admin_menu', [ $this, 'site_info_page' ] ); | |
} | |
public function site_info_page() { | |
add_submenu_page( 'sites.php', 'Site Info', 'Site Info', 'manage_network', 'site-info', [ $this, 'site_info_page_cb' ] ); | |
} | |
public function site_info_page_cb() { | |
if( $_POST['share_info_submit'] ){ | |
if( ! is_super_admin() ){ | |
return; | |
} | |
if ( ! isset( $_POST['share_info_nonce_field'] ) || ! wp_verify_nonce( $_POST['share_info_nonce_field'], 'share_info_action' ) ) { | |
return; | |
} | |
if( update_site_option( 'share_site_info', $_POST['share_info'] ) ) | |
wp_redirect( admin_url( 'network/sites.php?page=site-info&msg=Data+Saved.' ) ); | |
} | |
$sites = wp_get_sites(); | |
$info = get_site_option( 'share_site_info' ); | |
?> | |
<div class="wrap"> | |
<h2>Add the data you want to share for all sites</h2> | |
<br> | |
<?php if( $_REQUEST['msg'] ) { ?> | |
<div id="message" class="updated below-h2"><p><?php echo str_replace( '+', ' ', $_REQUEST['msg'] ) ?></a></p></div> | |
<?php } ?> | |
<form action="<?php echo admin_url( 'network/sites.php?page=site-info&noheader=true' ) ?>" method="post"> | |
<?php wp_nonce_field('share_info_action','share_info_nonce_field'); ?> | |
<table class="widefat"> | |
<tr> | |
<th width="25%">Site name</th> | |
<th>Site info</th> | |
</tr> | |
<?php | |
$i = 0; | |
foreach( $sites as $site ) { | |
switch_to_blog( $site['blog_id'] ); ?> | |
<tr class="<?php echo $i++ % 2 == 0 ? 'alternate' : '' ?>"> | |
<td><?php echo get_bloginfo( 'name' ); ?></td> | |
<td> | |
<textarea style="width: 100%" rows="5" name="share_info[<?php echo $site['blog_id'] ?>]"><?php echo $info[$site['blog_id']] ?></textarea><br><em>HTML Allowed</em> | |
</td> | |
</tr> | |
<?php | |
restore_current_blog(); | |
} | |
?> | |
</table> | |
<br> | |
<input type="submit" name="share_info_submit" class="button button-primary" value="Save"> | |
</form> | |
</div> | |
<?php | |
} | |
} | |
new share_info(); | |
} | |
if( ! class_exists( 'Share_Info_Widget' ) ) { | |
class Share_Info_Widget extends WP_Widget{ | |
/** | |
* Sets up the widgets name etc | |
*/ | |
public function __construct() { | |
parent::__construct( | |
'share_info_widget', // Base ID | |
__( 'Share Static Content across Multisite' ), // Name | |
array( 'description' => __( 'A widget to share static content' ), ) // Args | |
); | |
} | |
/** | |
* Outputs the content of the widget | |
* | |
* @param array $args | |
* @param array $instance | |
*/ | |
public function widget( $args, $instance ) { | |
echo $args['before_widget']; | |
if ( ! empty( $instance['title'] ) ) { | |
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title']; | |
$sites = wp_get_sites(); | |
$info = get_site_option( 'share_site_info' ); | |
$id = get_current_blog_id(); | |
foreach( $sites as $site ){ | |
if( $site['blog_id'] == $id ) continue; | |
switch_to_blog( $site['blog_id'] ); | |
?> | |
<ul> | |
<li> | |
<h4><?php echo get_bloginfo( 'name' ); ?></h4> | |
<p><?php echo $info[$site['blog_id']] ?></p> | |
</li> | |
</ul> | |
<?php | |
restore_current_blog(); | |
} | |
} | |
echo $args['after_widget']; | |
} | |
/** | |
* Outputs the options form on admin | |
* | |
* @param array $instance The widget options | |
*/ | |
public function form( $instance ) { | |
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Site Information' ); | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"> | |
</p> | |
<?php | |
} | |
/** | |
* Processing widget options on save | |
* | |
* @param array $new_instance The new options | |
* @param array $old_instance The previous options | |
*/ | |
public function update( $new_instance, $old_instance ) { | |
$instance = array(); | |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; | |
return $instance; | |
} | |
} | |
add_action( 'widgets_init', function() { | |
register_widget( 'shareinfo\Share_Info_Widget' ); | |
} ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment