Skip to content

Instantly share code, notes, and snippets.

@mjangda
Created October 19, 2010 17:25
Show Gist options
  • Select an option

  • Save mjangda/634609 to your computer and use it in GitHub Desktop.

Select an option

Save mjangda/634609 to your computer and use it in GitHub Desktop.
Stub for a the Public Preview plugin
<?php
/*
Plugin Name: Public Preview
Plugin URI: http://digitalize.ca
Description: Enables unauthenticated users to preview posts with a secret link!
Author: Mohammad Jangda
Version: 0.1
Author URI: http://digitalize.ca/
*/
define( 'PUBLIC_PREVIEW_URL', path_join( WP_PLUGIN_URL, basename( dirname( __FILE__ ) ) ) );
define( 'PUBLIC_PREVIEW_PATH', dirname( __FILE__ ) );
define( 'PUBLIC_PREVIEW_SECRET_KEY', '_preview_secret_key' );
define( 'PUBLIC_PREVIEW_PREVIEW_PARAM', 'preview_id' );
define( 'PUBLIC_PREVIEW_KEY_PARAM', 'preview_secret' );
add_action( 'init', 'public_preview_init' );
function public_preview_init() {
if ( isset( $_GET[ PUBLIC_PREVIEW_PREVIEW_PARAM ] ) && isset( $_GET[ PUBLIC_PREVIEW_KEY_PARAM ] ) ) {
$post_id = (int) $_GET['preview_id'];
if ( false == public_preview_verify_secret_key( $_GET[ PUBLIC_PREVIEW_KEY_PARAM ], $post_id ) )
wp_die( __( 'You do not have permission to preview drafts.' ) );
add_filter( 'the_preview', '_set_preview' );
// Add fixed header that warns user that viewing preview
//add_filter( 'wp_footer', 'public_preview_notice' );
}
}
function public_preview_get_secret_key( $post_id ) {
return get_metadata( 'post', $post_id, PUBLIC_PREVIEW_SECRET_KEY, true);
}
function public_preview_set_secret_key( $post_id, $secret_key = '' ) {
if( ! $secret_key ) $secret_key = public_preview_build_secret_key( $post_id );
return add_metadata( 'post', $post_id, PUBLIC_PREVIEW_SECRET_KEY, $secret_key );
// TODO: Add expiry time
}
function public_preview_verify_secret_key( $submitted_key, $post_id ) {
$secret_key = public_preview_get_secret_key( $post_id );
return $secret_key == $submitted_key;
}
// TODO: Maybe change to edit_post (?)
add_action( 'save_post', 'public_preview_add_secret_key' );
function public_preview_add_secret_key( $post ) {
$post_id = $post->ID;
$secret_key = public_preview_get_secret_key( $post_id );
if( ! $secret_key ) {
$secret_key = public_preview_build_secret_key( $post_id );
public_preview_set_secret_key( $post_id, $secret_key );
}
}
function public_preview_build_secret_key( $post_id ) {
return wp_generate_password( 32, false, false );
}
function public_preview_meta_box( ) {
// wp_nonce_tick()
return wp_generate_password( 32, false, false );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment