Skip to content

Commit

Permalink
Added ability to skip serialization of attached media
Browse files Browse the repository at this point in the history
  • Loading branch information
SaFrMo committed Nov 14, 2018
1 parent 6a9096f commit b1ce02d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ Serializers are designed to take any WordPress object and translate it into JSON

- `rez_get_next_page_id($target_post)` - Get the ID of the page/post following the `$target_post`.
- `rez_get_previous_page_id($target_post)` - Get the ID of the page/post before the `$target_post`.
- `set_attachment_serialization($target_post, $value)` - Set whether or not a target post should automatically serialize its attached media. (Can save time when serializing pages with large amounts of attachments, for example.)

## Integrations

Expand Down
2 changes: 1 addition & 1 deletion rest-easy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Rest Easy
* Plugin URI: https://github.com/funkhaus/rest-easy
* Description: Rest-ify your site with zero effort and powerful customization.
* Version: 1.47
* Version: 1.48
* Author: Funkhaus
* Author URI: http://funkhaus.us
*/
Expand Down
11 changes: 7 additions & 4 deletions serializers/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ function rez_default_serialize_post( $target_post = null ){
}

// Add all attached media
$attached_media = get_attached_media('image', $target_post->ID);
$output['attachedMedia'] = array();
foreach( $attached_media as $single_attached_media ){
$output['attachedMedia'][] = apply_filters('rez_serialize_object', $single_attached_media);
// (Ignore if _custom_deactivate_attachment_serialization is set to 'on')
if ($target_post->_custom_deactivate_attachment_serialization != 'on'){
$attached_media = get_attached_media('image', $target_post->ID);
$output['attachedMedia'] = array();
foreach( $attached_media as $single_attached_media ){
$output['attachedMedia'][] = apply_filters('rez_serialize_object', $single_attached_media);
}
}

// Add terms
Expand Down
29 changes: 19 additions & 10 deletions utils.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
<?php

/*
* DEPRECATED: this used to be a custom function,
* but now just uses the built in WP utility function.
* wp_make_link_relative should now be used everywhere,
* this will be removed in future versions.
*/
function rez_remove_siteurl( $url ){
return wp_make_link_relative($url);
}

/*
* Next page/post ID
*/
Expand Down Expand Up @@ -91,3 +81,22 @@ function rez_get_previous_page_id($target_post = null) {

return $output;
}

/*
* Specify whether or not a page should serialize all of its attachments.
*/
function set_attachment_serialization($target_post, $val = true) {
$target_post = get_post($target_post);
update_post_meta($target_post->ID, '_custom_deactivate_attachment_serialization', $val ? 'on' : '')
}


/*
* DEPRECATED FUNCTION: this used to be a custom function,
* but now just uses the built in WP utility function.
* wp_make_link_relative should now be used everywhere,
* this will be removed in future versions.
*/
function rez_remove_siteurl( $url ){
return wp_make_link_relative($url);
}

0 comments on commit b1ce02d

Please sign in to comment.