暫無描述

Packages.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Loads WooCommece packages from the /packages directory. These are packages developed outside of core.
  4. */
  5. namespace Automattic\WooCommerce;
  6. defined( 'ABSPATH' ) || exit;
  7. /**
  8. * Packages class.
  9. *
  10. * @since 3.7.0
  11. */
  12. class Packages {
  13. /**
  14. * Static-only class.
  15. */
  16. private function __construct() {}
  17. /**
  18. * Array of package names and their main package classes.
  19. *
  20. * @var array Key is the package name/directory, value is the main package class which handles init.
  21. */
  22. protected static $packages = array(
  23. 'woocommerce-blocks' => '\\Automattic\\WooCommerce\\Blocks\\Package',
  24. 'woocommerce-admin' => '\\Automattic\\WooCommerce\\Admin\\Composer\\Package',
  25. );
  26. /**
  27. * Init the package loader.
  28. *
  29. * @since 3.7.0
  30. */
  31. public static function init() {
  32. add_action( 'plugins_loaded', array( __CLASS__, 'on_init' ) );
  33. }
  34. /**
  35. * Callback for WordPress init hook.
  36. */
  37. public static function on_init() {
  38. self::load_packages();
  39. }
  40. /**
  41. * Checks a package exists by looking for it's directory.
  42. *
  43. * @param string $package Package name.
  44. * @return boolean
  45. */
  46. public static function package_exists( $package ) {
  47. return file_exists( dirname( __DIR__ ) . '/packages/' . $package );
  48. }
  49. /**
  50. * Loads packages after plugins_loaded hook.
  51. *
  52. * Each package should include an init file which loads the package so it can be used by core.
  53. */
  54. protected static function load_packages() {
  55. foreach ( self::$packages as $package_name => $package_class ) {
  56. if ( ! self::package_exists( $package_name ) ) {
  57. self::missing_package( $package_name );
  58. continue;
  59. }
  60. call_user_func( array( $package_class, 'init' ) );
  61. }
  62. // Proxies "activated_plugin" hook for embedded packages listen on WC plugin activation
  63. // https://github.com/woocommerce/woocommerce/issues/28697.
  64. if ( is_admin() ) {
  65. $activated_plugin = get_transient( 'woocommerce_activated_plugin' );
  66. if ( $activated_plugin ) {
  67. delete_transient( 'woocommerce_activated_plugin' );
  68. /**
  69. * WooCommerce is activated hook.
  70. *
  71. * @since 5.0.0
  72. * @param bool $activated_plugin Activated plugin path,
  73. * generally woocommerce/woocommerce.php.
  74. */
  75. do_action( 'woocommerce_activated_plugin', $activated_plugin );
  76. }
  77. }
  78. }
  79. /**
  80. * If a package is missing, add an admin notice.
  81. *
  82. * @param string $package Package name.
  83. */
  84. protected static function missing_package( $package ) {
  85. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  86. error_log( // phpcs:ignore
  87. sprintf(
  88. /* Translators: %s package name. */
  89. esc_html__( 'Missing the WooCommerce %s package', 'woocommerce' ),
  90. '<code>' . esc_html( $package ) . '</code>'
  91. ) . ' - ' . esc_html__( 'Your installation of WooCommerce is incomplete. If you installed WooCommerce from GitHub, please refer to this document to set up your development environment: https://github.com/woocommerce/woocommerce/wiki/How-to-set-up-WooCommerce-development-environment', 'woocommerce' )
  92. );
  93. }
  94. add_action(
  95. 'admin_notices',
  96. function() use ( $package ) {
  97. ?>
  98. <div class="notice notice-error">
  99. <p>
  100. <strong>
  101. <?php
  102. printf(
  103. /* Translators: %s package name. */
  104. esc_html__( 'Missing the WooCommerce %s package', 'woocommerce' ),
  105. '<code>' . esc_html( $package ) . '</code>'
  106. );
  107. ?>
  108. </strong>
  109. <br>
  110. <?php
  111. printf(
  112. /* translators: 1: is a link to a support document. 2: closing link */
  113. esc_html__( 'Your installation of WooCommerce is incomplete. If you installed WooCommerce from GitHub, %1$splease refer to this document%2$s to set up your development environment.', 'woocommerce' ),
  114. '<a href="' . esc_url( 'https://github.com/woocommerce/woocommerce/wiki/How-to-set-up-WooCommerce-development-environment' ) . '" target="_blank" rel="noopener noreferrer">',
  115. '</a>'
  116. );
  117. ?>
  118. </p>
  119. </div>
  120. <?php
  121. }
  122. );
  123. }
  124. }