PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/packages/woocommerce-admin/src/Features/Navigation/Init.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 161 lines | 102 code | 24 blank | 35 comment | 11 complexity | ced45c8be9955dcafe120367d04f66f7 MD5 | raw file
  1. <?php
  2. /**
  3. * Navigation Experience
  4. *
  5. * @package Woocommerce Admin
  6. */
  7. namespace Automattic\WooCommerce\Admin\Features\Navigation;
  8. use Automattic\WooCommerce\Admin\Loader;
  9. use Automattic\WooCommerce\Admin\Survey;
  10. use Automattic\WooCommerce\Admin\Features\Features;
  11. use Automattic\WooCommerce\Admin\Features\Navigation\Screen;
  12. use Automattic\WooCommerce\Admin\Features\Navigation\Menu;
  13. use Automattic\WooCommerce\Admin\Features\Navigation\CoreMenu;
  14. /**
  15. * Contains logic for the Navigation
  16. */
  17. class Init {
  18. /**
  19. * Option name used to toggle this feature.
  20. */
  21. const TOGGLE_OPTION_NAME = 'woocommerce_navigation_enabled';
  22. /**
  23. * Hook into WooCommerce.
  24. */
  25. public function __construct() {
  26. add_filter( 'woocommerce_settings_features', array( $this, 'add_feature_toggle' ) );
  27. add_action( 'update_option_' . self::TOGGLE_OPTION_NAME, array( $this, 'reload_page_on_toggle' ), 10, 2 );
  28. add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_opt_out_scripts' ) );
  29. if ( Features::is_enabled( 'navigation' ) ) {
  30. Menu::instance()->init();
  31. CoreMenu::instance()->init();
  32. Screen::instance()->init();
  33. }
  34. }
  35. /**
  36. * Add the feature toggle to the features settings.
  37. *
  38. * @param array $features Feature sections.
  39. * @return array
  40. */
  41. public static function add_feature_toggle( $features ) {
  42. $description = __(
  43. 'Adds the new WooCommerce navigation experience to the dashboard',
  44. 'woocommerce'
  45. );
  46. $update_text = '';
  47. $needs_update = version_compare( get_bloginfo( 'version' ), '5.6', '<' );
  48. if ( $needs_update && current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
  49. $update_text = sprintf(
  50. /* translators: 1: line break tag, 2: open link to WordPress update link, 3: close link tag. */
  51. __( '%1$s %2$sUpdate WordPress to enable the new navigation%3$s', 'woocommerce' ),
  52. '<br/>',
  53. '<a href="' . self_admin_url( 'update-core.php' ) . '" target="_blank">',
  54. '</a>'
  55. );
  56. }
  57. $features[] = array(
  58. 'title' => __( 'Navigation', 'woocommerce' ),
  59. 'desc' => $description . $update_text,
  60. 'id' => self::TOGGLE_OPTION_NAME,
  61. 'type' => 'checkbox',
  62. 'class' => $needs_update ? 'disabled' : '',
  63. );
  64. return $features;
  65. }
  66. /**
  67. * Determine if sufficient versions are present to support Navigation feature
  68. */
  69. public function is_nav_compatible() {
  70. include_once ABSPATH . 'wp-admin/includes/plugin.php';
  71. $gutenberg_minimum_version = '9.0.0'; // https://github.com/WordPress/gutenberg/releases/tag/v9.0.0.
  72. $wp_minimum_version = '5.6';
  73. $has_gutenberg = is_plugin_active( 'gutenberg/gutenberg.php' );
  74. $gutenberg_version = $has_gutenberg ? get_plugin_data( WP_PLUGIN_DIR . '/gutenberg/gutenberg.php' )['Version'] : false;
  75. if ( $gutenberg_version && version_compare( $gutenberg_version, $gutenberg_minimum_version, '>=' ) ) {
  76. return true;
  77. }
  78. // Get unmodified $wp_version.
  79. include ABSPATH . WPINC . '/version.php';
  80. // Strip '-src' from the version string. Messes up version_compare().
  81. $wp_version = str_replace( '-src', '', $wp_version );
  82. if ( version_compare( $wp_version, $wp_minimum_version, '>=' ) ) {
  83. return true;
  84. }
  85. return false;
  86. }
  87. /**
  88. * Reloads the page when the option is toggled to make sure all nav features are loaded.
  89. *
  90. * @param string $old_value Old value.
  91. * @param string $value New value.
  92. */
  93. public static function reload_page_on_toggle( $old_value, $value ) {
  94. if ( $old_value === $value ) {
  95. return;
  96. }
  97. if ( 'yes' !== $value ) {
  98. update_option( 'woocommerce_navigation_show_opt_out', 'yes' );
  99. }
  100. if ( isset( $_SERVER['REQUEST_URI'] ) ) {
  101. wp_safe_redirect( wp_unslash( $_SERVER['REQUEST_URI'] ) );
  102. exit();
  103. }
  104. }
  105. /**
  106. * Enqueue the opt out scripts.
  107. */
  108. public function maybe_enqueue_opt_out_scripts() {
  109. if ( 'yes' !== get_option( 'woocommerce_navigation_show_opt_out', 'no' ) ) {
  110. return;
  111. }
  112. $rtl = is_rtl() ? '.rtl' : '';
  113. wp_enqueue_style(
  114. 'wc-admin-navigation-opt-out',
  115. Loader::get_url( "navigation-opt-out/style{$rtl}", 'css' ),
  116. array( 'wp-components' ),
  117. Loader::get_file_version( 'css' )
  118. );
  119. $script_assets_filename = Loader::get_script_asset_filename( 'wp-admin-scripts', 'navigation-opt-out' );
  120. $script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . 'wp-admin-scripts/' . $script_assets_filename;
  121. wp_enqueue_script(
  122. 'wc-admin-navigation-opt-out',
  123. Loader::get_url( 'wp-admin-scripts/navigation-opt-out', 'js' ),
  124. array_merge( array( WC_ADMIN_APP ), $script_assets ['dependencies'] ),
  125. Loader::get_file_version( 'js' ),
  126. true
  127. );
  128. wp_localize_script(
  129. 'wc-admin-navigation-opt-out',
  130. 'surveyData',
  131. array(
  132. 'url' => Survey::get_url( '/new-navigation-opt-out' ),
  133. )
  134. );
  135. delete_option( 'woocommerce_navigation_show_opt_out' );
  136. }
  137. }