diff --git a/main.go b/main.go index c163977..682a264 100644 --- a/main.go +++ b/main.go @@ -6,10 +6,12 @@ import ( "fmt" ) +// WP Config const baseUrl = "https://slowtwitch.cloud/" const wordpressKey = "admin@slowtwitch.cloud" const wordpressSecret = "6zY7 xsKZ dGIt l1Lp ypIK 6TWh" +// DB Config const slowtwitchAdminUser = "admin" const slowtwitchAdminPass = "yxnh93Ybbz2Nm8#mp28zCVv" const slowtwitchDbName = "slowtwitch" diff --git a/services/migration/migrate-posts.go b/services/migration/migrate-posts.go index 22f4158..64868dc 100644 --- a/services/migration/migrate-posts.go +++ b/services/migration/migrate-posts.go @@ -18,7 +18,7 @@ type MigratePosts struct { WordpressPassword string } -func (migration MigratePosts) Execute() { +func (migration MigratePosts) Execute() []PostResult { slowtwitchPostIds, err := slowtwitch.GetAllPostIds(migration.SlowtwitchDatabase) if err != nil { panic("Could not migrate posts: " + err.Error()) @@ -36,7 +36,7 @@ func (migration MigratePosts) Execute() { } slowtwitchPostIdsForMigration := getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds) - + var postResults []PostResult for _, postId := range slowtwitchPostIdsForMigration { errorMessage := "" //migrate @@ -220,21 +220,42 @@ func (migration MigratePosts) Execute() { fmt.Println("Error creating redirect for", postId, ":"+postRedirectErr.Error()) } fmt.Println("Successfully created post and result for", postId) - // TODO Update advanced Custom Fields with images - //Install ACF and create field - /* - "acf": { - "post_images": [...ids] - } - */ + updateAcfImages(imageResults, post.Id, migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) + postResults = append(postResults, postResult) } - // TODO Update related posts (get from post results db) as second loop - //Install ACF and create field - /* - "acf": { - "related_posts": [...ids] + // Update related posts once work is done + for _, postResult := range postResults { + if postResult.IsSuccess { + var relatedWordpressIds []int + + relatedSlowtwitchIds, slowtwitchIdsErr := slowtwitch.GetRelatedArticleIds(postResult.SlowtwitchId, migration.SlowtwitchDatabase) + + if slowtwitchIdsErr != nil { + continue + } + for _, slowtwitchRelatedId := range relatedSlowtwitchIds { + wordpressRelatedId, wordpressIdErr := GetWordpressPostIdBySlowtwitchPostId(slowtwitchRelatedId, migration.ResultsDatabase) + if wordpressIdErr != nil { + continue + } + relatedSlowtwitchIds = append(relatedWordpressIds, wordpressRelatedId) + } + + if len(relatedWordpressIds) > 0 { + updateWordpressRelatedPosts := wordpress.UpdateAcfRelatedPosts{ + Acf: wordpress.AcfRelatedPosts{ + PostIds: relatedWordpressIds, + }, + } + updateRelatedPostErr := updateWordpressRelatedPosts.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword, postResult.WordpressId) + if updateRelatedPostErr != nil { + fmt.Println("Error updating wordpressRelatedPosts", updateRelatedPostErr.Error()) + } + } } - */ + } + + return postResults } func getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds []int) []int { @@ -285,3 +306,29 @@ func createImageFailureResult(url string, resultsDatabase *sql.DB) { fmt.Println("Failed to create failure result: ", err) } } + +func getSuccessfulWordpressImageIds(imageResults []ImageResult) []int { + var output []int + + for _, imageResult := range imageResults { + output = append(output, imageResult.WordpressId) + } + + return output +} + +func updateAcfImages(imageResults []ImageResult, postId int, baseUrl, user, pass string) { + if len(imageResults) > 0 { + wordpressImageIds := getSuccessfulWordpressImageIds(imageResults) + updateAcfImages := wordpress.UpdateAcfImages{ + Acf: wordpress.AcfImages{ + PostImages: wordpressImageIds, + }, + } + acfImagesErr := updateAcfImages.Execute(baseUrl, user, pass, postId) + + if acfImagesErr != nil { + fmt.Println("Error updating acf images for post", postId, ":", acfImagesErr.Error()) + } + } +} diff --git a/services/migration/post-result.go b/services/migration/post-result.go index beb7213..2b94050 100644 --- a/services/migration/post-result.go +++ b/services/migration/post-result.go @@ -24,3 +24,12 @@ func CreatePostResult(parameters PostResult, db *sql.DB) (int, error) { return int(id), nil } } + +func GetWordpressPostIdBySlowtwitchPostId(slowtwitchPostId int, db *sql.DB) (int, error) { + var wordpressId int + + post := db.QueryRow("select WordpressId from PostResults where SlowtwitchId = (?)", slowtwitchPostId) + err := post.Scan(&wordpressId) + + return wordpressId, err +} diff --git a/services/slowtwitch/get-related-post-ids.go b/services/slowtwitch/get-related-post-ids.go new file mode 100644 index 0000000..a3a898a --- /dev/null +++ b/services/slowtwitch/get-related-post-ids.go @@ -0,0 +1,26 @@ +package slowtwitch + +import ( + "database/sql" + "strconv" + "strings" +) + +func GetRelatedArticleIds(postId int, db *sql.DB) ([]int, error) { + var spaceSeparatedIds string + rows := db.QueryRow("SELECT RelatedArticles FROM slowtwitch.glinks_Links where ID = ?", postId) + err := rows.Scan(&spaceSeparatedIds) + if err != nil { + return make([]int, 0), err + } + + var ids []int + idsAsStrings := strings.Split(spaceSeparatedIds, " ") + for _, stringId := range idsAsStrings { + id, err := strconv.Atoi(stringId) + if err == nil { + ids = append(ids, id) + } + } + return ids, nil +} diff --git a/services/wordpress/update-acf-images.go b/services/wordpress/update-acf-images.go new file mode 100644 index 0000000..2f02295 --- /dev/null +++ b/services/wordpress/update-acf-images.go @@ -0,0 +1,20 @@ +package wordpress + +import ( + "federated.computer/wp-sync-slowtwitch/utilities" + "strconv" +) + +type UpdateAcfImages struct { + Acf AcfImages `json:"acf"` +} + +type AcfImages struct { + PostImages []int `json:"post_images"` +} + +func (parameters *UpdateAcfImages) Execute(baseUrl, user, pass string, postId int) error { + endpoint := baseUrl + "wp-json/wp/v2/posts/" + strconv.Itoa(postId) + _, err := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) + return err +} diff --git a/services/wordpress/update-acf-related-posts.go b/services/wordpress/update-acf-related-posts.go new file mode 100644 index 0000000..33ee27b --- /dev/null +++ b/services/wordpress/update-acf-related-posts.go @@ -0,0 +1,20 @@ +package wordpress + +import ( + "federated.computer/wp-sync-slowtwitch/utilities" + "strconv" +) + +type UpdateAcfRelatedPosts struct { + Acf AcfRelatedPosts `json:"acf"` +} + +type AcfRelatedPosts struct { + PostIds []int `json:"related_posts"` +} + +func (parameters *UpdateAcfRelatedPosts) Execute(baseUrl, user, pass string, postId int) error { + endpoint := baseUrl + "wp-json/wp/v2/posts/" + strconv.Itoa(postId) + _, err := utilities.PostHttpRequestToWordpress(endpoint, user, pass, parameters) + return err +}