PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/wordpress/wp-content/plugins/woocommerce-services/classes/class-wc-connect-options.php

https://bitbucket.org/spirulineteam/spiruline
PHP | 338 lines | 205 code | 38 blank | 95 comment | 28 complexity | f2164e03c1f643794fc7981ba5d39fe2 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, Apache-2.0, LGPL-2.1
  1. <?php
  2. if ( ! class_exists( 'WC_Connect_Options' ) ) {
  3. class WC_Connect_Options {
  4. /**
  5. * An array that maps a grouped option type to an option name.
  6. * @var array
  7. */
  8. private static $grouped_options = array(
  9. 'compact' => 'wc_connect_options',
  10. );
  11. /**
  12. * Returns an array of option names for a given type.
  13. *
  14. * @param string $type The type of option to return. Defaults to 'compact'.
  15. *
  16. * @return array
  17. */
  18. public static function get_option_names( $type = 'compact' ) {
  19. switch ( $type ) {
  20. case 'non_compact':
  21. return array(
  22. 'error_notice',
  23. 'services',
  24. 'services_last_update',
  25. 'last_heartbeat',
  26. 'origin_address',
  27. 'last_rate_request',
  28. );
  29. case 'shipping_method':
  30. return array(
  31. 'form_settings',
  32. 'failure_timestamp',
  33. );
  34. }
  35. return array(
  36. 'tos_accepted',
  37. 'store_guid',
  38. 'debug_logging_enabled',
  39. 'debug_display_enabled',
  40. 'payment_methods',
  41. 'account_settings',
  42. 'paper_size',
  43. 'packages',
  44. 'predefined_packages',
  45. 'shipping_methods_migrated',
  46. 'should_display_nux_after_jp_cxn_banner',
  47. 'needs_tax_environment_setup',
  48. 'stripe_state',
  49. 'banner_ppec',
  50. );
  51. }
  52. /**
  53. * Deletes all options created by WooCommerce Services, including shipping method options
  54. */
  55. public static function delete_all_options() {
  56. if ( defined( 'WOOCOMMERCE_CONNECT_DEV_SERVER_URL' ) ) {
  57. return;
  58. }
  59. foreach( self::$grouped_options as $group_key => $group ) {
  60. //delete legacy options
  61. foreach ( self::get_option_names( $group_key ) as $group_option ) {
  62. delete_option( "wc_connect_$group_option" );
  63. }
  64. delete_option( $group );
  65. }
  66. $non_compacts = self::get_option_names( 'non_compact' );
  67. foreach ( $non_compacts as $non_compact ) {
  68. delete_option( "wc_connect_$non_compact" );
  69. }
  70. self::delete_all_shipping_methods_options();
  71. }
  72. /**
  73. * Returns the requested option. Looks in wc_connect_options or wc_connect_$name as appropriate.
  74. *
  75. * @param string $name Option name
  76. * @param mixed $default (optional)
  77. *
  78. * @return mixed
  79. */
  80. public static function get_option( $name, $default = false ) {
  81. if ( self::is_valid( $name, 'non_compact' ) ) {
  82. return get_option( "wc_connect_$name", $default );
  83. }
  84. foreach ( array_keys( self::$grouped_options ) as $group ) {
  85. if ( self::is_valid( $name, $group ) ) {
  86. return self::get_grouped_option( $group, $name, $default );
  87. }
  88. }
  89. trigger_error( sprintf( 'Invalid WooCommerce Services option name: %s', $name ), E_USER_WARNING );
  90. return $default;
  91. }
  92. /**
  93. * Updates the single given option. Updates wc_connect_options or wc_connect_$name as appropriate.
  94. *
  95. * @param string $name Option name
  96. * @param mixed $value Option value
  97. *
  98. * @return bool Was the option successfully updated?
  99. */
  100. public static function update_option( $name, $value) {
  101. if ( self::is_valid( $name, 'non_compact' ) ) {
  102. return update_option( "wc_connect_$name", $value );
  103. }
  104. foreach ( array_keys( self::$grouped_options ) as $group ) {
  105. if ( self::is_valid( $name, $group ) ) {
  106. return self::update_grouped_option( $group, $name, $value );
  107. }
  108. }
  109. trigger_error( sprintf( 'Invalid WooCommerce Services option name: %s', $name ), E_USER_WARNING );
  110. return false;
  111. }
  112. /**
  113. * Deletes the given option. May be passed multiple option names as an array.
  114. * Updates wc_connect_options and/or deletes wc_connect_$name as appropriate.
  115. *
  116. * @param string|array $names
  117. *
  118. * @return bool Was the option successfully deleted?
  119. */
  120. public static function delete_option( $names ) {
  121. $result = true;
  122. $names = (array) $names;
  123. if ( ! self::is_valid( $names ) ) {
  124. trigger_error( sprintf( 'Invalid WooCommerce Services option names: %s', print_r( $names, 1 ) ), E_USER_WARNING );
  125. return false;
  126. }
  127. foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) {
  128. if ( ! delete_option( "wc_connect_$name" ) ) {
  129. $result = false;
  130. }
  131. }
  132. foreach ( array_keys( self::$grouped_options ) as $group ) {
  133. if ( ! self::delete_grouped_option( $group, $names ) ) {
  134. $result = false;
  135. }
  136. }
  137. return $result;
  138. }
  139. /**
  140. * Gets a shipping method option
  141. *
  142. * @param $name
  143. * @param $default
  144. * @param $service_id
  145. * @param $service_instance
  146. *
  147. * @return mixed
  148. */
  149. public static function get_shipping_method_option( $name, $default, $service_id, $service_instance = false ) {
  150. $option_name = self::get_shipping_method_option_name( $name, $service_id, $service_instance );
  151. if ( ! $option_name ) {
  152. trigger_error( sprintf( 'Invalid WooCommerce Services shipping method option name: %s', $name ), E_USER_WARNING );
  153. return $default;
  154. }
  155. return get_option( $option_name, $default );
  156. }
  157. /**
  158. * Updates a shipping method option
  159. *
  160. * @param $name
  161. * @param $value
  162. * @param $service_id
  163. * @param $service_instance
  164. *
  165. * @return bool
  166. */
  167. public static function update_shipping_method_option( $name, $value, $service_id, $service_instance = false ) {
  168. $option_name = self::get_shipping_method_option_name( $name, $service_id, $service_instance );
  169. if ( ! $option_name ) {
  170. trigger_error( sprintf( 'Invalid WooCommerce Services shipping method option name: %s', $name ), E_USER_WARNING );
  171. return false;
  172. }
  173. return update_option( $option_name, $value );
  174. }
  175. /**
  176. * Deletes a shipping method option
  177. *
  178. * @param $name
  179. * @param $service_id
  180. * @param $service_instance
  181. *
  182. * @return bool
  183. */
  184. public static function delete_shipping_method_option( $name, $service_id, $service_instance = false ) {
  185. $option_name = self::get_shipping_method_option_name( $name, $service_id, $service_instance );
  186. if ( ! $option_name ) {
  187. trigger_error( sprintf( 'Invalid WooCommerce Services shipping method option name: %s', $name ), E_USER_WARNING );
  188. return false;
  189. }
  190. return delete_option( $option_name );
  191. }
  192. /**
  193. * Deletes all options related to a shipping method
  194. *
  195. * @param $service_id
  196. * @param $service_instance
  197. */
  198. public static function delete_shipping_method_options( $service_id, $service_instance = false ) {
  199. $option_names = self::get_option_names( 'shipping_method' );
  200. foreach ( $option_names as $name ) {
  201. delete_option( self::get_shipping_method_option_name( $name, $service_id, $service_instance ) );
  202. }
  203. }
  204. private static function get_grouped_option( $group, $name, $default ) {
  205. $options = get_option( self::$grouped_options[ $group ] );
  206. if ( is_array( $options ) && isset( $options[ $name ] ) ) {
  207. return $options[ $name ];
  208. }
  209. //make the grouped options backwards-compatible and migrate the old options
  210. $legacy_name = "wc_connect_$name";
  211. $legacy_option = get_option( $legacy_name, false );
  212. if ( ! $legacy_option ) {
  213. return $default;
  214. }
  215. if ( self::update_grouped_option( $group, $name, $legacy_option ) ) {
  216. delete_option( $legacy_name );
  217. }
  218. return $legacy_option;
  219. }
  220. private static function update_grouped_option( $group, $name, $value ) {
  221. $options = get_option( self::$grouped_options[ $group ] );
  222. if ( ! is_array( $options ) ) {
  223. $options = array();
  224. }
  225. $options[ $name ] = $value;
  226. return update_option( self::$grouped_options[ $group ], $options );
  227. }
  228. private static function delete_grouped_option( $group, $names ) {
  229. $options = get_option( self::$grouped_options[ $group ], array() );
  230. $to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) );
  231. if ( $to_delete ) {
  232. foreach ( $to_delete as $name ) {
  233. unset( $options[ $name ] );
  234. }
  235. return update_option( self::$grouped_options[ $group ], $options );
  236. }
  237. return true;
  238. }
  239. /**
  240. * Based on the service id and optional instance, generates the option name
  241. *
  242. * @param $name
  243. * @param $service_id
  244. * @param $service_instance
  245. *
  246. * @return string|bool
  247. */
  248. private static function get_shipping_method_option_name( $name, $service_id, $service_instance = false ) {
  249. if ( ! in_array( $name, self::get_option_names( 'shipping_method' ) ) ) {
  250. return false;
  251. }
  252. if ( ! $service_instance ) {
  253. return 'woocommerce_' . $service_id . '_' . $name;
  254. }
  255. return 'woocommerce_' . $service_id . '_' . $service_instance . '_' . $name;
  256. }
  257. /**
  258. * Is the option name valid?
  259. *
  260. * @param string $name The name of the option
  261. * @param string $group The name of the group that the option is in. Defaults to compact.
  262. *
  263. * @return bool Is the option name valid?
  264. */
  265. private static function is_valid( $name, $group = 'non_compact' ) {
  266. $group_keys = array_keys( self::$grouped_options );
  267. if ( is_array( $name ) ) {
  268. $compact_names = array();
  269. foreach ( $group_keys as $_group ) {
  270. $compact_names = array_merge( $compact_names, self::get_option_names( $_group ) );
  271. }
  272. $result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names );
  273. return empty( $result );
  274. }
  275. if ( is_null( $group ) || 'non_compact' === $group ) {
  276. if ( in_array( $name, self::get_option_names( $group ) ) ) {
  277. return true;
  278. }
  279. }
  280. foreach ( array_keys( self::$grouped_options ) as $_group ) {
  281. if ( is_null( $group ) || $group === $_group ) {
  282. if ( in_array( $name, self::get_option_names( $_group ) ) ) {
  283. return true;
  284. }
  285. }
  286. }
  287. return false;
  288. }
  289. /**
  290. * Deletes all options of all shipping methods
  291. */
  292. private static function delete_all_shipping_methods_options() {
  293. global $wpdb;
  294. $methods = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_methods " );
  295. foreach ( (array) $methods as $method ) {
  296. self::delete_shipping_method_options( $method->method_id, $method->instance_id );
  297. }
  298. }
  299. }
  300. }