Repository navigation
Fixed file loading helpers for upload ability #1416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1202,6 +1202,10 @@ private function buildManualSource( $input ) { | |
| return $errors; | ||
| } | ||
|
|
||
| // REST and MCP requests do not load the admin file helpers. | ||
| if ( ! function_exists( 'wp_tempnam' ) ) { | ||
| require_once ABSPATH . 'wp-admin/includes/file.php'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only reader is |
||
| } | ||
|
Comment on lines
+1205
to
+1208
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checked against visualizer-pro
Comment on lines
+1206
to
+1208
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bot's own search found that neither plugin loads its copy outside its own bridge entry point, so the trigger isn't reachable on a REST ability request. And if one were loaded first, core's |
||
| $tmpfile = wp_tempnam( Visualizer_Plugin::NAME ); | ||
| // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen | ||
| $handle = $tmpfile ? fopen( $tmpfile, 'w' ) : false; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
| /** | ||
| * Stop the SDK promotions from loading for requests carrying the test header. | ||
| * | ||
| * The promotions preload wp-admin/includes/file.php, which hides missing | ||
| * includes on sites that show no promotions. | ||
| */ | ||
|
|
||
| if ( defined( 'TI_E2E_TESTING' ) && isset( $_SERVER['HTTP_X_VISUALIZER_E2E_SKIP_PROMOTIONS'] ) ) { | ||
| add_filter( 'themeisle_sdk_ran_promos', '__return_true' ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| const { deleteAllCharts } = require( '../utils/common' ); | ||
|
|
||
| const UPSERT_PATH = '/wp-abilities/v1/abilities/visualizer/upsert-chart/run'; | ||
|
|
||
| // Keeps the admin file helpers unloaded, as on sites without SDK promotions. | ||
| const HEADERS = { 'X-Visualizer-E2E-Skip-Promotions': '1' }; | ||
|
|
||
| const CHART_INPUT = { | ||
| type: 'line', | ||
| columns: [ | ||
| { name: 'Month', type: 'string' }, | ||
| { name: 'Sales', type: 'number' }, | ||
| ], | ||
| rows: [ | ||
| [ 'Jan', 10 ], | ||
| [ 'Feb', 20 ], | ||
| ], | ||
| }; | ||
|
|
||
| test.describe( 'Chart abilities over REST', () => { | ||
| test.beforeEach( async ( { requestUtils } ) => { | ||
| await deleteAllCharts( requestUtils ); | ||
| } ); | ||
|
|
||
| test.afterAll( async ( { requestUtils } ) => { | ||
| await deleteAllCharts( requestUtils ); | ||
| } ); | ||
|
|
||
| test( 'upsert-chart dry run returns the parsed preview from manual data', async ( { | ||
| requestUtils, | ||
| } ) => { | ||
| const result = await requestUtils.rest( { | ||
| method: 'POST', | ||
| path: UPSERT_PATH, | ||
| headers: HEADERS, | ||
| data: { input: { ...CHART_INPUT, dry_run: true } }, | ||
| } ); | ||
|
|
||
| expect( result.dry_run ).toBe( true ); | ||
| expect( result.valid ).toBe( true ); | ||
| expect( result.parsed_preview ).toBeTruthy(); | ||
| expect( JSON.stringify( result.parsed_preview ) ).toContain( 'Feb' ); | ||
| } ); | ||
|
|
||
| test( 'upsert-chart creates a chart from manual data', async ( { | ||
| requestUtils, | ||
| } ) => { | ||
| const result = await requestUtils.rest( { | ||
| method: 'POST', | ||
| path: UPSERT_PATH, | ||
| headers: HEADERS, | ||
| data: { input: CHART_INPUT }, | ||
| } ); | ||
|
|
||
| expect( result.created ).toBe( true ); | ||
| expect( result.id ).toBeGreaterThan( 0 ); | ||
|
|
||
| const chart = await requestUtils.rest( { | ||
| path: `/wp/v2/visualizer/${ result.id }`, | ||
| params: { context: 'edit' }, | ||
| } ); | ||
| expect( chart.status ).toBe( 'publish' ); | ||
| } ); | ||
| } ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noteThe check tests onlywp_tempnam(), butfile.phpdeclares about 30 global functions. A plugin that declares one of them, such aslist_files(), without a guard and is loaded in the same request makes this include end the request with "Cannot redeclare". Is any such plugin loaded on REST ability requests? None means safe. One means the save fatals.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A plugin declaring
list_files()or similar without a guard would already crash every wp-admin request (wp-admin/includes/admin.php:31loadsfile.phpunconditionally) and core's REST media upload. Visualizer's remote imports also already load it unconditionally (Remote/Fetch.php:105). This PR adds no new way for that to happen, and the bot's directory search found no copy that collides on REST requests.