'awl-labels', 'numberposts' => -1, 'include' => $label_id, 'post_status' => 'any' ) ); if ( false === $label ) { wp_die( sprintf( esc_html__( 'Label creation failed, could not find original label: %s', 'advanced-woo-labels' ), esc_html( $label_id ) ) ); } $duplicate_id = $this->label_duplicate( $label ); if ( isset( $_REQUEST['single_post'] ) ) { wp_redirect( admin_url( 'post.php?action=edit&post=' . $duplicate_id ) ); } else { wp_redirect( admin_url( 'edit.php?post_type=awl-labels' ) ); } exit; } /* * Function to create the duplicate of the label * @param $label * @return $duplicate The duplicate */ private function label_duplicate( $label ) { $duplicate = (array) $label[0]; $duplicate_meta = get_post_meta( $duplicate['ID'], '_awl_label', true ); $duplicate_priority = get_post_meta( $duplicate['ID'], '_awl_label_priority', true ); $duplicate['ID'] = 0; $duplicate['post_status'] = 'draft'; $duplicate['post_title'] = $duplicate['post_title'] . ' ' . esc_html__( '(copy)', 'advanced-woo-labels' ); $duplicate['post_name'] = ''; $duplicate['post_date'] = null; $duplicate['post_date_gmt'] = null; $duplicate['post_modified'] = null; $duplicate['post_modified_gmt'] = null; $duplicate_id = wp_insert_post( $duplicate, true ); if ( is_wp_error( $duplicate_id ) ) { wp_die( sprintf( esc_html__( 'Label creation failed, could not create label duplicate: %s', 'advanced-woo-labels' ), esc_html( $duplicate_id ) ) ); } update_post_meta( $duplicate_id, '_awl_label', $duplicate_meta ); update_post_meta( $duplicate_id, '_awl_label_priority', $duplicate_priority ); return $duplicate_id; } /** * Show the "Duplicate" link in admin products list * @param array $actions Array of actions. * @param WP_Post $post Post object. * @return array */ public function duplicate_link( $actions, $post ) { if ( ! current_user_can( 'edit_posts' ) ) { return $actions; } if ( 'awl-labels' !== $post->post_type ) { return $actions; } $actions['duplicate'] = '' . esc_html__( 'Duplicate', 'advanced-woo-labels' ) . ''; if ( isset( $actions['trash'] ) ) { $trash_action = $actions['trash']; unset( $actions['trash'] ); $actions['trash'] = $trash_action; } return $actions; } /** * Show the duplicate product link in admin */ public function duplicate_button() { global $post; if ( ! current_user_can( 'edit_posts' ) ) { return; } if ( ! is_object( $post ) ) { return; } if ( 'awl-labels' !== $post->post_type ) { return; } $notify_url = wp_nonce_url( admin_url( 'edit.php?post_type=awl-labels&action=duplicate_label&single_post=true&post=' . absint( $post->ID ) ), 'awl-duplicate-label_' . $post->ID ); echo '
'; } }