get_comment_actions( 'dropdown', $comment, $comment_depth ); $fragment['commentTrashedActions'] = $this->get_comment_actions( 'trashed_dropdown', $comment, $comment_depth ); return $fragment; } public function get_comment_actions( $location, $comment, $comment_depth ) { // Accepted answer 'comments' do not have proper IDs. if ( ! $comment || ! is_numeric( $comment->comment_ID ) ) { return ''; } $actions = array(); $actions = apply_filters( 'o2_comment_actions', $actions, $location, $comment, $comment_depth ); $actions = (array) $actions; if ( 0 === count( $actions ) ) { return ''; } // no actions on xcomments please $xcomment_original_permalink = get_comment_meta( $comment->comment_ID, 'xcomment_original_permalink', true ); if ( ! empty( $xcomment_original_permalink ) ) { return ''; } $comments_menu = new EllipsisMenu(); $comments_menu->label = esc_html__( 'Comment Actions', 'p2020' ); $comments_menu->classes = 'o2-dropdown-actions o2-comment-actions o2-comment-dropdown-actions'; $actions = $this->prepare_plugin_actions( $actions ); $actions = $this->sort_group_actions( $actions ); foreach ( $actions as $action ) { if ( self::SEPARATOR === $action ) { $comments_menu->add_separator(); } elseif ( is_string( $action ) ) { $comments_menu->add_item_html( $action ); } else { $comments_menu->add_item( $action['label'], $action['url'], $action['icon'], $action['attributes'] ?? [] ); } } return $comments_menu->generate(); } // As some plugins do not provide their actions in expected format, and we still want // to correctly order, let's change action html. // Alternative would be to update known plugins private function prepare_plugin_actions( &$actions ) { $result = []; foreach ( $actions as $key => $action ) { $new_key = $key; // strip existing groups $action = str_replace( 'o2-actions-border-top', '', $action ); // give keys to known actions if ( is_int( $key ) ) { if ( str_contains( $action, 'o2-comment-top' ) ) { unset( $actions[ $key ] ); $new_key = 'o2-comment-top'; } if ( str_contains( $action, 'o2-short-link' ) ) { unset( $actions[ $key ] ); $new_key = 'o2-short-link'; } } $result[ $new_key ] = $action; } return $result; } private function sort_group_actions( $actions ) { $expected_order = [ 'o2-comment-edit', 'o2-comment-top', self::SEPARATOR, 'comment-like', 'o2-comment-reply', 'o2-login-to-reply', 'o2-permalink', 'a8c-public-shorthand', 'o2-short-link', self::SEPARATOR, 'o2-comment-trash', 'o2-comment-untrash', ]; $result = []; $render_group_start = false; // fill $result array with enabled actions in $expected_order, and also add separators foreach ( $expected_order as $key ) { if ( self::SEPARATOR === $key ) { $render_group_start = true; } if ( empty( $actions[ $key ] ) ) { continue; } $action = $actions[ $key ]; if ( $action ) { if ( $render_group_start ) { $render_group_start = false; $result[] = self::SEPARATOR; } $result[] = $action; } } return $result; } /** * Add comment link action to the list of comment actions */ public static function add_comment_link_action( $actions, $location, $comment ) { if ( 'dropdown' === $location ) { $link = get_comment_link( $comment ); if ( ! $link ) { return $actions; } $actions['o2-permalink'] = '' . __( 'Copy link', 'p2020' ) . ''; } return $actions; } } new CommentActions();