class-wc-helper-plugin-info.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * WC_Helper_Plugin_Info Class
  7. *
  8. * Provides the "View Information" core modals with data for WooCommerce.com
  9. * hosted extensions.
  10. */
  11. class WC_Helper_Plugin_Info {
  12. /**
  13. * Loads the class, runs on init.
  14. */
  15. public static function load() {
  16. add_filter( 'plugins_api', array( __CLASS__, 'plugins_api' ), 20, 3 );
  17. }
  18. /**
  19. * Plugin information callback for Woo extensions.
  20. *
  21. * @param object $response The response core needs to display the modal.
  22. * @param string $action The requested plugins_api() action.
  23. * @param object $args Arguments passed to plugins_api().
  24. *
  25. * @return object An updated $response.
  26. */
  27. public static function plugins_api( $response, $action, $args ) {
  28. if ( 'plugin_information' !== $action ) {
  29. return $response;
  30. }
  31. if ( empty( $args->slug ) ) {
  32. return $response;
  33. }
  34. // Only for slugs that start with woo-
  35. if ( 0 !== strpos( $args->slug, 'woocommerce-com-' ) ) {
  36. return $response;
  37. }
  38. $clean_slug = str_replace( 'woocommerce-com-', '', $args->slug );
  39. // Look through update data by slug.
  40. $update_data = WC_Helper_Updater::get_update_data();
  41. $products = wp_list_filter( $update_data, array( 'slug' => $clean_slug ) );
  42. if ( empty( $products ) ) {
  43. return $response;
  44. }
  45. $product_id = array_keys( $products );
  46. $product_id = array_shift( $product_id );
  47. // Fetch the product information from the Helper API.
  48. $request = WC_Helper_API::get(
  49. add_query_arg(
  50. array(
  51. 'product_id' => absint( $product_id ),
  52. ), 'info'
  53. ), array( 'authenticated' => true )
  54. );
  55. $results = json_decode( wp_remote_retrieve_body( $request ), true );
  56. if ( ! empty( $results ) ) {
  57. $response = (object) $results;
  58. }
  59. return $response;
  60. }
  61. }
  62. WC_Helper_Plugin_Info::load();