PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-notices.php

https://gitlab.com/webkod3r/tripolis
PHP | 187 lines | 107 code | 25 blank | 55 comment | 21 complexity | 251ce5933f0e56141292f4fbbaa1f33f MD5 | raw file
  1. <?php
  2. /**
  3. * Display notices in admin
  4. *
  5. * @author WooThemes
  6. * @category Admin
  7. * @package WooCommerce/Admin
  8. * @version 2.3.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. /**
  14. * WC_Admin_Notices Class.
  15. */
  16. class WC_Admin_Notices {
  17. /**
  18. * Array of notices - name => callback.
  19. * @var array
  20. */
  21. private $core_notices = array(
  22. 'install' => 'install_notice',
  23. 'update' => 'update_notice',
  24. 'template_files' => 'template_file_check_notice',
  25. 'theme_support' => 'theme_check_notice'
  26. );
  27. /**
  28. * Constructor.
  29. */
  30. public function __construct() {
  31. add_action( 'switch_theme', array( $this, 'reset_admin_notices' ) );
  32. add_action( 'woocommerce_installed', array( $this, 'reset_admin_notices' ) );
  33. add_action( 'wp_loaded', array( $this, 'hide_notices' ) );
  34. if ( current_user_can( 'manage_woocommerce' ) ) {
  35. add_action( 'admin_print_styles', array( $this, 'add_notices' ) );
  36. }
  37. }
  38. /**
  39. * Remove all notices.
  40. */
  41. public static function remove_all_notices() {
  42. delete_option( 'woocommerce_admin_notices' );
  43. }
  44. /**
  45. * Reset notices for themes when switched or a new version of WC is installed.
  46. */
  47. public function reset_admin_notices() {
  48. if ( ! current_theme_supports( 'woocommerce' ) && ! in_array( get_option( 'template' ), wc_get_core_supported_themes() ) ) {
  49. self::add_notice( 'theme_support' );
  50. }
  51. self::add_notice( 'template_files' );
  52. }
  53. /**
  54. * Show a notice.
  55. * @param string $name
  56. */
  57. public static function add_notice( $name ) {
  58. $notices = array_unique( array_merge( get_option( 'woocommerce_admin_notices', array() ), array( $name ) ) );
  59. update_option( 'woocommerce_admin_notices', $notices );
  60. }
  61. /**
  62. * Remove a notice from being displayed.
  63. * @param string $name
  64. */
  65. public static function remove_notice( $name ) {
  66. $notices = array_diff( get_option( 'woocommerce_admin_notices', array() ), array( $name ) );
  67. update_option( 'woocommerce_admin_notices', $notices );
  68. }
  69. /**
  70. * See if a notice is being shown.
  71. * @param string $name
  72. * @return boolean
  73. */
  74. public static function has_notice( $name ) {
  75. return in_array( $name, get_option( 'woocommerce_admin_notices', array() ) );
  76. }
  77. /**
  78. * Hide a notice if the GET variable is set.
  79. */
  80. public function hide_notices() {
  81. if ( isset( $_GET['wc-hide-notice'] ) && isset( $_GET['_wc_notice_nonce'] ) ) {
  82. if ( ! wp_verify_nonce( $_GET['_wc_notice_nonce'], 'woocommerce_hide_notices_nonce' ) ) {
  83. wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
  84. }
  85. if ( ! current_user_can( 'manage_woocommerce' ) ) {
  86. wp_die( __( 'Cheatin&#8217; huh?', 'woocommerce' ) );
  87. }
  88. $hide_notice = sanitize_text_field( $_GET['wc-hide-notice'] );
  89. self::remove_notice( $hide_notice );
  90. do_action( 'woocommerce_hide_' . $hide_notice . '_notice' );
  91. }
  92. }
  93. /**
  94. * Add notices + styles if needed.
  95. */
  96. public function add_notices() {
  97. $notices = get_option( 'woocommerce_admin_notices', array() );
  98. if ( $notices ) {
  99. wp_enqueue_style( 'woocommerce-activation', plugins_url( '/assets/css/activation.css', WC_PLUGIN_FILE ) );
  100. foreach ( $notices as $notice ) {
  101. if ( ! empty( $this->core_notices[ $notice ] ) && apply_filters( 'woocommerce_show_admin_notice', true, $notice ) ) {
  102. add_action( 'admin_notices', array( $this, $this->core_notices[ $notice ] ) );
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * If we need to update, include a message with the update button.
  109. */
  110. public function update_notice() {
  111. include( 'views/html-notice-update.php' );
  112. }
  113. /**
  114. * If we have just installed, show a message with the install pages button.
  115. */
  116. public function install_notice() {
  117. include( 'views/html-notice-install.php' );
  118. }
  119. /**
  120. * Show the Theme Check notice.
  121. */
  122. public function theme_check_notice() {
  123. if ( ! current_theme_supports( 'woocommerce' ) && ! in_array( get_option( 'template' ), wc_get_core_supported_themes() ) ) {
  124. include( 'views/html-notice-theme-support.php' );
  125. } else {
  126. self::remove_notice( 'theme_support' );
  127. }
  128. }
  129. /**
  130. * Show a notice highlighting bad template files.
  131. */
  132. public function template_file_check_notice() {
  133. $core_templates = WC_Admin_Status::scan_template_files( WC()->plugin_path() . '/templates' );
  134. $outdated = false;
  135. foreach ( $core_templates as $file ) {
  136. $theme_file = false;
  137. if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
  138. $theme_file = get_stylesheet_directory() . '/' . $file;
  139. } elseif ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $file ) ) {
  140. $theme_file = get_stylesheet_directory() . '/woocommerce/' . $file;
  141. } elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
  142. $theme_file = get_template_directory() . '/' . $file;
  143. } elseif( file_exists( get_template_directory() . '/woocommerce/' . $file ) ) {
  144. $theme_file = get_template_directory() . '/woocommerce/' . $file;
  145. }
  146. if ( $theme_file !== false ) {
  147. $core_version = WC_Admin_Status::get_file_version( WC()->plugin_path() . '/templates/' . $file );
  148. $theme_version = WC_Admin_Status::get_file_version( $theme_file );
  149. if ( $core_version && $theme_version && version_compare( $theme_version, $core_version, '<' ) ) {
  150. $outdated = true;
  151. break;
  152. }
  153. }
  154. }
  155. if ( $outdated ) {
  156. include( 'views/html-notice-template-check.php' );
  157. } else {
  158. self::remove_notice( 'template_files' );
  159. }
  160. }
  161. }
  162. new WC_Admin_Notices();