Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic author ID based on the author email from post content #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions includes/publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function get_posts_by_parent( $parent ){
'post_parent' => $parent
));


foreach( $posts as $index => $post ){

$result[ $post->post_name ] = array(
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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' ] );
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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' ];
Expand Down Expand Up @@ -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 );
Expand All @@ -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' ) {
Expand All @@ -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] );

Expand All @@ -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 = "<img src='$src' alt='$alt' />";

return $html;
} else {
return new WP_Error( 'image_sideload_failed' );
Expand Down