Making WordPress.org

Ticket #2059: 2059.patch

File 2059.patch, 6.0 KB (added by ocean90, 9 years ago)
  • trunk/wordpress.org/public_html/wp-content/plugins/wporg-o2-posting-access/wporg-o2-posting-access.php

     
     1<?php
     2/**
     3 * Plugin Name: o2 Posting Access
     4 * Description: Allows any registered member to post on your o2 site.
     5 * Version:     1.0
     6 * License:     GPLv2 or later
     7 * Author:      WordPress.org
     8 * Author URI:  https://wordpress.org/
     9 */
     10
     11namespace WordPressdotorg\o2\Posting_Access;
     12
     13class Plugin {
     14
     15        /**
     16         * Initializes actions and filters.
     17         */
     18        public function init() {
     19                add_filter( 'user_has_cap', [ $this, 'add_post_capabilities' ], 10, 4 );
     20                add_filter( 'map_meta_cap', [ $this, 'do_not_allow_editing_comments' ], 9999, 4 );
     21                add_action( 'admin_bar_menu', [ $this, 'remove_non_accessible_menu_items' ], 100 );
     22
     23                if ( apply_filters( 'wporg_o2_enable_pending_for_unknown_users', true ) ) {
     24                        add_filter( 'gettext_with_context', [ $this, 'replace_post_button_label' ], 10, 4 );
     25                        add_filter( 'o2_create_post', [ $this, 'save_new_post_as_pending' ] );
     26                        add_filter( 'the_title', [ $this, 'prepend_pending_notice' ], 10, 2 );
     27                }
     28        }
     29
     30        /**
     31         * Prepends 'Pending Review:' to a post title.
     32         *
     33         * @param string $title   The post title.
     34         * @param int    $post_id The post ID.
     35         * @return string Filtered post title.
     36         */
     37        public function prepend_pending_notice( $title, $post_id = null ) {
     38                if ( $post_id && ( ! is_admin() || is_admin() && wp_doing_ajax() ) && 'pending' === get_post_status( $post_id ) ) {
     39                        /* translators: %s: Post title */
     40                        $title = sprintf( __( 'Pending Review: %s', 'o2' ), $title );
     41                }
     42
     43                return $title;
     44        }
     45
     46        /**
     47         * Removes content related menu items which are not accessible because of
     48         * the missing 'read' capability.
     49         *
     50         * @param \WP_Admin_Bar $wp_admin_bar The admin bar instance.
     51         */
     52        public function remove_non_accessible_menu_items( $wp_admin_bar ) {
     53                if ( current_user_can( 'read' ) ) {
     54                        return;
     55                }
     56
     57                $wp_admin_bar->remove_node( 'new-content' );
     58                $wp_admin_bar->remove_node( 'comments' );
     59                $wp_admin_bar->remove_node( 'edit' );
     60        }
     61
     62        /**
     63         * Don't allow editing comments for non-members.
     64         *
     65         * @param array  $caps    User's actual capabilities.
     66         * @param string $cap     Capability name.
     67         * @param int    $user_id User ID.
     68         * @param array  $args    Adds the context to the cap. Typically the object ID.
     69         * @return array Filtered capabilities.
     70         */
     71        function do_not_allow_editing_comments( $caps, $cap, $user_id, $args ) {
     72                if ( 'edit_comment' === $cap && ! is_user_member_of_blog( $user_id )  ) {
     73                        $caps = [ 'do_not_allow' ];
     74                }
     75
     76                return $caps;
     77        }
     78
     79        /**
     80         * Sets post status of a new post to 'pending'.
     81         *
     82         * @param object $post Post data.
     83         * @return object Filtered post data.
     84         */
     85        public function save_new_post_as_pending( $post ) {
     86                if ( is_user_member_of_blog( $post->post_author ) ) {
     87                        return $post;
     88                }
     89
     90                if ( $this->user_can_publish( $post->post_author ) ) {
     91                        return $post;
     92                }
     93
     94                $post->post_status = 'pending';
     95
     96                return $post;
     97        }
     98
     99        /**
     100         * Replaces the button label "Post" with "Submit for review" if current user
     101         * can't publish a post with post status 'publish'.
     102         *
     103         * @param string $translation  Translated text.
     104         * @param string $text         Text to translate.
     105         * @param string $context      Context information for the translators.
     106         * @param string $domain       Text domain. Unique identifier for retrieving translated strings.
     107         * @return string Filtered translated text.
     108         */
     109        public function replace_post_button_label( $translation, $text, $context, $domain ) {
     110                if ( 'o2' !== $domain || 'Verb, to post' !== $context || 'Post' !== $text ) {
     111                        return $translation;
     112                }
     113
     114                remove_filter( 'gettext_with_context', [ $this, 'replace_post_button_label' ] );
     115
     116                if ( $this->user_can_publish() ) {
     117                        return $translation;
     118                }
     119
     120                return __( 'Submit for review', 'o2' );
     121        }
     122
     123        /**
     124         * Whether a user can publish a post with post status 'publish'
     125         * .
     126         * @param int $user_id Optional. The user ID.
     127         * @return bool True when user can, false if not.
     128         */
     129        public function user_can_publish( $user_id = 0 ) {
     130                if ( ! $user_id ) {
     131                        $user_id = get_current_user_id();
     132                }
     133
     134                $user = get_user_by( 'id', $user_id );
     135
     136                $mod_keys = trim( get_option( 'moderation_keys', '' ) );
     137                if ( ! empty( $mod_keys ) ) {
     138                        $mod_keys = explode( "\n", $mod_keys );
     139                        $mod_keys = array_map( 'trim', $mod_keys );
     140                        $mod_keys = array_filter( $mod_keys );
     141                        $mod_keys = array_map( function( $mod_key ) {
     142                                return preg_quote( $mod_key, '#' );
     143                        }, $mod_keys);
     144
     145                        $pattern = '#(' . implode( '|', $mod_keys ) . ')#i';
     146
     147                        if ( preg_match( $pattern, $user->user_email ) ) {
     148                                return false;
     149                        }
     150                }
     151
     152                $has_published_post = (bool) get_posts( [ 'post_type' => 'post', 'post_status' => 'publish', 'author' => $user_id, 'numberposts' => 1 ] );
     153                if ( ! $has_published_post ) {
     154                        return false;
     155                }
     156
     157                return apply_filters( 'wporg_o2_user_can_publish', true, $user );
     158        }
     159
     160        /**
     161         * Adds post capabilities to current user.
     162         *
     163         * @param array   $allcaps An array of all the user's capabilities.
     164         * @param array   $caps    Actual capabilities for meta capability.
     165         * @param array   $args    Optional parameters passed to has_cap(), typically object ID.
     166         * @param WP_User $user    The user object.
     167         * @return array Array of all the user's capabilities.
     168         */
     169        public function add_post_capabilities( $allcaps, $caps, $args, $user ) {
     170                if ( ! is_user_logged_in() || in_array( 'publish_posts', $allcaps, true ) || is_user_member_of_blog( $user->ID ) ) {
     171                        return $allcaps;
     172                }
     173
     174                $allcaps['publish_posts'] = true;
     175                $allcaps['edit_posts'] = true;
     176                $allcaps['edit_published_posts'] = true;
     177
     178                return $allcaps;
     179        }
     180}
     181
     182add_action( 'o2_loaded', [ new Plugin(), 'init' ] );