Skip to content

Commit 47bc1df

Browse files
authored
Post meta: Update meta keys to be private (#1090)
* Post meta: Update meta keys to be private WordPress convention states that meta keys prefixed with an underscore are considered private and hidden from the custom fields UI. Since our meta keys are for internal plugin use only, they should follow this convention. This change helps prevent accidental modification of ActivityPub meta data through WordPress's custom fields interface while maintaining the expected pattern for plugin developers. See: https://developer.wordpress.org/plugins/metadata/managing-post-metadata/#hidden-custom-fields * Add changelog * Fix phpcs * Don't make activitypub_content_visibility private * Add missing bits * Also remove content_warning Needs to be editable in editor * update phpcs
1 parent 3bd67d7 commit 47bc1df

File tree

12 files changed

+144
-43
lines changed

12 files changed

+144
-43
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
* Added a filter to make custom comment types manageable in WP.com Calypso
1313

14+
### Changed
15+
16+
* Hide ActivityPub post meta keys from the custom Fields UI
17+
1418
### Fixed
1519

1620
* Undefined array key warnings in various places

includes/class-activitypub.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public static function get_avatar_url( $comment ) {
339339
public static function trash_post( $post_id ) {
340340
\add_post_meta(
341341
$post_id,
342-
'activitypub_canonical_url',
342+
'_activitypub_canonical_url',
343343
\get_permalink( $post_id ),
344344
true
345345
);
@@ -351,7 +351,7 @@ public static function trash_post( $post_id ) {
351351
* @param string $post_id The Post ID.
352352
*/
353353
public static function untrash_post( $post_id ) {
354-
\delete_post_meta( $post_id, 'activitypub_canonical_url' );
354+
\delete_post_meta( $post_id, '_activitypub_canonical_url' );
355355
}
356356

357357
/**
@@ -482,7 +482,7 @@ private static function register_post_types() {
482482

483483
\register_post_meta(
484484
Followers::POST_TYPE,
485-
'activitypub_inbox',
485+
'_activitypub_inbox',
486486
array(
487487
'type' => 'string',
488488
'single' => true,
@@ -492,7 +492,7 @@ private static function register_post_types() {
492492

493493
\register_post_meta(
494494
Followers::POST_TYPE,
495-
'activitypub_errors',
495+
'_activitypub_errors',
496496
array(
497497
'type' => 'string',
498498
'single' => false,
@@ -508,7 +508,7 @@ private static function register_post_types() {
508508

509509
\register_post_meta(
510510
Followers::POST_TYPE,
511-
'activitypub_user_id',
511+
'_activitypub_user_id',
512512
array(
513513
'type' => 'string',
514514
'single' => false,
@@ -520,7 +520,7 @@ private static function register_post_types() {
520520

521521
\register_post_meta(
522522
Followers::POST_TYPE,
523-
'activitypub_actor_json',
523+
'_activitypub_actor_json',
524524
array(
525525
'type' => 'string',
526526
'single' => true,

includes/class-migration.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ public static function maybe_migrate() {
161161
if ( \version_compare( $version_from_db, '4.5.0', '<' ) ) {
162162
\wp_schedule_single_event( \time() + MINUTE_IN_SECONDS, 'activitypub_update_comment_counts' );
163163
}
164+
if ( \version_compare( $version_from_db, '4.6.0', '<' ) ) {
165+
self::migrate_to_4_6_0();
166+
}
164167

165168
/**
166169
* Fires when the system has to be migrated.
@@ -387,6 +390,26 @@ public static function migrate_to_4_1_0() {
387390
);
388391
}
389392

393+
/**
394+
* Updates post meta keys to be prefixed with an underscore.
395+
*/
396+
public static function migrate_to_4_6_0() {
397+
global $wpdb;
398+
399+
$meta_keys = array(
400+
'activitypub_actor_json',
401+
'activitypub_canonical_url',
402+
'activitypub_errors',
403+
'activitypub_inbox',
404+
'activitypub_user_id',
405+
);
406+
407+
foreach ( $meta_keys as $meta_key ) {
408+
// phpcs:ignore WordPress.DB
409+
$wpdb->update( $wpdb->postmeta, array( 'meta_key' => '_' . $meta_key ), array( 'meta_key' => $meta_key ) );
410+
}
411+
}
412+
390413
/**
391414
* Update comment counts for posts in batches.
392415
*

includes/collection/class-actors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static function get_by_username( $username ) {
100100
'meta_query' => array(
101101
'relation' => 'OR',
102102
array(
103-
'key' => 'activitypub_user_identifier',
103+
'key' => '_activitypub_user_identifier',
104104
'value' => $username,
105105
'compare' => 'LIKE',
106106
),

includes/collection/class-followers.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ public static function add_follower( $user_id, $actor ) {
5252
return $id;
5353
}
5454

55-
$post_meta = get_post_meta( $id, 'activitypub_user_id', false );
55+
$post_meta = get_post_meta( $id, '_activitypub_user_id', false );
5656

5757
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
5858
if ( is_array( $post_meta ) && ! in_array( $user_id, $post_meta ) ) {
59-
add_post_meta( $id, 'activitypub_user_id', $user_id );
59+
add_post_meta( $id, '_activitypub_user_id', $user_id );
6060
wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
6161
}
6262

@@ -89,7 +89,7 @@ public static function remove_follower( $user_id, $actor ) {
8989
*/
9090
do_action( 'activitypub_followers_pre_remove_follower', $follower, $user_id, $actor );
9191

92-
return delete_post_meta( $follower->get__id(), 'activitypub_user_id', $user_id );
92+
return delete_post_meta( $follower->get__id(), '_activitypub_user_id', $user_id );
9393
}
9494

9595
/**
@@ -106,7 +106,7 @@ public static function get_follower( $user_id, $actor ) {
106106
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
107107
$post_id = $wpdb->get_var(
108108
$wpdb->prepare(
109-
"SELECT DISTINCT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id WHERE p.post_type = %s AND pm.meta_key = 'activitypub_user_id' AND pm.meta_value = %d AND p.guid = %s",
109+
"SELECT DISTINCT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id WHERE p.post_type = %s AND pm.meta_key = '_activitypub_user_id' AND pm.meta_value = %d AND p.guid = %s",
110110
array(
111111
esc_sql( self::POST_TYPE ),
112112
esc_sql( $user_id ),
@@ -188,7 +188,7 @@ public static function get_followers_with_count( $user_id, $number = -1, $page =
188188
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
189189
'meta_query' => array(
190190
array(
191-
'key' => 'activitypub_user_id',
191+
'key' => '_activitypub_user_id',
192192
'value' => $user_id,
193193
),
194194
),
@@ -219,11 +219,11 @@ public static function get_all_followers() {
219219
'meta_query' => array(
220220
'relation' => 'AND',
221221
array(
222-
'key' => 'activitypub_inbox',
222+
'key' => '_activitypub_inbox',
223223
'compare' => 'EXISTS',
224224
),
225225
array(
226-
'key' => 'activitypub_actor_json',
226+
'key' => '_activitypub_actor_json',
227227
'compare' => 'EXISTS',
228228
),
229229
),
@@ -247,15 +247,15 @@ public static function count_followers( $user_id ) {
247247
'meta_query' => array(
248248
'relation' => 'AND',
249249
array(
250-
'key' => 'activitypub_user_id',
250+
'key' => '_activitypub_user_id',
251251
'value' => $user_id,
252252
),
253253
array(
254-
'key' => 'activitypub_inbox',
254+
'key' => '_activitypub_inbox',
255255
'compare' => 'EXISTS',
256256
),
257257
array(
258-
'key' => 'activitypub_actor_json',
258+
'key' => '_activitypub_actor_json',
259259
'compare' => 'EXISTS',
260260
),
261261
),
@@ -290,15 +290,15 @@ public static function get_inboxes( $user_id ) {
290290
'meta_query' => array(
291291
'relation' => 'AND',
292292
array(
293-
'key' => 'activitypub_inbox',
293+
'key' => '_activitypub_inbox',
294294
'compare' => 'EXISTS',
295295
),
296296
array(
297-
'key' => 'activitypub_user_id',
297+
'key' => '_activitypub_user_id',
298298
'value' => $user_id,
299299
),
300300
array(
301-
'key' => 'activitypub_inbox',
301+
'key' => '_activitypub_inbox',
302302
'value' => '',
303303
'compare' => '!=',
304304
),
@@ -318,7 +318,7 @@ public static function get_inboxes( $user_id ) {
318318
$wpdb->prepare(
319319
"SELECT DISTINCT meta_value FROM {$wpdb->postmeta}
320320
WHERE post_id IN (" . implode( ', ', array_fill( 0, count( $posts ), '%d' ) ) . ")
321-
AND meta_key = 'activitypub_inbox'
321+
AND meta_key = '_activitypub_inbox'
322322
AND meta_value IS NOT NULL",
323323
$posts
324324
)
@@ -378,24 +378,24 @@ public static function get_faulty_followers( $number = 20 ) {
378378
'meta_query' => array(
379379
'relation' => 'OR',
380380
array(
381-
'key' => 'activitypub_errors',
381+
'key' => '_activitypub_errors',
382382
'compare' => 'EXISTS',
383383
),
384384
array(
385-
'key' => 'activitypub_inbox',
385+
'key' => '_activitypub_inbox',
386386
'compare' => 'NOT EXISTS',
387387
),
388388
array(
389-
'key' => 'activitypub_actor_json',
389+
'key' => '_activitypub_actor_json',
390390
'compare' => 'NOT EXISTS',
391391
),
392392
array(
393-
'key' => 'activitypub_inbox',
393+
'key' => '_activitypub_inbox',
394394
'value' => '',
395395
'compare' => '=',
396396
),
397397
array(
398-
'key' => 'activitypub_actor_json',
398+
'key' => '_activitypub_actor_json',
399399
'value' => '',
400400
'compare' => '=',
401401
),
@@ -437,7 +437,7 @@ public static function add_error( $post_id, $error ) {
437437

438438
return add_post_meta(
439439
$post_id,
440-
'activitypub_errors',
440+
'_activitypub_errors',
441441
$error_message
442442
);
443443
}

includes/model/class-follower.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Follower extends Actor {
3636
* @return mixed
3737
*/
3838
public function get_errors() {
39-
return get_post_meta( $this->_id, 'activitypub_errors', false );
39+
return get_post_meta( $this->_id, '_activitypub_errors', false );
4040
}
4141

4242
/**
@@ -72,7 +72,7 @@ public function get_url() {
7272
* Reset (delete) all errors.
7373
*/
7474
public function reset_errors() {
75-
delete_post_meta( $this->_id, 'activitypub_errors' );
75+
delete_post_meta( $this->_id, '_activitypub_errors' );
7676
}
7777

7878
/**
@@ -216,9 +216,9 @@ public function delete() {
216216
* Update the post meta.
217217
*/
218218
protected function get_post_meta_input() {
219-
$meta_input = array();
220-
$meta_input['activitypub_inbox'] = $this->get_shared_inbox();
221-
$meta_input['activitypub_actor_json'] = $this->to_json();
219+
$meta_input = array();
220+
$meta_input['_activitypub_inbox'] = $this->get_shared_inbox();
221+
$meta_input['_activitypub_actor_json'] = $this->to_json();
222222

223223
return $meta_input;
224224
}
@@ -334,7 +334,7 @@ public function get_shared_inbox() {
334334
* @return \Activitypub\Activity\Base_Object|WP_Error
335335
*/
336336
public static function init_from_cpt( $post ) {
337-
$actor_json = get_post_meta( $post->ID, 'activitypub_actor_json', true );
337+
$actor_json = get_post_meta( $post->ID, '_activitypub_actor_json', true );
338338
$object = self::init_from_json( $actor_json );
339339
$object->set__id( $post->ID );
340340
$object->set_id( $post->guid );

includes/transformer/class-post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function get_url() {
152152

153153
switch ( \get_post_status( $post ) ) {
154154
case 'trash':
155-
$permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true );
155+
$permalink = \get_post_meta( $post->ID, '_activitypub_canonical_url', true );
156156
break;
157157
case 'draft':
158158
// Get_sample_permalink is in wp-admin, not always loaded.

integration/class-jetpack.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public static function add_sync_meta( $allow_list ) {
3535
return $allow_list;
3636
}
3737
$activitypub_meta_keys = array(
38-
'activitypub_user_id',
39-
'activitypub_inbox',
40-
'activitypub_actor_json',
38+
'_activitypub_user_id',
39+
'_activitypub_inbox',
40+
'_activitypub_actor_json',
4141
);
4242
return \array_merge( $allow_list, $activitypub_meta_keys );
4343
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ For reasons of data protection, it is not possible to see the followers of other
135135
= Unreleased =
136136

137137
* Added: A filter to make custom comment types manageable in WP.com Calypso
138+
* Changed: Hide ActivityPub post meta keys from the custom Fields UI
138139
* Fixed: Undefined array key warnings in various places
139140

140141
= 4.6.0 =

0 commit comments

Comments
 (0)