Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created January 4, 2012 00:24
Show Gist options
  • Save trepmal/1557750 to your computer and use it in GitHub Desktop.
Save trepmal/1557750 to your computer and use it in GitHub Desktop.
WordPress: Network-Wide Tips
<?php
/*
Plugin Name: Network-Wide Tips
Description: Save multiple notices, only activate one at a time. Displayed across WP Network.
Author: Kailey Lampert
Author: http://kaileylampert.com
Version: 1.0
*/
new Network_Tips();
class Network_Tips {
var $active_tip_default = array( 'id' => -1, 'content' => '' );
function __construct() {
add_action( 'init', array( &$this, 'register_tips_post_type' ) );
add_action( 'admin_notices', array( &$this, 'tips_output' ) );
add_action( 'network_admin_menu', array( &$this, 'tips_plug_pages' ) );
}
function register_tips_post_type() {
/* we don't need to do much here
* it will have a custom mgmt page
* and will never appear on the front-end
*/
register_post_type( 'tip', array(
'label' => 'Tips',
) );
}
function tips_output() {
/* display the tip
* it is saved to a site option
* which is available across the network
*/
$tip = get_site_option( 'active_tip', $this->active_tip_default );
if ( $tip['id'] > 0 )
echo "<div id='message' class='updated'><p>{$tip['content']}</p></div>";
}
function tips_plug_pages() {
/* setup page in network admin (see hook)
*/
add_submenu_page( 'settings.php', 'Tips', 'Tips', 'unfiltered_html', 'manage-tips', array( &$this, 'tips_manage_output' ) );
}
function tips_manage_output() {
/* our custom mgmt page
* add, edit, delete, and activate tips
*/
global $wpdb, $wp_roles, $current_site;
/* first, the 'saving' actions
*/
$active_tip = get_site_option('active_tip');
if ( isset( $_POST['edit_tip_id'] ) ) {
$id = $_POST['edit_tip_id'];
$tip_content = $_POST['new_tip'];
//editing tip
if ( $id > 0 ) {
//update the original post
wp_update_post( array( 'ID' => $id, 'post_content' => $tip_content ) );
echo '<div id="message" class="updated fade"><p>Tip edited.</p></div>';
//if the active tip, update site option
if ( $id == $active_tip['id'] ) {
update_site_option('active_tip', array( 'id' => $active_tip['id'], 'content' => $tip_content ) );
}
}
//adding tip
else if ( ! empty( $tip_content ) ) {
if ( wp_insert_post(
array('post_type' => 'tip', 'post_content' => $tip_content, 'post_status' => 'publish' )
) )
echo '<div id="message" class="updated fade"><p>New tip added.</p></div>';
}
}
/* if not add/editing
* handle update of active tip, and deletions
*/
if ( isset( $_POST['tips_'] ) ) {
//update 'active' tip
$active = $_POST['tips_']['act'];
$content = $active > 0 ? get_post($active)->post_content : '';
update_site_option('active_tip', array('id' => $active, 'content' => $content ) );
//delete checked tips. do this after 'active' update, in case the active tip is being deleted, it can be reset
if (isset($_POST['tips_']['del'])) {
$del = $_POST['tips_']['del'];
foreach( $del as $k => $id ) {
//if deleting the active tip, be sure to remove it from the site options
if ( $id == $active_tip['id'] )
update_site_option('active_tip', $this->active_tip_default );
wp_delete_post( $id, true );
}
}
}
/* this is where the page gets started
*/
echo '<div class="wrap">';
echo '<h2>Network-wide Notices</h2>';
echo '<form method="post">';
/* setup 'add new' options
*/
$opts = array('content' => '', 'pid' => 0, 'button' => 'Add New Tip');
/* change a few things if we're
* editing instead of adding
*/
if ( isset( $_POST['edit_tip'] ) ) {
$tip = get_post($_POST['edit_tip']);
if ( $tip->post_type != 'tip' ) break;
$content = $tip->post_content;
$opts = array('content' => $content, 'pid' => $_POST['edit_tip'], 'button' => 'Edit Tip');
}
/* build the editor
*/
extract( $opts );
wp_editor( $content, 'new_tip', array(
'media_buttons' => false,
'textarea_rows' => 5,
) );
echo "<p>
<input type='hidden' name='edit_tip_id' value='{$pid}' />
<input type='submit' class='button-primary' value='{$button}' /></p>";
echo '</form>';
/* list all tips with actions
*/
?>
<form method="post">
<table class="wp-list-table widefat">
<thead>
<tr>
<th id="cb" scope="col" class="column-cb check-column"><input type="checkbox" /></th>
<th>Content</th>
<th>Active&nbsp;Tip</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col" class="column-cb check-column"><input type="checkbox" /></th>
<td>Content</td>
<th>Active&nbsp;Tip</th>
</tr>
</tfoot>
<?php
/* get the active tip
*/
$active = get_site_option('active_tip', $this->active_tip_default );
/* get all tips
*/
$tips = get_posts('post_type=tip&numberposts=-1');
/* first row holds the 'none' option for Active Tips
*/
echo "<tr><th></th><td><em>none</em></td><td><input type='radio' name='tips_[act]' value='-1' ". checked( $active['id'], -1, false)."/></td></tr>";
/* loops thru tips
*/
foreach( $tips as $tip ) {
$chk = checked( $active['id'], $tip->ID, false );
echo "<tr>
<th class='check-column'><span><input type='checkbox' name='tips_[del][]' value='$tip->ID' /></span></th>
<td>$tip->post_content
<div class='row-actions'><button name='edit_tip' value='{$tip->ID}'>edit</button></div>
</td>
<td><span><input type='radio' name='tips_[act]' value='$tip->ID' {$chk}/></span></td>
</tr>";
}
echo '</table>';
echo '<p><input type="submit" class="button-primary" value="Update" /></p>';
echo '</form>';
echo '</div>';
}
}//end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment