[ 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   *
   6   * @package BuddyPress
   7   * @subpackage Messages
   8   * @since 3.0.0
   9   */
  10  
  11  // Exit if accessed directly.
  12  defined( 'ABSPATH' ) || exit;
  13  
  14  class BP_Messages_Notices_Admin {
  15  
  16      /**
  17       * The ID returned by `add_users_page()`.
  18       *
  19       * @since 3.0.0
  20       * @var string
  21       */
  22      public $screen_id = '';
  23  
  24      /**
  25       * The URL of the admin screen.
  26       *
  27       * @since 3.0.0
  28       * @var string
  29       */
  30      public $url = '';
  31  
  32      /**
  33       * The current instance of the BP_Messages_Notices_List_Table class.
  34       *
  35       * @since 3.0.0
  36       * @var object
  37       */
  38      public $list_table = '';
  39  
  40  
  41      /**
  42       * Create a new instance or access the current instance of this class.
  43       *
  44       * @since 3.0.0
  45       */
  46  	public static function register_notices_admin() {
  47  
  48          if ( ! is_admin() || ! bp_is_active( 'messages' ) || ! bp_current_user_can( 'bp_moderate' ) ) {
  49              return;
  50          }
  51  
  52          $bp = buddypress();
  53  
  54          if ( empty( $bp->messages->admin ) ) {
  55              $bp->messages->admin = new self;
  56          }
  57  
  58          return $bp->messages->admin;
  59      }
  60  
  61      /**
  62       * Constructor method.
  63       *
  64       * @since 3.0.0
  65       */
  66  	public function __construct() {
  67          $this->setup_globals();
  68          $this->setup_actions();
  69      }
  70  
  71      /**
  72       * Populate the classs variables.
  73       *
  74       * @since 3.0.0
  75       */
  76  	protected function setup_globals() {
  77          $this->url = add_query_arg( array( 'page' => 'bp-notices' ), bp_get_admin_url( 'users.php' ) );
  78      }
  79  
  80      /**
  81       * Add action hooks.
  82       *
  83       * @since 3.0.0
  84       */
  85  	protected function setup_actions() {
  86          add_action( bp_core_admin_hook(), array( $this, 'admin_menu' ) );
  87      }
  88  
  89      /**
  90       * Add the 'Site Notices' admin menu item.
  91       *
  92       * @since 3.0.0
  93       */
  94  	public function admin_menu() {
  95          // Bail if current user cannot moderate community.
  96          if ( ! bp_current_user_can( 'bp_moderate' ) || ! bp_is_active( 'messages' ) ) {
  97              return false;
  98          }
  99  
 100          $this->screen_id = add_users_page(
 101              _x( 'Site Notices', 'Notices admin page title', 'buddypress' ),
 102              _x( 'Site Notices', 'Admin Users menu', 'buddypress' ),
 103              'manage_options',
 104              'bp-notices',
 105              array( $this, 'admin_index' )
 106          );
 107  
 108          add_action( 'load-' . $this->screen_id, array( $this, 'admin_load' ) );
 109      }
 110  
 111      /**
 112       * Catch save/update requests or load the screen.
 113       *
 114       * @since 3.0.0
 115       */
 116  	public function admin_load() {
 117          $redirect_to = false;
 118  
 119          // Catch new notice saves.
 120          if ( ! empty( $_POST['bp_notice']['send'] ) ) {
 121  
 122              check_admin_referer( 'new-notice', 'ns-nonce' );
 123  
 124              $notice = wp_parse_args( $_POST['bp_notice'], array(
 125                  'subject' => '',
 126                  'content' => ''
 127              ) );
 128  
 129              if ( messages_send_notice( $notice['subject'], $notice['content'] ) ) {
 130                  $redirect_to = add_query_arg( 'success', 'create', $this->url );
 131  
 132              // Notice could not be sent.
 133              } else {
 134                  $redirect_to = add_query_arg( 'error', 'create', $this->url );
 135              }
 136          }
 137  
 138          // Catch activation/deactivation/delete requests
 139          if ( ! empty( $_GET['notice_id'] ) && ! empty( $_GET['notice_action'] ) ) {
 140              $notice_id = absint( $_GET['notice_id'] );
 141  
 142              check_admin_referer( 'messages-' . $_GET['notice_action'] . '-notice-' . $notice_id );
 143  
 144              $success = false;
 145              switch ( $_GET['notice_action'] ) {
 146                  case 'activate':
 147                      $notice = new BP_Messages_Notice( $notice_id );
 148                      $success = $notice->activate();
 149                      break;
 150                  case 'deactivate':
 151                      $notice = new BP_Messages_Notice( $notice_id );
 152                      $success = $notice->deactivate();
 153                      break;
 154                  case 'delete':
 155                      $notice = new BP_Messages_Notice( $notice_id );
 156                      $success = $notice->delete();
 157                      break;
 158              }
 159              if ( $success ) {
 160                  $redirect_to = add_query_arg( 'success', 'update', $this->url );
 161  
 162              // Notice could not be updated.
 163              } else {
 164                  $redirect_to = add_query_arg( 'error', 'update', $this->url );
 165              }
 166  
 167          }
 168  
 169          if ( $redirect_to ) {
 170              wp_safe_redirect( $redirect_to );
 171              exit();
 172          }
 173  
 174          $this->list_table = new BP_Messages_Notices_List_Table( array( 'screen' => get_current_screen()->id ) );
 175      }
 176  
 177      /**
 178       * Generate content for the bp-notices admin screen.
 179       *
 180       * @since 3.0.0
 181       */
 182  	public function admin_index() {
 183          $this->list_table->prepare_items();
 184          ?>
 185          <div class="wrap">
 186              <?php if ( version_compare( $GLOBALS['wp_version'], '4.8', '>=' ) ) : ?>
 187  
 188                  <h1 class="wp-heading-inline"><?php echo esc_html_x( 'Site Notices', 'Notices admin page title', 'buddypress' ); ?></h1>
 189                  <hr class="wp-header-end">
 190  
 191              <?php else : ?>
 192  
 193                  <h1><?php echo esc_html_x( 'Site Notices', 'Notices admin page title', 'buddypress' ); ?></h1>
 194  
 195              <?php endif; ?>
 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'; ?>">
 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: Mon Jul 15 01:01:40 2019 Cross-referenced by PHPXref 0.7.1