Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra fields #762

Merged
merged 42 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
39b311d
add extra fields settings
pfefferle May 16, 2024
d3228ca
add settings
pfefferle May 17, 2024
2af121c
add settings link
pfefferle May 17, 2024
6dafa00
allow users to only edit their extra fields
pfefferle May 17, 2024
48ba88c
Merge branch 'master' into update/PropertyValue
pfefferle May 17, 2024
7427951
Merge branch 'master' into update/PropertyValue
pfefferle May 21, 2024
de79497
Merge branch 'master' into update/PropertyValue
mattwiebe May 30, 2024
57ea0df
use nopaging instead of posts_per_page
pfefferle May 30, 2024
b6cb155
load filters only on admin page and remove the view actions
pfefferle May 31, 2024
38504d2
Merge branch 'master' into update/PropertyValue
pfefferle May 31, 2024
3fc673b
author should not be editable
pfefferle May 31, 2024
d3c877b
Merge branch 'update/PropertyValue' of https://github.com/Automattic/…
pfefferle May 31, 2024
bd93f4b
Merge branch 'master' into update/PropertyValue
pfefferle Jun 1, 2024
eda00c9
Merge branch 'master' into update/PropertyValue
pfefferle Jun 4, 2024
966e815
Merge branch 'master' into update/PropertyValue
pfefferle Jun 5, 2024
171fb79
Merge branch 'master' into update/PropertyValue
pfefferle Jun 7, 2024
4eba82b
Merge branch 'master' into update/PropertyValue
pfefferle Jul 1, 2024
0469b13
Merge branch 'master' into update/PropertyValue
pfefferle Jul 1, 2024
8d37095
Merge branch 'master' into update/PropertyValue
pfefferle Jul 2, 2024
6d869d7
Merge branch 'master' into update/PropertyValue
pfefferle Jul 4, 2024
0c6fc35
some improvements and extra informations
pfefferle Jul 4, 2024
1db6909
Merge branch 'update/PropertyValue' of https://github.com/Automattic/…
pfefferle Jul 4, 2024
039bba2
description
pfefferle Jul 5, 2024
2a78c24
use nopaging instead
pfefferle Jul 5, 2024
cfe9a11
add blog url
pfefferle Jul 8, 2024
3ddbdae
formatting
pfefferle Jul 8, 2024
483b136
formatting
pfefferle Jul 8, 2024
6e45ed1
some cosmetics
pfefferle Jul 8, 2024
896d825
return attachments
pfefferle Jul 8, 2024
300c436
formatting
pfefferle Jul 8, 2024
f7d9b7d
formatting
pfefferle Jul 8, 2024
68404f0
add default extra fields
pfefferle Jul 8, 2024
a81513a
Merge branch 'master' into update/PropertyValue
pfefferle Jul 8, 2024
63b0568
remove unused migration
pfefferle Jul 8, 2024
8d4f8b8
Merge branch 'master' into update/PropertyValue
pfefferle Jul 8, 2024
fbbdd97
better defaults handling
pfefferle Jul 9, 2024
8941a01
Merge branch 'master' into update/PropertyValue
pfefferle Jul 9, 2024
6be70ae
use a filter to be able to unhook/replace default extra fields
pfefferle Jul 11, 2024
3024807
Merge branch 'master' into update/PropertyValue
pfefferle Jul 11, 2024
b5c5e2c
Merge branch 'master' into update/PropertyValue
pfefferle Jul 12, 2024
812f4d2
should fix #810
pfefferle Jul 15, 2024
bf894a9
Merge branch 'master' into update/PropertyValue
pfefferle Jul 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions includes/class-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static function init() {

\add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) );

\add_action( 'pre_get_posts', array( self::class, 'filter_get_posts_query' ) );

// register several post_types
self::register_post_types();
}
Expand Down Expand Up @@ -463,13 +465,43 @@ private static function register_post_types() {
)
);

