Skip to content

Commit 1555e01

Browse files
omnisiteBaris @ 1815
andauthored
Fixed fatal and several warnings + WPCS + refactors (#23)
* refactor: add image check + change non-existing standard img size in get_featured_image_from_rest_api * fix: change undefined method count + add check if term exists in get_number_of_posts_in_term.php * refactor: check page exists before getting ID in get_page_id_from_page_title.php * chore: apply WPWC * fix: change glue order in implode because of deprecation in get_random_post_suggestion_by_categories.php * refactor: remove unneeded extra variable in get_random_post_suggestion_by_categories.php * refactor: change query_posts to get_posts because former changes the main query in get_random_post_suggestion_by_categories.php * refactor: check if there are categories before proceeding in get_random_post_suggestion_by_categories.php * fix: change non-existing $categories_string for $tags_string in get_random_post_suggestion_by_tag.php * refactor: remove unneeded extra variable in get_random_post_suggestion_by_tags.php * refactor: change query_posts to get_posts because former changes the main query in get_random_post_suggestion_by_tags.php * refactor: check if there are tags before proceeding in get_random_post_suggestion_by_tags.php * fix: correct way to get tags in get_random_post_suggestion_by_tags.php * fix: change glue order in implode because of deprecation in get_random_post_suggestion_by_tags.php * refactor: no need to change array to string for tag ids in get_random_post_suggestion_by_tags.php * chore: apply WPWC Co-authored-by: Baris @ 1815 <[email protected]>
1 parent 112fddb commit 1555e01

6 files changed

+40
-32
lines changed

src/get_featured_image_from_rest_api.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ function register_rest_images() {
1616

1717
function get_rest_featured_image( $object, $field_name, $request ) {
1818
if ( $object['featured_media'] ) {
19-
$img = wp_get_attachment_image_src( $object['featured_media'], 'app-thumb' );
19+
$img = wp_get_attachment_image_src( $object['featured_media'], 'thumbnail' ); // change 'thumbnail' to other image size if needed
20+
if ( empty( $img ) ) {
21+
return false;
22+
}
2023
return $img[0];
2124
}
2225
return false;

src/get_number_of_posts_in_term.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
//Show the number of posts of any term by any identifier (e.g. for the term category and the term with id 1)
1111

1212
$term = get_term_by( 'id', 1, 'category' );
13-
$term->count();
13+
if ( ! empty( $term ) ) {
14+
echo $term->count;
15+
}

src/get_page_id_from_page_title.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
22
// This will return an object of the page
3-
$page = get_page_by_title( 'page-name' );
4-
$page_id = $page->ID;
3+
$page = get_page_by_title( 'page-name' );
4+
if ( ! empty( $page ) ) {
5+
$page_id = $page->ID;
6+
}
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
<?php
22
//get categories of post
3-
$categories = get_the_terms( get_the_ID(), 'category' );
3+
$categories = get_the_terms( get_the_ID(), 'category' );
44

5-
//extract category id's from term objects
6-
$cat_ids = array_column( $categories, 'term_id' );
5+
if ( ! empty( $categories ) ) {
6+
//extract category id's from term objects
7+
$cat_ids = array_column( $categories, 'term_id' );
78

8-
//concat id's to a string with ',' as seperator
9-
$categories_string = implode($cat_ids, ',');
9+
//concat id's to a string with ',' as seperator
10+
$cat_ids = implode( ',', $cat_ids );
1011

11-
//get five random posts that are in any category of the current post
12-
$posts = query_posts( [
13-
'post_not_in' => get_the_ID(),
14-
'cat' => $categories_string,
15-
'order' => 'ASC',
16-
'orderby' => 'rand',
17-
'post_per_page' => 5
18-
] );
12+
//get five random posts that are in any category of the current post
13+
$posts = get_posts( [
14+
'post_not_in' => get_the_ID(),
15+
'cat' => $cat_ids,
16+
'order' => 'ASC',
17+
'orderby' => 'rand',
18+
'post_per_page' => 5
19+
] );
20+
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
<?php
22
//get tags of post
3-
$tags = get_the_terms( get_the_ID(), 'tag' );
3+
$tags = get_the_tags( get_the_ID() );
44

5-
//extract tag id's from term objects
6-
$tag_ids = array_column( $tags, 'term_id' );
5+
if ( ! empty( $tags ) ) {
6+
//extract tag id's from term objects
7+
$tag_ids = array_column( $tags, 'term_id' );
78

8-
//concat id's to a string with ',' as seperator
9-
$tags_string = implode( $tag_ids, ',' );
10-
11-
//get five random posts that are in any tag of the current post
12-
$posts = query_posts( [
13-
'post_not_in' => get_the_ID(),
14-
'tag__in' => $categories_string,
15-
'order' => 'ASC',
16-
'orderby' => 'rand',
17-
'post_per_page' => 5
18-
] );
9+
//get five random posts that are in any tag of the current post
10+
$posts = get_posts( [
11+
'post_not_in' => get_the_ID(),
12+
'tag__in' => $tags_string,
13+
'order' => 'ASC',
14+
'orderby' => 'rand',
15+
'post_per_page' => 5
16+
] );
17+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
function remove_tagcloud_inline_styling( $input ) {
3-
return preg_replace( '/ style=("|\')(.*?)("|\')/','',$input );
3+
return preg_replace( '/ style=("|\')(.*?)("|\')/', '', $input );
44
}
55

66
add_filter( 'wp_generate_tag_cloud', 'remove_tagcloud_inline_styling', 10, 1 );

0 commit comments

Comments
 (0)