Aucune description

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

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