Skip to content

Commit

Permalink
Improve error checking regarding post formats and missing ActivityPub…
Browse files Browse the repository at this point in the history
… metadata (#271)

* Better check for a valid metadata response

* Fix faulty queries when post formats are not enabled
  • Loading branch information
akirk authored Nov 15, 2023
1 parent 2d7b293 commit 4d54e1c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 63 deletions.
2 changes: 1 addition & 1 deletion feed-parsers/class-feed-parser-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public function discover_available_feeds( $content, $url ) {
$discovered_feeds = array();

$meta = self::get_metadata( $url );
if ( $meta && ! is_wp_error( $meta ) ) {
if ( $meta && ! is_wp_error( $meta ) && isset( $meta['id'] ) ) {
$discovered_feeds[ $meta['id'] ] = array(
'type' => 'application/activity+json',
'rel' => 'self',
Expand Down
70 changes: 36 additions & 34 deletions includes/class-subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,42 +296,44 @@ public function get_post_count_by_post_format() {
)
);

$post_format_counts = $wpdb->get_results(
$wpdb->prepare(
sprintf(
"SELECT relationships_post_format.term_taxonomy_id AS post_format_id, COUNT(relationships_post_format.term_taxonomy_id) AS count
FROM %s AS posts
JOIN %s AS relationships_post_format
JOIN %s AS taxonomy_author
JOIN %s AS relationships_author
WHERE posts.post_status IN ( 'publish', 'private' )
AND posts.post_type IN ( %s )
AND relationships_post_format.object_id = posts.ID
AND relationships_post_format.term_taxonomy_id IN ( %s )
AND relationships_author.object_id = posts.ID
AND taxonomy_author.term_taxonomy_id = relationships_author.term_taxonomy_id
AND taxonomy_author.term_id = %s
GROUP BY relationships_post_format.term_taxonomy_id",
$wpdb->posts,
$wpdb->term_relationships,
$wpdb->term_taxonomy,
$wpdb->term_relationships,
implode( ',', array_fill( 0, count( $post_types ), '%s' ) ),
implode( ',', array_fill( 0, count( $post_formats_term_ids ), '%d' ) ),
'%d'
),
array_merge(
$post_types,
array_keys( $post_formats_term_ids ),
array( $this->get_term_id() )
if ( ! empty( $post_formats_term_ids ) ) {
$post_format_counts = $wpdb->get_results(
$wpdb->prepare(
sprintf(
"SELECT relationships_post_format.term_taxonomy_id AS post_format_id, COUNT(relationships_post_format.term_taxonomy_id) AS count
FROM %s AS posts
JOIN %s AS relationships_post_format
JOIN %s AS taxonomy_author
JOIN %s AS relationships_author
WHERE posts.post_status IN ( 'publish', 'private' )
AND posts.post_type IN ( %s )
AND relationships_post_format.object_id = posts.ID
AND relationships_post_format.term_taxonomy_id IN ( %s )
AND relationships_author.object_id = posts.ID
AND taxonomy_author.term_taxonomy_id = relationships_author.term_taxonomy_id
AND taxonomy_author.term_id = %s
GROUP BY relationships_post_format.term_taxonomy_id",
$wpdb->posts,
$wpdb->term_relationships,
$wpdb->term_taxonomy,
$wpdb->term_relationships,
implode( ',', array_fill( 0, count( $post_types ), '%s' ) ),
implode( ',', array_fill( 0, count( $post_formats_term_ids ), '%d' ) ),
'%d'
),
array_merge(
$post_types,
array_keys( $post_formats_term_ids ),
array( $this->get_term_id() )
)
)
)
);
);

foreach ( $post_format_counts as $row ) {
$counts[ $post_formats_term_ids[ $row->post_format_id ] ] = $row->count;
$counts['standard'] -= $row->count;
foreach ( $post_format_counts as $row ) {
$counts[ $post_formats_term_ids[ $row->post_format_id ] ] = $row->count;
$counts['standard'] -= $row->count;
}
}

$counts = array_filter( $counts );
Expand Down
58 changes: 30 additions & 28 deletions includes/class-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,36 +765,38 @@ public function get_post_count_by_post_format() {
)
);

$post_format_counts = $wpdb->get_results(
$wpdb->prepare(
sprintf(
"SELECT relationships_post_format.term_taxonomy_id AS post_format_id, COUNT(relationships_post_format.term_taxonomy_id) AS count
FROM %s AS posts
JOIN %s AS relationships_post_format
WHERE posts.post_author = %s
AND posts.post_status IN ( 'publish', 'private' )
AND posts.post_type IN ( %s )
AND relationships_post_format.object_id = posts.ID
AND relationships_post_format.term_taxonomy_id IN ( %s )
GROUP BY relationships_post_format.term_taxonomy_id",
$wpdb->posts,
$wpdb->term_relationships,
'%d',
implode( ',', array_fill( 0, count( $post_types ), '%s' ) ),
implode( ',', array_fill( 0, count( $post_formats_term_ids ), '%d' ) )
),
array_merge(
array( $this->ID ),
$post_types,
array_keys( $post_formats_term_ids )
if ( ! empty( $post_formats_term_ids ) ) {
$post_format_counts = $wpdb->get_results(
$wpdb->prepare(
sprintf(
"SELECT relationships_post_format.term_taxonomy_id AS post_format_id, COUNT(relationships_post_format.term_taxonomy_id) AS count
FROM %s AS posts
JOIN %s AS relationships_post_format
WHERE posts.post_author = %s
AND posts.post_status IN ( 'publish', 'private' )
AND posts.post_type IN ( %s )
AND relationships_post_format.object_id = posts.ID
AND relationships_post_format.term_taxonomy_id IN ( %s )
GROUP BY relationships_post_format.term_taxonomy_id",
$wpdb->posts,
$wpdb->term_relationships,
'%d',
implode( ',', array_fill( 0, count( $post_types ), '%s' ) ),
implode( ',', array_fill( 0, count( $post_formats_term_ids ), '%d' ) )
),
array_merge(
array( $this->ID ),
$post_types,
array_keys( $post_formats_term_ids )
)
)
)
);
);

foreach ( $post_format_counts as $row ) {
$counts[ $post_formats_term_ids[ $row->post_format_id ] ] = $row->count;
$counts['standard'] -= $row->count;
foreach ( $post_format_counts as $row ) {
$counts[ $post_formats_term_ids[ $row->post_format_id ] ] = $row->count;
$counts['standard'] -= $row->count;
}
}

$counts = array_filter( $counts );
Expand Down

0 comments on commit 4d54e1c

Please sign in to comment.