distributor icon indicating copy to clipboard operation
distributor copied to clipboard

Notice generated through 'the_content' filter

Open JeroenSormani opened this issue 6 years ago • 2 comments

Here the_content filter is used on a media post content; https://github.com/10up/distributor/blob/052307f5ec19a9aedf68588d15d77c5610c11eb2/includes/utils.php#L518

Within another plugin we're using (Members) this is causing a notice as its assuming that the_content filter is used within the loop and a global $post is available for use to get the post ID.

Notice: Trying to get property 'post_parent' of non-object in /wp-content/plugins/members/inc/template.php on line 95

Looking at WP Core usage all original the_content filters are used within a loop context, so I think its Members is rightfully assuming its within one, though its not doing additional checks.

I do think its a bit flawed to begin with as there's no post ID argument which probably should be there (it is in 1/7 uses in WP Core).

Not sure if this is something you'd consider solving in the plugin, but 2 possible solutions;

  • Prefix the filter; dt_the_content > big disadvantage of not having the original filters on the_content
  • Reproduce a loop context

JeroenSormani avatar Oct 05 '18 14:10 JeroenSormani

@JeroenSormani Thanks for the report here! When you get this notice, are you pushing or pulling content? And are you using an internal or external connection?

I installed the Members plugin but not seeing that notice pop up, but I'm guessing I'm testing this incorrectly. Also, are you seeing any other notices related to the_content filter? We use that filter in a few other places that I don't think set up the global $post variable, so I'd expect them to also have this issue (though in those scenarios, we do have a $post = get_post( $post_id ); call at the top of each function. Wondering if adding that to the media function will fix this)

dkotter avatar Nov 09 '18 17:11 dkotter

Sorry for the late reply - I've completely missed the notification!

I actually didn't push/pull when I got this - though it may also happen at such time (I haven't been much involved on the Distributor parts of the project so far). It happens when a media object is prepared for a response, in my case during a API call to update a WooCommerce product image.

Here's a full stack trace;


Notice:  Trying to get property 'post_parent' of non-object in /wp-content/plugins/members/inc/template.php on line 95
Stack trace:
  1. {main}() /Users/jeroensormani/.composer/vendor/laravel/valet/server.php:0
  2. require() /Users/jeroensormani/.composer/vendor/laravel/valet/server.php:151
  3. require() /wp-index.php:1
  4. require() wp/index.php:17
  5. wp() wp/wp-blog-header.php:16
  6. WP->main() wp/wp-includes/functions.php:960
  7. WP->parse_request() wp/wp-includes/class-wp.php:713
  8. do_action_ref_array() wp/wp-includes/class-wp.php:373
  9. WP_Hook->do_action() wp/wp-includes/plugin.php:515
 10. WP_Hook->apply_filters() wp/wp-includes/class-wp-hook.php:310
 11. rest_api_loaded() wp/wp-includes/class-wp-hook.php:286
 12. WP_REST_Server->serve_request() wp/wp-includes/rest-api.php:266
 13. WP_REST_Server->dispatch() wp/wp-includes/rest-api/class-wp-rest-server.php:321
 14. WC_REST_Products_V2_Controller->get_item() wp/wp-includes/rest-api/class-wp-rest-server.php:936
 15. WC_REST_Products_V2_Controller->prepare_object_for_response() /wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-rest-crud-controller.php:143
 16. WC_REST_Products_V2_Controller->add_additional_fields_to_object() /wp-content/plugins/woocommerce/includes/api/v2/class-wc-rest-products-v2-controller.php:166
 17. Distributor\RestApi\{closure}() wp/wp-includes/rest-api/endpoints/class-wp-rest-controller.php:394
 18. Distributor\Utils\prepare_media() /wp-content/plugins/distributor/includes/rest-api.php:187
 19. Distributor\Utils\format_media_post() /wp-content/plugins/distributor/includes/utils.php:321
 20. apply_filters() /wp-content/plugins/distributor/includes/utils.php:558
 21. WP_Hook->apply_filters() wp/wp-includes/plugin.php:203
 22. members_content_permissions_protect() wp/wp-includes/class-wp-hook.php:286
 23. members_can_current_user_view_post() /wp-content/plugins/members/inc/functions-content-permissions.php:180
 24. members_can_user_view_post() /wp-content/plugins/members/inc/template.php:117

In case you want to replicate the Woo request;

POST: https://domain.com/wp-json/wc/v2/products/{PRODUCT-ID}?consumer_key={KEY}&consumer_secret={SECRET}

-- Body
{"images":[{"src":"https://placehold.it/250x250","position":0}]}

Or a direct cURL if preferred;

curl -X POST \
  'https://waves.dev/wp-json/wc/v2/products/10241?consumer_key={KEY}&consumer_secret={SECRET}' \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{"images":[{"src":"https://placehold.it/250x250","position":0}]}'

JeroenSormani avatar Dec 04 '18 18:12 JeroenSormani