<?php /** * WordPress Dashboard Widget Administration Screen API * * @package WordPress * @subpackage Administration */ /** * Registers dashboard widgets. * * Handles POST data, sets up filters. * * @since 2.5.0 * * @global array $wp_registered_widgets * @global array $wp_registered_widget_controls * @global callable[] $wp_dashboard_control_callbacks */ function wp_dashboard_setup() { global $wp_registered_widgets, $wp_registered_widget_controls, $wp_dashboard_control_callbacks; $screen = get_current_screen(); /* Register Widgets and Controls */ $wp_dashboard_control_callbacks = array(); // Browser version $check_browser = wp_check_browser_version(); if ( $check_browser && $check_browser['upgrade'] ) { add_filter( 'postbox_classes_dashboard_dashboard_browser_nag', 'dashboard_browser_nag_class' ); if ( $check_browser['insecure'] ) { wp_add_dashboard_widget( 'dashboard_browser_nag', __( 'You are using an insecure browser!' ), 'wp_dashboard_browser_nag' ); } else { wp_add_dashboard_widget( 'dashboard_browser_nag', __( 'Your browser is out of date!' ), 'wp_dashboard_browser_nag' ); } } // PHP Version. $check_php = wp_check_php_version(); if ( $check_php && current_user_can( 'update_php' ) ) { // If "not acceptable" the widget will be shown. if ( isset( $check_php['is_acceptable'] ) && ! $check_php['is_acceptable'] ) { add_filter( 'postbox_classes_dashboard_dashboard_php_nag', 'dashboard_php_nag_class' ); if ( $check_php['is_lower_than_future_minimum'] ) { wp_add_dashboard_widget( 'dashboard_php_nag', __( 'PHP Update Required' ), 'wp_dashboard_php_nag' ); } else { wp_add_dashboard_widget( 'dashboard_php_nag', __( 'PHP Update Recommended' ), 'wp_dashboard_php_nag' ); } } } // Site Health. if ( current_user_can( 'view_site_health_checks' ) && ! is_network_admin() ) { if ( ! class_exists( 'WP_Site_Health' ) ) { require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php'; } WP_Site_Health::get_instance(); wp_enqueue_style( 'site-health' ); wp_enqueue_script( 'site-health' ); wp_add_dashboard_widget( 'dashboard_site_health', __( 'Site Health Status' ), 'wp_dashboard_site_health' ); } // Right Now. if ( is_blog_admin() && current_user_can( 'edit_posts' ) ) { wp_add_dashboard_widget( 'dashboard_right_now', __( 'At a Glance' ), 'wp_dashboard_right_now' ); } if ( is_network_admin() ) { wp_add_dashboard_widget( 'network_dashboard_right_now', __( 'Right Now' ), 'wp_network_dashboard_right_now' ); } // Activity Widget. if ( is_blog_admin() ) { wp_add_dashboard_widget( 'dashboard_activity', __( 'Activity' ), 'wp_dashboard_site_activity' ); } // QuickPress Widget. if ( is_blog_admin() && current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) { $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' ) ); wp_add_dashboard_widget( 'dashboard_quick_press', $quick_draft_title, 'wp_dashboard_quick_press' ); } // WordPress Events and News. wp_add_dashboard_widget( 'dashboard_primary', __( 'WordPress Events and News' ), 'wp_dashboard_events_news' ); if ( is_network_admin() ) { /** * Fires after core widgets for the Network Admin dashboard have been registered. * * @since 3.1.0 */ do_action( 'wp_network_dashboard_setup' ); /** * Filters the list of widgets to load for the Network Admin dashboard. * * @since 3.1.0 * * @param string[] $dashboard_widgets An array of dashboard widget IDs. */ $dashboard_widgets = apply_filters( 'wp_network_dashboard_widgets', array() ); } elseif ( is_user_admin() ) { /** * Fires after core widgets for the User Admin dashboard have been registered. * * @since 3.1.0 */ do_action( 'wp_user_dashboard_setup' ); /** * Filters the list of widgets to load for the User Admin dashboard. * * @since 3.1.0 * * @param string[] $dashboard_widgets An array of dashboard widget IDs. */ $dashboard_widgets = apply_filters( 'wp_user_dashboard_widgets', array() ); } else { /** * Fires after core widgets for the admin dashboard have been registered. * * @since 2.5.0 */ do_action( 'wp_dashboard_setup' ); /** * Filters the list of widgets to load for the admin dashboard. * * @since 2.5.0 * * @param string[] $dashboard_widgets An array of dashboard widget IDs. */ $dashboard_widgets = apply_filters( 'wp_dashboard_widgets', array() ); } foreach ( $dashboard_widgets as $widget_id ) { $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>'; wp_add_dashboard_widget( $widget_id, $name, $wp_registered_widgets[ $widget_id ]['callback'], $wp_registered_widget_controls[ $widget_id ]['callback'] ); } if ( 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['widget_id'] ) ) { check_admin_referer( 'edit-dashboard-widget_' . $_POST['widget_id'], 'dashboard-widget-nonce' ); ob_start(); // Hack - but the same hack wp-admin/widgets.php uses. wp_dashboard_trigger_widget_control( $_POST['widget_id'] ); ob_end_clean(); wp_redirect( remove_query_arg( 'edit' ) ); exit; } /** This action is documented in wp-admin/includes/meta-boxes.php */ do_action( 'do_meta_boxes', $screen->id, 'normal', '' ); /** This action is documented in wp-admin/includes/meta-boxes.php */ do_action( 'do_meta_boxes', $screen->id, 'side', '' ); } /** * Adds a new dashboard widget. * * @since 2.7.0 * @since 5.6.0 The `$context` and `$priority` parameters were added. * * @global callable[] $wp_dashboard_control_callbacks * * @param string $widget_id Widget ID (used in the 'id' attribute for the widget). * @param string $widget_name Title of the widget. * @param callable $callback Function that fills the widget with the desired content. * The function should echo its output. * @param callable $control_callback Optional. Function that outputs controls for the widget. Default null. * @param array $callback_args Optional. Data that should be set as the $args property of the widget array * (which is the second parameter passed to your callback). Default null. * @param string $context Optional. The context within the screen where the box should display. * Accepts 'normal', 'side', 'column3', or 'column4'. Default 'normal'. * @param string $priority Optional. The priority within the context where the box should show. * Accepts 'high', 'core', 'default', or 'low'. Default 'core'. */ function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null, $callback_args = null, $context = 'normal', $priority = 'core' ) { global $wp_dashboard_control_callbacks; $screen = get_current_screen(); $private_callback_args = array( '__widget_basename' => $widget_name ); if ( is_null( $callback_args ) ) { $callback_args = $private_callback_args; } elseif ( is_array( $callback_args ) ) { $callback_args = array_merge( $callback_args, $private_callback_args ); } if ( $control_callback && is_callable( $control_callback ) && current_user_can( 'edit_dashboard' ) ) { $wp_dashboard_control_callbacks[ $widget_id ] = $control_callback; if ( isset( $_GET['edit'] ) && $widget_id === $_GET['edit'] ) { list($url) = explode( '#', add_query_arg( 'edit', false ), 2 ); $widget_name .= ' <span class="postbox-title-action"><a href="' . esc_url( $url ) . '">' . __( 'Cancel' ) . '</a></span>'; $callback = '_wp_dashboard_control_callback'; } else { list($url) = explode( '#', add_query_arg( 'edit', $widget_id ), 2 ); $widget_name .= ' <span class="postbox-title-action"><a href="' . esc_url( "$url#$widget_id" ) . '" class="edit-box open-box">' . __( 'Configure' ) . '</a></span>'; } } $side_widgets = array( 'dashboard_quick_press', 'dashboard_primary' ); if ( in_array( $widget_id, $side_widgets, true ) ) { $context = 'side'; } $high_priority_widgets = array( 'dashboard_browser_nag', 'dashboard_php_nag' ); if ( in_array( $widget_id, $high_priority_widgets, true ) ) { $priority = 'high'; } if ( empty( $context ) ) { $context = 'normal'; } if ( empty( $priority ) ) { $priority = 'core'; } add_meta_box( $widget_id, $widget_name, $callback, $screen, $context, $priority, $callback_args ); } /** * Outputs controls for the current dashboard widget. * * @access private * @since 2.7.0 * * @param mixed $dashboard * @param array $meta_box */ function _wp_dashboard_control_callback( $dashboard, $meta_box ) { echo '<form method="post" class="dashboard-widget-control-form wp-clearfix">'; wp_dashboard_trigger_widget_control( $meta_box['id'] ); wp_nonce_field( 'edit-dashboard-widget_' . $meta_box['id'], 'dashboard-widget-nonce' ); echo '<input type="hidden" name="widget_id" value="' . esc_attr( $meta_box['id'] ) . '" />'; submit_button( __( 'Save Changes' ) ); echo '</form>'; } /** * Displays the dashboard. * * @since 2.5.0 */ function wp_dashboard() { $screen = get_current_screen(); $columns = absint( $screen->get_columns() ); $columns_css = ''; if ( $columns ) { $columns_css = " columns-$columns"; } ?> <div id="dashboard-widgets" class="metabox-holder<?php echo $columns_css; ?>"> <div id="postbox-container-1" class="postbox-container"> <?php do_meta_boxes( $screen->id, 'normal', '' ); ?> </div> <div id="postbox-container-2" class="postbox-container"> <?php do_meta_boxes( $screen->id, 'side', '' ); ?> </div> <div id="postbox-container-3" class="postbox-container"> <?php do_meta_boxes( $screen->id, 'column3', '' ); ?> </div> <div id="postbox-container-4" class="postbox-container"> <?php do_meta_boxes( $screen->id, 'column4', '' ); ?> </div> </div> <?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); } // // Dashboard Widgets. // /** * Dashboard widget that displays some basic stats about the site. * * Formerly 'Right Now'. A streamlined 'At a Glance' as of 3.8. * * @since 2.7.0 */ function wp_dashboard_right_now() { ?> <div class="main"> <ul> <?php // Posts and Pages. foreach ( array( 'post', 'page' ) as $post_type ) { $num_posts = wp_count_posts( $post_type ); if ( $num_posts && $num_posts->publish ) { if ( 'post' === $post_type ) { /* translators: %s: Number of posts. */ $text = _n( '%s Post', '%s Posts', $num_posts->publish ); } else { /* translators: %s: Number of pages. */ $text = _n( '%s Page', '%s Pages', $num_posts->publish ); } $text = sprintf( $text, number_format_i18n( $num_posts->publish ) ); $post_type_object = get_post_type_object( $post_type ); if ( $post_type_object && current_user_can( $post_type_object->cap->edit_posts ) ) { printf( '<li class="%1$s-count"><a href="edit.php?post_type=%1$s">%2$s</a></li>', $post_type, $text ); } else { printf( '<li class="%1$s-count"><span>%2$s</span></li>', $post_type, $text ); } } } // Comments. $num_comm = wp_count_comments(); if ( $num_comm && ( $num_comm->approved || $num_comm->moderated ) ) { /* translators: %s: Number of comments. */ $text = sprintf( _n( '%s Comment', '%s Comments', $num_comm->approved ), number_format_i18n( $num_comm->approved ) ); ?> <li class="comment-count"> <a href="edit-comments.php"><?php echo $text; ?></a> </li> <?php $moderated_comments_count_i18n = number_format_i18n( $num_comm->moderated ); /* translators: %s: Number of comments. */ $text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $num_comm->moderated ), $moderated_comments_count_i18n ); ?> <li class="comment-mod-count<?php echo ! $num_comm->moderated ? ' hidden' : ''; ?>"> <a href="edit-comments.php?comment_status=moderated" class="comments-in-moderation-text"><?php echo $text; ?></a> </li> <?php } /** * Filters the array of extra elements to list in the 'At a Glance' * dashboard widget. * * Prior to 3.8.0, the widget was named 'Right Now'. Each element * is wrapped in list-item tags on output. * * @since 3.8.0 * * @param string[] $items Array of extra 'At a Glance' widget items. */ $elements = apply_filters( 'dashboard_glance_items', array() ); if ( $elements ) { echo '<li>' . implode( "</li>\n<li>", $elements ) . "</li>\n"; } ?> </ul> <?php update_right_now_message(); // Check if search engines are asked not to index this site. if ( ! is_network_admin() && ! is_user_admin() && current_user_can( 'manage_options' ) && ! get_option( 'blog_public' ) ) { /** * Filters the link title attribute for the 'Search engines discouraged' * message displayed in the 'At a Glance' dashboard widget. * * Prior to 3.8.0, the widget was named 'Right Now'. * * @since 3.0.0 * @since 4.5.0 The default for `$title` was updated to an empty string. * * @param string $title Default attribute text. */ $title = apply_filters( 'privacy_on_link_title', '' ); /** * Filters the link label for the 'Search engines discouraged' message * displayed in the 'At a Glance' dashboard widget. * * Prior to 3.8.0, the widget was named 'Right Now'. * * @since 3.0.0 * * @param string $content Default text. */ $content = apply_filters( 'privacy_on_link_text', __( 'Search engines discouraged' ) ); $title_attr = '' === $title ? '' : " title='$title'"; echo "<p class='search-engines-info'><a href='options-reading.php'$title_attr>$content</a></p>"; } ?> </div> <?php /* * activity_box_end has a core action, but only prints content when multisite. * Using an output buffer is the only way to really check if anything's displayed here. */ ob_start(); /** * Fires at the end of the 'At a Glance' dashboard widget. * * Prior to 3.8.0, the widget was named 'Right Now'. * * @since 2.5.0 */ do_action( 'rightnow_end' ); /** * Fires at the end of the 'At a Glance' dashboard widget. * * Prior to 3.8.0, the widget was named 'Right Now'. * * @since 2.0.0 */ do_action( 'activity_box_end' ); $actions = ob_get_clean(); if ( ! empty( $actions ) ) : ?> <div class="sub"> <?php echo $actions; ?> </div> <?php endif; } /** * @since 3.1.0 */ function wp_network_dashboard_right_now() { $actions = array(); if ( current_user_can( 'create_sites' ) ) { $actions['create-site'] = '<a href="' . network_admin_url( 'site-new.php' ) . '">' . __( 'Create a New Site' ) . '</a>'; } if ( current_user_can( 'create_users' ) ) { $actions['create-user'] = '<a href="' . network_admin_url( 'user-new.php' ) . '">' . __( 'Create a New User' ) . '</a>'; } $c_users = get_user_count(); $c_blogs = get_blog_count(); /* translators: %s: Number of users on the network. */ $user_text = sprintf( _n( '%s user', '%s users', $c_users ), number_format_i18n( $c_users ) ); /* translators: %s: Number of sites on the network. */ $blog_text = sprintf( _n( '%s site', '%s sites', $c_blogs ), number_format_i18n( $c_blogs ) ); /* translators: 1: Text indicating the number of sites on the network, 2: Text indicating the number of users on the network. */ $sentence = sprintf( __( 'You have %1$s and %2$s.' ), $blog_text, $user_text ); if ( $actions ) { echo '<ul class="subsubsub">'; foreach ( $actions as $class => $action ) { $actions[ $class ] = "\t<li class='$class'>$action"; } echo implode( " |</li>\n", $actions ) . "</li>\n"; echo '</ul>'; } ?> <br class="clear" /> <p class="youhave"><?php echo $sentence; ?></p> <?php /** * Fires in the Network Admin 'Right Now' dashboard widget * just before the user and site search form fields. * * @since MU (3.0.0) */ do_action( 'wpmuadminresult' ); ?> <form action="<?php echo esc_url( network_admin_url( 'users.php' ) ); ?>" method="get"> <p> <label class="screen-reader-text" for="search-users"> <?php