From 9894108bad62e8eff9dc9b0f51436030dbf2d71e Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Mon, 11 Mar 2024 04:50:49 +0100 Subject: [PATCH 1/6] Move settings into friendships and import/export --- includes/class-admin.php | 309 ++++++++++++-------- templates/admin/import-export.php | 61 ++++ templates/admin/notification-manager.php | 84 ++++-- templates/admin/settings-header.php | 8 +- templates/admin/settings-wp-friendships.php | 181 ++++++++++++ templates/admin/settings.php | 221 +------------- 6 files changed, 494 insertions(+), 370 deletions(-) create mode 100644 templates/admin/import-export.php create mode 100644 templates/admin/settings-wp-friendships.php diff --git a/includes/class-admin.php b/includes/class-admin.php index fd9d5099..85bc8e27 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -126,6 +126,10 @@ public function admin_menu() { add_action( 'load-' . $page_type . '_page_friends-page', array( $this, 'redirect_to_friends_page' ) ); add_submenu_page( 'friends', __( 'Notification Manager', 'friends' ), __( 'Notification Manager', 'friends' ), $required_role, 'friends-notification-manager', array( $this, 'render_admin_notification_manager' ) ); add_action( 'load-' . $page_type . '_page_friends-notification-manager', array( $this, 'process_admin_notification_manager' ) ); + add_submenu_page( 'friends', __( 'Friendships', 'friends' ), __( 'Friendships', 'friends' ), $required_role, 'friends-wp-friendships', array( $this, 'render_admin_wp_friendship_settings' ) ); + add_action( 'load-' . $page_type . '_page_friends-wp-friendships', array( $this, 'process_admin_wp_friendship_settings' ) ); + add_submenu_page( 'friends', __( 'Import/Export', 'friends' ), __( 'Import/Export', 'friends' ), $required_role, 'friends-import-export', array( $this, 'render_admin_import_export' ) ); + add_action( 'load-' . $page_type . '_page_friends-import-export', array( $this, 'process_admin_import_export' ) ); add_submenu_page( 'friends', __( 'Add New Friend', 'friends' ), __( 'Add New Friend', 'friends' ), $required_role, 'add-friend', array( $this, 'render_admin_add_friend' ) ); add_action( 'load-' . $page_type . '_page_friends-settings', array( $this, 'process_admin_settings' ) ); @@ -528,61 +532,11 @@ public function process_admin_settings() { } else { delete_option( 'friends_limit_homepage_post_format' ); } - - if ( isset( $_POST['main_user_id'] ) && is_numeric( $_POST['main_user_id'] ) ) { - update_option( 'friends_main_user_id', intval( $_POST['main_user_id'] ) ); - } else { - $main_user_id = Friends::get_main_friend_user_id(); - $main_user_id_exists = false; - $users = User_Query::all_admin_users(); - foreach ( $users->get_results() as $user ) { - if ( $user->ID === $main_user_id ) { - $main_user_id_exists = true; - break; - } - } - if ( ! $main_user_id_exists ) { - // Reset the main user id. - delete_option( 'friends_main_user_id' ); - Friends::get_main_friend_user_id(); - } - } - - if ( isset( $_POST['comment_registration'] ) && $_POST['comment_registration'] ) { - update_option( 'comment_registration', true ); - } else { - delete_option( 'comment_registration' ); - } - if ( isset( $_POST['blocks_everywhere'] ) && $_POST['blocks_everywhere'] ) { update_user_option( get_current_user_id(), 'friends_blocks_everywhere', 1 ); } else { delete_user_option( get_current_user_id(), 'friends_blocks_everywhere' ); } - - if ( isset( $_POST['comment_registration_message'] ) && $_POST['comment_registration_message'] ) { - update_option( 'friends_comment_registration_message', $_POST['comment_registration_message'] ); - } else { - delete_option( 'friends_comment_registration_message' ); - } - } - - if ( isset( $_POST['require_codeword'] ) && $_POST['require_codeword'] ) { - update_option( 'friends_require_codeword', true ); - } else { - delete_option( 'friends_require_codeword' ); - } - - if ( isset( $_POST['codeword'] ) && $_POST['codeword'] ) { - update_option( 'friends_codeword', $_POST['codeword'] ); - } else { - delete_option( 'friends_codeword' ); - } - - if ( isset( $_POST['wrong_codeword_message'] ) && $_POST['wrong_codeword_message'] ) { - update_option( 'friends_wrong_codeword_message', $_POST['wrong_codeword_message'] ); - } else { - delete_option( 'friends_wrong_codeword_message' ); } if ( isset( $_POST['available_emojis'] ) && $_POST['available_emojis'] ) { @@ -598,29 +552,6 @@ public function process_admin_settings() { delete_option( 'friends_selected_emojis' ); } - if ( isset( $_POST['notification_keywords'] ) && $_POST['notification_keywords'] ) { - $keywords = array(); - foreach ( $_POST['notification_keywords'] as $i => $keyword ) { - if ( trim( $keyword ) ) { - $keywords[] = array( - 'enabled' => isset( $_POST['notification_keywords_enabled'][ $i ] ) && $_POST['notification_keywords_enabled'][ $i ], - 'keyword' => $keyword, - ); - } - } - update_option( 'friends_notification_keywords', $keywords ); - } - - if ( isset( $_POST['default_role'] ) && in_array( $_POST['default_role'], array( 'friend', 'acquaintance' ), true ) ) { - update_option( 'friends_default_friend_role', $_POST['default_role'] ); - } - - if ( isset( $_POST['new_post_notification'] ) && $_POST['new_post_notification'] ) { - delete_user_option( get_current_user_id(), 'friends_no_new_post_notification' ); - } else { - update_user_option( get_current_user_id(), 'friends_no_new_post_notification', 1 ); - } - // Global retention. $retention_number_enabled = boolval( isset( $_POST['friends_enable_retention_number'] ) && $_POST['friends_enable_retention_number'] ); update_option( 'friends_enable_retention_number', $retention_number_enabled ); @@ -710,24 +641,6 @@ public function render_admin_settings() { get_frontend_locale() ); - // Now while loading the next translations we need to ensure that determine_locale() doesn't return the admin language but the frontend language. - add_filter( 'pre_determine_locale', array( $this, 'get_frontend_locale' ) ); - - $wrong_codeword_message = __( 'An invalid codeword was provided.', 'friends' ); - $comment_registration_message = __( 'Only people in my network can comment.', 'friends' ); - $my_network = __( 'my network', 'friends' ); - $comment_registration_default = strip_tags( - /* translators: %s: Login URL. */ - __( 'You must be logged in to post a comment.' ) // phpcs:ignore WordPress.WP.I18n.MissingArgDomain - ); - // Now let's switch back to the admin language. - remove_filter( 'pre_determine_locale', array( $this, 'get_frontend_locale' ) ); - restore_previous_locale(); $post_stats = Friends::get_post_stats(); Friends::template_loader()->get_template_part( @@ -736,32 +649,17 @@ public function render_admin_settings() { array_merge( Friends::get_post_stats(), array( - 'potential_main_users' => User_Query::all_admin_users(), - 'main_user_id' => Friends::get_main_friend_user_id(), - 'friend_roles' => $this->get_friend_roles(), - 'default_role' => get_option( 'friends_default_friend_role', 'friend' ), - 'force_enable_post_formats' => get_option( 'friends_force_enable_post_formats' ), - 'post_format_strings' => get_post_format_strings(), - 'limit_homepage_post_format' => get_option( 'friends_limit_homepage_post_format', false ), - 'expose_post_format_feeds' => get_option( 'friends_expose_post_format_feeds' ), - 'private_rss_key' => get_option( 'friends_private_rss_key' ), - 'comment_registration' => get_option( 'comment_registration' ), // WordPress option. - 'comment_registration_message' => get_option( 'friends_comment_registration_message', $comment_registration_message ), - 'comment_registration_default' => $comment_registration_default, - 'my_network' => $my_network, - 'public_profile_link' => home_url( '/friends/' ), - 'codeword' => get_option( 'friends_codeword', 'friends' ), - 'require_codeword' => get_option( 'friends_require_codeword' ), - 'wrong_codeword_message' => get_option( 'friends_wrong_codeword_message', $wrong_codeword_message ), - 'no_friend_request_notification' => get_user_option( 'friends_no_friend_request_notification' ), - 'no_new_post_notification' => get_user_option( 'friends_no_new_post_notification' ), - 'notification_keywords' => Feed::get_all_notification_keywords(), - 'retention_days' => Friends::get_retention_days(), - 'retention_number' => Friends::get_retention_number(), - 'retention_days_enabled' => get_option( 'friends_enable_retention_days' ), - 'retention_number_enabled' => get_option( 'friends_enable_retention_number' ), - 'frontend_default_view' => get_option( 'friends_frontend_default_view', 'expanded' ), - 'blocks_everywhere' => get_user_option( 'friends_blocks_everywhere' ), + 'force_enable_post_formats' => get_option( 'friends_force_enable_post_formats' ), + 'post_format_strings' => get_post_format_strings(), + 'limit_homepage_post_format' => get_option( 'friends_limit_homepage_post_format', false ), + 'expose_post_format_feeds' => get_option( 'friends_expose_post_format_feeds' ), + 'enable_wp_friendships' => get_option( 'friends_enable_wp_friendships' ), + 'retention_days' => Friends::get_retention_days(), + 'retention_number' => Friends::get_retention_number(), + 'retention_days_enabled' => get_option( 'friends_enable_retention_days' ), + 'retention_number_enabled' => get_option( 'friends_enable_retention_number' ), + 'frontend_default_view' => get_option( 'friends_frontend_default_view', 'expanded' ), + 'blocks_everywhere' => get_user_option( 'friends_blocks_everywhere' ), ) ) ); @@ -2185,6 +2083,26 @@ public function process_admin_notification_manager() { } $this->check_admin_settings(); + + if ( isset( $_POST['notification_keywords'] ) && $_POST['notification_keywords'] ) { + $keywords = array(); + foreach ( $_POST['notification_keywords'] as $i => $keyword ) { + if ( trim( $keyword ) ) { + $keywords[] = array( + 'enabled' => isset( $_POST['notification_keywords_enabled'][ $i ] ) && $_POST['notification_keywords_enabled'][ $i ], + 'keyword' => $keyword, + ); + } + } + update_option( 'friends_notification_keywords', $keywords ); + } + + if ( isset( $_POST['new_post_notification'] ) && $_POST['new_post_notification'] ) { + delete_user_option( get_current_user_id(), 'friends_no_new_post_notification' ); + } else { + update_user_option( get_current_user_id(), 'friends_no_new_post_notification', 1 ); + } + $friend_ids = $_POST['friend_listed']; $current_user_id = get_current_user_id(); $hide_from_friends_page = array(); @@ -2194,7 +2112,7 @@ public function process_admin_notification_manager() { $hide_from_friends_page[] = $friend_id; } - $no_new_post_notification = ! isset( $_POST['new_post_notification'][ $friend_id ] ); + $no_new_post_notification = ! isset( $_POST['new_friend_post_notification'][ $friend_id ] ) || '0' === $_POST['new_friend_post_notification'][ $friend_id ]; if ( get_user_option( 'friends_no_new_post_notification_' . $friend_id ) !== $no_new_post_notification ) { update_user_option( $current_user_id, 'friends_no_new_post_notification_' . $friend_id, $no_new_post_notification ); } @@ -2252,18 +2170,161 @@ public function render_admin_notification_manager() { 'admin/notification-manager', null, array( - 'friend_users' => $friend_users->get_results(), - 'friends_settings_url' => add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), self_admin_url( 'admin.php?page=friends-settings' ) ), - 'hide_from_friends_page' => $hide_from_friends_page, - 'no_new_post_notification' => get_user_option( 'friends_no_new_post_notification' ), - 'no_keyword_notification' => get_user_option( 'friends_no_keyword_notification' ), - 'active_keywords' => Feed::get_active_notification_keywords(), + 'friend_users' => $friend_users->get_results(), + 'friends_settings_url' => add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), self_admin_url( 'admin.php?page=friends-settings' ) ), + 'hide_from_friends_page' => $hide_from_friends_page, + 'no_friend_request_notification' => get_user_option( 'friends_no_friend_request_notification' ), + 'no_new_post_notification' => get_user_option( 'friends_no_new_post_notification' ), + 'no_keyword_notification' => get_user_option( 'friends_no_keyword_notification' ), + 'notification_keywords' => Feed::get_all_notification_keywords(), + 'active_keywords' => Feed::get_active_notification_keywords(), ) ); Friends::template_loader()->get_template_part( 'admin/settings-footer' ); } + public function render_admin_wp_friendship_settings() { + Friends::template_loader()->get_template_part( + 'admin/settings-header', + null, + array( + 'active' => 'friends-wp-friendships', + 'title' => __( 'Friends', 'friends' ), + ) + ); + $this->check_admin_settings(); + + // In order to switch to the frontend locale, we need to first pretend that nothing was loaded yet. + global $l10n; + $l10n = array(); + + switch_to_locale( $this->get_frontend_locale() ); + // Now while loading the next translations we need to ensure that determine_locale() doesn't return the admin language but the frontend language. + add_filter( 'pre_determine_locale', array( $this, 'get_frontend_locale' ) ); + + $wrong_codeword_message = __( 'An invalid codeword was provided.', 'friends' ); + $comment_registration_message = __( 'Only people in my network can comment.', 'friends' ); + $my_network = __( 'my network', 'friends' ); + $comment_registration_default = strip_tags( + /* translators: %s: Login URL. */ + __( 'You must be logged in to post a comment.' ) // phpcs:ignore WordPress.WP.I18n.MissingArgDomain + ); + // Now let's switch back to the admin language. + remove_filter( 'pre_determine_locale', array( $this, 'get_frontend_locale' ) ); + restore_previous_locale(); + + ?> +

+ get_template_part( + 'admin/settings-wp-friendships', + null, + array( + 'potential_main_users' => User_Query::all_admin_users(), + 'main_user_id' => Friends::get_main_friend_user_id(), + 'friend_roles' => $this->get_friend_roles(), + 'default_role' => get_option( 'friends_default_friend_role', 'friend' ), + 'comment_registration' => get_option( 'comment_registration' ), // WordPress option. + 'comment_registration_message' => get_option( 'friends_comment_registration_message', $comment_registration_message ), + 'comment_registration_default' => $comment_registration_default, + 'my_network' => $my_network, + 'public_profile_link' => home_url( '/friends/' ), + 'codeword' => get_option( 'friends_codeword', 'friends' ), + 'require_codeword' => get_option( 'friends_require_codeword' ), + 'wrong_codeword_message' => get_option( 'friends_wrong_codeword_message', $wrong_codeword_message ), + ) + ); + + Friends::template_loader()->get_template_part( 'admin/settings-footer' ); + } + public function process_admin_wp_friendship_settings() { + if ( current_user_can( 'manage_options' ) ) { + + if ( isset( $_POST['main_user_id'] ) && is_numeric( $_POST['main_user_id'] ) ) { + update_option( 'friends_main_user_id', intval( $_POST['main_user_id'] ) ); + } else { + $main_user_id = Friends::get_main_friend_user_id(); + $main_user_id_exists = false; + $users = User_Query::all_admin_users(); + foreach ( $users->get_results() as $user ) { + if ( $user->ID === $main_user_id ) { + $main_user_id_exists = true; + break; + } + } + if ( ! $main_user_id_exists ) { + // Reset the main user id. + delete_option( 'friends_main_user_id' ); + Friends::get_main_friend_user_id(); + } + } + + if ( isset( $_POST['require_codeword'] ) && $_POST['require_codeword'] ) { + update_option( 'friends_require_codeword', true ); + } else { + delete_option( 'friends_require_codeword' ); + } + + if ( isset( $_POST['codeword'] ) && $_POST['codeword'] ) { + update_option( 'friends_codeword', $_POST['codeword'] ); + } else { + delete_option( 'friends_codeword' ); + } + + if ( isset( $_POST['wrong_codeword_message'] ) && $_POST['wrong_codeword_message'] ) { + update_option( 'friends_wrong_codeword_message', $_POST['wrong_codeword_message'] ); + } else { + delete_option( 'friends_wrong_codeword_message' ); + } + + if ( isset( $_POST['default_role'] ) && in_array( $_POST['default_role'], array( 'friend', 'acquaintance' ), true ) ) { + update_option( 'friends_default_friend_role', $_POST['default_role'] ); + } + + if ( isset( $_POST['comment_registration'] ) && $_POST['comment_registration'] ) { + update_option( 'comment_registration', true ); + } else { + delete_option( 'comment_registration' ); + } + + if ( isset( $_POST['comment_registration_message'] ) && $_POST['comment_registration_message'] ) { + update_option( 'friends_comment_registration_message', $_POST['comment_registration_message'] ); + } else { + delete_option( 'friends_comment_registration_message' ); + } + } + } + public function render_admin_import_export() { + Friends::template_loader()->get_template_part( + 'admin/settings-header', + null, + array( + 'active' => 'friends-import-export', + 'title' => __( 'Friends', 'friends' ), + ) + ); + $this->check_admin_settings(); + + ?> +

+ get_template_part( + 'admin/import-export', + null, + array( + 'private_rss_key' => get_option( 'friends_private_rss_key' ), + ) + ); + + Friends::template_loader()->get_template_part( 'admin/settings-footer' ); + } + + public function process_admin_import_export() { + } + /** * Gets the friend roles. * diff --git a/templates/admin/import-export.php b/templates/admin/import-export.php new file mode 100644 index 00000000..cf99eee5 --- /dev/null +++ b/templates/admin/import-export.php @@ -0,0 +1,61 @@ + +
+ + + + + + + + + + + +
+ + Private OPML file (contains private urls!) and import it to your feed reader.', 'friends' ), esc_url( home_url( '/friends/opml/?auth=' . $args['private_rss_key'] ) ) ), array( 'a' => array( 'href' => array() ) ) ); + ?> + + + Public OPML file (only public urls).', 'friends' ), esc_url( home_url( '/friends/opml/?public' ) ) ), array( 'a' => array( 'href' => array() ) ) ); + ?> + +

+ +

+
+ + compiled RSS feed of friend posts.', 'friends' ), esc_url( home_url( '/friends/feed/?auth=' . $args['private_rss_key'] ) ) ), array( 'a' => array( 'href' => array() ) ) ); + ?> + +

+ +

+ +
+ +

+ +

+
+ -

- - -

- - - - -

- -
+?> + + + + + + + + + + + +
+ + +
+ +
+ +
+

+
+ + +
+ +
    + + $entry ) : ?> +
  1. + /> + +
  2. + +
+ +
+

+
@@ -53,8 +80,13 @@ user_login, $args['hide_from_friends_page'] ) ); ?> /> - + get_total() > 1 ) : - ?> - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- user_login ) ); ?> + + user_login ) ); ?> /> + + + user_login ) ); ?> /> + user_login ) ); ?> diff --git a/templates/admin/settings-header.php b/templates/admin/settings-header.php index 2c769219..9414f917 100644 --- a/templates/admin/settings-header.php +++ b/templates/admin/settings-header.php @@ -9,9 +9,11 @@ $args['menu'] = apply_filters( 'friends_admin_tabs', array( - __( 'Welcome', 'friends' ) => 'friends', - __( 'Settings' ) => 'friends-settings', // phpcs:ignore WordPress.WP.I18n.MissingArgDomain - __( 'Notification Manager', 'friends' ) => 'friends-notification-manager', + __( 'Welcome', 'friends' ) => 'friends', + __( 'Settings' ) => 'friends-settings', // phpcs:ignore WordPress.WP.I18n.MissingArgDomain + __( 'Friendships', 'friends' ) => 'friends-wp-friendships', + __( 'Notifications', 'friends' ) => 'friends-notification-manager', + __( 'Import/Export', 'friends' ) => 'friends-import-export', ) ); } diff --git a/templates/admin/settings-wp-friendships.php b/templates/admin/settings-wp-friendships.php new file mode 100644 index 00000000..5bed06ad --- /dev/null +++ b/templates/admin/settings-wp-friendships.php @@ -0,0 +1,181 @@ + + + + + + + + + + + get_total() > 1 ) : + ?> + + + + + + + + + + + + + + +
+
+ +
+
+
+ +

+ +

+

+ your public profile page.', 'friends' ), + $args['my_network'], + $args['public_profile_link'] + ), + array( + 'a' => array( + 'href' => array(), + ), + ) + ); + ?> +

+
+
+
+ + +

+ get_results() as $potential_main_user ) { + $c += 1; + if ( $potential_main_user->ID === $args['main_user_id'] ) { + ?> + display_name ); ?> + + ( + + ) +

+ +
+
+ +
+
+
+ +

+ +

+
+
+ +

+ +

+
+
+
+ +

+ + +

+
+ +

+ +

+ +

+ +

    + +
  • + +
+

+ + + @@ -24,184 +14,19 @@
-
-
-
- -

- -

-

- your public profile page.', 'friends' ), - $args['my_network'], - $args['public_profile_link'] - ), - array( - 'a' => array( - 'href' => array(), - ), - ) - ); - ?> -

-
-
- - -

- get_results() as $potential_main_user ) { - $c += 1; - if ( $potential_main_user->ID === $args['main_user_id'] ) { - ?> - display_name ); ?> - - ( - - ) -

- -
-
- -
-
-
- -

- -

-
-
- -

- -

-
-
-
- - -
- -
- -
-

-
- - -
- -
    - - $entry ) : ?> -
  1. - /> - -
  2. - -
- -
-

-
- -

- - -

-
@@ -430,44 +255,6 @@
- - Private OPML file (contains private urls!) and import it to your feed reader.', 'friends' ), esc_url( home_url( '/friends/opml/?auth=' . $args['private_rss_key'] ) ) ), array( 'a' => array( 'href' => array() ) ) ); - ?> - - - Public OPML file (only public urls).', 'friends' ), esc_url( home_url( '/friends/opml/?public' ) ) ), array( 'a' => array( 'href' => array() ) ) ); - ?> - -

- -

-
- - compiled RSS feed of friend posts.', 'friends' ), esc_url( home_url( '/friends/feed/?auth=' . $args['private_rss_key'] ) ) ), array( 'a' => array( 'href' => array() ) ) ); - ?> - -

- -

- -
From 2a2c18f4dd61bc0d297a1dea072eb4bc23d746f9 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Mon, 11 Mar 2024 04:56:44 +0100 Subject: [PATCH 2/6] Store the friendship setting --- includes/class-admin.php | 16 +++++++++++++--- templates/admin/notification-manager.php | 5 +++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/includes/class-admin.php b/includes/class-admin.php index 85bc8e27..fd62e173 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -68,6 +68,7 @@ private function register_hooks() { add_filter( 'site_status_test_php_modules', array( $this, 'site_status_test_php_modules' ) ); add_filter( 'debug_information', array( $this, 'site_health_debug' ) ); add_filter( 'friends_create_and_follow', array( $this, 'create_and_follow' ), 10, 4 ); + add_filter( 'friends_admin_tabs', array( $this, 'maybe_remove_friendship_settings' ) ); if ( ! get_option( 'permalink_structure' ) ) { add_action( 'admin_notices', array( $this, 'admin_notice_unsupported_permalink_structure' ) ); @@ -126,8 +127,10 @@ public function admin_menu() { add_action( 'load-' . $page_type . '_page_friends-page', array( $this, 'redirect_to_friends_page' ) ); add_submenu_page( 'friends', __( 'Notification Manager', 'friends' ), __( 'Notification Manager', 'friends' ), $required_role, 'friends-notification-manager', array( $this, 'render_admin_notification_manager' ) ); add_action( 'load-' . $page_type . '_page_friends-notification-manager', array( $this, 'process_admin_notification_manager' ) ); - add_submenu_page( 'friends', __( 'Friendships', 'friends' ), __( 'Friendships', 'friends' ), $required_role, 'friends-wp-friendships', array( $this, 'render_admin_wp_friendship_settings' ) ); - add_action( 'load-' . $page_type . '_page_friends-wp-friendships', array( $this, 'process_admin_wp_friendship_settings' ) ); + if ( get_option( 'friends_enable_wp_friendships' ) ) { + add_submenu_page( 'friends', __( 'Friendships', 'friends' ), __( 'Friendships', 'friends' ), $required_role, 'friends-wp-friendships', array( $this, 'render_admin_wp_friendship_settings' ) ); + add_action( 'load-' . $page_type . '_page_friends-wp-friendships', array( $this, 'process_admin_wp_friendship_settings' ) ); + } add_submenu_page( 'friends', __( 'Import/Export', 'friends' ), __( 'Import/Export', 'friends' ), $required_role, 'friends-import-export', array( $this, 'render_admin_import_export' ) ); add_action( 'load-' . $page_type . '_page_friends-import-export', array( $this, 'process_admin_import_export' ) ); add_submenu_page( 'friends', __( 'Add New Friend', 'friends' ), __( 'Add New Friend', 'friends' ), $required_role, 'add-friend', array( $this, 'render_admin_add_friend' ) ); @@ -502,7 +505,7 @@ public function process_admin_settings() { } $this->check_admin_settings(); - foreach ( array( 'ignore_incoming_friend_requests' ) as $checkbox ) { + foreach ( array( 'ignore_incoming_friend_requests', 'enable_wp_friendships' ) as $checkbox ) { if ( isset( $_POST[ $checkbox ] ) && $_POST[ $checkbox ] ) { update_option( 'friends_' . $checkbox, true ); } else { @@ -2184,6 +2187,13 @@ public function render_admin_notification_manager() { Friends::template_loader()->get_template_part( 'admin/settings-footer' ); } + public function maybe_remove_friendship_settings( $items ) { + if ( ! get_option( 'friends_enable_wp_friendships' ) ) { + unset( $items[ __( 'Friendships', 'friends' ) ] ); + } + return $items; + } + public function render_admin_wp_friendship_settings() { Friends::template_loader()->get_template_part( 'admin/settings-header', diff --git a/templates/admin/notification-manager.php b/templates/admin/notification-manager.php index 24d6525c..f3ceac5f 100644 --- a/templates/admin/notification-manager.php +++ b/templates/admin/notification-manager.php @@ -59,6 +59,11 @@ +

+ +

+ +

From 5e05cde1badc3370fe17144484822a49efd429f0 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Mon, 11 Mar 2024 05:10:58 +0100 Subject: [PATCH 3/6] Don't show settings menu items when not in settings --- includes/class-access-control.php | 6 ++++++ includes/class-admin.php | 17 ++++++++++------- includes/class-automatic-status.php | 13 +++++-------- templates/admin/select-feeds.php | 2 +- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/includes/class-access-control.php b/includes/class-access-control.php index 854d0300..05aa6353 100644 --- a/includes/class-access-control.php +++ b/includes/class-access-control.php @@ -103,6 +103,9 @@ public static function private_rss_is_authenticated() { * @return int|bool The user id or false. */ public function verify_token( $token, $until, $auth ) { + if ( ! get_option( 'friends_enable_wp_friendships' ) ) { + return false; + } $user_id = get_option( 'friends_in_token_' . $token ); if ( ! $user_id ) { $me = User::get_user( User::get_user_login_for_url( $token ) ); @@ -136,6 +139,9 @@ public function verify_token( $token, $until, $auth ) { * Log in a friend via URL parameter */ public function remote_login() { + if ( ! get_option( 'friends_enable_wp_friendships' ) ) { + return false; + } if ( ! isset( $_GET['friend_auth'] ) ) { return; } diff --git a/includes/class-admin.php b/includes/class-admin.php index fd62e173..327dc34c 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -122,19 +122,22 @@ public function admin_menu() { add_menu_page( 'friends', $menu_title, $required_role, 'friends', null, 'dashicons-groups', 3 ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain add_submenu_page( 'friends', __( 'Home' ), __( 'Home' ), $required_role, 'friends', array( $this, 'render_admin_home' ) ); + add_action( 'load-' . $page_type . '_page_friends-page', array( $this, 'redirect_to_friends_page' ) ); + add_submenu_page( 'friends', __( 'Add New Friend', 'friends' ), __( 'Add New Friend', 'friends' ), $required_role, 'add-friend', array( $this, 'render_admin_add_friend' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain add_submenu_page( 'friends', __( 'Settings' ), __( 'Settings' ), $required_role, 'friends-settings', array( $this, 'render_admin_settings' ) ); - add_action( 'load-' . $page_type . '_page_friends-page', array( $this, 'redirect_to_friends_page' ) ); - add_submenu_page( 'friends', __( 'Notification Manager', 'friends' ), __( 'Notification Manager', 'friends' ), $required_role, 'friends-notification-manager', array( $this, 'render_admin_notification_manager' ) ); + if ( isset( $_GET['page'] ) && in_array( $_GET['page'], apply_filters( 'friends_admin_settings_slugs', array( 'friends-settings', 'friends-notification-manager', 'friends-wp-friendships', 'friends-import-export' ) ) ) ) { + add_submenu_page( 'friends', __( 'Notification Manager', 'friends' ), '- ' . __( 'Notification Manager', 'friends' ), $required_role, 'friends-notification-manager', array( $this, 'render_admin_notification_manager' ) ); + add_submenu_page( 'friends', __( 'Friendships', 'friends' ), '- ' . __( 'Friendships', 'friends' ), $required_role, 'friends-wp-friendships', array( $this, 'render_admin_wp_friendship_settings' ) ); + add_submenu_page( 'friends', __( 'Import/Export', 'friends' ), '- ' . __( 'Import/Export', 'friends' ), $required_role, 'friends-import-export', array( $this, 'render_admin_import_export' ) ); + do_action( 'friends_admin_menu_settings', $page_type ); + } add_action( 'load-' . $page_type . '_page_friends-notification-manager', array( $this, 'process_admin_notification_manager' ) ); + add_action( 'load-' . $page_type . '_page_friends-import-export', array( $this, 'process_admin_import_export' ) ); + add_action( 'load-' . $page_type . '_page_friends-settings', array( $this, 'process_admin_settings' ) ); if ( get_option( 'friends_enable_wp_friendships' ) ) { - add_submenu_page( 'friends', __( 'Friendships', 'friends' ), __( 'Friendships', 'friends' ), $required_role, 'friends-wp-friendships', array( $this, 'render_admin_wp_friendship_settings' ) ); add_action( 'load-' . $page_type . '_page_friends-wp-friendships', array( $this, 'process_admin_wp_friendship_settings' ) ); } - add_submenu_page( 'friends', __( 'Import/Export', 'friends' ), __( 'Import/Export', 'friends' ), $required_role, 'friends-import-export', array( $this, 'render_admin_import_export' ) ); - add_action( 'load-' . $page_type . '_page_friends-import-export', array( $this, 'process_admin_import_export' ) ); - add_submenu_page( 'friends', __( 'Add New Friend', 'friends' ), __( 'Add New Friend', 'friends' ), $required_role, 'add-friend', array( $this, 'render_admin_add_friend' ) ); - add_action( 'load-' . $page_type . '_page_friends-settings', array( $this, 'process_admin_settings' ) ); add_submenu_page( 'friends', __( 'Friends & Requests', 'friends' ), __( 'Friends & Requests', 'friends' ), $required_role, 'friends-list', array( $this, 'render_friends_list' ) ); diff --git a/includes/class-automatic-status.php b/includes/class-automatic-status.php index a3e52faf..c7a8e7f9 100644 --- a/includes/class-automatic-status.php +++ b/includes/class-automatic-status.php @@ -39,7 +39,7 @@ public function __construct( Friends $friends ) { * Register the WordPress hooks */ private function register_hooks() { - add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 ); + add_action( 'friends_admin_menu_settings', array( $this, 'friends_admin_menu_settings' ), 20 ); add_action( 'friends_admin_tabs', array( $this, 'admin_tabs' ), 20 ); add_filter( 'handle_bulk_actions-edit-post', array( $this, 'bulk_publish' ), 10, 3 ); @@ -53,17 +53,14 @@ private function register_hooks() { /** * Add the admin menu to the sidebar. + * + * @param string $page_type The page type. */ - public function admin_menu() { - $unread_badge = $this->friends->admin->get_unread_badge(); - - $menu_title = __( 'Friends', 'friends' ) . $unread_badge; - $page_type = sanitize_title( $menu_title ); - + public function friends_admin_menu_settings( $page_type ) { add_submenu_page( 'friends', __( 'Automatic Status', 'friends' ), - __( 'Automatic Status', 'friends' ), + '- ' . __( 'Automatic Status', 'friends' ), Friends::required_menu_role(), 'friends-auto-status', array( $this, 'validate_drafts' ) diff --git a/templates/admin/select-feeds.php b/templates/admin/select-feeds.php index 657ab9c3..e6f01e3a 100644 --- a/templates/admin/select-feeds.php +++ b/templates/admin/select-feeds.php @@ -111,7 +111,7 @@

- +
From b37145535e6e9d69d727494c2272f2d268412b27 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Mon, 11 Mar 2024 05:16:11 +0100 Subject: [PATCH 4/6] Enable friendships on upgrade if applicable --- includes/class-admin.php | 8 +++++++- includes/class-friends.php | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/includes/class-admin.php b/includes/class-admin.php index 327dc34c..1a634571 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -126,7 +126,13 @@ public function admin_menu() { add_submenu_page( 'friends', __( 'Add New Friend', 'friends' ), __( 'Add New Friend', 'friends' ), $required_role, 'add-friend', array( $this, 'render_admin_add_friend' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain add_submenu_page( 'friends', __( 'Settings' ), __( 'Settings' ), $required_role, 'friends-settings', array( $this, 'render_admin_settings' ) ); - if ( isset( $_GET['page'] ) && in_array( $_GET['page'], apply_filters( 'friends_admin_settings_slugs', array( 'friends-settings', 'friends-notification-manager', 'friends-wp-friendships', 'friends-import-export' ) ) ) ) { + if ( + isset( $_GET['page'] ) && + in_array( + $_GET['page'], + apply_filters( 'friends_admin_settings_slugs', array( 'friends-settings', 'friends-notification-manager', 'friends-wp-friendships', 'friends-import-export' ) ) + ) + ) { add_submenu_page( 'friends', __( 'Notification Manager', 'friends' ), '- ' . __( 'Notification Manager', 'friends' ), $required_role, 'friends-notification-manager', array( $this, 'render_admin_notification_manager' ) ); add_submenu_page( 'friends', __( 'Friendships', 'friends' ), '- ' . __( 'Friendships', 'friends' ), $required_role, 'friends-wp-friendships', array( $this, 'render_admin_wp_friendship_settings' ) ); add_submenu_page( 'friends', __( 'Import/Export', 'friends' ), '- ' . __( 'Import/Export', 'friends' ), $required_role, 'friends-import-export', array( $this, 'render_admin_import_export' ) ); diff --git a/includes/class-friends.php b/includes/class-friends.php index d34711e4..490c99d6 100644 --- a/includes/class-friends.php +++ b/includes/class-friends.php @@ -444,6 +444,16 @@ public static function upgrade_plugin( $upgrader_object, $options ) { } $previous_version = get_option( 'friends_plugin_version' ); + if ( version_compare( $previous_version, '2.9.0', '<' ) ) { + $users = User_Query::all_associated_users(); + foreach ( $users->get_results() as $user ) { + if ( ! ( $user instanceof Subscription ) ) { + // We have a user that is not a virtual user, so the friendship functionality had been used. + update_option( 'friends_enable_wp_friendships', 1 ); + break; + } + } + } if ( version_compare( $previous_version, '0.20.1', '<' ) ) { $users = User_Query::all_associated_users(); foreach ( $users->get_results() as $user ) { From b69709bed216296aaa37087efec784763dc729b4 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Tue, 12 Mar 2024 14:44:39 +0100 Subject: [PATCH 5/6] Fix ActivityPub tests --- tests/test-activitypub.php | 147 +++++++++++++++++++++---------------- 1 file changed, 84 insertions(+), 63 deletions(-) diff --git a/tests/test-activitypub.php b/tests/test-activitypub.php index 58a85a54..d0f78ef0 100644 --- a/tests/test-activitypub.php +++ b/tests/test-activitypub.php @@ -37,38 +37,41 @@ public function test_incoming_post() { $date = gmdate( \DATE_W3C, $now++ ); $id = 'test' . $status_id; $content = 'Test ' . $date . ' ' . wp_rand(); - - $request = new \WP_REST_Request( 'POST', '/activitypub/1.0/users/' . get_current_user_id() . '/inbox' ); - $request->set_param( 'type', 'Create' ); - $request->set_param( 'id', $id ); - $request->set_param( 'actor', $this->actor ); - $attachment_url = 'https://mastodon.local/files/original/1234.png'; $attachment_width = 400; $attachment_height = 600; - $request->set_param( - 'object', - array( - 'type' => 'Note', - 'id' => $id, - 'attributedTo' => $this->actor, - 'content' => $content, - 'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ), - 'published' => $date, - 'attachment' => array( - array( - 'type' => 'Document', - 'mediaType' => 'image/png', - 'url' => $attachment_url, - 'name' => '', - 'blurhash' => '', - 'width' => $attachment_width, - 'height' => $attachment_height, + $request = new \WP_REST_Request( 'POST', '/activitypub/1.0/users/' . get_current_user_id() . '/inbox' ); + $request->set_body( + wp_json_encode( + array( + 'type' => 'Create', + 'id' => $id, + 'actor' => $this->actor, + 'object' => array( + 'type' => 'Note', + 'id' => $id, + 'attributedTo' => $this->actor, + 'content' => $content, + 'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ), + 'published' => $date, + 'attachment' => array( + array( + 'type' => 'Document', + 'mediaType' => 'image/png', + 'url' => $attachment_url, + 'name' => '', + 'blurhash' => '', + 'width' => $attachment_width, + 'height' => $attachment_height, + + ), + ), ), - ), + ) ) ); + $request->set_header( 'Content-type', 'application/json' ); $response = $this->server->dispatch( $request ); $this->assertEquals( 202, $response->get_status() ); @@ -91,21 +94,28 @@ public function test_incoming_post() { $content = 'Test ' . $date . ' ' . wp_rand(); $request = new \WP_REST_Request( 'POST', '/activitypub/1.0/users/' . get_current_user_id() . '/inbox' ); - $request->set_param( 'type', 'Create' ); - $request->set_param( 'id', $id ); - $request->set_param( 'actor', 'https://mastodon.local/@akirk' ); - $request->set_param( - 'object', - array( - 'type' => 'Note', - 'id' => $id, - 'attributedTo' => 'https://mastodon.local/@akirk', - 'content' => $content, - 'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ), - 'published' => $date, + $request->set_body( + wp_json_encode( + array( + 'type' => 'Create', + 'id' => $id, + 'actor' => 'https://mastodon.local/@akirk', + + 'object' => + array( + 'type' => 'Note', + 'id' => $id, + 'attributedTo' => 'https://mastodon.local/@akirk', + 'content' => $content, + 'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ), + 'published' => $date, + ), + ) ) ); + $request->set_header( 'Content-type', 'application/json' ); + $response = $this->server->dispatch( $request ); $this->assertEquals( 202, $response->get_status() ); @@ -143,21 +153,26 @@ public function test_incoming_mention_of_others() { $content = '@abc Test ' . $date . ' ' . wp_rand(); $request = new \WP_REST_Request( 'POST', '/activitypub/1.0/users/' . get_current_user_id() . '/inbox' ); - $request->set_param( 'type', 'Create' ); - $request->set_param( 'id', $id ); - $request->set_param( 'actor', $this->actor ); + $request->set_body( + wp_json_encode( + array( + 'type' => 'Create', + 'id' => $id, + 'actor' => $this->actor, - $request->set_param( - 'object', - array( - 'type' => 'Note', - 'id' => $id, - 'attributedTo' => $this->actor, - 'content' => $content, - 'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ), - 'published' => $date, + 'object' => + array( + 'type' => 'Note', + 'id' => $id, + 'attributedTo' => $this->actor, + 'content' => $content, + 'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ), + 'published' => $date, + ), + ) ) ); + $request->set_header( 'Content-type', 'application/json' ); $response = $this->server->dispatch( $request ); $this->assertEquals( 202, $response->get_status() ); @@ -200,11 +215,18 @@ public function test_incoming_announce() { $object = 'https://notiz.blog/2022/11/14/the-at-protocol/'; $request = new \WP_REST_Request( 'POST', '/activitypub/1.0/users/' . get_current_user_id() . '/inbox' ); - $request->set_param( 'type', 'Announce' ); - $request->set_param( 'id', $id ); - $request->set_param( 'actor', $this->actor ); - $request->set_param( 'published', $date ); - $request->set_param( 'object', $object ); + $request->set_body( + wp_json_encode( + array( + 'type' => 'Announce', + 'id' => $id, + 'actor' => $this->actor, + 'published' => $date, + 'object' => $object, + ) + ) + ); + $request->set_header( 'Content-type', 'application/json' ); $response = $this->server->dispatch( $request ); $this->assertEquals( 202, $response->get_status() ); @@ -230,17 +252,15 @@ public function test_incoming_announce() { public function test_friend_mentions() { add_filter( 'activitypub_cache_possible_friend_mentions', '__return_false' ); - $post = \wp_insert_post( + $post_id = \wp_insert_post( array( 'post_author' => 1, 'post_content' => '@' . sanitize_title( $this->friend_nicename ) . ' hello', ) ); - $activitypub_post = new \Activitypub\Model\Post( $post ); - - $activitypub_activity = new \Activitypub\Model\Activity( 'Create' ); - $activitypub_activity->from_post( $activitypub_post ); + $activitypub_post = new \Activitypub\Transformer\Post( get_post( $post_id ) ); + $object = $activitypub_post->to_object(); $this->assertContains( array( @@ -248,16 +268,16 @@ public function test_friend_mentions() { 'href' => $this->actor, 'name' => '@' . $this->friend_nicename, ), - $activitypub_post->get_tags() + $object->get_tag() ); - $this->assertContains( \get_rest_url( null, '/activitypub/1.0/users/1/followers' ), $activitypub_activity->get_to() ); - $this->assertContains( $this->actor, $activitypub_activity->get_cc() ); + $this->assertContains( \get_rest_url( null, '/activitypub/1.0/users/1/followers' ), $object->get_to() ); + $this->assertContains( $this->actor, $object->get_cc() ); remove_all_filters( 'activitypub_from_post_object' ); remove_all_filters( 'activitypub_cache_possible_friend_mentions' ); - \wp_trash_post( $post ); + \wp_trash_post( $post_id ); } /** @@ -304,6 +324,7 @@ public function set_up() { } parent::set_up(); + add_filter( 'activitypub_defer_signature_verification', '__return_true' ); add_filter( 'pre_get_remote_metadata_by_actor', array( get_called_class(), 'friends_get_activitypub_metadata' ), 10, 2 ); add_filter( 'friends_get_activitypub_metadata', array( get_called_class(), 'friends_get_activitypub_metadata' ), 5, 2 ); From 34c5d9b19039fb4cd5682a057356401e45b94482 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Tue, 12 Mar 2024 14:51:45 +0100 Subject: [PATCH 6/6] Enable friendships for tests --- tests/test-feed.php | 1 + tests/test-messages.php | 1 + tests/test-notifications.php | 1 + 3 files changed, 3 insertions(+) diff --git a/tests/test-feed.php b/tests/test-feed.php index 0652910b..741e388c 100644 --- a/tests/test-feed.php +++ b/tests/test-feed.php @@ -55,6 +55,7 @@ class FeedTest extends \WP_UnitTestCase { */ public function set_up() { parent::set_up(); + update_option( 'friends_enable_wp_friendships', true ); $this->factory->post->create( array( diff --git a/tests/test-messages.php b/tests/test-messages.php index 04e14306..83740534 100644 --- a/tests/test-messages.php +++ b/tests/test-messages.php @@ -37,6 +37,7 @@ class MessagesTest extends Friends_TestCase_Cache_HTTP { */ public function set_up() { parent::set_up(); + update_option( 'friends_enable_wp_friendships', true ); $this->user_id = $this->factory->user->create( array( diff --git a/tests/test-notifications.php b/tests/test-notifications.php index 19db36f7..9195dc86 100644 --- a/tests/test-notifications.php +++ b/tests/test-notifications.php @@ -24,6 +24,7 @@ class NotificationTest extends \WP_UnitTestCase { */ public function set_up() { parent::set_up(); + update_option( 'friends_enable_wp_friendships', true ); $this->factory->post->create( array(