/wp-content/plugins/dk-pricr-responsive-pricing-table/inc/cmb2/includes/CMB2_Utils.php

https://gitlab.com/iamgraeme/royalmile · PHP · 194 lines · 88 code · 25 blank · 81 comment · 20 complexity · 3e02006389d6cb60087df08dbeb2bfb4 MD5 · raw file

  1. <?php
  2. /**
  3. * CMB2 Utilities
  4. *
  5. * @since 1.1.0
  6. *
  7. * @category WordPress_Plugin
  8. * @package CMB2
  9. * @author WebDevStudios
  10. * @license GPL-2.0+
  11. * @link http://webdevstudios.com
  12. */
  13. class CMB2_Utils {
  14. /**
  15. * The url which is used to load local resources.
  16. * @var string
  17. * @since 2.0.0
  18. */
  19. protected $url = '';
  20. /**
  21. * Utility method that attempts to get an attachment's ID by it's url
  22. * @since 1.0.0
  23. * @param string $img_url Attachment url
  24. * @return mixed Attachment ID or false
  25. */
  26. public function image_id_from_url( $img_url ) {
  27. global $wpdb;
  28. $img_url = esc_url_raw( $img_url );
  29. // Get just the file name
  30. if ( false !== strpos( $img_url, '/' ) ) {
  31. $explode = explode( '/', $img_url );
  32. $img_url = end( $explode );
  33. }
  34. // And search for a fuzzy match of the file name
  35. $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid LIKE '%%%s%%' LIMIT 1;", $img_url ) );
  36. // If we found an attachement ID, return it
  37. if ( ! empty( $attachment ) && is_array( $attachment ) ) {
  38. return $attachment[0];
  39. }
  40. // No luck
  41. return false;
  42. }
  43. /**
  44. * Utility method that returns time string offset by timezone
  45. * @since 1.0.0
  46. * @param string $tzstring Time string
  47. * @return string Offset time string
  48. */
  49. public function timezone_offset( $tzstring ) {
  50. $tz_offset = 0;
  51. if ( ! empty( $tzstring ) && is_string( $tzstring ) ) {
  52. if ( 'UTC' === substr( $tzstring, 0, 3 ) ) {
  53. $tzstring = str_replace( array( ':15', ':30', ':45' ), array( '.25', '.5', '.75' ), $tzstring );
  54. return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS );
  55. }
  56. try {
  57. $date_time_zone_selected = new DateTimeZone( $tzstring );
  58. $tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() );
  59. } catch ( Exception $e ) {
  60. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  61. error_log( 'CMB2_Sanitize:::text_datetime_timestamp_timezone, ' . __LINE__ . ': ' . print_r( $e->getMessage(), true ) );
  62. }
  63. }
  64. }
  65. return $tz_offset;
  66. }
  67. /**
  68. * Utility method that returns a timezone string representing the default timezone for the site.
  69. *
  70. * Roughly copied from WordPress, as get_option('timezone_string') will return
  71. * an empty string if no value has been set on the options page.
  72. * A timezone string is required by the wp_timezone_choice() used by the
  73. * select_timezone field.
  74. *
  75. * @since 1.0.0
  76. * @return string Timezone string
  77. */
  78. public function timezone_string() {
  79. $current_offset = get_option( 'gmt_offset' );
  80. $tzstring = get_option( 'timezone_string' );
  81. if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists
  82. if ( 0 == $current_offset ) {
  83. $tzstring = 'UTC+0';
  84. } elseif ( $current_offset < 0 ) {
  85. $tzstring = 'UTC' . $current_offset;
  86. } else {
  87. $tzstring = 'UTC+' . $current_offset;
  88. }
  89. }
  90. return $tzstring;
  91. }
  92. /**
  93. * Returns a timestamp, first checking if value already is a timestamp.
  94. * @since 2.0.0
  95. * @param string|int $string Possible timestamp string
  96. * @return int Time stamp
  97. */
  98. public function make_valid_time_stamp( $string ) {
  99. if ( ! $string ) {
  100. return 0;
  101. }
  102. return $this->is_valid_time_stamp( $string )
  103. ? (int) $string :
  104. strtotime( (string) $string );
  105. }
  106. /**
  107. * Determine if a value is a valid timestamp
  108. * @since 2.0.0
  109. * @param mixed $timestamp Value to check
  110. * @return boolean Whether value is a valid timestamp
  111. */
  112. public function is_valid_time_stamp( $timestamp ) {
  113. return (string) (int) $timestamp === (string) $timestamp
  114. && $timestamp <= PHP_INT_MAX
  115. && $timestamp >= ~PHP_INT_MAX;
  116. }
  117. /**
  118. * Checks if a value is 'empty'. Still accepts 0.
  119. * @since 2.0.0
  120. * @param mixed $value Value to check
  121. * @return bool True or false
  122. */
  123. public function isempty( $value ) {
  124. return null === $value || '' === $value || false === $value;
  125. }
  126. /**
  127. * Insert a single array item inside another array at a set position
  128. * @since 2.0.2
  129. * @param array &$array Array to modify. Is passed by reference, and no return is needed.
  130. * @param array $new New array to insert
  131. * @param int $position Position in the main array to insert the new array
  132. */
  133. public function array_insert( &$array, $new, $position ) {
  134. $before = array_slice( $array, 0, $position - 1 );
  135. $after = array_diff_key( $array, $before );
  136. $array = array_merge( $before, $new, $after );
  137. }
  138. /**
  139. * Defines the url which is used to load local resources.
  140. * This may need to be filtered for local Window installations.
  141. * If resources do not load, please check the wiki for details.
  142. * @since 1.0.1
  143. * @return string URL to CMB2 resources
  144. */
  145. public function url( $path = '' ) {
  146. if ( $this->url ) {
  147. return $this->url . $path;
  148. }
  149. if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ) {
  150. // Windows
  151. $content_dir = str_replace( '/', DIRECTORY_SEPARATOR, WP_CONTENT_DIR );
  152. $content_url = str_replace( $content_dir, WP_CONTENT_URL, cmb2_dir() );
  153. $cmb2_url = str_replace( DIRECTORY_SEPARATOR, '/', $content_url );
  154. } else {
  155. $cmb2_url = str_replace(
  156. array( WP_CONTENT_DIR, WP_PLUGIN_DIR ),
  157. array( WP_CONTENT_URL, WP_PLUGIN_URL ),
  158. cmb2_dir()
  159. );
  160. }
  161. /**
  162. * Filter the CMB location url
  163. *
  164. * @param string $cmb2_url Currently registered url
  165. */
  166. $this->url = trailingslashit( apply_filters( 'cmb2_meta_box_url', set_url_scheme( $cmb2_url ), CMB2_VERSION ) );
  167. return $this->url . $path;
  168. }
  169. }