Je souhaitais ajouter des liens de duplication de post dans l'écran listant mes articles. Vu que pour chaque post publié, je créé toujours 2 à 3 posts "liés" avec les prefixes "News - Nom de l'article", "Vidéos - Nom de l'article" et "Forum - Nom de l'article", j'ai tout simplement ajouté une fonction par lien de duplication, avec le add_action associé.
Seulement voilà, le code pour ces (4) fonctions est relativement similaire. Seules la variable $args change, ainsi que le nom attribué aux fonctions, aux add_action, et au lien (Duplication Vidéo / Duplication News / ...).
Ma question est donc la suivante : pourriez-vous me guider sur une éventuelle optimisation du code que je vais fournir plus bas? Il doit bien y avoir un moyen de spécifier les valeurs de l'array de $args conditionnellement, et les associer aux liens de duplication correspondant en une fonction et un add_action? Corrigez moi si je me trompe.
Voici les 4 fonctions (relativement similaires donc) :
Code : Tout sélectionner
/****************************/
/* Duplication -> Générique */
/****************************/
function alt_duplicate_function(){
global $wpdb;
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'alt_duplicate_function' == $_REQUEST['action'] ) ) ) {
wp_die('No post to duplicate has been supplied!');
}
// get the original post id
$post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
// and all the original post data then
$post = get_post( $post_id );
// if you don't want current user to be the new post author,
// then change next couple of lines to this:
// $new_post_author = $post->post_author;
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
// if post data exists, create the post duplicate
if (isset( $post ) && $post != null) {
// new post data array
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
// insert the post by wp_insert_post() function
$new_post_id = wp_insert_post( $args );
// get all current post terms ad set them to the new post draft
$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
// duplicate all post meta
$data = get_post_custom($post_id);
foreach ( $data as $key => $values) {
foreach ($values as $value) {
add_post_meta( $new_post_id, $key, $value );
}
}
//finally, redirect to the edit post screen for the new draft
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
} else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}
add_action( 'admin_action_alt_duplicate_function', 'alt_duplicate_function' );
// Add the duplicate link to action list for post_row_actions
function alt_duplicate_post_link( $actions, $post ) {
if( ! is_singular( array('page', 'attachment', 'post') ) && current_user_can('edit_posts')) {
$actions['duplicate'] = '<a href="admin.php?action=alt_duplicate_function&post=' . $post->ID . '" title="Dupliquer l\'article" rel="permalink">Dupliquer</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'alt_duplicate_post_link', 10, 2 );Code : Tout sélectionner
/***********************/
/* Duplication -> NEWS */
/***********************/
function alt_duplicate_news_function(){
global $wpdb;
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'alt_duplicate_news_function' == $_REQUEST['action'] ) ) ) {
wp_die('No post to duplicate has been supplied!');
}
// get the original post id
$post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
// and all the original post data then
$post = get_post( $post_id );
// if you don't want current user to be the new post author,
// then change next couple of lines to this:
// $new_post_author = $post->post_author;
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
// if post data exists, create the post duplicate
if (isset( $post ) && $post != null) {
// new post data array
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_excerpt' => $post->post_excerpt,
'post_name' => 'news-'.$post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => 'News - '.$post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
// insert the post by wp_insert_post() function
$new_post_id = wp_insert_post( $args );
// get all current post terms ad set them to the new post draft
$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
// duplicate all post meta
$data = get_post_custom($post_id);
foreach ( $data as $key => $values) {
foreach ($values as $value) {
add_post_meta( $new_post_id, $key, $value );
}
}
//finally, redirect to the edit post screen for the new draft
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
} else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}
add_action( 'admin_action_alt_duplicate_news_function', 'alt_duplicate_news_function' );
// Add the duplicate link to action list for post_row_actions
function alt_duplicate_post_news_link( $actions, $post ) {
if( ! is_singular( array('page', 'attachment', 'post') ) && current_user_can('edit_posts')) {
$actions['duplicate_news'] = '<a href="admin.php?action=alt_duplicate_news_function&post=' . $post->ID . '" title="Créer un article NEWS associé" rel="permalink">Créer l\'article News</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'alt_duplicate_post_news_link', 10, 2 );Code : Tout sélectionner
/************************/
/* Duplication -> FORUM */
/************************/
function alt_duplicate_forum_function(){
global $wpdb;
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'alt_duplicate_forum_function' == $_REQUEST['action'] ) ) ) {
wp_die('No post to duplicate has been supplied!');
}
// get the original post id
$post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
// and all the original post data then
$post = get_post( $post_id );
// if you don't want current user to be the new post author,
// then change next couple of lines to this:
// $new_post_author = $post->post_author;
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
// if post data exists, create the post duplicate
if (isset( $post ) && $post != null) {
// new post data array
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_excerpt' => $post->post_excerpt,
'post_name' => 'forum-'.$post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => 'Forum - '.$post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
// insert the post by wp_insert_post() function
$new_post_id = wp_insert_post( $args );
// get all current post terms ad set them to the new post draft
$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
// duplicate all post meta
$data = get_post_custom($post_id);
foreach ( $data as $key => $values) {
foreach ($values as $value) {
add_post_meta( $new_post_id, $key, $value );
}
}
//finally, redirect to the edit post screen for the new draft
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
} else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}
add_action( 'admin_action_alt_duplicate_forum_function', 'alt_duplicate_forum_function' );
// Add the duplicate link to action list for post_row_actions
function alt_duplicate_post_forum_link( $actions, $post ) {
if( ! is_singular( array('page', 'attachment', 'post') ) && current_user_can('edit_posts')) {
$actions['duplicate_forum'] = '<a href="admin.php?action=alt_duplicate_forum_function&post=' . $post->ID . '" title="Créer un article FORUM associé" rel="permalink">Créer l\'article Forum</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'alt_duplicate_post_forum_link', 10, 2 );Code : Tout sélectionner
/*************************/
/* Duplication -> VIDEOS */
/*************************/
function alt_duplicate_videos_function(){
global $wpdb;
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'alt_duplicate_videos_function' == $_REQUEST['action'] ) ) ) {
wp_die('No post to duplicate has been supplied!');
}
// get the original post id
$post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
// and all the original post data then
$post = get_post( $post_id );
// if you don't want current user to be the new post author,
// then change next couple of lines to this:
// $new_post_author = $post->post_author;
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
// if post data exists, create the post duplicate
if (isset( $post ) && $post != null) {
// new post data array
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_excerpt' => $post->post_excerpt,
'post_name' => 'videos-'.$post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => 'Vidéos - '.$post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
// insert the post by wp_insert_post() function
$new_post_id = wp_insert_post( $args );
// get all current post terms ad set them to the new post draft
$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
// duplicate all post meta
$data = get_post_custom($post_id);
foreach ( $data as $key => $values) {
foreach ($values as $value) {
add_post_meta( $new_post_id, $key, $value );
}
}
//finally, redirect to the edit post screen for the new draft
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
} else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}
add_action( 'admin_action_alt_duplicate_videos_function', 'alt_duplicate_videos_function' );
// Add the duplicate link to action list for post_row_actions
function alt_duplicate_post_videos_link( $actions, $post ) {
if( ! is_singular( array('page', 'attachment', 'post') ) && current_user_can('edit_posts')) {
$actions['duplicate_videos'] = '<a href="admin.php?action=alt_duplicate_videos_function&post=' . $post->ID . '" title="Créer un article VIDEOS associé" rel="permalink">Créer l\'article Vidéos</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'alt_duplicate_post_videos_link', 10, 2 );