diff --git a/includes/publisher.php b/includes/publisher.php index 15103db..aa690cf 100644 --- a/includes/publisher.php +++ b/includes/publisher.php @@ -68,7 +68,7 @@ public function get_posts_by_parent( $parent ){ 'post_parent' => $parent )); - + foreach( $posts as $index => $post ){ $result[ $post->post_name ] = array( @@ -117,7 +117,7 @@ public function create_post( $post_id, $item_slug, $item_props, $parent ){ } } - + // Check if item props exist, in case of dir posts if( $item_props ){ $item_content = $this->repository->get_item_content( $item_props ); @@ -147,6 +147,22 @@ public function create_post( $post_id, $item_slug, $item_props, $parent ){ $taxonomy = $front_matter[ 'taxonomy' ]; $custom_fields = $front_matter[ 'custom_fields' ]; + // Set default post author + $post_author_id = $this->post_author; + + // Check if the author email is provided in the front matter + if (!empty($front_matter['author'])) { + $post_author_email = $front_matter['author']; + + // Get the user object by email + $user = get_user_by('email', $post_author_email); + + // If a user is found, update the dynamic user ID + if ($user) { + $post_author_id = $user->ID; + } + } + $post_date = ''; if( !empty( $front_matter[ 'post_date' ] ) ){ $post_date = GIW_Utils::process_date( $front_matter[ 'post_date' ] ); @@ -199,7 +215,7 @@ public function create_post( $post_id, $item_slug, $item_props, $parent ){ 'post_name' => $item_slug, 'post_content' => $content, 'post_type' => $this->post_type, - 'post_author' => $this->post_author, + 'post_author' => $post_author_id, 'post_status' => $post_status, 'post_excerpt' => $post_excerpt, 'post_parent' => $parent, @@ -298,7 +314,7 @@ public function create_posts( $repo_structure, $parent ){ $this->create_post( $directory_post, $item_slug, $index_props, $parent ); }else{ - + // If index posts exists for the directory if( array_key_exists( 'index', $item_props[ 'items' ] ) ){ $index_props = $item_props[ 'items' ][ 'index' ]; @@ -370,7 +386,7 @@ public function upload_images_recursive( $images, &$uploaded_images ){ $uploaded_image_url = wp_get_attachment_url( $uploaded_image_id ); - // Check if image is uploaded correctly and + // Check if image is uploaded correctly and if( !empty( $uploaded_image_url ) ){ GIW_Utils::log( 'Image is uploaded [' . $uploaded_image_url . ']. ID: ' . $uploaded_image_id ); @@ -391,11 +407,11 @@ public function upload_images_recursive( $images, &$uploaded_images ){ } } - + } /** - * Uploads image from a URL. A modified version of `media_sideload_image` function + * Uploads image from a URL. A modified version of `media_sideload_image` function * to honor authentication while fetching image data with GET from private repositories */ public function upload_image( $image_props, $post_id = 0, $desc = null, $return_type = 'html' ) { @@ -414,7 +430,7 @@ public function upload_image( $image_props, $post_id = 0, $desc = null, $return_ if ( ! $matches ) { return new WP_Error( 'image_sideload_failed', __( 'Unsupported image format.' ) ); } - + $file_array = array(); $file_array['name'] = wp_basename( $matches[0] ); @@ -439,36 +455,36 @@ public function upload_image( $image_props, $post_id = 0, $desc = null, $return_ if ( is_wp_error( $file_array['tmp_name'] ) ) { return $file_array['tmp_name']; } - + // Loads the downloaded image file to the library. Temporary file is deleted here after upload. $id = media_handle_sideload( $file_array, $post_id, $desc ); - + // If error storing permanently, unlink. if ( is_wp_error( $id ) ) { @unlink( $file_array['tmp_name'] ); return $id; } - + // Store the original attachment source in meta. add_post_meta( $id, '_source_url', $file ); - + // If attachment ID was requested, return it. if ( 'id' === $return_type ) { return $id; } - + $src = wp_get_attachment_url( $id ); } - + // Finally, check to make sure the file has been saved, then return the HTML. if ( ! empty( $src ) ) { if ( 'src' === $return_type ) { return $src; } - + $alt = isset( $desc ) ? esc_attr( $desc ) : ''; $html = "$alt"; - + return $html; } else { return new WP_Error( 'image_sideload_failed' );