plugin = $plugin; $this->screens = array( 'post' ); add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); add_action( 'save_post', array( $this, 'save' ), 10, 2 ); } function add_meta_box() { if(!get_current_screen()->is_block_editor()){ $args = array( '_builtin' => false, ); $posttypes = get_post_types( $args ); foreach ( $posttypes as $posttype ) { if ( post_type_supports( $posttype, 'author' ) ) { $this->screens[] = $posttype; } } add_meta_box( 'additional-authors-meta-box', __( 'Additional Authors', Plugin::DOMAIN ), array( $this, 'additional_authors_html' ), $this->screens, 'side', 'high', array( '__block_editor_compatible_meta_box' => false, ) ); } } function additional_authors_html( $post ) { wp_nonce_field( '_additional_authors_nonce', 'additional_authors_nonce' ); /** * get selected users */ $selected = $this->plugin->database->get_author_ids( $post->ID ); /** * We want to filter possible Authors by their capabilities, default is 'edit_posts'. */ $capabilities = apply_filters(Plugin::FILTER_WP_QUERY_CAPABILITY_FOR_AUTHORS, 'edit_posts'); /** * get all users */ $users = get_users( apply_filters( Plugin::FILTER_META_BOX_GET_USERS, array( 'capability' => $capabilities, 'orderby' => 'display_name', // No user_login - see Assets::enqueueGutenberg(). 'fields' => array( 'ID', 'display_name', 'user_nicename', ), ) ) ); $config = array( "users" => $users, "selected" => $selected, "language" => array( "label" => __( 'Search for author:' ), "description" => __( 'Selected authors.' ), ), "root_id" => "meta_additional_authors", ); $this->plugin->assets->enqueueMetaBox($config); do_action( Plugin::ACTION_META_BOX_BEFORE, $post ); ?>
plugin->database->delete_all_of_post($post_id); } if ( isset( $_POST[ self::POST_AUTHORS ]["ids"] ) && is_array( $_POST[ self::POST_AUTHORS ]["ids"] ) ) { $names = isset( $_POST[ self::POST_AUTHORS ]["names"] ) && is_array( $_POST[ self::POST_AUTHORS ]["names"] ) ? $_POST[ self::POST_AUTHORS ]["names"] : array(); foreach ( $_POST[ self::POST_AUTHORS ]["ids"] as $index => $additional_author ) { /** * skip first because it is main author and saved on post * only if is not gutenberg */ if ( $index == 0 && ! $is_gutenberg ) { // Reassigning a post to somebody else is what edit_others_posts // governs in WordPress, and the id has to belong to a real user. if ( ! current_user_can( 'edit_others_posts' ) || ! get_userdata( intval( $additional_author ) ) ) { continue; } remove_action( 'save_post', array( $this, 'save' ) ); wp_update_post( array( "ID" => $post_id, "post_author" => intval( $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 ) { // A non-positive id means "create an account for this name". That // is user creation, which core reserves for administrators, so it // is gated instead of being available to everyone who may edit // this post - a Contributor could otherwise mint Author accounts. if ( ! $this->mayCreateUsers() ) { continue; } if ( ! isset( $names[ $index ] ) || ! is_string( $names[ $index ] ) ) { continue; } $name = sanitize_text_field( $names[ $index ] ); if ( '' === trim( $name ) ) { continue; } $additional_author = $this->plugin->user->create( $name ); if ( is_wp_error( $additional_author ) ) { // TODO: error display continue; } } elseif ( ! get_userdata( intval( $additional_author ) ) ) { continue; } // @deprecated // add_post_meta( $post_id, Plugin::META_POST_ADDITIONAL_AUTHORS, $additional_author ); $this->plugin->database->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 ); $this->plugin->database->delete($post_id, $post->post_author ); break; } } } } }