Parsed: 130730

  public static function get_user_data_from_wp_global_styles( $theme, $create_post = false, $post_status_filter = array( 'publish' ) ) {
    if ( ! $theme instanceof WP_Theme ) {
      $theme = wp_get_theme();
    }

    /*
		 * Bail early if the theme does not support a theme.json.
		 *
		 * Since WP_Theme_JSON_Resolver::theme_has_support() only supports the active
		 * theme, the extra condition for whether $theme is the active theme is
		 * present here.
		 */
    if ( $theme->get_stylesheet() === get_stylesheet() && ! static::theme_has_support() ) {
      return array();
    }

    $user_cpt         = array();
    $post_type_filter = 'wp_global_styles';
    $stylesheet       = $theme->get_stylesheet();
    $args             = array(
      'posts_per_page'      => 1,
      'orderby'             => 'date',
      'order'               => 'desc',
      'post_type'           => $post_type_filter,
      'post_status'         => $post_status_filter,
      'ignore_sticky_posts' => true,
      'no_found_rows'       => true,
      'tax_query'           => array(
        array(
          'taxonomy' => 'wp_theme',
          'field'    => 'name',
          'terms'    => $stylesheet,
        ),
      ),
    );

    $global_style_query = new WP_Query();
    $recent_posts       = $global_style_query->query( $args );
    if ( count( $recent_posts ) === 1 ) {
      $user_cpt = get_post( $recent_posts[0], ARRAY_A );
    } elseif ( $create_post ) {
      $cpt_post_id = wp_insert_post(
        array(
          'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
          'post_status'  => 'publish',
          'post_title'   => 'Custom Styles', // Do not make string translatable, see https://core.trac.wordpress.org/ticket/54518.
          'post_type'    => $post_type_filter,
          'post_name'    => sprintf( 'wp-global-styles-%s', urlencode( $stylesheet ) ),
          'tax_input'    => array(
            'wp_theme' => array( $stylesheet ),
          ),
        ),
        true
      );
      if ( ! is_wp_error( $cpt_post_id ) ) {
        $user_cpt = get_post( $cpt_post_id, ARRAY_A );
      }
    }

    return $user_cpt;
  }