UNPKG

@wordpress/e2e-test-utils-playwright

Version:
8 lines (7 loc) 4.27 kB
{ "version": 3, "sources": ["../../src/request-utils/themes.ts"], "sourcesContent": ["/**\n * Internal dependencies\n */\nimport type { RequestUtils } from './index';\nimport { WP_BASE_URL } from '../config';\n\nconst THEMES_URL = new URL( 'wp-admin/themes.php', WP_BASE_URL ).href;\n\nasync function activateTheme(\n\tthis: RequestUtils,\n\tthemeSlug: string\n): Promise< void > {\n\tlet response = await this.request.get( THEMES_URL );\n\tconst html = await response.text();\n\tconst optionalFolder = '([a-z0-9-]+%2F)?';\n\n\t// The `optionalFolder` regex part matches paths with a folder,\n\t// so it will return the first match, which might contain a folder.\n\t// First try to honor the included theme slug, that is, without a folder.\n\tlet matchGroup = html.match(\n\t\t`action=activate&amp;stylesheet=${ encodeURIComponent(\n\t\t\tthemeSlug\n\t\t) }&amp;_wpnonce=[a-z0-9]+`\n\t);\n\n\t// If the theme is not found, try to match the theme slug with a folder.\n\tif ( ! matchGroup ) {\n\t\tmatchGroup = html.match(\n\t\t\t`action=activate&amp;stylesheet=${ optionalFolder }${ encodeURIComponent(\n\t\t\t\tthemeSlug\n\t\t\t) }&amp;_wpnonce=[a-z0-9]+`\n\t\t);\n\t}\n\n\tif ( ! matchGroup ) {\n\t\tif ( html.includes( `data-slug=\"${ themeSlug }\"` ) ) {\n\t\t\t// The theme is already activated.\n\t\t\treturn;\n\t\t}\n\n\t\tthrow new Error( `The theme \"${ themeSlug }\" is not installed` );\n\t}\n\n\tconst [ activateQuery ] = matchGroup;\n\tconst activateLink =\n\t\tTHEMES_URL + `?${ activateQuery }`.replace( /&amp;/g, '&' );\n\n\tresponse = await this.request.get( activateLink );\n\n\tawait response.dispose();\n}\n\n// https://developer.wordpress.org/rest-api/reference/themes/#definition\nasync function getCurrentThemeGlobalStylesPostId( this: RequestUtils ) {\n\ttype ThemeItem = {\n\t\tstylesheet: string;\n\t\tstatus: string;\n\t\t_links: { 'wp:user-global-styles': { href: string }[] };\n\t};\n\tconst themes = await this.rest< ThemeItem[] >( {\n\t\tpath: '/wp/v2/themes',\n\t} );\n\tlet themeGlobalStylesId: string = '';\n\tif ( themes && themes.length ) {\n\t\tconst currentTheme: ThemeItem | undefined = themes.find(\n\t\t\t( { status } ) => status === 'active'\n\t\t);\n\n\t\tconst globalStylesURL =\n\t\t\tcurrentTheme?._links?.[ 'wp:user-global-styles' ]?.[ 0 ]?.href;\n\t\tif ( globalStylesURL ) {\n\t\t\tthemeGlobalStylesId = globalStylesURL?.split(\n\t\t\t\t'rest_route=/wp/v2/global-styles/'\n\t\t\t)[ 1 ];\n\t\t}\n\t}\n\treturn themeGlobalStylesId;\n}\n\n/**\n * Deletes all post revisions using the REST API.\n *\n * @param {} this RequestUtils.\n * @param {string|number} parentId Post attributes.\n */\nasync function getThemeGlobalStylesRevisions(\n\tthis: RequestUtils,\n\tparentId: number | string\n) {\n\t// Lists all global styles revisions.\n\treturn await this.rest< Record< string, Object >[] >( {\n\t\tpath: `/wp/v2/global-styles/${ parentId }/revisions`,\n\t\tparams: {\n\t\t\tper_page: 100,\n\t\t},\n\t} );\n}\n\nexport {\n\tactivateTheme,\n\tgetCurrentThemeGlobalStylesPostId,\n\tgetThemeGlobalStylesRevisions,\n};\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,oBAA4B;AAE5B,IAAM,aAAa,IAAI,IAAK,uBAAuB,yBAAY,EAAE;AAEjE,eAAe,cAEd,WACkB;AAClB,MAAI,WAAW,MAAM,KAAK,QAAQ,IAAK,UAAW;AAClD,QAAM,OAAO,MAAM,SAAS,KAAK;AACjC,QAAM,iBAAiB;AAKvB,MAAI,aAAa,KAAK;AAAA,IACrB,kCAAmC;AAAA,MAClC;AAAA,IACD,CAAE;AAAA,EACH;AAGA,MAAK,CAAE,YAAa;AACnB,iBAAa,KAAK;AAAA,MACjB,kCAAmC,cAAe,GAAI;AAAA,QACrD;AAAA,MACD,CAAE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