\register_post_type(
'ap_extrafield',
array(
'labels' => array(
'name' => _x( 'Extra fields', 'post_type plural name', 'activitypub' ),
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
'singular_name' => _x( 'Extra field', 'post_type single name', 'activitypub' ),
'add_new' => __( 'Add new', 'activitypub' ),
'add_new_item' => __( 'Add new extra field', 'activitypub' ),
'new_item' => __( 'New extra field', 'activitypub' ),
'edit_item' => __( 'Edit extra field', 'activitypub' ),
'view_item' => __( 'View extra field', 'activitypub' ),
'all_items' => __( 'All extra fields', 'activitypub' ),
),
'public' => false,
'hierarchical' => false,
'query_var' => false,
'has_archive' => false,
'publicly_queryable' => false,
'show_in_menu' => false,
'delete_with_user' => true,
'can_export' => true,
'exclude_from_search' => true,
'show_in_rest' => true,
'map_meta_cap' => true,
'show_ui' => true,
'supports' => array( 'title', 'editor', 'author' ),
)
);

\do_action( 'activitypub_after_register_post_type' );
}

/**
* Add the 'activitypub' query variable so WordPress won't mangle it.
*
* @param int $user_id User ID.
*
* @param array $userdata The raw array of data passed to wp_insert_user().
*/
public static function user_register( $user_id ) {
Expand All @@ -478,4 +510,17 @@ public static function user_register( $user_id ) {
$user->add_cap( 'activitypub' );
}
}

