WC_GZD_Dependencies

The WooCommerce Germanized WC GZD Dependencies class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-gzd-dependencies.php  
  1. class WC_GZD_Dependencies { 
  2.  
  3. /** 
  4. * Single instance of WooCommerce Germanized Main Class 
  5. * @var object 
  6. */ 
  7. protected static $_instance = null; 
  8.  
  9. public $loadable = true; 
  10.  
  11. public $plugin = null; 
  12.  
  13. public $prefix = 'gzd'; 
  14.  
  15. public $plugins = array(); 
  16.  
  17. public $wc_supports_crud = false; 
  18.  
  19. public $plugins_header = array( 
  20. 'woocommerce' => array(  
  21. 'name' => 'WooCommerce',  
  22. 'tested' => '',  
  23. 'requires' => '',  
  24. 'version' => '',  
  25. 'version_prefix' => 'woocommerce',  
  26. ),  
  27. ); 
  28.  
  29. public $plugins_result = array( 
  30. 'outdated' => array(),  
  31. 'unactivated' => array(),  
  32. 'untested' => array(),  
  33. ); 
  34.  
  35. public static function instance( $plugin = null ) { 
  36. if ( is_null( self::$_instance ) ) { 
  37. self::$_instance = new self( $plugin ); 
  38. return self::$_instance; 
  39.  
  40. /** 
  41. * Cloning is forbidden. 
  42. * @since 1.0 
  43. */ 
  44. public function __clone() { 
  45. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized-pro' ), '1.0' ); 
  46.  
  47. /** 
  48. * Unserializing instances of this class is forbidden. 
  49. * @since 1.0 
  50. */ 
  51. public function __wakeup() { 
  52. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized-pro' ), '1.0' ); 
  53.  
  54. public function __construct( $plugin = null ) { 
  55.  
  56. if ( ! $plugin ) { 
  57. $plugin = WC_germanized(); 
  58.  
  59. $this->plugin = $plugin; 
  60.  
  61. // Set whether current WooCommerce Version supports CRUD 
  62. $this->set_wc_supports_crud(); 
  63.  
  64. if ( $plugin->version != get_option( 'woocommerce_' . $this->prefix . '_version' ) ) { 
  65. $this->delete_cached_plugin_header_data(); 
  66.  
  67. $this->plugins = (array) get_option( 'active_plugins', array() ); 
  68.  
  69. if ( is_multisite() ) 
  70. $this->plugins = array_merge( $this->plugins, get_site_option( 'active_sitewide_plugins', array() ) ); 
  71.  
  72. $this->parse_plugin_header_data(); 
  73.  
  74. // Set Plugin versions 
  75. foreach ( $this->plugins_header as $plugin => $data ) { 
  76. $this->plugins_header[ $plugin ][ 'version' ] = $this->get_plugin_version( $data[ 'version_prefix' ] ); 
  77.  
  78. foreach ( $this->plugins_header as $plugin => $data ) { 
  79.  
  80. if ( ! $this->is_plugin_activated( $plugin ) ) { 
  81. $this->plugins_result[ 'unactivated' ][ $plugin ] = $data; 
  82. } elseif ( $this->is_plugin_outdated( $plugin ) ) { 
  83. $this->plugins_result[ 'outdated' ][ $plugin ] = $data; 
  84. } elseif ( ! $this->is_plugin_tested( $plugin ) ) { 
  85. $this->plugins_result[ 'untested' ][ $plugin ] = $data; 
  86.  
  87.  
  88. if ( ! empty( $this->plugins_result[ 'unactivated' ] ) || ! empty( $this->plugins_result[ 'outdated' ] ) ) { 
  89. $this->loadable = false; 
  90. remove_all_actions( 'admin_notices' ); 
  91. add_action( 'admin_notices', array( $this, 'dependencies_notice' ) ); 
  92. } elseif ( ! empty( $this->plugins_result[ 'untested' ] ) ) { 
  93. remove_all_actions( 'admin_notices' ); 
  94. add_action( 'admin_notices', array( $this, 'dependencies_notice' ) ); 
  95.  
  96.  
  97. public function set_wc_supports_crud() { 
  98. $this->wc_supports_crud = ( $this->compare_versions( $this->get_plugin_version( 'woocommerce' ), '2.7', '>=' ) ); 
  99.  
  100. public function delete_cached_plugin_header_data() { 
  101. delete_option( 'woocommerce_' . $this->prefix . '_plugin_header_data' ); 
  102.  
  103. protected function get_current_plugin_path() { 
  104. return $this->plugin->plugin_path() . '/woocommerce-germanized.php'; 
  105.  
  106. protected function parse_plugin_header_data() { 
  107.  
  108. $plugin_header_data = get_option( 'woocommerce_' . $this->prefix . '_plugin_header_data', array() ); 
  109.  
  110. if ( ! empty( $plugin_header_data ) ) { 
  111. $this->plugins_header = $plugin_header_data; 
  112. return; 
  113.  
  114. $plugin_header_check = array(); 
  115.  
  116. foreach ( $this->plugins_header as $plugin => $data ) { 
  117.  
  118. $plugin_header_check[ 'requires_' . $plugin ] = 'Requires at least ' . $data[ 'name' ]; 
  119. $plugin_header_check[ 'tested_' . $plugin ] = 'Tested up to ' . $data[ 'name' ]; 
  120.  
  121.  
  122. if ( ! empty( $plugin_header_check ) ) { 
  123.  
  124. $plugin_data = get_file_data( $this->get_current_plugin_path(), $plugin_header_check ); 
  125.  
  126. foreach ( $plugin_data as $key => $value ) { 
  127. if ( strpos( $key, 'requires' ) !== false ) { 
  128. $this->plugins_header[ str_replace( 'requires_', '', $key ) ][ 'requires' ] = $value; 
  129. } elseif ( strpos( $key, 'tested' ) !== false ) { 
  130. $this->plugins_header[ str_replace( 'tested_', '', $key ) ][ 'tested' ] = $value; 
  131.  
  132. update_option( 'woocommerce_' . $this->prefix . '_plugin_header_data', $this->plugins_header ); 
  133.  
  134. public function get_plugin_version( $plugin_slug ) { 
  135. $version = preg_replace( '#(\.0+)+($|-)#', '', get_option( $plugin_slug . '_version', '1.0' ) ); 
  136. return $version; 
  137.  
  138. public function is_plugin_outdated( $plugin ) { 
  139.  
  140. $plugin_data = ( isset( $this->plugins_header[ $plugin ] ) ? $this->plugins_header[ $plugin ] : false ); 
  141.  
  142. if ( ! $plugin_data || ! isset( $plugin_data[ 'requires' ] ) || empty( $plugin_data[ 'requires' ] ) ) 
  143. return false; 
  144.  
  145. if ( $this->compare_versions( $plugin_data[ 'requires' ], $this->get_plugin_version( $plugin_data[ 'version_prefix' ] ), ">" ) ) 
  146. return true; 
  147.  
  148. return false; 
  149.  
  150. public function get_plugin_path( $plugin ) { 
  151.  
  152. if ( strpos( $plugin, '.php' ) === false ) { 
  153. $plugin = trailingslashit( $plugin ) . $plugin . '.php'; 
  154.  
  155. return $plugin; 
  156.  
  157. public function is_plugin_activated( $plugin ) { 
  158.  
  159. if ( isset( $this->plugins_header[ $plugin ][ 'constant' ] ) ) { 
  160.  
  161. if ( ! defined( $this->plugins_header[ $plugin ][ 'constant' ] ) ) 
  162. return false; 
  163.  
  164. if ( strpos( $plugin, '.php' ) === false ) { 
  165. $plugin = trailingslashit( $plugin ) . $plugin . '.php'; 
  166.  
  167. return ( in_array( $plugin, $this->plugins ) || array_key_exists( $plugin, $this->plugins ) ); 
  168.  
  169. public function is_plugin_tested( $plugin ) { 
  170.  
  171. $plugin_data = ( isset( $this->plugins_header[ $plugin ] ) ? $this->plugins_header[ $plugin ] : false ); 
  172.  
  173. if ( ! $plugin_data || ! isset( $plugin_data[ 'tested' ] ) || empty( $plugin_data[ 'tested' ] ) ) 
  174. return true; 
  175.  
  176. if ( $this->compare_versions( $plugin_data[ 'tested' ], $this->get_plugin_version( $plugin_data[ 'version_prefix' ] ), ">=" ) ) 
  177. return true; 
  178.  
  179. return false; 
  180.  
  181.  
  182. /** 
  183. * This method removes accuration from $ver2 if this version is more accurate than $main_ver 
  184. */ 
  185. public function compare_versions( $main_ver, $ver2, $operator ) { 
  186.  
  187. $expl_main_ver = explode( '.', $main_ver ); 
  188. $expl_ver2 = explode( '.', $ver2 ); 
  189.  
  190. // Check if ver2 string is more accurate than main_ver 
  191. if ( sizeof( $expl_main_ver ) == 2 && sizeof( $expl_ver2 ) > 2 ) { 
  192. $new_ver_2 = array_slice( $expl_ver2, 0, 2 ); 
  193. $ver2 = implode( '.', $new_ver_2 ); 
  194.  
  195. return version_compare( $main_ver, $ver2, $operator ); 
  196.  
  197. public function woocommerce_version_supports_crud() { 
  198. return $this->wc_supports_crud; 
  199.  
  200. /** 
  201. * Checks if WooCommerce is activated 
  202. *  
  203. * @return boolean true if WooCommerce is activated 
  204. */ 
  205. public function is_woocommerce_activated() { 
  206. return $this->is_plugin_activated( 'woocommerce/woocommerce.php' ); 
  207.  
  208. public function is_loadable() { 
  209. return $this->loadable; 
  210.  
  211. public function dependencies_notice() { 
  212. global $dependencies; 
  213. $dependencies = $this; 
  214.  
  215. include_once( 'admin/views/html-notice-dependencies.php' ); 
  216.