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

Add new media endpoints #5281

Merged
merged 5 commits into from
Feb 28, 2017

Conversation

retrofox
Copy link
Contributor

@retrofox retrofox commented Oct 7, 2016

This PR adds new versions of get-media endpoint and add the new edit-media endpoint to the WP COM API.

Changes proposed in this Pull Request:

Add v1.2 [POST] /sites/$site/media/$mediaID/edit

The edit-media endpoint was added into the WP COM API a while ago. This patch adds the needed code to support the edit-media endpoint now for Jetpack sites.
The hardest part of this process is to detect when a media item should be added (new-media endpoint) or when it should be updated (edit-media endpoint).

The right action to take depends on the action parameter into the request body. it could be jetpack_upload_action (the only one action used at the moment) and jetpack_update_action. When the action is the last one, the attached metadata of the media item is updated according to the new uploaded media file instead of creating a new media item.
Also, other tasks are done in this editing process, such as store the original media file, keep the revision history, clean the remaining files when the media item is deleted, etc.

Add v1.2 [GET] /sites/$site/media/$mediaID

There aren't significant changes in this endpoint. It just adds some fields to the response body.

@jeherve jeherve added this to the 4.3.2 milestone Oct 7, 2016
@jeherve jeherve added the [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it label Oct 7, 2016
@retrofox retrofox changed the title Update/add get and update 1 2 media endpoints [WIP] Add new versions of get and update media endpoints Oct 7, 2016
@gwwar
Copy link
Contributor

gwwar commented Oct 7, 2016

cc @mcsf @gravityrail if we're missing anything major for a Jetpack enpoint

@retrofox
Copy link
Contributor Author

retrofox commented Oct 7, 2016

Something that we have to achieve is to be able to upload a new image without creating a new record into of dB.

As far as I see we should split the request into two requests. First one to upload the new image and the second one to update the image-post record.

For this reason, I think we should create a new Jetpack action, for instance, jetpack_update_file. The behavior behind this action should be similar to jetpack_upload_action which is tied the upload-media endpoint.

So the jetpack_update_file action is added we should just upload the file and do not create a new record. Finally, in the second step of this request, we could update the attached file of the media post with the new file uploaded in the previous step.

@retrofox
Copy link
Contributor Author

retrofox commented Oct 7, 2016

I wonder if we can modify upload_handler() to avoid creating a new record into dB: https://github.com/Automattic/jetpack/blob/master/class.jetpack.php#L3010

@mcsf
Copy link
Member

mcsf commented Oct 8, 2016

@gwwar: for Jetpack stuff, you probably meant the other Miguel, @lezama. Signed: the other other Miguel

@gwwar
Copy link
Contributor

gwwar commented Oct 9, 2016

😅 Sorry about that Miguel

@samhotchkiss samhotchkiss modified the milestones: 4.3.4, 4.3.2 Oct 11, 2016
@zinigor zinigor modified the milestones: 4.4, 4.3.3 Oct 25, 2016
@samhotchkiss
Copy link
Contributor

Removing the milestone until this is completed-- please feel free to move it back into the appropriate milestone when it's out of "In Progress"

@samhotchkiss samhotchkiss modified the milestones: Not Currently Planned, 4.4 Nov 9, 2016
@samhotchkiss
Copy link
Contributor

Closing this for now, feel free to reopen if it ever gets fresh attention

@jeherve jeherve deleted the update/add-get-and-update-1-2-media-endpoints branch February 8, 2017 14:57
@obenland
Copy link
Member

obenland commented Feb 14, 2017

See Automattic/wp-calypso#11241

@retrofox retrofox restored the update/add-get-and-update-1-2-media-endpoints branch February 15, 2017 21:13
@retrofox
Copy link
Contributor Author

Starting to work on this again.

@retrofox retrofox added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels Feb 28, 2017
@retrofox retrofox force-pushed the update/add-get-and-update-1-2-media-endpoints branch from d3ffed6 to f89ed2f Compare February 28, 2017 14:13

if ( ! empty( $original_media ) ) {
$original_file_parts = pathinfo( $original_media['file'] );
$original_file_ext = $original_file_parts['filename'];
Copy link
Member

Choose a reason for hiding this comment

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

This var is not used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. removing ...

}

// hook: clean revision history when the media item is deleted
$clean_revision_history = function( $media_id ) {
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately we need to support PHP 5.2.4 :(

Anonymous functions were introduced in 5.3

}
}

return $post_update_action;
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this may not be defined if there's no $attrs['alt'] set.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's right. I've decided to implement the same behavior that we already do with other endpoints which is silence these errors when these metadata aren't updated, like here: https://github.com/Automattic/jetpack/blob/master/json-endpoints/class.wpcom-json-api-update-media-v1-1-endpoint.php#L41

We still returning an error when the media item updating (post) fails.

* @param {String} $url - image URL to save locally
* @return {Array|WP_Error} An array with information about the new file saved or a WP_Error is something went wrong.
*/
private function build_file_array_from_url( $media_id, $url ) {
Copy link
Member

Choose a reason for hiding this comment

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

do we need $media_id param?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, If I'm not wrong it's needed to update the media item (post object).

$insert['ID'] = $media_id;
$update_action = wp_update_post( (object) $insert );

* WARNING: This file is distributed verbatim in Jetpack.
* There should be nothing WordPress.com specific in this file.
*
* @hide-in-jetpack
Copy link
Member

Choose a reason for hiding this comment

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

You can remove this block -- it should only be added to wpcom as it tells our syncing script to omit it when building out the commits.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it's confusing. tl;dr this @hide-in-jetpack tells a script that we use to hide this block from Jetpack and it's safe to remove from this PR.

Anyway, it's fine to leave it in too. Not a blocker.

/**
* Class to handle different actions related to media.
*/
class Media {
Copy link
Member

Choose a reason for hiding this comment

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

Let's namespace this Jetpack_

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cool. done


// hook: clean revision history when the media item is deleted
function clean_revision_history( $media_id ) {
error_log( 'mas se perdio en la guerra ' );
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this may have been left in...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦‍♂️ already removed

@retrofox retrofox force-pushed the update/add-get-and-update-1-2-media-endpoints branch 3 times, most recently from a3e2408 to 8b882c6 Compare February 28, 2017 15:35
@retrofox retrofox force-pushed the update/add-get-and-update-1-2-media-endpoints branch from 8b882c6 to 9941c20 Compare February 28, 2017 16:05
@rralian
Copy link
Contributor

rralian commented Feb 28, 2017

This is working well for me. And although github says changes requested, it looks like the changes @dereksmart asked for are handled. So if it looks good to him, I think this is good to 🚢

Copy link
Member

@dereksmart dereksmart 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 happy :) merging!

@dereksmart dereksmart merged commit fee5812 into master Feb 28, 2017
@dereksmart dereksmart deleted the update/add-get-and-update-1-2-media-endpoints branch February 28, 2017 19:28
@dereksmart dereksmart removed the [Status] Needs Review This PR is ready for review. label Feb 28, 2017
@retrofox
Copy link
Contributor Author

I'm happy too. :-D

jeherve added a commit that referenced this pull request Feb 28, 2017
dereksmart pushed a commit that referenced this pull request Feb 28, 2017
* Changelog: update stable tag and move changelog to changelog.txt

Also remove old releases from readme.txt to keep the changelog tab short.

* Changelog: add #5883

Also update the filter's docblock to match new version.

* Changelog: add #5938

* Changelog: add #6298

* Changelog: add #3405

* Changelog: add #5941

* Changelog: add #6239

* Changelog: add #6281

* Changelog: add #6303

* Changelog: add #6018

* Changelog: add #6300

* Changelog: add #6296

* Changelog: add #6130

* Changelog: add #6292

* Readme: remove extra "on".

* Changelog: add #6307

* Changelog: add #3297

* Changelog: add #6275

* Changelog: add #6321

* Changelog: add #6297

* Readme: update the support forum link anchor.

Anchor changed when WordPress.org forums were updated to bbPress 2

* Readme: update list of a12s, it wasn't up to date anymore!

* Changelog: add #6338

* Changelog: add #6337

* Changelog: add #6335

* Changelog: add #6333

* Testing List: first version of the 4.7 testing list.

* Changelog: add #6332

* Changelog: add #6325

* Changelog: add #6326

* Changelog: add #6339

* Changelog: add #6342

* Changelog: add #6343

* Changelog: add #6346

* Changelog: add #6347

* Changelog: add #6279

* Changelog: add #6306

* Changelog: add #6312

* Changelog: add #6316

* Changelog: add #6171

* Changelog: add #6317

* Changelog: add #6246

* Changelog: add #6263

* Changelog: add #4220

* Changelog: add #5888

* Changelog: add #3406

* Changelog: add #3637

* Changelog: add #6320

* Changelog: add #5992

* Changelog: add #6322

* Changelog: add #6324

* Changelog: add #6352

* Changelog: add #6355

* Changelog: add #6360

* Changelog: add #6362

* Changelog: add #6369, #6382

* Changelog: add #6370

* Changelog: add #6375

* Changelog: add #6383

* Changelog: add #6384

* Changelog: add #6386

* Changelog: add #6395

* Changelog: add #6403

* Changelog: add #6406

* Changelog: add #6418

* Changelog: add #6419

* Changelog: add #6434

* Changelog: add #6446

* Changelog: add #6006

* Changelog: add #6096

* Changelog: add #6399

* Changelog: fix typo.

@see #6331 (comment)

* Changelog: add #6440

* Changelog: add #6443

* Changelog: add #6445

* Changelog: add #6463

* Changelog: add #6468

* Changelog: add #6471

* Changelog: add #6474

* Changelog: add #6480

* Changelog: add #6497

* Changelog: add #6499

* Changelog: add #6514

* Changelog: add #6267

* Changelog: add #5940

* Changelog: add #6492

* Changelog: add #5281

* Changelog: add #6327

* Changelog: add #6451

* Changelog: add #6525

* Changelog: add #6530
@lezama
Copy link
Contributor

lezama commented Mar 1, 2017

yay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] WPCOM API [Pri] High Touches WP.com Files [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.