Datei drafts-for-friends.php
:
<?php
/*
Plugin Name: Drafts for Friends
Plugin URI: https://wp-plugin-erstellen.de
Version: 0.1.0
Author: Florian Simeth
Author URI: https://florian-simeth.de
Description: Allows to create links to draft posts.
Text Domain: drafts-for-friends
Domain Path: /languages
License: GPL
*/
add_action( 'admin_menu', 'dff_menus' );
function dff_menus() {
$hook = add_submenu_page(
'edit.php', # Eltern-Menü
__( 'Drafts for Friends', 'drafts-for-friends' ), # Seitentitel
__( 'Drafts for Friends', 'drafts-for-friends' ), # Menütitel
'manage_options', # Benutzerrecht
'drafts-for-friends', # Menü-Slug
'dff_settings_page_render' # Funktionsname
);
add_action( 'load-' . $hook, 'dff_new_draft_create' );
}
function dff_settings_page_render() {
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<?php
include( plugin_dir_path( __FILE__ ) . '/templates/released-posts.php' );
include( plugin_dir_path( __FILE__ ) . '/templates/new-draft-form.php' );
?>
</div>
<?php
}
function dff_draft_select_options() {
$query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'draft',
'meta_query' => array(
array(
'key' => 'dff_key',
'compare' => 'NOT EXISTS',
),
),
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
printf(
'<option value="%d">%s</option>',
get_the_ID(),
esc_attr( get_the_title() )
);
}
} else {
printf(
'<option value="0">%s</option>',
esc_attr__( 'No drafts found.', 'drafts-for-friends' )
);
}
}
function dff_new_draft_create() {
if ( ! isset( $_POST['action'] ) ) {
return;
}
$nonce_check = check_admin_referer( 'dff_new_draft' );
if ( ! $nonce_check ) {
add_action( 'admin_notices', function () {
?>
<div class="notice notice-error is-dismissible">
<p><?php _e( 'Sorry, you cannot create a new draft.', 'drafts-for-friends' ); ?></p>
</div>
<?php
} );
return;
}
$post_id = absint( $_POST['post_id'] ?? 0 ); // works in PHP > 7.0 only
if ( empty( $post_id ) ) {
add_action( 'admin_notices', function () {
?>
<div class="notice notice-error is-dismissible">
<p><?php _e( 'Please select a valid draft post from the select box.', 'drafts-for-friends' ); ?></p>
</div>
<?php
} );
return;
}
$is_saved = add_post_meta( $post_id, 'dff_key', uniqid() );
if ( ! $is_saved ) {
add_action( 'admin_notices', function () {
?>
<div class="notice notice-error is-dismissible">
<p><?php _e( 'Sorry, draft could not be released.', 'drafts-for-friends' ); ?></p>
</div>
<?php
} );
return;
}
add_action( 'admin_notices', function () {
?>
<div class="notice notice-success is-dismissible">
<p><?php _e( 'Draft has been released.', 'drafts-for-friends' ); ?></p>
</div>
<?php
} );
}
add_filter( 'query_vars', 'dff_add_query_var' );
function dff_add_query_var( $vars ) {
$vars[] = 'dff_key';
return $vars;
}
add_action( 'pre_get_posts', 'dff_intersect' );
function dff_intersect( $wp_query ) {
if ( ! $wp_query->is_main_query() ) {
return;
}
if ( ! $wp_query->is_single ) {
return;
}
$post_stati = (array) get_query_var( 'post_status' );
$post_stati[] = 'draft';
$post_stati = array_filter( $post_stati );
set_query_var( 'post_status', $post_stati );
$key = get_query_var( 'dff_key' );
$meta_queries = (array) get_query_var( 'meta_query' );
$meta_queries[] = array(
'key' => 'dff_key',
'value' => esc_sql( $key ),
'compare' => '=',
);
$meta_queries = array_filter( $meta_queries );
set_query_var( 'meta_query', $meta_queries );
}
add_action( 'publish_post', 'dff_delete_key_on_transition' );
function dff_delete_key_on_transition( $post_id ) {
delete_post_meta( $post_id, 'dff_key' );
}
add_filter( 'wp_redirect', 'dff_redirect_remove_key' );
function dff_redirect_remove_key( $url ) {
$key = get_query_var( 'dff_key' );
if ( empty( $key ) ) {
return $url;
}
$post_id = get_query_var( 'p' );
if ( 'draft' != get_post_status( $post_id ) ) {
$url = remove_query_arg( 'dff_key', $url );
}
return $url;
}
Beschreibung: