[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/admin/ -> bp-core-admin-optouts.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Opt-outs management.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 8.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Set up the Opt-outs admin page.
  15   *
  16   * Loaded before the page is rendered, this function does all initial
  17   * setup, including: processing form requests, registering contextual
  18   * help, and setting up screen options.
  19   *
  20   * @since 8.0.0
  21   *
  22   * @global $bp_optouts_list_table
  23   */
  24  function bp_core_optouts_admin_load() {
  25      global $bp_optouts_list_table;
  26  
  27      // Build redirection URL.
  28      $redirect_to = remove_query_arg( array( 'action', 'error', 'updated', 'activated', 'notactivated', 'deleted', 'notdeleted', 'resent', 'notresent', 'do_delete', 'do_resend', 'do_activate', '_wpnonce', 'signup_ids' ), $_SERVER['REQUEST_URI'] );
  29      $doaction    = bp_admin_list_table_current_bulk_action();
  30  
  31      /**
  32       * Fires at the start of the nonmember opt-outs admin load.
  33       *
  34       * @since 8.0.0
  35       *
  36       * @param string $doaction Current bulk action being processed.
  37       * @param array  $_REQUEST Current $_REQUEST global.
  38       */
  39      do_action( 'bp_optouts_admin_load', $doaction, $_REQUEST );
  40  
  41      /**
  42       * Filters the allowed actions for use in the nonmember opt-outs admin page.
  43       *
  44       * @since 8.0.0
  45       *
  46       * @param array $value Array of allowed actions to use.
  47       */
  48      $allowed_actions = apply_filters( 'bp_optouts_admin_allowed_actions', array( 'do_delete',  'do_resend' ) );
  49  
  50      if ( ! in_array( $doaction, $allowed_actions ) || ( -1 == $doaction ) ) {
  51  
  52          require_once( ABSPATH . 'wp-admin/includes/class-wp-users-list-table.php' );
  53          $bp_optouts_list_table = new BP_Optouts_List_Table();
  54  
  55          // The per_page screen option.
  56          add_screen_option( 'per_page', array( 'label' => _x( 'Nonmember opt-outs', 'Nonmember opt-outs per page (screen options)', 'buddypress' ) ) );
  57  
  58          // Current screen.
  59          $current_screen = get_current_screen();
  60  
  61          $current_screen->add_help_tab(
  62              array(
  63                  'id'      => 'bp-optouts-overview',
  64                  'title'   => __( 'Overview', 'buddypress' ),
  65                  'content' =>
  66                      '<p>' . __( 'This is the administration screen for nonmember opt-outs on your site.', 'buddypress' ) . '</p>' .
  67                      '<p>' . __( 'From the screen options, you can customize the displayed columns and the pagination of this screen.', 'buddypress' ) . '</p>' .
  68                      '<p>' . __( 'You can reorder the list of opt-outs by clicking on the Email Sender, Email Type or Date Modified column headers.', 'buddypress' ) . '</p>' .
  69                      '<p>' . __( 'Using the search form, you can search for an opt-out to a specific email address.', 'buddypress' ) . '</p>',
  70              )
  71          );
  72  
  73          $current_screen->add_help_tab(
  74              array(
  75                  'id'      => 'bp-optouts-actions',
  76                  'title'   => __( 'Actions', 'buddypress' ),
  77                  'content' =>
  78                      '<p>' . __( 'Hovering over a row in the opt-outs list will display action links that allow you to manage the opt-out. You can perform the following actions:', 'buddypress' ) . '</p>' .
  79                      '<ul><li>' . __( '"Delete" allows you to delete the record of an opt-out. You will be asked to confirm this deletion.', 'buddypress' ) . '</li></ul>' .
  80                      '<p>' . __( 'Bulk actions allow you to perform these actions for the selected rows.', 'buddypress' ) . '</p>',
  81              )
  82          );
  83  
  84          // Help panel - sidebar links.
  85          $current_screen->set_help_sidebar(
  86              '<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
  87              '<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
  88          );
  89  
  90          // Add accessible hidden headings and text for the Pending Users screen.
  91          $current_screen->set_screen_reader_content(
  92              array(
  93                  /* translators: accessibility text */
  94                  'heading_views'      => __( 'Filter opt-outs list', 'buddypress' ),
  95                  /* translators: accessibility text */
  96                  'heading_pagination' => __( 'Opt-out list navigation', 'buddypress' ),
  97                  /* translators: accessibility text */
  98                  'heading_list'       => __( 'Opt-outs list', 'buddypress' ),
  99              )
 100          );
 101  
 102      } else {
 103          if ( empty( $_REQUEST['optout_ids' ] ) ) {
 104              return;
 105          }
 106          $optout_ids = wp_parse_id_list( $_REQUEST['optout_ids' ] );
 107  
 108          // Handle optout deletion.
 109          if ( 'do_delete' == $doaction ) {
 110  
 111              // Nonce check.
 112              check_admin_referer( 'optouts_delete' );
 113  
 114              $success = 0;
 115              foreach ( $optout_ids as $optout_id ) {
 116                  if ( bp_delete_optout_by_id( $optout_id ) ) {
 117                      $success++;
 118                  }
 119              }
 120  
 121              $query_arg = array( 'updated' => 'deleted' );
 122  
 123              if ( ! empty( $success ) ) {
 124                  $query_arg['deleted'] = $success;
 125              }
 126  
 127              $notdeleted = count( $optout_ids ) - $success;
 128              if ( $notdeleted > 0 ) {
 129                  $query_arg['notdeleted'] = $notdeleted;
 130              }
 131  
 132              $redirect_to = add_query_arg( $query_arg, $redirect_to );
 133  
 134              bp_core_redirect( $redirect_to );
 135  
 136          // Plugins can update other stuff from here.
 137          } else {
 138  
 139              /**
 140               * Fires at end of opt-outs admin load
 141               * if doaction does not match any actions.
 142               *
 143               * @since 2.0.0
 144               *
 145               * @param string $doaction Current bulk action being processed.
 146               * @param array  $_REQUEST Current $_REQUEST global.
 147               * @param string $redirect Determined redirect url to send user to.
 148               */
 149              do_action( 'bp_core_admin_update_optouts', $doaction, $_REQUEST, $redirect_to );
 150  
 151              bp_core_redirect( $redirect_to );
 152          }
 153      }
 154  }
 155  add_action( "load-tools_page_bp-optouts", 'bp_core_optouts_admin_load' );
 156  
 157  /**
 158   * Get admin notice when viewing the optouts management page.
 159   *
 160   * @since 8.0.0
 161   *
 162   * @return array
 163   */
 164  function bp_core_get_optouts_notice() {
 165  
 166      // Setup empty notice for return value.
 167      $notice = array();
 168  
 169      // Updates.
 170      if ( ! empty( $_REQUEST['updated'] ) && 'deleted' === $_REQUEST['updated'] ) {
 171          $notice = array(
 172              'class'   => 'updated',
 173              'message' => ''
 174          );
 175  
 176          if ( ! empty( $_REQUEST['deleted'] ) ) {
 177              $deleted            = absint( $_REQUEST['deleted'] );
 178              $notice['message'] .= sprintf(
 179                  _nx(
 180                      /* translators: %s: number of deleted optouts */
 181                      '%s opt-out successfully deleted!', '%s opt-outs successfully deleted!',
 182                      $deleted,
 183                      'nonmembers opt-out deleted',
 184                      'buddypress'
 185                  ),
 186                  number_format_i18n( absint( $_REQUEST['deleted'] ) )
 187              );
 188          }
 189  
 190          if ( ! empty( $_REQUEST['notdeleted'] ) ) {
 191              $notdeleted         = absint( $_REQUEST['notdeleted'] );
 192              $notice['message'] .= sprintf(
 193                  _nx(
 194                      /* translators: %s: number of optouts that failed to be deleted */
 195                      '%s opt-out was not deleted.', '%s opt-outs were not deleted.',
 196                      $notdeleted,
 197                      'nonmembers opt-out not deleted',
 198                      'buddypress'
 199                  ),
 200                  number_format_i18n( $notdeleted )
 201              );
 202  
 203              if ( empty( $_REQUEST['deleted'] ) ) {
 204                  $notice['class'] = 'error';
 205              }
 206          }
 207      }
 208  
 209      // Errors.
 210      if ( ! empty( $_REQUEST['error'] ) && 'do_delete' === $_REQUEST['error'] ) {
 211          $notice = array(
 212              'class'   => 'error',
 213              'message' => esc_html__( 'There was a problem deleting opt-outs. Please try again.', 'buddypress' ),
 214          );
 215      }
 216  
 217      return $notice;
 218  }
 219  
 220  /**
 221   * Opt-outs admin page router.
 222   *
 223   * Depending on the context, display
 224   * - the list of optouts,
 225   * - or the delete confirmation screen,
 226   *
 227   * Also prepare the admin notices.
 228   *
 229   * @since 8.0.0
 230   */
 231  function bp_core_optouts_admin() {
 232      $doaction = bp_admin_list_table_current_bulk_action();
 233  
 234      // Prepare notices for admin.
 235      $notice = bp_core_get_optouts_notice();
 236  
 237      // Display notices.
 238      if ( ! empty( $notice ) ) :
 239          if ( 'updated' === $notice['class'] ) : ?>
 240  
 241              <div id="message" class="<?php echo esc_attr( $notice['class'] ); ?> notice is-dismissible">
 242  
 243          <?php else: ?>
 244  
 245              <div class="<?php echo esc_attr( $notice['class'] ); ?> notice is-dismissible">
 246  
 247          <?php endif; ?>
 248  
 249              <p><?php echo esc_html( $notice['message'] ); ?></p>
 250          </div>
 251  
 252      <?php endif;
 253  
 254      // Show the proper screen.
 255      switch ( $doaction ) {
 256          case 'delete' :
 257              bp_core_optouts_admin_manage( $doaction );
 258              break;
 259  
 260          default:
 261              bp_core_optouts_admin_index();
 262              break;
 263      }
 264  }
 265  
 266  /**
 267   * This is the list of optouts.
 268   *
 269   * @since 8.0.0
 270   *
 271   * @global $plugin_page
 272   * @global $bp_optouts_list_table
 273   */
 274  function bp_core_optouts_admin_index() {
 275      global $plugin_page, $bp_optouts_list_table;
 276  
 277      $usersearch = ! empty( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : '';
 278  
 279      // Prepare the group items for display.
 280      $bp_optouts_list_table->prepare_items();
 281  
 282      if ( is_network_admin() ) {
 283          $form_url = network_admin_url( 'admin.php' );
 284      } else {
 285          $form_url = bp_get_admin_url( 'tools.php' );
 286      }
 287  
 288      $form_url = add_query_arg(
 289          array(
 290              'page' => 'bp-optouts',
 291          ),
 292          $form_url
 293      );
 294  
 295      $search_form_url = remove_query_arg(
 296          array(
 297              'action',
 298              'deleted',
 299              'notdeleted',
 300              'error',
 301              'updated',
 302              'delete',
 303              'activate',
 304              'activated',
 305              'notactivated',
 306              'resend',
 307              'resent',
 308              'notresent',
 309              'do_delete',
 310              'do_activate',
 311              'do_resend',
 312              'action2',
 313              '_wpnonce',
 314              'optout_ids'
 315          ),
 316          $_SERVER['REQUEST_URI']
 317      );
 318  
 319      ?>
 320  
 321      <div class="wrap">
 322          <h1 class="wp-heading-inline"><?php esc_html_e( 'BuddyPress tools', 'buddypress' ); ?></h1>
 323          <hr class="wp-header-end">
 324  
 325          <h2 class="nav-tab-wrapper"><?php bp_core_admin_tabs( __( 'Manage Opt-outs', 'buddypress' ), 'tools' ); ?></h2>
 326  
 327          <?php
 328          if ( $usersearch ) {
 329              $num_results = (int) $bp_optouts_list_table->total_items;
 330              printf(
 331                  '<p><span class="subtitle">%s</span></p>',
 332                  sprintf(
 333                      esc_html(
 334                          /* translators: %s: the searched email. */
 335                          _n( 'Opt-out with an email address matching &#8220;%s&#8221;', 'Opt-outs with an email address matching &#8220;%s&#8221;', $num_results, 'buddypress' )
 336                      ),
 337                      esc_html( $usersearch )
 338                  )
 339              );
 340          }
 341          ?>
 342          <p><?php esc_html_e( 'This table shows opt-out requests from people who are not members of this site, but have been contacted via communication from this site, and wish to receive no further communications.', 'buddypress' ); ?></p>
 343  
 344          <?php // Display each opt-out on its own row. ?>
 345          <?php $bp_optouts_list_table->views(); ?>
 346  
 347          <form id="bp-optouts-search-form" action="<?php echo esc_url( $search_form_url ) ;?>">
 348              <input type="hidden" name="page" value="<?php echo esc_attr( $plugin_page ); ?>" />
 349              <?php $bp_optouts_list_table->search_box( esc_html__( 'Search for a specific email address', 'buddypress' ), 'bp-optouts' ); ?>
 350          </form>
 351  
 352          <form id="bp-optouts-form" action="<?php echo esc_url( $form_url );?>" method="post">
 353              <?php $bp_optouts_list_table->display(); ?>
 354          </form>
 355      </div>
 356  <?php
 357  }
 358  
 359  /**
 360   * This is the confirmation screen for actions.
 361   *
 362   * @since 8.0.0
 363   *
 364   * @param string $action Delete or resend optout.
 365   *
 366   * @return null|false
 367   */
 368  function bp_core_optouts_admin_manage( $action = '' ) {
 369      $capability = bp_core_do_network_admin() ? 'manage_network_options' : 'manage_options';
 370      if ( ! current_user_can( $capability ) || empty( $action ) ) {
 371          die( '-1' );
 372      }
 373  
 374      // Get the IDs from the URL.
 375      $ids = false;
 376      if ( ! empty( $_POST['optout_ids'] ) ) {
 377          $ids = wp_parse_id_list( $_POST['optout_ids'] );
 378      } elseif ( ! empty( $_GET['optout_id'] ) ) {
 379          $ids = absint( $_GET['optout_id'] );
 380      }
 381  
 382      if ( empty( $ids ) ) {
 383          return false;
 384      }
 385  
 386      // Query for matching optouts, and filter out bad IDs.
 387      $args = array(
 388          'id'     => $ids,
 389      );
 390      $optouts    = bp_get_optouts( $args );
 391      $optout_ids = wp_list_pluck( $optouts, 'id' );
 392  
 393      // Check optout IDs and set up strings.
 394      switch ( $action ) {
 395          case 'delete' :
 396              if ( 0 === count( $optouts ) ) {
 397                  $helper_text = __( 'No opt-out requests were found.', 'buddypress' );
 398              } else {
 399                  $helper_text = _n( 'You are about to delete the following opt-out request:', 'You are about to delete the following opt-out requests:', count( $optouts ), 'buddypress' );
 400              }
 401              break;
 402      }
 403  
 404      // These arguments are added to all URLs.
 405      $url_args = array( 'page' => 'bp-optouts' );
 406  
 407      // These arguments are only added when performing an action.
 408      $action_args = array(
 409          'action'     => 'do_' . $action,
 410          'optout_ids' => implode( ',', $optout_ids )
 411      );
 412  
 413      if ( is_network_admin() ) {
 414          $base_url = network_admin_url( 'admin.php' );
 415      } else {
 416          $base_url = bp_get_admin_url( 'tools.php' );
 417      }
 418  
 419      $cancel_url = add_query_arg( $url_args, $base_url );
 420      $action_url = wp_nonce_url(
 421          add_query_arg(
 422              array_merge( $url_args, $action_args ),
 423              $base_url
 424          ),
 425          'optouts_' . $action
 426      );
 427  
 428      ?>
 429  
 430      <div class="wrap">
 431          <h1 class="wp-heading-inline"><?php esc_html_e( 'BuddyPress tools', 'buddypress' ); ?></h1>
 432          <hr class="wp-header-end">
 433  
 434          <h2 class="nav-tab-wrapper"><?php bp_core_admin_tabs( __( 'Manage Opt-outs', 'buddypress' ), 'tools' ); ?></h2>
 435  
 436          <p><?php echo esc_html( $helper_text ); ?></p>
 437  
 438          <ol class="bp-optouts-list">
 439          <?php foreach ( $optouts as $optout ) :    ?>
 440  
 441              <li>
 442                  <strong><?php echo esc_html( $optout->email_address ) ?></strong>
 443                  <p class="description">
 444                      <?php
 445                      $last_modified = mysql2date( 'Y/m/d g:i:s a', $optout->date_modified );
 446                      /* translators: %s: modification date */
 447                      printf( esc_html__( 'Date modified: %s', 'buddypress'), $last_modified );
 448                      ?>
 449                  </p>
 450              </li>
 451  
 452          <?php endforeach; ?>
 453          </ol>
 454  
 455          <?php if ( 'delete' === $action && count( $optouts ) ) : ?>
 456  
 457              <p><strong><?php esc_html_e( 'This action cannot be undone.', 'buddypress' ) ?></strong></p>
 458  
 459          <?php endif ; ?>
 460  
 461          <?php if ( count( $optouts ) ) : ?>
 462  
 463              <a class="button-primary" href="<?php echo esc_url( $action_url ); ?>"><?php esc_html_e( 'Confirm', 'buddypress' ); ?></a>
 464  
 465          <?php endif; ?>
 466  
 467          <a class="button" href="<?php echo esc_url( $cancel_url ); ?>"><?php esc_html_e( 'Cancel', 'buddypress' ) ?></a>
 468      </div>
 469  
 470      <?php
 471  }


Generated: Thu Oct 28 01:00:59 2021 Cross-referenced by PHPXref 0.7.1