| 1 | <?php |
| 2 | |
| 3 | namespace WordPressdotorg\Forums\Topic_NSFW; |
| 4 | |
| 5 | class Plugin { |
| 6 | |
| 7 | /** |
| 8 | * @var Plugin The singleton instance. |
| 9 | */ |
| 10 | private static $instance; |
| 11 | |
| 12 | const META_KEY = 'topic_nsfw'; |
| 13 | |
| 14 | /** |
| 15 | * Always return the same instance of this plugin. |
| 16 | * |
| 17 | * @return Plugin |
| 18 | */ |
| 19 | public static function get_instance() { |
| 20 | if ( ! ( self::$instance instanceof Plugin ) ) { |
| 21 | self::$instance = new Plugin(); |
| 22 | } |
| 23 | return self::$instance; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Instantiates a new Plugin object. |
| 28 | */ |
| 29 | private function __construct() { |
| 30 | add_action( 'bbp_loaded', array( $this, 'bbp_loaded' ) ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Initializes the plugin. |
| 35 | */ |
| 36 | public function bbp_loaded() { |
| 37 | // Change the topic title when NSFW flag checked. |
| 38 | add_filter( 'bbp_get_topic_title', array( $this, 'get_topic_title' ), 10, 2 ); |
| 39 | |
| 40 | // Display the form for new/edit topics. |
| 41 | add_action( 'bbp_theme_before_topic_form_subscriptions', array( $this, 'form_topic_nsfw_checkbox' ), 15 ); |
| 42 | |
| 43 | // Process field submission for topics. |
| 44 | add_action( 'bbp_new_topic_post_extras', array( $this, 'topic_post_extras' ) ); |
| 45 | add_action( 'bbp_edit_topic_post_extras', array( $this, 'topic_post_extras' ) ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Add "NSFW" status to title. |
| 50 | */ |
| 51 | public function get_topic_title( $title, $topic_id ) { |
| 52 | // Don't run in the admin. |
| 53 | if ( is_admin() ) { |
| 54 | return $title; |
| 55 | } |
| 56 | |
| 57 | // Don't run when viewing a topic edit page or a reply edit page. |
| 58 | if ( bbp_is_topic_edit() || bbp_is_reply_edit() ) { |
| 59 | return $title; |
| 60 | } |
| 61 | |
| 62 | if ( $this->is_nsfw_topic( $topic_id ) ) { |
| 63 | $title = sprintf( |
| 64 | '<abbr title="%s">%s</abbr> ', |
| 65 | esc_attr__( 'Not safe for work', 'wporg-forums' ), |
| 66 | esc_html__( '[NSFW]', 'wporg-forums' ) |
| 67 | ) . $title; |
| 68 | } |
| 69 | |
| 70 | return $title; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Output topic NSFW checkbox. |
| 75 | */ |
| 76 | public function form_topic_nsfw_checkbox() { |
| 77 | $is_nsfw = $this->is_nsfw_topic(); |
| 78 | ?> |
| 79 | <p> |
| 80 | <label for="topic-nsfw"> |
| 81 | <input type="checkbox" name="<?php echo esc_attr( self::META_KEY ); ?>" id="topic-nsfw" value="yes"<?php checked( $is_nsfw, 'yes' ); ?>> |
| 82 | <?php esc_html_e( 'My site or content may be considered Not Safe for Work (NSFW)', 'wporg-forums' ); ?> |
| 83 | </label> |
| 84 | </p> |
| 85 | <?php |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Process topic form submission. |
| 90 | */ |
| 91 | public function topic_post_extras( $topic_id ) { |
| 92 | if ( isset( $_POST[ self::META_KEY ] ) ) { |
| 93 | update_post_meta( $topic_id, self::META_KEY, 'yes' ); |
| 94 | } else { |
| 95 | delete_post_meta( $topic_id, self::META_KEY ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Is a NSFW topic? |
| 101 | */ |
| 102 | public function is_nsfw_topic() { |
| 103 | return get_post_meta( get_the_ID(), self::META_KEY, true ); |
| 104 | } |
| 105 | } |