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
} );
}
Datei templates/new-draft-form.php
: