Skip to content

Instantly share code, notes, and snippets.

@thomasgriffin
Created August 29, 2012 13:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save thomasgriffin/3512335 to your computer and use it in GitHub Desktop.
Save thomasgriffin/3512335 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Soliloquy Demo
Plugin URI: http://soliloquywp.com/
Description: Creates demo user and manages roles so Soliloquy can have a live demo.
Author: Thomas Griffin
Author URI: http://thomasgriffinmedia.com/
Version: 1.0.0
License: GNU General Public License v2.0 or later
License URI: http://www.opensource.org/licenses/gpl-license.php
*/
/*
Copyright 2012 Thomas Griffin (email : thomas@thomasgriffinmedia.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Demo class for the Soliloquy for WordPress plugin.
*
* It is a final class so it cannot be extended.
*
* @since 1.0.0
*
* @package TGM_Soliloquy_Demo
* @author Thomas Griffin <thomas@thomasgriffinmedia.com>
*/
final class TGM_Soliloquy_Demo {
/**
* Holds user roles information.
*
* @since 1.0.0
*
* @var object
*/
private $role;
/**
* Holds user capabilities information.
*
* @since 1.0.0
*
* @var array
*/
private $caps;
/**
* The maximum number of sliders the demo user can create.
*
* @since 1.0.0
*
* @var int
*/
private $limit = 5;
/**
* Constructor. Loads the class.
*
* @since 1.0.0
*/
public function __construct() {
/** Load the class */
$this->load();
}
/**
* Hooks all interactions into WordPress to kickstart the class.
*
* @since 1.0.0
*/
private function load() {
/** Hook everything into plugins_loaded */
add_action( 'plugins_loaded', array( $this, 'init' ) );
}
/**
* In this method, we set any filters or actions and start modifying
* our user to have the correct permissions for demo usage.
*
* @since 1.0.0
*/
public function init() {
/** Don't process anything unless the current user is a demo user */
if ( $this->is_demo_user() ) {
/** Setup capabilities for our user */
$this->role = get_role( 'subscriber' );
$this->caps = array( 'edit_posts', 'edit_published_posts', 'delete_posts', 'delete_published_posts','publish_posts', 'upload_files' );
/** Add capabilities to the user */
foreach ( $this->caps as $cap )
if ( ! current_user_can( $cap ) )
$this->role->add_cap( $cap );
/** Add a post limit the user and hook a function to redirect if they have reached their limit */
$this->role->post_limit = $this->limit;
if ( $this->is_demo_area() )
add_action( 'load-post-new.php', array( $this, 'limit' ) );
/** Add some extra image sizes for testing */
add_image_size( 'demo-small', 500, 250, true );
add_image_size( 'demo-regular', 600, 300, true );
add_image_size( 'demo-large', 960, 300, true );
add_image_size( 'demo-larger', 960, 400, true );
/** Load hooks and filters */
add_action( 'wp_loaded', array( $this, 'cheatin' ) );
add_action( 'admin_init', array( $this, 'admin_init' ), 11 );
add_filter( 'login_redirect', array( $this, 'redirect' ) );
add_filter( 'login_message', array( $this, 'login_message' ) );
add_action( 'wp_dashboard_setup', array( $this, 'dashboard' ), 100 );
add_action( 'admin_menu', array( $this, 'remove_menu_items' ) );
add_action( 'admin_notices', array( $this, 'notices' ) );
add_action( 'untrashed_post', array( $this, 'trash' ) );
add_action( 'wp_before_admin_bar_render', array( $this, 'admin_bar' ) );
add_action( 'admin_footer', array( $this, 'jquery' ) );
add_filter( 'admin_footer_text', array( $this, 'footer' ) );
add_action( 'wp_footer', array( $this, 'jquery' ), 1000 );
}
}
/**
* Get all sliders that have been created, and if the limit has been met, output
* an error message and redirect the user.
*
* @since 1.0.0
*/
public function limit() {
/** If the limit has been reached, output a notice and redirect to the edit screen */
if ( count( $this->get_sliders() ) >= $this->limit ) {
wp_safe_redirect( add_query_arg( array( 'post_type' => 'soliloquy', 'limit' => true ), admin_url( 'edit.php' ) ) );
exit;
}
}
/**
* Make sure users don't try to access an admin page that they shouldn't.
*
* @since 1.0.0
*
* @global string $pagenow The current page slug
*/
public function cheatin() {
global $pagenow;
/** Paranoia security to make sure the demo user cannot access any page other than what we specify */
$not_allowed = array( 'about.php', 'credits.php', 'freedoms.php', 'update-core.php', 'edit-tags.php', 'upload.php', 'media-new.php', 'link-manager.php', 'link-add.php', 'edit-comments.php', 'themes.php', 'widgets.php', 'nav-menus.php', 'theme-editor.php', 'plugins.php', 'plugin-install.php', 'plugin-editor.php', 'users.php', 'user-new.php', 'profile.php', 'tools.php', 'import.php', 'export.php', 'options-general.php', 'options-writing.php', 'options-reading.php', 'options-discussion.php', 'options-media.php', 'options-privacy.php', 'options-permalink.php' );
/** If we find a user is trying to access a forbidden page, redirect them back to the dashboard */
if ( in_array( $pagenow, $not_allowed ) || 'edit.php' == $pagenow && ! ( $this->is_demo_area() ) || 'post-new.php' == $pagenow && ! ( $this->is_demo_area() ) ) {
wp_safe_redirect( get_admin_url() );
exit;
}
}
/**
* Remove the ability for users to mess with the screen options panel.
*
* @since 1.0.0
*/
public function admin_init() {
add_filter( 'screen_options_show_screen', '__return_false' );
}
/**
* Redirect the user to the Dashboard page upon logging in.
*
* @since 1.0.0
*
* @param string $redirect_to Default redirect URL (profile page)
* @return string $redirect_to Amended redirect URL (dashboard)
*/
public function redirect( $redirect_to ) {
return get_admin_url();
}
/**
* Customize the login message with the demo username and password.
*
* @since 1.0.0
*
* @param string $message The default login message
* @return string $message Amended login message
*/
public function login_message( $message ) {
$message = '<div style="font-size: 15px; margin-left: 8px; text-align: center;">';
$message .= '<p>In order to gain access to the Soliloquy demo area, use the login credentials below:</p><br />';
$message .= '<strong>Username: </strong> <span style="color: #cc0000;">demo</span><br />';
$message .= '<strong>Password: </strong> <span style="color: #cc0000;">demo</span><br /><br />';
$message .= '</div>';
return $message;
}
/**
* If the user is not an admin, set the dashboard screen to one column
* and remove the default dashbord widgets.
*
* @since 1.0.0
*
* @global string $pagenow The current page slug
*/
public function dashboard() {
global $pagenow;
$layout = get_user_option( 'screen_layout_dashboard', get_current_user_id() );
/** Set the screen layout to one column in the dashboard */
if ( 'index.php' == $pagenow && 1 !== $layout )
update_user_option( get_current_user_id(), 'screen_layout_dashboard', 1, true );
/** Remove dashboard widgets from view */
remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
remove_meta_box( 'dashboard_secondary', 'dashboard', 'side' );
}
/**
* Remove certain menu items from view so demo user cannot mess with them.
*
* @since 1.0.0
*
* @global array $menu Current array of menu items
*/
public function remove_menu_items() {
global $menu;
end( $menu );
/** Remove the first menu separator */
unset( $menu[4] );
/** Now remove the menu items we don't want our user to see */
$remove_menu_items = array( __( 'Posts' ), __( 'Media' ), __( 'Comments' ), __( 'Profile' ), __( 'Tools' ) );
while ( prev( $menu ) ) {
$item = explode( ' ', $menu[key( $menu )][0] );
if ( in_array( $item[0] != null ? $item[0] : '', $remove_menu_items ) )
unset( $menu[key( $menu )] );
}
}
/**
* Outputs error messages registered with add_settings_error().
*
* @since 1.0.0
*/
public function notices() {
/** Our limit has been reached if this query arg is set and is true */
if ( isset( $_GET['limit'] ) && $_GET['limit'] )
add_settings_error( 'soliloquy-demo', 'limit-reached', __( 'You have reached the maximum number of ' . $this->limit . ' sliders that can be created in the demo area. You must modify an existing slider or delete an existing slider to create a new one.' ), 'updated' );
/** The slider cannot be untrashed if we already have the maximum number of active sliders */
if ( isset( $_GET['remain'] ) && $_GET['remain'] )
add_settings_error( 'soliloquy-demo', 'cannot-restore', __( 'Slider could not be restored because the maximum number of ' . $this->limit . ' sliders are active. You must delete an active slider in order to restore this one.' ), 'updated' );
/** Output the nag */
settings_errors( 'soliloquy-demo' );
}
/**
* Get all sliders that have been created, and if the limit has been met, output
* an error message and redirect the user.
*
* @since 1.0.0
*/
public function trash( $post_id ) {
/** Can't untrash a post if the limit has been reached! */
if ( count( $this->get_sliders() ) >= $this->limit ) {
wp_trash_post( $post_id );
wp_safe_redirect( add_query_arg( array( 'post_type' => 'soliloquy', 'remain' => true ), admin_url( 'edit.php' ) ) );
exit;
}
}
/**
* Modify the admin bar to remove unnecessary links.
*
* @since 1.0.0
*
* @global object $wp_admin_bar The admin bar object
*/
public function admin_bar() {
global $wp_admin_bar;
/** Remove admin bar menu items that demo users don't need to see or access */
$wp_admin_bar->remove_menu( 'wp-logo' );
$wp_admin_bar->remove_menu( 'new-content' );
$wp_admin_bar->remove_menu( 'comments' );
$wp_admin_bar->remove_menu( 'user-info' );
$wp_admin_bar->remove_menu( 'edit-profile' );
}
/**
* We can't filter the Profile URL for the main account link in the admin bar, so we
* replace it using jQuery instead. We also remove the "+ New" item from the admin bar.
*
* This method also adds some extra text to spice up the currently empty dashboard area.
* Call it plugin marketing if you will. :-)
*
* @since 1.0.0
*/
public function jquery() {
?>
<script type="text/javascript">
jQuery(document).ready(function($){
/** Remove items from the admin bar first */
$('#wp-admin-bar-my-account a:first').attr('href', '<?php echo get_admin_url(); ?>');
$('#wp-admin-bar-view').remove();
/** Customize the Dashboard area */
$('.index-php #normal-sortables').fadeIn('fast', function(){
/** Change width of the container */
$(this).css({ 'height' : 'auto' });
/** Store HTML output in a variable */
var output = '';
/** Build the HTML */
output += '<div class="soliloquy-logo">Soliloquy - The Best Responsive WordPress Slider Plugin. Period.</div>';
output += '<p style="font-size: 15px; font-weight: bold; line-height: 22px;">Within this demo area, you have the ability to create, edit, publish and delete sliders, just like you would on a real install of Soliloquy. Just follow the simple instructions below to get started:</p>';
output += '<ol style="font-size: 13px">';
output += '<li>Navigate to Soliloquy > Add New to create a new slider.</li>';
output += '<li>Give your slider a title and then click on the "Click Here to Upload Images" button to begin uploading your images.</li>';
output += '<li>Play around with the settings, sort your images, add some image meta, and get a feel for how the plugin works.</li>';
output += '<li>Publish your slider and go to the home page to see it active.</li>';
output += '<li><a href="http://soliloquywp.com/pricing/" title="Purchase Soliloquy because it is simply the best! :-)" target="_blank">Purchase Soliloquy because it is simply the best! :-)</a></li>';
output += '</ol>';
output += '<p style="color: #cc0000; font-size: 15px; font-weight: bold; line-height: 22px; margin-bottom: 40px;">Since this is a demo, there is a limit of 5 slider instances at any given time, and all slider instances and associated data are wiped every 12 hours.</p>';
/** Output the HTML */
$(this).html(output);
});
});
</script>
<style type="text/css">.soliloquy-logo { background: url(<?php echo plugins_url( 'login-logo.png', __FILE__ ); ?>) no-repeat scroll 0 0; display: block; height: 64px; margin: 40px auto 30px; text-align: center; text-indent: -9999px; width: 312px; } @media only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (-o-min-device-pixel-ratio: 3/2), only screen and (min--moz-device-pixel-ratio: 1.5), only screen and (min-device-pixel-ratio: 1.5) { .soliloquy-logo { background-image: url(<?php echo plugins_url( 'login-logo@2x.png', __FILE__ ); ?>); background-size: 312px 64px; } }</style>
<?php
}
/**
* Modify the footer text for the demo area.
*
* @since 1.0.0
*
* @param string $text The default footer text
* @return string $text Amended footer text
*/
public function footer( $text ) {
return sprintf( __( 'You are currently enjoying a demo of Soliloquy. Want the real thing? <a href="%s" title="Click here to purchase a license!" target="_blank">Click here to purchase a license!</a>' ), 'http://soliloquywp.com/pricing/' );
}
/**
* Helper function for determining whether the current user is a demo user or not.
*
* @since 1.0.0
*
* @return bool Whether or not the user is a demo
*/
private function is_demo_user() {
return (bool) ! current_user_can( 'manage_options' );
}
/**
* Helper function for determining whether the current page is in the demo area.
*
* @since 1.0.0
*
* @return bool Demo area or not
*/
private function is_demo_area() {
global $current_screen;
if ( $current_screen )
if ( 'soliloquy' == $current_screen->post_type )
return true;
if ( isset( $_GET['post_type'] ) && 'soliloquy' == $_GET['post_type'] )
return true;
return false;
}
/**
* Helper function to get all non-trashed sliders.
*
* @since 1.0.0
*
* @return array $sliders An array of slider objects
*/
private function get_sliders() {
/** Get all created slider instances */
$args = array(
'post_type' => 'soliloquy',
'post_status' => null,
'posts_per_page' => -1
);
$sliders = get_posts( $args );
return $sliders;
}
}
/** Instantiate the class */
$tgm_soliloquy_demo = new TGM_Soliloquy_Demo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment