[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> dashboard.php (source)

   1  <?php
   2  /**
   3   * WordPress Dashboard Widget Administration Screen API
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Determines whether the site has ever received a comment.
  11   *
  12   * The result is persisted so the Recent Comments widget remains registered if
  13   * all comments are later deleted, preserving its Screen Options and position.
  14   *
  15   * @since 7.2.0
  16   * @access private
  17   *
  18   * @return bool Whether the site has received a comment.
  19   */
  20  function _wp_dashboard_has_received_comments() {
  21      if ( get_option( 'dashboard_recent_comments_widget_registered' ) ) {
  22          return true;
  23      }
  24  
  25      $comment_count = wp_count_comments();
  26  
  27      if ( empty( $comment_count->all ) ) {
  28          return false;
  29      }
  30  
  31      update_option( 'dashboard_recent_comments_widget_registered', 1, false );
  32  
  33      return true;
  34  }
  35  
  36  /**
  37   * Registers dashboard widgets.
  38   *
  39   * Handles POST data, sets up filters.
  40   *
  41   * @since 2.5.0
  42   *
  43   * @global array $wp_registered_widgets
  44   * @global array $wp_registered_widget_controls
  45   * @global callable[] $wp_dashboard_control_callbacks
  46   */
  47  function wp_dashboard_setup() {
  48      global $wp_registered_widgets, $wp_registered_widget_controls, $wp_dashboard_control_callbacks;
  49  
  50      $screen = get_current_screen();
  51  
  52      /* Register Widgets and Controls */
  53      $wp_dashboard_control_callbacks = array();
  54  
  55      // Browser version
  56      $check_browser = wp_check_browser_version();
  57  
  58      if ( $check_browser && $check_browser['upgrade'] ) {
  59          add_filter( 'postbox_classes_dashboard_dashboard_browser_nag', 'dashboard_browser_nag_class' );
  60  
  61          if ( $check_browser['insecure'] ) {
  62              wp_add_dashboard_widget( 'dashboard_browser_nag', __( 'You are using an insecure browser!' ), 'wp_dashboard_browser_nag' );
  63          } else {
  64              wp_add_dashboard_widget( 'dashboard_browser_nag', __( 'Your browser is out of date!' ), 'wp_dashboard_browser_nag' );
  65          }
  66      }
  67  
  68      // PHP Version.
  69      $check_php = wp_check_php_version();
  70  
  71      if ( $check_php && current_user_can( 'update_php' ) ) {
  72          // If "not acceptable" the widget will be shown.
  73          if ( isset( $check_php['is_acceptable'] ) && ! $check_php['is_acceptable'] ) {
  74              add_filter( 'postbox_classes_dashboard_dashboard_php_nag', 'dashboard_php_nag_class' );
  75  
  76              if ( $check_php['is_lower_than_future_minimum'] ) {
  77                  wp_add_dashboard_widget( 'dashboard_php_nag', __( 'PHP Update Required' ), 'wp_dashboard_php_nag' );
  78              } else {
  79                  wp_add_dashboard_widget( 'dashboard_php_nag', __( 'PHP Update Recommended' ), 'wp_dashboard_php_nag' );
  80              }
  81          }
  82      }
  83  
  84      // Site Health.
  85      if ( current_user_can( 'view_site_health_checks' ) && ! is_network_admin() ) {
  86          if ( ! class_exists( 'WP_Site_Health' ) ) {
  87              require_once  ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
  88          }
  89  
  90          WP_Site_Health::get_instance();
  91  
  92          wp_enqueue_style( 'site-health' );
  93          wp_enqueue_script( 'site-health' );
  94  
  95          wp_add_dashboard_widget( 'dashboard_site_health', __( 'Site Health Status' ), 'wp_dashboard_site_health' );
  96      }
  97  
  98      // Right Now.
  99      if ( is_blog_admin() && current_user_can( 'edit_posts' ) ) {
 100          wp_add_dashboard_widget( 'dashboard_right_now', __( 'At a Glance' ), 'wp_dashboard_right_now' );
 101      }
 102  
 103      if ( is_network_admin() ) {
 104          wp_add_dashboard_widget( 'network_dashboard_right_now', __( 'Right Now' ), 'wp_network_dashboard_right_now' );
 105      }
 106  
 107      // Activity Widget.
 108      if ( is_blog_admin() ) {
 109          wp_add_dashboard_widget( 'dashboard_activity', __( 'Activity' ), 'wp_dashboard_site_activity' );
 110      }
 111  
 112      // Recent Comments Widget.
 113      if ( is_blog_admin() && _wp_dashboard_has_received_comments() ) {
 114          wp_add_dashboard_widget( 'dashboard_recent_comments', __( 'Recent Comments' ), 'wp_dashboard_recent_comments_widget' );
 115      }
 116  
 117      // QuickPress Widget.
 118      if ( is_blog_admin() && current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
 119          $quick_draft_title = sprintf( '<span class="hide-if-no-js">%1$s</span> <span class="hide-if-js">%2$s</span>', __( 'Quick Draft' ), __( 'Your Recent Drafts' ) );
 120          wp_add_dashboard_widget( 'dashboard_quick_press', $quick_draft_title, 'wp_dashboard_quick_press' );
 121      }
 122  
 123      // WordPress Events and News.
 124      wp_add_dashboard_widget( 'dashboard_primary', __( 'WordPress Events and News' ), 'wp_dashboard_events_news' );
 125  
 126      if ( is_network_admin() ) {
 127  
 128          /**
 129           * Fires after core widgets for the Network Admin dashboard have been registered.
 130           *
 131           * @since 3.1.0
 132           */
 133          do_action( 'wp_network_dashboard_setup' );
 134  
 135          /**
 136           * Filters the list of widgets to load for the Network Admin dashboard.
 137           *
 138           * @since 3.1.0
 139           *
 140           * @param string[] $dashboard_widgets An array of dashboard widget IDs.
 141           */
 142          $dashboard_widgets = apply_filters( 'wp_network_dashboard_widgets', array() );
 143      } elseif ( is_user_admin() ) {
 144  
 145          /**
 146           * Fires after core widgets for the User Admin dashboard have been registered.
 147           *
 148           * @since 3.1.0
 149           */
 150          do_action( 'wp_user_dashboard_setup' );
 151  
 152          /**
 153           * Filters the list of widgets to load for the User Admin dashboard.
 154           *
 155           * @since 3.1.0
 156           *
 157           * @param string[] $dashboard_widgets An array of dashboard widget IDs.
 158           */
 159          $dashboard_widgets = apply_filters( 'wp_user_dashboard_widgets', array() );
 160      } else {
 161  
 162          /**
 163           * Fires after core widgets for the admin dashboard have been registered.
 164           *
 165           * @since 2.5.0
 166           */
 167          do_action( 'wp_dashboard_setup' );
 168  
 169          /**
 170           * Filters the list of widgets to load for the admin dashboard.
 171           *
 172           * @since 2.5.0
 173           *
 174           * @param string[] $dashboard_widgets An array of dashboard widget IDs.
 175           */
 176          $dashboard_widgets = apply_filters( 'wp_dashboard_widgets', array() );
 177      }
 178  
 179      foreach ( $dashboard_widgets as $widget_id ) {
 180          $name = empty( $wp_registered_widgets[ $widget_id ]['all_link'] ) ? $wp_registered_widgets[ $widget_id ]['name'] : $wp_registered_widgets[ $widget_id ]['name'] . " <a href='{$wp_registered_widgets[$widget_id]['all_link']}' class='edit-box open-box'>" . __( 'View all' ) . '</a>';
 181          wp_add_dashboard_widget( $widget_id, $name, $wp_registered_widgets[ $widget_id ]['callback'], $wp_registered_widget_controls[ $widget_id ]['callback'] );
 182      }
 183  
 184      if ( 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['widget_id'] ) ) {
 185          check_admin_referer( 'edit-dashboard-widget_' . $_POST['widget_id'], 'dashboard-widget-nonce' );
 186          ob_start(); // Hack - but the same hack wp-admin/widgets.php uses.
 187          wp_dashboard_trigger_widget_control( $_POST['widget_id'] );
 188          ob_end_clean();
 189          wp_redirect( remove_query_arg( 'edit' ) );
 190          exit;
 191      }
 192  
 193      /** This action is documented in wp-admin/includes/meta-boxes.php */
 194      do_action( 'do_meta_boxes', $screen->id, 'normal', '' );
 195  
 196      /** This action is documented in wp-admin/includes/meta-boxes.php */
 197      do_action( 'do_meta_boxes', $screen->id, 'side', '' );
 198  }
 199  
 200  /**
 201   * Adds a new dashboard widget.
 202   *
 203   * @since 2.7.0
 204   * @since 5.6.0 The `$context` and `$priority` parameters were added.
 205   *
 206   * @global callable[] $wp_dashboard_control_callbacks
 207   *
 208   * @param string   $widget_id        Widget ID  (used in the 'id' attribute for the widget).
 209   * @param string   $widget_name      Title of the widget.
 210   * @param callable $callback         Function that fills the widget with the desired content.
 211   *                                   The function should echo its output.
 212   * @param callable $control_callback Optional. Function that outputs controls for the widget. Default null.
 213   * @param array    $callback_args    Optional. Data that should be set as the $args property of the widget array
 214   *                                   (which is the second parameter passed to your callback). Default null.
 215   * @param string   $context          Optional. The context within the screen where the box should display.
 216   *                                   Accepts 'normal', 'side', 'column3', or 'column4'. Default 'normal'.
 217   * @param string   $priority         Optional. The priority within the context where the box should show.
 218   *                                   Accepts 'high', 'core', 'default', or 'low'. Default 'core'.
 219   */
 220  function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null, $callback_args = null, $context = 'normal', $priority = 'core' ) {
 221      global $wp_dashboard_control_callbacks;
 222  
 223      $screen = get_current_screen();
 224  
 225      $private_callback_args = array( '__widget_basename' => $widget_name );
 226  
 227      if ( is_null( $callback_args ) ) {
 228          $callback_args = $private_callback_args;
 229      } elseif ( is_array( $callback_args ) ) {
 230          $callback_args = array_merge( $callback_args, $private_callback_args );
 231      }
 232  
 233      if ( $control_callback && is_callable( $control_callback ) && current_user_can( 'edit_dashboard' ) ) {
 234          $wp_dashboard_control_callbacks[ $widget_id ] = $control_callback;
 235  
 236          if ( isset( $_GET['edit'] ) && $widget_id === $_GET['edit'] ) {
 237              list($url)    = explode( '#', add_query_arg( 'edit', false ), 2 );
 238              $widget_name .= ' <span class="postbox-title-action"><a href="' . esc_url( $url ) . '">' . __( 'Cancel' ) . '</a></span>';
 239              $callback     = '_wp_dashboard_control_callback';
 240          } else {
 241              list($url)    = explode( '#', add_query_arg( 'edit', $widget_id ), 2 );
 242              $widget_name .= ' <span class="postbox-title-action"><a href="' . esc_url( "$url#$widget_id" ) . '" class="edit-box open-box">' . __( 'Configure' ) . '</a></span>';
 243          }
 244      }
 245  
 246      $side_widgets = array( 'dashboard_quick_press', 'dashboard_primary' );
 247  
 248      if ( in_array( $widget_id, $side_widgets, true ) ) {
 249          $context = 'side';
 250      }
 251  
 252      $high_priority_widgets = array( 'dashboard_browser_nag', 'dashboard_php_nag' );
 253  
 254      if ( in_array( $widget_id, $high_priority_widgets, true ) ) {
 255          $priority = 'high';
 256      }
 257  
 258      if ( empty( $context ) ) {
 259          $context = 'normal';
 260      }
 261  
 262      if ( empty( $priority ) ) {
 263          $priority = 'core';
 264      }
 265  
 266      add_meta_box( $widget_id, $widget_name, $callback, $screen, $context, $priority, $callback_args );
 267  }
 268  
 269  /**
 270   * Outputs controls for the current dashboard widget.
 271   *
 272   * @access private
 273   * @since 2.7.0
 274   *
 275   * @param mixed $dashboard
 276   * @param array $meta_box
 277   */
 278  function _wp_dashboard_control_callback( $dashboard, $meta_box ) {
 279      echo '<form method="post" class="dashboard-widget-control-form wp-clearfix">';
 280      wp_dashboard_trigger_widget_control( $meta_box['id'] );
 281      wp_nonce_field( 'edit-dashboard-widget_' . $meta_box['id'], 'dashboard-widget-nonce' );
 282      echo '<input type="hidden" name="widget_id" value="' . esc_attr( $meta_box['id'] ) . '" />';
 283      submit_button( __( 'Save Changes' ) );
 284      echo '</form>';
 285  }
 286  
 287  /**
 288   * Displays the dashboard.
 289   *
 290   * @since 2.5.0
 291   */
 292  function wp_dashboard() {
 293      $screen      = get_current_screen();
 294      $columns     = absint( $screen->get_columns() );
 295      $columns_css = '';
 296  
 297      if ( $columns ) {
 298          $columns_css = " columns-$columns";
 299      }
 300      ?>
 301  <div id="dashboard-widgets" class="metabox-holder<?php echo $columns_css; ?>">
 302      <div id="postbox-container-1" class="postbox-container">
 303      <?php do_meta_boxes( $screen->id, 'normal', '' ); ?>
 304      </div>
 305      <div id="postbox-container-2" class="postbox-container">
 306      <?php do_meta_boxes( $screen->id, 'side', '' ); ?>
 307      </div>
 308      <div id="postbox-container-3" class="postbox-container">
 309      <?php do_meta_boxes( $screen->id, 'column3', '' ); ?>
 310      </div>
 311      <div id="postbox-container-4" class="postbox-container">
 312      <?php do_meta_boxes( $screen->id, 'column4', '' ); ?>
 313      </div>
 314  </div>
 315  
 316      <?php
 317      wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
 318      wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
 319  }
 320  
 321  //
 322  // Dashboard Widgets.
 323  //
 324  
 325  /**
 326   * Dashboard widget that displays some basic stats about the site.
 327   *
 328   * Formerly 'Right Now'. A streamlined 'At a Glance' as of 3.8.
 329   *
 330   * @since 2.7.0
 331   */
 332  function wp_dashboard_right_now() {
 333      ?>
 334      <div class="main">
 335      <ul>
 336      <?php
 337      // Posts and Pages.
 338      foreach ( array( 'post', 'page' ) as $post_type ) {
 339          $num_posts = wp_count_posts( $post_type );
 340  
 341          if ( $num_posts && $num_posts->publish ) {
 342              if ( 'post' === $post_type ) {
 343                  /* translators: %s: Number of posts. */
 344                  $text = _n( '%s Published post', '%s Published posts', $num_posts->publish );
 345              } else {
 346                  /* translators: %s: Number of pages. */
 347                  $text = _n( '%s Published page', '%s Published pages', $num_posts->publish );
 348              }
 349  
 350              $text             = sprintf( $text, number_format_i18n( $num_posts->publish ) );
 351              $post_type_object = get_post_type_object( $post_type );
 352              $icon             = wp_get_icon( 'post' === $post_type ? 'core-admin/pin' : 'core-admin/page', array( 'size' => 20 ) );
 353  
 354              if ( $post_type_object && current_user_can( $post_type_object->cap->edit_posts ) ) {
 355                  $url = add_query_arg(
 356                      array(
 357                          'post_status' => 'publish',
 358                          'post_type'   => $post_type,
 359                      ),
 360                      admin_url( 'edit.php' )
 361                  );
 362                  printf( '<li class="%1$s-count"><a href="%2$s">%3$s%4$s</a></li>', $post_type, esc_url( $url ), $icon, esc_html( $text ) );
 363              } else {
 364                  printf( '<li class="%1$s-count"><span>%2$s%3$s</span></li>', $post_type, $icon, esc_html( $text ) );
 365              }
 366          }
 367      }
 368  
 369      // Comments.
 370      $num_comm = wp_count_comments();
 371  
 372      if ( $num_comm && ( $num_comm->approved || $num_comm->moderated ) ) {
 373          /* translators: %s: Number of comments. */
 374          $text = sprintf( _n( '%s Comment', '%s Comments', $num_comm->approved ), number_format_i18n( $num_comm->approved ) );
 375          ?>
 376          <li class="comment-count">
 377              <a href="edit-comments.php"><?php echo wp_get_icon( 'core-admin/comment', array( 'size' => 20 ) ); ?><span class="comments-count-text"><?php echo $text; ?></span></a>
 378          </li>
 379          <?php
 380          $moderated_comments_count_i18n = number_format_i18n( $num_comm->moderated );
 381          /* translators: %s: Number of comments. */
 382          $text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $num_comm->moderated ), $moderated_comments_count_i18n );
 383          ?>
 384          <li class="comment-mod-count<?php echo ! $num_comm->moderated ? ' hidden' : ''; ?>">
 385              <a href="edit-comments.php?comment_status=moderated"><?php echo wp_get_icon( 'core-admin/post-comments', array( 'size' => 20 ) ); ?><span class="comments-in-moderation-text"><?php echo $text; ?></span></a>
 386          </li>
 387          <?php
 388      }
 389  
 390      /**
 391       * Filters the array of extra elements to list in the 'At a Glance'
 392       * dashboard widget.
 393       *
 394       * Prior to 3.8.0, the widget was named 'Right Now'. Each element
 395       * is wrapped in list-item tags on output.
 396       *
 397       * @since 3.8.0
 398       *
 399       * @param string[] $items Array of extra 'At a Glance' widget items.
 400       */
 401      $elements = apply_filters( 'dashboard_glance_items', array() );
 402  
 403      if ( $elements ) {
 404          echo '<li>' . implode( "</li>\n<li>", $elements ) . "</li>\n";
 405      }
 406  
 407      ?>
 408      </ul>
 409      <?php
 410      update_right_now_message();
 411  
 412      // Check if search engines are asked not to index this site.
 413      if ( ! is_network_admin() && ! is_user_admin()
 414          && current_user_can( 'manage_options' ) && ! get_option( 'blog_public' )
 415      ) {
 416  
 417          /**
 418           * Filters the link title attribute for the 'Search engines discouraged'
 419           * message displayed in the 'At a Glance' dashboard widget.
 420           *
 421           * Prior to 3.8.0, the widget was named 'Right Now'.
 422           *
 423           * @since 3.0.0
 424           * @since 4.5.0 The default for `$title` was updated to an empty string.
 425           *
 426           * @param string $title Default attribute text.
 427           */
 428          $title = apply_filters( 'privacy_on_link_title', '' );
 429  
 430          /**
 431           * Filters the link label for the 'Search engines discouraged' message
 432           * displayed in the 'At a Glance' dashboard widget.
 433           *
 434           * Prior to 3.8.0, the widget was named 'Right Now'.
 435           *
 436           * @since 3.0.0
 437           *
 438           * @param string $content Default text.
 439           */
 440          $content = apply_filters( 'privacy_on_link_text', __( 'Search engines discouraged' ) );
 441  
 442          $title_attr = '' === $title ? '' : " title='$title'";
 443  
 444          $icon = wp_get_icon( 'core-admin/info', array( 'size' => 20 ) );
 445          echo "<p class='search-engines-info'><a href='options-reading.php'$title_attr>$icon$content</a></p>";
 446      }
 447      ?>
 448      </div>
 449      <?php
 450      /*
 451       * activity_box_end has a core action, but only prints content when multisite.
 452       * Using an output buffer is the only way to really check if anything's displayed here.
 453       */
 454      ob_start();
 455  
 456      /**
 457       * Fires at the end of the 'At a Glance' dashboard widget.
 458       *
 459       * Prior to 3.8.0, the widget was named 'Right Now'.
 460       *
 461       * @since 2.5.0
 462       */
 463      do_action( 'rightnow_end' );
 464  
 465      /**
 466       * Fires at the end of the 'At a Glance' dashboard widget.
 467       *
 468       * Prior to 3.8.0, the widget was named 'Right Now'.
 469       *
 470       * @since 2.0.0
 471       */
 472      do_action( 'activity_box_end' );
 473  
 474      $actions = ob_get_clean();
 475  
 476      if ( ! empty( $actions ) ) :
 477          ?>
 478      <div class="sub">
 479          <?php echo $actions; ?>
 480      </div>
 481          <?php
 482      endif;
 483  }
 484  
 485  /**
 486   * @since 3.1.0
 487   */
 488  function wp_network_dashboard_right_now() {
 489      $actions = array();
 490  
 491      if ( current_user_can( 'create_sites' ) ) {
 492          $actions['create-site'] = '<a href="' . network_admin_url( 'site-new.php' ) . '">' . __( 'Create a New Site' ) . '</a>';
 493      }
 494      if ( current_user_can( 'create_users' ) ) {
 495          $actions['create-user'] = '<a href="' . network_admin_url( 'user-new.php' ) . '">' . __( 'Create a New User' ) . '</a>';
 496      }
 497  
 498      $c_users = get_user_count();
 499      $c_blogs = get_blog_count();
 500  
 501      /* translators: %s: Number of users on the network. */
 502      $user_text = sprintf( _n( '%s user', '%s users', $c_users ), number_format_i18n( $c_users ) );
 503      /* translators: %s: Number of sites on the network. */
 504      $blog_text = sprintf( _n( '%s site', '%s sites', $c_blogs ), number_format_i18n( $c_blogs ) );
 505  
 506      /* translators: 1: Text indicating the number of sites on the network, 2: Text indicating the number of users on the network. */
 507      $sentence = sprintf( __( 'You have %1$s and %2$s.' ), $blog_text, $user_text );
 508  
 509      if ( $actions ) {
 510          echo '<ul class="subsubsub">';
 511          foreach ( $actions as $class => $action ) {
 512              $actions[ $class ] = "\t<li class='$class'>$action";
 513          }
 514          echo implode( " |</li>\n", $actions ) . "</li>\n";
 515          echo '</ul>';
 516      }
 517      ?>
 518      <br class="clear" />
 519  
 520      <p class="youhave"><?php echo $sentence; ?></p>
 521  
 522  
 523      <?php
 524          /**
 525           * Fires in the Network Admin 'Right Now' dashboard widget
 526           * just before the user and site search form fields.
 527           *
 528           * @since MU (3.0.0)
 529           */
 530          do_action( 'wpmuadminresult' );
 531      ?>
 532  
 533      <form action="<?php echo esc_url( network_admin_url( 'users.php' ) ); ?>" method="get">
 534          <p>
 535              <label class="screen-reader-text" for="search-users">
 536                  <?php
 537                  /* translators: Hidden accessibility text. */
 538                  _e( 'Search Users' );
 539                  ?>
 540              </label>
 541              <input type="search" name="s" value="" size="30" autocomplete="off" id="search-users" />
 542              <?php submit_button( __( 'Search Users' ), '', false, false, array( 'id' => 'submit_users' ) ); ?>
 543          </p>
 544      </form>
 545  
 546      <form action="<?php echo esc_url( network_admin_url( 'sites.php' ) ); ?>" method="get">
 547          <p>
 548              <label class="screen-reader-text" for="search-sites">
 549                  <?php
 550                  /* translators: Hidden accessibility text. */
 551                  _e( 'Search Sites' );
 552                  ?>
 553              </label>
 554              <input type="search" name="s" value="" size="30" autocomplete="off" id="search-sites" />
 555              <?php submit_button( __( 'Search Sites' ), '', false, false, array( 'id' => 'submit_sites' ) ); ?>
 556          </p>
 557      </form>
 558      <?php
 559      /**
 560       * Fires at the end of the 'Right Now' widget in the Network Admin dashboard.
 561       *
 562       * @since MU (3.0.0)
 563       */
 564      do_action( 'mu_rightnow_end' );
 565  
 566      /**
 567       * Fires at the end of the 'Right Now' widget in the Network Admin dashboard.
 568       *
 569       * @since MU (3.0.0)
 570       */
 571      do_action( 'mu_activity_box_end' );
 572  }
 573  
 574  /**
 575   * Displays the Quick Draft widget.
 576   *
 577   * @since 3.8.0
 578   * @since 7.1.0 Added $notice_type parameter.
 579   *
 580   * @global int $post_ID
 581   *
 582   * @param string|false $message     Optional. Error or success message. Default false.
 583   * @param string       $notice_type Optional. Admin notice type. Default 'error'.
 584   */
 585  function wp_dashboard_quick_press( $message = false, $notice_type = 'error' ) {
 586      global $post_ID;
 587  
 588      if ( ! current_user_can( 'edit_posts' ) ) {
 589          return;
 590      }
 591  
 592      // Check if a new auto-draft (= no new post_ID) is needed or if the old can be used.
 593      $last_post_id = (int) get_user_option( 'dashboard_quick_press_last_post_id' ); // Get the last post_ID.
 594  
 595      if ( $last_post_id ) {
 596          $post = get_post( $last_post_id );
 597  
 598          if ( empty( $post ) || 'auto-draft' !== $post->post_status ) { // auto-draft doesn't exist anymore.
 599              $post = get_default_post_to_edit( 'post', true );
 600              update_user_option( get_current_user_id(), 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID.
 601          } else {
 602              $post->post_title = ''; // Remove the auto draft title.
 603          }
 604      } else {
 605          $post            = get_default_post_to_edit( 'post', true );
 606          $user_id         = get_current_user_id();
 607          $user_blogs      = get_blogs_of_user( $user_id );
 608          $current_blog_id = get_current_blog_id();
 609  
 610          // Don't create an option if this is a super admin who does not belong to this site.
 611          if ( isset( $user_blogs[ $current_blog_id ] ) ) {
 612              update_user_option( $user_id, 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID.
 613          }
 614      }
 615  
 616      $post_ID = (int) $post->ID;
 617      ?>
 618  
 619      <form name="post" action="<?php echo esc_url( admin_url( 'post.php' ) ); ?>" method="post" id="quick-press" class="hide-if-no-js">
 620  
 621          <?php
 622          if ( $message ) {
 623              wp_admin_notice(
 624                  $message,
 625                  array(
 626                      'type'       => $notice_type,
 627                      'attributes' => array(
 628                          'role' => 'alert',
 629                      ),
 630                  )
 631              );
 632          }
 633          ?>
 634  
 635          <div class="input-text-wrap" id="title-wrap">
 636              <label for="title">
 637                  <?php
 638                  /** This filter is documented in wp-admin/edit-form-advanced.php */
 639                  echo apply_filters( 'enter_title_here', __( 'Title' ), $post );
 640                  ?>
 641              </label>
 642              <input type="text" name="post_title" id="title" autocomplete="off" />
 643          </div>
 644  
 645          <div class="textarea-wrap" id="description-wrap">
 646              <label for="content"><?php _e( 'Content' ); ?></label>
 647              <textarea name="content" id="content" placeholder="<?php esc_attr_e( 'What&#8217;s on your mind?' ); ?>" class="mceEditor" rows="3" cols="15" autocomplete="off"></textarea>
 648          </div>
 649  
 650          <p class="submit">
 651              <input type="hidden" name="action" id="quickpost-action" value="post-quickdraft-save" />
 652              <input type="hidden" name="post_ID" value="<?php echo $post_ID; ?>" />
 653              <input type="hidden" name="post_type" value="post" />
 654              <?php wp_nonce_field( 'add-post' ); ?>
 655              <?php submit_button( __( 'Save Draft' ), 'primary', 'save', false, array( 'id' => 'save-post' ) ); ?>
 656              <br class="clear" />
 657          </p>
 658  
 659      </form>
 660      <?php
 661      wp_dashboard_recent_drafts();
 662  }
 663  
 664  /**
 665   * Show recent drafts of the user on the dashboard.
 666   *
 667   * @since 2.7.0
 668   *
 669   * @param WP_Post[]|false $drafts Optional. Array of posts to display. Default false.
 670   */
 671  function wp_dashboard_recent_drafts( $drafts = false ) {
 672      if ( ! $drafts ) {
 673          $query_args = array(
 674              'post_type'      => 'post',
 675              'post_status'    => 'draft',
 676              'author'         => get_current_user_id(),
 677              'posts_per_page' => 4,
 678              'orderby'        => 'modified',
 679              'order'          => 'DESC',
 680          );
 681  
 682          /**
 683           * Filters the post query arguments for the 'Recent Drafts' dashboard widget.
 684           *
 685           * @since 4.4.0
 686           *
 687           * @param array $query_args The query arguments for the 'Recent Drafts' dashboard widget.
 688           */
 689          $query_args = apply_filters( 'dashboard_recent_drafts_query_args', $query_args );
 690  
 691          $drafts = get_posts( $query_args );
 692          if ( ! $drafts ) {
 693              echo '<div class="no-drafts">';
 694              echo '<p>' . __( 'There are no recent drafts.' ) . '</p>';
 695              echo '</div>';
 696              return;
 697          }
 698      }
 699  
 700      echo '<div class="drafts">';
 701  
 702      if ( count( $drafts ) > 3 ) {
 703          printf(
 704              '<p class="view-all"><a href="%s">%s</a></p>' . "\n",
 705              esc_url( admin_url( 'edit.php?post_status=draft' ) ),
 706              __( 'View all drafts' )
 707          );
 708      }
 709  
 710      echo '<h2 class="hide-if-no-js">' . __( 'Your Recent Drafts' ) . "</h2>\n";
 711      echo '<ul>';
 712  
 713      /* translators: Maximum number of words used in a preview of a draft on the dashboard. */
 714      $draft_length = (int) _x( '10', 'draft_length' );
 715  
 716      $drafts = array_slice( $drafts, 0, 3 );
 717      foreach ( $drafts as $draft ) {
 718          $url   = get_edit_post_link( $draft->ID );
 719          $title = _draft_or_post_title( $draft->ID );
 720  
 721          echo "<li>\n";
 722          printf(
 723              '<div class="draft-title"><a href="%s" aria-label="%s">%s</a><time datetime="%s">%s</time></div>',
 724              esc_url( $url ),
 725              /* translators: %s: Post title. */
 726              esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $title ) ),
 727              esc_html( $title ),
 728              get_the_time( 'c', $draft ),
 729              get_the_time( __( 'F j, Y' ), $draft )
 730          );
 731  
 732          $the_content = wp_trim_words( $draft->post_content, $draft_length );
 733  
 734          if ( $the_content ) {
 735              echo '<p class="draft-content">' . $the_content . '</p>';
 736          }
 737          echo "</li>\n";
 738      }
 739  
 740      echo "</ul>\n";
 741      echo '</div>';
 742  }
 743  
 744  /**
 745   * Outputs a row for the Recent Comments widget.
 746   *
 747   * @access private
 748   * @since 2.7.0
 749   *
 750   * @global WP_Comment $comment Global comment object.
 751   *
 752   * @param WP_Comment $comment   The current comment.
 753   * @param bool       $show_date Optional. Whether to display the date.
 754   */
 755  function _wp_dashboard_recent_comments_row( &$comment, $show_date = true ) {
 756      $GLOBALS['comment'] = clone $comment;
 757  
 758      if ( $comment->comment_post_ID > 0 ) {
 759          $comment_post_title = _draft_or_post_title( $comment->comment_post_ID );
 760          $comment_post_url   = get_the_permalink( $comment->comment_post_ID );
 761          $comment_post_link  = '<a href="' . esc_url( $comment_post_url ) . '">' . $comment_post_title . '</a>';
 762      } else {
 763          $comment_post_link = '';
 764      }
 765  
 766      $actions_string = '';
 767      if ( current_user_can( 'edit_comment', $comment->comment_ID ) ) {
 768          // Pre-order it: Approve | Reply | Edit | Spam | Trash.
 769          $actions = array(
 770              'approve'   => '',
 771              'unapprove' => '',
 772              'reply'     => '',
 773              'edit'      => '',
 774              'spam'      => '',
 775              'trash'     => '',
 776              'delete'    => '',
 777              'view'      => '',
 778          );
 779  
 780          $approve_nonce = esc_html( '_wpnonce=' . wp_create_nonce( 'approve-comment_' . $comment->comment_ID ) );
 781          $del_nonce     = esc_html( '_wpnonce=' . wp_create_nonce( 'delete-comment_' . $comment->comment_ID ) );
 782  
 783          $action_string = 'comment.php?action=%s&p=' . $comment->comment_post_ID . '&c=' . $comment->comment_ID . '&%s';
 784  
 785          $approve_url   = sprintf( $action_string, 'approvecomment', $approve_nonce );
 786          $unapprove_url = sprintf( $action_string, 'unapprovecomment', $approve_nonce );
 787          $spam_url      = sprintf( $action_string, 'spamcomment', $del_nonce );
 788          $trash_url     = sprintf( $action_string, 'trashcomment', $del_nonce );
 789          $delete_url    = sprintf( $action_string, 'deletecomment', $del_nonce );
 790  
 791          $actions['approve'] = sprintf(
 792              '<a href="%s" data-wp-lists="%s" class="vim-a aria-button-if-js" aria-label="%s">%s</a>',
 793              esc_url( $approve_url ),
 794              "dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=approved",
 795              esc_attr__( 'Approve this comment' ),
 796              __( 'Approve' )
 797          );
 798  
 799          $actions['unapprove'] = sprintf(
 800              '<a href="%s" data-wp-lists="%s" class="vim-u aria-button-if-js" aria-label="%s">%s</a>',
 801              esc_url( $unapprove_url ),
 802              "dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=unapproved",
 803              esc_attr__( 'Unapprove this comment' ),
 804              __( 'Unapprove' )
 805          );
 806  
 807          $actions['edit'] = sprintf(
 808              '<a href="%s" aria-label="%s">%s</a>',
 809              "comment.php?action=editcomment&amp;c={$comment->comment_ID}",
 810              esc_attr__( 'Edit this comment' ),
 811              __( 'Edit' )
 812          );
 813  
 814          $actions['reply'] = sprintf(
 815              '<button type="button" onclick="window.commentReply && commentReply.open(\'%s\',\'%s\');" class="vim-r button-link hide-if-no-js" aria-label="%s">%s</button>',
 816              $comment->comment_ID,
 817              $comment->comment_post_ID,
 818              esc_attr__( 'Reply to this comment' ),
 819              /* translators: Comment reply button text. */
 820              _x( 'Reply', 'verb' )
 821          );
 822  
 823          $actions['spam'] = sprintf(
 824              '<a href="%s" data-wp-lists="%s" class="vim-s vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
 825              esc_url( $spam_url ),
 826              "delete:the-comment-list:comment-{$comment->comment_ID}::spam=1",
 827              esc_attr__( 'Mark this comment as spam' ),
 828              /* translators: "Mark as spam" link. */
 829              _x( 'Spam', 'verb' )
 830          );
 831  
 832          if ( ! EMPTY_TRASH_DAYS ) {
 833              $actions['delete'] = sprintf(
 834                  '<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
 835                  esc_url( $delete_url ),
 836                  "delete:the-comment-list:comment-{$comment->comment_ID}::trash=1",
 837                  esc_attr__( 'Delete this comment permanently' ),
 838                  __( 'Delete Permanently' )
 839              );
 840          } else {
 841              $actions['trash'] = sprintf(
 842                  '<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
 843                  esc_url( $trash_url ),
 844                  "delete:the-comment-list:comment-{$comment->comment_ID}::trash=1",
 845                  esc_attr__( 'Move this comment to the Trash' ),
 846                  _x( 'Trash', 'verb' )
 847              );
 848          }
 849  
 850          $actions['view'] = sprintf(
 851              '<a class="comment-link" href="%s" aria-label="%s">%s</a>',
 852              esc_url( get_comment_link( $comment ) ),
 853              esc_attr__( 'View this comment' ),
 854              __( 'View' )
 855          );
 856  
 857          /** This filter is documented in wp-admin/includes/class-wp-comments-list-table.php */
 858          $actions = apply_filters( 'comment_row_actions', array_filter( $actions ), $comment );
 859  
 860          $i = 0;
 861  
 862          foreach ( $actions as $action => $link ) {
 863              ++$i;
 864  
 865              if ( ( ( 'approve' === $action || 'unapprove' === $action ) && 2 === $i )
 866                  || 1 === $i
 867              ) {
 868                  $separator = '';
 869              } else {
 870                  $separator = ' | ';
 871              }
 872  
 873              // Reply and quickedit need a hide-if-no-js span.
 874              if ( 'reply' === $action || 'quickedit' === $action ) {
 875                  $action .= ' hide-if-no-js';
 876              }
 877  
 878              if ( 'view' === $action && '1' !== $comment->comment_approved ) {
 879                  $action .= ' hidden';
 880              }
 881  
 882              $actions_string .= "<span class='$action'>{$separator}{$link}</span>";
 883          }
 884      }
 885      ?>
 886  
 887          <li id="comment-<?php echo $comment->comment_ID; ?>" <?php comment_class( array( 'comment-item', wp_get_comment_status( $comment ) ), $comment ); ?>>
 888  
 889              <?php
 890              $comment_row_class = '';
 891  
 892              if ( get_option( 'show_avatars' ) ) {
 893                  echo get_avatar( $comment, 50, 'mystery' );
 894                  $comment_row_class .= ' has-avatar';
 895              }
 896              ?>
 897  
 898              <?php if ( ! $comment->comment_type || 'comment' === $comment->comment_type ) : ?>
 899  
 900              <div class="dashboard-comment-wrap has-row-actions <?php echo $comment_row_class; ?>">
 901              <p class="comment-meta">
 902                  <?php
 903                  // Comments might not have a post they relate to, e.g. programmatically created ones.
 904                  if ( $comment_post_link ) {
 905                      printf(
 906                          /* translators: 1: Comment author, 2: Post link, 3: Notification if the comment is pending. */
 907                          __( 'From %1$s on %2$s %3$s' ),
 908                          '<cite class="comment-author">' . get_comment_author_link( $comment ) . '</cite>',
 909                          $comment_post_link,
 910                          '<span class="approve">' . __( '[Pending]' ) . '</span>'
 911                      );
 912                  } else {
 913                      printf(
 914                          /* translators: 1: Comment author, 2: Notification if the comment is pending. */
 915                          __( 'From %1$s %2$s' ),
 916                          '<cite class="comment-author">' . get_comment_author_link( $comment ) . '</cite>',
 917                          '<span class="approve">' . __( '[Pending]' ) . '</span>'
 918                      );
 919                  }
 920                  ?>
 921              </p>
 922  
 923                  <?php
 924              else :
 925                  switch ( $comment->comment_type ) {
 926                      case 'pingback':
 927                          $type = __( 'Pingback' );
 928                          break;
 929                      case 'trackback':
 930                          $type = __( 'Trackback' );
 931                          break;
 932                      default:
 933                          $type = ucwords( $comment->comment_type );
 934                  }
 935                  $type = esc_html( $type );
 936                  ?>
 937              <div class="dashboard-comment-wrap has-row-actions">
 938              <p class="comment-meta">
 939                  <?php
 940                  // Pingbacks, Trackbacks or custom comment types might not have a post they relate to, e.g. programmatically created ones.
 941                  if ( $comment_post_link ) {
 942                      printf(
 943                          /* translators: 1: Type of comment, 2: Post link, 3: Notification if the comment is pending. */
 944                          _x( '%1$s on %2$s %3$s', 'dashboard' ),
 945                          "<strong>$type</strong>",
 946                          $comment_post_link,
 947                          '<span class="approve">' . __( '[Pending]' ) . '</span>'
 948                      );
 949                  } else {
 950                      printf(
 951                          /* translators: 1: Type of comment, 2: Notification if the comment is pending. */
 952                          _x( '%1$s %2$s', 'dashboard' ),
 953                          "<strong>$type</strong>",
 954                          '<span class="approve">' . __( '[Pending]' ) . '</span>'
 955                      );
 956                  }
 957                  ?>
 958              </p>
 959              <p class="comment-author"><?php comment_author_link( $comment ); ?></p>
 960  
 961              <?php endif; // comment_type ?>
 962              <blockquote><p><?php comment_excerpt( $comment ); ?></p></blockquote>
 963              <?php if ( $actions_string ) : ?>
 964              <p class="row-actions"><?php echo $actions_string; ?></p>
 965              <?php endif; ?>
 966              </div>
 967          </li>
 968      <?php
 969      $GLOBALS['comment'] = null;
 970  }
 971  
 972  /**
 973   * Outputs the Activity widget.
 974   *
 975   * Callback function for {@see 'dashboard_activity'}.
 976   *
 977   * @since 3.8.0
 978   * @since 7.2.0 Added the On This Day section and moved Recent Comments to a dedicated widget.
 979   */
 980  function wp_dashboard_site_activity() {
 981  
 982      echo '<div id="activity-widget">';
 983  
 984      $on_this_day_posts = wp_dashboard_on_this_day_get_posts();
 985      $future_posts      = wp_dashboard_recent_posts(
 986          array(
 987              'max'    => 5,
 988              'status' => 'future',
 989              'order'  => 'ASC',
 990              'title'  => __( 'Publishing Soon' ),
 991              'id'     => 'future-posts',
 992          )
 993      );
 994      $recent_posts      = wp_dashboard_recent_posts(
 995          array(
 996              'max'     => 5,
 997              'status'  => 'publish',
 998              'order'   => 'DESC',
 999              'title'   => __( 'Recently Published' ),
1000              'id'      => 'published-posts',
1001              'exclude' => wp_list_pluck( $on_this_day_posts, 'ID' ),
1002          )
1003      );
1004  
1005      $on_this_day_rendered = wp_dashboard_on_this_day( $on_this_day_posts );
1006  
1007      if ( ! $future_posts && ! $recent_posts && ! $on_this_day_rendered ) {
1008          echo '<div class="no-activity">';
1009          echo '<p>' . __( 'No activity yet!' ) . '</p>';
1010          echo '</div>';
1011      }
1012  
1013      echo '</div>';
1014  }
1015  
1016  /**
1017   * Outputs the On This Day section of the Activity widget.
1018   *
1019   * @since 7.2.0
1020   *
1021   * @param WP_Post[]|null $posts Optional. Posts to render. Defaults to querying for posts.
1022   * @return bool False if no posts were found. True otherwise.
1023   */
1024  function wp_dashboard_on_this_day( $posts = null ) {
1025      if ( null === $posts ) {
1026          $posts = wp_dashboard_on_this_day_get_posts();
1027      }
1028  
1029      if ( empty( $posts ) ) {
1030          return false;
1031      }
1032  
1033      echo '<div id="on-this-day-posts" class="activity-block">';
1034      echo '<h3>' . esc_html__( 'Published On This Day' ) . '</h3>';
1035      echo '<ul>';
1036  
1037      foreach ( $posts as $post ) {
1038          /* translators: Date format for posts published in a previous year. */
1039          $date = get_the_date( __( 'M jS, Y' ), $post );
1040  
1041          $title = get_the_title( $post );
1042  
1043          if ( '' === trim( $title ) ) {
1044              $title = __( '(no title)' );
1045  
1046              if ( current_user_can( 'read_post', $post->ID ) && ! post_password_required( $post ) ) {
1047                  $excerpt = get_the_excerpt( $post );
1048  
1049                  if ( is_string( $excerpt ) && '' !== $excerpt ) {
1050                      $title .= ' ' . wp_trim_words( $excerpt, 15 );
1051                  }
1052              }
1053          }
1054  
1055          $can_edit = current_user_can( 'edit_post', $post->ID );
1056          $link     = $can_edit ? get_edit_post_link( $post->ID ) : get_permalink( $post );
1057  
1058          if ( $can_edit ) {
1059              /* translators: %s: Post title. */
1060              $aria_label = sprintf( __( 'Edit &#8220;%s&#8221;' ), $title );
1061          } else {
1062              /* translators: %s: Post title. */
1063              $aria_label = sprintf( __( 'View &#8220;%s&#8221;' ), $title );
1064          }
1065  
1066          $author_id   = (int) $post->post_author;
1067          $author_name = $author_id > 0 ? (string) get_the_author_meta( 'display_name', $author_id ) : '';
1068  
1069          printf(
1070              '<li><span>%1$s</span><span><a href="%2$s" aria-label="%3$s">%4$s</a>',
1071              esc_html( $date ),
1072              esc_url( $link ),
1073              esc_attr( $aria_label ),
1074              esc_html( $title )
1075          );
1076  
1077          if ( '' !== trim( $author_name ) && get_current_user_id() !== $author_id ) {
1078              printf(
1079                  ' <span class="wp-on-this-day-post-author">%s</span>',
1080                  esc_html(
1081                      sprintf(
1082                          /* translators: %s: Post author's display name. */
1083                          __( 'by %s' ),
1084                          $author_name
1085                      )
1086                  )
1087              );
1088          }
1089  
1090          echo '</span></li>';
1091      }
1092  
1093      echo '</ul>';
1094      echo '</div>';
1095  
1096      return true;
1097  }
1098  
1099  /**
1100   * Retrieves posts published on this calendar day in previous years.
1101   *
1102   * Up to ten posts are returned by default. Use the
1103   * {@see 'wp_dashboard_on_this_day_query_args'} filter to change the limit.
1104   * Results are cached by WP_Query's native query caching.
1105   *
1106   * @since 7.2.0
1107   *
1108   * @return WP_Post[] Array of posts ordered by newest first.
1109   */
1110  function wp_dashboard_on_this_day_get_posts() {
1111      $today = current_datetime();
1112      $year  = (int) $today->format( 'Y' );
1113  
1114      $args = array(
1115          'post_type'              => 'post',
1116          'post_status'            => array( 'publish' ),
1117          'posts_per_page'         => 10,
1118          'ignore_sticky_posts'    => true,
1119          'orderby'                => 'date',
1120          'order'                  => 'DESC',
1121          'no_found_rows'          => true,
1122          'update_post_term_cache' => false,
1123          'update_post_meta_cache' => false,
1124          'date_query'             => array(
1125              'relation' => 'AND',
1126              array(
1127                  'before' => array( 'year' => $year ),
1128              ),
1129              _wp_dashboard_on_this_day_date_query_clause( $today ),
1130          ),
1131      );
1132  
1133      /**
1134       * Filters the arguments used to query posts for the On This Day section.
1135       *
1136       * @since 7.2.0
1137       *
1138       * @param array $args WP_Query arguments.
1139       */
1140      $args = apply_filters( 'wp_dashboard_on_this_day_query_args', $args );
1141  
1142      $query = new WP_Query( $args );
1143  
1144      return $query->posts;
1145  }
1146  
1147  /**
1148   * Builds the date query clause for today's anniversary date.
1149   *
1150   * On February 28 in a non-leap year, February 29 posts are included so
1151   * leap-day anniversaries still appear.
1152   *
1153   * @since 7.2.0
1154   * @access private
1155   *
1156   * @param DateTimeInterface $date Date to build the clause for.
1157   * @return array Date query clause.
1158   */
1159  function _wp_dashboard_on_this_day_date_query_clause( $date ) {
1160      $month  = (int) $date->format( 'm' );
1161      $day    = (int) $date->format( 'd' );
1162      $clause = array(
1163          'month' => $month,
1164          'day'   => $day,
1165      );
1166  
1167      // Display leap day posts on February 28 in non-leap years.
1168      if (
1169          28 === $day
1170          && 2 === $month
1171          && false === (bool) $date->format( 'L' )
1172      ) {
1173          $clause = array(
1174              'relation' => 'OR',
1175              $clause,
1176              array(
1177                  'month' => 2,
1178                  'day'   => 29,
1179              ),
1180          );
1181      }
1182  
1183      return $clause;
1184  }
1185  
1186  /**
1187   * Generates Publishing Soon and Recently Published sections.
1188   *
1189   * @since 3.8.0
1190   *
1191   * @param array $args {
1192   *     An array of query and display arguments.
1193   *
1194   *     @type int    $max     Number of posts to display.
1195   *     @type string $status  Post status.
1196   *     @type string $order   Designates ascending ('ASC') or descending ('DESC') order.
1197   *     @type string $title   Section title.
1198   *     @type string $id      The container id.
1199   *     @type int[]  $exclude Optional. Post IDs to exclude.
1200   * }
1201   * @return bool False if no posts were found. True otherwise.
1202   */
1203  function wp_dashboard_recent_posts( $args ) {
1204      $query_args = array(
1205          'post_type'      => 'post',
1206          'post_status'    => $args['status'],
1207          'orderby'        => 'date',
1208          'order'          => $args['order'],
1209          'posts_per_page' => (int) $args['max'],
1210          'no_found_rows'  => true,
1211          'cache_results'  => true,
1212          'perm'           => ( 'future' === $args['status'] ) ? 'editable' : 'readable',
1213      );
1214  
1215      if ( ! empty( $args['exclude'] ) ) {
1216          $query_args['post__not_in'] = array_map( 'intval', $args['exclude'] );
1217      }
1218  
1219      /**
1220       * Filters the query arguments used for the Recent Posts widget.
1221       *
1222       * @since 4.2.0
1223       *
1224       * @param array $query_args The arguments passed to WP_Query to produce the list of posts.
1225       */
1226      $query_args = apply_filters( 'dashboard_recent_posts_query_args', $query_args );
1227  
1228      $posts = new WP_Query( $query_args );
1229  
1230      if ( $posts->have_posts() ) {
1231  
1232          echo '<div id="' . $args['id'] . '" class="activity-block">';
1233  
1234          echo '<h3>' . $args['title'] . '</h3>';
1235  
1236          echo '<ul>';
1237  
1238          $today    = current_time( 'Y-m-d' );
1239          $tomorrow = current_datetime()->modify( '+1 day' )->format( 'Y-m-d' );
1240          $year     = current_time( 'Y' );
1241  
1242          while ( $posts->have_posts() ) {
1243              $posts->the_post();
1244  
1245              $time = get_the_time( 'U' );
1246  
1247              if ( ! is_int( $time ) ) {
1248                  /* translators: Date and time format for recent posts on the dashboard, from a different calendar year, see https://www.php.net/manual/datetime.format.php */
1249                  $date = get_the_date( __( 'M jS Y' ) );
1250              } elseif ( gmdate( 'Y-m-d', $time ) === $today ) {
1251                  $date = __( 'Today' );
1252              } elseif ( gmdate( 'Y-m-d', $time ) === $tomorrow ) {
1253                  $date = __( 'Tomorrow' );
1254              } elseif ( gmdate( 'Y', $time ) !== $year ) {
1255                  /* translators: Date and time format for recent posts on the dashboard, from a different calendar year, see https://www.php.net/manual/datetime.format.php */
1256                  $date = date_i18n( __( 'M jS Y' ), $time );
1257              } else {
1258                  /* translators: Date and time format for recent posts on the dashboard, see https://www.php.net/manual/datetime.format.php */
1259                  $date = date_i18n( __( 'M jS' ), $time );
1260              }
1261  
1262              // Use the post edit link for those who can edit, the permalink otherwise.
1263              $recent_post_link = current_user_can( 'edit_post', get_the_ID() ) ? get_edit_post_link() : get_permalink();
1264  
1265              $draft_or_post_title = _draft_or_post_title();
1266              printf(
1267                  '<li><span>%1$s</span> <a href="%2$s" aria-label="%3$s">%4$s</a></li>',
1268                  /* translators: 1: Relative date, 2: Time. */
1269                  sprintf( _x( '%1$s, %2$s', 'dashboard' ), $date, get_the_time() ),
1270                  $recent_post_link,
1271                  /* translators: %s: Post title. */
1272                  esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $draft_or_post_title ) ),
1273                  $draft_or_post_title
1274              );
1275          }
1276  
1277          echo '</ul>';
1278          echo '</div>';
1279  
1280      } else {
1281          return false;
1282      }
1283  
1284      wp_reset_postdata();
1285  
1286      return true;
1287  }
1288  
1289  /**
1290   * Outputs the Recent Comments dashboard widget.
1291   *
1292   * Callback function for {@see 'dashboard_recent_comments'}.
1293   *
1294   * @since 7.2.0
1295   */
1296  function wp_dashboard_recent_comments_widget() {
1297      if ( ! wp_dashboard_recent_comments( 5, false ) ) {
1298          echo '<p>' . esc_html__( 'No comments to show.' ) . '</p>';
1299      }
1300  }
1301  
1302  /**
1303   * Outputs recent comments.
1304   *
1305   * @since 3.8.0
1306   * @since 7.2.0 Moved from the Activity widget to a dedicated dashboard widget.
1307   *
1308   * @param int  $total_items Optional. Number of comments to query. Default 5.
1309   * @param bool $show_title  Optional. Whether to display the section title. Default true.
1310   * @return bool False if no comments were found. True otherwise.
1311   */
1312  function wp_dashboard_recent_comments( $total_items = 5, $show_title = true ) {
1313      // Select all comment types and filter out spam later for better query performance.
1314      $comments = array();
1315  
1316      $comments_query = array(
1317          'number' => $total_items * 5,
1318          'offset' => 0,
1319      );
1320  
1321      if ( ! current_user_can( 'edit_posts' ) ) {
1322          $comments_query['status'] = 'approve';
1323      }
1324  
1325      $comments_count = 0;
1326      do {
1327          $possible = get_comments( $comments_query );
1328  
1329          if ( empty( $possible ) || ! is_array( $possible ) ) {
1330              break;
1331          }
1332  
1333          foreach ( $possible as $comment ) {
1334              if ( ! current_user_can( 'edit_post', $comment->comment_post_ID )
1335                  && ( post_password_required( $comment->comment_post_ID )
1336                      || ! current_user_can( 'read_post', $comment->comment_post_ID ) )
1337              ) {
1338                  // The user has no access to the post and thus cannot see the comments.
1339                  continue;
1340              }
1341  
1342              $comments[]     = $comment;
1343              $comments_count = count( $comments );
1344  
1345              if ( $comments_count === $total_items ) {
1346                  break 2;
1347              }
1348          }
1349  
1350          $comments_query['offset'] += $comments_query['number'];
1351          $comments_query['number']  = $total_items * 10;
1352      } while ( $comments_count < $total_items );
1353  
1354      if ( $comments ) {
1355          echo '<div id="latest-comments" class="activity-block table-view-list">';
1356  
1357          if ( $show_title ) {
1358              echo '<h3>' . esc_html__( 'Recent Comments' ) . '</h3>';
1359          }
1360  
1361          echo '<ul id="the-comment-list" data-wp-lists="list:comment">';
1362          foreach ( $comments as $comment ) {
1363              _wp_dashboard_recent_comments_row( $comment );
1364          }
1365          echo '</ul>';
1366  
1367          if ( current_user_can( 'edit_posts' ) ) {
1368              echo '<h3 class="screen-reader-text">' .
1369                  /* translators: Hidden accessibility text. */
1370                  __( 'View more comments' ) .
1371              '</h3>';
1372              _get_list_table( 'WP_Comments_List_Table' )->views();
1373          }
1374  
1375          wp_comment_reply( -1, false, 'dashboard', false );
1376          wp_comment_trashnotice();
1377  
1378          echo '</div>';
1379      } else {
1380          return false;
1381      }
1382      return true;
1383  }
1384  
1385  /**
1386   * Display generic dashboard RSS widget feed.
1387   *
1388   * @since 2.5.0
1389   *
1390   * @param string $widget_id
1391   */
1392  function wp_dashboard_rss_output( $widget_id ) {
1393      $widgets = get_option( 'dashboard_widget_options' );
1394      echo '<div class="rss-widget">';
1395      wp_widget_rss_output( $widgets[ $widget_id ] );
1396      echo '</div>';
1397  }
1398  
1399  /**
1400   * Checks to see if all of the feed url in $check_urls are cached.
1401   *
1402   * If $check_urls is empty, look for the rss feed url found in the dashboard
1403   * widget options of $widget_id. If cached, call $callback, a function that
1404   * echoes out output for this widget. If not cache, echo a "Loading..." stub
1405   * which is later replaced by Ajax call (see top of /wp-admin/index.php)
1406   *
1407   * @since 2.5.0
1408   * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
1409   *              by adding it to the function signature.
1410   *
1411   * @param string   $widget_id  The widget ID.
1412   * @param callable $callback   The callback function used to display each feed.
1413   * @param array    $check_urls RSS feeds.
1414   * @param mixed    ...$args    Optional additional parameters to pass to the callback function.
1415   * @return bool True on success, false on failure.
1416   */
1417  function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array(), ...$args ) {
1418      $doing_ajax = wp_doing_ajax();
1419      $loading    = '<p class="widget-loading hide-if-no-js">' . __( 'Loading&hellip;' ) . '</p>';
1420      $loading   .= wp_get_admin_notice(
1421          __( 'This widget requires JavaScript.' ),
1422          array(
1423              'type'               => 'error',
1424              'additional_classes' => array( 'inline', 'hide-if-js' ),
1425          )
1426      );
1427  
1428      if ( empty( $check_urls ) ) {
1429          $widgets = get_option( 'dashboard_widget_options' );
1430  
1431          if ( empty( $widgets[ $widget_id ]['url'] ) && ! $doing_ajax ) {
1432              echo $loading;
1433              return false;
1434          }
1435  
1436          $check_urls = array( $widgets[ $widget_id ]['url'] );
1437      }
1438  
1439      $locale    = get_user_locale();
1440      $cache_key = 'dash_v2_' . md5( $widget_id . '_' . $locale );
1441      $output    = get_transient( $cache_key );
1442  
1443      if ( false !== $output ) {
1444          echo $output;
1445          return true;
1446      }
1447  
1448      if ( ! $doing_ajax ) {
1449          echo $loading;
1450          return false;
1451      }
1452  
1453      if ( $callback && is_callable( $callback ) ) {
1454          array_unshift( $args, $widget_id, $check_urls );
1455          ob_start();
1456          call_user_func_array( $callback, $args );
1457          // Default lifetime in cache of 12 hours (same as the feeds).
1458          set_transient( $cache_key, ob_get_flush(), 12 * HOUR_IN_SECONDS );
1459      }
1460  
1461      return true;
1462  }
1463  
1464  //
1465  // Dashboard Widgets Controls.
1466  //
1467  
1468  /**
1469   * Calls widget control callback.
1470   *
1471   * @since 2.5.0
1472   *
1473   * @global callable[] $wp_dashboard_control_callbacks
1474   *
1475   * @param int|false $widget_control_id Optional. Registered widget ID. Default false.
1476   */
1477  function wp_dashboard_trigger_widget_control( $widget_control_id = false ) {
1478      global $wp_dashboard_control_callbacks;
1479  
1480      if ( is_scalar( $widget_control_id ) && $widget_control_id
1481          && isset( $wp_dashboard_control_callbacks[ $widget_control_id ] )
1482          && is_callable( $wp_dashboard_control_callbacks[ $widget_control_id ] )
1483      ) {
1484          call_user_func(
1485              $wp_dashboard_control_callbacks[ $widget_control_id ],
1486              '',
1487              array(
1488                  'id'       => $widget_control_id,
1489                  'callback' => $wp_dashboard_control_callbacks[ $widget_control_id ],
1490              )
1491          );
1492      }
1493  }
1494  
1495  /**
1496   * Sets up the RSS dashboard widget control and $args to be used as input to wp_widget_rss_form().
1497   *
1498   * Handles POST data from RSS-type widgets.
1499   *
1500   * @since 2.5.0
1501   *
1502   * @param string $widget_id
1503   * @param array  $form_inputs
1504   */
1505  function wp_dashboard_rss_control( $widget_id, $form_inputs = array() ) {
1506      $widget_options = get_option( 'dashboard_widget_options' );
1507  
1508      if ( ! $widget_options ) {
1509          $widget_options = array();
1510      }
1511  
1512      if ( ! isset( $widget_options[ $widget_id ] ) ) {
1513          $widget_options[ $widget_id ] = array();
1514      }
1515  
1516      $number = 1; // Hack to use wp_widget_rss_form().
1517  
1518      $widget_options[ $widget_id ]['number'] = $number;
1519  
1520      if ( 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['widget-rss'][ $number ] ) ) {
1521          $_POST['widget-rss'][ $number ]         = wp_unslash( $_POST['widget-rss'][ $number ] );
1522          $widget_options[ $widget_id ]           = wp_widget_rss_process( $_POST['widget-rss'][ $number ] );
1523          $widget_options[ $widget_id ]['number'] = $number;
1524  
1525          // Title is optional. If blank, fill it if possible.
1526          if ( ! $widget_options[ $widget_id ]['title'] && isset( $_POST['widget-rss'][ $number ]['title'] ) ) {
1527              $rss = fetch_feed( $widget_options[ $widget_id ]['url'] );
1528              if ( is_wp_error( $rss ) ) {
1529                  $widget_options[ $widget_id ]['title'] = htmlentities( __( 'Unknown Feed' ) );
1530              } else {
1531                  $widget_options[ $widget_id ]['title'] = htmlentities( strip_tags( $rss->get_title() ) );
1532                  $rss->__destruct();
1533                  unset( $rss );
1534              }
1535          }
1536  
1537          update_option( 'dashboard_widget_options', $widget_options, false );
1538  
1539          $locale    = get_user_locale();
1540          $cache_key = 'dash_v2_' . md5( $widget_id . '_' . $locale );
1541          delete_transient( $cache_key );
1542      }
1543  
1544      wp_widget_rss_form( $widget_options[ $widget_id ], $form_inputs );
1545  }
1546  
1547  
1548  /**
1549   * Renders the Events and News dashboard widget.
1550   *
1551   * @since 4.8.0
1552   */
1553  function wp_dashboard_events_news() {
1554      wp_print_community_events_markup();
1555  
1556      ?>
1557  
1558      <div class="wordpress-news hide-if-no-js">
1559          <?php wp_dashboard_primary(); ?>
1560      </div>
1561  
1562      <p class="community-events-footer">
1563          <?php
1564              printf(
1565                  '<a href="%1$s" target="_blank">%2$s <span class="screen-reader-text"> %3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>',
1566                  'https://make.wordpress.org/community/meetups-landing-page',
1567                  __( 'Meetups' ),
1568                  /* translators: Hidden accessibility text. */
1569                  __( '(opens in a new tab)' )
1570              );
1571          ?>
1572  
1573          |
1574  
1575          <?php
1576              printf(
1577                  '<a href="%1$s" target="_blank">%2$s <span class="screen-reader-text"> %3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>',
1578                  'https://central.wordcamp.org/schedule/',
1579                  __( 'WordCamps' ),
1580                  /* translators: Hidden accessibility text. */
1581                  __( '(opens in a new tab)' )
1582              );
1583          ?>
1584  
1585          |
1586  
1587          <?php
1588              printf(
1589                  '<a href="%1$s" target="_blank">%2$s <span class="screen-reader-text"> %3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>',
1590                  /* translators: If a Rosetta site exists (e.g. https://es.wordpress.org/news/), then use that. Otherwise, leave untranslated. */
1591                  esc_url( _x( 'https://wordpress.org/news/', 'Events and News dashboard widget' ) ),
1592                  __( 'News' ),
1593                  /* translators: Hidden accessibility text. */
1594                  __( '(opens in a new tab)' )
1595              );
1596          ?>
1597      </p>
1598  
1599      <?php
1600  }
1601  
1602  /**
1603   * Prints the markup for the Community Events section of the Events and News Dashboard widget.
1604   *
1605   * @since 4.8.0
1606   */
1607  function wp_print_community_events_markup() {
1608      $community_events_notice  = '<p class="hide-if-js">' . __( 'The WordPress Events and News feeds require JavaScript.' ) . '</p>';
1609      $community_events_notice .= '<p class="community-events-error-occurred" aria-hidden="true">' . __( 'An error occurred. Please try again.' ) . '</p>';
1610      $community_events_notice .= '<p class="community-events-could-not-locate" aria-hidden="true"></p>';
1611  
1612      wp_admin_notice(
1613          $community_events_notice,
1614          array(
1615              'type'               => 'error',
1616              'additional_classes' => array( 'community-events-errors', 'inline', 'hide-if-js' ),
1617              'paragraph_wrap'     => false,
1618          )
1619      );
1620  
1621      /*
1622       * Hide the main element when the page first loads, because the content
1623       * won't be ready until wp.communityEvents.renderEventsTemplate() has run.
1624       */
1625      ?>
1626      <div id="community-events" class="community-events" aria-hidden="true">
1627          <div class="activity-block">
1628              <p>
1629                  <span id="community-events-location-message"></span>
1630  
1631                  <button class="button-link community-events-toggle-location" aria-expanded="false">
1632                      <span class="dashicons dashicons-location" aria-hidden="true"></span>
1633                      <span class="community-events-location-edit"><?php _e( 'Select location' ); ?></span>
1634                  </button>
1635              </p>
1636  
1637              <form class="community-events-form" aria-hidden="true" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" method="post">
1638                  <label for="community-events-location">
1639                      <?php _e( 'City:' ); ?>
1640                  </label>
1641                  <?php
1642                  /* translators: Replace with a city related to your locale.
1643                   * Test that it matches the expected location and has upcoming
1644                   * events before including it. If no cities related to your
1645                   * locale have events, then use a city related to your locale
1646                   * that would be recognizable to most users. Use only the city
1647                   * name itself, without any region or country. Use the endonym
1648                   * (native locale name) instead of the English name if possible.
1649                   */
1650                  ?>
1651                  <input id="community-events-location" class="regular-text" type="text" name="community-events-location" placeholder="<?php esc_attr_e( 'Cincinnati' ); ?>" />
1652  
1653                  <?php submit_button( __( 'Submit' ), 'secondary', 'community-events-submit', false ); ?>
1654  
1655                  <button class="community-events-cancel button-link" type="button" aria-expanded="false">
1656                      <?php _e( 'Cancel' ); ?>
1657                  </button>
1658  
1659                  <span class="spinner"></span>
1660              </form>
1661          </div>
1662  
1663          <ul class="community-events-results activity-block last"></ul>
1664      </div>
1665  
1666      <?php
1667  }
1668  
1669  /**
1670   * Renders the events templates for the Event and News widget.
1671   *
1672   * @since 4.8.0
1673   */
1674  function wp_print_community_events_templates() {
1675      ?>
1676  
1677      <script id="tmpl-community-events-attend-event-near" type="text/template">
1678          <?php
1679          printf(
1680              /* translators: %s: The name of a city. */
1681              __( 'Attend an upcoming event near %s.' ),
1682              '<strong>{{ data.location.description }}</strong>'
1683          );
1684          ?>
1685      </script>
1686  
1687      <script id="tmpl-community-events-could-not-locate" type="text/template">
1688          <?php
1689          printf(
1690              /* translators: %s is the name of the city we couldn't locate.
1691               * Replace the examples with cities in your locale, but test
1692               * that they match the expected location before including them.
1693               * Use endonyms (native locale names) whenever possible.
1694               */
1695              __( '%s could not be located. Please try another nearby city. For example: Kansas City; Springfield; Portland.' ),
1696              '<em>{{data.unknownCity}}</em>'
1697          );
1698          ?>
1699      </script>
1700  
1701      <script id="tmpl-community-events-event-list" type="text/template">
1702          <# _.each( data.events, function( event ) { #>
1703              <li class="event event-{{ event.type }} wp-clearfix">
1704                  <div class="event-info">
1705                      <div class="dashicons event-icon" aria-hidden="true"></div>
1706                      <div class="event-info-inner">
1707                          <a class="event-title" href="{{ event.url }}">{{ event.title }}</a>
1708                          <# if ( event.type ) {
1709                              const titleCaseEventType = event.type.replace(
1710                                  /\w\S*/g,
1711                                  function ( type ) { return type.charAt(0).toUpperCase() + type.substr(1).toLowerCase(); }
1712                              );
1713                          #>
1714                              {{ 'wordcamp' === event.type ? 'WordCamp' : titleCaseEventType }}
1715                              <span class="ce-separator" aria-hidden="true"></span>
1716                          <# } #>
1717                          <span class="event-city">{{ event.location.location }}</span>
1718                      </div>
1719                  </div>
1720  
1721                  <div class="event-date-time">
1722                      <span class="event-date">{{ event.user_formatted_date }}</span>
1723                      <# if ( 'meetup' === event.type ) { #>
1724                          <span class="event-time">
1725                              {{ event.user_formatted_time }} {{ event.timeZoneAbbreviation }}
1726                          </span>
1727                      <# } #>
1728                  </div>
1729              </li>
1730          <# } ) #>
1731  
1732          <# if ( data.events.length <= 2 ) { #>
1733              <li class="event-none">
1734                  <?php
1735                  printf(
1736                      /* translators: %s: Localized meetup organization documentation URL. */
1737                      __( 'Want more events? <a href="%s">Help organize the next one</a>!' ),
1738                      __( 'https://make.wordpress.org/community/organize-event-landing-page/' )
1739                  );
1740                  ?>
1741              </li>
1742          <# } #>
1743  
1744      </script>
1745  
1746      <script id="tmpl-community-events-no-upcoming-events" type="text/template">
1747          <li class="event-none">
1748              <# if ( data.location.description ) { #>
1749                  <?php
1750                  printf(
1751                      /* translators: 1: The city the user searched for, 2: Meetup organization documentation URL. */
1752                      __( 'There are no events scheduled near %1$s at the moment. Would you like to <a href="%2$s">organize a WordPress event</a>?' ),
1753                      '{{ data.location.description }}',
1754                      __( 'https://make.wordpress.org/community/handbook/meetup-organizer/welcome/' )
1755                  );
1756                  ?>
1757  
1758              <# } else { #>
1759                  <?php
1760                  printf(
1761                      /* translators: %s: Meetup organization documentation URL. */
1762                      __( 'There are no events scheduled near you at the moment. Would you like to <a href="%s">organize a WordPress event</a>?' ),
1763                      __( 'https://make.wordpress.org/community/handbook/meetup-organizer/welcome/' )
1764                  );
1765                  ?>
1766              <# } #>
1767          </li>
1768      </script>
1769      <?php
1770  }
1771  
1772  /**
1773   * 'WordPress Events and News' dashboard widget.
1774   *
1775   * @since 2.7.0
1776   * @since 4.8.0 Removed popular plugins feed.
1777   */
1778  function wp_dashboard_primary() {
1779      $feeds = array(
1780          'news'   => array(
1781  
1782              /**
1783               * Filters the primary link URL for the 'WordPress Events and News' dashboard widget.
1784               *
1785               * @since 2.5.0
1786               *
1787               * @param string $link The widget's primary link URL.
1788               */
1789              'link'         => apply_filters( 'dashboard_primary_link', __( 'https://wordpress.org/news/' ) ),
1790  
1791              /**
1792               * Filters the primary feed URL for the 'WordPress Events and News' dashboard widget.
1793               *
1794               * @since 2.3.0
1795               *
1796               * @param string $url The widget's primary feed URL.
1797               */
1798              'url'          => apply_filters( 'dashboard_primary_feed', __( 'https://wordpress.org/news/feed/' ) ),
1799  
1800              /**
1801               * Filters the primary link title for the 'WordPress Events and News' dashboard widget.
1802               *
1803               * @since 2.3.0
1804               *
1805               * @param string $title Title attribute for the widget's primary link.
1806               */
1807              'title'        => apply_filters( 'dashboard_primary_title', __( 'WordPress Blog' ) ),
1808              'items'        => 2,
1809              'show_summary' => 0,
1810              'show_author'  => 0,
1811              'show_date'    => 0,
1812          ),
1813          'planet' => array(
1814  
1815              /**
1816               * Filters the secondary link URL for the 'WordPress Events and News' dashboard widget.
1817               *
1818               * @since 2.3.0
1819               *
1820               * @param string $link The widget's secondary link URL.
1821               */
1822              'link'         => apply_filters(
1823                  'dashboard_secondary_link',
1824                  /* translators: Link to the Planet website of the locale. */
1825                  __( 'https://planet.wordpress.org/' )
1826              ),
1827  
1828              /**
1829               * Filters the secondary feed URL for the 'WordPress Events and News' dashboard widget.
1830               *
1831               * @since 2.3.0
1832               *
1833               * @param string $url The widget's secondary feed URL.
1834               */
1835              'url'          => apply_filters(
1836                  'dashboard_secondary_feed',
1837                  /* translators: Link to the Planet feed of the locale. */
1838                  __( 'https://planet.wordpress.org/feed/' )
1839              ),
1840  
1841              /**
1842               * Filters the secondary link title for the 'WordPress Events and News' dashboard widget.
1843               *
1844               * @since 2.3.0
1845               *
1846               * @param string $title Title attribute for the widget's secondary link.
1847               */
1848              'title'        => apply_filters( 'dashboard_secondary_title', __( 'Other WordPress News' ) ),
1849  
1850              /**
1851               * Filters the number of secondary link items for the 'WordPress Events and News' dashboard widget.
1852               *
1853               * @since 4.4.0
1854               *
1855               * @param string $items How many items to show in the secondary feed.
1856               */
1857              'items'        => apply_filters( 'dashboard_secondary_items', 3 ),
1858              'show_summary' => 0,
1859              'show_author'  => 0,
1860              'show_date'    => 0,
1861          ),
1862      );
1863  
1864      wp_dashboard_cached_rss_widget( 'dashboard_primary', 'wp_dashboard_primary_output', $feeds );
1865  }
1866  
1867  /**
1868   * Displays the WordPress events and news feeds.
1869   *
1870   * @since 3.8.0
1871   * @since 4.8.0 Removed popular plugins feed.
1872   *
1873   * @param string $widget_id Widget ID.
1874   * @param array  $feeds     Array of RSS feeds.
1875   */
1876  function wp_dashboard_primary_output( $widget_id, $feeds ) {
1877      foreach ( $feeds as $type => $args ) {
1878          $args['type'] = $type;
1879          echo '<div class="rss-widget">';
1880              wp_widget_rss_output( $args['url'], $args );
1881          echo '</div>';
1882      }
1883  }
1884  
1885  /**
1886   * Displays file upload quota on dashboard.
1887   *
1888   * Runs on the {@see 'activity_box_end'} hook in wp_dashboard_right_now().
1889   *
1890   * @since 3.0.0
1891   *
1892   * @return true|null True if not multisite, user can't upload files, or the space check option is disabled.
1893   */
1894  function wp_dashboard_quota() {
1895      if ( ! is_multisite() || ! current_user_can( 'upload_files' )
1896          || get_site_option( 'upload_space_check_disabled' )
1897      ) {
1898          return true;
1899      }
1900  
1901      $quota = get_space_allowed();
1902      $used  = get_space_used();
1903  
1904      if ( $used > $quota ) {
1905          $percentused = '100';
1906      } else {
1907          $percentused = ( $used / $quota ) * 100;
1908      }
1909  
1910      $used_class  = ( $percentused >= 70 ) ? ' warning' : '';
1911      $used        = round( $used, 2 );
1912      $percentused = number_format( $percentused );
1913  
1914      ?>
1915      <h3 class="mu-storage"><?php _e( 'Storage Space' ); ?></h3>
1916      <div class="mu-storage">
1917      <ul>
1918          <li class="storage-count">
1919              <?php
1920              $text = sprintf(
1921                  /* translators: %s: Number of megabytes. */
1922                  __( '%s MB Space Allowed' ),
1923                  number_format_i18n( $quota )
1924              );
1925              printf(
1926                  '<a href="%1$s">%2$s%3$s<span class="screen-reader-text"> (%4$s)</span></a>',
1927                  esc_url( admin_url( 'upload.php' ) ),
1928                  wp_get_icon( 'core-admin/media', array( 'size' => 20 ) ),
1929                  $text,
1930                  /* translators: Hidden accessibility text. */
1931                  __( 'Manage Uploads' )
1932              );
1933              ?>
1934          </li><li class="storage-count <?php echo $used_class; ?>">
1935              <?php
1936              $text = sprintf(
1937                  /* translators: 1: Number of megabytes, 2: Percentage. */
1938                  __( '%1$s MB (%2$s%%) Space Used' ),
1939                  number_format_i18n( $used, 2 ),
1940                  $percentused
1941              );
1942              printf(
1943                  '<a href="%1$s" class="musublink">%2$s%3$s<span class="screen-reader-text"> (%4$s)</span></a>',
1944                  esc_url( admin_url( 'upload.php' ) ),
1945                  wp_get_icon( $used_class ? 'core-admin/error' : 'core-admin/media', array( 'size' => 20 ) ),
1946                  $text,
1947                  /* translators: Hidden accessibility text. */
1948                  __( 'Manage Uploads' )
1949              );
1950              ?>
1951          </li>
1952      </ul>
1953      </div>
1954      <?php
1955      return null;
1956  }
1957  
1958  /**
1959   * Displays the browser update nag.
1960   *
1961   * @since 3.2.0
1962   * @since 5.8.0 Added a special message for Internet Explorer users.
1963   *
1964   * @global bool $is_IE
1965   */
1966  function wp_dashboard_browser_nag() {
1967      global $is_IE;
1968  
1969      $notice   = '';
1970      $response = wp_check_browser_version();
1971  
1972      if ( $response ) {
1973          if ( $is_IE ) {
1974              $msg = __( 'Internet Explorer does not give you the best WordPress experience. Switch to Microsoft Edge, or another more modern browser to get the most from your site.' );
1975          } elseif ( $response['insecure'] ) {
1976              $msg = sprintf(
1977                  /* translators: %s: Browser name and link. */
1978                  __( "It looks like you're using an insecure version of %s. Using an outdated browser makes your computer unsafe. For the best WordPress experience, please update your browser." ),
1979                  sprintf( '<a href="%s">%s</a>', esc_url( $response['update_url'] ), esc_html( $response['name'] ) )
1980              );
1981          } else {
1982              $msg = sprintf(
1983                  /* translators: %s: Browser name and link. */
1984                  __( "It looks like you're using an old version of %s. For the best WordPress experience, please update your browser." ),
1985                  sprintf( '<a href="%s">%s</a>', esc_url( $response['update_url'] ), esc_html( $response['name'] ) )
1986              );
1987          }
1988  
1989          $browser_nag_class = '';
1990          if ( ! empty( $response['img_src'] ) ) {
1991              $img_src = ( is_ssl() && ! empty( $response['img_src_ssl'] ) ) ? $response['img_src_ssl'] : $response['img_src'];
1992  
1993              $notice           .= '<div class="alignright browser-icon"><img src="' . esc_url( $img_src ) . '" alt="" /></div>';
1994              $browser_nag_class = ' has-browser-icon';
1995          }
1996          $notice .= "<p class='browser-update-nag{$browser_nag_class}'>{$msg}</p>";
1997  
1998          $browsehappy = 'https://browsehappy.com/';
1999          $locale      = get_user_locale();
2000          if ( 'en_US' !== $locale ) {
2001              $browsehappy = add_query_arg( 'locale', $locale, $browsehappy );
2002          }
2003  
2004          if ( $is_IE ) {
2005              $msg_browsehappy = sprintf(
2006                  /* translators: %s: Browse Happy URL. */
2007                  __( 'Learn how to <a href="%s" class="update-browser-link">browse happy</a>' ),
2008                  esc_url( $browsehappy )
2009              );
2010          } else {
2011              $msg_browsehappy = sprintf(
2012                  /* translators: 1: Browser update URL, 2: Browser name, 3: Browse Happy URL. */
2013                  __( '<a href="%1$s" class="update-browser-link">Update %2$s</a> or learn how to <a href="%3$s" class="browse-happy-link">browse happy</a>' ),
2014                  esc_attr( $response['update_url'] ),
2015                  esc_html( $response['name'] ),
2016                  esc_url( $browsehappy )
2017              );
2018          }
2019  
2020          $notice .= '<p>' . $msg_browsehappy . '</p>';
2021          $notice .= '<p class="hide-if-no-js"><a href="" class="dismiss" aria-label="' . esc_attr__( 'Dismiss the browser warning panel' ) . '">' . __( 'Dismiss' ) . '</a></p>';
2022          $notice .= '<div class="clear"></div>';
2023      }
2024  
2025      /**
2026       * Filters the notice output for the 'Browse Happy' nag meta box.
2027       *
2028       * @since 3.2.0
2029       *
2030       * @param string      $notice   The notice content.
2031       * @param array|false $response An array containing web browser information, or
2032       *                              false on failure. See wp_check_browser_version().
2033       */
2034      echo apply_filters( 'browse-happy-notice', $notice, $response ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
2035  }
2036  
2037  /**
2038   * Adds an additional class to the browser nag if the current version is insecure.
2039   *
2040   * @since 3.2.0
2041   *
2042   * @param string[] $classes Array of meta box classes.
2043   * @return string[] Modified array of meta box classes.
2044   */
2045  function dashboard_browser_nag_class( $classes ) {
2046      $response = wp_check_browser_version();
2047  
2048      if ( $response && $response['insecure'] ) {
2049          $classes[] = 'browser-insecure';
2050      }
2051  
2052      return $classes;
2053  }
2054  
2055  /**
2056   * Checks if the user needs a browser update.
2057   *
2058   * @since 3.2.0
2059   *
2060   * @return array|false Array of browser data on success, false on failure.
2061   */
2062  function wp_check_browser_version() {
2063      if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
2064          return false;
2065      }
2066  
2067      $key = md5( $_SERVER['HTTP_USER_AGENT'] );
2068  
2069      $response = get_site_transient( 'browser_' . $key );
2070  
2071      if ( false === $response ) {
2072          $url     = 'http://api.wordpress.org/core/browse-happy/1.1/';
2073          $options = array(
2074              'body'       => array( 'useragent' => $_SERVER['HTTP_USER_AGENT'] ),
2075              'user-agent' => 'WordPress/' . wp_get_wp_version() . '; ' . home_url( '/' ),
2076          );
2077  
2078          if ( wp_http_supports( array( 'ssl' ) ) ) {
2079              $url = set_url_scheme( $url, 'https' );
2080          }
2081  
2082          $response = wp_remote_post( $url, $options );
2083  
2084          if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
2085              return false;
2086          }
2087  
2088          /**
2089           * Response should be an array with:
2090           *  'platform' - string - A user-friendly platform name, if it can be determined
2091           *  'name' - string - A user-friendly browser name
2092           *  'version' - string - The version of the browser the user is using
2093           *  'current_version' - string - The most recent version of the browser
2094           *  'upgrade' - boolean - Whether the browser needs an upgrade
2095           *  'insecure' - boolean - Whether the browser is deemed insecure
2096           *  'update_url' - string - The url to visit to upgrade
2097           *  'img_src' - string - An image representing the browser
2098           *  'img_src_ssl' - string - An image (over SSL) representing the browser
2099           */
2100          $response = json_decode( wp_remote_retrieve_body( $response ), true );
2101  
2102          if ( ! is_array( $response ) ) {
2103              return false;
2104          }
2105  
2106          set_site_transient( 'browser_' . $key, $response, WEEK_IN_SECONDS );
2107      }
2108  
2109      return $response;
2110  }
2111  
2112  /**
2113   * Displays the PHP update nag.
2114   *
2115   * @since 5.1.0
2116   */
2117  function wp_dashboard_php_nag() {
2118      $response = wp_check_php_version();
2119  
2120      if ( ! $response ) {
2121          return;
2122      }
2123  
2124      if ( isset( $response['is_secure'] ) && ! $response['is_secure'] ) {
2125          // The `is_secure` array key name doesn't actually imply this is a secure version of PHP. It only means it receives security updates.
2126  
2127          if ( $response['is_lower_than_future_minimum'] ) {
2128              $message = sprintf(
2129                  /* translators: %s: The server PHP version. */
2130                  __( 'Your site is running on an outdated version of PHP (%s), which does not receive security updates and soon will not be supported by WordPress. Ensure that PHP is updated on your server as soon as possible. Otherwise you will not be able to upgrade WordPress.' ),
2131                  PHP_VERSION
2132              );
2133          } else {
2134              $message = sprintf(
2135                  /* translators: %s: The server PHP version. */
2136                  __( 'Your site is running on an outdated version of PHP (%s), which does not receive security updates. It should be updated.' ),
2137                  PHP_VERSION
2138              );
2139          }
2140      } elseif ( $response['is_lower_than_future_minimum'] ) {
2141          $message = sprintf(
2142              /* translators: %s: The server PHP version. */
2143              __( 'Your site is running on an outdated version of PHP (%s), which soon will not be supported by WordPress. Ensure that PHP is updated on your server as soon as possible. Otherwise you will not be able to upgrade WordPress.' ),
2144              PHP_VERSION
2145          );
2146      } else {
2147          $message = sprintf(
2148              /* translators: %s: The server PHP version. */
2149              __( 'Your site is running on an outdated version of PHP (%s), which should be updated.' ),
2150              PHP_VERSION
2151          );
2152      }
2153      ?>
2154      <p class="bigger-bolder-text"><?php echo $message; ?></p>
2155  
2156      <p><?php _e( 'What is PHP and how does it affect my site?' ); ?></p>
2157      <p>
2158          <?php _e( 'PHP is one of the programming languages used to build WordPress. Newer versions of PHP receive regular security updates and may increase your site&#8217;s performance.' ); ?>
2159          <?php
2160          if ( ! empty( $response['recommended_version'] ) ) {
2161              printf(
2162                  /* translators: %s: The minimum recommended PHP version. */
2163                  __( 'The minimum recommended version of PHP is %s.' ),
2164                  $response['recommended_version']
2165              );
2166          }
2167          ?>
2168      </p>
2169  
2170      <p class="button-container">
2171          <?php
2172          printf(
2173              '<a class="button button-primary" href="%1$s" target="_blank">%2$s<span class="screen-reader-text"> %3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>',
2174              esc_url( wp_get_update_php_url() ),
2175              __( 'Learn more about updating PHP' ),
2176              /* translators: Hidden accessibility text. */
2177              __( '(opens in a new tab)' )
2178          );
2179          ?>
2180      </p>
2181      <?php
2182  
2183      wp_update_php_annotation();
2184      wp_direct_php_update_button();
2185  }
2186  
2187  /**
2188   * Adds an additional class to the PHP nag if the current version is insecure.
2189   *
2190   * @since 5.1.0
2191   *
2192   * @param string[] $classes Array of meta box classes.
2193   * @return string[] Modified array of meta box classes.
2194   */
2195  function dashboard_php_nag_class( $classes ) {
2196      $response = wp_check_php_version();
2197  
2198      if ( ! $response ) {
2199          return $classes;
2200      }
2201  
2202      if ( isset( $response['is_secure'] ) && ! $response['is_secure'] ) {
2203          $classes[] = 'php-no-security-updates';
2204      } elseif ( $response['is_lower_than_future_minimum'] ) {
2205          $classes[] = 'php-version-lower-than-future-minimum';
2206      }
2207  
2208      return $classes;
2209  }
2210  
2211  /**
2212   * Displays the Site Health Status widget.
2213   *
2214   * @since 5.4.0
2215   */
2216  function wp_dashboard_site_health() {
2217      $get_issues = get_transient( 'health-check-site-status-result' );
2218  
2219      $issue_counts = array();
2220  
2221      if ( false !== $get_issues ) {
2222          $issue_counts = json_decode( $get_issues, true );
2223      }
2224  
2225      if ( ! is_array( $issue_counts ) || ! $issue_counts ) {
2226          $issue_counts = array(
2227              'good'        => 0,
2228              'recommended' => 0,
2229              'critical'    => 0,
2230          );
2231      }
2232  
2233      $issues_total = $issue_counts['recommended'] + $issue_counts['critical'];
2234      ?>
2235      <div class="health-check-widget">
2236          <div class="health-check-widget-title-section site-health-progress-wrapper loading">
2237              <div class="site-health-progress">
2238                  <svg aria-hidden="true" focusable="false" width="100%" height="100%" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg">
2239                      <circle r="90" cx="100" cy="100" fill="transparent" stroke-dasharray="565.48" stroke-dashoffset="0"></circle>
2240                      <circle id="bar" r="90" cx="100" cy="100" fill="transparent" stroke-dasharray="565.48" stroke-dashoffset="0"></circle>
2241                  </svg>
2242              </div>
2243              <div class="site-health-progress-label">
2244                  <?php if ( false === $get_issues ) : ?>
2245                      <?php _e( 'No information yet&hellip;' ); ?>
2246                  <?php else : ?>
2247                      <?php _e( 'Results are still loading&hellip;' ); ?>
2248                  <?php endif; ?>
2249              </div>
2250          </div>
2251  
2252          <div class="site-health-details">
2253              <?php if ( false === $get_issues ) : ?>
2254                  <p>
2255                      <?php
2256                      printf(
2257                          /* translators: %s: URL to Site Health screen. */
2258                          __( 'Site health checks will automatically run periodically to gather information about your site. You can also <a href="%s">visit the Site Health screen</a> to gather information about your site now.' ),
2259                          esc_url( admin_url( 'site-health.php' ) )
2260                      );
2261                      ?>
2262                  </p>
2263              <?php else : ?>
2264                  <p>
2265                      <?php if ( $issues_total <= 0 ) : ?>
2266                          <?php _e( 'Great job! Your site currently passes all site health checks.' ); ?>
2267                      <?php elseif ( 1 === (int) $issue_counts['critical'] ) : ?>
2268                          <?php _e( 'Your site has a critical issue that should be addressed as soon as possible to improve its performance and security.' ); ?>
2269                      <?php elseif ( $issue_counts['critical'] > 1 ) : ?>
2270                          <?php _e( 'Your site has critical issues that should be addressed as soon as possible to improve its performance and security.' ); ?>
2271                      <?php elseif ( 1 === (int) $issue_counts['recommended'] ) : ?>
2272                          <?php _e( 'Your site&#8217;s health is looking good, but there is still one thing you can do to improve its performance and security.' ); ?>
2273                      <?php else : ?>
2274                          <?php _e( 'Your site&#8217;s health is looking good, but there are still some things you can do to improve its performance and security.' ); ?>
2275                      <?php endif; ?>
2276                  </p>
2277              <?php endif; ?>
2278  
2279              <?php if ( $issues_total > 0 && false !== $get_issues ) : ?>
2280                  <p>
2281                      <?php
2282                      printf(
2283                          /* translators: 1: Number of issues. 2: URL to Site Health screen. */
2284                          _n(
2285                              'Take a look at the <strong>%1$d item</strong> on the <a href="%2$s">Site Health screen</a>.',
2286                              'Take a look at the <strong>%1$d items</strong> on the <a href="%2$s">Site Health screen</a>.',
2287                              $issues_total
2288                          ),
2289                          $issues_total,
2290                          esc_url( admin_url( 'site-health.php' ) )
2291                      );
2292                      ?>
2293                  </p>
2294              <?php endif; ?>
2295          </div>
2296      </div>
2297  
2298      <?php
2299  }
2300  
2301  /**
2302   * Outputs empty dashboard widget to be populated by JS later.
2303   *
2304   * Usable by plugins.
2305   *
2306   * @since 2.5.0
2307   */
2308  function wp_dashboard_empty() {}
2309  
2310  /**
2311   * Displays a welcome panel to introduce users to WordPress.
2312   *
2313   * @since 3.3.0
2314   * @since 5.9.0 Send users to the Site Editor if the active theme is block-based.
2315   */
2316  function wp_welcome_panel() {
2317      list( $display_version ) = explode( '-', wp_get_wp_version() );
2318      $can_customize           = current_user_can( 'customize' );
2319      $is_block_theme          = wp_is_block_theme();
2320      ?>
2321      <div class="welcome-panel-content">
2322      <div class="welcome-panel-header-wrap">
2323          <div class="welcome-panel-header">
2324              <div class="welcome-panel-header-image">
2325                  <?php echo file_get_contents( dirname( __DIR__ ) . '/images/dashboard-background.svg' ); ?>
2326              </div>
2327              <h2><?php _e( 'Welcome to WordPress!' ); ?></h2>
2328              <p>
2329                  <a href="<?php echo esc_url( admin_url( 'about.php' ) ); ?>">
2330                  <?php
2331                      /* translators: %s: Current WordPress version. */
2332                      printf( __( 'Learn more about the %s version.' ), esc_html( $display_version ) );
2333                  ?>
2334                  </a>
2335              </p>
2336          </div>
2337      </div>
2338      <div class="welcome-panel-column-container">
2339          <div class="welcome-panel-column">
2340              <svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
2341                  <rect width="48" height="48" rx="4" fill="#1E1E1E"/>
2342                  <path fill-rule="evenodd" clip-rule="evenodd" d="M32.0668 17.0854L28.8221 13.9454L18.2008 24.671L16.8983 29.0827L21.4257 27.8309L32.0668 17.0854ZM16 32.75H24V31.25H16V32.75Z" fill="white"/>
2343              </svg>
2344              <div class="welcome-panel-column-content">
2345                  <h3><?php _e( 'Author rich content with blocks and patterns' ); ?></h3>
2346                  <p><?php _e( 'Block patterns are pre-configured block layouts. Use them to get inspired or create new pages in a flash.' ); ?></p>
2347                  <a href="<?php echo esc_url( admin_url( 'post-new.php?post_type=page' ) ); ?>"><?php _e( 'Add a new page' ); ?></a>
2348              </div>
2349          </div>
2350          <div class="welcome-panel-column">
2351              <svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
2352                  <rect width="48" height="48" rx="4" fill="#1E1E1E"/>
2353                  <path fill-rule="evenodd" clip-rule="evenodd" d="M18 16h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H18a2 2 0 0 1-2-2V18a2 2 0 0 1 2-2zm12 1.5H18a.5.5 0 0 0-.5.5v3h13v-3a.5.5 0 0 0-.5-.5zm.5 5H22v8h8a.5.5 0 0 0 .5-.5v-7.5zm-10 0h-3V30a.5.5 0 0 0 .5.5h2.5v-8z" fill="#fff"/>
2354              </svg>
2355              <div class="welcome-panel-column-content">
2356              <?php if ( $is_block_theme ) : ?>
2357                  <h3><?php _e( 'Customize your entire site with block themes' ); ?></h3>
2358                  <p><?php _e( 'Design everything on your site &#8212; from the header down to the footer, all using blocks and patterns.' ); ?></p>
2359                  <a href="<?php echo esc_url( admin_url( 'site-editor.php' ) ); ?>"><?php _e( 'Open site editor' ); ?></a>
2360              <?php else : ?>
2361                  <h3><?php _e( 'Start Customizing' ); ?></h3>
2362                  <p><?php _e( 'Configure your site&#8217;s logo, header, menus, and more in the Customizer.' ); ?></p>
2363                  <?php if ( $can_customize ) : ?>
2364                      <a class="load-customize hide-if-no-customize" href="<?php echo wp_customize_url(); ?>"><?php _e( 'Open the Customizer' ); ?></a>
2365                  <?php endif; ?>
2366              <?php endif; ?>
2367              </div>
2368          </div>
2369          <div class="welcome-panel-column">
2370              <svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
2371                  <rect width="48" height="48" rx="4" fill="#1E1E1E"/>
2372                  <path fill-rule="evenodd" clip-rule="evenodd" d="M31 24a7 7 0 0 1-7 7V17a7 7 0 0 1 7 7zm-7-8a8 8 0 1 1 0 16 8 8 0 0 1 0-16z" fill="#fff"/>
2373              </svg>
2374              <div class="welcome-panel-column-content">
2375              <?php if ( $is_block_theme ) : ?>
2376                  <h3><?php _e( 'Switch up your site&#8217;s look & feel with Styles' ); ?></h3>
2377                  <p><?php _e( 'Tweak your site, or give it a whole new look! Get creative &#8212; how about a new color palette or font?' ); ?></p>
2378                  <a href="<?php echo esc_url( add_query_arg( 'p', rawurlencode( '/styles' ), admin_url( 'site-editor.php' ) ) ); ?>"><?php _e( 'Edit styles' ); ?></a>
2379              <?php else : ?>
2380                  <h3><?php _e( 'Discover a new way to build your site.' ); ?></h3>
2381                  <p><?php _e( 'There is a new kind of WordPress theme, called a block theme, that lets you build the site you&#8217;ve always wanted &#8212; with blocks and styles.' ); ?></p>
2382                  <a href="<?php echo esc_url( __( 'https://wordpress.org/documentation/article/block-themes/' ) ); ?>"><?php _e( 'Learn about block themes' ); ?></a>
2383              <?php endif; ?>
2384              </div>
2385          </div>
2386      </div>
2387      </div>
2388      <?php
2389  }


Generated : Wed Nov 4 08:20:38 2026 Cross-referenced by PHPXref