Skip to content

Commit 1ccf0ba

Browse files
authored
Hide sticky posts that are not public (#969)
* hide sticky posts that are not public * remove value * check if meta does not exist * also ignore for outbox query! * Delete `activitypub_content_visibility` when updated to an empty value. * hide only local posts in outbox * add unit tests
1 parent 1b7a138 commit 1ccf0ba

5 files changed

+104
-0
lines changed

includes/class-activitypub.php

+17
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public static function init() {
5050

5151
\add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 );
5252

53+
\add_action( 'updated_postmeta', array( self::class, 'updated_postmeta' ), 10, 4 );
54+
5355
// Register several post_types.
5456
self::register_post_types();
5557
}
@@ -567,4 +569,19 @@ public static function user_register( $user_id ) {
567569
$user->add_cap( 'activitypub' );
568570
}
569571
}
572+
573+
/**
574+
* Delete `activitypub_content_visibility` when updated to an empty value.
575+
*
576+
* @param int $meta_id ID of updated metadata entry.
577+
* @param int $object_id Post ID.
578+
* @param string $meta_key Metadata key.
579+
* @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value
580+
* if the value is an array, an object, or itself a PHP-serialized string.
581+
*/
582+
public static function updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value ) {
583+
if ( 'activitypub_content_visibility' === $meta_key && empty( $meta_value ) ) {
584+
\delete_post_meta( $object_id, 'activitypub_content_visibility' );
585+
}
586+
}
570587
}

includes/class-migration.php

+8
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ public static function migrate_to_4_1_0() {
355355
if ( ! $object_type ) {
356356
\update_option( 'activitypub_object_type', 'note' );
357357
}
358+
359+
// Clean up empty visibility meta.
360+
global $wpdb;
361+
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
362+
"DELETE FROM $wpdb->postmeta
363+
WHERE meta_key = 'activitypub_content_visibility'
364+
AND (meta_value IS NULL OR meta_value = '')"
365+
);
358366
}
359367

360368
/**

includes/rest/class-collection.php

+8
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,19 @@ public static function featured_get( $request ) {
217217
if ( ! is_single_user() && User_Collection::BLOG_USER_ID === $user->get__id() ) {
218218
$posts = array();
219219
} elseif ( $sticky_posts && is_array( $sticky_posts ) ) {
220+
// only show public posts.
220221
$args = array(
221222
'post__in' => $sticky_posts,
222223
'ignore_sticky_posts' => 1,
223224
'orderby' => 'date',
224225
'order' => 'DESC',
226+
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
227+
'meta_query' => array(
228+
array(
229+
'key' => 'activitypub_content_visibility',
230+
'compare' => 'NOT EXISTS',
231+
),
232+
),
225233
);
226234

227235
if ( $user->get__id() > 0 ) {

includes/rest/class-outbox.php

+13
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ public static function user_outbox_get( $request ) {
114114
'author' => $user_id > 0 ? $user_id : null,
115115
'paged' => $page,
116116
'post_type' => $post_types,
117+
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
118+
'meta_query' => array(
119+
'relation' => 'OR',
120+
array(
121+
'key' => 'activitypub_content_visibility',
122+
'compare' => 'NOT EXISTS',
123+
),
124+
array(
125+
'key' => 'activitypub_content_visibility',
126+
'value' => ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL,
127+
'compare' => '!=',
128+
),
129+
),
117130
)
118131
);
119132

tests/test-class-activitypub-migrate.php

+58
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,45 @@ public function test_migrate_actor_mode() {
5656
}
5757

5858
public function test_migrate_to_4_1_0() {
59+
$post1 = \wp_insert_post(
60+
array(
61+
'post_author' => 1,
62+
'post_content' => 'activitypub_content_visibility test',
63+
)
64+
);
65+
66+
$post2 = \wp_insert_post(
67+
array(
68+
'post_author' => 1,
69+
'post_content' => 'activitypub_content_visibility test',
70+
)
71+
);
72+
73+
\update_post_meta( $post1, 'activitypub_content_visibility', '' );
74+
\update_post_meta( $post1, 'activitypub_content_123', '456' );
75+
\update_post_meta( $post2, 'activitypub_content_visibility', 'local' );
76+
\update_post_meta( $post2, 'activitypub_content_123', '' );
77+
78+
$metas1 = \get_post_meta( $post1 );
79+
80+
$this->assertEquals(
81+
array(
82+
'activitypub_content_visibility' => array( '' ),
83+
'activitypub_content_123' => array( '456' ),
84+
),
85+
$metas1
86+
);
87+
88+
$metas2 = \get_post_meta( $post2 );
89+
90+
$this->assertEquals(
91+
array(
92+
'activitypub_content_visibility' => array( 'local' ),
93+
'activitypub_content_123' => array( '' ),
94+
),
95+
$metas2
96+
);
97+
5998
$template = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
6099
$object_type = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
61100

@@ -66,6 +105,25 @@ public function test_migrate_to_4_1_0() {
66105

67106
\Activitypub\Migration::migrate_to_4_1_0();
68107

108+
\clean_post_cache( $post1 );
109+
$metas1 = \get_post_meta( $post1 );
110+
$this->assertEquals(
111+
array(
112+
'activitypub_content_123' => array( '456' ),
113+
),
114+
$metas1
115+
);
116+
117+
\clean_post_cache( $post2 );
118+
$metas2 = \get_post_meta( $post2 );
119+
$this->assertEquals(
120+
array(
121+
'activitypub_content_visibility' => array( 'local' ),
122+
'activitypub_content_123' => array( '' ),
123+
),
124+
$metas2
125+
);
126+
69127
$template = \get_option( 'activitypub_custom_post_content' );
70128
$content_type = \get_option( 'activitypub_post_content_type' );
71129
$object_type = \get_option( 'activitypub_object_type' );

0 commit comments

Comments
 (0)