wp/wp-admin/includes/class-wp-debug-data.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    11 	/**
    11 	/**
    12 	 * Calls all core functions to check for updates.
    12 	 * Calls all core functions to check for updates.
    13 	 *
    13 	 *
    14 	 * @since 5.2.0
    14 	 * @since 5.2.0
    15 	 */
    15 	 */
    16 	static function check_for_updates() {
    16 	public static function check_for_updates() {
    17 		wp_version_check();
    17 		wp_version_check();
    18 		wp_update_plugins();
    18 		wp_update_plugins();
    19 		wp_update_themes();
    19 		wp_update_themes();
    20 	}
    20 	}
    21 
    21 
    30 	 * @throws ImagickException
    30 	 * @throws ImagickException
    31 	 * @global wpdb $wpdb WordPress database abstraction object.
    31 	 * @global wpdb $wpdb WordPress database abstraction object.
    32 	 *
    32 	 *
    33 	 * @return array The debug data for the site.
    33 	 * @return array The debug data for the site.
    34 	 */
    34 	 */
    35 	static function debug_data() {
    35 	public static function debug_data() {
    36 		global $wpdb;
    36 		global $wpdb;
    37 
    37 
    38 		// Save few function calls.
    38 		// Save few function calls.
    39 		$upload_dir             = wp_upload_dir();
    39 		$upload_dir             = wp_upload_dir();
    40 		$permalink_structure    = get_option( 'permalink_structure' );
    40 		$permalink_structure    = get_option( 'permalink_structure' );
   232 		} else {
   232 		} else {
   233 			$compress_css       = __( 'Undefined' );
   233 			$compress_css       = __( 'Undefined' );
   234 			$compress_css_debug = 'undefined';
   234 			$compress_css_debug = 'undefined';
   235 		}
   235 		}
   236 
   236 
   237 		// Check WP_LOCAL_DEV.
   237 		// Check WP_ENVIRONMENT_TYPE.
   238 		if ( defined( 'WP_LOCAL_DEV' ) ) {
   238 		if ( defined( 'WP_ENVIRONMENT_TYPE' ) ) {
   239 			$wp_local_dev       = WP_LOCAL_DEV ? __( 'Enabled' ) : __( 'Disabled' );
   239 			$wp_environment_type = WP_ENVIRONMENT_TYPE;
   240 			$wp_local_dev_debug = WP_LOCAL_DEV ? 'true' : 'false';
   240 		} else {
   241 		} else {
   241 			$wp_environment_type = __( 'Undefined' );
   242 			$wp_local_dev       = __( 'Undefined' );
       
   243 			$wp_local_dev_debug = 'undefined';
       
   244 		}
   242 		}
   245 
   243 
   246 		$info['wp-constants'] = array(
   244 		$info['wp-constants'] = array(
   247 			'label'       => __( 'WordPress Constants' ),
   245 			'label'       => __( 'WordPress Constants' ),
   248 			'description' => __( 'These settings alter where and how parts of WordPress are loaded.' ),
   246 			'description' => __( 'These settings alter where and how parts of WordPress are loaded.' ),
   316 				'COMPRESS_CSS'        => array(
   314 				'COMPRESS_CSS'        => array(
   317 					'label' => 'COMPRESS_CSS',
   315 					'label' => 'COMPRESS_CSS',
   318 					'value' => $compress_css,
   316 					'value' => $compress_css,
   319 					'debug' => $compress_css_debug,
   317 					'debug' => $compress_css_debug,
   320 				),
   318 				),
   321 				'WP_LOCAL_DEV'        => array(
   319 				'WP_ENVIRONMENT_TYPE' => array(
   322 					'label' => 'WP_LOCAL_DEV',
   320 					'label' => 'WP_ENVIRONMENT_TYPE',
   323 					'value' => $wp_local_dev,
   321 					'value' => $wp_environment_type,
   324 					'debug' => $wp_local_dev_debug,
   322 					'debug' => $wp_environment_type,
   325 				),
   323 				),
   326 				'DB_CHARSET'          => array(
   324 				'DB_CHARSET'          => array(
   327 					'label' => 'DB_CHARSET',
   325 					'label' => 'DB_CHARSET',
   328 					'value' => ( defined( 'DB_CHARSET' ) ? DB_CHARSET : __( 'Undefined' ) ),
   326 					'value' => ( defined( 'DB_CHARSET' ) ? DB_CHARSET : __( 'Undefined' ) ),
   329 					'debug' => ( defined( 'DB_CHARSET' ) ? DB_CHARSET : 'undefined' ),
   327 					'debug' => ( defined( 'DB_CHARSET' ) ? DB_CHARSET : 'undefined' ),
   388 			$site_count = 0;
   386 			$site_count = 0;
   389 			foreach ( $network_ids as $network_id ) {
   387 			foreach ( $network_ids as $network_id ) {
   390 				$site_count += get_blog_count( $network_id );
   388 				$site_count += get_blog_count( $network_id );
   391 			}
   389 			}
   392 
   390 
   393 			$info['wp-core']['fields']['user_count'] = array(
       
   394 				'label' => __( 'User count' ),
       
   395 				'value' => get_user_count(),
       
   396 			);
       
   397 
       
   398 			$info['wp-core']['fields']['site_count'] = array(
   391 			$info['wp-core']['fields']['site_count'] = array(
   399 				'label' => __( 'Site count' ),
   392 				'label' => __( 'Site count' ),
   400 				'value' => $site_count,
   393 				'value' => $site_count,
   401 			);
   394 			);
   402 
   395 
   403 			$info['wp-core']['fields']['network_count'] = array(
   396 			$info['wp-core']['fields']['network_count'] = array(
   404 				'label' => __( 'Network count' ),
   397 				'label' => __( 'Network count' ),
   405 				'value' => $network_query->found_networks,
   398 				'value' => $network_query->found_networks,
   406 			);
   399 			);
   407 		} else {
   400 		}
   408 			$user_count = count_users();
   401 
   409 
   402 		$info['wp-core']['fields']['user_count'] = array(
   410 			$info['wp-core']['fields']['user_count'] = array(
   403 			'label' => __( 'User count' ),
   411 				'label' => __( 'User count' ),
   404 			'value' => get_user_count(),
   412 				'value' => $user_count['total_users'],
   405 		);
   413 			);
       
   414 		}
       
   415 
   406 
   416 		// WordPress features requiring processing.
   407 		// WordPress features requiring processing.
   417 		$wp_dotorg = wp_remote_get( 'https://wordpress.org', array( 'timeout' => 10 ) );
   408 		$wp_dotorg = wp_remote_get( 'https://wordpress.org', array( 'timeout' => 10 ) );
   418 
   409 
   419 		if ( ! is_wp_error( $wp_dotorg ) ) {
   410 		if ( ! is_wp_error( $wp_dotorg ) ) {
   921 
   912 
   922 		$info['wp-database']['fields']['database_collate'] = array(
   913 		$info['wp-database']['fields']['database_collate'] = array(
   923 			'label'   => __( 'Database collation' ),
   914 			'label'   => __( 'Database collation' ),
   924 			'value'   => $wpdb->collate,
   915 			'value'   => $wpdb->collate,
   925 			'private' => true,
   916 			'private' => true,
       
   917 		);
       
   918 
       
   919 		$info['wp-database']['fields']['max_allowed_packet'] = array(
       
   920 			'label' => __( 'Max allowed packet size' ),
       
   921 			'value' => self::get_mysql_var( 'max_allowed_packet' ),
       
   922 		);
       
   923 
       
   924 		$info['wp-database']['fields']['max_connections'] = array(
       
   925 			'label' => __( 'Max connections number' ),
       
   926 			'value' => self::get_mysql_var( 'max_connections' ),
   926 		);
   927 		);
   927 
   928 
   928 		// List must use plugins if there are any.
   929 		// List must use plugins if there are any.
   929 		$mu_plugins = get_mu_plugins();
   930 		$mu_plugins = get_mu_plugins();
   930 
   931 
  1396 				'debug' => ( $is_writable_wpmu_plugin_dir ? 'writable' : 'not writable' ),
  1397 				'debug' => ( $is_writable_wpmu_plugin_dir ? 'writable' : 'not writable' ),
  1397 			);
  1398 			);
  1398 		}
  1399 		}
  1399 
  1400 
  1400 		/**
  1401 		/**
  1401 		 * Add or modify the debug information.
  1402 		 * Add to or modify the debug information shown on the Tools -> Site Health -> Info screen.
  1402 		 *
  1403 		 *
  1403 		 * Plugin or themes may wish to introduce their own debug information without creating additional admin pages
  1404 		 * Plugin or themes may wish to introduce their own debug information without creating
  1404 		 * they can utilize this filter to introduce their own sections or add more data to existing sections.
  1405 		 * additional admin pages. They can utilize this filter to introduce their own sections
       
  1406 		 * or add more data to existing sections.
  1405 		 *
  1407 		 *
  1406 		 * Array keys for sections added by core are all prefixed with `wp-`, plugins and themes should use their own slug as
  1408 		 * Array keys for sections added by core are all prefixed with `wp-`. Plugins and themes
  1407 		 * a prefix, both for consistency as well as avoiding key collisions. Note that the array keys are used as labels
  1409 		 * should use their own slug as a prefix, both for consistency as well as avoiding
  1408 		 * for the copied data.
  1410 		 * key collisions. Note that the array keys are used as labels for the copied data.
  1409 		 *
  1411 		 *
  1410 		 * All strings are expected to be plain text except $description that can contain inline HTML tags (see below).
  1412 		 * All strings are expected to be plain text except `$description` that can contain
       
  1413 		 * inline HTML tags (see below).
  1411 		 *
  1414 		 *
  1412 		 * @since 5.2.0
  1415 		 * @since 5.2.0
  1413 		 *
  1416 		 *
  1414 		 * @param array $args {
  1417 		 * @param array $args {
  1415 		 *     The debug information to be added to the core information page.
  1418 		 *     The debug information to be added to the core information page.
  1416 		 *
  1419 		 *
  1417 		 *     This is an associative multi-dimensional array, up to three levels deep. The topmost array holds the sections.
  1420 		 *     This is an associative multi-dimensional array, up to three levels deep.
  1418 		 *     Each section has a `$fields` associative array (see below), and each `$value` in `$fields` can be
  1421 		 *     The topmost array holds the sections, keyed by section ID.
  1419 		 *     another associative array of name/value pairs when there is more structured data to display.
       
  1420 		 *
  1422 		 *
  1421 		 *     @type string  $label        The title for this section of the debug output.
  1423 		 *     @type array ...$0 {
  1422 		 *     @type string  $description  Optional. A description for your information section which may contain basic HTML
  1424 		 *         Each section has a `$fields` associative array (see below), and each `$value` in `$fields`
  1423 		 *                                 markup, inline tags only as it is outputted in a paragraph.
  1425 		 *         can be another associative array of name/value pairs when there is more structured data
  1424 		 *     @type boolean $show_count   Optional. If set to `true` the amount of fields will be included in the title for
  1426 		 *         to display.
  1425 		 *                                 this section.
       
  1426 		 *     @type boolean $private      Optional. If set to `true` the section and all associated fields will be excluded
       
  1427 		 *                                 from the copied data.
       
  1428 		 *     @type array   $fields {
       
  1429 		 *         An associative array containing the data to be displayed.
       
  1430 		 *
  1427 		 *
  1431 		 *         @type string  $label    The label for this piece of information.
  1428 		 *         @type string $label       Required. The title for this section of the debug output.
  1432 		 *         @type string  $value    The output that is displayed for this field. Text should be translated. Can be
  1429 		 *         @type string $description Optional. A description for your information section which
  1433 		 *                                 an associative array that is displayed as name/value pairs.
  1430 		 *                                   may contain basic HTML markup, inline tags only as it is
  1434 		 *         @type string  $debug    Optional. The output that is used for this field when the user copies the data.
  1431 		 *                                   outputted in a paragraph.
  1435 		 *                                 It should be more concise and not translated. If not set, the content of `$value` is used.
  1432 		 *         @type bool   $show_count  Optional. If set to `true`, the amount of fields will be included
  1436 		 *                                 Note that the array keys are used as labels for the copied data.
  1433 		 *                                   in the title for this section. Default false.
  1437 		 *         @type boolean $private  Optional. If set to `true` the field will not be included in the copied data
  1434 		 *         @type bool   $private     Optional. If set to `true`, the section and all associated fields
  1438 		 *                                 allowing you to show, for example, API keys here.
  1435 		 *                                   will be excluded from the copied data. Default false.
       
  1436 		 *         @type array  $fields {
       
  1437 		 *             Required. An associative array containing the fields to be displayed in the section,
       
  1438 		 *             keyed by field ID.
       
  1439 		 *
       
  1440 		 *             @type array ...$0 {
       
  1441 		 *                 An associative array containing the data to be displayed for the field.
       
  1442 		 *
       
  1443 		 *                 @type string $label    Required. The label for this piece of information.
       
  1444 		 *                 @type mixed  $value    Required. The output that is displayed for this field.
       
  1445 		 *                                        Text should be translated. Can be an associative array
       
  1446 		 *                                        that is displayed as name/value pairs.
       
  1447 		 *                                        Accepted types: `string|int|float|(string|int|float)[]`.
       
  1448 		 *                 @type string $debug    Optional. The output that is used for this field when
       
  1449 		 *                                        the user copies the data. It should be more concise and
       
  1450 		 *                                        not translated. If not set, the content of `$value`
       
  1451 		 *                                        is used. Note that the array keys are used as labels
       
  1452 		 *                                        for the copied data.
       
  1453 		 *                 @type bool   $private  Optional. If set to `true`, the field will be excluded
       
  1454 		 *                                        from the copied data, allowing you to show, for example,
       
  1455 		 *                                        API keys here. Default false.
       
  1456 		 *             }
       
  1457 		 *         }
  1439 		 *     }
  1458 		 *     }
  1440 		 * }
  1459 		 * }
  1441 		 */
  1460 		 */
  1442 		$info = apply_filters( 'debug_information', $info );
  1461 		$info = apply_filters( 'debug_information', $info );
  1443 
  1462 
  1444 		return $info;
  1463 		return $info;
       
  1464 	}
       
  1465 
       
  1466 	/**
       
  1467 	 * Returns the value of a MySQL system variable.
       
  1468 	 *
       
  1469 	 * @since 5.9.0
       
  1470 	 *
       
  1471 	 * @global wpdb $wpdb WordPress database abstraction object.
       
  1472 	 *
       
  1473 	 * @param string $mysql_var Name of the MySQL system variable.
       
  1474 	 * @return string|null The variable value on success. Null if the variable does not exist.
       
  1475 	 */
       
  1476 	public static function get_mysql_var( $mysql_var ) {
       
  1477 		global $wpdb;
       
  1478 
       
  1479 		$result = $wpdb->get_row(
       
  1480 			$wpdb->prepare( 'SHOW VARIABLES LIKE %s', $mysql_var ),
       
  1481 			ARRAY_A
       
  1482 		);
       
  1483 
       
  1484 		if ( ! empty( $result ) && array_key_exists( 'Value', $result ) ) {
       
  1485 			return $result['Value'];
       
  1486 		}
       
  1487 
       
  1488 		return null;
  1445 	}
  1489 	}
  1446 
  1490 
  1447 	/**
  1491 	/**
  1448 	 * Format the information gathered for debugging, in a manner suitable for copying to a forum or support ticket.
  1492 	 * Format the information gathered for debugging, in a manner suitable for copying to a forum or support ticket.
  1449 	 *
  1493 	 *
  1450 	 * @since 5.2.0
  1494 	 * @since 5.2.0
  1451 	 *
  1495 	 *
  1452 	 * @param array  $info_array Information gathered from the `WP_Debug_Data::debug_data` function.
  1496 	 * @param array  $info_array Information gathered from the `WP_Debug_Data::debug_data()` function.
  1453 	 * @param string $type       The data type to return, either 'info' or 'debug'.
  1497 	 * @param string $data_type  The data type to return, either 'info' or 'debug'.
  1454 	 * @return string The formatted data.
  1498 	 * @return string The formatted data.
  1455 	 */
  1499 	 */
  1456 	public static function format( $info_array, $type ) {
  1500 	public static function format( $info_array, $data_type ) {
  1457 		$return = "`\n";
  1501 		$return = "`\n";
  1458 
  1502 
  1459 		foreach ( $info_array as $section => $details ) {
  1503 		foreach ( $info_array as $section => $details ) {
  1460 			// Skip this section if there are no fields, or the section has been declared as private.
  1504 			// Skip this section if there are no fields, or the section has been declared as private.
  1461 			if ( empty( $details['fields'] ) || ( isset( $details['private'] ) && $details['private'] ) ) {
  1505 			if ( empty( $details['fields'] ) || ( isset( $details['private'] ) && $details['private'] ) ) {
  1462 				continue;
  1506 				continue;
  1463 			}
  1507 			}
  1464 
  1508 
  1465 			$section_label = 'debug' === $type ? $section : $details['label'];
  1509 			$section_label = 'debug' === $data_type ? $section : $details['label'];
  1466 
  1510 
  1467 			$return .= sprintf(
  1511 			$return .= sprintf(
  1468 				"### %s%s ###\n\n",
  1512 				"### %s%s ###\n\n",
  1469 				$section_label,
  1513 				$section_label,
  1470 				( isset( $details['show_count'] ) && $details['show_count'] ? sprintf( ' (%d)', count( $details['fields'] ) ) : '' )
  1514 				( isset( $details['show_count'] ) && $details['show_count'] ? sprintf( ' (%d)', count( $details['fields'] ) ) : '' )
  1473 			foreach ( $details['fields'] as $field_name => $field ) {
  1517 			foreach ( $details['fields'] as $field_name => $field ) {
  1474 				if ( isset( $field['private'] ) && true === $field['private'] ) {
  1518 				if ( isset( $field['private'] ) && true === $field['private'] ) {
  1475 					continue;
  1519 					continue;
  1476 				}
  1520 				}
  1477 
  1521 
  1478 				if ( 'debug' === $type && isset( $field['debug'] ) ) {
  1522 				if ( 'debug' === $data_type && isset( $field['debug'] ) ) {
  1479 					$debug_data = $field['debug'];
  1523 					$debug_data = $field['debug'];
  1480 				} else {
  1524 				} else {
  1481 					$debug_data = $field['value'];
  1525 					$debug_data = $field['value'];
  1482 				}
  1526 				}
  1483 
  1527 
  1494 					$value = 'undefined';
  1538 					$value = 'undefined';
  1495 				} else {
  1539 				} else {
  1496 					$value = $debug_data;
  1540 					$value = $debug_data;
  1497 				}
  1541 				}
  1498 
  1542 
  1499 				if ( 'debug' === $type ) {
  1543 				if ( 'debug' === $data_type ) {
  1500 					$label = $field_name;
  1544 					$label = $field_name;
  1501 				} else {
  1545 				} else {
  1502 					$label = $field['label'];
  1546 					$label = $field['label'];
  1503 				}
  1547 				}
  1504 
  1548