Skip to content
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

Allow /sites/${site}/external-media/copy/pexels to insert post meta data #21659

Merged
merged 13 commits into from
Nov 12, 2021

Conversation

mreishus
Copy link
Contributor

@mreishus mreishus commented Nov 5, 2021

Changes proposed in this Pull Request:

  • Allow /sites/${site}/external-media/copy/pexels to insert post meta data

Does this pull request change what data or activity we track or use?

No

Testing instructions (Jetpack):

  • N/A

Testing Instructions (Unit Tests):

  • jetpack docker phpunit -- --filter=Endpoint_External_Media

Testing instructions (WPCOM):

  • Apply D69692-code to sandbox
  • Visit internal vertical-images tool (See: D69577-code for full instructions)
  • Choose a vertical
  • Enter a search term (example: "building") and press enter or click on magnifying glass
  • See photos
  • Click on at least on image to select it
  • Click on the "Add 1 photos to VERTICAL" button
  • see no feedback but use devtools to see that the POST happened :)
  • sandbox PduBjF-5-p2 (See: D69577-code for full instructions)
  • Visit https:///wp-admin/upload.php (See: D69577-code for full instructions)
  • click on the photo you uploaded
  • click on edit more details
  • On sandbox, edit wp-admin/post.php and add
$meta = get_post_meta( $post_id );
l(compact('meta'));

