WP_Screen::get() – Fetches a screen object.

You appear to be a bot. Output may be restricted

Description

Fetches a screen object.

Usage

$WP_Screen = WP_Screen::get( $hook_name );

Parameters

$hook_name
( string|WP_Screen ) optional – Optional. The hook name (also known as the hook suffix) used to determine the screen. Defaults to the current $hook_suffix global.

Returns

WP_Screen Screen object.

Source

File name: wordpress/wp-admin/includes/class-wp-screen.php


Lines:

1 to 100 of 191
  public static function get( $hook_name = '' ) {
    if ( $hook_name instanceof WP_Screen ) {
      return $hook_name;
    }

    $id              = '';
    $post_type       = null;
    $taxonomy        = null;
    $in_admin        = false;
    $action          = '';
    $is_block_editor = false;

    if ( $hook_name ) {
      $id = $hook_name;
    } elseif ( ! empty( $GLOBALS['hook_suffix'] ) ) {
      $id = $GLOBALS['hook_suffix'];
    }

    // For those pesky meta boxes.
    if ( $hook_name && post_type_exists( $hook_name ) ) {
      $post_type = $id;
      $id        = 'post'; // Changes later. Ends up being $base.
    } else {
      if ( '.php' === substr( $id, -4 ) ) {
        $id = substr( $id, 0, -4 );
      }

      if ( in_array( $id, array( 'post-new', 'link-add', 'media-new', 'user-new' ), true ) ) {
        $id     = substr( $id, 0, -4 );
        $action = 'add';
      }
    }

    if ( ! $post_type && $hook_name ) {
      if ( '-network' === substr( $id, -8 ) ) {
        $id       = substr( $id, 0, -8 );
        $in_admin = 'network';
      } elseif ( '-user' === substr( $id, -5 ) ) {
        $id       = substr( $id, 0, -5 );
        $in_admin = 'user';
      }

      $id = sanitize_key( $id );
      if ( 'edit-comments' !== $id && 'edit-tags' !== $id && 'edit-' === substr( $id, 0, 5 ) ) {
        $maybe = substr( $id, 5 );
        if ( taxonomy_exists( $maybe ) ) {
          $id       = 'edit-tags';
          $taxonomy = $maybe;
        } elseif ( post_type_exists( $maybe ) ) {
          $id        = 'edit';
          $post_type = $maybe;
        }
      }

      if ( ! $in_admin ) {
        $in_admin = 'site';
      }
    } else {
      if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) {
        $in_admin = 'network';
      } elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN ) {
        $in_admin = 'user';
      } else {
        $in_admin = 'site';
      }
    }

    if ( 'index' === $id ) {
      $id = 'dashboard';
    } elseif ( 'front' === $id ) {
      $in_admin = false;
    }

    $base = $id;

    // If this is the current screen, see if we can be more accurate for post types and taxonomies.
    if ( ! $hook_name ) {
      if ( isset( $_REQUEST['post_type'] ) ) {
        $post_type = post_type_exists( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : false;
      }
      if ( isset( $_REQUEST['taxonomy'] ) ) {
        $taxonomy = taxonomy_exists( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : false;
      }

      switch ( $base ) {
        case 'post':
          if ( isset( $_GET['post'] ) && isset( $_POST['post_ID'] ) && (int) $_GET['post'] !== (int) $_POST['post_ID'] ) {
            wp_die( __( 'A post ID mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 );
          } elseif ( isset( $_GET['post'] ) ) {
            $post_id = (int) $_GET['post'];
          } elseif ( isset( $_POST['post_ID'] ) ) {
            $post_id = (int) $_POST['post_ID'];
          } else {
            $post_id = 0;
          }

          if ( $post_id ) {
            $post = get_post( $post_id );
            if ( $post ) {
              $post_type = $post->post_type;

 View on GitHub View on Trac