PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cmb2/includes/CMB2_Utils.php

https://gitlab.com/almoore1/buy-button-wordpress
PHP | 301 lines | 142 code | 37 blank | 122 comment | 21 complexity | 6163860921a2994c0b5be9190870e64c 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 int|false Attachment ID or false
  25. */
  26. public function image_id_from_url( $img_url ) {
  27. $attachment_id = 0;
  28. $dir = wp_upload_dir();
  29. // Is URL in uploads directory?
  30. if ( false === strpos( $img_url, $dir['baseurl'] . '/' ) ) {
  31. return false;
  32. }
  33. $file = basename( $img_url );
  34. $query_args = array(
  35. 'post_type' => 'attachment',
  36. 'post_status' => 'inherit',
  37. 'fields' => 'ids',
  38. 'meta_query' => array(
  39. array(
  40. 'value' => $file,
  41. 'compare' => 'LIKE',
  42. 'key' => '_wp_attachment_metadata',
  43. ),
  44. )
  45. );
  46. $query = new WP_Query( $query_args );
  47. if ( $query->have_posts() ) {
  48. foreach ( $query->posts as $post_id ) {
  49. $meta = wp_get_attachment_metadata( $post_id );
  50. $original_file = basename( $meta['file'] );
  51. $cropped_image_files = isset( $meta['sizes'] ) ? wp_list_pluck( $meta['sizes'], 'file' ) : array();
  52. if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
  53. $attachment_id = $post_id;
  54. break;
  55. }
  56. }
  57. }
  58. return 0 === $attachment_id ? false : $attachment_id;
  59. }
  60. /**
  61. * Utility method that returns time string offset by timezone
  62. * @since 1.0.0
  63. * @param string $tzstring Time string
  64. * @return string Offset time string
  65. */
  66. public function timezone_offset( $tzstring ) {
  67. $tz_offset = 0;
  68. if ( ! empty( $tzstring ) && is_string( $tzstring ) ) {
  69. if ( 'UTC' === substr( $tzstring, 0, 3 ) ) {
  70. $tzstring = str_replace( array( ':15', ':30', ':45' ), array( '.25', '.5', '.75' ), $tzstring );
  71. return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS );
  72. }
  73. try {
  74. $date_time_zone_selected = new DateTimeZone( $tzstring );
  75. $tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() );
  76. } catch ( Exception $e ) {
  77. $this->log_if_debug( __METHOD__, __LINE__, $e->getMessage() );
  78. }
  79. }
  80. return $tz_offset;
  81. }
  82. /**
  83. * Utility method that returns a timezone string representing the default timezone for the site.
  84. *
  85. * Roughly copied from WordPress, as get_option('timezone_string') will return
  86. * an empty string if no value has been set on the options page.
  87. * A timezone string is required by the wp_timezone_choice() used by the
  88. * select_timezone field.
  89. *
  90. * @since 1.0.0
  91. * @return string Timezone string
  92. */
  93. public function timezone_string() {
  94. $current_offset = get_option( 'gmt_offset' );
  95. $tzstring = get_option( 'timezone_string' );
  96. if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists
  97. if ( 0 == $current_offset ) {
  98. $tzstring = 'UTC+0';
  99. } elseif ( $current_offset < 0 ) {
  100. $tzstring = 'UTC' . $current_offset;
  101. } else {
  102. $tzstring = 'UTC+' . $current_offset;
  103. }
  104. }
  105. return $tzstring;
  106. }
  107. /**
  108. * Returns a timestamp, first checking if value already is a timestamp.
  109. * @since 2.0.0
  110. * @param string|int $string Possible timestamp string
  111. * @return int Time stamp
  112. */
  113. public function make_valid_time_stamp( $string ) {
  114. if ( ! $string ) {
  115. return 0;
  116. }
  117. return $this->is_valid_time_stamp( $string )
  118. ? (int) $string :
  119. strtotime( (string) $string );
  120. }
  121. /**
  122. * Determine if a value is a valid timestamp
  123. * @since 2.0.0
  124. * @param mixed $timestamp Value to check
  125. * @return boolean Whether value is a valid timestamp
  126. */
  127. public function is_valid_time_stamp( $timestamp ) {
  128. return (string) (int) $timestamp === (string) $timestamp
  129. && $timestamp <= PHP_INT_MAX
  130. && $timestamp >= ~PHP_INT_MAX;
  131. }
  132. /**
  133. * Checks if a value is 'empty'. Still accepts 0.
  134. * @since 2.0.0
  135. * @param mixed $value Value to check
  136. * @return bool True or false
  137. */
  138. public function isempty( $value ) {
  139. return null === $value || '' === $value || false === $value;
  140. }
  141. /**
  142. * Insert a single array item inside another array at a set position
  143. * @since 2.0.2
  144. * @param array &$array Array to modify. Is passed by reference, and no return is needed.
  145. * @param array $new New array to insert
  146. * @param int $position Position in the main array to insert the new array
  147. */
  148. public function array_insert( &$array, $new, $position ) {
  149. $before = array_slice( $array, 0, $position - 1 );
  150. $after = array_diff_key( $array, $before );
  151. $array = array_merge( $before, $new, $after );
  152. }
  153. /**
  154. * Defines the url which is used to load local resources.
  155. * This may need to be filtered for local Window installations.
  156. * If resources do not load, please check the wiki for details.
  157. * @since 1.0.1
  158. * @return string URL to CMB2 resources
  159. */
  160. public function url( $path = '' ) {
  161. if ( $this->url ) {
  162. return $this->url . $path;
  163. }
  164. if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ) {
  165. // Windows
  166. $content_dir = str_replace( '/', DIRECTORY_SEPARATOR, WP_CONTENT_DIR );
  167. $content_url = str_replace( $content_dir, WP_CONTENT_URL, cmb2_dir() );
  168. $cmb2_url = str_replace( DIRECTORY_SEPARATOR, '/', $content_url );
  169. } else {
  170. $cmb2_url = str_replace(
  171. array( WP_CONTENT_DIR, WP_PLUGIN_DIR ),
  172. array( WP_CONTENT_URL, WP_PLUGIN_URL ),
  173. cmb2_dir()
  174. );
  175. }
  176. /**
  177. * Filter the CMB location url
  178. *
  179. * @param string $cmb2_url Currently registered url
  180. */
  181. $this->url = trailingslashit( apply_filters( 'cmb2_meta_box_url', set_url_scheme( $cmb2_url ), CMB2_VERSION ) );
  182. return $this->url . $path;
  183. }
  184. /**
  185. * Get timestamp from text date
  186. * @since 2.2.0
  187. * @param string $value Date value
  188. * @param string $date_format Expected date format
  189. * @return mixed Unix timestamp representing the date.
  190. */
  191. public function get_timestamp_from_value( $value, $date_format ) {
  192. $date_object = date_create_from_format( $date_format, $value );
  193. return $date_object ? $date_object->setTime( 0, 0, 0 )->getTimeStamp() : strtotime( $value );
  194. }
  195. /**
  196. * Takes a php date() format string and returns a string formatted to suit for the date/time pickers
  197. * It will work with only with the following subset ot date() options:
  198. *
  199. * d, j, z, m, n, y, and Y.
  200. *
  201. * A slight effort is made to deal with escaped characters.
  202. *
  203. * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or
  204. * bring even more translation troubles.
  205. *
  206. * @since 2.2.0
  207. * @param string $format php date format
  208. * @return string reformatted string
  209. */
  210. public function php_to_js_dateformat( $format ) {
  211. // order is relevant here, since the replacement will be done sequentially.
  212. $supported_options = array(
  213. 'd' => 'dd', // Day, leading 0
  214. 'j' => 'd', // Day, no 0
  215. 'z' => 'o', // Day of the year, no leading zeroes,
  216. // 'D' => 'D', // Day name short, not sure how it'll work with translations
  217. // 'l' => 'DD', // Day name full, idem before
  218. 'm' => 'mm', // Month of the year, leading 0
  219. 'n' => 'm', // Month of the year, no leading 0
  220. // 'M' => 'M', // Month, Short name
  221. // 'F' => 'MM', // Month, full name,
  222. 'y' => 'y', // Year, two digit
  223. 'Y' => 'yy', // Year, full
  224. 'H' => 'HH', // Hour with leading 0 (24 hour)
  225. 'G' => 'H', // Hour with no leading 0 (24 hour)
  226. 'h' => 'hh', // Hour with leading 0 (12 hour)
  227. 'g' => 'h', // Hour with no leading 0 (12 hour),
  228. 'i' => 'mm', // Minute with leading 0,
  229. 's' => 'ss', // Second with leading 0,
  230. 'a' => 'tt', // am/pm
  231. 'A' => 'TT' // AM/PM
  232. );
  233. foreach ( $supported_options as $php => $js ) {
  234. // replaces every instance of a supported option, but skips escaped characters
  235. $format = preg_replace( "~(?<!\\\\)$php~", $js, $format );
  236. }
  237. $format = preg_replace_callback( '~(?:\\\.)+~', array( $this, 'wrap_escaped_chars' ), $format );
  238. return $format;
  239. }
  240. /**
  241. * Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded.
  242. * @since 2.2.0
  243. * @param $value Value to wrap/escape
  244. * @return string Modified value
  245. */
  246. public function wrap_escaped_chars( $value ) {
  247. return "&#39;" . str_replace( '\\', '', $value[0] ) . "&#39;";
  248. }
  249. /**
  250. * Send to debug.log if WP_DEBUG is defined and true
  251. *
  252. * @since 2.2.0
  253. *
  254. * @param string $function Function name
  255. * @param int $line Line number
  256. * @param mixed $msg Message to output
  257. * @param mixed $debug Variable to print_r
  258. */
  259. public function log_if_debug( $function, $line, $msg, $debug = null ) {
  260. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  261. error_log( "In $function, $line:" . print_r( $msg, true ) . ( $debug ? print_r( $debug, true ) : '' ) );
  262. }
  263. }
  264. }