Making WordPress.org

Changeset 3937


Ignore:
Timestamp:
09/03/2016 12:40:00 AM (10 years ago)
Author:
jmdodd
Message:

Support Forums: Add "archived" queue for moderator use.

Archived posts are those that should not be visible, but may need to be referenced later.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-moderators.php

    r3913 r3937  
    55class Moderators {
    66
     7        const ARCHIVED       = 'archived';
     8        const ARCHIVED_META  = '_wporg_bbp_unarchived_post_status';
     9        const DEFAULT_STATUS = 'publish';
     10
    711        public function __construct() {
    8                 add_action( 'parse_query', array( $this, 'register_views' ), 1 );
     12                // Moderator-specific views.
     13                add_action( 'bbp_register_views', array( $this, 'register_views' ), 1 );
     14
     15                // Scripts and styles.
     16                add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
     17
     18                // Archived post status.
     19                add_action( 'bbp_register_post_statuses',       array( $this, 'register_post_statuses' ) );
     20                add_action( 'bbp_get_request',                  array( $this, 'archive_handler' ) );
     21                add_filter( 'bbp_after_has_topics_parse_args',  array( $this, 'add_post_status_to_query' ) );
     22                add_filter( 'bbp_after_has_replies_parse_args', array( $this, 'add_post_status_to_query' ) );
     23                add_filter( 'bbp_topic_admin_links',            array( $this, 'admin_links' ), 10, 2 );
     24                add_filter( 'bbp_reply_admin_links',            array( $this, 'admin_links' ), 10, 2 );
    925        }
    1026
    … …  
    4359                        )
    4460                );
     61
     62                bbp_register_view(
     63                        'archived',
     64                        __( 'Archived', 'wporg-forums' ),
     65                        array(
     66                                'meta_key'      => null,
     67                                'post_type'     => array(
     68                                        'topic',
     69                                        'reply',
     70                                ),
     71                                'post_status'   => 'archived',
     72                                'show_stickies' => false,
     73                                'orderby'       => 'ID',
     74                        )
     75                );
     76        }
     77
     78        public function enqueue_styles() {
     79                if ( current_user_can( 'moderate' ) ) {
     80                        wp_enqueue_style( 'support-forums-moderators', plugins_url( 'css/styles-moderators.css', __DIR__ ) );
     81                }
     82        }
     83
     84        public function register_post_statuses() {
     85
     86                /**
     87                 * Add archived post status.
     88                 *
     89                 * Archived posts are intended for moderator use in determining a pattern
     90                 * of behavior. They are not duplicates, or spam, but should not be moved
     91                 * to the trash because they record user activity.
     92                 */
     93                register_post_status(
     94                        self::ARCHIVED,
     95                        array(
     96                                'label'                     => _x( 'Archived', 'post', 'wporg-forums' ),
     97                                'label_count'               => _nx_noop( 'Archived <span class="count">(%s)</span>', 'Archived <span class="count">(%s)</span>', 'post', 'wporg-forums' ),
     98                                'protected'                 => true,
     99                                'exclude_from_search'       => true,
     100                                'show_in_admin_status_list' => true,
     101                                'show_in_admin_add_list'    => false,
     102                        )
     103                );
     104
     105        }
     106
     107        public function archive_handler( $action = '' ) {
     108                if ( ! current_user_can( 'moderate' ) ) {
     109                        return;
     110                }
     111                $user_id = get_current_user_id();
     112
     113                if ( ! in_array( $action, $this->get_valid_actions() ) ) {
     114                        return;
     115                }
     116
     117                if ( empty( $_GET['post_id'] ) ) {
     118                        return;
     119                }
     120
     121                $post = get_post( absint( $_GET['post_id'] ) );
     122                if ( ! $post ) {
     123                        return false;
     124                }
     125
     126                // Check for empty post id.
     127                if ( ! $post ) {
     128                        bbp_add_error( 'wporg_bbp_archive_post_id', __( '<strong>ERROR</strong>: No post was found! Which post are you archiving?', 'wporg-forums' ) );
     129
     130                // Check for current user.
     131                } elseif ( empty( $user_id ) ) {
     132                        bbp_add_error( 'wporg_bbp_archive_logged_in', __( '<strong>ERROR</strong>: You must be logged in to do this!', 'wporg-forums' ) );
     133
     134                // Check nonce.
     135                } elseif ( ! bbp_verify_nonce_request( 'toggle-post-archive_' . $user_id . '_' . $post->ID ) ) {
     136                        bbp_add_error( 'wporg_bbp_archive_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'wporg-forums' ) );
     137
     138                }
     139
     140                if ( bbp_has_errors() ) {
     141                        return;
     142                }
     143
     144                $is_archived = $this->is_post_archived( $post->ID );
     145                $success = false;
     146                $add_view_all = false;
     147
     148                if ( true == $is_archived && 'wporg_bbp_unarchive_post' === $action ) {
     149                        $success = $this->unarchive_post( $post->ID );
     150                } elseif ( false == $is_archived && 'wporg_bbp_archive_post' === $action ) {
     151                        $success = $this->archive_post( $post->ID );
     152                        $add_view_all = true;
     153                }
     154
     155                $permalink = $this->get_permalink( $post->ID );
     156                if ( $add_view_all ) {
     157                        $permalink = bbp_add_view_all( $permalink, true );
     158                }
     159
     160                if ( true === $success ) {
     161                        $redirect = $this->get_permalink( $post->ID );
     162                        bbp_redirect( $redirect );
     163                } elseif ( true === $is_archived && 'wporg_bbp_archive_post' === $action ) {
     164                        bbp_add_error( 'wporg_bbp_archive_post', __( '<strong>ERROR</strong>: There was a problem archiving that post!', 'wporg-forums' ) );
     165                } elseif ( false === $is_archived && 'wporg_bbp_unarchive_post' === $action ) {
     166                        bbp_add_error( 'wporg_bbp_unarchive_post', __( '<strong>ERROR</strong>: There was a problem unarchiving that post!', 'wporg-forums' ) );
     167                }
     168        }
     169
     170        public function add_post_status_to_query( $args = array() ) {
     171                if ( bbp_get_view_all() ) {
     172                        $post_stati = explode( ',', $args['post_status'] );
     173                        $post_stati[] = self::ARCHIVED;
     174                        $args['post_status'] = implode( ',', $post_stati );
     175                }
     176                return $args;
     177        }
     178
     179        public function admin_links( $r, $post_id ) {
     180                $r['archive'] = $this->get_archive_link( array( 'post_id' => $post_id ) );
     181                return $r;
     182        }
     183
     184        public function get_archive_link( $args = array() ) {
     185                if ( ! current_user_can( 'moderate' ) ) {
     186                        return false;
     187                }
     188                $user_id = get_current_user_id();
     189
     190                $r = bbp_parse_args( $args, array(
     191                        'post_id' => get_the_ID(),
     192                        'archive' => esc_html__( 'Archive', 'wporg-forums' ),
     193                        'unarchive' => esc_html__( 'Unarchive', 'wporg-forums' ),
     194                ), 'get_post_archive_link' );
     195                if ( empty( $r['post_id'] ) ) {
     196                        return false;
     197                }
     198                $post_id = $r['post_id'];
     199
     200                $post = get_post( $post_id );
     201                if ( ! $post ) {
     202                        return false;
     203                }
     204
     205                if ( $this->is_post_archived( $post->ID ) ) {
     206                        $text = $r['unarchive'];
     207                        $query_args = array( 'action' => 'wporg_bbp_unarchive_post', 'post_id' => $post->ID );
     208                } else {
     209                        $text = $r['archive'];
     210                        $query_args = array( 'action' => 'wporg_bbp_archive_post', 'post_id' => $post->ID );
     211                }
     212
     213                $permalink = $this->get_permalink( $post->ID );
     214
     215                $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-post-archive_' . $user_id . '_' . $post->ID ) );
     216                return sprintf( "<a href='%s'>%s</a>", $url, esc_html( $text ) );
     217        }
     218
     219        public function is_post_archived( $post_id ) {
     220                $post = get_post( $post_id );
     221                if ( ! $post ) {
     222                        return false;
     223                }
     224
     225                return $post->post_status == self::ARCHIVED;
     226        }
     227
     228        public function archive_post( $post_id ) {
     229                if ( empty( $post_id ) ) {
     230                        return false;
     231                }
     232
     233                $post = get_post( $post_id );
     234                if ( ! $post ) {
     235                        return false;
     236                }
     237
     238                if ( ! $this->is_post_archived( $post->ID ) ) {
     239                        $post_id = wp_update_post( array(
     240                                'ID'          => $post->ID,
     241                                'post_status' => self::ARCHIVED,
     242                        ), true );
     243                        if ( $post_id ) {
     244                                update_post_meta( $post->ID, self::ARCHIVED_META, $post->post_status );
     245                                return true;
     246                        }
     247                }
     248
     249                return false;
     250        }
     251
     252        public function unarchive_post( $post_id ) {
     253                if ( empty( $post_id ) ) {
     254                        return false;
     255                }
     256
     257                $post = get_post( $post_id );
     258                if ( ! $post ) {
     259                        return false;
     260                }
     261
     262                if ( $this->is_post_archived( $post->ID ) ) {
     263                        $post_status = get_post_meta( $post->ID, self::ARCHIVED_META, true );
     264                        if ( ! $post_status ) {
     265                                $post_status = self::DEFAULT_STATUS;
     266                        }
     267                        $post_id = wp_update_post( array(
     268                                'ID'          => $post->ID,
     269                                'post_status' => $post_status,
     270                        ) );
     271                        if ( $post_id ) {
     272                                delete_post_meta( $post->ID, self::ARCHIVED_META );
     273                                return true;
     274                        }
     275                }
     276
     277                return false;
     278        }
     279
     280        public function get_permalink( $post_id ) {
     281                if ( empty( $post_id ) ) {
     282                        return false;
     283                }
     284
     285                $post = get_post( $post_id );
     286                if ( ! $post ) {
     287                        return false;
     288                }
     289
     290                switch ( $post->post_type ) {
     291                        case 'topic' :
     292                                $permalink = bbp_get_topic_permalink( $post->ID );
     293                                break;
     294                        case 'reply' :
     295                                $permalink = bbp_get_topic_permalink( bbp_get_reply_topic_id( $post->ID ) );
     296                                break;
     297                        case 'post' :
     298                        default :
     299                                $permalink = get_permalink( $post->ID );
     300                }
     301
     302                return $permalink;
     303        }
     304
     305        public function get_valid_actions() {
     306                return array(
     307                        'wporg_bbp_archive_post',
     308                        'wporg_bbp_unarchive_post',
     309                );
    45310        }
    46311}
Note: See TracChangeset for help on using the changeset viewer.