PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/robo-gallery/cmb2/includes/CMB2_Utils.php

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