/**
* Filter the query to only show the current author's extra fields
*
* @param WP_Query $query The WP_Query instance (passed by reference).
*
* @return void
*/
public static function filter_get_posts_query( $query ) {
if ( $query->get( 'post_type' ) === 'ap_extrafield' ) {
$query->set( 'author', get_current_user_id() );
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
27 changes: 27 additions & 0 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static function init() {
\add_action( 'admin_menu', array( self::class, 'admin_menu' ) );
\add_action( 'admin_init', array( self::class, 'register_settings' ) );
\add_action( 'load-comment.php', array( self::class, 'edit_comment' ) );
\add_action( 'load-post.php', array( self::class, 'edit_post' ) );
\add_action( 'personal_options_update', array( self::class, 'save_user_description' ) );
\add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_scripts' ) );
\add_action( 'admin_notices', array( self::class, 'admin_notices' ) );
Expand Down Expand Up @@ -345,6 +346,32 @@ function ( $allcaps, $caps, $arg ) {
);
}

public static function edit_post() {
// Disable the edit_post capability for federated posts.
\add_filter(
'user_has_cap',
function ( $allcaps, $caps, $arg ) {
if ( 'edit_post' !== $arg[0] ) {
return $allcaps;
}

$post = get_post( $arg[2] );

if ( 'ap_extrafield' !== $post->post_type ) {
return $allcaps;
}

if ( (int) get_current_user_id() !== (int) $post->post_author ) {
return false;
}

return $allcaps;
},
1,
3
);
}

public static function comment_row_actions( $actions, $comment ) {
if ( was_comment_received( $comment ) ) {
unset( $actions['edit'] );
Expand Down
10 changes: 9 additions & 1 deletion includes/class-scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public static function deregister_schedules() {
public static function schedule_post_activity( $new_status, $old_status, $post ) {
$post = get_post( $post );

if ( 'ap_extrafield' === $post->post_type ) {
self::schedule_profile_update( $post->post_author );
return;
}

// Do not send activities if post is password protected.
if ( \post_password_required( $post ) ) {
return;
Expand Down Expand Up @@ -311,7 +316,9 @@ public static function user_update( $user_id ) {

/**
* Theme mods only have a dynamic filter so we fudge it like this.
* @param mixed $value
*
* @param mixed $value
*
* @return mixed
*/
public static function blog_user_update( $value = null ) {
Expand All @@ -321,6 +328,7 @@ public static function blog_user_update( $value = null ) {

/**
* Send a profile update to all followers. Gets hooked into all relevant options/meta etc.
*
* @param int $user_id The user ID to update (Could be 0 for Blog-User).
*/
public static function schedule_profile_update( $user_id ) {
Expand Down
45 changes: 40 additions & 5 deletions includes/model/class-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,44 @@ public function get_endpoints() {
* @return array The extended User-Output.
*/
public function get_attachment() {
$array = array();
$extra_fields = new WP_Query(
array(
'post_type' => 'ap_extrafield',
'posts_per_page' => -1,
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
'status' => 'publish',
'author' => $this->get__id(),
)
);

if ( $extra_fields->have_posts() ) {
$attachments = array();
foreach ( $extra_fields->posts as $post ) {
$content = \get_the_content( null, false, $post );
$content = \make_clickable( $content );
$content = \do_blocks( $content );
$content = \wptexturize( $content );
$content = \wp_filter_content_tags( $content );
// replace script and style elements
$content = \preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $content );
$content = \strip_shortcodes( $content );
$content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );

$attachments[] = array(
'type' => 'PropertyValue',
'name' => \get_the_title( $post ),
'value' => \html_entity_decode(
$content,
\ENT_QUOTES,
'UTF-8'
),
);
}
return $attachments;
}

$extra_fields = array();

$array[] = array(
$extra_fields[] = array(
'type' => 'PropertyValue',
'name' => \__( 'Blog', 'activitypub' ),
'value' => \html_entity_decode(
Expand All @@ -245,7 +280,7 @@ public function get_attachment() {
),
);

$array[] = array(
$extra_fields[] = array(
'type' => 'PropertyValue',
'name' => \__( 'Profile', 'activitypub' ),
'value' => \html_entity_decode(
Expand All @@ -256,7 +291,7 @@ public function get_attachment() {
);

if ( \get_the_author_meta( 'user_url', $this->get__id() ) ) {
$array[] = array(
$extra_fields[] = array(
'type' => 'PropertyValue',
'name' => \__( 'Website', 'activitypub' ),
'value' => \html_entity_decode(
Expand All @@ -267,7 +302,7 @@ public function get_attachment() {
);
}

return $array;
return $extra_fields;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions templates/user-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,35 @@
</td>
<?php wp_nonce_field( 'activitypub-user-description', '_apnonce' ); ?>
</tr>
<tr scope="row">
<th>
<label><?php \esc_html_e( 'Extra fields', 'activitypub' ); ?></label>
</th>
<td>
<p class="description"><?php \esc_html_e( 'Your homepage, social profiles, pronouns, age, anything you want.', 'activitypub' ); ?></p>
<?php
$extra_fields = new WP_Query(
array(
'post_type' => 'ap_extrafield',
'posts_per_page' => -1,
'status' => 'publish',
'author' => get_current_user_id(),
)
);

foreach ( $extra_fields->posts as $extra_field ) {
?>
<p>
<input class="regular-text" type="text" size="5" value="<?php echo \esc_html( $extra_field->post_title ); ?>" disabled />
<input class="regular-text" type="text" value="<?php echo \wp_kses_post( \get_the_excerpt( $extra_field ) ); ?>" disabled />
<a href="<?php echo \esc_url( \get_edit_post_link( $extra_field->ID ) ); ?>"><?php \esc_html_e( 'Edit', 'activitypub' ); ?></a>
</p>
<?php } ?>
<p>
<a href="<?php echo esc_url( admin_url( '/post-new.php?post_type=ap_extrafield' ) ); ?>" class="button"><?php esc_html_e( 'Add new', 'activitypub' ); ?></a>
<a href="<?php echo esc_url( admin_url( '/edit.php?post_type=ap_extrafield' ) ); ?>"><?php esc_html_e( 'Manage all', 'activitypub' ); ?></a>
</p>
</td>
</tr>
</tbody>
</table>
Loading