plugin = $plugin; add_action( 'add_meta_boxes_post', array( $this, 'add_meta_box' ) ); add_action( 'save_post', array( $this, 'save' ), 10, 2 ); } function add_meta_box() { add_meta_box( 'additional-authors-meta-box', __( 'Additional Authors', 'additional_authors' ), array( $this, 'additional_authors_html' ), 'post', 'side', 'high' ); } function additional_authors_html( $post ) { wp_nonce_field( '_additional_authors_nonce', 'additional_authors_nonce' ); wp_enqueue_style( "additional_authors_meta_box_style", $this->plugin->url . "/css/meta-box.css" ); $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG == true ) ? "" : ".min"; wp_enqueue_script( "additional_authors_meta_box_script", $this->plugin->url . "/js/bundle/main" . $min . ".js", array(), 1, true ); /** * get selected users */ $selected = Table\get_author_ids( $post->ID ); /** * get all users */ $users = get_users( array( 'who' => 'authors', 'orderby' => 'display_name', 'fields' => array( 'ID', 'display_name', 'user_login', 'user_nicename', ), ) ); array_shift( $users ); $config = array( "users" => $users, "selected" => $selected, "language" => array( "label" => __( 'Search for author:' ), "description" => __( 'Selected authors.' ), ), "root_id" => "meta_additional_authors", ); wp_localize_script( 'additional_authors_meta_box_script', 'AdditionalAuthors', $config ); do_action( Plugin::ACTION_META_BOX_BEFORE, $post ); ?>
$additional_author ) { /** * skip first because it is main author and saved on post * only if is not gutenberg */ if ( $index == 0 && !$is_gutenberg ) { remove_action( 'save_post', array( $this, 'save' ) ); wp_update_post( array( "ID" => $post_id, "post_author" => $additional_author, ) ); add_action( 'save_post', array( $this, 'save' ), 10, 2 ); continue; } // to not add main author as additional author if(intval($additional_author) > 0 && $additional_author == $post->post_author){ continue; } if ( intval( $additional_author ) <= 0 ) { $name = $_POST[ self::POST_AUTORS ]["names"][ $index ]; $additional_author = $this->plugin->user->create( $name ); if ( is_wp_error( $additional_author ) ) { // TODO: error display } } // @deprecated // add_post_meta( $post_id, Plugin::META_POST_ADDITIONAL_AUTHORS, $additional_author ); Table\set( $post_id, $additional_author ); } } else { // something else called wp_update_post $user_ids = $this->plugin->get_ids( $post_id ); for ( $i = 1; $i < count( $user_ids ); $i ++ ) { if ( $post->post_author == $user_ids[ $i ] ) { // additional author is now main author delete_post_meta( $post_id, Plugin::META_POST_ADDITIONAL_AUTHORS, $post->post_author ); Table\delete( $post_id, $post->post_author ); break; } } } } }