| @@ -39,42 +39,43 @@ | ||
| 39 | 39 | * Register the WordPress hooks |
| 40 | 40 | */ |
| 41 | 41 | private function register_hooks() { |
| 42 | 42 | add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 43 | - add_action( 'friends_own_site_menu_top', array( $this, 'friends_add_menu_open_friend_request' ), 10, 2 ); | |
| 44 | 43 | add_filter( 'users_list_table_query_args', array( $this, 'allow_role_multi_select' ) ); |
| 45 | - add_filter( 'user_row_actions', array( get_called_class(), 'user_row_actions' ), 10, 2 ); | |
| 46 | - add_filter( 'handle_bulk_actions-users', array( $this, 'handle_bulk_friend_request_approval' ), 10, 3 ); | |
| 47 | - add_filter( 'bulk_actions-users', array( $this, 'add_user_bulk_options' ) ); | |
| 48 | - add_filter( 'manage_users_columns', array( $this, 'user_list_columns' ) ); | |
| 49 | - add_filter( 'manage_users_custom_column', array( get_called_class(), 'user_list_custom_column' ), 10, 3 ); | |
| 50 | 44 | add_filter( 'the_title', array( $this, 'override_post_format_title' ), 10, 2 ); |
| 51 | 45 | add_filter( 'get_edit_user_link', array( $this, 'admin_edit_user_link' ), 10, 2 ); |
| 52 | 46 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_friends_menu' ), 39 ); |
| 53 | 47 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_new_content' ), 71 ); |
| 54 | 48 | add_action( 'wp_head', array( $this, 'admin_bar_mobile' ) ); |
| 49 | + add_action( 'admin_head', array( $this, 'admin_bar_mobile' ) ); | |
| 55 | 50 | add_action( 'current_screen', array( $this, 'register_help' ) ); |
| 56 | 51 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 39 ); |
| 57 | 52 | add_action( 'gettext_with_context', array( $this->friends, 'translate_user_role' ), 10, 4 ); |
| 58 | 53 | add_action( 'wp_ajax_friends_preview_rules', array( $this, 'ajax_preview_friend_rules' ) ); |
| 59 | - add_action( 'wp_ajax_friends_refresh_link_token', array( $this, 'ajax_refresh_link_token' ) ); | |
| 60 | 54 | add_action( 'wp_ajax_friends_fetch_feeds', array( $this, 'ajax_fetch_feeds' ) ); |
| 61 | 55 | add_action( 'wp_ajax_friends_set_avatar', array( $this, 'ajax_set_avatar' ) ); |
| 56 | + add_action( 'wp_ajax_friends-refresh-feeds', array( $this, 'ajax_refresh_feeds' ) ); | |
| 57 | + add_action( 'wp_ajax_friends-preview-subscription', array( $this, 'ajax_preview_subscription' ) ); | |
| 58 | + add_action( 'wp_ajax_friends-preview-subscription-feed', array( $this, 'ajax_preview_subscription_feed' ) ); | |
| 59 | + add_action( 'wp_ajax_friends-subscribe-frontend', array( $this, 'ajax_subscribe_frontend' ) ); | |
| 62 | 60 | add_action( 'delete_user_form', array( $this, 'delete_user_form' ), 10, 2 ); |
| 63 | 61 | add_action( 'delete_user', array( $this, 'delete_user' ) ); |
| 64 | 62 | add_action( 'remove_user_from_blog', array( $this, 'delete_user' ) ); |
| 65 | 63 | add_action( 'tool_box', array( $this, 'toolbox_bookmarklets' ) ); |
| 66 | 64 | add_action( 'dashboard_glance_items', array( $this, 'dashboard_glance_items' ) ); |
| 67 | - add_filter( 'site_status_tests', array( $this, 'site_status_tests' ) ); | |
| 65 | + add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widgets' ), 8 ); | |
| 66 | + add_action( 'wp_ajax_friends_dashboard', array( $this, 'ajax_friends_dashboard' ) ); | |
| 68 | 67 | add_filter( 'site_status_test_php_modules', array( $this, 'site_status_test_php_modules' ) ); |
| 69 | - add_filter( 'debug_information', array( $this, 'site_health_debug' ) ); | |
| 70 | 68 | add_filter( 'friends_create_and_follow', array( $this, 'create_and_follow' ), 10, 4 ); |
| 71 | - add_filter( 'friends_admin_tabs', array( $this, 'maybe_remove_friendship_settings' ) ); | |
| 69 | + add_action( 'friends_edit_feed_content_top', array( $this, 'maybe_render_activitypub_inactive_notice' ), 10, 3 ); | |
| 72 | 70 | |
| 73 | 71 | if ( ! get_option( 'permalink_structure' ) ) { |
| 74 | 72 | add_action( 'admin_notices', array( $this, 'admin_notice_unsupported_permalink_structure' ) ); |
| 75 | 73 | } |
| 76 | - add_filter( 'friends_unread_count', array( $this, 'friends_unread_friend_request_count' ) ); | |
| 74 | + if ( get_option( 'friends_welcome_version' ) ) { | |
| 75 | + add_action( 'admin_notices', array( $this, 'admin_notice_welcome' ) ); | |
| 76 | + } | |
| 77 | + add_filter( 'pre_get_posts', array( $this, 'admin_friend_posts_query' ) ); | |
| 77 | 78 | } |
| 78 | 79 | |
| 79 | 80 | /** |
| 80 | 81 | * Display admin notice about an unsupported permalink structure |
| @@ -108,9 +109,9 @@ | ||
| 108 | 109 | /** |
| 109 | 110 | * Registers the admin menus |
| 110 | 111 | */ |
| 111 | 112 | public function admin_menu() { |
| 112 | - if ( isset( $_REQUEST['rerun-activate'] ) && isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'friends-settings' ) ) { | |
| 113 | + if ( isset( $_REQUEST['rerun-activate'] ) && isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'friends-settings' ) ) { | |
| 113 | 114 | Friends::activate_plugin(); |
| 114 | 115 | wp_safe_redirect( add_query_arg( array( 'reran-activation' => 'friends' ), wp_get_referer() ) ); |
| 115 | 116 | exit; |
| 116 | 117 | } |
| @@ -118,63 +119,82 @@ | ||
| 118 | 119 | $unread_badge = $this->get_unread_badge(); |
| 119 | 120 | |
| 120 | 121 | $menu_title = __( 'Friends', 'friends' ) . $unread_badge; |
| 121 | 122 | $page_type = sanitize_title( $menu_title ); |
| 122 | - add_menu_page( 'friends', $menu_title, $required_role, 'friends', null, 'dashicons-groups', 3 ); | |
| 123 | - // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 124 | - add_submenu_page( 'friends', __( 'Home' ), __( 'Home' ), $required_role, 'friends', array( $this, 'render_admin_home' ) ); | |
| 123 | + $current_page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : ''; | |
| 124 | + add_menu_page( __( 'Friends', 'friends' ), $menu_title, $required_role, 'friends', null, 'dashicons-groups', 3 ); | |
| 125 | + add_submenu_page( 'friends', __( 'Friends', 'friends' ), __( 'Home', 'friends' ), $required_role, 'friends', array( $this, 'render_admin_home' ) ); | |
| 125 | 126 | add_action( 'load-' . $page_type . '_page_friends-page', array( $this, 'redirect_to_friends_page' ) ); |
| 126 | - add_submenu_page( 'friends', __( 'Add New Friend', 'friends' ), __( 'Add New Friend', 'friends' ), $required_role, 'add-friend', array( $this, 'render_admin_add_friend' ) ); | |
| 127 | + add_submenu_page( 'friends', __( 'Add Friend', 'friends' ), __( 'Add Friend', 'friends' ), $required_role, 'add-friend', array( $this, 'render_admin_add_friend' ) ); | |
| 127 | 128 | // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 128 | 129 | add_submenu_page( 'friends', __( 'Settings' ), __( 'Settings' ), $required_role, 'friends-settings', array( $this, 'render_admin_settings' ) ); |
| 129 | 130 | if ( |
| 130 | - isset( $_GET['page'] ) && | |
| 131 | 131 | in_array( |
| 132 | - $_GET['page'], | |
| 133 | - apply_filters( 'friends_admin_settings_slugs', array( 'friends-settings', 'friends-notification-manager', 'friends-wp-friendships', 'friends-import-export' ) ) | |
| 132 | + $current_page, | |
| 133 | + apply_filters( 'friends_admin_settings_slugs', array( 'friends-settings', 'friends-notification-manager', 'friends-wp-friendships', 'friends-import-export', 'friends-migrations' ) ) | |
| 134 | 134 | ) |
| 135 | 135 | ) { |
| 136 | - add_submenu_page( 'friends', __( 'Notification Manager', 'friends' ), '- ' . __( 'Notification Manager', 'friends' ), $required_role, 'friends-notification-manager', array( $this, 'render_admin_notification_manager' ) ); | |
| 137 | - add_submenu_page( 'friends', __( 'Friendships', 'friends' ), '- ' . __( 'Friendships', 'friends' ), $required_role, 'friends-wp-friendships', array( $this, 'render_admin_wp_friendship_settings' ) ); | |
| 136 | + add_submenu_page( 'friends', __( 'Notifications', 'friends' ), '- ' . __( 'Notifications', 'friends' ), $required_role, 'friends-notification-manager', array( $this, 'render_admin_notification_manager' ) ); | |
| 138 | 137 | add_submenu_page( 'friends', __( 'Import/Export', 'friends' ), '- ' . __( 'Import/Export', 'friends' ), $required_role, 'friends-import-export', array( $this, 'render_admin_import_export' ) ); |
| 139 | 138 | do_action( 'friends_admin_menu_settings', $page_type ); |
| 140 | 139 | } |
| 140 | + | |
| 141 | + if ( 'friends-migrations' === $current_page && current_user_can( 'manage_options' ) ) { | |
| 142 | + add_submenu_page( 'friends', __( 'Migrations', 'friends' ), __( 'Migrations', 'friends' ), 'manage_options', 'friends-migrations', array( Migration::class, 'render_admin_page' ) ); | |
| 143 | + } | |
| 141 | 144 | add_action( 'load-' . $page_type . '_page_friends-notification-manager', array( $this, 'process_admin_notification_manager' ) ); |
| 142 | 145 | add_action( 'load-' . $page_type . '_page_friends-import-export', array( $this, 'process_admin_import_export' ) ); |
| 143 | 146 | add_action( 'load-' . $page_type . '_page_friends-settings', array( $this, 'process_admin_settings' ) ); |
| 144 | - if ( get_option( 'friends_enable_wp_friendships' ) ) { | |
| 145 | - add_action( 'load-' . $page_type . '_page_friends-wp-friendships', array( $this, 'process_admin_wp_friendship_settings' ) ); | |
| 147 | + | |
| 148 | + if ( | |
| 149 | + isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'friends-refresh' ) && 'friends-refresh' === $current_page | |
| 150 | + ) { | |
| 151 | + add_submenu_page( 'friends', __( 'Refresh', 'friends' ), __( 'Refresh', 'friends' ), $required_role, 'friends-refresh', array( $this, 'admin_refresh_friend_posts' ) ); | |
| 146 | 152 | } |
| 147 | 153 | |
| 148 | - add_submenu_page( 'friends', __( 'Friends & Requests', 'friends' ), __( 'Friends & Requests', 'friends' ), $required_role, 'friends-list', array( $this, 'render_friends_list' ) ); | |
| 154 | + $friend_submenu_items = array( | |
| 155 | + 'edit-friend' => __( 'Edit User', 'friends' ), | |
| 156 | + 'edit-friend-feeds' => __( 'Edit Feeds', 'friends' ), | |
| 157 | + 'edit-friend-notifications' => __( 'Edit Notifications', 'friends' ), | |
| 158 | + 'edit-friend-rules' => __( 'Edit Rules', 'friends' ), | |
| 159 | + 'duplicate-remover' => __( 'Duplicates', 'friends' ), | |
| 160 | + ); | |
| 161 | + if ( isset( $friend_submenu_items[ $current_page ] ) ) { | |
| 162 | + foreach ( $friend_submenu_items as $slug => $title ) { | |
| 163 | + $user_param = ''; | |
| 164 | + if ( isset( $_GET['user'] ) ) { | |
| 165 | + $username = sanitize_user( wp_unslash( $_GET['user'] ) ); | |
| 166 | + $user_param = '&user=' . $username . '&_wpnonce=' . wp_create_nonce( $slug . '-' . $username ); | |
| 167 | + } | |
| 168 | + $slug_ = strtr( $slug, '-', '_' ); | |
| 149 | 169 | |
| 150 | - if ( $this->friends_unread_friend_request_count( 0 ) > 0 ) { | |
| 151 | - add_submenu_page( 'friends', __( 'Friend Requests', 'friends' ), __( 'Friend Requests', 'friends' ) . $unread_badge, $required_role, 'friends-list-requests', array( $this, 'render_friends_list' ) ); | |
| 152 | - } elseif ( isset( $_GET['page'] ) && 'friends-list-requests' === $_GET['page'] ) { | |
| 153 | - // Don't show a no permission page but redirect to the friends list. | |
| 154 | - add_submenu_page( 'friends', __( 'Friend Requests', 'friends' ), __( 'Friend Requests', 'friends' ) . $unread_badge, $required_role, 'friends-list-requests', array( $this, 'render_friends_list' ) ); | |
| 170 | + add_submenu_page( | |
| 171 | + 'friends', | |
| 172 | + $title, | |
| 173 | + $title, | |
| 174 | + $required_role, | |
| 175 | + $slug . ( $slug === $current_page ? '' : $user_param ), | |
| 176 | + array( $this, 'render_admin_' . $slug_ ) | |
| 177 | + ); | |
| 178 | + | |
| 179 | + add_action( | |
| 180 | + 'load-' . $page_type . '_page_' . $slug, | |
| 181 | + array( $this, 'process_admin_' . $slug_ ) | |
| 182 | + ); | |
| 183 | + } | |
| 155 | 184 | } |
| 156 | 185 | |
| 157 | - if ( isset( $_GET['page'] ) && 'friends-refresh' === $_GET['page'] ) { | |
| 158 | - add_submenu_page( 'friends', __( 'Refresh', 'friends' ), __( 'Refresh', 'friends' ), $required_role, 'friends-refresh', array( $this, 'admin_refresh_friend_posts' ) ); | |
| 186 | + if ( isset( $_GET['page'] ) && 'friends-logs' === $_GET['page'] ) { | |
| 187 | + // translators: as in log file. | |
| 188 | + $title = __( 'Log', 'friends' ); | |
| 189 | + add_submenu_page( 'friends', $title, $title, $required_role, 'friends-logs', array( $this, 'render_friends_logs' ) ); | |
| 159 | 190 | } |
| 160 | 191 | |
| 161 | - // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 162 | - add_submenu_page( 'friends', __( 'Plugins' ), __( 'Plugins' ), $required_role, 'friends-plugins', array( $this, 'admin_plugin_installer' ) ); | |
| 192 | + $title = __( 'Browser Extension', 'friends' ); | |
| 193 | + add_submenu_page( 'friends', $title, $title, $required_role, 'friends-browser-extension', array( $this, 'render_browser_extension' ) ); | |
| 163 | 194 | |
| 164 | - if ( isset( $_GET['page'] ) && 0 === strpos( $_GET['page'], 'edit-friend' ) ) { | |
| 165 | - add_submenu_page( 'friends', __( 'Edit User', 'friends' ), __( 'Edit User', 'friends' ), $required_role, 'edit-friend' . ( 'edit-friend' !== $_GET['page'] && isset( $_GET['user'] ) ? '&user=' . $_GET['user'] : '' ), array( $this, 'render_admin_edit_friend' ) ); | |
| 166 | - add_submenu_page( 'friends', __( 'Edit Feeds', 'friends' ), __( 'Edit Feeds', 'friends' ), $required_role, 'edit-friend-feeds' . ( 'edit-friend-feeds' !== $_GET['page'] && isset( $_GET['user'] ) ? '&user=' . $_GET['user'] : '' ), array( $this, 'render_admin_edit_friend_feeds' ) ); | |
| 167 | - add_submenu_page( 'friends', __( 'Edit Notifications', 'friends' ), __( 'Edit Notifications', 'friends' ), $required_role, 'edit-friend-notifications' . ( 'edit-friend-notifications' !== $_GET['page'] && isset( $_GET['user'] ) ? '&user=' . $_GET['user'] : '' ), array( $this, 'render_admin_edit_friend_notifications' ) ); | |
| 168 | - add_submenu_page( 'friends', __( 'Edit Rules', 'friends' ), __( 'Edit Rules', 'friends' ), $required_role, 'edit-friend-rules' . ( 'edit-friend-rules' !== $_GET['page'] && isset( $_GET['user'] ) ? '&user=' . $_GET['user'] : '' ), array( $this, 'render_admin_edit_friend_rules' ) ); | |
| 169 | - add_action( 'load-' . $page_type . '_page_edit-friend', array( $this, 'process_admin_edit_friend' ) ); | |
| 170 | - add_action( 'load-' . $page_type . '_page_edit-friend-feeds', array( $this, 'process_admin_edit_friend_feeds' ) ); | |
| 171 | - add_action( 'load-' . $page_type . '_page_edit-friend-notifications', array( $this, 'process_admin_edit_friend_notifications' ) ); | |
| 172 | - add_action( 'load-' . $page_type . '_page_edit-friend-rules', array( $this, 'process_admin_edit_friend_rules' ) ); | |
| 173 | - } | |
| 174 | - | |
| 175 | 195 | if ( isset( $_GET['page'] ) && 'unfriend' === $_GET['page'] ) { |
| 176 | - $user = new User( $_GET['user'] ); | |
| 196 | + $user = new User( intval( $_GET['user'] ) ); | |
| 177 | 197 | if ( $user ) { |
| 178 | 198 | $title = /* translators: %s is a username. */ sprintf( __( 'Unfriend %s', 'friends' ), $user->user_login ); |
| 179 | 199 | add_submenu_page( 'friends', $title, $title, $required_role, 'unfriend', array( $this, 'render_admin_unfriend' ) ); |
| 180 | 200 | add_action( 'load-' . $page_type . '_page_unfriend', array( $this, 'process_admin_unfriend' ) ); |
| @@ -243,12 +263,12 @@ | ||
| 243 | 263 | __( 'Welcome to the Friends Settings! You can configure the Friends plugin here to your liking.', 'friends' ) . |
| 244 | 264 | '</p>' . |
| 245 | 265 | '<p>' . |
| 246 | 266 | sprintf( |
| 247 | - // translators: %1$s is a URL, %2$s is the name of a wp-admin screen. | |
| 248 | - __( 'There are more settings available for each friend or subscription individually. To get there, click on the user on the <a href=%1$s>%2$s</a> screen.', 'friends' ), | |
| 249 | - '"' . esc_attr( self_admin_url( self::get_users_url() ) ) . '"', | |
| 250 | - __( 'Friends & Requests', 'friends' ) | |
| 267 | + // translators: %1$s is a URL, %2$s is the name of a page. | |
| 268 | + __( 'There are more settings available for each friend or subscription individually. To get there, click on the user on the <a href=%1$s>%2$s</a> page.', 'friends' ), | |
| 269 | + '"' . esc_attr( self::get_users_url() ) . '"', | |
| 270 | + __( 'Following', 'friends' ) | |
| 251 | 271 | ) . |
| 252 | 272 | '</p>', |
| 253 | 273 | ) |
| 254 | 274 | ); |
| @@ -271,19 +291,17 @@ | ||
| 271 | 291 | public function admin_enqueue_scripts() { |
| 272 | 292 | $handle = 'friends-admin'; |
| 273 | 293 | $file = 'friends-admin.js'; |
| 274 | 294 | $version = Friends::VERSION; |
| 275 | - wp_enqueue_script( $handle, plugins_url( $file, FRIENDS_PLUGIN_FILE ), array( 'jquery' ), apply_filters( 'friends_debug_enqueue', $version, $handle, dirname( FRIENDS_PLUGIN_FILE ) . '/' . $file ) ); | |
| 295 | + wp_enqueue_script( $handle, plugins_url( $file, FRIENDS_PLUGIN_FILE ), array( 'jquery' ), apply_filters( 'friends_debug_enqueue', $version, $handle, dirname( FRIENDS_PLUGIN_FILE ) . '/' . $file ), true ); | |
| 276 | 296 | |
| 277 | 297 | $variables = array( |
| 278 | 298 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 279 | 299 | 'add_friend_url' => self_admin_url( 'admin.php?page=add-friend' ), |
| 280 | 300 | 'add_friend_text' => __( 'Add a Friend', 'friends' ), |
| 301 | + 'copy_text' => __( 'Copy', 'friends' ), | |
| 302 | + 'copied_text' => __( 'Copied!', 'friends' ), | |
| 281 | 303 | 'delete_feed_question' => __( 'Delete the feed? You need to click "Save Changes" to really delete it.', 'friends' ), |
| 282 | - 'role_friend' => __( 'Friend', 'friends' ), | |
| 283 | - 'role_acquaintance' => __( 'Acquaintance', 'friends' ), | |
| 284 | - 'role_friend_request' => __( 'Friend Request', 'friends' ), | |
| 285 | - 'role_pending_friend_request' => __( 'Pending Friend Request', 'friends' ), | |
| 286 | 304 | 'role_subscription' => __( 'Following', 'friends' ), |
| 287 | 305 | 'role_connection' => __( 'Connection', 'friends' ), |
| 288 | 306 | 'role_contact' => __( 'Contact', 'friends' ), |
| 289 | 307 | 'role_connection_request' => __( 'Connection Request', 'friends' ), |
| @@ -310,10 +328,17 @@ | ||
| 310 | 328 | |
| 311 | 329 | add_filter( |
| 312 | 330 | 'friends_friend_private_feed_url', |
| 313 | 331 | function ( $feed_url, $friend_user ) { |
| 314 | - // translators: %1s is the name of the friend, %2$s is the feed URL. | |
| 315 | - printf( __( 'Refreshing %1$s at %2$s', 'friends' ) . '<br/>', '<a href="' . esc_url( $friend_user->get_local_friends_page_url() ) . '">' . esc_html( $friend_user->user_login ) . '</a>', '<a href="' . esc_url( $feed_url ) . '">' . esc_html( $feed_url ) . '</a>' ); | |
| 332 | + echo wp_kses( | |
| 333 | + // translators: %1s is the name of the friend, %2$s is the feed URL. | |
| 334 | + sprintf( __( 'Refreshing %1$s at %2$s', 'friends' ) . '<br/>', '<a href="' . esc_url( $friend_user->get_local_friends_page_url() ) . '">' . esc_html( $friend_user->user_login ) . '</a>', '<a href="' . esc_url( $feed_url ) . '">' . esc_html( $feed_url ) . '</a>' ), | |
| 335 | + array( | |
| 336 | + 'a' => array( | |
| 337 | + 'href' => array(), | |
| 338 | + ), | |
| 339 | + ) | |
| 340 | + ); | |
| 316 | 341 | return $feed_url; |
| 317 | 342 | }, |
| 318 | 343 | 10, |
| 319 | 344 | 2 |
| @@ -354,10 +379,10 @@ | ||
| 354 | 379 | 10, |
| 355 | 380 | 2 |
| 356 | 381 | ); |
| 357 | 382 | |
| 358 | - if ( isset( $_GET['user'] ) ) { | |
| 359 | - $friend_user = User::get_by_username( $_GET['user'] ); | |
| 383 | + if ( isset( $_GET['user'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 384 | + $friend_user = User::get_by_username( sanitize_user( wp_unslash( $_GET['user'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 360 | 385 | if ( ! $friend_user || is_wp_error( $friend_user ) || ! $friend_user->can_refresh_feeds() ) { |
| 361 | 386 | wp_die( esc_html__( 'Invalid user ID.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 362 | 387 | } |
| 363 | 388 | $friend_user->retrieve_posts_from_active_feeds(); |
| @@ -366,85 +391,8 @@ | ||
| 366 | 391 | } |
| 367 | 392 | } |
| 368 | 393 | |
| 369 | 394 | /** |
| 370 | - * Admin page for installing plugins. | |
| 371 | - */ | |
| 372 | - public function admin_plugin_installer() { | |
| 373 | - Friends::template_loader()->get_template_part( 'admin/plugin-installer-header' ); | |
| 374 | - Plugin_Installer::init(); | |
| 375 | - Friends::template_loader()->get_template_part( 'admin/plugin-installer-footer' ); | |
| 376 | - } | |
| 377 | - | |
| 378 | - /** | |
| 379 | - * Send a friend request to another WordPress with the Friends plugin | |
| 380 | - * | |
| 381 | - * @param string $rest_url The site URL of the friend's | |
| 382 | - * WordPress. | |
| 383 | - * @param string $user_login The user login. | |
| 384 | - * @param string $user_url The user url. | |
| 385 | - * @param string $display_name The display name. | |
| 386 | - * @param string $codeword A codeword to send along. | |
| 387 | - * @param string $message A message to send along. | |
| 388 | - * | |
| 389 | - * @return \WP_User|\WP_error $user The new associated user or an error object. | |
| 390 | - */ | |
| 391 | - public function send_friend_request( $rest_url, $user_login, $user_url, $display_name, $codeword = 'friends', $message = '' ) { | |
| 392 | - if ( ! is_string( $rest_url ) || ! Friends::check_url( $rest_url ) ) { | |
| 393 | - return new \WP_Error( 'invalid-url', __( 'You entered an invalid URL.', 'friends' ) ); | |
| 394 | - } | |
| 395 | - | |
| 396 | - $future_in_token = wp_generate_password( 128, false ); | |
| 397 | - | |
| 398 | - $current_user = wp_get_current_user(); | |
| 399 | - $response = wp_safe_remote_post( | |
| 400 | - $rest_url . '/friend-request', | |
| 401 | - array( | |
| 402 | - 'body' => array( | |
| 403 | - 'version' => 2, | |
| 404 | - 'codeword' => $codeword, | |
| 405 | - 'name' => $current_user->display_name, | |
| 406 | - 'url' => home_url(), | |
| 407 | - 'icon_url' => get_avatar_url( $current_user->ID ), | |
| 408 | - 'message' => mb_substr( trim( $message ), 0, 2000 ), | |
| 409 | - 'key' => $future_in_token, | |
| 410 | - ), | |
| 411 | - 'timeout' => 20, | |
| 412 | - 'redirection' => 5, | |
| 413 | - ) | |
| 414 | - ); | |
| 415 | - if ( is_wp_error( $response ) ) { | |
| 416 | - return $response; | |
| 417 | - } | |
| 418 | - | |
| 419 | - $json = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 420 | - if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { | |
| 421 | - if ( $json && isset( $json->code ) && isset( $json->message ) ) { | |
| 422 | - // translators: %s is the message from the other server. | |
| 423 | - return new \WP_Error( $json->code, sprintf( __( 'The other side responded: %s', 'friends' ), $json->message ), $json->data ); | |
| 424 | - } | |
| 425 | - } | |
| 426 | - | |
| 427 | - if ( ! $json || ! is_object( $json ) ) { | |
| 428 | - return new \WP_Error( 'unexpected-rest-response', 'Unexpected remote response: ' . substr( wp_remote_retrieve_body( $response ), 0, 30 ), $response ); | |
| 429 | - } | |
| 430 | - | |
| 431 | - $friend_user = User::create( $user_login, 'pending_friend_request', $user_url, $display_name ); | |
| 432 | - if ( is_wp_error( $friend_user ) ) { | |
| 433 | - return $friend_user; | |
| 434 | - } | |
| 435 | - $friend_user->update_user_option( 'friends_rest_url', $rest_url ); | |
| 436 | - | |
| 437 | - if ( isset( $json->request ) ) { | |
| 438 | - update_option( 'friends_request_' . sha1( $json->request ), $friend_user->ID ); | |
| 439 | - $friend_user->update_user_option( 'friends_future_in_token_' . sha1( $json->request ), $future_in_token ); | |
| 440 | - $friend_user->set_role( 'pending_friend_request' ); | |
| 441 | - } | |
| 442 | - | |
| 443 | - return $friend_user; | |
| 444 | - } | |
| 445 | - | |
| 446 | - /** | |
| 447 | 395 | * Don't show the edit link for friend posts |
| 448 | 396 | * |
| 449 | 397 | * @param string $link The edit link. |
| 450 | 398 | * @param int|User $user The user. |
| @@ -450,8 +398,21 @@ | ||
| 450 | 398 | * @param int|User $user The user. |
| 451 | 399 | * @return string|bool The edit link or false. |
| 452 | 400 | */ |
| 453 | 401 | public static function admin_edit_user_link( $link, $user ) { |
| 402 | + static $cache = array(); | |
| 403 | + if ( $user instanceof \WP_User ) { | |
| 404 | + $cache_key = $user->ID; | |
| 405 | + } else { | |
| 406 | + $cache_key = $user; | |
| 407 | + } | |
| 408 | + | |
| 409 | + if ( isset( $cache[ $cache_key ] ) ) { | |
| 410 | + if ( false === $cache[ $cache_key ] ) { | |
| 411 | + return $link; | |
| 412 | + } | |
| 413 | + return $cache[ $cache_key ]; | |
| 414 | + } | |
| 454 | 415 | if ( ! $user instanceof \WP_User ) { |
| 455 | 416 | if ( is_string( $user ) ) { |
| 456 | 417 | $user = User::get_by_username( $user ); |
| 457 | 418 | } else { |
| @@ -458,22 +419,37 @@ | ||
| 458 | 419 | $user = new \WP_User( $user ); |
| 459 | 420 | } |
| 460 | 421 | } |
| 461 | 422 | |
| 423 | + if ( ! $user || is_wp_error( $user ) ) { | |
| 424 | + $cache[ $cache_key ] = false; | |
| 425 | + return $link; | |
| 426 | + } | |
| 427 | + | |
| 462 | 428 | if ( is_multisite() && is_super_admin( $user->ID ) ) { |
| 429 | + $cache[ $cache_key ] = false; | |
| 463 | 430 | return $link; |
| 464 | 431 | } |
| 465 | 432 | if ( ! $user->has_cap( 'friends_plugin' ) ) { |
| 433 | + $cache[ $cache_key ] = false; | |
| 466 | 434 | return $link; |
| 467 | 435 | } |
| 468 | 436 | |
| 469 | - return self_admin_url( 'admin.php?page=edit-friend&user=' . $user->user_login ); | |
| 437 | + $cache[ $cache_key ] = self_admin_url( 'admin.php?page=edit-friend&user=' . $user->user_login ); | |
| 438 | + return $cache[ $cache_key ]; | |
| 470 | 439 | } |
| 471 | 440 | |
| 472 | 441 | public static function get_edit_friend_link( $user ) { |
| 473 | - if ( ! $user instanceof \WP_User ) { | |
| 474 | - $user = new \WP_User( $user ); | |
| 442 | + if ( is_string( $user ) ) { | |
| 443 | + $user = User::get_by_username( $user ); | |
| 444 | + } elseif ( ! $user instanceof User && ! $user instanceof Subscription ) { | |
| 445 | + $user = new User( $user ); | |
| 475 | 446 | } |
| 447 | + | |
| 448 | + if ( ! $user || is_wp_error( $user ) ) { | |
| 449 | + return ''; | |
| 450 | + } | |
| 451 | + | |
| 476 | 452 | return apply_filters( 'get_edit_user_link', $user->user_url, $user->user_login ); |
| 477 | 453 | } |
| 478 | 454 | |
| 479 | 455 | public static function get_unfriend_link( $user ) { |
| @@ -508,15 +484,15 @@ | ||
| 508 | 484 | if ( empty( $_REQUEST ) || ! isset( $_REQUEST['_wpnonce'] ) ) { |
| 509 | 485 | return; |
| 510 | 486 | } |
| 511 | 487 | |
| 512 | - if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'friends-settings' ) ) { | |
| 488 | + if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'friends-settings' ) ) { | |
| 513 | 489 | return; |
| 514 | 490 | } |
| 515 | 491 | |
| 516 | 492 | $this->check_admin_settings(); |
| 517 | - foreach ( array( 'ignore_incoming_friend_requests', 'enable_wp_friendships' ) as $checkbox ) { | |
| 518 | - if ( isset( $_POST[ $checkbox ] ) && $_POST[ $checkbox ] ) { | |
| 493 | + foreach ( array( 'disable_auto_tagging', 'disable_link_previews' ) as $checkbox ) { | |
| 494 | + if ( isset( $_POST[ $checkbox ] ) && boolval( $_POST[ $checkbox ] ) ) { | |
| 519 | 495 | update_option( 'friends_' . $checkbox, true ); |
| 520 | 496 | } else { |
| 521 | 497 | delete_option( 'friends_' . $checkbox ); |
| 522 | 498 | } |
| @@ -521,19 +497,18 @@ | ||
| 521 | 497 | delete_option( 'friends_' . $checkbox ); |
| 522 | 498 | } |
| 523 | 499 | } |
| 524 | 500 | |
| 525 | - foreach ( array( 'friend_request_notification' ) as $negative_user_checkbox ) { | |
| 526 | - if ( isset( $_POST[ $negative_user_checkbox ] ) && $_POST[ $negative_user_checkbox ] ) { | |
| 527 | - delete_user_option( get_current_user_id(), 'friends_no_' . $negative_user_checkbox ); | |
| 528 | - } else { | |
| 529 | - update_user_option( get_current_user_id(), 'friends_no_' . $negative_user_checkbox, 1 ); | |
| 501 | + if ( current_user_can( 'manage_options' ) ) { | |
| 502 | + if ( isset( $_POST['main_user_id'] ) ) { | |
| 503 | + $main_user_id = absint( $_POST['main_user_id'] ); | |
| 504 | + if ( $main_user_id && user_can( $main_user_id, Friends::REQUIRED_ROLE ) ) { | |
| 505 | + update_option( 'friends_main_user_id', $main_user_id ); | |
| 506 | + } | |
| 530 | 507 | } |
| 531 | - } | |
| 532 | 508 | |
| 533 | - if ( current_user_can( 'manage_options' ) ) { | |
| 534 | - foreach ( array( 'force_enable_post_formats', 'expose_post_format_feeds' ) as $checkbox ) { | |
| 535 | - if ( isset( $_POST[ $checkbox ] ) && $_POST[ $checkbox ] ) { | |
| 509 | + foreach ( array( 'force_enable_post_formats', 'expose_post_format_feeds', 'exclude_compose_format_from_feed' ) as $checkbox ) { | |
| 510 | + if ( isset( $_POST[ $checkbox ] ) && boolval( $_POST[ $checkbox ] ) ) { | |
| 536 | 511 | update_option( 'friends_' . $checkbox, true ); |
| 537 | 512 | } else { |
| 538 | 513 | delete_option( 'friends_' . $checkbox ); |
| 539 | 514 | } |
| @@ -538,23 +513,20 @@ | ||
| 538 | 513 | delete_option( 'friends_' . $checkbox ); |
| 539 | 514 | } |
| 540 | 515 | } |
| 541 | 516 | |
| 542 | - if ( isset( $_POST['limit_homepage_post_format'] ) && $_POST['limit_homepage_post_format'] && in_array( $_POST['limit_homepage_post_format'], get_post_format_slugs() ) ) { | |
| 543 | - update_option( 'friends_limit_homepage_post_format', $_POST['limit_homepage_post_format'] ); | |
| 517 | + $post_format_slugs = get_post_format_slugs(); | |
| 518 | + if ( isset( $_POST['friends_compose_post_format'] ) && in_array( sanitize_key( $_POST['friends_compose_post_format'] ), array_merge( array( 'standard' ), $post_format_slugs ), true ) ) { | |
| 519 | + update_option( 'friends_compose_post_format', sanitize_key( $_POST['friends_compose_post_format'] ) ); | |
| 544 | 520 | } else { |
| 545 | - delete_option( 'friends_limit_homepage_post_format' ); | |
| 521 | + delete_option( 'friends_compose_post_format' ); | |
| 546 | 522 | } |
| 547 | - if ( isset( $_POST['blocks_everywhere'] ) && $_POST['blocks_everywhere'] ) { | |
| 548 | - update_user_option( get_current_user_id(), 'friends_blocks_everywhere', 1 ); | |
| 549 | - } else { | |
| 550 | - delete_user_option( get_current_user_id(), 'friends_blocks_everywhere' ); | |
| 551 | - } | |
| 552 | 523 | } |
| 553 | 524 | |
| 554 | - if ( isset( $_POST['available_emojis'] ) && $_POST['available_emojis'] ) { | |
| 525 | + if ( isset( $_POST['available_emojis'] ) && is_array( $_POST['available_emojis'] ) ) { | |
| 555 | 526 | $available_emojis = array(); |
| 556 | - foreach ( $_POST['available_emojis'] as $id ) { | |
| 527 | + foreach ( wp_unslash( $_POST['available_emojis'] ) as $id ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput | |
| 528 | + $id = sanitize_key( $id ); | |
| 557 | 529 | $data = Reactions::get_emoji_data( $id ); |
| 558 | 530 | if ( $data ) { |
| 559 | 531 | $available_emojis[ $id ] = $data; |
| 560 | 532 | } |
| @@ -566,32 +538,56 @@ | ||
| 566 | 538 | |
| 567 | 539 | // Global retention. |
| 568 | 540 | $retention_number_enabled = boolval( isset( $_POST['friends_enable_retention_number'] ) && $_POST['friends_enable_retention_number'] ); |
| 569 | 541 | update_option( 'friends_enable_retention_number', $retention_number_enabled ); |
| 570 | - if ( $retention_number_enabled ) { | |
| 542 | + if ( $retention_number_enabled && isset( $_POST['friends_retention_number'] ) ) { | |
| 571 | 543 | update_option( 'friends_retention_number', max( 1, intval( $_POST['friends_retention_number'] ) ) ); |
| 572 | 544 | } |
| 573 | 545 | $retention_days_enabled = boolval( isset( $_POST['friends_enable_retention_days'] ) && $_POST['friends_enable_retention_days'] ); |
| 574 | 546 | update_option( 'friends_enable_retention_days', $retention_days_enabled ); |
| 575 | - if ( $retention_days_enabled ) { | |
| 547 | + if ( $retention_days_enabled && isset( $_POST['friends_retention_days'] ) ) { | |
| 576 | 548 | update_option( 'friends_retention_days', max( 1, intval( $_POST['friends_retention_days'] ) ) ); |
| 577 | 549 | } |
| 578 | 550 | |
| 551 | + if ( isset( $_POST['retention_delete_reacted'] ) && 1 === intval( $_POST['retention_delete_reacted'] ) ) { | |
| 552 | + delete_option( 'friends_retention_delete_reacted' ); | |
| 553 | + } else { | |
| 554 | + update_option( 'friends_retention_delete_reacted', true ); | |
| 555 | + } | |
| 556 | + | |
| 579 | 557 | if ( isset( $_POST['frontend_default_view'] ) && in_array( |
| 580 | - $_POST['frontend_default_view'], | |
| 558 | + wp_unslash( $_POST['frontend_default_view'] ), | |
| 581 | 559 | array( |
| 582 | 560 | 'collapsed', |
| 583 | 561 | ) |
| 584 | 562 | ) ) { |
| 585 | - update_option( 'friends_frontend_default_view', $_POST['frontend_default_view'] ); | |
| 563 | + update_user_option( get_current_user_id(), 'friends_frontend_default_view', wp_unslash( $_POST['frontend_default_view'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput | |
| 586 | 564 | } else { |
| 587 | - delete_option( 'friends_frontend_default_view' ); | |
| 565 | + delete_user_option( get_current_user_id(), 'friends_frontend_default_view' ); | |
| 588 | 566 | } |
| 589 | 567 | |
| 568 | + foreach ( array_merge( array( '' ), get_post_format_slugs() ) as $post_type ) { | |
| 569 | + $name = 'friends_frontend_theme'; | |
| 570 | + if ( $post_type ) { | |
| 571 | + $name = 'friends_frontend_theme_' . $post_type; | |
| 572 | + } | |
| 573 | + $theme = 'default'; | |
| 574 | + if ( isset( $_POST[ $name ] ) && in_array( $theme, array_keys( Frontend::get_themes() ) ) ) { | |
| 575 | + $theme = wp_unslash( $_POST[ $name ] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput | |
| 576 | + } | |
| 577 | + if ( 'default' === $theme ) { | |
| 578 | + delete_user_option( get_current_user_id(), $name ); | |
| 579 | + } else { | |
| 580 | + update_user_option( get_current_user_id(), $name, $theme ); | |
| 581 | + } | |
| 582 | + } | |
| 583 | + | |
| 584 | + $redirect_args = array( 'updated' => '1' ); | |
| 585 | + | |
| 590 | 586 | if ( isset( $_GET['_wp_http_referer'] ) ) { |
| 591 | 587 | wp_safe_redirect( wp_get_referer() ); |
| 592 | 588 | } else { |
| 593 | - wp_safe_redirect( add_query_arg( 'updated', '1', remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ); | |
| 589 | + wp_safe_redirect( add_query_arg( $redirect_args, remove_query_arg( array( '_wp_http_referer', '_wpnonce' ) ) ) ); | |
| 594 | 590 | } |
| 595 | 591 | exit; |
| 596 | 592 | } |
| 597 | 593 | |
| @@ -605,13 +601,121 @@ | ||
| 605 | 601 | return empty( $locale ) ? 'en_US' : $locale; |
| 606 | 602 | } |
| 607 | 603 | |
| 608 | 604 | /** |
| 609 | - * Render the Friends Admin home page | |
| 605 | + * Get the registry of news entries, newest first. | |
| 606 | + * | |
| 607 | + * Each entry has: version, title, template, and optionally migration_version | |
| 608 | + * for entries that should show migration status. | |
| 609 | + * | |
| 610 | + * @return array | |
| 610 | 611 | */ |
| 612 | + public static function get_news_entries() { | |
| 613 | + return apply_filters( | |
| 614 | + 'friends_news_entries', | |
| 615 | + array( | |
| 616 | + array( | |
| 617 | + 'version' => '4.3', | |
| 618 | + 'title' => __( '4.3: Link Previews', 'friends' ), | |
| 619 | + 'template' => 'admin/news-4-3', | |
| 620 | + ), | |
| 621 | + array( | |
| 622 | + 'version' => '4.2', | |
| 623 | + 'title' => __( '4.2: Direct Messages', 'friends' ), | |
| 624 | + 'template' => 'admin/news-4-2', | |
| 625 | + ), | |
| 626 | + array( | |
| 627 | + 'version' => '4.1', | |
| 628 | + 'title' => __( '4.1: Add Friend Frontend, Twitter Theme & Browser Extension', 'friends' ), | |
| 629 | + 'template' => 'admin/news-4-1', | |
| 630 | + ), | |
| 631 | + array( | |
| 632 | + 'version' => '4.0', | |
| 633 | + 'title' => __( '4.0: A Major Update', 'friends' ), | |
| 634 | + 'template' => 'admin/welcome-4-0', | |
| 635 | + 'migration_version' => '4.0.0', | |
| 636 | + ), | |
| 637 | + array( | |
| 638 | + 'version' => '3.3', | |
| 639 | + 'title' => __( '3.3: Styling Overhaul', 'friends' ), | |
| 640 | + 'template' => 'admin/news-3-3', | |
| 641 | + ), | |
| 642 | + array( | |
| 643 | + 'version' => '3.0', | |
| 644 | + 'title' => __( '3.0: Followers & Notifications', 'friends' ), | |
| 645 | + 'template' => 'admin/news-3-0', | |
| 646 | + ), | |
| 647 | + array( | |
| 648 | + 'version' => '2.4', | |
| 649 | + 'title' => __( '2.4: Mastodon Compatibility', 'friends' ), | |
| 650 | + 'template' => 'admin/news-2-4', | |
| 651 | + ), | |
| 652 | + array( | |
| 653 | + 'version' => '2.1', | |
| 654 | + 'title' => __( '2.1: Frontend & Plugins', 'friends' ), | |
| 655 | + 'template' => 'admin/news-2-1', | |
| 656 | + ), | |
| 657 | + array( | |
| 658 | + 'version' => '2.0', | |
| 659 | + 'title' => __( '2.0: Revisions & Site Health', 'friends' ), | |
| 660 | + 'template' => 'admin/news-2-0', | |
| 661 | + ), | |
| 662 | + array( | |
| 663 | + 'version' => '0', | |
| 664 | + 'title' => __( 'Welcome to the Friends Plugin!', 'friends' ), | |
| 665 | + 'template' => 'admin/welcome', | |
| 666 | + ), | |
| 667 | + ) | |
| 668 | + ); | |
| 669 | + } | |
| 670 | + | |
| 671 | + /** | |
| 672 | + * Get migration statuses for a specific version. | |
| 673 | + * | |
| 674 | + * @param string $migration_version The version to filter migrations for. | |
| 675 | + * @return array With keys: statuses, all_complete, has_in_progress. | |
| 676 | + */ | |
| 677 | + public static function get_migration_data( $migration_version ) { | |
| 678 | + $all_statuses = Migration::get_all_statuses(); | |
| 679 | + $statuses = array(); | |
| 680 | + $all_complete = true; | |
| 681 | + $has_in_progress = false; | |
| 682 | + | |
| 683 | + foreach ( $all_statuses as $id => $status ) { | |
| 684 | + if ( $status['version'] !== $migration_version ) { | |
| 685 | + continue; | |
| 686 | + } | |
| 687 | + $statuses[ $id ] = $status; | |
| 688 | + if ( empty( $status['completed'] ) ) { | |
| 689 | + $all_complete = false; | |
| 690 | + } | |
| 691 | + if ( ! empty( $status['in_progress'] ) ) { | |
| 692 | + $has_in_progress = true; | |
| 693 | + } | |
| 694 | + } | |
| 695 | + | |
| 696 | + return array( | |
| 697 | + 'statuses' => $statuses, | |
| 698 | + 'all_complete' => $all_complete, | |
| 699 | + 'has_in_progress' => $has_in_progress, | |
| 700 | + ); | |
| 701 | + } | |
| 702 | + | |
| 703 | + /** | |
| 704 | + * Render the Friends Admin home page. | |
| 705 | + * | |
| 706 | + * Shows the welcome page for new users (no subscriptions), | |
| 707 | + * or a news/changelog view for existing users. | |
| 708 | + */ | |
| 611 | 709 | public function render_admin_home() { |
| 710 | + // Dismiss the update notice permanently when visiting this page. | |
| 711 | + if ( get_option( 'friends_welcome_version' ) ) { | |
| 712 | + delete_option( 'friends_welcome_version' ); | |
| 713 | + } | |
| 714 | + | |
| 612 | 715 | $friends_subscriptions = User_Query::all_associated_users(); |
| 613 | - $has_friend_users = $friends_subscriptions->get_total() > 0; | |
| 716 | + $is_new_user = 0 === $friends_subscriptions->get_total(); | |
| 717 | + | |
| 614 | 718 | wp_enqueue_script( 'plugin-install' ); |
| 615 | 719 | add_thickbox(); |
| 616 | 720 | wp_enqueue_script( 'updates' ); |
| 617 | 721 | |
| @@ -619,18 +723,548 @@ | ||
| 619 | 723 | 'admin/settings-header', |
| 620 | 724 | null, |
| 621 | 725 | array( |
| 622 | 726 | 'active' => 'friends', |
| 623 | - 'title' => __( 'Friends', 'friends' ), | |
| 624 | 727 | ) |
| 625 | 728 | ); |
| 626 | 729 | |
| 627 | - Friends::template_loader()->get_template_part( 'admin/welcome', null, array( 'installed_plugins' => get_plugins() ) ); | |
| 730 | + $news_entries = self::get_news_entries(); | |
| 628 | 731 | |
| 732 | + if ( $is_new_user ) { | |
| 733 | + // New users: welcome entry first, rest after. | |
| 734 | + $news_entries = array_reverse( $news_entries ); | |
| 735 | + } | |
| 736 | + | |
| 737 | + Friends::template_loader()->get_template_part( | |
| 738 | + 'admin/news', | |
| 739 | + null, | |
| 740 | + array( | |
| 741 | + 'entries' => $news_entries, | |
| 742 | + ) | |
| 743 | + ); | |
| 744 | + | |
| 629 | 745 | Friends::template_loader()->get_template_part( 'admin/settings-footer' ); |
| 630 | 746 | } |
| 631 | 747 | |
| 632 | 748 | /** |
| 749 | + * Process the response after adding a friend/subscription. | |
| 750 | + * | |
| 751 | + * @param User|\WP_Error $friend_user The friend user object. | |
| 752 | + * @param array $vars The form variables. | |
| 753 | + * | |
| 754 | + * @return bool Whether the operation was successful. | |
| 755 | + */ | |
| 756 | + private function process_admin_add_friend_response( $friend_user, $vars ) { | |
| 757 | + if ( is_wp_error( $friend_user ) ) { | |
| 758 | + $this->display_errors( $friend_user ); | |
| 759 | + return false; | |
| 760 | + } | |
| 761 | + | |
| 762 | + if ( ! $friend_user instanceof User ) { | |
| 763 | + ?> | |
| 764 | + <div id="message" class="updated notice is-dismissible"><p> | |
| 765 | + <?php esc_html_e( 'Unknown error', 'friends' ); ?> | |
| 766 | + </p></div> | |
| 767 | + <?php | |
| 768 | + return false; | |
| 769 | + } | |
| 770 | + | |
| 771 | + $feed_options = array(); | |
| 772 | + if ( ! isset( $vars['feeds'] ) ) { | |
| 773 | + $vars['feeds'] = array(); | |
| 774 | + } | |
| 775 | + foreach ( $vars['feeds'] as $feed ) { | |
| 776 | + if ( isset( $feed['type'] ) ) { | |
| 777 | + $feed['mime-type'] = $feed['type']; | |
| 778 | + unset( $feed['type'] ); | |
| 779 | + } | |
| 780 | + $feed_options[ $feed['url'] ] = $feed; | |
| 781 | + } | |
| 782 | + | |
| 783 | + $friend_user->save_feeds( $feed_options ); | |
| 784 | + | |
| 785 | + if ( ! isset( $vars['subscribe'] ) ) { | |
| 786 | + $vars['subscribe'] = array(); | |
| 787 | + } | |
| 788 | + | |
| 789 | + $count = 0; | |
| 790 | + foreach ( $vars['subscribe'] as $feed_url ) { | |
| 791 | + if ( ! isset( $feed_options[ $feed_url ] ) ) { | |
| 792 | + continue; | |
| 793 | + } | |
| 794 | + $new_feed = $friend_user->subscribe( $feed_url, $feed_options[ $feed_url ] ); | |
| 795 | + if ( ! is_wp_error( $new_feed ) ) { | |
| 796 | + do_action( 'friends_user_feed_activated', $new_feed ); | |
| 797 | + ++$count; | |
| 798 | + } | |
| 799 | + } | |
| 800 | + | |
| 801 | + add_filter( 'notify_about_new_friend_post', '__return_false', 999 ); | |
| 802 | + wp_schedule_single_event( time(), 'friends_retrieve_user_feeds', array( $friend_user->ID ) ); | |
| 803 | + | |
| 804 | + $friend_link = '<a href="' . esc_url( $this->admin_edit_user_link( $friend_user->get_local_friends_page_url(), $friend_user ) ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( $friend_user->display_name ) . '</a>'; | |
| 805 | + | |
| 806 | + // translators: %s is a Site URL. | |
| 807 | + $message = sprintf( __( "You're now subscribed to %s.", 'friends' ), $friend_link ); | |
| 808 | + | |
| 809 | + ?> | |
| 810 | + <div id="message" class="updated notice is-dismissible"><p> | |
| 811 | + <?php | |
| 812 | + echo wp_kses( $message, array( 'a' => array( 'href' => array() ) ) ); | |
| 813 | + // translators: %s is the friends page URL. | |
| 814 | + echo ' ', wp_kses( sprintf( __( 'Go to your <a href=%s>friends page</a> to view their posts.', 'friends' ), '"' . esc_url( $friend_user->get_local_friends_page_url() ) . '"' ), array( 'a' => array( 'href' => array() ) ) ); | |
| 815 | + echo ' <span id="fetch-feeds" data-nonce="', esc_attr( wp_create_nonce( 'fetch-feeds-' . sanitize_user( $friend_user->user_login ) ) ), '" data-friend=', esc_attr( $friend_user->user_login ), '>', esc_html__( 'Fetching feeds...', 'friends' ), '</span>'; | |
| 816 | + ?> | |
| 817 | + </p></div> | |
| 818 | + <?php | |
| 819 | + return true; | |
| 820 | + } | |
| 821 | + | |
| 822 | + /** | |
| 823 | + * Process the Add Friend form. | |
| 824 | + * | |
| 825 | + * @param array $vars The POST or GET variables. | |
| 826 | + * | |
| 827 | + * @return \WP_Error|null|bool A \WP_Error, null, or true on success. | |
| 828 | + */ | |
| 829 | + public function process_admin_add_friend( $vars ) { | |
| 830 | + $errors = new \WP_Error(); | |
| 831 | + | |
| 832 | + $friend_url = isset( $vars['friend_url'] ) ? trim( $vars['friend_url'] ) : ''; | |
| 833 | + | |
| 834 | + $friend_user = false; | |
| 835 | + | |
| 836 | + $protocol = wp_parse_url( $friend_url, PHP_URL_SCHEME ); | |
| 837 | + if ( ! $protocol ) { | |
| 838 | + if ( is_multisite() ) { | |
| 839 | + $friend_user = get_user_by( 'login', $friend_url ); | |
| 840 | + if ( $friend_user ) { | |
| 841 | + $site = get_active_blog_for_user( $friend_user->ID ); | |
| 842 | + $friend_url = set_url_scheme( $site->siteurl ); | |
| 843 | + } | |
| 844 | + } | |
| 845 | + | |
| 846 | + if ( ! $friend_user ) { | |
| 847 | + $friend_url = apply_filters( 'friends_rewrite_incoming_url', 'https://' . $friend_url, $friend_url ); | |
| 848 | + } | |
| 849 | + } | |
| 850 | + $friend_user_login = apply_filters( 'friends_suggest_user_login', User::get_user_login_for_url( $friend_url ), $friend_url ); | |
| 851 | + $friend_display_name = apply_filters( 'friends_suggest_display_name', User::get_display_name_for_url( $friend_url ), $friend_url ); | |
| 852 | + | |
| 853 | + $friend_user = get_user_by( 'login', $friend_user_login ); | |
| 854 | + | |
| 855 | + $args = array(); | |
| 856 | + if ( $friend_user ) { | |
| 857 | + $args['friends_multisite_user_login'] = $friend_user_login; | |
| 858 | + $args['friends_multisite_display_name'] = $friend_display_name; | |
| 859 | + } | |
| 860 | + | |
| 861 | + if ( ( isset( $vars['step2'] ) && isset( $vars['feeds'] ) && is_array( $vars['feeds'] ) ) || isset( $vars['step3'] ) ) { | |
| 862 | + $friend_user_login = trim( str_replace( ' ', '-', sanitize_user( $vars['user_login'] ) ), '-' ); | |
| 863 | + $friend_display_name = sanitize_text_field( $vars['display_name'] ); | |
| 864 | + if ( ! $friend_user_login ) { | |
| 865 | + // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 866 | + $errors->add( 'user_login', __( '<strong>Error</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) ); | |
| 867 | + } elseif ( ! is_multisite() && username_exists( $friend_user_login ) ) { | |
| 868 | + // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 869 | + $errors->add( 'user_login', __( '<strong>Error</strong>: This username is already registered. Please choose another one.' ) ); | |
| 870 | + } | |
| 871 | + | |
| 872 | + $feeds = $vars['feeds']; | |
| 873 | + if ( ! $errors->has_errors() ) { | |
| 874 | + $avatar = null; | |
| 875 | + $description = null; | |
| 876 | + foreach ( $feeds as $feed_details ) { | |
| 877 | + if ( ! $avatar && ! empty( $feed_details['avatar'] ) ) { | |
| 878 | + $avatar = $feed_details['avatar']; | |
| 879 | + } | |
| 880 | + if ( ! $description && ! empty( $feed_details['description'] ) ) { | |
| 881 | + $description = wp_encode_emoji( $feed_details['description'] ); | |
| 882 | + } | |
| 883 | + } | |
| 884 | + | |
| 885 | + $friend_user = User::create( $friend_user_login, 'subscription', $friend_url, $friend_display_name, $avatar, $description ); | |
| 886 | + | |
| 887 | + return $this->process_admin_add_friend_response( $friend_user, $vars ); | |
| 888 | + } | |
| 889 | + } else { | |
| 890 | + if ( str_starts_with( $friend_url, home_url() ) ) { | |
| 891 | + return new \WP_Error( 'friend-yourself', __( 'It seems like you sent a friend request to yourself.', 'friends' ) ); | |
| 892 | + } | |
| 893 | + | |
| 894 | + if ( preg_match( '#https://.*?@threads.net#', $friend_url ) ) { | |
| 895 | + return new \WP_Error( | |
| 896 | + 'threads-net', | |
| 897 | + sprintf( | |
| 898 | + // translators: %s is a URL. | |
| 899 | + __( '⚠️ This user has <a href="%s">not enabled Fediverse sharing on their Threads.net account</a>.', 'friends' ), | |
| 900 | + 'https://about.fb.com/news/2023/07/introducing-threads-new-app-text-sharing/' | |
| 901 | + ) | |
| 902 | + ); | |
| 903 | + } | |
| 904 | + | |
| 905 | + if ( ! Friends::check_url( $friend_url ) ) { | |
| 906 | + return new \WP_Error( 'invalid-url', __( 'You entered an invalid URL.', 'friends' ) ); | |
| 907 | + } | |
| 908 | + | |
| 909 | + $friend_user = User::get_user( $friend_user_login ); | |
| 910 | + if ( $friend_user && ! is_wp_error( $friend_user ) ) { | |
| 911 | + // translators: %s is the name of a friend / site. | |
| 912 | + return new \WP_Error( 'already-subscribed', sprintf( __( 'You are already subscribed to this site: %s', 'friends' ), '<a href="' . esc_url( $this->admin_edit_user_link( $friend_user->get_local_friends_page_url(), $friend_user ) ) . '">' . esc_html( $friend_user->display_name ) . '</a>' ) ); | |
| 913 | + } | |
| 914 | + | |
| 915 | + $feeds = $this->friends->feed->discover_available_feeds( $friend_url ); | |
| 916 | + if ( is_wp_error( $feeds ) ) { | |
| 917 | + return $feeds; | |
| 918 | + } | |
| 919 | + if ( ! $feeds ) { | |
| 920 | + return new \WP_Error( 'no-feed-found', __( 'No suitable feed was found at the provided address.', 'friends' ) ); | |
| 921 | + } | |
| 922 | + $has_subscribable_feeds = false; | |
| 923 | + $has_threads_net = false; | |
| 924 | + foreach ( $feeds as $url => $feed ) { | |
| 925 | + if ( 0 === strpos( $url, 'https://threads.net/' ) ) { | |
| 926 | + $has_threads_net = true; | |
| 927 | + } | |
| 928 | + if ( isset( $feed['autoselect'] ) && $feed['autoselect'] ) { | |
| 929 | + $has_subscribable_feeds = true; | |
| 930 | + break; | |
| 931 | + } | |
| 932 | + if ( 'unsupported' !== $feed['parser'] ) { | |
| 933 | + $has_subscribable_feeds = true; | |
| 934 | + break; | |
| 935 | + } | |
| 936 | + } | |
| 937 | + | |
| 938 | + if ( ! $has_subscribable_feeds && $has_threads_net ) { | |
| 939 | + $args['feeds_notice'] = sprintf( | |
| 940 | + // translators: %s is a URL. | |
| 941 | + __( '⚠️ This user has <a href="%s">not enabled Fediverse sharing on their Threads.net account</a>.', 'friends' ), | |
| 942 | + 'https://about.fb.com/news/2023/07/introducing-threads-new-app-text-sharing/' | |
| 943 | + ); | |
| 944 | + } | |
| 945 | + | |
| 946 | + $better_user_login = User::get_user_login_from_feeds( $feeds ); | |
| 947 | + if ( $better_user_login ) { | |
| 948 | + $friend_user_login = trim( $better_user_login, '-' ); | |
| 949 | + } | |
| 950 | + | |
| 951 | + $better_display_name = User::get_display_name_from_feeds( $feeds ); | |
| 952 | + if ( $better_display_name ) { | |
| 953 | + $friend_display_name = $better_display_name; | |
| 954 | + if ( ! $better_user_login ) { | |
| 955 | + $friend_user_login = trim( strtolower( str_replace( ' ', '-', sanitize_user( $better_display_name ) ) ), '-' ); | |
| 956 | + } | |
| 957 | + } | |
| 958 | + } | |
| 959 | + | |
| 960 | + if ( isset( $vars['quick-subscribe'] ) ) { | |
| 961 | + $vars['feeds'] = $feeds; | |
| 962 | + $vars['subscribe'] = array(); | |
| 963 | + foreach ( $feeds as $feed_url => $details ) { | |
| 964 | + if ( isset( $details['autoselect'] ) && $details['autoselect'] ) { | |
| 965 | + $vars['subscribe'][] = $feed_url; | |
| 966 | + } | |
| 967 | + } | |
| 968 | + | |
| 969 | + $avatar = null; | |
| 970 | + $description = null; | |
| 971 | + foreach ( $feeds as $feed_details ) { | |
| 972 | + if ( ! $avatar && ! empty( $feed_details['avatar'] ) ) { | |
| 973 | + $avatar = $feed_details['avatar']; | |
| 974 | + } | |
| 975 | + if ( ! $description && ! empty( $feed_details['description'] ) ) { | |
| 976 | + $description = $feed_details['description']; | |
| 977 | + } | |
| 978 | + } | |
| 979 | + | |
| 980 | + $friend_user = User::create( $friend_user_login, 'subscription', $friend_url, $friend_display_name, $avatar, $description ); | |
| 981 | + | |
| 982 | + return $this->process_admin_add_friend_response( $friend_user, $vars ); | |
| 983 | + } | |
| 984 | + | |
| 985 | + Friends::template_loader()->get_template_part( | |
| 986 | + 'admin/settings-header', | |
| 987 | + null, | |
| 988 | + array( | |
| 989 | + 'active' => 'add-friend-confirm', | |
| 990 | + 'title' => __( 'Add Friend', 'friends' ), | |
| 991 | + 'menu' => array( | |
| 992 | + '1. ' . __( 'Enter Details', 'friends' ) => array( | |
| 993 | + 'page' => 'add-friend', | |
| 994 | + 'url' => ! empty( $friend_url ) ? $friend_url : false, | |
| 995 | + ), | |
| 996 | + '2. ' . __( 'Confirm', 'friends' ) => 'add-friend-confirm', | |
| 997 | + ), | |
| 998 | + ) | |
| 999 | + ); | |
| 1000 | + | |
| 1001 | + if ( $errors->has_errors() ) { | |
| 1002 | + ?> | |
| 1003 | + <div id="message" class="updated notice is-dismissible"><p><?php echo wp_kses( $errors->get_error_message(), array( 'strong' => array() ) ); ?></p> | |
| 1004 | + </div> | |
| 1005 | + <?php | |
| 1006 | + } | |
| 1007 | + | |
| 1008 | + Friends::template_loader()->get_template_part( | |
| 1009 | + 'admin/select-feeds', | |
| 1010 | + null, | |
| 1011 | + array_merge( | |
| 1012 | + $args, | |
| 1013 | + array( | |
| 1014 | + 'friend_url' => $friend_url, | |
| 1015 | + 'friend_user_login' => $friend_user_login, | |
| 1016 | + 'friend_display_name' => $friend_display_name, | |
| 1017 | + 'post_formats' => array_merge( array( 'autodetect' => __( 'Autodetect Post Format', 'friends' ) ), get_post_format_strings() ), | |
| 1018 | + 'registered_parsers' => $this->friends->feed->get_registered_parsers(), | |
| 1019 | + 'feeds' => $feeds, | |
| 1020 | + ) | |
| 1021 | + ) | |
| 1022 | + ); | |
| 1023 | + } | |
| 1024 | + | |
| 1025 | + /** | |
| 1026 | + * Render the admin form for following someone. | |
| 1027 | + */ | |
| 1028 | + public function render_admin_add_friend() { | |
| 1029 | + if ( ! Friends::has_required_privileges() ) { | |
| 1030 | + wp_die( esc_html__( 'Sorry, you are not allowed to do this.', 'friends' ) ); | |
| 1031 | + } | |
| 1032 | + | |
| 1033 | + if ( ! empty( $_GET['preview'] ) ) { | |
| 1034 | + $url = sanitize_text_field( wp_unslash( $_GET['preview'] ) ); | |
| 1035 | + | |
| 1036 | + ?> | |
| 1037 | + <h1> | |
| 1038 | + <?php | |
| 1039 | + // translators: %s is a URL. | |
| 1040 | + echo esc_html( sprintf( __( 'Preview for %s', 'friends' ), $url ) ); | |
| 1041 | + ?> | |
| 1042 | + </h1> | |
| 1043 | + <?php | |
| 1044 | + | |
| 1045 | + if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'preview-feed' ) ) { | |
| 1046 | + ?> | |
| 1047 | + <div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'For security reasons, this preview is not available.', 'friends' ); ?></p> | |
| 1048 | + </div> | |
| 1049 | + <?php | |
| 1050 | + return; | |
| 1051 | + } | |
| 1052 | + $parser = false; | |
| 1053 | + if ( isset( $_GET['parser'] ) ) { | |
| 1054 | + $parser_name = $this->friends->feed->get_registered_parser( sanitize_text_field( wp_unslash( $_GET['parser'] ) ) ); | |
| 1055 | + $parser = $this->friends->feed->get_feed_parser( sanitize_text_field( wp_unslash( $_GET['parser'] ) ) ); | |
| 1056 | + } | |
| 1057 | + if ( ! $parser ) { | |
| 1058 | + ?> | |
| 1059 | + <div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'An unknown parser name was supplied.', 'friends' ); ?></p> | |
| 1060 | + </div> | |
| 1061 | + <?php | |
| 1062 | + return; | |
| 1063 | + } | |
| 1064 | + ?> | |
| 1065 | + <h3><?php esc_html_e( 'Parser Details', 'friends' ); ?></h3> | |
| 1066 | + <ul id="parser"> | |
| 1067 | + <li> | |
| 1068 | + <?php | |
| 1069 | + echo wp_kses( | |
| 1070 | + // translators: %s is the name of a parser, e.g. simplepie. | |
| 1071 | + sprintf( __( 'Parser: %s', 'friends' ), $parser_name ), | |
| 1072 | + array( | |
| 1073 | + 'a' => array( | |
| 1074 | + 'href' => array(), | |
| 1075 | + 'rel' => array(), | |
| 1076 | + 'target' => array(), | |
| 1077 | + ), | |
| 1078 | + ) | |
| 1079 | + ); | |
| 1080 | + ?> | |
| 1081 | + </li> | |
| 1082 | + </ul> | |
| 1083 | + <h3><?php esc_html_e( 'Items in the Feed', 'friends' ); ?></h3> | |
| 1084 | + | |
| 1085 | + <?php | |
| 1086 | + $feed_id = null; | |
| 1087 | + if ( isset( $_GET['feed'] ) ) { | |
| 1088 | + $feed_id = intval( $_GET['feed'] ); | |
| 1089 | + } | |
| 1090 | + $items = $this->friends->feed->preview( $parser, $url, $feed_id ); | |
| 1091 | + if ( is_wp_error( $items ) ) { | |
| 1092 | + ?> | |
| 1093 | + <div id="message" class="updated notice is-dismissible"><p><?php echo esc_html( $items->get_error_message() ); ?></p> | |
| 1094 | + </div> | |
| 1095 | + <?php | |
| 1096 | + return; | |
| 1097 | + } | |
| 1098 | + ?> | |
| 1099 | + | |
| 1100 | + <ul> | |
| 1101 | + <?php | |
| 1102 | + foreach ( $items as $item ) { | |
| 1103 | + $title = $item->title; | |
| 1104 | + if ( 'status' === $item->post_format ) { | |
| 1105 | + $title = wp_strip_all_tags( $item->content ); | |
| 1106 | + } | |
| 1107 | + ?> | |
| 1108 | + <li> | |
| 1109 | + <?php if ( $title ) : ?> | |
| 1110 | + <details><summary> | |
| 1111 | + <?php endif; ?> | |
| 1112 | + <a href="<?php echo esc_url( $item->permalink ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $item->date ); ?></a> (author: <?php echo esc_html( $item->author ); ?>, type: <?php echo esc_html( $item->post_format ); ?>): | |
| 1113 | + <?php if ( $title ) : ?> | |
| 1114 | + <a href="<?php echo esc_url( $item->permalink ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $title ); ?></a> <?php echo esc_html( str_word_count( wp_strip_all_tags( $item->content ) ) ); ?> words</summary> | |
| 1115 | + <?php else : ?> | |
| 1116 | + <p> | |
| 1117 | + <?php endif; ?> | |
| 1118 | + <?php echo esc_textarea( $item->content ); ?> | |
| 1119 | + <?php if ( $title ) : ?> | |
| 1120 | + </details> | |
| 1121 | + <?php else : ?> | |
| 1122 | + </p> | |
| 1123 | + <?php endif; ?> | |
| 1124 | + </li> | |
| 1125 | + <?php | |
| 1126 | + } | |
| 1127 | + ?> | |
| 1128 | + </ul> | |
| 1129 | + <?php | |
| 1130 | + return; | |
| 1131 | + } | |
| 1132 | + | |
| 1133 | + if ( apply_filters( 'friends_debug', false ) && isset( $_GET['next'] ) ) { | |
| 1134 | + $_POST = $_REQUEST; // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 1135 | + $_POST['_wpnonce'] = wp_create_nonce( 'add-friend' ); | |
| 1136 | + if ( ! empty( $_POST['url'] ) && ! isset( $_POST['friend_url'] ) ) { | |
| 1137 | + $friend_url = sanitize_text_field( wp_unslash( $_POST['url'] ) ); | |
| 1138 | + $parsed_url = wp_parse_url( $friend_url ); | |
| 1139 | + if ( isset( $parsed_url['host'] ) ) { | |
| 1140 | + if ( ! isset( $parsed_url['scheme'] ) ) { | |
| 1141 | + $friend_url = 'https://' . ltrim( $friend_url, '/' ); | |
| 1142 | + } | |
| 1143 | + } | |
| 1144 | + $_POST['friend_url'] = $friend_url; | |
| 1145 | + } | |
| 1146 | + } | |
| 1147 | + | |
| 1148 | + $response = null; | |
| 1149 | + $postdata = apply_filters( 'friends_add_friend_postdata', $_POST ); | |
| 1150 | + if ( ! empty( $postdata ) ) { | |
| 1151 | + if ( ! wp_verify_nonce( sanitize_key( $postdata['_wpnonce'] ), 'add-friend' ) ) { | |
| 1152 | + $response = new \WP_Error( 'invalid-nonce', __( 'For security reasons, please verify the URL and click next if you want to proceed.', 'friends' ) ); | |
| 1153 | + } else { | |
| 1154 | + $response = $this->process_admin_add_friend( $postdata ); | |
| 1155 | + } | |
| 1156 | + if ( is_wp_error( $response ) ) { | |
| 1157 | + ?> | |
| 1158 | + <div id="message" class="updated notice is-dismissible"><p> | |
| 1159 | + <?php | |
| 1160 | + $message = $response->get_error_message(); | |
| 1161 | + if ( $response->get_error_data() ) { | |
| 1162 | + $message .= ' (' . $response->get_error_data() . ')'; | |
| 1163 | + } | |
| 1164 | + echo wp_kses( | |
| 1165 | + $message, | |
| 1166 | + array( | |
| 1167 | + 'strong' => array(), | |
| 1168 | + 'a' => array( | |
| 1169 | + 'href' => array(), | |
| 1170 | + 'rel' => array(), | |
| 1171 | + 'target' => array(), | |
| 1172 | + ), | |
| 1173 | + ) | |
| 1174 | + ); | |
| 1175 | + ?> | |
| 1176 | + </p> | |
| 1177 | + </div> | |
| 1178 | + <?php | |
| 1179 | + } | |
| 1180 | + if ( is_null( $response ) ) { | |
| 1181 | + return; | |
| 1182 | + } | |
| 1183 | + } | |
| 1184 | + | |
| 1185 | + $args = array( | |
| 1186 | + 'friend_url' => '', | |
| 1187 | + 'add-friends-placeholder' => apply_filters( 'friends_add_friends_input_placeholder', __( 'Enter URL', 'friends' ) ), | |
| 1188 | + ); | |
| 1189 | + | |
| 1190 | + if ( ! empty( $_REQUEST['url'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 1191 | + $friend_url = sanitize_text_field( wp_unslash( $_REQUEST['url'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 1192 | + $parsed_url = wp_parse_url( $friend_url ); | |
| 1193 | + if ( isset( $parsed_url['host'] ) ) { | |
| 1194 | + if ( ! isset( $parsed_url['scheme'] ) ) { | |
| 1195 | + $args['friend_url'] = apply_filters( 'friends_rewrite_incoming_url', 'https://' . ltrim( $friend_url, '/' ), $friend_url, $parsed_url ); | |
| 1196 | + } else { | |
| 1197 | + $args['friend_url'] = $friend_url; | |
| 1198 | + } | |
| 1199 | + } elseif ( class_exists( 'Friends\Feed_Parser_ActivityPub' ) && preg_match( '/^@?' . Feed_Parser_ActivityPub::ACTIVITYPUB_USERNAME_REGEXP . '$/i', $friend_url ) ) { | |
| 1200 | + $args['friend_url'] = $friend_url; | |
| 1201 | + } | |
| 1202 | + } | |
| 1203 | + | |
| 1204 | + Friends::template_loader()->get_template_part( | |
| 1205 | + 'admin/settings-header', | |
| 1206 | + null, | |
| 1207 | + array( | |
| 1208 | + 'active' => 'add-friend', | |
| 1209 | + 'title' => __( 'Add Friend', 'friends' ), | |
| 1210 | + 'menu' => array( | |
| 1211 | + '1. ' . __( 'Enter Details', 'friends' ) => array( | |
| 1212 | + 'page' => 'add-friend', | |
| 1213 | + 'url' => ! empty( $friend_url ) ? $friend_url : false, | |
| 1214 | + ), | |
| 1215 | + '2. ' . __( 'Confirm', 'friends' ) => false, | |
| 1216 | + ), | |
| 1217 | + ) | |
| 1218 | + ); | |
| 1219 | + | |
| 1220 | + Friends::template_loader()->get_template_part( 'admin/add-friend', null, $args ); | |
| 1221 | + | |
| 1222 | + Friends::template_loader()->get_template_part( | |
| 1223 | + 'admin/latest-friends', | |
| 1224 | + null, | |
| 1225 | + array( | |
| 1226 | + 'friend_requests' => User_Query::recent_friends_subscriptions( 25 )->get_results(), | |
| 1227 | + ) | |
| 1228 | + ); | |
| 1229 | + Friends::template_loader()->get_template_part( 'admin/settings-footer', null, $args ); | |
| 1230 | + } | |
| 1231 | + | |
| 1232 | + /** | |
| 1233 | + * Display admin notice about a new version. | |
| 1234 | + */ | |
| 1235 | + public function admin_notice_welcome() { | |
| 1236 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 1237 | + return; | |
| 1238 | + } | |
| 1239 | + | |
| 1240 | + if ( isset( $_GET['page'] ) && 'friends' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 1241 | + return; | |
| 1242 | + } | |
| 1243 | + | |
| 1244 | + $version = get_option( 'friends_welcome_version' ); | |
| 1245 | + $url = admin_url( 'admin.php?page=friends' ); | |
| 1246 | + ?> | |
| 1247 | + <div class="friends-notice notice notice-info"> | |
| 1248 | + <p> | |
| 1249 | + <b><?php esc_html_e( 'Friends', 'friends' ); ?></b> | |
| 1250 | + <?php | |
| 1251 | + echo wp_kses( | |
| 1252 | + sprintf( | |
| 1253 | + // translators: %1$s is the version number, %2$s is a URL to the What's New page. | |
| 1254 | + __( '— You have been updated to version %1$s! <a href="%2$s">See what\'s new and check the migration status</a>.', 'friends' ), | |
| 1255 | + esc_html( $version ), | |
| 1256 | + esc_url( $url ) | |
| 1257 | + ), | |
| 1258 | + array( 'a' => array( 'href' => array() ) ) | |
| 1259 | + ); | |
| 1260 | + ?> | |
| 1261 | + </p> | |
| 1262 | + </div> | |
| 1263 | + <?php | |
| 1264 | + } | |
| 1265 | + | |
| 1266 | + /** | |
| 633 | 1267 | * Render the Friends Admin settings page |
| 634 | 1268 | */ |
| 635 | 1269 | public function render_admin_settings() { |
| 636 | 1270 | Friends::template_loader()->get_template_part( |
| @@ -637,14 +1271,13 @@ | ||
| 637 | 1271 | 'admin/settings-header', |
| 638 | 1272 | null, |
| 639 | 1273 | array( |
| 640 | 1274 | 'active' => 'friends-settings', |
| 641 | - 'title' => __( 'Friends', 'friends' ), | |
| 642 | 1275 | ) |
| 643 | 1276 | ); |
| 644 | 1277 | $this->check_admin_settings(); |
| 645 | 1278 | |
| 646 | - if ( isset( $_GET['updated'] ) ) { | |
| 1279 | + if ( isset( $_GET['updated'] ) && boolval( $_GET['updated'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 647 | 1280 | ?> |
| 648 | 1281 | <div id="message" class="updated notice is-dismissible"><p> |
| 649 | 1282 | <?php |
| 650 | 1283 | esc_html_e( 'Your settings were updated.', 'friends' ); |
| @@ -653,8 +1286,12 @@ | ||
| 653 | 1286 | <?php |
| 654 | 1287 | } |
| 655 | 1288 | |
| 656 | 1289 | $post_stats = Friends::get_post_stats(); |
| 1290 | + $post_type_themes = array(); | |
| 1291 | + foreach ( get_post_format_slugs() as $slug ) { | |
| 1292 | + $post_type_themes[ 'frontend_theme_' . $slug ] = get_user_option( 'friends_frontend_theme_' . $slug ); | |
| 1293 | + } | |
| 657 | 1294 | |
| 658 | 1295 | Friends::template_loader()->get_template_part( |
| 659 | 1296 | 'admin/settings', |
| 660 | 1297 | null, |
| @@ -659,20 +1296,27 @@ | ||
| 659 | 1296 | 'admin/settings', |
| 660 | 1297 | null, |
| 661 | 1298 | array_merge( |
| 662 | 1299 | Friends::get_post_stats(), |
| 1300 | + $post_type_themes, | |
| 663 | 1301 | array( |
| 664 | - 'force_enable_post_formats' => get_option( 'friends_force_enable_post_formats' ), | |
| 665 | - 'post_format_strings' => get_post_format_strings(), | |
| 666 | - 'limit_homepage_post_format' => get_option( 'friends_limit_homepage_post_format', false ), | |
| 667 | - 'expose_post_format_feeds' => get_option( 'friends_expose_post_format_feeds' ), | |
| 668 | - 'enable_wp_friendships' => get_option( 'friends_enable_wp_friendships' ), | |
| 669 | - 'retention_days' => Friends::get_retention_days(), | |
| 670 | - 'retention_number' => Friends::get_retention_number(), | |
| 671 | - 'retention_days_enabled' => get_option( 'friends_enable_retention_days' ), | |
| 672 | - 'retention_number_enabled' => get_option( 'friends_enable_retention_number' ), | |
| 673 | - 'frontend_default_view' => get_option( 'friends_frontend_default_view', 'expanded' ), | |
| 674 | - 'blocks_everywhere' => get_user_option( 'friends_blocks_everywhere' ), | |
| 1302 | + 'force_enable_post_formats' => get_option( 'friends_force_enable_post_formats' ), | |
| 1303 | + 'post_format_strings' => get_post_format_strings(), | |
| 1304 | + 'limit_homepage_post_format' => get_option( 'friends_limit_homepage_post_format', false ), | |
| 1305 | + 'expose_post_format_feeds' => get_option( 'friends_expose_post_format_feeds' ), | |
| 1306 | + 'compose_post_format' => get_option( 'friends_compose_post_format', 'status' ), | |
| 1307 | + 'exclude_compose_format_from_feed' => get_option( 'friends_exclude_compose_format_from_feed' ), | |
| 1308 | + 'main_user_id' => Friends::get_main_friend_user_id(), | |
| 1309 | + 'potential_main_users' => User_Query::all_admin_users(), | |
| 1310 | + 'disable_auto_tagging' => get_option( 'friends_disable_auto_tagging' ), | |
| 1311 | + 'disable_link_previews' => get_option( 'friends_disable_link_previews' ), | |
| 1312 | + 'retention_days' => Friends::get_retention_days(), | |
| 1313 | + 'retention_number' => Friends::get_retention_number(), | |
| 1314 | + 'retention_days_enabled' => get_option( 'friends_enable_retention_days' ), | |
| 1315 | + 'retention_number_enabled' => get_option( 'friends_enable_retention_number' ), | |
| 1316 | + 'retention_delete_reacted' => get_option( 'friends_retention_delete_reacted' ), | |
| 1317 | + 'frontend_default_view' => get_user_option( 'friends_frontend_default_view', get_current_user_id() ), | |
| 1318 | + 'frontend_theme' => get_user_option( 'friends_frontend_theme' ), | |
| 675 | 1319 | ) |
| 676 | 1320 | ) |
| 677 | 1321 | ); |
| 678 | 1322 | |
| @@ -690,19 +1334,18 @@ | ||
| 690 | 1334 | if ( ! isset( $_GET['user'] ) ) { |
| 691 | 1335 | wp_die( esc_html__( 'Invalid user.', 'friends' ) ); |
| 692 | 1336 | } |
| 693 | 1337 | |
| 694 | - $friend = User::get_by_username( $_GET['user'] ); | |
| 1338 | + if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'edit-friend-rules-' . sanitize_user( wp_unslash( $_GET['user'] ) ) ) ) { | |
| 1339 | + wp_die( esc_html__( 'Invalid nonce.', 'friends' ) ); | |
| 1340 | + } | |
| 1341 | + | |
| 1342 | + $friend = User::get_by_username( sanitize_user( wp_unslash( $_GET['user'] ) ) ); | |
| 695 | 1343 | if ( ! $friend || is_wp_error( $friend ) ) { |
| 696 | 1344 | wp_die( esc_html__( 'Invalid username.', 'friends' ) ); |
| 697 | 1345 | } |
| 698 | 1346 | |
| 699 | - if ( | |
| 700 | - ! $friend->has_cap( 'friend_request' ) && | |
| 701 | - ! $friend->has_cap( 'pending_friend_request' ) && | |
| 702 | - ! $friend->has_cap( 'friend' ) && | |
| 703 | - ! $friend->has_cap( 'subscription' ) | |
| 704 | - ) { | |
| 1347 | + if ( ! $friend->has_cap( 'subscription' ) ) { | |
| 705 | 1348 | wp_die( esc_html__( 'This is not a user related to this plugin.', 'friends' ) ); |
| 706 | 1349 | } |
| 707 | 1350 | |
| 708 | 1351 | return $friend; |
| @@ -714,18 +1357,24 @@ | ||
| 714 | 1357 | public function process_admin_edit_friend_rules() { |
| 715 | 1358 | $friend = $this->check_admin_edit_friend_rules(); |
| 716 | 1359 | $arg = 'updated'; |
| 717 | 1360 | $arg_value = 1; |
| 718 | - if ( isset( $_POST['friend-rules-raw'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'friend-rules-raw-' . $friend->user_login ) ) { | |
| 719 | - $rules = $this->friends->feed->validate_feed_rules( json_decode( stripslashes( $_POST['rules'] ), true ) ); | |
| 1361 | + if ( isset( $_POST['_wpnonce'] ) && ! empty( $_POST['friend-rules-raw'] ) && wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'friend-rules-raw-' . $friend->user_login ) ) { | |
| 1362 | + $rules = validate_feed_rules( wp_unslash( $_POST['friend-rules-raw'] ) ); | |
| 720 | 1363 | if ( false === $rules ) { |
| 721 | 1364 | $arg = 'error'; |
| 722 | 1365 | } else { |
| 723 | 1366 | $friend->update_user_option( 'friends_feed_rules', $rules ); |
| 724 | 1367 | } |
| 725 | - } elseif ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'edit-friend-rules-' . $friend->user_login ) ) { | |
| 726 | - $friend->update_user_option( 'friends_feed_catch_all', $this->friends->feed->validate_feed_catch_all( $_POST['catch_all'] ) ); | |
| 727 | - $friend->update_user_option( 'friends_feed_rules', $this->friends->feed->validate_feed_rules( $_POST['rules'] ) ); | |
| 1368 | + } elseif ( isset( $_POST['_wpnonce'] ) && ! empty( $_POST['rules'] ) && ! empty( $_POST['catch_all'] ) && wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'edit-friend-rules-' . sanitize_user( $friend->user_login ) ) ) { | |
| 1369 | + $friend->update_user_option( | |
| 1370 | + 'friends_feed_catch_all', | |
| 1371 | + validate_feed_catch_all( wp_unslash( $_POST['catch_all'] ) ) | |
| 1372 | + ); | |
| 1373 | + $friend->update_user_option( | |
| 1374 | + 'friends_feed_rules', | |
| 1375 | + validate_feed_rules( wp_unslash( $_POST['rules'] ) ) | |
| 1376 | + ); | |
| 728 | 1377 | } else { |
| 729 | 1378 | return; |
| 730 | 1379 | } |
| 731 | 1380 | |
| @@ -731,9 +1380,9 @@ | ||
| 731 | 1380 | |
| 732 | 1381 | if ( isset( $_GET['_wp_http_referer'] ) ) { |
| 733 | 1382 | wp_safe_redirect( wp_get_referer() ); |
| 734 | 1383 | } else { |
| 735 | - wp_safe_redirect( add_query_arg( $arg, $arg_value, remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ); | |
| 1384 | + wp_safe_redirect( add_query_arg( $arg, $arg_value, remove_query_arg( '_wp_http_referer' ) ) ); | |
| 736 | 1385 | } |
| 737 | 1386 | exit; |
| 738 | 1387 | } |
| 739 | 1388 | |
| @@ -764,12 +1413,14 @@ | ||
| 764 | 1413 | 'action' => in_array( $catch_all, array( 'trash', 'delete' ), true ) ? 'accept' : 'trash', |
| 765 | 1414 | 'replace' => '', |
| 766 | 1415 | ); |
| 767 | 1416 | |
| 768 | - if ( isset( $_GET['post'] ) && intval( $_GET['post'] ) ) { | |
| 769 | - $post = get_post( intval( $_GET['post'] ) ); | |
| 770 | - } else { | |
| 771 | - $post = null; | |
| 1417 | + if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'edit-friend-rules-' . sanitize_user( $friend->user_login ) ) ) { | |
| 1418 | + if ( isset( $_GET['post'] ) && intval( $_GET['post'] ) ) { | |
| 1419 | + $post = get_post( intval( $_GET['post'] ) ); | |
| 1420 | + } else { | |
| 1421 | + $post = null; | |
| 1422 | + } | |
| 772 | 1423 | } |
| 773 | 1424 | |
| 774 | 1425 | $args = array( |
| 775 | 1426 | 'rules' => $rules, |
| @@ -793,16 +1444,28 @@ | ||
| 793 | 1444 | public function ajax_preview_friend_rules() { |
| 794 | 1445 | if ( ! Friends::has_required_privileges() ) { |
| 795 | 1446 | wp_die( -1 ); |
| 796 | 1447 | } |
| 1448 | + if ( ! isset( $_GET['user'] ) ) { | |
| 1449 | + wp_die( esc_html__( 'Invalid user.', 'friends' ) ); | |
| 1450 | + } | |
| 797 | 1451 | |
| 1452 | + check_ajax_referer( 'edit-friend-rules-' . sanitize_user( wp_unslash( $_GET['user'] ) ) ); | |
| 1453 | + | |
| 798 | 1454 | if ( isset( $_GET['post'] ) && intval( $_GET['post'] ) ) { |
| 799 | 1455 | $post = get_post( intval( $_GET['post'] ) ); |
| 800 | 1456 | } else { |
| 801 | 1457 | $post = null; |
| 802 | 1458 | } |
| 803 | - | |
| 804 | - $this->render_preview_friend_rules( $_POST['rules'], $_POST['catch_all'], $post ); | |
| 1459 | + $rules = array(); | |
| 1460 | + if ( isset( $_POST['rules'] ) ) { | |
| 1461 | + $rules = validate_feed_rules( wp_unslash( $_POST['rules'] ) ); | |
| 1462 | + } | |
| 1463 | + $catch_all = array(); | |
| 1464 | + if ( isset( $_POST['catch_all'] ) ) { | |
| 1465 | + $catch_all = validate_feed_rules( wp_unslash( $_POST['catch_all'] ) ); | |
| 1466 | + } | |
| 1467 | + $this->render_preview_friend_rules( $rules, $catch_all, $post ); | |
| 805 | 1468 | wp_die( 1 ); |
| 806 | 1469 | } |
| 807 | 1470 | |
| 808 | 1471 | /** |
| @@ -811,123 +1474,24 @@ | ||
| 811 | 1474 | public function ajax_fetch_feeds() { |
| 812 | 1475 | if ( ! isset( $_POST['friend'] ) ) { |
| 813 | 1476 | wp_send_json_error( 'missing-parameters' ); |
| 814 | 1477 | } |
| 815 | - check_ajax_referer( 'fetch-feeds-' . $_POST['friend'] ); | |
| 816 | 1478 | |
| 817 | - $friend_user = User::get_by_username( $_POST['friend'] ); | |
| 1479 | + check_ajax_referer( 'fetch-feeds-' . sanitize_user( wp_unslash( $_POST['friend'] ) ) ); | |
| 1480 | + | |
| 1481 | + $friend_user = User::get_by_username( sanitize_user( wp_unslash( $_POST['friend'] ) ) ); | |
| 818 | 1482 | if ( ! $friend_user ) { |
| 819 | 1483 | wp_send_json_error( 'unknown-user' ); |
| 820 | 1484 | } |
| 821 | 1485 | |
| 1486 | + add_filter( 'notify_about_new_friend_post', '__return_false', 999 ); | |
| 1487 | + | |
| 822 | 1488 | $friend_user->retrieve_posts_from_active_feeds(); |
| 823 | 1489 | |
| 824 | 1490 | wp_send_json_success(); |
| 825 | 1491 | } |
| 826 | 1492 | |
| 827 | - | |
| 828 | 1493 | /** |
| 829 | - * Respond to the Ajax request to refresh the link token | |
| 830 | - */ | |
| 831 | - public function ajax_refresh_link_token() { | |
| 832 | - if ( ! isset( $_POST['url'] ) || ! isset( $_POST['friend'] ) ) { | |
| 833 | - wp_send_json_error( 'missing-parameters' ); | |
| 834 | - } | |
| 835 | - $url = $_POST['url']; | |
| 836 | - check_ajax_referer( 'auth-link-' . $url ); | |
| 837 | - | |
| 838 | - if ( ! friends::has_required_privileges() ) { | |
| 839 | - wp_send_json_error( 'missing-priviledges' ); | |
| 840 | - } | |
| 841 | - | |
| 842 | - $friend_user = User::get_user( $_POST['friend'] ); | |
| 843 | - if ( ! $friend_user ) { | |
| 844 | - wp_send_json_error( 'unknown-user' ); | |
| 845 | - } | |
| 846 | - | |
| 847 | - wp_send_json_success( | |
| 848 | - array( | |
| 849 | - 'success' => true, | |
| 850 | - 'data' => array( | |
| 851 | - 'token' => $friend_user->get_friend_auth(), | |
| 852 | - ), | |
| 853 | - ) | |
| 854 | - ); | |
| 855 | - } | |
| 856 | - | |
| 857 | - public function render_friends_list() { | |
| 858 | - Friends::template_loader()->get_template_part( | |
| 859 | - 'admin/settings-header', | |
| 860 | - null, | |
| 861 | - array( | |
| 862 | - 'menu' => array( | |
| 863 | - __( 'Your Friends & Subscriptions', 'friends' ) => 'friends-list', | |
| 864 | - __( 'Your Friend Requests', 'friends' ) => 'friends-list-requests', | |
| 865 | - ), | |
| 866 | - 'active' => $_GET['page'], | |
| 867 | - 'title' => __( 'Friends', 'friends' ), | |
| 868 | - ) | |
| 869 | - ); | |
| 870 | - | |
| 871 | - if ( isset( $_GET['page'] ) && 'friends-list-requests' === $_GET['page'] ) { | |
| 872 | - echo '<p>'; | |
| 873 | - echo wp_kses( | |
| 874 | - sprintf( | |
| 875 | - // translators: %1$s is a URL, %2$s is the translated text "Your Friends & Subscriptions". | |
| 876 | - __( 'These are your current friend requests. To see all your friends and subscriptions, go to <a href="%1$s">%2$s</a>.', 'friends' ), | |
| 877 | - self_admin_url( 'admin.php?page=friends-list' ), | |
| 878 | - __( 'Your Friends & Subscriptions', 'friends' ) | |
| 879 | - ), | |
| 880 | - array( | |
| 881 | - 'a' => array( | |
| 882 | - 'href' => array(), | |
| 883 | - ), | |
| 884 | - ) | |
| 885 | - ); | |
| 886 | - echo '</p>'; | |
| 887 | - $query = User_Query::all_friend_requests(); | |
| 888 | - } else { | |
| 889 | - $query = User_Query::all_associated_users(); | |
| 890 | - } | |
| 891 | - | |
| 892 | - if ( isset( $_GET['deleted'] ) ) { | |
| 893 | - ?> | |
| 894 | - <div id="message" class="updated notice is-dismissible"><p> | |
| 895 | - <?php | |
| 896 | - echo esc_html( | |
| 897 | - sprintf( | |
| 898 | - // translators: % s is a username. | |
| 899 | - __( '%s was deleted.', 'friends' ), | |
| 900 | - $_GET['deleted'] | |
| 901 | - ) | |
| 902 | - ); | |
| 903 | - ?> | |
| 904 | - </p></div> | |
| 905 | - <?php | |
| 906 | - } elseif ( isset( $_GET['error'] ) ) { | |
| 907 | - ?> | |
| 908 | - <div id="message" class="updated error is-dismissible"><p> | |
| 909 | - <?php | |
| 910 | - esc_html_e( 'An error occurred.', 'friends' ); | |
| 911 | - echo ' '; | |
| 912 | - echo esc_html( $_GET['error'] ); | |
| 913 | - ?> | |
| 914 | - </p></div> | |
| 915 | - <?php | |
| 916 | - } | |
| 917 | - | |
| 918 | - Friends::template_loader()->get_template_part( | |
| 919 | - 'admin/friends-list', | |
| 920 | - null, | |
| 921 | - array( | |
| 922 | - 'friends' => $query->get_results(), | |
| 923 | - ) | |
| 924 | - ); | |
| 925 | - | |
| 926 | - Friends::template_loader()->get_template_part( 'admin/settings-footer' ); | |
| 927 | - } | |
| 928 | - | |
| 929 | - /** | |
| 930 | 1494 | * Render the Friend rules preview |
| 931 | 1495 | * |
| 932 | 1496 | * @param array $rules The rules to apply. |
| 933 | 1497 | * @param string $catch_all The catch all behavior. |
| @@ -932,9 +1496,9 @@ | ||
| 932 | 1496 | * @param array $rules The rules to apply. |
| 933 | 1497 | * @param string $catch_all The catch all behavior. |
| 934 | 1498 | * @param \WP_Post $post The post. |
| 935 | 1499 | */ |
| 936 | - public function render_preview_friend_rules( $rules, $catch_all, \WP_Post $post = null ) { | |
| 1500 | + public function render_preview_friend_rules( $rules, $catch_all, ?\WP_Post $post = null ) { | |
| 937 | 1501 | $friend = $this->check_admin_edit_friend_rules(); |
| 938 | 1502 | $friend_posts = new \WP_Query(); |
| 939 | 1503 | |
| 940 | 1504 | $friend_posts->set( 'post_type', Friends::CPT ); |
| @@ -962,13 +1526,13 @@ | ||
| 962 | 1526 | if ( ! friends::has_required_privileges() ) { |
| 963 | 1527 | wp_die( esc_html__( 'Sorry, you are not allowed to edit this user.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 964 | 1528 | } |
| 965 | 1529 | |
| 966 | - if ( ! isset( $_GET['user'] ) ) { | |
| 1530 | + if ( ! isset( $_GET['user'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 967 | 1531 | wp_die( esc_html__( 'Invalid user.', 'friends' ) ); |
| 968 | 1532 | } |
| 969 | 1533 | |
| 970 | - $friend = User::get_by_username( $_GET['user'] ); | |
| 1534 | + $friend = User::get_by_username( sanitize_user( wp_unslash( $_GET['user'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 971 | 1535 | if ( ! $friend || is_wp_error( $friend ) ) { |
| 972 | 1536 | wp_die( esc_html__( 'Invalid username.', 'friends' ) ); |
| 973 | 1537 | } |
| 974 | 1538 | |
| @@ -986,86 +1550,32 @@ | ||
| 986 | 1550 | $friend = $this->check_admin_edit_friend(); |
| 987 | 1551 | $arg = 'updated'; |
| 988 | 1552 | $arg_value = 1; |
| 989 | 1553 | |
| 990 | - if ( isset( $_GET['convert-to-user'] ) && wp_verify_nonce( $_GET['convert-to-user'], 'convert-to-user-' . $friend->user_login ) ) { | |
| 991 | - if ( $friend instanceof Subscription ) { | |
| 992 | - Subscription::convert_to_user( $friend ); | |
| 993 | - } | |
| 994 | - } elseif ( isset( $_GET['convert-from-user'] ) && wp_verify_nonce( $_GET['convert-from-user'], 'convert-from-user-' . $friend->user_login ) ) { | |
| 995 | - if ( $friend instanceof User && ! $friend instanceof Subscription ) { | |
| 996 | - if ( $friend->has_cap( 'friends_plugin' ) && ! $friend->has_cap( 'friend' ) && ! $friend->has_cap( 'pending_friend_request' ) && ! $friend->has_cap( 'friend_request' ) ) { | |
| 997 | - Subscription::convert_from_user( $friend ); | |
| 998 | - } else { | |
| 999 | - $arg = 'error'; | |
| 1000 | - $arg_value = __( 'A friend cannot be converted to a virtual user.', 'friends' ); | |
| 1554 | + if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'edit-friend-' . $friend->user_login ) ) { | |
| 1555 | + if ( isset( $_POST['friends_display_name'] ) ) { | |
| 1556 | + $friends_display_name = trim( sanitize_text_field( wp_unslash( $_POST['friends_display_name'] ) ) ); | |
| 1557 | + if ( $friends_display_name ) { | |
| 1558 | + $friend->first_name = $friends_display_name; | |
| 1559 | + $friend->display_name = $friends_display_name; | |
| 1001 | 1560 | } |
| 1002 | 1561 | } |
| 1003 | - } elseif ( isset( $_GET['accept-friend-request'] ) && wp_verify_nonce( $_GET['accept-friend-request'], 'accept-friend-request-' . $friend->user_login ) ) { | |
| 1004 | - if ( $friend->has_cap( 'friend_request' ) ) { | |
| 1005 | - $friend->set_role( get_option( 'friends_default_friend_role', 'friend' ) ); | |
| 1006 | - $arg = 'friend'; | |
| 1562 | + if ( isset( $_POST['friends_description'] ) ) { | |
| 1563 | + $friend->description = trim( sanitize_text_field( wp_unslash( $_POST['friends_description'] ) ) ); | |
| 1007 | 1564 | } |
| 1008 | - } elseif ( isset( $_GET['add-friend'] ) && wp_verify_nonce( $_GET['add-friend'], 'add-friend-' . $friend->user_login ) ) { | |
| 1009 | - if ( $friend->has_cap( 'pending_friend_request' ) || $friend->has_cap( 'subscription' ) ) { | |
| 1010 | - $rest_url = $this->friends->rest->discover_rest_url( $friend->user_url ); | |
| 1011 | - if ( ! is_wp_error( $rest_url ) ) { | |
| 1012 | - $response = $this->send_friend_request( $rest_url, $friend->user_login, $friend->user_url, $friend->display_name ); | |
| 1013 | - } else { | |
| 1014 | - $response = $rest_url; | |
| 1565 | + if ( isset( $_POST['user_url'] ) ) { | |
| 1566 | + $user_url = sanitize_text_field( wp_unslash( $_POST['user_url'] ) ); | |
| 1567 | + if ( filter_var( $user_url, FILTER_VALIDATE_URL ) ) { | |
| 1568 | + $friend->user_url = $user_url; | |
| 1015 | 1569 | } |
| 1016 | - | |
| 1017 | - if ( is_wp_error( $response ) ) { | |
| 1018 | - $arg = 'error'; | |
| 1019 | - } elseif ( $response instanceof User ) { | |
| 1020 | - if ( $response->has_cap( 'pending_friend_request' ) ) { | |
| 1021 | - $arg = 'sent-request'; | |
| 1022 | - // translators: %s is a Site URL. | |
| 1023 | - $arg_value = wp_kses( sprintf( __( 'Friendship requested for site %s.', 'friends' ), $response->get_local_friends_page_url() ), array( 'a' => array( 'href' => array() ) ) ); | |
| 1024 | - } elseif ( $response->has_cap( 'friend' ) ) { | |
| 1025 | - $arg = 'friend'; | |
| 1026 | - $arg_value = 1; | |
| 1027 | - } elseif ( $response->has_cap( 'subscription' ) ) { | |
| 1028 | - $arg = 'subscribed'; | |
| 1029 | - $arg_value = 1; | |
| 1030 | - } | |
| 1570 | + } | |
| 1571 | + if ( isset( $_POST['friends_user_login'] ) ) { | |
| 1572 | + $new_user_login = User::sanitize_username( sanitize_text_field( wp_unslash( $_POST['friends_user_login'] ) ) ); | |
| 1573 | + if ( $new_user_login && $new_user_login !== $friend->user_login ) { | |
| 1574 | + $friend->update_user_login( $new_user_login ); | |
| 1031 | 1575 | } |
| 1032 | 1576 | } |
| 1033 | - } elseif ( isset( $_GET['change-to-restricted-friend'] ) && wp_verify_nonce( $_GET['change-to-restricted-friend'], 'change-to-restricted-friend-' . $friend->user_login ) ) { | |
| 1034 | - if ( $friend->has_cap( 'friend' ) ) { | |
| 1035 | - $friend->set_role( 'acquaintance' ); | |
| 1036 | - } | |
| 1037 | - } elseif ( isset( $_GET['change-to-friend'] ) && wp_verify_nonce( $_GET['change-to-friend'], 'change-to-friend-' . $friend->user_login ) ) { | |
| 1038 | - if ( $friend->has_cap( 'acquaintance' ) ) { | |
| 1039 | - $friend->set_role( 'friend' ); | |
| 1040 | - } | |
| 1041 | - } elseif ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'edit-friend-' . $friend->user_login ) ) { | |
| 1042 | - if ( trim( $_POST['friends_display_name'] ) ) { | |
| 1043 | - $friend->first_name = trim( $_POST['friends_display_name'] ); | |
| 1044 | - $friend->display_name = trim( $_POST['friends_display_name'] ); | |
| 1045 | - } | |
| 1046 | - | |
| 1047 | - $friend->description = trim( $_POST['friends_description'] ); | |
| 1048 | - if ( trim( $_POST['user_url'] ) && filter_var( $_POST['user_url'], FILTER_VALIDATE_URL ) ) { | |
| 1049 | - $friend->user_url = $_POST['user_url']; | |
| 1050 | - } | |
| 1051 | 1577 | $friend->save(); |
| 1052 | - | |
| 1053 | - $hide_from_friends_page = get_user_option( 'friends_hide_from_friends_page' ); | |
| 1054 | - if ( ! $hide_from_friends_page ) { | |
| 1055 | - $hide_from_friends_page = array(); | |
| 1056 | - } | |
| 1057 | - if ( ! isset( $_POST['show_on_friends_page'] ) || ! $_POST['show_on_friends_page'] ) { | |
| 1058 | - if ( ! in_array( $friend->user_login, $hide_from_friends_page ) ) { | |
| 1059 | - $hide_from_friends_page[] = $friend->user_login; | |
| 1060 | - update_user_option( get_current_user_id(), 'friends_hide_from_friends_page', $hide_from_friends_page ); | |
| 1061 | - } | |
| 1062 | - } else { | |
| 1063 | - if ( in_array( $friend->user_login, $hide_from_friends_page ) ) { | |
| 1064 | - $hide_from_friends_page = array_values( array_diff( $hide_from_friends_page, array( $friend->user_login ) ) ); | |
| 1065 | - update_user_option( get_current_user_id(), 'friends_hide_from_friends_page', $hide_from_friends_page ); | |
| 1066 | - } | |
| 1067 | - } | |
| 1068 | 1578 | } else { |
| 1069 | 1579 | return; |
| 1070 | 1580 | } |
| 1071 | 1581 | |
| @@ -1070,13 +1580,10 @@ | ||
| 1070 | 1580 | } |
| 1071 | 1581 | |
| 1072 | 1582 | do_action( 'friends_edit_friend_after_form_submit', $friend ); |
| 1073 | 1583 | |
| 1074 | - if ( isset( $_GET['_wp_http_referer'] ) ) { | |
| 1075 | - wp_safe_redirect( add_query_arg( $arg, $arg_value, wp_get_referer() ) ); | |
| 1076 | - } else { | |
| 1077 | - wp_safe_redirect( add_query_arg( $arg, $arg_value, remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ); | |
| 1078 | - } | |
| 1584 | + $redirect_url = self_admin_url( 'admin.php?page=edit-friend&user=' . $friend->user_login ); | |
| 1585 | + wp_safe_redirect( add_query_arg( $arg, rawurlencode( $arg_value ), $redirect_url ) ); | |
| 1079 | 1586 | exit; |
| 1080 | 1587 | } |
| 1081 | 1588 | |
| 1082 | 1589 | /** |
| @@ -1085,9 +1592,9 @@ | ||
| 1085 | 1592 | * @param User $friend The friend. |
| 1086 | 1593 | * @param string $active The active menu entry. |
| 1087 | 1594 | */ |
| 1088 | 1595 | public function header_edit_friend( User $friend, $active ) { |
| 1089 | - $append = '&user=' . $friend->user_login; | |
| 1596 | + $append = '&user=' . sanitize_user( $friend->user_login ); | |
| 1090 | 1597 | Friends::template_loader()->get_template_part( |
| 1091 | 1598 | 'admin/settings-header', |
| 1092 | 1599 | null, |
| 1093 | 1600 | array( |
| @@ -1093,12 +1600,13 @@ | ||
| 1093 | 1600 | array( |
| 1094 | 1601 | 'active' => $active . $append, |
| 1095 | 1602 | 'title' => $friend->user_login, |
| 1096 | 1603 | 'menu' => array( |
| 1604 | + __( 'Posts' ) => $friend->get_local_friends_page_url(), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 1097 | 1605 | __( 'Settings' ) => 'edit-friend' . $append, // phpcs:ignore WordPress.WP.I18n.MissingArgDomain |
| 1098 | 1606 | __( 'Feeds', 'friends' ) => 'edit-friend-feeds' . $append, |
| 1099 | 1607 | __( 'Notifications', 'friends' ) => 'edit-friend-notifications' . $append, |
| 1100 | - __( 'Rules', 'friends' ) => 'edit-friend-rules' . $append, | |
| 1608 | + __( 'Rules', 'friends' ) => 'edit-friend-rules' . $append . '&_wpnonce=' . wp_create_nonce( 'edit-friend-rules-' . $friend->user_login ), | |
| 1101 | 1609 | ), |
| 1102 | 1610 | ) |
| 1103 | 1611 | ); |
| 1104 | 1612 | } |
| @@ -1111,20 +1619,16 @@ | ||
| 1111 | 1619 | |
| 1112 | 1620 | $args = array_merge( |
| 1113 | 1621 | $friend->get_post_stats(), |
| 1114 | 1622 | array( |
| 1115 | - 'friend' => $friend, | |
| 1116 | - 'friends_settings_url' => add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), self_admin_url( 'admin.php?page=friends-settings' ) ), | |
| 1117 | - 'registered_parsers' => $this->friends->feed->get_registered_parsers(), | |
| 1118 | - 'hide_from_friends_page' => get_user_option( 'friends_hide_from_friends_page' ), | |
| 1623 | + 'friend' => $friend, | |
| 1624 | + 'friends_settings_url' => add_query_arg( '_wp_http_referer', remove_query_arg( '_wp_http_referer' ), self_admin_url( 'admin.php?page=friends-settings' ) ), | |
| 1625 | + 'registered_parsers' => $this->friends->feed->get_registered_parsers(), | |
| 1119 | 1626 | ) |
| 1120 | 1627 | ); |
| 1121 | - if ( ! $args['hide_from_friends_page'] ) { | |
| 1122 | - $args['hide_from_friends_page'] = array(); | |
| 1123 | - } | |
| 1124 | 1628 | |
| 1125 | 1629 | $this->header_edit_friend( $friend, 'edit-friend' ); |
| 1126 | - | |
| 1630 | + // phpcs:disable WordPress.Security.NonceVerification | |
| 1127 | 1631 | if ( isset( $_GET['updated'] ) ) { |
| 1128 | 1632 | ?> |
| 1129 | 1633 | <div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'User was updated.', 'friends' ); ?></p></div> |
| 1130 | 1634 | <?php |
| @@ -1133,10 +1637,18 @@ | ||
| 1133 | 1637 | <div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'You are now friends.', 'friends' ); ?></p></div> |
| 1134 | 1638 | <?php |
| 1135 | 1639 | } elseif ( isset( $_GET['error'] ) ) { |
| 1136 | 1640 | ?> |
| 1137 | - <div id="message" class="updated error is-dismissible"><p><?php esc_html_e( 'An error occurred.', 'friends' ); ?></p></div> | |
| 1641 | + <div id="message" class="updated error is-dismissible"><p> | |
| 1138 | 1642 | <?php |
| 1643 | + if ( 1 === intval( $_GET['error'] ) ) { | |
| 1644 | + esc_html_e( 'An error occurred.', 'friends' ); | |
| 1645 | + } else { | |
| 1646 | + echo esc_html( Rest::translate_error_message( sanitize_text_field( wp_unslash( $_GET['error'] ) ) ) ); | |
| 1647 | + } | |
| 1648 | + ?> | |
| 1649 | + </p></div> | |
| 1650 | + <?php | |
| 1139 | 1651 | } elseif ( isset( $_GET['sent-request'] ) ) { |
| 1140 | 1652 | ?> |
| 1141 | 1653 | <div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'Your request was sent.', 'friends' ); ?></p></div> |
| 1142 | 1654 | <?php |
| @@ -1144,31 +1656,363 @@ | ||
| 1144 | 1656 | ?> |
| 1145 | 1657 | <div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'Subscription activated.', 'friends' ); ?></p></div> |
| 1146 | 1658 | <?php |
| 1147 | 1659 | } |
| 1660 | + // phpcs:enable WordPress.Security.NonceVerification | |
| 1148 | 1661 | |
| 1149 | 1662 | Friends::template_loader()->get_template_part( 'admin/edit-friend', null, $args ); |
| 1150 | 1663 | } |
| 1151 | 1664 | |
| 1665 | + public function ajax_refresh_feeds() { | |
| 1666 | + check_ajax_referer( 'friends-refresh' ); | |
| 1667 | + | |
| 1668 | + if ( ! Friends::has_required_privileges() ) { | |
| 1669 | + wp_send_json_error( __( 'You do not have permission to do this.', 'friends' ) ); | |
| 1670 | + } | |
| 1671 | + | |
| 1672 | + add_filter( 'notify_about_new_friend_post', '__return_false', 999 ); | |
| 1673 | + | |
| 1674 | + if ( ! empty( $_POST['user'] ) ) { | |
| 1675 | + $friend_user = User::get_by_username( sanitize_user( wp_unslash( $_POST['user'] ) ) ); | |
| 1676 | + if ( ! $friend_user || is_wp_error( $friend_user ) || ! $friend_user->can_refresh_feeds() ) { | |
| 1677 | + wp_send_json_error( __( 'Invalid user ID.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 1678 | + } | |
| 1679 | + $friend_user->retrieve_posts_from_active_feeds(); | |
| 1680 | + } else { | |
| 1681 | + $this->friends->feed->retrieve_friend_posts(); | |
| 1682 | + } | |
| 1683 | + | |
| 1684 | + wp_send_json_success(); | |
| 1685 | + } | |
| 1686 | + | |
| 1687 | + private function normalize_frontend_subscription_url( $url ) { | |
| 1688 | + if ( ! is_string( $url ) ) { | |
| 1689 | + return ''; | |
| 1690 | + } | |
| 1691 | + | |
| 1692 | + $url = trim( $url ); | |
| 1693 | + if ( '' === $url ) { | |
| 1694 | + return ''; | |
| 1695 | + } | |
| 1696 | + | |
| 1697 | + $protocol = wp_parse_url( $url, PHP_URL_SCHEME ); | |
| 1698 | + if ( ! $protocol ) { | |
| 1699 | + return apply_filters( 'friends_rewrite_incoming_url', 'https://' . $url, $url ); | |
| 1700 | + } | |
| 1701 | + | |
| 1702 | + return apply_filters( 'friends_rewrite_incoming_url', $url, $url ); | |
| 1703 | + } | |
| 1704 | + | |
| 1705 | + public function ajax_preview_subscription() { | |
| 1706 | + if ( ! isset( $_POST['url'] ) || is_array( $_POST['url'] ) ) { | |
| 1707 | + wp_send_json_error( __( 'No URL provided.', 'friends' ) ); | |
| 1708 | + } | |
| 1709 | + | |
| 1710 | + check_ajax_referer( 'friends_add_subscription' ); | |
| 1711 | + | |
| 1712 | + if ( ! Friends::has_required_privileges() ) { | |
| 1713 | + wp_send_json_error( __( 'You do not have permission to do this.', 'friends' ) ); | |
| 1714 | + } | |
| 1715 | + | |
| 1716 | + $incoming_url = trim( wp_unslash( $_POST['url'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 1717 | + if ( ! class_exists( '\Activitypub\Activitypub' ) && preg_match( '/^@?[A-Za-z0-9_.-]+@(?:[A-Za-z0-9_-]+\.)+[A-Za-z]+$/i', $incoming_url ) ) { | |
| 1718 | + wp_send_json_error( __( 'The ActivityPub plugin is required to follow Mastodon handles. Please install and activate it first.', 'friends' ) ); | |
| 1719 | + } | |
| 1720 | + | |
| 1721 | + $url = $this->normalize_frontend_subscription_url( $incoming_url ); | |
| 1722 | + | |
| 1723 | + if ( '' === $url ) { | |
| 1724 | + wp_send_json_error( __( 'No URL provided.', 'friends' ) ); | |
| 1725 | + } | |
| 1726 | + | |
| 1727 | + if ( str_starts_with( $url, home_url() ) ) { | |
| 1728 | + wp_send_json_error( __( 'It seems like you sent a friend request to yourself.', 'friends' ) ); | |
| 1729 | + } | |
| 1730 | + | |
| 1731 | + if ( ! Friends::check_url( $url ) ) { | |
| 1732 | + wp_send_json_error( __( 'You entered an invalid URL.', 'friends' ) ); | |
| 1733 | + } | |
| 1734 | + | |
| 1735 | + $user_login = apply_filters( 'friends_suggest_user_login', User::get_user_login_for_url( $url ), $url ); | |
| 1736 | + $display_name = apply_filters( 'friends_suggest_display_name', User::get_display_name_for_url( $url ), $url ); | |
| 1737 | + | |
| 1738 | + $feeds = $this->friends->feed->discover_available_feeds( $url ); | |
| 1739 | + | |
| 1740 | + if ( is_wp_error( $feeds ) ) { | |
| 1741 | + wp_send_json_error( $feeds->get_error_message() ); | |
| 1742 | + } | |
| 1743 | + | |
| 1744 | + if ( empty( $feeds ) ) { | |
| 1745 | + wp_send_json_error( __( 'No suitable feed was found at the provided address.', 'friends' ) ); | |
| 1746 | + } | |
| 1747 | + | |
| 1748 | + $better_user_login = User::get_user_login_from_feeds( $feeds ); | |
| 1749 | + if ( $better_user_login ) { | |
| 1750 | + $user_login = trim( $better_user_login, '-' ); | |
| 1751 | + } | |
| 1752 | + | |
| 1753 | + $better_display_name = User::get_display_name_from_feeds( $feeds ); | |
| 1754 | + if ( $better_display_name ) { | |
| 1755 | + $display_name = $better_display_name; | |
| 1756 | + if ( ! $better_user_login ) { | |
| 1757 | + $user_login = trim( User::sanitize_username( $better_display_name ), '-' ); | |
| 1758 | + } | |
| 1759 | + } | |
| 1760 | + | |
| 1761 | + $friend_user = User::get_user( $user_login ); | |
| 1762 | + if ( ! $friend_user || is_wp_error( $friend_user ) ) { | |
| 1763 | + $friend_user = Subscription::get_by_username( $user_login ); | |
| 1764 | + } | |
| 1765 | + | |
| 1766 | + if ( $friend_user && ! is_wp_error( $friend_user ) ) { | |
| 1767 | + // translators: %s is the name of a friend / site. | |
| 1768 | + wp_send_json_error( sprintf( __( 'You are already subscribed to this site: %s', 'friends' ), $friend_user->display_name ) ); | |
| 1769 | + } | |
| 1770 | + | |
| 1771 | + $avatar = null; | |
| 1772 | + $description = null; | |
| 1773 | + foreach ( $feeds as $feed_details ) { | |
| 1774 | + if ( ! $avatar && ! empty( $feed_details['avatar'] ) ) { | |
| 1775 | + $avatar = $feed_details['avatar']; | |
| 1776 | + } | |
| 1777 | + if ( ! $description && ! empty( $feed_details['description'] ) ) { | |
| 1778 | + $description = $feed_details['description']; | |
| 1779 | + } | |
| 1780 | + } | |
| 1781 | + | |
| 1782 | + wp_send_json_success( | |
| 1783 | + array( | |
| 1784 | + 'feeds' => $feeds, | |
| 1785 | + 'display_name' => $display_name ? $display_name : '', | |
| 1786 | + 'user_login' => $user_login ? $user_login : '', | |
| 1787 | + 'avatar' => $avatar, | |
| 1788 | + 'description' => $description, | |
| 1789 | + 'url' => $url, | |
| 1790 | + ) | |
| 1791 | + ); | |
| 1792 | + } | |
| 1793 | + | |
| 1794 | + public function ajax_preview_subscription_feed() { | |
| 1795 | + check_ajax_referer( 'friends_add_subscription' ); | |
| 1796 | + | |
| 1797 | + if ( ! Friends::has_required_privileges() ) { | |
| 1798 | + wp_send_json_error( __( 'You do not have permission to do this.', 'friends' ) ); | |
| 1799 | + } | |
| 1800 | + | |
| 1801 | + $url = isset( $_POST['url'] ) && ! is_array( $_POST['url'] ) ? $this->normalize_frontend_subscription_url( wp_unslash( $_POST['url'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 1802 | + if ( '' === $url || ! Friends::check_url( $url ) ) { | |
| 1803 | + wp_send_json_error( __( 'You entered an invalid URL.', 'friends' ) ); | |
| 1804 | + } | |
| 1805 | + | |
| 1806 | + $parser = isset( $_POST['parser'] ) && ! is_array( $_POST['parser'] ) ? sanitize_key( wp_unslash( $_POST['parser'] ) ) : ''; | |
| 1807 | + if ( ! $parser ) { | |
| 1808 | + wp_send_json_error( __( 'An invalid parser was supplied.', 'friends' ) ); | |
| 1809 | + } | |
| 1810 | + | |
| 1811 | + $items = $this->friends->feed->preview( $parser, $url ); | |
| 1812 | + if ( is_wp_error( $items ) ) { | |
| 1813 | + wp_send_json_error( $items->get_error_message() ); | |
| 1814 | + } | |
| 1815 | + | |
| 1816 | + $preview_items = array(); | |
| 1817 | + foreach ( array_slice( $items, 0, 5 ) as $item ) { | |
| 1818 | + $title = $item->title; | |
| 1819 | + if ( 'status' === $item->post_format || ! $title ) { | |
| 1820 | + $title = wp_strip_all_tags( $item->content ); | |
| 1821 | + } | |
| 1822 | + | |
| 1823 | + $preview_items[] = array( | |
| 1824 | + 'title' => wp_trim_words( wp_strip_all_tags( $title ), 16 ), | |
| 1825 | + 'excerpt' => wp_trim_words( wp_strip_all_tags( $item->content ), 40 ), | |
| 1826 | + 'permalink' => $item->permalink, | |
| 1827 | + 'date' => $item->date, | |
| 1828 | + 'author' => $item->author, | |
| 1829 | + 'post_format' => $item->post_format, | |
| 1830 | + ); | |
| 1831 | + } | |
| 1832 | + | |
| 1833 | + wp_send_json_success( | |
| 1834 | + array( | |
| 1835 | + 'items' => $preview_items, | |
| 1836 | + ) | |
| 1837 | + ); | |
| 1838 | + } | |
| 1839 | + | |
| 1840 | + public function ajax_subscribe_frontend() { | |
| 1841 | + check_ajax_referer( 'friends_add_subscription' ); | |
| 1842 | + | |
| 1843 | + if ( ! Friends::has_required_privileges() ) { | |
| 1844 | + wp_send_json_error( __( 'You do not have permission to do this.', 'friends' ) ); | |
| 1845 | + } | |
| 1846 | + | |
| 1847 | + $url = isset( $_POST['url'] ) && ! is_array( $_POST['url'] ) ? $this->normalize_frontend_subscription_url( wp_unslash( $_POST['url'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 1848 | + $display_name = isset( $_POST['display_name'] ) && ! is_array( $_POST['display_name'] ) ? sanitize_text_field( wp_unslash( $_POST['display_name'] ) ) : ''; | |
| 1849 | + $user_login = isset( $_POST['user_login'] ) && ! is_array( $_POST['user_login'] ) ? User::sanitize_username( wp_unslash( $_POST['user_login'] ) ) : User::get_user_login_for_url( $url ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 1850 | + $feeds = isset( $_POST['feeds'] ) && is_array( $_POST['feeds'] ) ? wp_unslash( $_POST['feeds'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 1851 | + | |
| 1852 | + if ( empty( $url ) || empty( $feeds ) ) { | |
| 1853 | + wp_send_json_error( __( 'Missing required data.', 'friends' ) ); | |
| 1854 | + } | |
| 1855 | + | |
| 1856 | + if ( ! Friends::check_url( $url ) ) { | |
| 1857 | + wp_send_json_error( __( 'You entered an invalid URL.', 'friends' ) ); | |
| 1858 | + } | |
| 1859 | + | |
| 1860 | + $user_login = trim( $user_login, '-' ); | |
| 1861 | + if ( ! $user_login ) { | |
| 1862 | + wp_send_json_error( __( 'Please enter a valid username.', 'friends' ) ); | |
| 1863 | + } | |
| 1864 | + | |
| 1865 | + if ( ! $display_name ) { | |
| 1866 | + $display_name = User::get_display_name_for_url( $url ); | |
| 1867 | + } | |
| 1868 | + | |
| 1869 | + if ( ! is_multisite() && username_exists( $user_login ) ) { | |
| 1870 | + wp_send_json_error( __( 'This username is already registered. Please choose another one.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 1871 | + } | |
| 1872 | + | |
| 1873 | + $avatar = null; | |
| 1874 | + $description = null; | |
| 1875 | + $feed_options = array(); | |
| 1876 | + $subscribe = array(); | |
| 1877 | + $post_formats = array_merge( array( 'autodetect' => true ), array_fill_keys( array_keys( get_post_format_strings() ), true ) ); | |
| 1878 | + | |
| 1879 | + foreach ( $feeds as $feed ) { | |
| 1880 | + if ( ! is_array( $feed ) ) { | |
| 1881 | + continue; | |
| 1882 | + } | |
| 1883 | + | |
| 1884 | + $feed_url = ''; | |
| 1885 | + if ( ! empty( $feed['url'] ) && is_scalar( $feed['url'] ) ) { | |
| 1886 | + $feed_url = esc_url_raw( trim( $feed['url'] ) ); | |
| 1887 | + } | |
| 1888 | + | |
| 1889 | + if ( ! $feed_url || ! Friends::check_url( $feed_url ) ) { | |
| 1890 | + continue; | |
| 1891 | + } | |
| 1892 | + | |
| 1893 | + $parser = isset( $feed['parser'] ) && is_scalar( $feed['parser'] ) ? sanitize_key( $feed['parser'] ) : 'simplepie'; | |
| 1894 | + if ( ! $parser || 'unsupported' === $parser ) { | |
| 1895 | + continue; | |
| 1896 | + } | |
| 1897 | + | |
| 1898 | + $post_format = isset( $feed['post-format'] ) && is_scalar( $feed['post-format'] ) ? sanitize_key( $feed['post-format'] ) : 'standard'; | |
| 1899 | + if ( ! isset( $post_formats[ $post_format ] ) ) { | |
| 1900 | + $post_format = 'standard'; | |
| 1901 | + } | |
| 1902 | + | |
| 1903 | + $mime_type = isset( $feed['mime-type'] ) && is_scalar( $feed['mime-type'] ) ? sanitize_text_field( $feed['mime-type'] ) : ''; | |
| 1904 | + if ( ! $mime_type && ! empty( $feed['type'] ) && is_scalar( $feed['type'] ) ) { | |
| 1905 | + $mime_type = sanitize_text_field( $feed['type'] ); | |
| 1906 | + } | |
| 1907 | + | |
| 1908 | + $feed_options[ $feed_url ] = array( | |
| 1909 | + 'url' => $feed_url, | |
| 1910 | + 'parser' => $parser, | |
| 1911 | + 'post-format' => $post_format, | |
| 1912 | + 'title' => isset( $feed['title'] ) && is_scalar( $feed['title'] ) ? sanitize_text_field( $feed['title'] ) : $feed_url, | |
| 1913 | + ); | |
| 1914 | + | |
| 1915 | + if ( $mime_type ) { | |
| 1916 | + $feed_options[ $feed_url ]['mime-type'] = $mime_type; | |
| 1917 | + } | |
| 1918 | + | |
| 1919 | + $is_selected = isset( $feed['selected'] ) && in_array( $feed['selected'], array( true, 'true', '1', 1, 'on' ), true ); | |
| 1920 | + if ( $is_selected ) { | |
| 1921 | + $subscribe[] = $feed_url; | |
| 1922 | + } | |
| 1923 | + | |
| 1924 | + if ( ! $avatar && ! empty( $feed['avatar'] ) && is_scalar( $feed['avatar'] ) ) { | |
| 1925 | + $avatar = esc_url_raw( $feed['avatar'] ); | |
| 1926 | + } | |
| 1927 | + if ( ! $description && ! empty( $feed['description'] ) && is_scalar( $feed['description'] ) ) { | |
| 1928 | + $description = wp_encode_emoji( sanitize_textarea_field( $feed['description'] ) ); | |
| 1929 | + } | |
| 1930 | + } | |
| 1931 | + | |
| 1932 | + if ( empty( $feed_options ) ) { | |
| 1933 | + wp_send_json_error( __( 'No suitable feed was found at the provided address.', 'friends' ) ); | |
| 1934 | + } | |
| 1935 | + | |
| 1936 | + if ( empty( $subscribe ) ) { | |
| 1937 | + wp_send_json_error( __( 'Please select at least one feed.', 'friends' ) ); | |
| 1938 | + } | |
| 1939 | + | |
| 1940 | + $friend_user = User::get_user( $user_login ); | |
| 1941 | + if ( ! $friend_user || is_wp_error( $friend_user ) ) { | |
| 1942 | + $friend_user = Subscription::get_by_username( $user_login ); | |
| 1943 | + } | |
| 1944 | + | |
| 1945 | + if ( $friend_user && ! is_wp_error( $friend_user ) ) { | |
| 1946 | + // translators: %s is the name of a friend / site. | |
| 1947 | + wp_send_json_error( sprintf( __( 'You are already subscribed to this site: %s', 'friends' ), $friend_user->display_name ) ); | |
| 1948 | + } | |
| 1949 | + | |
| 1950 | + $friend_user = User::create( $user_login, 'subscription', $url, $display_name, $avatar, $description ); | |
| 1951 | + | |
| 1952 | + if ( is_wp_error( $friend_user ) ) { | |
| 1953 | + wp_send_json_error( $friend_user->get_error_message() ); | |
| 1954 | + } | |
| 1955 | + | |
| 1956 | + $saved_feeds = $friend_user->save_feeds( $feed_options ); | |
| 1957 | + if ( is_wp_error( $saved_feeds ) ) { | |
| 1958 | + wp_send_json_error( $saved_feeds->get_error_message() ); | |
| 1959 | + } | |
| 1960 | + | |
| 1961 | + foreach ( $subscribe as $feed_url ) { | |
| 1962 | + if ( ! isset( $feed_options[ $feed_url ] ) ) { | |
| 1963 | + continue; | |
| 1964 | + } | |
| 1965 | + $new_feed = $friend_user->subscribe( $feed_url, $feed_options[ $feed_url ] ); | |
| 1966 | + if ( ! is_wp_error( $new_feed ) ) { | |
| 1967 | + do_action( 'friends_user_feed_activated', $new_feed ); | |
| 1968 | + } | |
| 1969 | + } | |
| 1970 | + | |
| 1971 | + add_filter( 'notify_about_new_friend_post', '__return_false', 999 ); | |
| 1972 | + wp_schedule_single_event( time(), 'friends_retrieve_user_feeds', array( $friend_user->ID ) ); | |
| 1973 | + | |
| 1974 | + wp_send_json_success( | |
| 1975 | + array( | |
| 1976 | + 'message' => sprintf( | |
| 1977 | + // translators: %s is the name of a friend. | |
| 1978 | + __( 'You are now following %s.', 'friends' ), | |
| 1979 | + $display_name | |
| 1980 | + ), | |
| 1981 | + 'url' => $friend_user->get_local_friends_page_url(), | |
| 1982 | + ) | |
| 1983 | + ); | |
| 1984 | + } | |
| 1985 | + | |
| 1152 | 1986 | public function ajax_set_avatar() { |
| 1153 | - $user_id = isset( $_POST['user'] ) ? $_POST['user'] : 0; | |
| 1987 | + if ( ! isset( $_POST['user'] ) ) { | |
| 1988 | + wp_send_json_error( __( 'No user specified.', 'friends' ) ); | |
| 1989 | + } | |
| 1154 | 1990 | |
| 1155 | - check_ajax_referer( "set-avatar-$user_id" ); | |
| 1991 | + check_ajax_referer( 'set-avatar-' . sanitize_user( wp_unslash( $_POST['user'] ) ) ); | |
| 1156 | 1992 | |
| 1157 | 1993 | if ( ! current_user_can( Friends::REQUIRED_ROLE ) ) { |
| 1158 | 1994 | wp_send_json_error(); |
| 1159 | 1995 | exit; |
| 1160 | 1996 | } |
| 1161 | - | |
| 1162 | - if ( empty( $_POST['avatar'] ) || ! Friends::check_url( $_POST['avatar'] ) ) { | |
| 1997 | + if ( empty( $_POST['avatar'] ) ) { | |
| 1163 | 1998 | wp_send_json_error(); |
| 1164 | 1999 | exit; |
| 1165 | 2000 | } |
| 2001 | + $avatar = check_url( wp_unslash( $_POST['avatar'] ) ); | |
| 2002 | + if ( empty( $avatar ) ) { | |
| 2003 | + wp_send_json_error(); | |
| 2004 | + exit; | |
| 2005 | + } | |
| 1166 | 2006 | |
| 1167 | - $friend = User::get_user_by_id( $user_id ); | |
| 2007 | + $friend = User::get_by_username( sanitize_user( wp_unslash( $_POST['user'] ) ) ); | |
| 2008 | + if ( ! $friend || is_wp_error( $friend ) ) { | |
| 2009 | + wp_send_json_error( __( 'Invalid user.', 'friends' ) ); | |
| 2010 | + exit; | |
| 2011 | + } | |
| 1168 | 2012 | |
| 1169 | 2013 | // Use WordPress functions to check the image dimensions. |
| 1170 | - $size = \wp_getimagesize( $_POST['avatar'] ); | |
| 2014 | + $size = \wp_getimagesize( $avatar ); | |
| 1171 | 2015 | if ( ! $size ) { |
| 1172 | 2016 | wp_send_json_error( __( 'Image is in an unknown format.', 'friends' ) ); |
| 1173 | 2017 | exit; |
| 1174 | 2018 | } |
| @@ -1177,9 +2021,9 @@ | ||
| 1177 | 2021 | wp_send_json_error( __( 'Image must be square and not larger than 512x512.', 'friends' ) ); |
| 1178 | 2022 | exit; |
| 1179 | 2023 | } |
| 1180 | 2024 | |
| 1181 | - $url = $friend->update_user_icon_url( $_POST['avatar'] ); | |
| 2025 | + $url = $friend->update_user_icon_url( $avatar ); | |
| 1182 | 2026 | |
| 1183 | 2027 | if ( ! $url || is_wp_error( $url ) ) { |
| 1184 | 2028 | wp_send_json_error( $url ); |
| 1185 | 2029 | exit; |
| @@ -1199,11 +2043,11 @@ | ||
| 1199 | 2043 | $friend = $this->check_admin_edit_friend(); |
| 1200 | 2044 | $arg = 'updated'; |
| 1201 | 2045 | $arg_value = 1; |
| 1202 | 2046 | |
| 1203 | - if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'edit-friend-notifications-' . $friend->user_login ) ) { | |
| 2047 | + if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'edit-friend-notifications-' . $friend->user_login ) ) { | |
| 1204 | 2048 | if ( ! get_user_option( 'friends_no_new_post_notification' ) ) { |
| 1205 | - if ( isset( $_POST['friends_new_post_notification'] ) && $_POST['friends_new_post_notification'] ) { | |
| 2049 | + if ( isset( $_POST['friends_new_post_notification'] ) && boolval( $_POST['friends_new_post_notification'] ) ) { | |
| 1206 | 2050 | delete_user_option( get_current_user_id(), 'friends_no_new_post_notification_' . $friend->user_login ); |
| 1207 | 2051 | } else { |
| 1208 | 2052 | update_user_option( get_current_user_id(), 'friends_no_new_post_notification_' . $friend->user_login, 1 ); |
| 1209 | 2053 | } |
| @@ -1209,9 +2053,9 @@ | ||
| 1209 | 2053 | } |
| 1210 | 2054 | } |
| 1211 | 2055 | |
| 1212 | 2056 | if ( ! get_user_option( 'friends_no_keyword_notification' ) ) { |
| 1213 | - if ( isset( $_POST['friends_keyword_notification'] ) && $_POST['friends_keyword_notification'] ) { | |
| 2057 | + if ( isset( $_POST['friends_keyword_notification'] ) && boolval( $_POST['friends_keyword_notification'] ) ) { | |
| 1214 | 2058 | delete_user_option( get_current_user_id(), 'friends_no_keyword_notification_' . $friend->user_login ); |
| 1215 | 2059 | } else { |
| 1216 | 2060 | update_user_option( get_current_user_id(), 'friends_no_keyword_notification_' . $friend->user_login, 1 ); |
| 1217 | 2061 | } |
| @@ -1224,9 +2068,9 @@ | ||
| 1224 | 2068 | |
| 1225 | 2069 | if ( isset( $_GET['_wp_http_referer'] ) ) { |
| 1226 | 2070 | wp_safe_redirect( wp_get_referer() ); |
| 1227 | 2071 | } else { |
| 1228 | - wp_safe_redirect( add_query_arg( $arg, $arg_value, remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ); | |
| 2072 | + wp_safe_redirect( add_query_arg( $arg, $arg_value, remove_query_arg( array( '_wp_http_referer', '_wpnonce' ) ) ) ); | |
| 1229 | 2073 | } |
| 1230 | 2074 | exit; |
| 1231 | 2075 | } |
| 1232 | 2076 | |
| @@ -1238,8 +2082,9 @@ | ||
| 1238 | 2082 | $post_stats = $friend->get_post_stats(); |
| 1239 | 2083 | |
| 1240 | 2084 | $this->header_edit_friend( $friend, 'edit-friend-notifications' ); |
| 1241 | 2085 | |
| 2086 | + // phpcs:disable WordPress.Security.NonceVerification | |
| 1242 | 2087 | if ( isset( $_GET['updated'] ) ) { |
| 1243 | 2088 | ?> |
| 1244 | 2089 | <div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'Notification Settings were updated.', 'friends' ); ?></p></div> |
| 1245 | 2090 | <?php |
| @@ -1247,8 +2092,9 @@ | ||
| 1247 | 2092 | ?> |
| 1248 | 2093 | <div id="message" class="updated error is-dismissible"><p><?php esc_html_e( 'An error occurred.', 'friends' ); ?></p></div> |
| 1249 | 2094 | <?php |
| 1250 | 2095 | } |
| 2096 | + // phpcs:enable WordPress.Security.NonceVerification | |
| 1251 | 2097 | |
| 1252 | 2098 | Friends::template_loader()->get_template_part( |
| 1253 | 2099 | 'admin/edit-notifications', |
| 1254 | 2100 | null, |
| @@ -1265,30 +2111,28 @@ | ||
| 1265 | 2111 | $friend = $this->check_admin_edit_friend(); |
| 1266 | 2112 | $arg = 'updated'; |
| 1267 | 2113 | $arg_value = 1; |
| 1268 | 2114 | |
| 1269 | - if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'edit-friend-feeds-' . $friend->user_login ) ) { | |
| 2115 | + if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'edit-friend-feeds-' . $friend->user_login ) ) { | |
| 1270 | 2116 | $hide_from_friends_page = get_user_option( 'friends_hide_from_friends_page' ); |
| 1271 | 2117 | if ( ! $hide_from_friends_page ) { |
| 1272 | 2118 | $hide_from_friends_page = array(); |
| 1273 | 2119 | } |
| 1274 | - if ( ! isset( $_POST['show_on_friends_page'] ) || ! $_POST['show_on_friends_page'] ) { | |
| 2120 | + if ( ! isset( $_POST['show_on_friends_page'] ) || ! boolval( $_POST['show_on_friends_page'] ) ) { | |
| 1275 | 2121 | if ( ! in_array( $friend->user_login, $hide_from_friends_page ) ) { |
| 1276 | 2122 | $hide_from_friends_page[] = $friend->user_login; |
| 1277 | 2123 | update_user_option( get_current_user_id(), 'friends_hide_from_friends_page', $hide_from_friends_page ); |
| 1278 | 2124 | } |
| 1279 | - } else { | |
| 1280 | - if ( in_array( $friend->user_login, $hide_from_friends_page ) ) { | |
| 2125 | + } elseif ( in_array( $friend->user_login, $hide_from_friends_page ) ) { | |
| 1281 | 2126 | $hide_from_friends_page = array_values( array_diff( $hide_from_friends_page, array( $friend->user_login ) ) ); |
| 1282 | 2127 | update_user_option( get_current_user_id(), 'friends_hide_from_friends_page', $hide_from_friends_page ); |
| 1283 | - } | |
| 1284 | 2128 | } |
| 1285 | 2129 | |
| 1286 | - if ( $friend->set_retention_number_enabled( isset( $_POST['friends_enable_retention_number'] ) && $_POST['friends_enable_retention_number'] ) ) { | |
| 1287 | - $friend->set_retention_number( $_POST['friends_retention_number'] ); | |
| 2130 | + if ( $friend->set_retention_number_enabled( boolval( filter_input( INPUT_POST, 'friends_enable_retention_number', FILTER_SANITIZE_NUMBER_INT ) ) ) && isset( $_POST['friends_retention_number'] ) ) { | |
| 2131 | + $friend->set_retention_number( filter_input( INPUT_POST, 'friends_retention_number', FILTER_SANITIZE_NUMBER_INT ) ); | |
| 1288 | 2132 | } |
| 1289 | - if ( $friend->set_retention_days_enabled( isset( $_POST['friends_enable_retention_days'] ) && $_POST['friends_enable_retention_days'] ) ) { | |
| 1290 | - $friend->set_retention_days( $_POST['friends_retention_days'] ); | |
| 2133 | + if ( $friend->set_retention_days_enabled( boolval( filter_input( INPUT_POST, 'friends_enable_retention_days', FILTER_SANITIZE_NUMBER_INT ) ) ) && isset( $_POST['friends_retention_days'] ) ) { | |
| 2134 | + $friend->set_retention_days( filter_input( INPUT_POST, 'friends_retention_days', FILTER_SANITIZE_NUMBER_INT ) ); | |
| 1291 | 2135 | } |
| 1292 | 2136 | |
| 1293 | 2137 | $hide_from_friends_page = get_user_option( 'friends_hide_from_friends_page' ); |
| 1294 | 2138 | if ( ! $hide_from_friends_page ) { |
| @@ -1294,28 +2138,53 @@ | ||
| 1294 | 2138 | if ( ! $hide_from_friends_page ) { |
| 1295 | 2139 | $hide_from_friends_page = array(); |
| 1296 | 2140 | } |
| 1297 | 2141 | |
| 2142 | + $show_on_dashboard = filter_input( INPUT_POST, 'show_on_dashboard', FILTER_VALIDATE_BOOLEAN ); | |
| 2143 | + $already_on_dashboard = false; | |
| 2144 | + $widgets = get_user_option( 'friends_dashboard_widgets', get_current_user_id() ); | |
| 2145 | + if ( ! $widgets ) { | |
| 2146 | + $widgets = array(); | |
| 2147 | + } | |
| 2148 | + foreach ( $widgets as $k => $widget ) { | |
| 2149 | + if ( ! empty( $widget['friend'] ) && $widget['friend'] === $friend->user_login ) { | |
| 2150 | + $already_on_dashboard = true; | |
| 2151 | + if ( ! $show_on_dashboard ) { | |
| 2152 | + unset( $widgets[ $k ] ); | |
| 2153 | + update_user_option( get_current_user_id(), 'friends_dashboard_widgets', $widgets ); | |
| 2154 | + } | |
| 2155 | + break; | |
| 2156 | + } | |
| 2157 | + } | |
| 2158 | + if ( $show_on_dashboard && ! $already_on_dashboard ) { | |
| 2159 | + $widgets[] = array( 'friend' => $friend->user_login ); | |
| 2160 | + update_user_option( get_current_user_id(), 'friends_dashboard_widgets', $widgets ); | |
| 2161 | + } | |
| 2162 | + | |
| 1298 | 2163 | if ( isset( $_POST['feeds'] ) ) { |
| 2164 | + // Sanitized below. | |
| 2165 | + $feeds = wp_unslash( $_POST['feeds'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 1299 | 2166 | $existing_feeds = $friend->get_feeds(); |
| 1300 | - if ( '' === trim( $_POST['feeds']['new']['url'] ) ) { | |
| 1301 | - unset( $_POST['feeds']['new'] ); | |
| 1302 | - } else { | |
| 1303 | - foreach ( $existing_feeds as $term_id => $user_feed ) { | |
| 1304 | - if ( $user_feed->get_url() === trim( $_POST['feeds']['new']['url'] ) ) { | |
| 1305 | - if ( isset( $_POST['feeds'][ $term_id ] ) ) { | |
| 1306 | - // Let a newly entered feed overrule an existing one. | |
| 1307 | - $_POST['feeds'][ $term_id ] = array_merge( $_POST['feeds'][ $term_id ], $_POST['feeds']['new'] ); | |
| 1308 | - $_POST['feeds'][ $term_id ]['active'] = 1; | |
| 2167 | + if ( isset( $feeds['new'] ) ) { | |
| 2168 | + if ( ! isset( $feeds['new']['url'] ) || '' === trim( $feeds['new']['url'] ) ) { | |
| 2169 | + unset( $feeds['new'] ); | |
| 2170 | + } else { | |
| 2171 | + foreach ( $existing_feeds as $term_id => $user_feed ) { | |
| 2172 | + if ( $user_feed->get_url() === trim( $feeds['new']['url'] ) ) { | |
| 2173 | + if ( isset( $feeds[ $term_id ] ) ) { | |
| 2174 | + // Let a newly entered feed overrule an existing one. | |
| 2175 | + $feeds[ $term_id ] = array_merge( $feeds[ $term_id ], $feeds['new'] ); | |
| 2176 | + $feeds[ $term_id ]['active'] = 1; | |
| 2177 | + } | |
| 2178 | + unset( $feeds['new'] ); | |
| 2179 | + break; | |
| 1309 | 2180 | } |
| 1310 | - unset( $_POST['feeds']['new'] ); | |
| 1311 | - break; | |
| 1312 | 2181 | } |
| 1313 | 2182 | } |
| 1314 | 2183 | } |
| 1315 | - foreach ( $_POST['feeds'] as $term_id => $feed ) { | |
| 2184 | + foreach ( $feeds as $term_id => $feed ) { | |
| 1316 | 2185 | if ( 'new' === $term_id ) { |
| 1317 | - if ( '' === trim( $feed['url'] ) ) { | |
| 2186 | + if ( ! isset( $feed['url'] ) || '' === trim( $feed['url'] ) ) { | |
| 1318 | 2187 | continue; |
| 1319 | 2188 | } |
| 1320 | 2189 | |
| 1321 | 2190 | $feed['active'] = true; |
| @@ -1416,9 +2285,9 @@ | ||
| 1416 | 2285 | |
| 1417 | 2286 | if ( isset( $_GET['_wp_http_referer'] ) ) { |
| 1418 | 2287 | wp_safe_redirect( wp_get_referer() ); |
| 1419 | 2288 | } else { |
| 1420 | - wp_safe_redirect( add_query_arg( $arg, $arg_value, remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ); | |
| 2289 | + wp_safe_redirect( add_query_arg( $arg, $arg_value, remove_query_arg( array( '_wp_http_referer', '_wpnonce' ) ) ) ); | |
| 1421 | 2290 | } |
| 1422 | 2291 | exit; |
| 1423 | 2292 | } |
| 1424 | 2293 | |
| @@ -1427,8 +2296,21 @@ | ||
| 1427 | 2296 | */ |
| 1428 | 2297 | public function render_admin_edit_friend_feeds() { |
| 1429 | 2298 | $friend = $this->check_admin_edit_friend(); |
| 1430 | 2299 | |
| 2300 | + $already_on_dashboard = false; | |
| 2301 | + $widgets = get_user_option( 'friends_dashboard_widgets', get_current_user_id() ); | |
| 2302 | + | |
| 2303 | + if ( ! $widgets ) { | |
| 2304 | + $widgets = array(); | |
| 2305 | + } | |
| 2306 | + foreach ( $widgets as $widget ) { | |
| 2307 | + if ( ! empty( $widget['friend'] ) && $widget['friend'] === $friend->user_login ) { | |
| 2308 | + $already_on_dashboard = true; | |
| 2309 | + break; | |
| 2310 | + } | |
| 2311 | + } | |
| 2312 | + | |
| 1431 | 2313 | $args = array_merge( |
| 1432 | 2314 | $friend->get_post_stats(), |
| 1433 | 2315 | array( |
| 1434 | 2316 | 'friend' => $friend, |
| @@ -1434,14 +2316,15 @@ | ||
| 1434 | 2316 | 'friend' => $friend, |
| 1435 | 2317 | 'rules' => $friend->get_feed_rules(), |
| 1436 | 2318 | 'hide_from_friends_page' => get_user_option( 'friends_hide_from_friends_page' ), |
| 1437 | 2319 | 'post_formats' => array_merge( array( 'autodetect' => __( 'Autodetect Post Format', 'friends' ) ), get_post_format_strings() ), |
| 1438 | - 'friends_settings_url' => add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), self_admin_url( 'admin.php?page=friends-settings' ) ), | |
| 2320 | + 'friends_settings_url' => add_query_arg( '_wp_http_referer', remove_query_arg( '_wp_http_referer' ), self_admin_url( 'admin.php?page=friends-settings' ) ), | |
| 1439 | 2321 | 'registered_parsers' => $this->friends->feed->get_registered_parsers(), |
| 1440 | 2322 | 'global_retention_days' => Friends::get_retention_days(), |
| 1441 | 2323 | 'global_retention_number' => Friends::get_retention_number(), |
| 1442 | 2324 | 'global_retention_days_enabled' => get_option( 'friends_enable_retention_days' ), |
| 1443 | 2325 | 'global_retention_number_enabled' => get_option( 'friends_enable_retention_number' ), |
| 2326 | + 'show_on_dashboard' => $already_on_dashboard, | |
| 1444 | 2327 | ) |
| 1445 | 2328 | ); |
| 1446 | 2329 | if ( ! $args['hide_from_friends_page'] ) { |
| 1447 | 2330 | $args['hide_from_friends_page'] = array(); |
| @@ -1447,8 +2330,9 @@ | ||
| 1447 | 2330 | $args['hide_from_friends_page'] = array(); |
| 1448 | 2331 | } |
| 1449 | 2332 | $this->header_edit_friend( $friend, 'edit-friend-feeds' ); |
| 1450 | 2333 | |
| 2334 | + // phpcs:disable WordPress.Security.NonceVerification | |
| 1451 | 2335 | if ( isset( $_GET['updated'] ) ) { |
| 1452 | 2336 | ?> |
| 1453 | 2337 | <div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'Feeds were updated.', 'friends' ); ?></p></div> |
| 1454 | 2338 | <?php |
| @@ -1456,8 +2340,9 @@ | ||
| 1456 | 2340 | ?> |
| 1457 | 2341 | <div id="message" class="updated error is-dismissible"><p><?php esc_html_e( 'An error occurred.', 'friends' ); ?></p></div> |
| 1458 | 2342 | <?php |
| 1459 | 2343 | } |
| 2344 | + // phpcs:enable WordPress.Security.NonceVerification | |
| 1460 | 2345 | |
| 1461 | 2346 | Friends::template_loader()->get_template_part( 'admin/edit-feeds', null, $args ); |
| 1462 | 2347 | } |
| 1463 | 2348 | |
| @@ -1468,9 +2353,9 @@ | ||
| 1468 | 2353 | $friend = $this->check_admin_edit_friend(); |
| 1469 | 2354 | $arg = 'deleted'; |
| 1470 | 2355 | $arg_value = $friend->user_login; |
| 1471 | 2356 | |
| 1472 | - if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'unfriend-' . $friend->user_login ) ) { | |
| 2357 | + if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'unfriend-' . $friend->user_login ) ) { | |
| 1473 | 2358 | $friend->delete(); |
| 1474 | 2359 | } else { |
| 1475 | 2360 | return; |
| 1476 | 2361 | } |
| @@ -1477,9 +2362,9 @@ | ||
| 1477 | 2362 | |
| 1478 | 2363 | if ( isset( $_GET['_wp_http_referer'] ) ) { |
| 1479 | 2364 | wp_safe_redirect( wp_get_referer() ); |
| 1480 | 2365 | } else { |
| 1481 | - wp_safe_redirect( add_query_arg( $arg, $arg_value, self_admin_url( 'admin.php?page=friends-list' ) ) ); | |
| 2366 | + wp_safe_redirect( add_query_arg( $arg, $arg_value, home_url( '/friends/following/' ) ) ); | |
| 1482 | 2367 | } |
| 1483 | 2368 | exit; |
| 1484 | 2369 | } |
| 1485 | 2370 | |
| @@ -1513,14 +2398,14 @@ | ||
| 1513 | 2398 | <div id="message" class="updated error is-dismissible"><p><?php echo esc_html( $errors->get_error_message() ); ?></p> |
| 1514 | 2399 | <?php |
| 1515 | 2400 | $error_data = $errors->get_error_data(); |
| 1516 | 2401 | if ( isset( $error_data->error ) ) { |
| 1517 | - $error = unserialize( $error_data->error ); | |
| 2402 | + $error = unserialize( $error_data->error ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize | |
| 1518 | 2403 | if ( is_wp_error( $error ) ) { |
| 1519 | 2404 | ?> |
| 1520 | 2405 | <pre> |
| 1521 | 2406 | <?php |
| 1522 | - print_r( $error ); | |
| 2407 | + print_r( $error ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r | |
| 1523 | 2408 | ?> |
| 1524 | 2409 | </pre> |
| 1525 | 2410 | <?php |
| 1526 | 2411 | } elseif ( is_array( $error ) && isset( $error['body'] ) ) { |
| @@ -1537,805 +2422,450 @@ | ||
| 1537 | 2422 | </div> |
| 1538 | 2423 | <?php |
| 1539 | 2424 | } |
| 1540 | 2425 | |
| 1541 | - public function create_and_follow( $user_id, $url, $type, $vars = array() ) { | |
| 1542 | - $vars['friend_url'] = $url; | |
| 1543 | - | |
| 1544 | - $vars['user_login'] = apply_filters( 'friends_suggest_user_login', User::get_user_login_for_url( $url ), $url ); | |
| 1545 | - if ( empty( $vars['display_name'] ) ) { | |
| 1546 | - $vars['display_name'] = apply_filters( 'friends_suggest_display_name', User::get_display_name_for_url( $url ), $url ); | |
| 1547 | - } | |
| 1548 | - | |
| 1549 | - $vars['step2'] = true; | |
| 1550 | - | |
| 1551 | - $vars['subscribe'] = array( $url ); | |
| 1552 | - $vars['feeds'] = $this->friends->feed->discover_available_feeds( $url ); | |
| 1553 | - | |
| 1554 | - ob_start(); | |
| 1555 | - $ret = $this->process_admin_add_friend( $vars ); | |
| 1556 | - ob_end_clean(); | |
| 1557 | - | |
| 1558 | - if ( is_wp_error( $ret ) ) { | |
| 1559 | - return $ret; | |
| 1560 | - } | |
| 1561 | - | |
| 1562 | - $friend_user = User::get_by_username( $vars['user_login'] ); | |
| 1563 | - if ( ! $friend_user || is_wp_error( $friend_user ) ) { | |
| 1564 | - return new \WP_Error( 'friend_not_created', __( 'Friend could not be created.', 'friends' ) ); | |
| 1565 | - } | |
| 1566 | - | |
| 1567 | - return $friend_user->ID; | |
| 2426 | + public function create_and_follow( $user_id, $url ) { | |
| 2427 | + // TODO: replace with frontend functionality. | |
| 1568 | 2428 | } |
| 1569 | 2429 | |
| 1570 | 2430 | /** |
| 1571 | - * Previous process the Add Friend form. Todo: re-integrate. | |
| 1572 | - * | |
| 1573 | - * @param User $friend_user The Friend user. | |
| 1574 | - * @param array $vars The variables from the admin | |
| 1575 | - * submission. | |
| 1576 | - * | |
| 1577 | - * @return boolean true when there was no error. | |
| 2431 | + * Process the admin notification manager form submission. | |
| 1578 | 2432 | */ |
| 1579 | - public function process_admin_add_friend_response( $friend_user, $vars ) { | |
| 1580 | - if ( is_wp_error( $friend_user ) ) { | |
| 1581 | - $this->display_errors( $friend_user ); | |
| 1582 | - return false; | |
| 2433 | + public function process_admin_notification_manager() { | |
| 2434 | + if ( empty( $_POST ) ) { | |
| 2435 | + return; | |
| 1583 | 2436 | } |
| 1584 | 2437 | |
| 1585 | - if ( ! $friend_user instanceof User ) { | |
| 1586 | - ?> | |
| 1587 | - <div id="message" class="updated notice is-dismissible"><p> | |
| 1588 | - <?php | |
| 1589 | - // translators: %s is a username. | |
| 1590 | - esc_html_e( 'Unknown error', 'friends' ); | |
| 1591 | - ?> | |
| 1592 | - </p></div> | |
| 1593 | - <?php | |
| 1594 | - return false; | |
| 2438 | + if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'notification-manager' ) ) { | |
| 2439 | + return; | |
| 1595 | 2440 | } |
| 1596 | 2441 | |
| 1597 | - $feed_options = array(); | |
| 1598 | - if ( ! isset( $vars['feeds'] ) ) { | |
| 1599 | - $vars['feeds'] = array(); | |
| 1600 | - } | |
| 1601 | - foreach ( $vars['feeds'] as $feed ) { | |
| 1602 | - if ( isset( $feed['type'] ) ) { | |
| 1603 | - $feed['mime-type'] = $feed['type']; | |
| 1604 | - unset( $feed['type'] ); | |
| 2442 | + $this->check_admin_settings(); | |
| 2443 | + | |
| 2444 | + if ( ! empty( $_POST['notification_keywords'] ) && is_array( $_POST['notification_keywords'] ) ) { | |
| 2445 | + $keywords = array(); | |
| 2446 | + foreach ( wp_unslash( $_POST['notification_keywords'] ) as $i => $keyword ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 2447 | + if ( trim( $keyword ) ) { | |
| 2448 | + $keywords[] = array( | |
| 2449 | + 'enabled' => isset( $_POST['notification_keywords_enabled'][ $i ] ) && boolval( $_POST['notification_keywords_enabled'][ $i ] ), | |
| 2450 | + 'keyword' => sanitize_text_field( $keyword ), | |
| 2451 | + ); | |
| 2452 | + } | |
| 1605 | 2453 | } |
| 1606 | - $feed_options[ $feed['url'] ] = $feed; | |
| 2454 | + update_option( 'friends_notification_keywords', $keywords ); | |
| 1607 | 2455 | } |
| 1608 | 2456 | |
| 1609 | - // Save the all feeds for possible later activation. | |
| 1610 | - $friend_user->save_feeds( $feed_options ); | |
| 1611 | - | |
| 1612 | - if ( ! isset( $vars['subscribe'] ) ) { | |
| 1613 | - $vars['subscribe'] = array(); | |
| 2457 | + if ( isset( $_POST['keyword_notification_override'] ) && boolval( $_POST['keyword_notification_override'] ) ) { | |
| 2458 | + delete_user_option( get_current_user_id(), 'friends_keyword_notification_override_disabled' ); | |
| 2459 | + } else { | |
| 2460 | + update_user_option( get_current_user_id(), 'friends_keyword_notification_override_disabled', 1 ); | |
| 1614 | 2461 | } |
| 1615 | 2462 | |
| 1616 | - $count = 0; | |
| 1617 | - foreach ( $vars['subscribe'] as $feed_url ) { | |
| 1618 | - if ( ! isset( $feed_options[ $feed_url ] ) ) { | |
| 1619 | - continue; | |
| 1620 | - } | |
| 1621 | - $new_feed = $friend_user->subscribe( $feed_url, $feed_options[ $feed_url ] ); | |
| 1622 | - if ( ! is_wp_error( $new_feed ) ) { | |
| 1623 | - do_action( 'friends_user_feed_activated', $new_feed ); | |
| 1624 | - $count += 1; | |
| 1625 | - } | |
| 2463 | + if ( isset( $_POST['new_post_notification'] ) && boolval( $_POST['new_post_notification'] ) ) { | |
| 2464 | + delete_user_option( get_current_user_id(), 'friends_no_new_post_notification' ); | |
| 2465 | + } else { | |
| 2466 | + update_user_option( get_current_user_id(), 'friends_no_new_post_notification', 1 ); | |
| 1626 | 2467 | } |
| 1627 | 2468 | |
| 1628 | - add_filter( 'notify_about_new_friend_post', '__return_false', 999 ); | |
| 1629 | - | |
| 1630 | - wp_schedule_single_event( time(), 'friends_retrieve_user_feeds', array( $friend_user->ID ) ); | |
| 1631 | - | |
| 1632 | - if ( isset( $vars['errors'] ) ) { | |
| 1633 | - $this->display_errors( $vars['errors'] ); | |
| 2469 | + if ( isset( $_POST['friend_follower_notification'] ) && boolval( $_POST['friend_follower_notification'] ) ) { | |
| 2470 | + delete_user_option( get_current_user_id(), 'friends_no_friend_follower_notification' ); | |
| 2471 | + } else { | |
| 2472 | + update_user_option( get_current_user_id(), 'friends_no_friend_follower_notification', 1 ); | |
| 1634 | 2473 | } |
| 1635 | 2474 | |
| 1636 | - $friend_link = '<a href="' . esc_url( $this->admin_edit_user_link( $friend_user->get_local_friends_page_url(), $friend_user ) ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( $friend_user->display_name ) . '</a>'; | |
| 1637 | - $message = false; | |
| 1638 | - | |
| 1639 | - if ( $friend_user->has_cap( 'pending_friend_request' ) ) { | |
| 1640 | - // translators: %s is a Site URL. | |
| 1641 | - $message = sprintf( __( 'Friendship requested for site %s.', 'friends' ), $friend_link ); | |
| 1642 | - $message .= ' ' . sprintf( __( 'Until they respond, we have already subscribed you to their updates.', 'friends' ), $friend_link ); | |
| 1643 | - } elseif ( $friend_user->has_cap( 'friend' ) ) { | |
| 1644 | - // translators: %s is a Site URL. | |
| 1645 | - $message = sprintf( __( "You're now a friend of site %s.", 'friends' ), $friend_link ); | |
| 1646 | - // translators: %s is the friends page URL. | |
| 1647 | - } elseif ( $friend_user->has_cap( 'subscription' ) ) { | |
| 1648 | - if ( isset( $vars['friendship'] ) ) { | |
| 1649 | - // translators: %s is a Site URL. | |
| 1650 | - $message = sprintf( __( 'No friends plugin installed at %s.', 'friends' ), $friend_link ); | |
| 1651 | - $message .= ' ' . esc_html__( 'We subscribed you to their updates.', 'friends' ); | |
| 2475 | + foreach ( get_post_format_slugs() as $post_format ) { | |
| 2476 | + if ( isset( $_POST[ 'new_post_format_notification_' . $post_format ] ) && boolval( $_POST[ 'new_post_format_notification_' . $post_format ] ) ) { | |
| 2477 | + delete_user_option( get_current_user_id(), 'friends_no_new_post_format_notification_' . $post_format ); | |
| 1652 | 2478 | } else { |
| 1653 | - // translators: %s is a Site URL. | |
| 1654 | - $message = sprintf( __( "You're now subscribed to %s.", 'friends' ), $friend_link ); | |
| 2479 | + update_user_option( get_current_user_id(), 'friends_no_new_post_format_notification_' . $post_format, 1 ); | |
| 1655 | 2480 | } |
| 1656 | 2481 | } |
| 1657 | 2482 | |
| 1658 | - if ( $message ) { | |
| 1659 | - ?> | |
| 1660 | - <div id="message" class="updated notice is-dismissible"><p> | |
| 1661 | - <?php | |
| 1662 | - echo wp_kses( $message, array( 'a' => array( 'href' => array() ) ) ); | |
| 1663 | - // translators: %s is the friends page URL. | |
| 1664 | - echo ' ', wp_kses( sprintf( __( 'Go to your <a href=%s>friends page</a> to view their posts.', 'friends' ), '"' . esc_url( $friend_user->get_local_friends_page_url() ) . '"' ), array( 'a' => array( 'href' => array() ) ) ); | |
| 1665 | - echo ' <span id="fetch-feeds" data-nonce="', esc_attr( wp_create_nonce( 'fetch-feeds-' . $friend_user->user_login ) ), '" data-friend=', esc_attr( $friend_user->user_login ), '>', __( 'Fetching feeds...', 'friends' ), '</span>'; | |
| 1666 | - ?> | |
| 1667 | - </p></div> | |
| 1668 | - <?php | |
| 1669 | - return true; | |
| 1670 | - } | |
| 1671 | - | |
| 1672 | - ?> | |
| 1673 | - <div id="message" class="updated notice is-dismissible"><p> | |
| 1674 | - <?php | |
| 1675 | - // translators: %s is a username. | |
| 1676 | - echo esc_html( sprintf( __( 'User %s could not be assigned the appropriate role.', 'friends' ), $friend_user->display_name ) ); | |
| 1677 | - ?> | |
| 1678 | - </p></div> | |
| 1679 | - <?php | |
| 1680 | - return false; | |
| 1681 | - } | |
| 1682 | - | |
| 1683 | - /** | |
| 1684 | - * Process the Add Friend form. | |
| 1685 | - * | |
| 1686 | - * @param array $vars The POST or GET variables. | |
| 1687 | - * | |
| 1688 | - * @return boolean A \WP_Error or void. | |
| 1689 | - */ | |
| 1690 | - public function process_admin_add_friend( $vars ) { | |
| 1691 | - $errors = new \WP_Error(); | |
| 1692 | - $args = array(); | |
| 1693 | - | |
| 1694 | - $friend_url = isset( $vars['friend_url'] ) ? trim( $vars['friend_url'] ) : ''; | |
| 1695 | - $codeword = isset( $vars['codeword'] ) ? trim( $vars['codeword'] ) : ''; | |
| 1696 | - $message = isset( $vars['message'] ) ? trim( $vars['message'] ) : ''; | |
| 1697 | - | |
| 1698 | - $friends_plugin = false; | |
| 1699 | - $friend_user = false; | |
| 1700 | - | |
| 1701 | - $protocol = wp_parse_url( $friend_url, PHP_URL_SCHEME ); | |
| 1702 | - if ( ! $protocol ) { | |
| 1703 | - // Allow adding a friend by username. | |
| 1704 | - if ( is_multisite() ) { | |
| 1705 | - $friend_user = get_user_by( 'login', $friend_url ); | |
| 1706 | - if ( $friend_user ) { | |
| 1707 | - $site = get_active_blog_for_user( $friend_user->ID ); | |
| 1708 | - // Ensure we're using the same URL protocol. | |
| 1709 | - $friend_url = set_url_scheme( $site->siteurl ); | |
| 1710 | - } | |
| 2483 | + foreach ( array_keys( $this->friends->feed->get_registered_parsers() ) as $parser ) { | |
| 2484 | + if ( isset( $_POST[ 'new_post_by_parser_notification_' . $parser ] ) && boolval( $_POST[ 'new_post_by_parser_notification_' . $parser ] ) ) { | |
| 2485 | + delete_user_option( get_current_user_id(), 'friends_no_new_post_by_parser_notification_' . $parser ); | |
| 2486 | + } else { | |
| 2487 | + update_user_option( get_current_user_id(), 'friends_no_new_post_by_parser_notification_' . $parser, 1 ); | |
| 1711 | 2488 | } |
| 1712 | - | |
| 1713 | - // If unsuccessful, then the protocol was forgotten. | |
| 1714 | - if ( ! $friend_user ) { | |
| 1715 | - $friend_url = apply_filters( 'friends_rewrite_incoming_url', 'https://' . $friend_url, $friend_url ); | |
| 1716 | - } | |
| 1717 | 2489 | } |
| 1718 | - $friend_user_login = apply_filters( 'friends_suggest_user_login', User::get_user_login_for_url( $friend_url ), $friend_url ); | |
| 1719 | - $friend_display_name = apply_filters( 'friends_suggest_display_name', User::get_display_name_for_url( $friend_url ), $friend_url ); | |
| 1720 | 2490 | |
| 1721 | - $friend_user = get_user_by( 'login', $friend_user_login ); | |
| 1722 | - | |
| 1723 | - if ( $friend_user ) { | |
| 1724 | - $args['friends_multisite_user_login'] = $friend_user_login; | |
| 1725 | - $args['friends_multisite_display_name'] = $friend_display_name; | |
| 2491 | + if ( empty( $_POST['friend_listed'] ) ) { | |
| 2492 | + return; | |
| 1726 | 2493 | } |
| 1727 | - $rest_url = false; | |
| 2494 | + // This is an array, it is checked before use below. | |
| 2495 | + $friend_usernames = wp_unslash( $_POST['friend_listed'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 2496 | + $current_user_id = get_current_user_id(); | |
| 2497 | + $hide_from_friends_page = array(); | |
| 1728 | 2498 | |
| 1729 | - if ( ( isset( $vars['step2'] ) && isset( $vars['feeds'] ) && is_array( $vars['feeds'] ) ) || isset( $vars['step3'] ) ) { | |
| 1730 | - $friend_user_login = str_replace( ' ', '-', sanitize_user( $vars['user_login'] ) ); | |
| 1731 | - $friend_display_name = sanitize_text_field( $vars['display_name'] ); | |
| 1732 | - if ( ! $friend_user_login ) { | |
| 1733 | - // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 1734 | - $errors->add( 'user_login', __( '<strong>Error</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) ); | |
| 1735 | - } elseif ( ! is_multisite() && username_exists( $friend_user_login ) ) { | |
| 1736 | - // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 1737 | - $errors->add( 'user_login', __( '<strong>Error</strong>: This username is already registered. Please choose another one.' ) ); | |
| 2499 | + foreach ( $friend_usernames as $friend_username ) { | |
| 2500 | + $friend_user = User::get_by_username( $friend_username ); | |
| 2501 | + if ( ! $friend_user ) { | |
| 2502 | + continue; | |
| 1738 | 2503 | } |
| 1739 | - | |
| 1740 | - $feeds = $vars['feeds']; | |
| 1741 | - if ( ! $errors->has_errors() ) { | |
| 1742 | - $friend_user = false; | |
| 1743 | - if ( isset( $vars['friendship'] ) ) { | |
| 1744 | - $friend_user = $this->send_friend_request( $vars['friendship'], $friend_user_login, $friend_url, $friend_display_name, $codeword, $message ); | |
| 1745 | - if ( $friend_user->has_errors() ) { | |
| 1746 | - $vars['errors'] = $friend_user; | |
| 1747 | - } | |
| 1748 | - } | |
| 1749 | - | |
| 1750 | - $avatar = null; | |
| 1751 | - $description = null; | |
| 1752 | - foreach ( $feeds as $feed_details ) { | |
| 1753 | - if ( ! $avatar && ! empty( $feed_details['avatar'] ) ) { | |
| 1754 | - $avatar = $feed_details['avatar']; | |
| 1755 | - } | |
| 1756 | - if ( ! $description && ! empty( $feed_details['description'] ) ) { | |
| 1757 | - $description = $feed_details['description']; | |
| 1758 | - } | |
| 1759 | - } | |
| 1760 | - | |
| 1761 | - if ( ! $friend_user || is_wp_error( $friend_user ) ) { | |
| 1762 | - $friend_user = User::create( $friend_user_login, 'subscription', $friend_url, $friend_display_name, $avatar, $description ); | |
| 1763 | - } | |
| 1764 | - | |
| 1765 | - return $this->process_admin_add_friend_response( $friend_user, $vars ); | |
| 2504 | + $friend_username = $friend_user->user_login; | |
| 2505 | + if ( ! isset( $_POST['show_on_friends_page'][ $friend_username ] ) ) { | |
| 2506 | + $hide_from_friends_page[] = $friend_username; | |
| 1766 | 2507 | } |
| 1767 | 2508 | |
| 1768 | - if ( isset( $vars['friendship'] ) ) { | |
| 1769 | - $rest_url = $vars['friendship']; | |
| 1770 | - } else { | |
| 1771 | - $rest_url = $this->friends->rest->get_friends_rest_url( $feeds ); | |
| 2509 | + $no_new_post_notification = ! isset( $_POST['new_friend_post_notification'][ $friend_username ] ) || '0' === $_POST['new_friend_post_notification'][ $friend_username ]; | |
| 2510 | + if ( get_user_option( 'friends_no_new_post_notification_' . $friend_username ) !== $no_new_post_notification ) { | |
| 2511 | + update_user_option( $current_user_id, 'friends_no_new_post_notification_' . $friend_username, $no_new_post_notification ); | |
| 1772 | 2512 | } |
| 1773 | - } else { | |
| 1774 | - if ( home_url() === trailingslashit( $friend_url ) ) { | |
| 1775 | - return new \WP_Error( 'friend-yourself', __( 'It seems like you sent a friend request to yourself.', 'friends' ) ); | |
| 1776 | - } | |
| 1777 | 2513 | |
| 1778 | - if ( ! Friends::check_url( $friend_url ) ) { | |
| 1779 | - return new \WP_Error( 'invalid-url', __( 'You entered an invalid URL.', 'friends' ) ); | |
| 2514 | + $no_keyword_notification = ! isset( $_POST['keyword_notification'][ $friend_username ] ); | |
| 2515 | + if ( get_user_option( 'friends_no_keyword_notification_' . $friend_username ) !== $no_keyword_notification ) { | |
| 2516 | + update_user_option( $current_user_id, 'friends_no_keyword_notification_' . $friend_username, $no_keyword_notification ); | |
| 1780 | 2517 | } |
| 2518 | + } | |
| 1781 | 2519 | |
| 1782 | - $friend_user = User::get_user( $friend_user_login ); | |
| 1783 | - if ( $friend_user && ! is_wp_error( $friend_user ) ) { | |
| 1784 | - if ( $friend_user->is_valid_friend() ) { | |
| 1785 | - return new \WP_Error( 'already-friend', __( 'You are already friends with this site.', 'friends' ) ); | |
| 1786 | - } | |
| 2520 | + update_user_option( $current_user_id, 'friends_hide_from_friends_page', $hide_from_friends_page ); | |
| 1787 | 2521 | |
| 1788 | - // translators: %s is the name of a friend / site. | |
| 1789 | - return new \WP_Error( 'already-subscribed', sprintf( __( 'You are already subscribed to this site: %s', 'friends' ), '<a href="' . esc_url( $this->admin_edit_user_link( $friend_user->get_local_friends_page_url(), $friend_user ) ) . '">' . esc_html( $friend_user->display_name ) . '</a>' ) ); | |
| 1790 | - } | |
| 2522 | + do_action( 'friends_notification_manager_after_form_submit', $friend_usernames ); | |
| 1791 | 2523 | |
| 1792 | - $feeds = $this->friends->feed->discover_available_feeds( $friend_url ); | |
| 1793 | - if ( is_wp_error( $feeds ) ) { | |
| 1794 | - return $feeds; | |
| 1795 | - } | |
| 1796 | - if ( ! $feeds ) { | |
| 1797 | - return new \WP_Error( 'no-feed-found', __( 'No suitable feed was found at the provided address.', 'friends' ) ); | |
| 1798 | - } | |
| 2524 | + if ( isset( $_GET['_wp_http_referer'] ) ) { | |
| 2525 | + wp_safe_redirect( wp_get_referer() ); | |
| 2526 | + } else { | |
| 2527 | + wp_safe_redirect( add_query_arg( 'updated', '1', remove_query_arg( array( '_wp_http_referer', '_wpnonce' ) ) ) ); | |
| 2528 | + } | |
| 2529 | + exit; | |
| 2530 | + } | |
| 1799 | 2531 | |
| 1800 | - $better_display_name = User::get_display_name_from_feeds( $feeds ); | |
| 1801 | - if ( $better_display_name ) { | |
| 1802 | - $friend_display_name = $better_display_name; | |
| 1803 | - } | |
| 2532 | + /** | |
| 2533 | + * Render the admin notification manager. | |
| 2534 | + */ | |
| 2535 | + public function render_admin_notification_manager() { | |
| 2536 | + Friends::template_loader()->get_template_part( | |
| 2537 | + 'admin/settings-header', | |
| 2538 | + null, | |
| 2539 | + array( | |
| 2540 | + 'active' => 'friends-notification-manager', | |
| 2541 | + 'title' => __( 'Friends', 'friends' ), | |
| 2542 | + ) | |
| 2543 | + ); | |
| 2544 | + $this->check_admin_settings(); | |
| 1804 | 2545 | |
| 1805 | - $rest_url = $this->friends->rest->get_friends_rest_url( $feeds ); | |
| 1806 | - } | |
| 2546 | + $friend_users = User_Query::all_subscriptions(); | |
| 1807 | 2547 | |
| 1808 | - if ( $rest_url ) { | |
| 1809 | - $friends_plugin = $rest_url; | |
| 1810 | - unset( $feeds[ $rest_url ] ); | |
| 2548 | + $hide_from_friends_page = get_user_option( 'friends_hide_from_friends_page' ); | |
| 2549 | + if ( ! $hide_from_friends_page ) { | |
| 2550 | + $hide_from_friends_page = array(); | |
| 1811 | 2551 | } |
| 1812 | 2552 | |
| 1813 | - if ( isset( $vars['quick-subscribe'] ) ) { | |
| 1814 | - $vars['feeds'] = $feeds; | |
| 1815 | - $vars['subscribe'] = array(); | |
| 1816 | - foreach ( $feeds as $feed_url => $details ) { | |
| 1817 | - if ( isset( $details['autoselect'] ) && $details['autoselect'] ) { | |
| 1818 | - $vars['subscribe'][] = $feed_url; | |
| 1819 | - } | |
| 1820 | - } | |
| 2553 | + $args = array( | |
| 2554 | + 'friend_users' => $friend_users->get_results(), | |
| 2555 | + 'friends_settings_url' => add_query_arg( '_wp_http_referer', remove_query_arg( '_wp_http_referer' ), self_admin_url( 'admin.php?page=friends-settings' ) ), | |
| 2556 | + 'hide_from_friends_page' => $hide_from_friends_page, | |
| 2557 | + 'keyword_override_disabled' => get_user_option( 'friends_keyword_notification_override_disabled' ), | |
| 2558 | + 'no_new_post_notification' => get_user_option( 'friends_no_new_post_notification' ), | |
| 2559 | + 'no_keyword_notification' => get_user_option( 'friends_no_keyword_notification' ), | |
| 2560 | + 'notification_keywords' => Feed::get_all_notification_keywords(), | |
| 2561 | + 'active_keywords' => Feed::get_active_notification_keywords(), | |
| 2562 | + 'feed_parsers' => $this->friends->feed->get_registered_parsers(), | |
| 2563 | + ); | |
| 1821 | 2564 | |
| 1822 | - $friend_user = false; | |
| 1823 | - if ( isset( $rest_url ) ) { | |
| 1824 | - $friend_user = $this->send_friend_request( $rest_url, $friend_user_login, $friend_url, $friend_display_name, $codeword, $message ); | |
| 1825 | - } | |
| 2565 | + if ( class_exists( '\Activitypub\Notification' ) ) { | |
| 2566 | + $args['no_friend_follower_notification'] = get_user_option( 'friends_no_friend_follower_notification' ); | |
| 2567 | + } | |
| 1826 | 2568 | |
| 1827 | - $avatar = null; | |
| 1828 | - $description = null; | |
| 1829 | - foreach ( $feeds as $feed_details ) { | |
| 1830 | - if ( ! $avatar && ! empty( $feed_details['avatar'] ) ) { | |
| 1831 | - $avatar = $feed_details['avatar']; | |
| 1832 | - } | |
| 1833 | - if ( ! $description && ! empty( $feed_details['description'] ) ) { | |
| 1834 | - $description = $feed_details['description']; | |
| 1835 | - } | |
| 1836 | - } | |
| 2569 | + Friends::template_loader()->get_template_part( | |
| 2570 | + 'admin/notification-manager', | |
| 2571 | + null, | |
| 2572 | + $args | |
| 2573 | + ); | |
| 1837 | 2574 | |
| 1838 | - if ( ! $friend_user || is_wp_error( $friend_user ) ) { | |
| 1839 | - $friend_user = User::create( $friend_user_login, 'subscription', $friend_url, $friend_display_name, $avatar, $description ); | |
| 1840 | - } | |
| 2575 | + Friends::template_loader()->get_template_part( 'admin/settings-footer' ); | |
| 2576 | + } | |
| 1841 | 2577 | |
| 1842 | - return $this->process_admin_add_friend_response( $friend_user, $vars ); | |
| 1843 | - } | |
| 1844 | - | |
| 2578 | + public function render_admin_import_export() { | |
| 1845 | 2579 | Friends::template_loader()->get_template_part( |
| 1846 | 2580 | 'admin/settings-header', |
| 1847 | 2581 | null, |
| 1848 | 2582 | array( |
| 1849 | - 'active' => 'add-friend-confirm', | |
| 1850 | - 'title' => __( 'Add New Friend', 'friends' ), | |
| 1851 | - 'menu' => array( | |
| 1852 | - '1. ' . __( 'Enter Details', 'friends' ) => 'add-friend' . ( isset( $friend_url ) ? '&url=' . urlencode( $friend_url ) : '' ), | |
| 1853 | - '2. ' . __( 'Confirm', 'friends' ) => 'add-friend-confirm', | |
| 1854 | - ), | |
| 2583 | + 'active' => 'friends-import-export', | |
| 2584 | + 'title' => __( 'Friends', 'friends' ), | |
| 1855 | 2585 | ) |
| 1856 | 2586 | ); |
| 2587 | + $this->check_admin_settings(); | |
| 1857 | 2588 | |
| 1858 | - if ( $errors->has_errors() ) { | |
| 1859 | - ?> | |
| 1860 | - <div id="message" class="updated notice is-dismissible"><p><?php echo wp_kses( $errors->get_error_message(), array( 'strong' => array() ) ); ?></p> | |
| 1861 | - </div> | |
| 1862 | - <?php | |
| 1863 | - } | |
| 2589 | + ?> | |
| 2590 | + <h1><?php esc_html_e( 'Import/Export', 'friends' ); ?></h1> | |
| 2591 | + <?php | |
| 1864 | 2592 | |
| 1865 | 2593 | Friends::template_loader()->get_template_part( |
| 1866 | - 'admin/select-feeds', | |
| 2594 | + 'admin/import-export', | |
| 1867 | 2595 | null, |
| 1868 | - array_merge( | |
| 1869 | - $args, | |
| 1870 | - array( | |
| 1871 | - 'friends_plugin' => $friends_plugin, | |
| 1872 | - 'friend_url' => $friend_url, | |
| 1873 | - 'friend_user_login' => $friend_user_login, | |
| 1874 | - 'friend_display_name' => $friend_display_name, | |
| 1875 | - 'friend_roles' => $this->get_friend_roles(), | |
| 1876 | - 'default_role' => get_option( 'friends_default_friend_role', 'friend' ), | |
| 1877 | - 'codeword' => $codeword, | |
| 1878 | - 'message' => $message, | |
| 1879 | - 'post_formats' => array_merge( array( 'autodetect' => __( 'Autodetect Post Format', 'friends' ) ), get_post_format_strings() ), | |
| 1880 | - 'registered_parsers' => $this->friends->feed->get_registered_parsers(), | |
| 1881 | - 'feeds' => $feeds, | |
| 1882 | - ) | |
| 2596 | + array( | |
| 2597 | + 'private_rss_key' => get_option( 'friends_private_rss_key' ), | |
| 1883 | 2598 | ) |
| 1884 | 2599 | ); |
| 2600 | + | |
| 2601 | + Friends::template_loader()->get_template_part( 'admin/settings-footer' ); | |
| 1885 | 2602 | } |
| 1886 | 2603 | |
| 1887 | - /** | |
| 1888 | - * Render the admin form for sending a friend request. | |
| 1889 | - */ | |
| 1890 | - public function render_admin_add_friend() { | |
| 1891 | - if ( ! friends::has_required_privileges() ) { | |
| 1892 | - wp_die( esc_html__( 'Sorry, you are not allowed to add friends.', 'friends' ) ); | |
| 2604 | + public function process_admin_import_export() { | |
| 2605 | + if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'friends-settings' ) ) { | |
| 2606 | + return; | |
| 1893 | 2607 | } |
| 1894 | 2608 | |
| 1895 | - if ( ! empty( $_GET['preview'] ) ) { | |
| 1896 | - $url = $_GET['preview']; | |
| 2609 | + if ( ! Friends::has_required_privileges() ) { | |
| 2610 | + return; | |
| 2611 | + } | |
| 1897 | 2612 | |
| 1898 | - ?> | |
| 1899 | - <h1> | |
| 1900 | - <?php | |
| 1901 | - // translators: %s is a URL. | |
| 1902 | - echo esc_html( sprintf( __( 'Preview for %s', 'friends' ), $url ) ); | |
| 1903 | - ?> | |
| 1904 | - </h1> | |
| 1905 | - <?php | |
| 1906 | - | |
| 1907 | - if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'preview-feed' ) ) { | |
| 1908 | - ?> | |
| 1909 | - <div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'For security reasons, this preview is not available.', 'friends' ); ?></p> | |
| 1910 | - </div> | |
| 1911 | - <?php | |
| 1912 | - exit; | |
| 2613 | + if ( isset( $_FILES['opml']['tmp_name'] ) ) { | |
| 2614 | + $opml = file_get_contents( $_FILES['opml']['tmp_name'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 2615 | + $feeds = Import::opml( $opml ); | |
| 2616 | + $users_created = count( $feeds ); | |
| 2617 | + $feeds_imported = 0; | |
| 2618 | + foreach ( $feeds as $user => $user_feeds ) { | |
| 2619 | + $feeds_imported += count( $user_feeds ); | |
| 1913 | 2620 | } |
| 1914 | - | |
| 1915 | - $parser_name = $this->friends->feed->get_registered_parser( $_GET['parser'] ); | |
| 1916 | - if ( ! $parser_name ) { | |
| 1917 | - ?> | |
| 1918 | - <div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'An unknown parser name was supplied.', 'friends' ); ?></p> | |
| 1919 | - </div> | |
| 1920 | - <?php | |
| 1921 | - exit; | |
| 1922 | - } | |
| 1923 | 2621 | ?> |
| 1924 | - <h3><?php esc_html_e( 'Parser Details', 'friends' ); ?></h3> | |
| 1925 | - <ul id="parser"> | |
| 1926 | - <li> | |
| 2622 | + <div class="friends-notice notice notice-success is-dismissible"> | |
| 2623 | + <p> | |
| 1927 | 2624 | <?php |
| 1928 | - echo wp_kses( | |
| 1929 | - // translators: %s is the name of a parser, e.g. simplepie. | |
| 1930 | - sprintf( __( 'Parser: %s', 'friends' ), $parser_name ), | |
| 1931 | - array( | |
| 1932 | - 'a' => array( | |
| 1933 | - 'href' => array(), | |
| 1934 | - 'rel' => array(), | |
| 1935 | - 'target' => array(), | |
| 1936 | - ), | |
| 2625 | + echo esc_html( | |
| 2626 | + sprintf( | |
| 2627 | + // translators: %d is the number of users imported. | |
| 2628 | + _n( 'Imported %d user.', 'Imported %d users.', $users_created, 'friends' ), | |
| 2629 | + $users_created | |
| 1937 | 2630 | ) |
| 1938 | 2631 | ); |
| 1939 | 2632 | ?> |
| 1940 | - </li> | |
| 1941 | - </ul> | |
| 1942 | - <h3><?php esc_html_e( 'Items in the Feed', 'friends' ); ?></h3> | |
| 2633 | + <?php | |
| 2634 | + echo esc_html( | |
| 2635 | + sprintf( | |
| 2636 | + // translators: %d is the number of feeds imported. | |
| 2637 | + _n( 'They had %d feed.', 'They had %d feeds.', $feeds_imported, 'friends' ), | |
| 2638 | + $feeds_imported | |
| 2639 | + ) | |
| 2640 | + ); | |
| 2641 | + ?> | |
| 2642 | + </p> | |
| 2643 | + </div> | |
| 2644 | + <?php | |
| 2645 | + } | |
| 2646 | + } | |
| 1943 | 2647 | |
| 1944 | - <?php | |
| 2648 | + public function process_admin_duplicate_remover() { | |
| 2649 | + $friend = $this->check_admin_duplicate_remover(); | |
| 1945 | 2650 | |
| 1946 | - $items = $this->friends->feed->preview( $_GET['parser'], $url, isset( $_GET['feed'] ) ? intval( $_GET['feed'] ) : null ); | |
| 1947 | - if ( is_wp_error( $items ) ) { | |
| 1948 | - ?> | |
| 1949 | - <div id="message" class="updated notice is-dismissible"><p><?php echo esc_html( $items->get_error_message() ); ?></p> | |
| 1950 | - </div> | |
| 1951 | - <?php | |
| 1952 | - exit; | |
| 1953 | - } | |
| 1954 | - ?> | |
| 2651 | + // Nonce verification done in check_admin_duplicate_remover. | |
| 2652 | + // phpcs:disable WordPress.Security.NonceVerification.Missing | |
| 1955 | 2653 | |
| 1956 | - <ul> | |
| 1957 | - <?php | |
| 1958 | - foreach ( $items as $item ) { | |
| 1959 | - $title = $item->title; | |
| 1960 | - if ( 'status' === $item->post_format ) { | |
| 1961 | - $title = strip_tags( $item->content ); | |
| 1962 | - } | |
| 1963 | - ?> | |
| 1964 | - <li><a href="<?php echo esc_url( $item->permalink ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $item->date ); ?></a> (author: <?php echo esc_html( $item->author ); ?>, type: <?php echo esc_html( $item->post_format ); ?>): | |
| 1965 | - <?php if ( $title ) : ?> | |
| 1966 | - <a href="<?php echo esc_url( $item->permalink ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $title ); ?></a> <?php echo esc_html( str_word_count( wp_strip_all_tags( $item->content ) ) ); ?> words | |
| 1967 | - <?php else : ?> | |
| 1968 | - <p> | |
| 1969 | - <?php | |
| 1970 | - echo wp_kses( | |
| 1971 | - wp_trim_excerpt( $item->content ), | |
| 1972 | - array( | |
| 1973 | - 'a' => array( 'href' => array() ), | |
| 1974 | - 'img' => array( 'src' => array() ), | |
| 1975 | - ) | |
| 1976 | - ); | |
| 1977 | - ?> | |
| 1978 | - </p> | |
| 1979 | - <?php endif; ?> | |
| 1980 | - </li> | |
| 1981 | - <?php | |
| 1982 | - } | |
| 1983 | - ?> | |
| 1984 | - </ul> | |
| 1985 | - <?php | |
| 1986 | - return; | |
| 2654 | + // We iterate over this array and then we sanitize _id. | |
| 2655 | + // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 2656 | + if ( empty( $_POST['deleteduplicate'] ) || ! is_array( $_POST['deleteduplicate'] ) ) { | |
| 2657 | + return; | |
| 1987 | 2658 | } |
| 1988 | 2659 | |
| 1989 | - if ( apply_filters( 'friends_debug', false ) && isset( $_GET['next'] ) ) { | |
| 1990 | - $_POST = $_REQUEST; | |
| 1991 | - $_POST['_wpnonce'] = wp_create_nonce( 'add-friend' ); | |
| 1992 | - if ( ! empty( $_POST['url'] ) && ! isset( $_POST['friend_url'] ) ) { | |
| 1993 | - $_POST['friend_url'] = $_POST['url']; | |
| 1994 | - $parsed_url = parse_url( $_POST['friend_url'] ); | |
| 1995 | - if ( isset( $parsed_url['host'] ) ) { | |
| 1996 | - if ( ! isset( $parsed_url['scheme'] ) ) { | |
| 1997 | - $_POST['friend_url'] = 'https://' . ltrim( $_POST['friend_url'], '/' ); | |
| 1998 | - } | |
| 1999 | - } | |
| 2660 | + $deleted = 0; | |
| 2661 | + foreach ( array_keys( wp_unslash( $_POST['deleteduplicate'] ) ) as $_id ) { | |
| 2662 | + if ( ! is_numeric( $_id ) ) { | |
| 2663 | + continue; | |
| 2000 | 2664 | } |
| 2665 | + | |
| 2666 | + if ( wp_delete_post( intval( $_id ) ) ) { | |
| 2667 | + ++$deleted; | |
| 2668 | + } | |
| 2001 | 2669 | } |
| 2670 | + // phpcs:enable WordPress.Security.NonceVerification.Missing | |
| 2671 | + // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 2002 | 2672 | |
| 2003 | - $response = null; | |
| 2004 | - $postdata = apply_filters( 'friends_add_friend_postdata', $_POST ); | |
| 2005 | - if ( ! empty( $postdata ) ) { | |
| 2006 | - if ( ! wp_verify_nonce( $postdata['_wpnonce'], 'add-friend' ) ) { | |
| 2007 | - $response = new \WP_Error( 'invalid-nonce', __( 'For security reasons, please verify the URL and click next if you want to proceed.', 'friends' ) ); | |
| 2008 | - } else { | |
| 2009 | - $response = $this->process_admin_add_friend( $postdata ); | |
| 2010 | - } | |
| 2011 | - if ( is_wp_error( $response ) ) { | |
| 2012 | - ?> | |
| 2013 | - <div id="message" class="updated notice is-dismissible"><p> | |
| 2014 | - <?php | |
| 2015 | - $message = $response->get_error_message(); | |
| 2016 | - if ( $response->get_error_data() ) { | |
| 2017 | - $message .= ' (' . $response->get_error_data() . ')'; | |
| 2018 | - } | |
| 2019 | - echo wp_kses( | |
| 2020 | - $message, | |
| 2021 | - array( | |
| 2022 | - 'strong' => array(), | |
| 2023 | - 'a' => array( | |
| 2024 | - 'href' => array(), | |
| 2025 | - 'rel' => array(), | |
| 2026 | - 'target' => array(), | |
| 2027 | - ), | |
| 2028 | - ) | |
| 2029 | - ); | |
| 2030 | - ?> | |
| 2031 | - </p> | |
| 2032 | - </div> | |
| 2033 | - <?php | |
| 2034 | - } | |
| 2035 | - if ( is_null( $response ) ) { | |
| 2036 | - return; | |
| 2037 | - } | |
| 2673 | + if ( $deleted ) { | |
| 2674 | + wp_safe_redirect( add_query_arg( 'deleted', $deleted ) ); | |
| 2675 | + exit; | |
| 2038 | 2676 | } |
| 2677 | + } | |
| 2678 | + public function check_admin_duplicate_remover() { | |
| 2679 | + if ( ! Friends::is_main_user() ) { | |
| 2680 | + wp_die( esc_html__( 'Sorry, you are not allowed to edit the rules.', 'friends' ) ); | |
| 2681 | + } | |
| 2039 | 2682 | |
| 2040 | - $args = array( | |
| 2041 | - 'friend_url' => '', | |
| 2042 | - 'add-friends-placeholder' => apply_filters( 'friends_add_friends_input_placeholder', __( 'Enter URL', 'friends' ) ), | |
| 2043 | - ); | |
| 2683 | + if ( ! isset( $_GET['user'] ) ) { | |
| 2684 | + wp_die( esc_html__( 'Invalid user.', 'friends' ) ); | |
| 2685 | + } | |
| 2044 | 2686 | |
| 2045 | - if ( ! empty( $_GET['url'] ) || ! empty( $_POST['url'] ) ) { | |
| 2046 | - $friend_url = isset( $_GET['url'] ) ? $_GET['url'] : $_POST['url']; | |
| 2047 | - $parsed_url = parse_url( $friend_url ); | |
| 2048 | - if ( isset( $parsed_url['host'] ) ) { | |
| 2049 | - if ( ! isset( $parsed_url['scheme'] ) ) { | |
| 2050 | - $args['friend_url'] = apply_filters( 'friends_rewrite_incoming_url', 'https://' . ltrim( $friend_url, '/' ), $friend_url, $parsed_url ); | |
| 2051 | - } else { | |
| 2052 | - $args['friend_url'] = $friend_url; | |
| 2053 | - } | |
| 2054 | - } elseif ( preg_match( '/^@?' . Feed_Parser_ActivityPub::ACTIVITYPUB_USERNAME_REGEXP . '$/i', $friend_url ) ) { | |
| 2055 | - $args['friend_url'] = $friend_url; | |
| 2056 | - } | |
| 2687 | + if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'duplicate-remover-' . sanitize_user( wp_unslash( $_GET['user'] ) ) ) ) { | |
| 2688 | + wp_die( esc_html__( 'Invalid nonce.', 'friends' ) ); | |
| 2057 | 2689 | } |
| 2058 | 2690 | |
| 2059 | - Friends::template_loader()->get_template_part( | |
| 2060 | - 'admin/settings-header', | |
| 2061 | - null, | |
| 2062 | - array( | |
| 2063 | - 'active' => 'add-friend', | |
| 2064 | - 'title' => __( 'Add New Friend', 'friends' ), | |
| 2065 | - 'menu' => array( | |
| 2066 | - '1. ' . __( 'Enter Details', 'friends' ) => 'add-friend' . ( isset( $friend_url ) ? '&url=' . urlencode( $friend_url ) : '' ), | |
| 2067 | - '2. ' . __( 'Confirm', 'friends' ) => false, | |
| 2068 | - ), | |
| 2069 | - ) | |
| 2070 | - ); | |
| 2691 | + $friend = User::get_by_username( sanitize_user( wp_unslash( $_GET['user'] ) ) ); | |
| 2692 | + if ( ! $friend || is_wp_error( $friend ) ) { | |
| 2693 | + wp_die( esc_html__( 'Invalid username.', 'friends' ) ); | |
| 2694 | + } | |
| 2071 | 2695 | |
| 2072 | - Friends::template_loader()->get_template_part( 'admin/add-friend', null, $args ); | |
| 2696 | + if ( ! $friend->has_cap( 'subscription' ) ) { | |
| 2697 | + wp_die( esc_html__( 'This is not a user related to this plugin.', 'friends' ) ); | |
| 2698 | + } | |
| 2073 | 2699 | |
| 2074 | - Friends::template_loader()->get_template_part( | |
| 2075 | - 'admin/latest-friends', | |
| 2076 | - null, | |
| 2077 | - array( | |
| 2078 | - 'friend_requests' => User_Query::recent_friends_subscriptions( 25 )->get_results(), | |
| 2079 | - ) | |
| 2080 | - ); | |
| 2081 | - Friends::template_loader()->get_template_part( 'admin/settings-footer', null, $args ); | |
| 2700 | + return $friend; | |
| 2082 | 2701 | } |
| 2083 | - | |
| 2084 | 2702 | /** |
| 2085 | - * Process the admin notification manager form submission. | |
| 2703 | + * Render the duplicates remover | |
| 2086 | 2704 | */ |
| 2087 | - public function process_admin_notification_manager() { | |
| 2705 | + public function render_admin_duplicate_remover() { | |
| 2706 | + $friend = $this->check_admin_duplicate_remover(); | |
| 2088 | 2707 | |
| 2089 | - if ( empty( $_POST ) || empty( $_POST['friend_listed'] ) ) { | |
| 2090 | - return; | |
| 2708 | + $this->header_edit_friend( $friend, 'duplicate-remover' ); | |
| 2709 | + // phpcs:disable WordPress.Security.NonceVerification | |
| 2710 | + if ( isset( $_GET['deleted'] ) ) { | |
| 2711 | + ?> | |
| 2712 | + <div id="message" class="updated notice is-dismissible"><p> | |
| 2713 | + <?php | |
| 2714 | + $deleted = intval( $_GET['deleted'] ); | |
| 2715 | + echo esc_html( | |
| 2716 | + sprintf( | |
| 2717 | + // translators: %d is the number of duplicates deleted. | |
| 2718 | + _n( 'Deleted %d selected duplicate.', 'Deleted %d selected duplicates.', $deleted, 'friends' ), | |
| 2719 | + $deleted | |
| 2720 | + ) | |
| 2721 | + ); | |
| 2722 | + ?> | |
| 2723 | + </p></div> | |
| 2724 | + <?php | |
| 2091 | 2725 | } |
| 2726 | + // phpcs:enable WordPress.Security.NonceVerification | |
| 2092 | 2727 | |
| 2093 | - if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'notification-manager' ) ) { | |
| 2094 | - return; | |
| 2095 | - } | |
| 2728 | + $friend_posts = new \WP_Query(); | |
| 2096 | 2729 | |
| 2097 | - $this->check_admin_settings(); | |
| 2730 | + $friend_posts->set( 'post_type', Friends::CPT ); | |
| 2731 | + $friend_posts->set( 'post_status', array( 'publish', 'private', 'trash' ) ); | |
| 2732 | + $friend_posts->set( 'posts_per_page', 100 ); | |
| 2733 | + $friend_posts = $friend->modify_query_by_author( $friend_posts ); | |
| 2098 | 2734 | |
| 2099 | - if ( isset( $_POST['notification_keywords'] ) && $_POST['notification_keywords'] ) { | |
| 2100 | - $keywords = array(); | |
| 2101 | - foreach ( $_POST['notification_keywords'] as $i => $keyword ) { | |
| 2102 | - if ( trim( $keyword ) ) { | |
| 2103 | - $keywords[] = array( | |
| 2104 | - 'enabled' => isset( $_POST['notification_keywords_enabled'][ $i ] ) && $_POST['notification_keywords_enabled'][ $i ], | |
| 2105 | - 'keyword' => $keyword, | |
| 2106 | - ); | |
| 2107 | - } | |
| 2735 | + $uniques = array(); | |
| 2736 | + foreach ( $friend_posts->get_posts() as $_post ) { | |
| 2737 | + $permalink = get_permalink( $_post ); | |
| 2738 | + if ( ! isset( $uniques[ $permalink ] ) ) { | |
| 2739 | + $uniques[ $permalink ] = $_post->ID; | |
| 2108 | 2740 | } |
| 2109 | - update_option( 'friends_notification_keywords', $keywords ); | |
| 2110 | 2741 | } |
| 2111 | 2742 | |
| 2112 | - if ( isset( $_POST['new_post_notification'] ) && $_POST['new_post_notification'] ) { | |
| 2113 | - delete_user_option( get_current_user_id(), 'friends_no_new_post_notification' ); | |
| 2114 | - } else { | |
| 2115 | - update_user_option( get_current_user_id(), 'friends_no_new_post_notification', 1 ); | |
| 2116 | - } | |
| 2743 | + $args = array( | |
| 2744 | + 'friend' => $friend, | |
| 2745 | + 'friend_posts' => $friend_posts, | |
| 2746 | + 'uniques' => array_flip( $uniques ), | |
| 2747 | + 'feed' => $this->friends->feed, | |
| 2748 | + ); | |
| 2117 | 2749 | |
| 2118 | - $friend_ids = $_POST['friend_listed']; | |
| 2119 | - $current_user_id = get_current_user_id(); | |
| 2120 | - $hide_from_friends_page = array(); | |
| 2750 | + Friends::template_loader()->get_template_part( 'admin/duplicates', null, $args ); | |
| 2751 | + } | |
| 2121 | 2752 | |
| 2122 | - foreach ( $friend_ids as $friend_id ) { | |
| 2123 | - if ( ! isset( $_POST['show_on_friends_page'][ $friend_id ] ) ) { | |
| 2124 | - $hide_from_friends_page[] = $friend_id; | |
| 2125 | - } | |
| 2126 | 2753 | |
| 2127 | - $no_new_post_notification = ! isset( $_POST['new_friend_post_notification'][ $friend_id ] ) || '0' === $_POST['new_friend_post_notification'][ $friend_id ]; | |
| 2128 | - if ( get_user_option( 'friends_no_new_post_notification_' . $friend_id ) !== $no_new_post_notification ) { | |
| 2129 | - update_user_option( $current_user_id, 'friends_no_new_post_notification_' . $friend_id, $no_new_post_notification ); | |
| 2130 | - } | |
| 2754 | + public static function get_browser_api_key_user( $key ) { | |
| 2755 | + $key = (string) $key; | |
| 2756 | + if ( ! $key ) { | |
| 2757 | + return false; | |
| 2758 | + } | |
| 2131 | 2759 | |
| 2132 | - $no_keyword_notification = ! isset( $_POST['keyword_notification'][ $friend_id ] ); | |
| 2133 | - if ( get_user_option( 'friends_no_keyword_notification_' . $friend_id ) !== $no_keyword_notification ) { | |
| 2134 | - update_user_option( $current_user_id, 'friends_no_keyword_notification_' . $friend_id, $no_keyword_notification ); | |
| 2135 | - } | |
| 2760 | + $parts = explode( '-', $key, 3 ); | |
| 2761 | + if ( 3 !== count( $parts ) ) { | |
| 2762 | + return false; | |
| 2136 | 2763 | } |
| 2137 | 2764 | |
| 2138 | - update_user_option( $current_user_id, 'friends_hide_from_friends_page', $hide_from_friends_page ); | |
| 2765 | + $user_id = (int) $parts[1]; | |
| 2766 | + if ( ! $user_id ) { | |
| 2767 | + return false; | |
| 2768 | + } | |
| 2139 | 2769 | |
| 2140 | - do_action( 'friends_notification_manager_after_form_submit', $friend_ids ); | |
| 2770 | + $desired_key = get_user_option( 'friends_browser_api_key', $user_id ); | |
| 2771 | + if ( ! $desired_key || ! hash_equals( (string) $desired_key, (string) $key ) ) { | |
| 2772 | + return false; | |
| 2773 | + } | |
| 2141 | 2774 | |
| 2142 | - if ( isset( $_GET['_wp_http_referer'] ) ) { | |
| 2143 | - wp_safe_redirect( wp_get_referer() ); | |
| 2144 | - } else { | |
| 2145 | - wp_safe_redirect( add_query_arg( 'updated', '1', remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ); | |
| 2775 | + $user = get_user_by( 'ID', $user_id ); | |
| 2776 | + if ( ! $user ) { | |
| 2777 | + return false; | |
| 2146 | 2778 | } |
| 2147 | - exit; | |
| 2779 | + | |
| 2780 | + return $user; | |
| 2148 | 2781 | } |
| 2149 | 2782 | |
| 2150 | - /** | |
| 2151 | - * Render the admin notification manager. | |
| 2152 | - */ | |
| 2153 | - public function render_admin_notification_manager() { | |
| 2154 | - Friends::template_loader()->get_template_part( | |
| 2155 | - 'admin/settings-header', | |
| 2156 | - null, | |
| 2157 | - array( | |
| 2158 | - 'active' => 'friends-notification-manager', | |
| 2159 | - 'title' => __( 'Friends', 'friends' ), | |
| 2160 | - ) | |
| 2161 | - ); | |
| 2162 | - $this->check_admin_settings(); | |
| 2783 | + public static function check_browser_api_key( $key ) { | |
| 2784 | + return false !== self::get_browser_api_key_user( $key ); | |
| 2785 | + } | |
| 2163 | 2786 | |
| 2164 | - ?> | |
| 2165 | - <h1><?php esc_html_e( 'Notification Manager', 'friends' ); ?></h1> | |
| 2166 | - <?php | |
| 2787 | + public static function revoke_browser_api_key( $user_id = false ) { | |
| 2788 | + if ( ! $user_id ) { | |
| 2789 | + $user_id = get_current_user_id(); | |
| 2790 | + } | |
| 2167 | 2791 | |
| 2168 | - $friend_users = new User_Query( | |
| 2169 | - array( | |
| 2170 | - 'role__in' => array( 'friend', 'acquaintance', 'pending_friend_request', 'friend_request', 'subscription' ), | |
| 2171 | - 'orderby' => 'display_name', | |
| 2172 | - 'order' => 'ASC', | |
| 2173 | - ) | |
| 2174 | - ); | |
| 2792 | + delete_user_option( $user_id, 'friends_browser_api_key' ); | |
| 2793 | + } | |
| 2175 | 2794 | |
| 2176 | - $hide_from_friends_page = get_user_option( 'friends_hide_from_friends_page' ); | |
| 2177 | - if ( ! $hide_from_friends_page ) { | |
| 2178 | - $hide_from_friends_page = array(); | |
| 2795 | + public static function get_browser_api_key( $user_id = false ) { | |
| 2796 | + if ( ! $user_id ) { | |
| 2797 | + $user_id = get_current_user_id(); | |
| 2179 | 2798 | } |
| 2180 | 2799 | |
| 2181 | - Friends::template_loader()->get_template_part( | |
| 2182 | - 'admin/notification-manager', | |
| 2183 | - null, | |
| 2184 | - array( | |
| 2185 | - 'friend_users' => $friend_users->get_results(), | |
| 2186 | - 'friends_settings_url' => add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), self_admin_url( 'admin.php?page=friends-settings' ) ), | |
| 2187 | - 'hide_from_friends_page' => $hide_from_friends_page, | |
| 2188 | - 'no_friend_request_notification' => get_user_option( 'friends_no_friend_request_notification' ), | |
| 2189 | - 'no_new_post_notification' => get_user_option( 'friends_no_new_post_notification' ), | |
| 2190 | - 'no_keyword_notification' => get_user_option( 'friends_no_keyword_notification' ), | |
| 2191 | - 'notification_keywords' => Feed::get_all_notification_keywords(), | |
| 2192 | - 'active_keywords' => Feed::get_active_notification_keywords(), | |
| 2193 | - ) | |
| 2194 | - ); | |
| 2800 | + $key = get_user_option( 'friends_browser_api_key', $user_id ); | |
| 2801 | + if ( ! $key ) { | |
| 2802 | + $key = 'friends-' . $user_id . '-' . wp_generate_password( 32, false ); | |
| 2803 | + update_user_option( $user_id, 'friends_browser_api_key', $key ); | |
| 2804 | + } | |
| 2195 | 2805 | |
| 2196 | - Friends::template_loader()->get_template_part( 'admin/settings-footer' ); | |
| 2806 | + return $key; | |
| 2197 | 2807 | } |
| 2198 | 2808 | |
| 2199 | - public function maybe_remove_friendship_settings( $items ) { | |
| 2200 | - if ( ! get_option( 'friends_enable_wp_friendships' ) ) { | |
| 2201 | - unset( $items[ __( 'Friendships', 'friends' ) ] ); | |
| 2202 | - } | |
| 2203 | - return $items; | |
| 2204 | - } | |
| 2205 | - | |
| 2206 | - public function render_admin_wp_friendship_settings() { | |
| 2809 | + public function render_browser_extension() { | |
| 2810 | + add_filter( | |
| 2811 | + 'friends_admin_tabs', | |
| 2812 | + function ( $menu ) { | |
| 2813 | + $menu[ __( 'Browser Extension', 'friends' ) ] = 'friends-browser-extension'; | |
| 2814 | + return $menu; | |
| 2815 | + } | |
| 2816 | + ); | |
| 2207 | 2817 | Friends::template_loader()->get_template_part( |
| 2208 | 2818 | 'admin/settings-header', |
| 2209 | 2819 | null, |
| 2210 | 2820 | array( |
| 2211 | - 'active' => 'friends-wp-friendships', | |
| 2212 | - 'title' => __( 'Friends', 'friends' ), | |
| 2821 | + 'active' => 'friends-browser-extension', | |
| 2213 | 2822 | ) |
| 2214 | 2823 | ); |
| 2215 | 2824 | $this->check_admin_settings(); |
| 2825 | + $browser_api_key = self::get_browser_api_key(); | |
| 2216 | 2826 | |
| 2217 | - // In order to switch to the frontend locale, we need to first pretend that nothing was loaded yet. | |
| 2218 | - global $l10n; | |
| 2219 | - $l10n = array(); | |
| 2827 | + if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'friends-browser-extension' ) ) { | |
| 2828 | + if ( isset( $_POST['revoke-api-key'] ) ) { | |
| 2829 | + self::revoke_browser_api_key(); | |
| 2830 | + $browser_api_key = self::get_browser_api_key(); | |
| 2831 | + } | |
| 2832 | + } | |
| 2220 | 2833 | |
| 2221 | - switch_to_locale( $this->get_frontend_locale() ); | |
| 2222 | - // Now while loading the next translations we need to ensure that determine_locale() doesn't return the admin language but the frontend language. | |
| 2223 | - add_filter( 'pre_determine_locale', array( $this, 'get_frontend_locale' ) ); | |
| 2224 | - | |
| 2225 | - $wrong_codeword_message = __( 'An invalid codeword was provided.', 'friends' ); | |
| 2226 | - $comment_registration_message = __( 'Only people in my network can comment.', 'friends' ); | |
| 2227 | - $my_network = __( 'my network', 'friends' ); | |
| 2228 | - $comment_registration_default = strip_tags( | |
| 2229 | - /* translators: %s: Login URL. */ | |
| 2230 | - __( 'You must be <a href="%s">logged in</a> to post a comment.' ) // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 2231 | - ); | |
| 2232 | - // Now let's switch back to the admin language. | |
| 2233 | - remove_filter( 'pre_determine_locale', array( $this, 'get_frontend_locale' ) ); | |
| 2234 | - restore_previous_locale(); | |
| 2235 | - | |
| 2236 | - ?> | |
| 2237 | - <h1><?php esc_html_e( 'Friendships', 'friends' ); ?></h1> | |
| 2238 | - <?php | |
| 2239 | - | |
| 2240 | 2834 | Friends::template_loader()->get_template_part( |
| 2241 | - 'admin/settings-wp-friendships', | |
| 2835 | + 'admin/browser-extension', | |
| 2242 | 2836 | null, |
| 2243 | 2837 | array( |
| 2244 | - 'potential_main_users' => User_Query::all_admin_users(), | |
| 2245 | - 'main_user_id' => Friends::get_main_friend_user_id(), | |
| 2246 | - 'friend_roles' => $this->get_friend_roles(), | |
| 2247 | - 'default_role' => get_option( 'friends_default_friend_role', 'friend' ), | |
| 2248 | - 'comment_registration' => get_option( 'comment_registration' ), // WordPress option. | |
| 2249 | - 'comment_registration_message' => get_option( 'friends_comment_registration_message', $comment_registration_message ), | |
| 2250 | - 'comment_registration_default' => $comment_registration_default, | |
| 2251 | - 'my_network' => $my_network, | |
| 2252 | - 'public_profile_link' => home_url( '/friends/' ), | |
| 2253 | - 'codeword' => get_option( 'friends_codeword', 'friends' ), | |
| 2254 | - 'require_codeword' => get_option( 'friends_require_codeword' ), | |
| 2255 | - 'wrong_codeword_message' => get_option( 'friends_wrong_codeword_message', $wrong_codeword_message ), | |
| 2838 | + 'browser-api-key' => $browser_api_key, | |
| 2256 | 2839 | ) |
| 2257 | 2840 | ); |
| 2258 | 2841 | |
| 2259 | 2842 | Friends::template_loader()->get_template_part( 'admin/settings-footer' ); |
| 2260 | 2843 | } |
| 2261 | - public function process_admin_wp_friendship_settings() { | |
| 2262 | - if ( current_user_can( 'manage_options' ) ) { | |
| 2263 | 2844 | |
| 2264 | - if ( isset( $_POST['main_user_id'] ) && is_numeric( $_POST['main_user_id'] ) ) { | |
| 2265 | - update_option( 'friends_main_user_id', intval( $_POST['main_user_id'] ) ); | |
| 2266 | - } else { | |
| 2267 | - $main_user_id = Friends::get_main_friend_user_id(); | |
| 2268 | - $main_user_id_exists = false; | |
| 2269 | - $users = User_Query::all_admin_users(); | |
| 2270 | - foreach ( $users->get_results() as $user ) { | |
| 2271 | - if ( $user->ID === $main_user_id ) { | |
| 2272 | - $main_user_id_exists = true; | |
| 2273 | - break; | |
| 2274 | - } | |
| 2275 | - } | |
| 2276 | - if ( ! $main_user_id_exists ) { | |
| 2277 | - // Reset the main user id. | |
| 2278 | - delete_option( 'friends_main_user_id' ); | |
| 2279 | - Friends::get_main_friend_user_id(); | |
| 2280 | - } | |
| 2845 | + public function render_friends_logs() { | |
| 2846 | + add_filter( | |
| 2847 | + 'friends_admin_tabs', | |
| 2848 | + function ( $menu ) { | |
| 2849 | + $menu[ __( 'Logs', 'friends' ) ] = 'friends-logs'; | |
| 2850 | + return $menu; | |
| 2281 | 2851 | } |
| 2852 | + ); | |
| 2282 | 2853 | |
| 2283 | - if ( isset( $_POST['require_codeword'] ) && $_POST['require_codeword'] ) { | |
| 2284 | - update_option( 'friends_require_codeword', true ); | |
| 2285 | - } else { | |
| 2286 | - delete_option( 'friends_require_codeword' ); | |
| 2287 | - } | |
| 2288 | - | |
| 2289 | - if ( isset( $_POST['codeword'] ) && $_POST['codeword'] ) { | |
| 2290 | - update_option( 'friends_codeword', $_POST['codeword'] ); | |
| 2291 | - } else { | |
| 2292 | - delete_option( 'friends_codeword' ); | |
| 2293 | - } | |
| 2294 | - | |
| 2295 | - if ( isset( $_POST['wrong_codeword_message'] ) && $_POST['wrong_codeword_message'] ) { | |
| 2296 | - update_option( 'friends_wrong_codeword_message', $_POST['wrong_codeword_message'] ); | |
| 2297 | - } else { | |
| 2298 | - delete_option( 'friends_wrong_codeword_message' ); | |
| 2299 | - } | |
| 2300 | - | |
| 2301 | - if ( isset( $_POST['default_role'] ) && in_array( $_POST['default_role'], array( 'friend', 'acquaintance' ), true ) ) { | |
| 2302 | - update_option( 'friends_default_friend_role', $_POST['default_role'] ); | |
| 2303 | - } | |
| 2304 | - | |
| 2305 | - if ( isset( $_POST['comment_registration'] ) && $_POST['comment_registration'] ) { | |
| 2306 | - update_option( 'comment_registration', true ); | |
| 2307 | - } else { | |
| 2308 | - delete_option( 'comment_registration' ); | |
| 2309 | - } | |
| 2310 | - | |
| 2311 | - if ( isset( $_POST['comment_registration_message'] ) && $_POST['comment_registration_message'] ) { | |
| 2312 | - update_option( 'friends_comment_registration_message', $_POST['comment_registration_message'] ); | |
| 2313 | - } else { | |
| 2314 | - delete_option( 'friends_comment_registration_message' ); | |
| 2315 | - } | |
| 2316 | - } | |
| 2317 | - } | |
| 2318 | - public function render_admin_import_export() { | |
| 2319 | 2854 | Friends::template_loader()->get_template_part( |
| 2320 | 2855 | 'admin/settings-header', |
| 2321 | 2856 | null, |
| 2322 | 2857 | array( |
| 2323 | - 'active' => 'friends-import-export', | |
| 2324 | - 'title' => __( 'Friends', 'friends' ), | |
| 2858 | + 'active' => 'friends-logs', | |
| 2325 | 2859 | ) |
| 2326 | 2860 | ); |
| 2327 | 2861 | $this->check_admin_settings(); |
| 2328 | 2862 | |
| 2329 | - ?> | |
| 2330 | - <h1><?php esc_html_e( 'Import/Export', 'friends' ); ?></h1> | |
| 2331 | - <?php | |
| 2332 | - | |
| 2333 | 2863 | Friends::template_loader()->get_template_part( |
| 2334 | - 'admin/import-export', | |
| 2864 | + 'admin/logs', | |
| 2335 | 2865 | null, |
| 2336 | 2866 | array( |
| 2337 | - 'private_rss_key' => get_option( 'friends_private_rss_key' ), | |
| 2867 | + 'logs' => Logging::get_logs(), | |
| 2338 | 2868 | ) |
| 2339 | 2869 | ); |
| 2340 | 2870 | |
| 2341 | 2871 | Friends::template_loader()->get_template_part( 'admin/settings-footer' ); |
| @@ -2340,28 +2870,9 @@ | ||
| 2340 | 2870 | |
| 2341 | 2871 | Friends::template_loader()->get_template_part( 'admin/settings-footer' ); |
| 2342 | 2872 | } |
| 2343 | 2873 | |
| 2344 | - public function process_admin_import_export() { | |
| 2345 | - } | |
| 2346 | - | |
| 2347 | 2874 | /** |
| 2348 | - * Gets the friend roles. | |
| 2349 | - * | |
| 2350 | - * @return array The friend roles. | |
| 2351 | - */ | |
| 2352 | - public function get_friend_roles() { | |
| 2353 | - $roles = new \WP_Roles(); | |
| 2354 | - $friend_roles = array(); | |
| 2355 | - foreach ( $roles->roles as $role => $data ) { | |
| 2356 | - if ( isset( $data['capabilities']['friend'] ) ) { | |
| 2357 | - $friend_roles[ $role ] = $data['name']; | |
| 2358 | - } | |
| 2359 | - } | |
| 2360 | - return $friend_roles; | |
| 2361 | - } | |
| 2362 | - | |
| 2363 | - /** | |
| 2364 | 2875 | * Gets the roles associated with the Friends plugin. |
| 2365 | 2876 | * |
| 2366 | 2877 | * @return array The associated roles. |
| 2367 | 2878 | */ |
| @@ -2376,188 +2887,12 @@ | ||
| 2376 | 2887 | return $friend_roles; |
| 2377 | 2888 | } |
| 2378 | 2889 | |
| 2379 | 2890 | public static function get_users_url() { |
| 2380 | - return 'admin.php?page=friends-list'; | |
| 2891 | + return home_url( '/friends/following/' ); | |
| 2381 | 2892 | } |
| 2382 | 2893 | |
| 2383 | 2894 | /** |
| 2384 | - * Add actions to the user rows | |
| 2385 | - * | |
| 2386 | - * @param array $actions The existing actions. | |
| 2387 | - * @param \WP_User $user The user in question. | |
| 2388 | - * @return array The extended actions. | |
| 2389 | - */ | |
| 2390 | - public static function user_row_actions( array $actions, \WP_User $user ) { | |
| 2391 | - if ( | |
| 2392 | - ! Friends::has_required_privileges() || | |
| 2393 | - ( | |
| 2394 | - ! $user->has_cap( 'friend_request' ) && | |
| 2395 | - ! $user->has_cap( 'pending_friend_request' ) && | |
| 2396 | - ! $user->has_cap( 'friend' ) && | |
| 2397 | - ! $user->has_cap( 'subscription' ) | |
| 2398 | - ) | |
| 2399 | - ) { | |
| 2400 | - return $actions; | |
| 2401 | - } | |
| 2402 | - | |
| 2403 | - if ( is_multisite() ) { | |
| 2404 | - // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 2405 | - $actions = array_merge( array( 'edit' => '<a href="' . esc_url( self_admin_url( 'admin.php?page=edit-friend&user=' . $user->user_login ) ) . '">' . __( 'Edit' ) . '</a>' ), $actions ); | |
| 2406 | - } | |
| 2407 | - | |
| 2408 | - // Ensuire we have a friends user here. | |
| 2409 | - $user = new User( $user ); | |
| 2410 | - | |
| 2411 | - $actions['view'] = Frontend::get_link( | |
| 2412 | - $user->user_url, | |
| 2413 | - sprintf( | |
| 2414 | - // translators: %s: Author’s display name. | |
| 2415 | - __( 'Visit %s’s website' ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 2416 | - $user->display_name | |
| 2417 | - ), | |
| 2418 | - array(), | |
| 2419 | - $user | |
| 2420 | - ); | |
| 2421 | - unset( $actions['resetpassword'] ); | |
| 2422 | - | |
| 2423 | - if ( $user->has_cap( 'friend_request' ) ) { | |
| 2424 | - $link = self_admin_url( wp_nonce_url( 'users.php?action=accept_friend_request&users[]=' . $user->ID ) ); | |
| 2425 | - | |
| 2426 | - $actions['user_accept_friend_request'] = '<a href="' . esc_url( $link ) . '">' . __( 'Accept Friend Request', 'friends' ) . '</a>'; | |
| 2427 | - $message = get_user_option( 'friends_request_message', $user->ID ); | |
| 2428 | - $actions['friends friends_request_date'] = '<br/><span class="nonessential">' . esc_html( | |
| 2429 | - sprintf( | |
| 2430 | - // translators: %s is a date. | |
| 2431 | - __( 'Requested on %s', 'friends' ), | |
| 2432 | - date_i18n( __( 'F j, Y g:i a' ), strtotime( $user->user_registered ) ) // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 2433 | - ) | |
| 2434 | - ) . '</span>'; | |
| 2435 | - if ( $message ) { | |
| 2436 | - // translators: %s is a message text. | |
| 2437 | - $actions['friends friend_request_message'] = '<br/><span class="nonessential">' . esc_html( sprintf( __( 'Message: %s', 'friends' ), $message ) ) . '</span>'; | |
| 2438 | - } | |
| 2439 | - } | |
| 2440 | - | |
| 2441 | - if ( $user->has_cap( 'pending_friend_request' ) || $user->has_cap( 'subscription' ) ) { | |
| 2442 | - $link = wp_nonce_url( add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), self_admin_url( 'admin.php?page=edit-friend&user=' . $user->user_login ) ), 'add-friend-' . $user->user_login, 'add-friend' ); | |
| 2443 | - if ( $user->has_cap( 'pending_friend_request' ) ) { | |
| 2444 | - $actions['user_friend_request'] = '<a href="' . esc_url( $link ) . '">' . __( 'Resend Friend Request', 'friends' ) . '</a>'; | |
| 2445 | - } elseif ( $user->has_cap( 'subscription' ) ) { | |
| 2446 | - $actions['user_friend_request'] = '<a href="' . esc_url( $link ) . '">' . __( 'Send Friend Request', 'friends' ) . '</a>'; | |
| 2447 | - } | |
| 2448 | - } | |
| 2449 | - | |
| 2450 | - return $actions; | |
| 2451 | - } | |
| 2452 | - | |
| 2453 | - /** | |
| 2454 | - * Handle bulk friend request approvals on the user page | |
| 2455 | - * | |
| 2456 | - * @param string $sendback The URL to send the user back to. | |
| 2457 | - * @param string $action The requested action. | |
| 2458 | - * @param array $users The selected users. | |
| 2459 | - */ | |
| 2460 | - public function handle_bulk_friend_request_approval( $sendback, $action, $users ) { | |
| 2461 | - if ( 'accept_friend_request' !== $action ) { | |
| 2462 | - return $sendback; | |
| 2463 | - } | |
| 2464 | - | |
| 2465 | - $accepted = 0; | |
| 2466 | - foreach ( $users as $user_id ) { | |
| 2467 | - $user = new User( $user_id ); | |
| 2468 | - if ( ! $user || is_wp_error( $user ) ) { | |
| 2469 | - continue; | |
| 2470 | - } | |
| 2471 | - | |
| 2472 | - if ( ! $user->has_cap( 'friend_request' ) ) { | |
| 2473 | - continue; | |
| 2474 | - } | |
| 2475 | - | |
| 2476 | - if ( $user->has_cap( 'friend' ) ) { | |
| 2477 | - continue; | |
| 2478 | - } | |
| 2479 | - | |
| 2480 | - $user->set_role( get_option( 'friends_default_friend_role', 'friend' ) ); | |
| 2481 | - ++$accepted; | |
| 2482 | - } | |
| 2483 | - | |
| 2484 | - if ( ! $sendback ) { | |
| 2485 | - return array( | |
| 2486 | - 'accepted' => $accepted, | |
| 2487 | - ); | |
| 2488 | - } | |
| 2489 | - | |
| 2490 | - $sendback = add_query_arg( 'accepted', $accepted, $sendback ); | |
| 2491 | - $sendback = remove_query_arg( 'role', $sendback ); | |
| 2492 | - wp_safe_redirect( $sendback ); | |
| 2493 | - } | |
| 2494 | - | |
| 2495 | - /** | |
| 2496 | - * Add options to the Bulk dropdown on the users page | |
| 2497 | - * | |
| 2498 | - * @param array $actions The existing bulk options. | |
| 2499 | - * @return array The extended bulk options. | |
| 2500 | - */ | |
| 2501 | - public function add_user_bulk_options( $actions ) { | |
| 2502 | - $friends = User_Query::all_friend_requests(); | |
| 2503 | - $friends->get_results(); | |
| 2504 | - | |
| 2505 | - if ( ! empty( $friends ) ) { | |
| 2506 | - $actions['accept_friend_request'] = __( 'Accept Friend Request', 'friends' ); | |
| 2507 | - } | |
| 2508 | - | |
| 2509 | - $friends = User_Query::all_subscriptions(); | |
| 2510 | - $friends->get_results(); | |
| 2511 | - | |
| 2512 | - if ( ! empty( $friends ) ) { | |
| 2513 | - $actions['friend_request'] = __( 'Send Friend Request', 'friends' ); | |
| 2514 | - } | |
| 2515 | - | |
| 2516 | - return $actions; | |
| 2517 | - } | |
| 2518 | - | |
| 2519 | - /** | |
| 2520 | - * Add a column "Posts" (that emcompasses both user and friend posts.) | |
| 2521 | - * | |
| 2522 | - * @param array $columns The columns. | |
| 2523 | - * | |
| 2524 | - * @return array The columns extended by the friends_posts. | |
| 2525 | - */ | |
| 2526 | - public function user_list_columns( $columns ) { | |
| 2527 | - $columns['friends_posts'] = __( 'Friend Posts', 'friends' ); | |
| 2528 | - unset( $columns['email'] ); | |
| 2529 | - return $columns; | |
| 2530 | - } | |
| 2531 | - | |
| 2532 | - /** | |
| 2533 | - * Return the results for the friends_posts column. | |
| 2534 | - * | |
| 2535 | - * @param string $output Custom column output. Default empty. | |
| 2536 | - * @param string $column_name Column name. | |
| 2537 | - * @param int $user_id ID of the currently-listed user. | |
| 2538 | - * | |
| 2539 | - * @return string The column contents. | |
| 2540 | - */ | |
| 2541 | - public static function user_list_custom_column( $output, $column_name, $user_id ) { | |
| 2542 | - if ( 'friends_posts' !== $column_name ) { | |
| 2543 | - return $output; | |
| 2544 | - } | |
| 2545 | - $numposts = count_user_posts( $user_id, apply_filters( 'friends_frontend_post_types', array( 'post' ) ) ); | |
| 2546 | - $user = User::get_user_by_id( $user_id ); | |
| 2547 | - return sprintf( | |
| 2548 | - '<a href="%s" class="edit"><span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>', | |
| 2549 | - $user ? $user->get_local_friends_page_url() : "edit.php?author={$user_id}", | |
| 2550 | - $numposts, | |
| 2551 | - sprintf( | |
| 2552 | - /* translators: %s: Number of posts. */ | |
| 2553 | - _n( '%s post', '%s posts', $numposts ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 2554 | - number_format_i18n( $numposts ) | |
| 2555 | - ) | |
| 2556 | - ); | |
| 2557 | - } | |
| 2558 | - | |
| 2559 | - /** | |
| 2560 | 2895 | * Override the post title for specific post formats. |
| 2561 | 2896 | * |
| 2562 | 2897 | * @param string $title The title. |
| 2563 | 2898 | * @param int $post_id The post id. |
| @@ -2577,41 +2912,8 @@ | ||
| 2577 | 2912 | return $title; |
| 2578 | 2913 | } |
| 2579 | 2914 | |
| 2580 | 2915 | /** |
| 2581 | - * Adds the friend requests to the unread count. | |
| 2582 | - * | |
| 2583 | - * @param int $unread The unread count. | |
| 2584 | - * | |
| 2585 | - * @return int Unread count + friend requests. | |
| 2586 | - */ | |
| 2587 | - public function friends_unread_friend_request_count( $unread ) { | |
| 2588 | - $friend_requests = User_Query::all_friend_requests(); | |
| 2589 | - return $unread + $friend_requests->get_total(); | |
| 2590 | - } | |
| 2591 | - | |
| 2592 | - /** | |
| 2593 | - * Add open friend requests to the menu. | |
| 2594 | - * | |
| 2595 | - * @param \WP_Menu $wp_menu The wp menu. | |
| 2596 | - * @param string $my_url My url. | |
| 2597 | - */ | |
| 2598 | - public function friends_add_menu_open_friend_request( $wp_menu, $my_url ) { | |
| 2599 | - $friend_request_count = $this->friends_unread_friend_request_count( 0 ); | |
| 2600 | - if ( $friend_request_count > 0 ) { | |
| 2601 | - $wp_menu->add_menu( | |
| 2602 | - array( | |
| 2603 | - 'id' => 'open-friend-requests', | |
| 2604 | - 'parent' => 'friends-menu', | |
| 2605 | - // translators: %s is the number of open friend requests. | |
| 2606 | - 'title' => esc_html( sprintf( _n( 'Review %s Friend Request', 'Review %s Friends Request', $friend_request_count, 'friends' ), $friend_request_count ) ), | |
| 2607 | - 'href' => $my_url . '/wp-admin/admin.php?page=friends-list-requests', | |
| 2608 | - ) | |
| 2609 | - ); | |
| 2610 | - } | |
| 2611 | - } | |
| 2612 | - | |
| 2613 | - /** | |
| 2614 | 2916 | * Get the unread badge HTML |
| 2615 | 2917 | * |
| 2616 | 2918 | * @return string The unread badge HTML. |
| 2617 | 2919 | */ |
| @@ -2636,165 +2938,54 @@ | ||
| 2636 | 2938 | * |
| 2637 | 2939 | * @param \WP_Admin_Bar $wp_menu The admin bar to modify. |
| 2638 | 2940 | */ |
| 2639 | 2941 | public function admin_bar_friends_menu( \WP_Admin_Bar $wp_menu ) { |
| 2640 | - $my_url = false; | |
| 2641 | - $my_own_site = false; | |
| 2642 | - $on_my_own_site = false; | |
| 2643 | - $we_requested_friendship = false; | |
| 2644 | - $they_requested_friendship = false; | |
| 2942 | + if ( ! Friends::has_required_privileges() ) { | |
| 2943 | + return; | |
| 2944 | + } | |
| 2645 | 2945 | |
| 2646 | - if ( current_user_can( 'friend' ) ) { | |
| 2647 | - $current_user = wp_get_current_user(); | |
| 2648 | - if ( ! $current_user->user_url ) { | |
| 2649 | - return; | |
| 2650 | - } | |
| 2946 | + $my_url = home_url(); | |
| 2947 | + $my_admin_url = site_url(); | |
| 2651 | 2948 | |
| 2652 | - $my_url = $current_user->user_url; | |
| 2653 | - } elseif ( is_multisite() ) { | |
| 2654 | - $site = get_active_blog_for_user( get_current_user_id() ); | |
| 2655 | - if ( ! $site ) { | |
| 2656 | - // If we cannot find a site, we shouldn't show the admin bar entry. | |
| 2657 | - return; | |
| 2658 | - } | |
| 2949 | + $unread = $this->get_unread_badge(); | |
| 2659 | 2950 | |
| 2660 | - $my_url = set_url_scheme( $site->siteurl ); | |
| 2661 | - $my_own_site = $site; | |
| 2662 | - $on_my_own_site = get_current_blog_id() === intval( $site->blog_id ); | |
| 2663 | - if ( is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) { | |
| 2664 | - if ( current_user_can( 'pending_friend_request' ) ) { | |
| 2665 | - $they_requested_friendship = true; | |
| 2666 | - } elseif ( current_user_can( 'friend_request' ) ) { | |
| 2667 | - $we_requested_friendship = true; | |
| 2668 | - } | |
| 2669 | - } | |
| 2670 | - } elseif ( Friends::has_required_privileges() ) { | |
| 2671 | - $my_url = home_url(); | |
| 2672 | - $on_my_own_site = true; | |
| 2673 | - } | |
| 2674 | - | |
| 2675 | - if ( ! $on_my_own_site && $my_own_site ) { | |
| 2676 | - switch_to_blog( $my_own_site->blog_id ); | |
| 2677 | - } | |
| 2678 | - | |
| 2679 | - $unread = ''; | |
| 2680 | - if ( $on_my_own_site ) { | |
| 2681 | - $unread = $this->get_unread_badge(); | |
| 2682 | - } | |
| 2683 | 2951 | $wp_menu->add_node( |
| 2684 | 2952 | array( |
| 2685 | 2953 | 'id' => 'friends-menu', |
| 2686 | 2954 | 'parent' => '', |
| 2687 | - 'title' => '<span class="ab-icon dashicons dashicons-groups"></span> <span class="ab-label">' . esc_html( __( 'Friends', 'friends' ) ) . $unread . '</span>', | |
| 2955 | + 'title' => '<span class="ab-icon"></span> <span class="ab-label">' . esc_html( __( 'Friends', 'friends' ) ) . $unread . '</span>', | |
| 2688 | 2956 | 'href' => $my_url . '/friends/', |
| 2689 | 2957 | ) |
| 2690 | 2958 | ); |
| 2691 | 2959 | |
| 2692 | - if ( $on_my_own_site ) { | |
| 2693 | - do_action( 'friends_own_site_menu_top', $wp_menu, $my_url ); | |
| 2694 | - } | |
| 2960 | + do_action( 'friends_own_site_menu_top', $wp_menu, $my_url, $my_admin_url ); | |
| 2961 | + do_action( 'friends_current_site_menu_top', $wp_menu, $my_url, $my_admin_url ); | |
| 2695 | 2962 | |
| 2696 | - if ( ! $on_my_own_site && $my_own_site ) { | |
| 2697 | - restore_current_blog(); | |
| 2698 | - } | |
| 2699 | - | |
| 2700 | - do_action( 'friends_current_site_menu_top', $wp_menu, $my_url ); | |
| 2701 | - | |
| 2702 | 2963 | $wp_menu->add_menu( |
| 2703 | 2964 | array( |
| 2704 | 2965 | 'id' => 'your-feed', |
| 2705 | 2966 | 'parent' => 'friends-menu', |
| 2706 | - 'title' => esc_html__( 'My Friends Feed', 'friends' ), | |
| 2707 | - 'href' => $my_url . '/friends/', | |
| 2967 | + 'title' => esc_html__( 'Main Feed', 'friends' ), | |
| 2968 | + 'href' => home_url( '/friends/' ), | |
| 2708 | 2969 | ) |
| 2709 | 2970 | ); |
| 2710 | 2971 | |
| 2711 | - if ( $they_requested_friendship ) { | |
| 2712 | - $wp_menu->add_menu( | |
| 2713 | - array( | |
| 2714 | - 'id' => 'add-friend', | |
| 2715 | - 'parent' => 'friends-menu', | |
| 2716 | - 'title' => '<span style="border-left: 2px solid #d63638; padding-left: .5em">' . esc_html( | |
| 2717 | - sprintf( | |
| 2718 | - // translators: %s is a site title. | |
| 2719 | - __( "Respond to %s's friend request", 'friends' ), | |
| 2720 | - get_bloginfo( 'name' ) | |
| 2721 | - ) . '</span>' | |
| 2722 | - ), | |
| 2723 | - 'href' => $my_url . '/wp-admin/admin.php?page=friends-list-requests', | |
| 2724 | - ) | |
| 2725 | - ); | |
| 2726 | - } | |
| 2727 | - | |
| 2728 | - if ( $on_my_own_site ) { | |
| 2729 | - $wp_menu->add_menu( | |
| 2730 | - array( | |
| 2731 | - 'id' => 'your-profile', | |
| 2732 | - 'parent' => 'friends-menu', | |
| 2733 | - 'title' => esc_html__( 'My Public Friends Profile', 'friends' ), | |
| 2734 | - 'href' => $my_url . '/friends/?public', | |
| 2735 | - ) | |
| 2736 | - ); | |
| 2737 | - $wp_menu->add_menu( | |
| 2738 | - array( | |
| 2739 | - 'id' => 'friends-requests', | |
| 2740 | - 'parent' => 'friends-menu', | |
| 2741 | - 'title' => esc_html__( 'My Friends & Requests', 'friends' ), | |
| 2742 | - 'href' => $my_url . '/wp-admin/admin.php?page=friends-list', | |
| 2743 | - ) | |
| 2744 | - ); | |
| 2745 | - $wp_menu->add_menu( | |
| 2746 | - array( | |
| 2747 | - 'id' => 'friends', | |
| 2748 | - 'parent' => 'friends-menu', | |
| 2749 | - 'title' => esc_html__( 'Settings' ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 2750 | - 'href' => $my_url . '/wp-admin/admin.php?page=friends-settings', | |
| 2751 | - ) | |
| 2752 | - ); | |
| 2753 | - } else { | |
| 2754 | - if ( ! current_user_can( 'friend' ) ) { | |
| 2755 | - if ( $we_requested_friendship ) { | |
| 2756 | - $wp_menu->add_menu( | |
| 2757 | - array( | |
| 2758 | - 'id' => 'add-friend', | |
| 2759 | - 'parent' => 'friends-menu', | |
| 2760 | - 'title' => esc_html__( 'Friendship Already Requested', 'friends' ), | |
| 2761 | - 'href' => $my_url . '/wp-admin/' . self::get_users_url(), | |
| 2762 | - ) | |
| 2763 | - ); | |
| 2764 | - } elseif ( ! $they_requested_friendship ) { | |
| 2765 | - $wp_menu->add_menu( | |
| 2766 | - array( | |
| 2767 | - 'id' => 'add-friend', | |
| 2768 | - 'parent' => 'friends-menu', | |
| 2769 | - 'title' => esc_html( | |
| 2770 | - sprintf( | |
| 2771 | - // translators: %s is a site title. | |
| 2772 | - __( 'Add %s as a friend', 'friends' ), | |
| 2773 | - get_bloginfo( 'name' ) | |
| 2774 | - ) | |
| 2775 | - ), | |
| 2776 | - 'href' => $my_url . '/?add-friend=' . urlencode( home_url() ), | |
| 2777 | - ) | |
| 2778 | - ); | |
| 2779 | - } | |
| 2780 | - } | |
| 2781 | - | |
| 2782 | - $wp_menu->add_menu( | |
| 2783 | - array( | |
| 2784 | - 'id' => 'profile', | |
| 2785 | - 'parent' => 'friends-menu', | |
| 2786 | - 'title' => esc_html( | |
| 2787 | - sprintf( | |
| 2788 | - // translators: %s is a site title. | |
| 2789 | - __( "%s's Profile", 'friends' ), | |
| 2790 | - get_bloginfo( 'name' ) | |
| 2791 | - ) | |
| 2792 | - ), | |
| 2793 | - 'href' => home_url( '/friends/' ), | |
| 2794 | - ) | |
| 2795 | - ); | |
| 2796 | - } | |
| 2972 | + $wp_menu->add_menu( | |
| 2973 | + array( | |
| 2974 | + 'id' => 'add-friend', | |
| 2975 | + 'parent' => 'friends-menu', | |
| 2976 | + 'title' => esc_html__( 'Add a friend', 'friends' ), | |
| 2977 | + 'href' => home_url( '/friends/add-friend' ), | |
| 2978 | + ) | |
| 2979 | + ); | |
| 2980 | + $wp_menu->add_menu( | |
| 2981 | + array( | |
| 2982 | + 'id' => 'friends', | |
| 2983 | + 'parent' => 'friends-menu', | |
| 2984 | + 'title' => esc_html__( 'Settings' ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 2985 | + 'href' => home_url( '/friends/settings/' ), | |
| 2986 | + ) | |
| 2987 | + ); | |
| 2797 | 2988 | } |
| 2798 | 2989 | |
| 2799 | 2990 | /** |
| 2800 | 2991 | * Add Friend entries to the New Content admin section |
| @@ -2828,17 +3019,37 @@ | ||
| 2828 | 3019 | public function admin_bar_mobile() { |
| 2829 | 3020 | if ( ! is_user_logged_in() ) { |
| 2830 | 3021 | return; |
| 2831 | 3022 | } |
| 3023 | + $logo_mask = "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='-10 53 154 187'%3E%3Cpath d='M 132.29 90.93 C 119.28 54.95 70.12 63.99 38.89 88.85 -7.9 126.11 11.81 177.74 25.75 200.93 40.32 225.15 60.67 237.5 74.87 225.14 83.57 217.57 86.99 209.19 77.64 194.01 74.25 188.51 76.44 170.04 85.94 165.64 94.55 161.65 94.95 149.38 83.17 149.73 75.25 149.97 53.78 148.25 61.03 144.89 67.56 141.86 143.08 120.75 132.29 90.93 Z'/%3E%3C/svg%3E\") center/contain no-repeat"; | |
| 2832 | 3024 | ?> |
| 2833 | 3025 | <style type="text/css" media="screen"> |
| 3026 | + #wpadminbar #wp-admin-bar-friends-menu .ab-icon:before { | |
| 3027 | + content: ""; | |
| 3028 | + float: left; | |
| 3029 | + width: 20px; | |
| 3030 | + height: 20px; | |
| 3031 | + margin-top: 2px; | |
| 3032 | + background-color: currentColor; | |
| 3033 | + -webkit-mask: <?php echo $logo_mask; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>; | |
| 3034 | + mask: <?php echo $logo_mask; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>; | |
| 3035 | + } | |
| 2834 | 3036 | @media screen and (max-width: 782px) { |
| 2835 | - #wpadminbar #wp-admin-bar-friends, #wpadminbar #wp-admin-bar-friends .ab-icon { | |
| 3037 | + #wpadminbar #wp-admin-bar-friends-menu, #wpadminbar #wp-admin-bar-friends-menu .ab-icon { | |
| 2836 | 3038 | display: block !important; |
| 2837 | 3039 | } |
| 2838 | - #wpadminbar #wp-admin-bar-friends .ab-label { | |
| 3040 | + #wpadminbar #wp-admin-bar-friends-menu .ab-label { | |
| 2839 | 3041 | display: none !important; |
| 2840 | 3042 | } |
| 3043 | + #wpadminbar #wp-admin-bar-friends-menu .ab-icon:before { | |
| 3044 | + width: 32px; | |
| 3045 | + height: 32px; | |
| 3046 | + margin-top: 6px; | |
| 3047 | + margin-left: 6px; | |
| 3048 | + } | |
| 3049 | + body.friends-page #wpadminbar li#wp-admin-bar-comments { | |
| 3050 | + display: none; | |
| 3051 | + } | |
| 2841 | 3052 | } |
| 2842 | 3053 | </style> |
| 2843 | 3054 | <?php |
| 2844 | 3055 | } |
| @@ -2853,14 +3064,9 @@ | ||
| 2853 | 3064 | public function delete_user_form( $current_user, $userids ) { |
| 2854 | 3065 | $only_friends_affiliated = true; |
| 2855 | 3066 | foreach ( $userids as $user_id ) { |
| 2856 | 3067 | $user = new \WP_User( $user_id ); |
| 2857 | - if ( | |
| 2858 | - ! $user->has_cap( 'friend_request' ) && | |
| 2859 | - ! $user->has_cap( 'pending_friend_request' ) && | |
| 2860 | - ! $user->has_cap( 'friend' ) && | |
| 2861 | - ! $user->has_cap( 'subscription' ) | |
| 2862 | - ) { | |
| 3068 | + if ( ! $user->has_cap( 'subscription' ) ) { | |
| 2863 | 3069 | $only_friends_affiliated = false; |
| 2864 | 3070 | break; |
| 2865 | 3071 | } |
| 2866 | 3072 | } |
| @@ -2912,16 +3118,18 @@ | ||
| 2912 | 3118 | <h3><?php esc_html_e( 'Bookmarklets', 'friends' ); ?></h3> |
| 2913 | 3119 | |
| 2914 | 3120 | <p><?php esc_html_e( "Drag one of these bookmarklets to your bookmarks bar and click it when you're on a site around the web for the appropriate action.", 'friends' ); ?></p> |
| 2915 | 3121 | <p> |
| 2916 | - <a href="javascript:void(location.href='<?php echo esc_attr( self_admin_url( 'admin.php?page=add-friend&url=' ) ); ?>'+encodeURIComponent(location.href))" style="display: inline-block; padding: .5em; border: 1px solid #999; border-radius: 4px; background-color: #ddd;text-decoration: none; margin-right: 3em"><?php echo esc_html_e( 'Add friend', 'friends' ); ?></a> | |
| 2917 | - <a href="javascript:void(location.href='<?php echo esc_attr( self_admin_url( 'admin.php?page=add-friend&url=' ) ); ?>'+encodeURIComponent(location.href))" style="display: inline-block; padding: .5em; border: 1px solid #999; border-radius: 4px; background-color: #ddd; text-decoration: none; margin-right: 3em"><?php echo esc_html_e( 'Subscribe', 'friends' ); ?></a> | |
| 3122 | + <a href="javascript:void(location.href='<?php echo esc_attr( self_admin_url( 'admin.php?page=add-friend&url=' ) ); ?>'+encodeURIComponent(location.href))" style="display: inline-block; padding: .5em; border: 1px solid #999; border-radius: 4px; background-color: #ddd;text-decoration: none; margin-right: 3em"><?php esc_html_e( 'Add friend', 'friends' ); ?></a> | |
| 3123 | + <a href="javascript:void(location.href='<?php echo esc_attr( self_admin_url( 'admin.php?page=add-friend&url=' ) ); ?>'+encodeURIComponent(location.href))" style="display: inline-block; padding: .5em; border: 1px solid #999; border-radius: 4px; background-color: #ddd; text-decoration: none; margin-right: 3em"><?php esc_html_e( 'Subscribe', 'friends' ); ?></a> | |
| 2918 | 3124 | </p> |
| 2919 | 3125 | <h3><?php esc_html_e( 'Browser Extension', 'friends' ); ?></h3> |
| 2920 | 3126 | |
| 2921 | - <p><?php esc_html_e( 'There is also the option to use a browser extension.', 'friends' ); ?></p> | |
| 3127 | + <p><?php esc_html_e( 'For a smoother experience, install the Friends browser extension. It adds a toolbar button to subscribe to the current site with one click, plus quick actions provided by other Friends-aware plugins.', 'friends' ); ?></p> | |
| 2922 | 3128 | <p> |
| 2923 | - <a href="https://addons.mozilla.org/en-US/firefox/addon/wpfriends/"><?php echo esc_html_e( 'Firefox Extension', 'friends' ); ?></a> | |
| 3129 | + <a href="https://chromewebstore.google.com/detail/friends/ledbghpaplkpclndlommpbokndieflhl"><?php esc_html_e( 'Chrome Extension', 'friends' ); ?></a> | |
| 3130 | + · | |
| 3131 | + <a href="https://addons.mozilla.org/en-US/firefox/addon/wpfriends/"><?php esc_html_e( 'Firefox Extension', 'friends' ); ?></a> | |
| 2924 | 3132 | </p> |
| 2925 | 3133 | </div> |
| 2926 | 3134 | <?php |
| 2927 | 3135 | } |
| @@ -2932,33 +3140,12 @@ | ||
| 2932 | 3140 | * @param array $items Items inserted by another plugin. |
| 2933 | 3141 | * @return array Items + our items. |
| 2934 | 3142 | */ |
| 2935 | 3143 | public function dashboard_glance_items( $items ) { |
| 2936 | - $count_users = count_users(); | |
| 2937 | - $count = array_merge( | |
| 2938 | - array( | |
| 2939 | - 'friend' => 0, | |
| 2940 | - 'acquaintance' => 0, | |
| 2941 | - 'friend_request' => 0, | |
| 2942 | - 'subscription' => 0, | |
| 2943 | - ), | |
| 2944 | - $count_users['avail_roles'] | |
| 2945 | - ); | |
| 2946 | - $friend_count = $count['friend'] + $count['acquaintance']; | |
| 2947 | - $friend_request_count = $count['friend_request']; | |
| 2948 | - $subscription_count = $count['subscription']; | |
| 3144 | + $subscription_count = User_Query::all_subscriptions()->get_total(); | |
| 2949 | 3145 | $friend_post_count = wp_count_posts( Friends::CPT ); |
| 2950 | 3146 | $friend_post_count = $friend_post_count->publish + $friend_post_count->private; |
| 2951 | 3147 | |
| 2952 | - $items[] = '<a class="friends" href="' . self_admin_url( 'users.php?role=friend' ) . '">' . sprintf( | |
| 2953 | - // translators: %s is the number of your friends. | |
| 2954 | - _n( '%s Friend', '%s Friends', $friend_count, 'friends' ), | |
| 2955 | - $friend_count | |
| 2956 | - ) . '</a>'; | |
| 2957 | - if ( $friend_request_count ) { | |
| 2958 | - // translators: %s is the number of friend requests. | |
| 2959 | - $items[] = '<a class="friend-requests" href="' . self_admin_url( 'users.php?role=friend_request' ) . '">' . sprintf( _n( '%s Friend Request', '%s Friend Requests', $friend_request_count, 'friends' ), $friend_request_count ) . '</a>'; | |
| 2960 | - } | |
| 2961 | 3148 | if ( $subscription_count ) { |
| 2962 | 3149 | // translators: %s is the number of subscriptions. |
| 2963 | 3150 | $items[] = '<a class="subscriptions" href="' . self_admin_url( 'users.php?role=subscription' ) . '">' . sprintf( _n( '%s Subscription', '%s Subscriptions', $subscription_count, 'friends' ), $subscription_count ) . '</a>'; |
| 2964 | 3151 | } |
| @@ -2964,82 +3151,212 @@ | ||
| 2964 | 3151 | } |
| 2965 | 3152 | |
| 2966 | 3153 | if ( $friend_post_count ) { |
| 2967 | 3154 | // translators: %s is the number of friend posts. |
| 2968 | - $items[] = '<a class="friend-posts" href="' . home_url( '/friends/' ) . '">' . sprintf( _n( '%s Post by Friends', '%s Posts by Friends', $friend_post_count, 'friends' ), $friend_post_count ) . '</a>'; | |
| 3155 | + $items[] = '<a class="friend-posts" href="' . home_url( '/friends/' ) . '">' . sprintf( _n( '%s Post by Friends', '%s Posts by Friends', $friend_post_count, 'friends' ), number_format_i18n( $friend_post_count ) ) . '</a>'; | |
| 2969 | 3156 | } |
| 2970 | 3157 | return $items; |
| 2971 | 3158 | } |
| 2972 | 3159 | |
| 2973 | - public function site_status_tests( $tests ) { | |
| 2974 | - $tests['direct']['friends-roles'] = array( | |
| 2975 | - 'label' => __( 'Friend roles were created', 'friends' ), | |
| 2976 | - 'test' => array( $this, 'friend_roles_test' ), | |
| 2977 | - ); | |
| 2978 | - return $tests; | |
| 3160 | + public function add_dashboard_widgets() { | |
| 3161 | + if ( ! Friends::has_required_privileges() ) { | |
| 3162 | + return; | |
| 3163 | + } | |
| 3164 | + $user_id = get_current_user_id(); | |
| 3165 | + $widgets = get_user_option( 'friends_dashboard_widgets', $user_id ); | |
| 3166 | + if ( ! $widgets ) { | |
| 3167 | + $widgets = array( array() ); | |
| 3168 | + update_user_option( $user_id, 'friends_dashboard_widgets', $widgets ); | |
| 3169 | + } | |
| 3170 | + foreach ( $widgets as $i => $widget ) { | |
| 3171 | + if ( ! is_array( $widget ) ) { | |
| 3172 | + continue; | |
| 3173 | + } | |
| 3174 | + $title = __( 'Latest Posts', 'friends' ); | |
| 3175 | + if ( isset( $widget['format'] ) ) { | |
| 3176 | + $title = get_post_format_string( sanitize_key( $widget['format'] ) ); | |
| 3177 | + } | |
| 3178 | + | |
| 3179 | + if ( ! empty( $widget['friend'] ) ) { | |
| 3180 | + $user = User::get_by_username( $widget['friend'] ); | |
| 3181 | + $title = ' by ' . $user->display_name; | |
| 3182 | + } | |
| 3183 | + $title = sprintf( | |
| 3184 | + // translators: %s is an author name or "Latest Posts". | |
| 3185 | + __( 'Friends: %s', 'friends' ), | |
| 3186 | + $title | |
| 3187 | + ); | |
| 3188 | + wp_add_dashboard_widget( 'friends_dashboard_widget' . $i, $title, array( $this, 'render_dashboard_widget' ), array( $this, 'render_dashboard_widget_controls' ), $widget, 'side', 'high' ); | |
| 3189 | + } | |
| 2979 | 3190 | } |
| 2980 | 3191 | |
| 2981 | - public function get_missing_friends_plugin_roles() { | |
| 2982 | - $missing = Friends::get_friends_plugin_roles(); | |
| 2983 | - $roles = new \WP_Roles(); | |
| 2984 | - foreach ( $roles->roles as $role => $data ) { | |
| 2985 | - if ( isset( $data['capabilities']['friends_plugin'] ) ) { | |
| 2986 | - foreach ( $missing as $k => $cap ) { | |
| 2987 | - if ( isset( $data['capabilities'][ $cap ] ) ) { | |
| 2988 | - unset( $missing[ $k ] ); | |
| 2989 | - break; | |
| 2990 | - } | |
| 2991 | - } | |
| 3192 | + public function add_new_dashboard_widget( $friend = null, $format = null ) { | |
| 3193 | + $user_id = get_current_user_id(); | |
| 3194 | + $widgets = get_user_option( 'friends_dashboard_widgets', $user_id ); | |
| 3195 | + if ( ! $widgets ) { | |
| 3196 | + $widgets = array(); | |
| 3197 | + } | |
| 3198 | + $widget = array(); | |
| 3199 | + if ( $friend ) { | |
| 3200 | + $widget['friend'] = $friend; | |
| 3201 | + } | |
| 3202 | + if ( $format ) { | |
| 3203 | + $widget['format'] = $format; | |
| 3204 | + } | |
| 3205 | + $widgets[] = $widget; | |
| 3206 | + update_user_option( $user_id, 'friends_dashboard_widgets', $widgets ); | |
| 3207 | + } | |
| 3208 | + | |
| 3209 | + public function render_dashboard_widget_controls( $id, $widget = false ) { | |
| 3210 | + if ( empty( $id ) && $widget ) { | |
| 3211 | + $id = intval( str_replace( 'friends_dashboard_widget', '', $widget['id'] ) ); | |
| 3212 | + } | |
| 3213 | + $user_id = get_current_user_id(); | |
| 3214 | + $widgets = get_user_option( 'friends_dashboard_widgets', $user_id ); | |
| 3215 | + if ( ! $widgets ) { | |
| 3216 | + $widgets = array( array() ); | |
| 3217 | + } | |
| 3218 | + | |
| 3219 | + // phpcs:disable WordPress.Security.NonceVerification | |
| 3220 | + if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['widget_id'] ) ) { | |
| 3221 | + | |
| 3222 | + $id = intval( str_replace( 'friends_dashboard_widget', '', sanitize_text_field( wp_unslash( $_POST['widget_id'] ) ) ) ); | |
| 3223 | + if ( isset( $_POST['add-new'] ) ) { | |
| 3224 | + $id = count( $widgets ); | |
| 3225 | + $widgets[ $id ] = array(); | |
| 2992 | 3226 | } |
| 3227 | + if ( ! empty( $_POST['friend'] ) ) { | |
| 3228 | + $widgets[ $id ]['friend'] = sanitize_text_field( wp_unslash( $_POST['friend'] ) ); | |
| 3229 | + } else { | |
| 3230 | + unset( $widgets[ $id ]['friend'] ); | |
| 3231 | + } | |
| 3232 | + if ( ! empty( $_POST['format'] ) ) { | |
| 3233 | + $widgets[ $id ]['format'] = sanitize_text_field( wp_unslash( $_POST['format'] ) ); | |
| 3234 | + } else { | |
| 3235 | + unset( $widgets[ $id ]['format'] ); | |
| 3236 | + } | |
| 3237 | + if ( isset( $_POST['delete'] ) ) { | |
| 3238 | + unset( $widgets[ $id ] ); | |
| 3239 | + } | |
| 3240 | + | |
| 3241 | + update_user_option( $user_id, 'friends_dashboard_widgets', $widgets ); | |
| 2993 | 3242 | } |
| 3243 | + // phpcs:enable WordPress.Security.NonceVerification | |
| 3244 | + $args = array(); | |
| 3245 | + if ( isset( $widgets[ $id ] ) ) { | |
| 3246 | + $args = $widgets[ $id ]; | |
| 3247 | + } | |
| 3248 | + echo '<p>'; | |
| 3249 | + echo '<label>'; | |
| 3250 | + esc_html_e( 'Friend:', 'friends' ); | |
| 3251 | + echo '<select name="friend">'; | |
| 3252 | + echo '<option value="">' . esc_html__( 'Any Friend', 'friends' ) . '</option>'; | |
| 3253 | + $users = User_Query::all_associated_users(); | |
| 3254 | + foreach ( $users->get_results() as $user ) { | |
| 3255 | + echo '<option value="' . esc_attr( $user->user_login ) . '"'; | |
| 3256 | + if ( isset( $args['friend'] ) && $args['friend'] === $user->user_login ) { | |
| 3257 | + echo ' selected="selected"'; | |
| 3258 | + } | |
| 3259 | + echo '>' . esc_html( $user->display_name ) . ' (' . esc_html( $user->user_login ) . ')</option>'; | |
| 3260 | + } | |
| 3261 | + echo '</select>'; | |
| 3262 | + echo '</label>'; | |
| 3263 | + echo '</p>'; | |
| 3264 | + echo '<p>'; | |
| 3265 | + echo '<label>'; | |
| 3266 | + esc_html_e( 'Post Format:', 'friends' ); | |
| 3267 | + echo '<select name="format">'; | |
| 3268 | + echo '<option value="">' . esc_html__( 'Any Post Format', 'friends' ) . '</option>'; | |
| 3269 | + foreach ( get_post_format_strings() as $format => $label ) { | |
| 3270 | + echo '<option value="' . esc_attr( $format ) . '"'; | |
| 3271 | + if ( isset( $args['format'] ) && $args['format'] === $format ) { | |
| 3272 | + echo ' selected="selected"'; | |
| 3273 | + } | |
| 3274 | + echo '>' . esc_html( $label ) . '</option>'; | |
| 3275 | + } | |
| 3276 | + echo '</select>'; | |
| 3277 | + echo '</label>'; | |
| 3278 | + echo '</p>'; | |
| 3279 | + echo '<p>'; | |
| 3280 | + echo ' <button name="add-new" class="button button-secondary">' . esc_html__( 'Save as a new widget', 'friends' ) . '</button>'; | |
| 3281 | + echo ' <button name="delete" class="button">' . esc_html__( 'Delete this widget', 'friends' ) . '</button>'; | |
| 3282 | + echo '</p>'; | |
| 3283 | + } | |
| 2994 | 3284 | |
| 2995 | - return array_values( $missing ); | |
| 3285 | + public function render_dashboard_widget( $args, $widget ) { | |
| 3286 | + $args = $widget['args']; | |
| 3287 | + echo '<div class="friends-dashboard-widget" data-nonce="'; | |
| 3288 | + echo esc_attr( wp_create_nonce( 'friends-dashboard' ) ); | |
| 3289 | + echo '"'; | |
| 3290 | + if ( ! empty( $args['friend'] ) ) { | |
| 3291 | + echo ' data-friend="' . esc_attr( $args['friend'] ) . '"'; | |
| 3292 | + } | |
| 3293 | + if ( ! empty( $args['format'] ) ) { | |
| 3294 | + echo ' data-format="' . esc_attr( $args['format'] ) . '"'; | |
| 3295 | + } | |
| 3296 | + echo '></div>'; | |
| 2996 | 3297 | } |
| 2997 | 3298 | |
| 2998 | - public function friend_roles_test() { | |
| 2999 | - $result = array( | |
| 3000 | - 'label' => __( 'The friend roles have been installed', 'friends' ), | |
| 3001 | - 'status' => 'good', | |
| 3002 | - 'badge' => array( | |
| 3003 | - 'label' => __( 'Friends', 'friends' ), | |
| 3004 | - 'color' => 'green', | |
| 3005 | - ), | |
| 3006 | - 'description' => | |
| 3007 | - '<p>' . | |
| 3008 | - __( 'The Friends Plugin uses users and user roles to determine friendship status between sites.', 'friends' ) . | |
| 3009 | - '</p>' . | |
| 3010 | - '<p>' . | |
| 3011 | - sprintf( | |
| 3012 | - // translators: %s is a list of roles. | |
| 3013 | - __( 'These are the roles required for the friends plugin: %s', 'friends' ), | |
| 3014 | - implode( ', ', Friends::get_friends_plugin_roles() ) | |
| 3015 | - ) . | |
| 3016 | - '</p>', | |
| 3017 | - 'test' => 'friends-roles', | |
| 3018 | - ); | |
| 3299 | + public function ajax_friends_dashboard() { | |
| 3300 | + check_ajax_referer( 'friends-dashboard' ); | |
| 3019 | 3301 | |
| 3020 | - $missing_friend_roles = $this->get_missing_friends_plugin_roles(); | |
| 3021 | - if ( ! empty( $missing_friend_roles ) ) { | |
| 3302 | + $query_args = array(); | |
| 3303 | + $args = array(); | |
| 3022 | 3304 | |
| 3023 | - $result['label'] = sprintf( | |
| 3024 | - // translators: %s is a list of missing roles. | |
| 3025 | - __( 'Not all friend roles have been installed. Missing: %s', 'friends' ), | |
| 3026 | - implode( ', ', $missing_friend_roles ) | |
| 3305 | + if ( isset( $_POST['friend'] ) ) { | |
| 3306 | + $friend = User::get_by_username( sanitize_text_field( wp_unslash( $_POST['friend'] ) ) ); | |
| 3307 | + if ( $friend ) { | |
| 3308 | + $args['friend_user'] = $friend; | |
| 3309 | + $query_args = $friend->modify_get_posts_args_by_author( $query_args ); | |
| 3310 | + } | |
| 3311 | + } | |
| 3312 | + | |
| 3313 | + if ( isset( $_POST['format'] ) ) { | |
| 3314 | + $post_formats = get_post_format_slugs(); | |
| 3315 | + $format = sanitize_text_field( wp_unslash( $_POST['format'] ) ); | |
| 3316 | + | |
| 3317 | + if ( isset( $post_formats[ $format ] ) ) { | |
| 3318 | + $args['post_format'] = $format; | |
| 3319 | + if ( 'standard' !== $format ) { | |
| 3320 | + $query_args['tax_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query | |
| 3321 | + array( | |
| 3322 | + 'taxonomy' => 'post_format', | |
| 3323 | + 'field' => 'slug', | |
| 3324 | + 'terms' => array( 'post-format-' . $format ), | |
| 3325 | + ), | |
| 3326 | + ); | |
| 3327 | + } else { | |
| 3328 | + $query_args['tax_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query | |
| 3329 | + array( | |
| 3330 | + 'taxonomy' => 'post_format', | |
| 3331 | + 'operator' => 'NOT EXISTS', | |
| 3332 | + ), | |
| 3333 | + ); | |
| 3334 | + } | |
| 3335 | + } | |
| 3336 | + } | |
| 3337 | + | |
| 3338 | + $any_friends = User_Query::all_associated_users(); | |
| 3339 | + | |
| 3340 | + ob_start(); | |
| 3341 | + if ( 0 === $any_friends->get_total() && empty( $query_args ) ) { | |
| 3342 | + Friends::template_loader()->get_template_part( | |
| 3343 | + 'admin/dashboard-widget-welcome', | |
| 3344 | + null, | |
| 3345 | + array() | |
| 3027 | 3346 | ); |
| 3028 | - $result['badge']['color'] = 'red'; | |
| 3029 | - $result['status'] = 'critical'; | |
| 3030 | - $result['description'] .= '<p>'; | |
| 3031 | - $result['description'] .= wp_kses_post( | |
| 3032 | - sprintf( | |
| 3033 | - // translators: %s is a URL. | |
| 3034 | - __( '<strong>To fix this:</strong> <a href="%s">Re-run activation of the Friends plugin</a>.', 'friends' ), | |
| 3035 | - esc_url( wp_nonce_url( add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), self_admin_url( 'admin.php?page=friends-settings&rerun-activate' ) ), 'friends-settings' ) ) | |
| 3036 | - ) | |
| 3037 | - ); | |
| 3038 | - $result['description'] .= '</p>'; | |
| 3347 | + | |
| 3348 | + } else { | |
| 3349 | + $query_args['post_type'] = apply_filters( 'friends_frontend_post_types', array( 'post' ) ); | |
| 3350 | + $args['posts'] = get_posts( $query_args ); | |
| 3351 | + Friends::template_loader()->get_template_part( 'admin/dashboard-widget', null, $args ); | |
| 3039 | 3352 | } |
| 3353 | + $data = ob_get_contents(); | |
| 3354 | + ob_end_clean(); | |
| 3040 | 3355 | |
| 3041 | - return $result; | |
| 3356 | + wp_send_json_success( | |
| 3357 | + $data | |
| 3358 | + ); | |
| 3042 | 3359 | } |
| 3043 | 3360 | |
| 3044 | 3361 | public function site_status_test_php_modules( $modules ) { |
| 3045 | 3362 | $modules['mbstring']['required'] = true; |
| @@ -3045,61 +3362,70 @@ | ||
| 3045 | 3362 | $modules['mbstring']['required'] = true; |
| 3046 | 3363 | return $modules; |
| 3047 | 3364 | } |
| 3048 | 3365 | |
| 3049 | - public function site_health_debug( $debug_info ) { | |
| 3050 | - $missing_friend_roles = $this->get_missing_friends_plugin_roles(); | |
| 3051 | - $debug_info['friends'] = array( | |
| 3052 | - 'label' => __( 'Friends', 'friends' ), | |
| 3053 | - 'fields' => array( | |
| 3054 | - 'version' => array( | |
| 3055 | - 'label' => __( 'Friends Version', 'friends' ), | |
| 3056 | - 'value' => Friends::VERSION, | |
| 3057 | - ), | |
| 3058 | - 'mbstring' => array( | |
| 3059 | - 'label' => __( 'mbstring is available', 'friends' ), | |
| 3060 | - 'value' => function_exists( 'mb_check_encoding' ) ? __( 'Yes' ) : __( 'No' ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 3061 | - ), | |
| 3062 | - 'roles' => array( | |
| 3063 | - 'label' => __( 'Friend roles missing', 'friends' ), | |
| 3064 | - 'value' => empty( $missing_friend_roles ) ? sprintf( | |
| 3065 | - // translators: %s is a list of roles. | |
| 3066 | - __( 'All roles found: %s', 'friends' ), | |
| 3067 | - implode( ', ', Friends::get_friends_plugin_roles() ) | |
| 3068 | - ) : implode( ', ', $missing_friend_roles ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain | |
| 3069 | - ), | |
| 3070 | - 'main_user' => array( | |
| 3071 | - 'label' => __( 'Main Friend User', 'friends' ), | |
| 3072 | - 'value' => self::human_readable_main_user(), | |
| 3073 | - ), | |
| 3074 | - 'parsers' => array( | |
| 3075 | - 'label' => __( 'Registered Parsers', 'friends' ), | |
| 3076 | - 'value' => strip_tags( implode( ', ', $this->friends->feed->get_registered_parsers() ) ), | |
| 3077 | - ), | |
| 3078 | - ), | |
| 3079 | - ); | |
| 3366 | + public function admin_friend_posts_query( $query ) { | |
| 3367 | + global $wp_query, $wp, $authordata; | |
| 3368 | + if ( $wp_query !== $query || ! is_admin() ) { | |
| 3369 | + return $query; | |
| 3370 | + } | |
| 3371 | + if ( ! isset( $query->query['post_type'] ) || ! in_array( $query->query['post_type'], apply_filters( 'friends_frontend_post_types', array( 'post' ) ), true ) ) { | |
| 3372 | + return $query; | |
| 3373 | + } | |
| 3080 | 3374 | |
| 3081 | - return $debug_info; | |
| 3375 | + if ( empty( $query->query['author'] ) ) { | |
| 3376 | + return $query; | |
| 3377 | + } | |
| 3378 | + | |
| 3379 | + $author = User::get_user_by_id( $query->query['author'] ); | |
| 3380 | + if ( ! $author ) { | |
| 3381 | + return $query; | |
| 3382 | + } | |
| 3383 | + $query->query_vars['author'] = ''; | |
| 3384 | + $query = $author->modify_query_by_author( $query ); | |
| 3385 | + | |
| 3386 | + return $query; | |
| 3082 | 3387 | } |
| 3083 | 3388 | |
| 3084 | 3389 | /** |
| 3085 | - * Returns a human readable string for which user is the main user. | |
| 3390 | + * Render an "ActivityPub plugin not active" notice for activitypub-parser feeds | |
| 3391 | + * when the ActivityPub plugin is not loaded (so Feed_Parser_ActivityPub never fires). | |
| 3086 | 3392 | * |
| 3087 | - * @return string | |
| 3393 | + * @param User_Feed $feed The feed. | |
| 3394 | + * @param int $term_id The term ID. | |
| 3395 | + * @param string $parser The parser slug. | |
| 3088 | 3396 | */ |
| 3089 | - private static function human_readable_main_user() { | |
| 3090 | - $main_user = Friends::get_main_friend_user_id(); | |
| 3091 | - | |
| 3092 | - if ( ! $main_user ) { | |
| 3093 | - // translators: %d is the number of users. | |
| 3094 | - return esc_html( sprintf( __( 'No main user set. Admin users: %d', 'friends' ), User_Query::all_admin_users()->get_total() ) ); | |
| 3397 | + public function maybe_render_activitypub_inactive_notice( $feed, $term_id, $parser ) { | |
| 3398 | + if ( 'activitypub' !== $parser ) { | |
| 3399 | + return; | |
| 3095 | 3400 | } |
| 3096 | 3401 | |
| 3097 | - $user = new \WP_User( $main_user ); | |
| 3098 | - | |
| 3099 | - if ( ! $user ) { | |
| 3100 | - return sprintf( '#%1$d %2$s', $main_user, '???' ); | |
| 3402 | + if ( class_exists( '\Activitypub\Activitypub' ) ) { | |
| 3403 | + return; | |
| 3101 | 3404 | } |
| 3102 | - | |
| 3103 | - return sprintf( '#%1$d %2$s', $user->ID, $user->user_login ); | |
| 3405 | + ?> | |
| 3406 | + <div class="activitypub-subscription-check"> | |
| 3407 | + <div class="ap-section-header"><?php esc_html_e( 'ActivityPub Plugin', 'friends' ); ?></div> | |
| 3408 | + <div class="ap-data-grid"> | |
| 3409 | + <span class="ap-data-label"><?php esc_html_e( 'Status', 'friends' ); ?></span> | |
| 3410 | + <span class="ap-data-value"><em style="color: orange;"><?php esc_html_e( 'not active', 'friends' ); ?></em></span> | |
| 3411 | + </div> | |
| 3412 | + <div class="ap-section-footer"> | |
| 3413 | + <?php | |
| 3414 | + if ( current_user_can( 'activate_plugins' ) ) { | |
| 3415 | + echo wp_kses( | |
| 3416 | + sprintf( | |
| 3417 | + /* translators: %s is a link to the plugin search page */ | |
| 3418 | + __( 'The <a href="%s">ActivityPub plugin</a> is required to receive posts from this feed.', 'friends' ), | |
| 3419 | + esc_url( admin_url( 'plugin-install.php?s=activitypub&tab=search&type=term' ) ) | |
| 3420 | + ), | |
| 3421 | + array( 'a' => array( 'href' => array() ) ) | |
| 3422 | + ); | |
| 3423 | + } else { | |
| 3424 | + esc_html_e( 'The ActivityPub plugin is required to receive posts from this feed.', 'friends' ); | |
| 3425 | + } | |
| 3426 | + ?> | |
| 3427 | + </div> | |
| 3428 | + </div> | |
| 3429 | + <?php | |
| 3104 | 3430 | } |
| 3105 | 3431 | } |