PageRenderTime 83ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/packages/woocommerce-blocks/woocommerce-gutenberg-products-block.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 247 lines | 135 code | 28 blank | 84 comment | 23 complexity | a52faddc1263265aa879f3db266be0a4 MD5 | raw file
  1. <?php
  2. /**
  3. * Plugin Name: WooCommerce Blocks
  4. * Plugin URI: https://github.com/woocommerce/woocommerce-gutenberg-products-block
  5. * Description: WooCommerce blocks for the Gutenberg editor.
  6. * Version: 6.9.0
  7. * Author: Automattic
  8. * Author URI: https://woocommerce.com
  9. * Text Domain: woo-gutenberg-products-block
  10. * Requires at least: 5.9
  11. * Requires PHP: 7.0
  12. * WC requires at least: 6.0
  13. * WC tested up to: 6.1
  14. *
  15. * @package WooCommerce\Blocks
  16. * @internal This file is only used when running as a feature plugin.
  17. */
  18. defined( 'ABSPATH' ) || exit;
  19. $minimum_wp_version = '5.9';
  20. if ( ! defined( 'WC_BLOCKS_IS_FEATURE_PLUGIN' ) ) {
  21. define( 'WC_BLOCKS_IS_FEATURE_PLUGIN', true );
  22. }
  23. /**
  24. * Whether notices must be displayed in the current page (plugins and WooCommerce pages).
  25. *
  26. * @since 2.5.0
  27. */
  28. function should_display_compatibility_notices() {
  29. $current_screen = get_current_screen();
  30. if ( ! isset( $current_screen ) ) {
  31. return false;
  32. }
  33. $is_plugins_page =
  34. property_exists( $current_screen, 'id' ) &&
  35. 'plugins' === $current_screen->id;
  36. $is_woocommerce_page =
  37. property_exists( $current_screen, 'parent_base' ) &&
  38. 'woocommerce' === $current_screen->parent_base;
  39. return $is_plugins_page || $is_woocommerce_page;
  40. }
  41. if ( version_compare( $GLOBALS['wp_version'], $minimum_wp_version, '<' ) ) {
  42. /**
  43. * Outputs for an admin notice about running WooCommerce Blocks on outdated WordPress.
  44. *
  45. * @since 2.5.0
  46. */
  47. function woocommerce_blocks_admin_unsupported_wp_notice() {
  48. if ( should_display_compatibility_notices() ) {
  49. ?>
  50. <div class="notice notice-error">
  51. <p><?php esc_html_e( 'The WooCommerce Blocks feature plugin requires a more recent version of WordPress and has been paused. Please update WordPress to continue enjoying WooCommerce Blocks.', 'woocommerce' ); ?></p>
  52. </div>
  53. <?php
  54. }
  55. }
  56. add_action( 'admin_notices', 'woocommerce_blocks_admin_unsupported_wp_notice' );
  57. return;
  58. }
  59. /**
  60. * Returns whether the current version is a development version
  61. * Note this relies on composer.json version, not plugin version.
  62. * Development installs of the plugin don't have a version defined in
  63. * composer json.
  64. *
  65. * @return bool True means the current version is a development version.
  66. */
  67. function woocommerce_blocks_is_development_version() {
  68. $composer_file = __DIR__ . '/composer.json';
  69. if ( ! is_readable( $composer_file ) ) {
  70. return false;
  71. }
  72. // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- including local file
  73. $composer_config = json_decode( file_get_contents( $composer_file ), true );
  74. return ! isset( $composer_config['version'] );
  75. }
  76. /**
  77. * If development version is detected and the Jetpack constant is not defined, show a notice.
  78. */
  79. if ( woocommerce_blocks_is_development_version() && ! defined( 'JETPACK_AUTOLOAD_DEV' ) ) {
  80. add_action(
  81. 'admin_notices',
  82. function() {
  83. echo '<div class="error"><p>';
  84. printf(
  85. /* translators: %1$s is referring to a php constant name, %2$s is referring to the wp-config.php file. */
  86. esc_html__( 'WooCommerce Blocks development mode requires the %1$s constant to be defined and true in your %2$s file. Otherwise you are loading the blocks package from WooCommerce core.', 'woocommerce' ),
  87. 'JETPACK_AUTOLOAD_DEV',
  88. 'wp-config.php'
  89. );
  90. echo '</p></div>';
  91. }
  92. );
  93. }
  94. /**
  95. * Autoload packages.
  96. *
  97. * The package autoloader includes version information which prevents classes in this feature plugin
  98. * conflicting with WooCommerce core.
  99. *
  100. * We want to fail gracefully if `composer install` has not been executed yet, so we are checking for the autoloader.
  101. * If the autoloader is not present, let's log the failure and display a nice admin notice.
  102. */
  103. $autoloader = __DIR__ . '/vendor/autoload_packages.php';
  104. if ( is_readable( $autoloader ) ) {
  105. require $autoloader;
  106. } else {
  107. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  108. error_log( // phpcs:ignore
  109. sprintf(
  110. /* translators: 1: composer command. 2: plugin directory */
  111. esc_html__( 'Your installation of the WooCommerce Blocks feature plugin is incomplete. Please run %1$s within the %2$s directory.', 'woocommerce' ),
  112. '`composer install`',
  113. '`' . esc_html( str_replace( ABSPATH, '', __DIR__ ) ) . '`'
  114. )
  115. );
  116. }
  117. /**
  118. * Outputs an admin notice if composer install has not been ran.
  119. */
  120. add_action(
  121. 'admin_notices',
  122. function() {
  123. ?>
  124. <div class="notice notice-error">
  125. <p>
  126. <?php
  127. printf(
  128. /* translators: 1: composer command. 2: plugin directory */
  129. esc_html__( 'Your installation of the WooCommerce Blocks feature plugin is incomplete. Please run %1$s within the %2$s directory.', 'woocommerce' ),
  130. '<code>composer install</code>',
  131. '<code>' . esc_html( str_replace( ABSPATH, '', __DIR__ ) ) . '</code>'
  132. );
  133. ?>
  134. </p>
  135. </div>
  136. <?php
  137. }
  138. );
  139. return;
  140. }
  141. add_action( 'plugins_loaded', array( '\Automattic\WooCommerce\Blocks\Package', 'init' ) );
  142. /**
  143. * Pre-filters script translations for the given file, script handle and text domain.
  144. *
  145. * @param string|false|null $translations JSON-encoded translation data. Default null.
  146. * @param string|false $file Path to the translation file to load. False if there isn't one.
  147. * @param string $handle Name of the script to register a translation domain to.
  148. * @param string $domain The text domain.
  149. * @return string JSON translations.
  150. */
  151. function woocommerce_blocks_get_i18n_data_json( $translations, $file, $handle, $domain ) {
  152. if ( 'woo-gutenberg-products-block' !== $domain ) {
  153. return $translations;
  154. }
  155. global $wp_scripts;
  156. if ( ! isset( $wp_scripts->registered[ $handle ], $wp_scripts->registered[ $handle ]->src ) ) {
  157. return $translations;
  158. }
  159. $handle_filename = basename( $wp_scripts->registered[ $handle ]->src );
  160. $locale = determine_locale();
  161. $lang_dir = WP_LANG_DIR . '/plugins';
  162. // Translations are always based on the unminified filename.
  163. if ( substr( $handle_filename, -7 ) === '.min.js' ) {
  164. $handle_filename = substr( $handle_filename, 0, -7 ) . '.js';
  165. }
  166. // WordPress 5.0 uses md5 hashes of file paths to associate translation
  167. // JSON files with the file they should be included for. This is an md5
  168. // of 'packages/woocommerce-blocks/build/FILENAME.js'.
  169. $core_path_md5 = md5( 'packages/woocommerce-blocks/build/' . $handle_filename );
  170. $core_json_file = $lang_dir . '/woocommerce-' . $locale . '-' . $core_path_md5 . '.json';
  171. $json_translations = is_file( $core_json_file ) && is_readable( $core_json_file ) ? file_get_contents( $core_json_file ) : false; // phpcs:ignore
  172. if ( ! $json_translations ) {
  173. return $translations;
  174. }
  175. // Rather than short circuit pre_load_script_translations, we will output
  176. // core translations using an inline script. This will allow us to continue
  177. // to load feature-plugin translations which may exist as well.
  178. $output = <<<JS
  179. ( function( domain, translations ) {
  180. var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
  181. localeData[""].domain = domain;
  182. wp.i18n.setLocaleData( localeData, domain );
  183. } )( "{$domain}", {$json_translations} );
  184. JS;
  185. printf( "<script type='text/javascript'>\n%s\n</script>\n", $output ); // phpcs:ignore
  186. // Finally, short circuit the pre_load_script_translations hook by returning
  187. // the translation JSON from the feature plugin, if it exists so this hook
  188. // does not run again for the current handle.
  189. $path_md5 = md5( 'build/' . $handle_filename );
  190. $json_file = $lang_dir . '/' . $domain . '-' . $locale . '-' . $path_md5 . '.json';
  191. $translations = is_file( $json_file ) && is_readable( $json_file ) ? file_get_contents( $json_file ) : false; // phpcs:ignore
  192. if ( $translations ) {
  193. return $translations;
  194. }
  195. // Return valid empty Jed locale.
  196. return '{ "locale_data": { "messages": { "": {} } } }';
  197. }
  198. add_filter( 'pre_load_script_translations', 'woocommerce_blocks_get_i18n_data_json', 10, 4 );
  199. /**
  200. * Filter translations so we can retrieve translations from Core when the original and the translated
  201. * texts are the same (which happens when translations are missing).
  202. *
  203. * @param string $translation Translated text based on WC Blocks translations.
  204. * @param string $text Text to translate.
  205. * @param string $domain The text domain.
  206. * @return string WC Blocks translation. In case it's the same as $text, Core translation.
  207. */
  208. function woocommerce_blocks_get_php_translation_from_core( $translation, $text, $domain ) {
  209. if ( 'woo-gutenberg-products-block' !== $domain ) {
  210. return $translation;
  211. }
  212. // When translation is the same, that could mean the string is not translated.
  213. // In that case, load it from core.
  214. if ( $translation === $text ) {
  215. return translate( $text, 'woocommerce' ); // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction, WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.TextDomainMismatch
  216. }
  217. return $translation;
  218. }
  219. add_filter( 'gettext', 'woocommerce_blocks_get_php_translation_from_core', 10, 3 );