[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-messages/classes/ -> class-bp-messages-notices-admin.php (source)

   1  <?php
   2  /**
   3   * BuddyPress messages component Site-wide Notices admin screen.
   4   *
   5   * @package BuddyPress
   6   * @subpackage MessagesClasses
   7   * @since 3.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * BuddyPress Notices Admin class.
  15   */
  16  class BP_Messages_Notices_Admin {
  17  
  18      /**
  19       * The ID returned by `add_users_page()`.
  20       *
  21       * @since 3.0.0
  22       * @var string
  23       */
  24      public $screen_id = '';
  25  
  26      /**
  27       * The URL of the admin screen.
  28       *
  29       * @since 3.0.0
  30       * @var string
  31       */
  32      public $url = '';
  33  
  34      /**
  35       * The current instance of the BP_Messages_Notices_List_Table class.
  36       *
  37       * @since 3.0.0
  38       * @var BP_Messages_Notices_List_Table|string
  39       */
  40      public $list_table = '';
  41  
  42      /**
  43       * Create a new instance or access the current instance of this class.
  44       *
  45       * @global BuddyPress $bp The one true BuddyPress instance.
  46       *
  47       * @since 3.0.0
  48       *
  49       * @return BP_Messages_Notices_Admin
  50       */
  51  	public static function register_notices_admin() {
  52  
  53          if ( ! is_admin() || ! bp_is_active( 'messages' ) || ! bp_current_user_can( 'bp_moderate' ) ) {
  54              return;
  55          }
  56  
  57          $bp = buddypress();
  58  
  59          if ( empty( $bp->messages->admin ) ) {
  60              $bp->messages->admin = new self;
  61          }
  62  
  63          return $bp->messages->admin;
  64      }
  65  
  66      /**
  67       * Constructor.
  68       *
  69       * @since 3.0.0
  70       */
  71  	public function __construct() {
  72          $this->setup_globals();
  73          $this->setup_actions();
  74      }
  75  
  76      /**
  77       * Populate the classs variables.
  78       *
  79       * @since 3.0.0
  80       */
  81  	protected function setup_globals() {
  82          $this->url = add_query_arg( array( 'page' => 'bp-notices' ), bp_get_admin_url( 'users.php' ) );
  83      }
  84  
  85      /**
  86       * Add action hooks.
  87       *
  88       * @since 3.0.0
  89       */
  90  	protected function setup_actions() {
  91          add_action( bp_core_admin_hook(), array( $this, 'admin_menu' ) );
  92      }
  93  
  94      /**
  95       * Add the 'Site Notices' admin menu item.
  96       *
  97       * @since 3.0.0
  98       */
  99  	public function admin_menu() {
 100          // Bail if current user cannot moderate community.
 101          if ( ! bp_current_user_can( 'bp_moderate' ) || ! bp_is_active( 'messages' ) ) {
 102              return false;
 103          }
 104  
 105          $this->screen_id = add_users_page(
 106              _x( 'Site Notices', 'Notices admin page title', 'buddypress' ),
 107              _x( 'Site Notices', 'Admin Users menu', 'buddypress' ),
 108              'manage_options',
 109              'bp-notices',
 110              array( $this, 'admin_index' )
 111          );
 112  
 113          add_action( 'load-' . $this->screen_id, array( $this, 'admin_load' ) );
 114      }
 115  
 116      /**
 117       * Catch save/update requests or load the screen.
 118       *
 119       * @since 3.0.0
 120       */
 121  	public function admin_load() {
 122          $redirect_to = false;
 123  
 124          // Catch new notice saves.
 125          if ( ! empty( $_POST['bp_notice']['send'] ) ) {
 126  
 127              check_admin_referer( 'new-notice', 'ns-nonce' );
 128  
 129              $notice = bp_parse_args(
 130                  $_POST['bp_notice'],
 131                  array(
 132                      'subject' => '',
 133                      'content' => '',
 134                  )
 135              );
 136  
 137              if ( messages_send_notice( $notice['subject'], $notice['content'] ) ) {
 138                  $redirect_to = add_query_arg( 'success', 'create', $this->url );
 139  
 140              // Notice could not be sent.
 141              } else {
 142                  $redirect_to = add_query_arg( 'error', 'create', $this->url );
 143              }
 144          }
 145  
 146          // Catch activation/deactivation/delete requests
 147          if ( ! empty( $_GET['notice_id'] ) && ! empty( $_GET['notice_action'] ) ) {
 148              $notice_id = absint( $_GET['notice_id'] );
 149  
 150              check_admin_referer( 'messages-' . $_GET['notice_action'] . '-notice-' . $notice_id );
 151  
 152              $success = false;
 153              switch ( $_GET['notice_action'] ) {
 154                  case 'activate':
 155                      $notice = new BP_Messages_Notice( $notice_id );
 156                      $success = $notice->activate();
 157                      break;
 158                  case 'deactivate':
 159                      $notice = new BP_Messages_Notice( $notice_id );
 160                      $success = $notice->deactivate();
 161                      break;
 162                  case 'delete':
 163                      $notice = new BP_Messages_Notice( $notice_id );
 164                      $success = $notice->delete();
 165                      break;
 166              }
 167              if ( $success ) {
 168                  $redirect_to = add_query_arg( 'success', 'update', $this->url );
 169  
 170              // Notice could not be updated.
 171              } else {
 172                  $redirect_to = add_query_arg( 'error', 'update', $this->url );
 173              }
 174  
 175          }
 176  
 177          if ( $redirect_to ) {
 178              wp_safe_redirect( $redirect_to );
 179              exit();
 180          }
 181  
 182          $this->list_table = new BP_Messages_Notices_List_Table( array( 'screen' => get_current_screen()->id ) );
 183      }
 184  
 185      /**
 186       * Generate content for the bp-notices admin screen.
 187       *
 188       * @since 3.0.0
 189       */
 190  	public function admin_index() {
 191          $this->list_table->prepare_items();
 192          ?>
 193          <div class="wrap">
 194              <h1 class="wp-heading-inline"><?php echo esc_html_x( 'Site Notices', 'Notices admin page title', 'buddypress' ); ?></h1>
 195              <hr class="wp-header-end">
 196  
 197              <p class="bp-notice-about"><?php esc_html_e( 'Manage notices shown at front end of your site to all logged-in users.', 'buddypress' ); ?></p>
 198  
 199              <div class="bp-new-notice-panel">
 200  
 201                  <h2 class="bp-new-notice"><?php esc_html_e( 'Add New Notice', 'buddypress' ); ?></h2>
 202  
 203                  <form action="<?php echo esc_url( wp_nonce_url( $this->url, 'new-notice', 'ns-nonce' ) ); ?>" method="post">
 204  
 205                      <div>
 206                          <label for="bp_notice_subject"><?php esc_html_e( 'Subject', 'buddypress' ); ?></label>
 207                          <input type="text" class="bp-panel-input" id="bp_notice_subject" name="bp_notice[subject]"/>
 208  
 209                          <label for="bp_notice_content"><?php esc_html_e( 'Content', 'buddypress' ); ?></label>
 210                          <textarea class="bp-panel-textarea" id="bp_notice_content" name="bp_notice[content]"></textarea>
 211                      </div>
 212  
 213                      <input type="submit" value="<?php esc_attr_e( 'Publish Notice', 'buddypress' ); ?>" name="bp_notice[send]" class="button button-primary save alignleft">
 214  
 215                  </form>
 216  
 217              </div>
 218  
 219              <?php if ( isset( $_GET['success'] ) || isset( $_GET['error'] ) ) : ?>
 220  
 221                  <div id="message" class="<?php echo isset( $_GET['success'] ) ? 'updated' : 'error'; ?> notice is-dismissible">
 222  
 223                      <p>
 224                          <?php
 225                          if ( isset( $_GET['error'] ) ) {
 226                              if ( 'create' === $_GET['error'] ) {
 227                                  esc_html_e( 'Notice was not created. Please try again.', 'buddypress' );
 228                              } else {
 229                                  esc_html_e( 'Notice was not updated. Please try again.', 'buddypress' );
 230                              }
 231                           } else {
 232                              if ( 'create' === $_GET['success'] ) {
 233                                  esc_html_e( 'Notice successfully created.', 'buddypress' );
 234                              } else {
 235                                  esc_html_e( 'Notice successfully updated.', 'buddypress' );
 236                              }
 237                          }
 238                          ?>
 239                      </p>
 240  
 241                  </div>
 242  
 243              <?php endif; ?>
 244  
 245              <h2 class="bp-notices-list"><?php esc_html_e( 'Notices List', 'buddypress' ); ?></h2>
 246  
 247              <?php $this->list_table->display(); ?>
 248  
 249          </div>
 250          <?php
 251      }
 252  }


Generated: Tue Mar 19 01:01:09 2024 Cross-referenced by PHPXref 0.7.1