around line 37, inside the if ( $post_id ) { block

  • F5 the url (should look like https://<vertical site>/wp-admin/post.php?post=49&action=edit) and tail -f /tmp/php-errors
  • Expect to see the metadata

@matticbot
Copy link
Contributor

Caution: This PR has changes that must be merged to WordPress.com
Hello mreishus! These changes need to be synced to WordPress.com - If you 're an a11n, please commandeer and confirm D69692-code works as expected before merging this PR. Once this PR is merged, please commit the changes to WP.com. Thank you!
This revision will be updated with each commit to this PR

@github-actions github-actions bot added the [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ label Nov 5, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2021

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ⚠️ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks (other than "Required review") appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team review" label and ask someone from your team review the code.
Once you’ve done so, switch to the "[Status] Needs Review" label; someone from Jetpack Crew will then review this PR and merge it to be included in the next Jetpack release.


Jetpack plugin:

  • Next scheduled release: December 7, 2021.
  • Scheduled code freeze: November 30, 2021.

@github-actions github-actions bot added the [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! label Nov 5, 2021
@mreishus mreishus self-assigned this Nov 5, 2021
@mreishus mreishus added [Status] Needs Team Review and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Nov 5, 2021
Copy link
Member

@obenland obenland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the unit test you added!

// Only allow meta properties defined in the schema.
// None of the validation or sanitization functions are doing this for me.
$meta_allowed = array_keys( $this->media_schema['properties']['meta']['properties'] );
$meta_filtered = array_intersect_key( $item['meta'], array_flip( $meta_allowed ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's too bad additionalProperties doesn't cover that. I wonder if it doesn't work recursively but only in a top-level object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that's the case.

'additionalProperties' => false,
'properties' => array(
'vertical_id' => array(
'type' => 'string',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this, but after sending a vertical_id of test_id_with_spaces_at_end (there are multiple spaces at the end of this that github is filtering out), it's saved that way to the database, which makes me think it's not working. (Expected to see: Multiple spaces collapsed to one)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe all of these sanitations don't work nested?

'type' => 'object',
),
'orientations' => array(
'type' => 'array',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if defining an enum parameter would help with validation here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this, but it doesn't actually seem to work. Probably because of the same nested level thing. Maybe I should keep it anyway just to be more explicit?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that is really too bad

@mreishus mreishus force-pushed the add/external-media-copy-meta branch from 299d48f to 5686e66 Compare November 8, 2021 16:06
@mreishus mreishus requested a review from obenland November 8, 2021 16:16
@mreishus
Copy link
Contributor Author

mreishus commented Nov 8, 2021

Updated w/ additional validation.

@obenland
Copy link
Member

obenland commented Nov 8, 2021

@mreishus New idea: Let's sanitize the meta part separately:

public $meta_schema    => array(
 				'type'                 => 'object',
 				'additionalProperties' => false,
 				'properties'           => array(
 					'vertical_id'   => array(
 						'type'   => 'string',
 						'format' => 'text-field',
 					),
 					'pexels_object' => array(
 						'type' => 'object',
 					),
 					'orientations'  => array(
 						'type' => 'array',
 						'enum' => array( 'landscape', 'portrait', 'square' ),
 					),
 				),
 			);

In sanitize_media:

public function sanitize_media( $param ) {
		$param.        = $this->prepare_media_param( $param );
		$param['meta'] = rest_sanitize_value_from_schema( $param['meta'], $this->meta_schema );
		
		return rest_sanitize_value_from_schema( $param, $this->media_schema );
	}

Does that work?

@mreishus
Copy link
Contributor Author

mreishus commented Nov 8, 2021

@mreishus New idea: Let's sanitize the meta part separately:
Does that work?

No, it doesn't work. The closest I found was this:

	public function validate_media( $param ) {
		$param = $this->prepare_media_param( $param );
		foreach ( array_keys($param) as $i ) {
			$param[$i]['meta'] = rest_validate_value_from_schema( $param[$i]['meta'], $this->meta_schema );
		}

		return rest_validate_value_from_schema( $param, $this->media_schema, 'media' );
	}

That ends up replacing one of the sub-parts of $param with a WP_Error object:

WP_Error Object                                                               
(                           
    [errors] => Array
        (                  
            [rest_not_in_enum] => Array 
                (           
                    [0] => [orientations] is not one of portrait and square.                                                                                
                )                                                             
                                                                              
        )                                                                     
                                       
    [error_data] => Array 
        (        
        )                                                                                                                                                   
                                                                                                                                                            
    [additional_data:protected] => Array                                                                                                                    
        (                                                                                                                                                   
        )                                                                                                                                                   
                                                                                                                                                            
)                                                                                                                                                           

However, whatever is downstream/upstream of this and looking for errors doesn't find it, and the invalid orientations are saved anyway, and no error is passed to the user.

For sanitization, neither

public function sanitize_media( $param ) {
		$param        = $this->prepare_media_param( $param );
		$param['meta'] = rest_sanitize_value_from_schema( $param['meta'], $this->meta_schema );
		
		return rest_sanitize_value_from_schema( $param, $this->media_schema );
	}

nor

	public function sanitize_media( $param ) {
		$param = $this->prepare_media_param( $param );
		foreach ( array_keys($param) as $i ) {
			$param[$i]['meta'] = rest_sanitize_value_from_schema( $param[$i]['meta'], $this->meta_schema );
		}
		return rest_sanitize_value_from_schema( $param, $this->media_schema );
	}

changes vertical_id=test_id_with_spaces_at_end (multiple spaces) when given the definition

	public $meta_schema = array(
		'type'                 => 'object',
		'additionalProperties' => false,
		'properties'           => array(
			'vertical_id'   => array(
				'type'   => 'string',
				'format' => 'text-field',
			),
			'pexels_object' => array(
				'type' => 'object',
			),
			'orientations'  => array(
				'type' => 'array',
				'enum' => array( 'portrait', 'square' ),
			),
		),
	);

Additionally, I cannot find any instances of format.*text-field in jetpack or wpcom (except for minified files with very long lines).

@mreishus
Copy link
Contributor Author

mreishus commented Nov 8, 2021

We figured out a way to get the schema validation working in slack chat; updated.

@mreishus mreishus force-pushed the add/external-media-copy-meta branch from bd7be1b to e9c3912 Compare November 8, 2021 19:20
@mreishus
Copy link
Contributor Author

mreishus commented Nov 8, 2021

Reworked with adding a missing array layer and removing workarounds

sixhours
sixhours previously approved these changes Nov 8, 2021
Copy link
Contributor

@sixhours sixhours left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing the correct metadata come through:
Screen Shot 2021-11-08 at 3 11 36 PM

@mreishus mreishus added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Team Review labels Nov 8, 2021
@mreishus mreishus requested a review from jeherve November 8, 2021 21:57
@mreishus mreishus force-pushed the add/external-media-copy-meta branch from e9c3912 to a5aac22 Compare November 9, 2021 22:12
@mreishus
Copy link
Contributor Author

mreishus commented Nov 9, 2021

Rebased, only need crew review I believe

@mreishus mreishus requested a review from sixhours November 9, 2021 22:12
@mreishus mreishus force-pushed the add/external-media-copy-meta branch from a5aac22 to b1f7b36 Compare November 10, 2021 15:17
Copy link
Contributor

@sixhours sixhours left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still working after rebase! 👍

@jeherve jeherve added this to the jetpack/10.4 milestone Nov 11, 2021
@jeherve jeherve added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Nov 11, 2021
@mreishus mreishus enabled auto-merge (squash) November 11, 2021 22:35
@mreishus
Copy link
Contributor Author

@jeherve I can't seem to merge, only auto-merge. I see the required check Tests / PHP tests: PHP 8.0 WP latest (pull_request) Failing after 3m — PHP tests: PHP 8.0 WP latest is failing. Here's the message, I'm not sure if this is a new failure or an expected one:
2021-11-11_16-36

Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the message, I'm not sure if this is a new failure or an expected one:

I think that was a one-off. I've relaunched the test, it should be okay this time. 🤞

@mreishus mreishus merged commit 11cda7a into master Nov 12, 2021
@mreishus mreishus deleted the add/external-media-copy-meta branch November 12, 2021 16:57
@github-actions github-actions bot removed the [Status] Ready to Merge Go ahead, you can push that green button! label Nov 12, 2021
@github-actions
Copy link
Contributor

Great news! One last step: head over to your WordPress.com diff, D69692-code, and commit it.
Once you've done so, come back to this PR and add a comment with your changeset ID.

Thank you!

@mreishus
Copy link
Contributor Author

r234959

davidlonjon added a commit that referenced this pull request Nov 16, 2021
* master: (22 commits)
  VideoPress: reload block on rating change (#21653)
  Assets: Changelog for package version 1.12.0 (#21744)
  assets: Add `wp_register_script` wrapper (and then use it everywhere) (#21689)
  eslint-config-target-es: Configure mirror repo (#21731)
  Use monorepo `validate-es` script to validate Webpack builds (#21729)
  Backup: Replace daily backup references/upsell links with new real-time products (#21715)
  Likes: reimplement non-admin portions without jQuery (#21726)
  Autoloader: Not activated autoload queue is false (#21517)
  Sync: add a new method, do_only_first_initial_sync (#21676)
  webpack-config: Configure minifier to preserve translator comments (#21667)
  webpack-config: Use `@automattic/babel-plugin-preserve-i18n` (#21700)
  Create eslint-config-target-es JS package (#21660)
  webpack-config: Fork calypso-build's mini-css-with-rtl plugin (#21595)
  Allow /sites/${site}/external-media/copy/pexels to insert post meta data  (#21659)
  jetpack: Don't set Webpack's `output.pathinfo` in production builds (#21727)
  Boost: Implement support for loading stylesheets when JavaScript is disabled in the context Critical CSS being enabled (#21713)
  RNA: export the Connection store (#21388)
  Display notice when user has unactivated product license keys (#21474)
  Gardening: ensure it can use Composer (#21712)
  Nav Unification: Display the stats sparkline on WP Admin for Atomic sites (#21655)
  ...
davidlonjon added a commit that referenced this pull request Nov 16, 2021
* master:
  VideoPress: reload block on rating change (#21653)
  Assets: Changelog for package version 1.12.0 (#21744)
  assets: Add `wp_register_script` wrapper (and then use it everywhere) (#21689)
  eslint-config-target-es: Configure mirror repo (#21731)
  Use monorepo `validate-es` script to validate Webpack builds (#21729)
  Backup: Replace daily backup references/upsell links with new real-time products (#21715)
  Likes: reimplement non-admin portions without jQuery (#21726)
  Autoloader: Not activated autoload queue is false (#21517)
  Sync: add a new method, do_only_first_initial_sync (#21676)
  webpack-config: Configure minifier to preserve translator comments (#21667)
  webpack-config: Use `@automattic/babel-plugin-preserve-i18n` (#21700)
  Create eslint-config-target-es JS package (#21660)
  webpack-config: Fork calypso-build's mini-css-with-rtl plugin (#21595)
  Allow /sites/${site}/external-media/copy/pexels to insert post meta data  (#21659)
  jetpack: Don't set Webpack's `output.pathinfo` in production builds (#21727)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ Touches WP.com Files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants