From d6a43dbb908aa67c4b90cf3ea16300110450bd5a Mon Sep 17 00:00:00 2001
From: Erick Martinez <44077419+E-Rick@users.noreply.github.com>
Date: Wed, 2 Jun 2021 18:23:14 -0400
Subject: [PATCH 1/5] fix: check permalink return and break out loop at 5
---
article-recommendations.php | 28 ++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)
diff --git a/article-recommendations.php b/article-recommendations.php
index bcdd231..a512ece 100644
--- a/article-recommendations.php
+++ b/article-recommendations.php
@@ -187,11 +187,12 @@ function get_recommendations( $results, $id = 'recommendations' ) {
$count = 1;
$articles = "";
$list_items = "";
+ $model_attributes = "";
$count_array = ['one','two','three','four','five'];
foreach ( $results as $result ) {
// Limit 5 posts
if ( $count > 5 ) {
- return;
+ break;
}
// Get score from result (Rec API)
@@ -206,18 +207,28 @@ function get_recommendations( $results, $id = 'recommendations' ) {
$model = $result["model"];
$post = $result["recommended_article"];
- //$href = "href=https://www.washingtoncitypaper.com/{$rec['path']} >";
- $post_id = $post['external_id'] ?? $post['ID']; // If Rec API result, get the 'external_id' else set to WP 'ID'
- $title = $post['title'] ?? $post['post_title']; // If Rec API result, get the 'title' else set to WP 'post_title'
- $post_title = apply_filters('the_title', $title, $post_id); // Display the post_title
- $href = esc_url(get_permalink($post_id));
+ // If Rec API result, get the 'external_id' else set to WP 'ID'
+ $post_id = $post['external_id'] ?? $post['ID'];
+
+ // If Rec API result, get the 'title' else set to WP 'post_title'
+ $title = $post['title'] ?? $post['post_title'];
+
+ // Display the post_title
+ $post_title = apply_filters('the_title', $title, $post_id);
+
+ // If permalink returns false, no post found. Then use wcp link.
+ if( ! get_permalink( $post_id ) ) {
+ $href = esc_url( "https://www.washingtoncitypaper.com{$post['path']}" );
+ } else {
+ $href = esc_url( get_permalink( $post_id ) );
+ }
// Create the attributes for model and article recommendation
$model_attributes = "data-vars-model-id={$model['id']} data-vars-model-status={$model['status']} data-vars-model-type={$model['type']} ";
$article_attributes = "data-vars-article-id={$post['external_id']} data-vars-rec-id={$post['id']} data-vars-position={$count} data-vars-score={$score} ";
-
$articles .= "data-vars-{$count_array[ $count - 1 ]}={$post['external_id']} ";
+ // Add list item to string builder
$list_items .= "
{$post_title}";
$count++; // increase count
@@ -248,7 +259,7 @@ function get_recent_posts( $id = 'recentposts' ) {
foreach ( $posts as $post ) {
// Limit 5 posts
if ( $count > 5 ) {
- return;
+ break;
}
// I can probably made a whole default model object and use nullish coal to default the values to null.
@@ -281,6 +292,7 @@ function get_recent_posts( $id = 'recentposts' ) {
$count++;
}
+
return "";
} // end of function get_recent_posts
From cb288f2f79833cd4070dc05226551ab101edc56d Mon Sep 17 00:00:00 2001
From: Erick Martinez <44077419+E-Rick@users.noreply.github.com>
Date: Wed, 2 Jun 2021 18:50:56 -0400
Subject: [PATCH 2/5] fix: esc the wcp url
---
article-recommendations.php | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/article-recommendations.php b/article-recommendations.php
index a512ece..a8b6641 100644
--- a/article-recommendations.php
+++ b/article-recommendations.php
@@ -218,7 +218,8 @@ function get_recommendations( $results, $id = 'recommendations' ) {
// If permalink returns false, no post found. Then use wcp link.
if( ! get_permalink( $post_id ) ) {
- $href = esc_url( "https://www.washingtoncitypaper.com{$post['path']}" );
+ $url = esc_url ( "https://www.washingtoncitypaper.com{$post['path']}" );
+ $href = "href={$url}";
} else {
$href = esc_url( get_permalink( $post_id ) );
}
From 5edb6d8aab821828e10350181ac36a8f966dbadb Mon Sep 17 00:00:00 2001
From: Erick Martinez <44077419+E-Rick@users.noreply.github.com>
Date: Wed, 2 Jun 2021 18:59:54 -0400
Subject: [PATCH 3/5] fix: add model attributes to list items
---
article-recommendations.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/article-recommendations.php b/article-recommendations.php
index a8b6641..9ae2baf 100644
--- a/article-recommendations.php
+++ b/article-recommendations.php
@@ -230,7 +230,7 @@ function get_recommendations( $results, $id = 'recommendations' ) {
$articles .= "data-vars-{$count_array[ $count - 1 ]}={$post['external_id']} ";
// Add list item to string builder
- $list_items .= "{$post_title}";
+ $list_items .= "{$post_title}";
$count++; // increase count
}
@@ -331,4 +331,4 @@ public function update( $new_instance, $old_instance ) {
} // Class article_recommendations ends here
-?>
\ No newline at end of file
+?>
From a8f35a8bdce6f2cd5c6c0c372d05f027d9a0ef17 Mon Sep 17 00:00:00 2001
From: Erick Martinez <44077419+E-Rick@users.noreply.github.com>
Date: Wed, 2 Jun 2021 19:51:25 -0400
Subject: [PATCH 4/5] fix: put href assignment in the list items
---
article-recommendations.php | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)
diff --git a/article-recommendations.php b/article-recommendations.php
index 9ae2baf..5efe59f 100644
--- a/article-recommendations.php
+++ b/article-recommendations.php
@@ -217,12 +217,8 @@ function get_recommendations( $results, $id = 'recommendations' ) {
$post_title = apply_filters('the_title', $title, $post_id);
// If permalink returns false, no post found. Then use wcp link.
- if( ! get_permalink( $post_id ) ) {
- $url = esc_url ( "https://www.washingtoncitypaper.com{$post['path']}" );
- $href = "href={$url}";
- } else {
- $href = esc_url( get_permalink( $post_id ) );
- }
+ $href = esc_url( get_permalink( $post_id ) ?? "https://www.washingtoncitypaper.com{$post['path']}" );
+
// Create the attributes for model and article recommendation
$model_attributes = "data-vars-model-id={$model['id']} data-vars-model-status={$model['status']} data-vars-model-type={$model['type']} ";
@@ -230,7 +226,7 @@ function get_recommendations( $results, $id = 'recommendations' ) {
$articles .= "data-vars-{$count_array[ $count - 1 ]}={$post['external_id']} ";
// Add list item to string builder
- $list_items .= "{$post_title}";
+ $list_items .= "{$post_title}";
$count++; // increase count
}
From 7d0c8ff5865af214836d04e3c7aef5500f2dc0cb Mon Sep 17 00:00:00 2001
From: Erick Martinez <44077419+E-Rick@users.noreply.github.com>
Date: Wed, 2 Jun 2021 20:11:59 -0400
Subject: [PATCH 5/5] revert: fix href permalinks for recs
---
article-recommendations.php | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/article-recommendations.php b/article-recommendations.php
index 5efe59f..2889a05 100644
--- a/article-recommendations.php
+++ b/article-recommendations.php
@@ -217,8 +217,11 @@ function get_recommendations( $results, $id = 'recommendations' ) {
$post_title = apply_filters('the_title', $title, $post_id);
// If permalink returns false, no post found. Then use wcp link.
- $href = esc_url( get_permalink( $post_id ) ?? "https://www.washingtoncitypaper.com{$post['path']}" );
+ $href = "https://www.washingtoncitypaper.com{$post['path']}";
+ if( get_permalink( $post_id ) ){
+ $href = esc_url( get_permalink( $post_id ) );
+ };
// Create the attributes for model and article recommendation
$model_attributes = "data-vars-model-id={$model['id']} data-vars-model-status={$model['status']} data-vars-model-type={$model['type']} ";
@@ -226,7 +229,7 @@ function get_recommendations( $results, $id = 'recommendations' ) {
$articles .= "data-vars-{$count_array[ $count - 1 ]}={$post['external_id']} ";
// Add list item to string builder
- $list_items .= "{$post_title}";
+ $list_items .= "{$post_title}";
$count++; // increase count
}
@@ -327,4 +330,4 @@ public function update( $new_instance, $old_instance ) {
} // Class article_recommendations ends here
-?>
+?>
\ No newline at end of file