CMB2_Utils

CMB2 Utilities.

Defined (2)

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

/includes/libraries/metabox/includes/CMB2_Utils.php  
  1. class CMB2_Utils { 
  2.  
  3. /** 
  4. * The url which is used to load local resources. 
  5. * @var string 
  6. * @since 2.0.0 
  7. */ 
  8. protected $url = ''; 
  9.  
  10. /** 
  11. * Utility method that attempts to get an attachment's ID by it's url 
  12. * @since 1.0.0 
  13. * @param string $img_url Attachment url 
  14. * @return mixed Attachment ID or false 
  15. */ 
  16. public function image_id_from_url( $img_url ) { 
  17. global $wpdb; 
  18.  
  19. $img_url = esc_url_raw( $img_url ); 
  20. // Get just the file name 
  21. if ( false !== strpos( $img_url, '/' ) ) { 
  22. $explode = explode( '/', $img_url ); 
  23. $img_url = end( $explode ); 
  24.  
  25. // And search for a fuzzy match of the file name 
  26. $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid LIKE '%%%s%%' LIMIT 1;", $img_url ) ); 
  27.  
  28. // If we found an attachement ID, return it 
  29. if ( ! empty( $attachment ) && is_array( $attachment ) ) { 
  30. return $attachment[0]; 
  31.  
  32. // No luck 
  33. return false; 
  34.  
  35. /** 
  36. * Utility method that returns time string offset by timezone 
  37. * @since 1.0.0 
  38. * @param string $tzstring Time string 
  39. * @return string Offset time string 
  40. */ 
  41. public function timezone_offset( $tzstring ) { 
  42. if ( ! empty( $tzstring ) && is_string( $tzstring ) ) { 
  43. if ( 'UTC' === substr( $tzstring, 0, 3 ) ) { 
  44. $tzstring = str_replace( array( ':15', ':30', ':45' ), array( '.25', '.5', '.75' ), $tzstring ); 
  45. return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS ); 
  46.  
  47. $date_time_zone_selected = new DateTimeZone( $tzstring ); 
  48. $tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() ); 
  49.  
  50. return $tz_offset; 
  51.  
  52. return 0; 
  53.  
  54. /** 
  55. * Utility method that returns a timezone string representing the default timezone for the site. 
  56. * Roughly copied from WordPress, as get_option('timezone_string') will return 
  57. * an empty string if no value has been set on the options page. 
  58. * A timezone string is required by the wp_timezone_choice() used by the 
  59. * select_timezone field. 
  60. * @since 1.0.0 
  61. * @return string Timezone string 
  62. */ 
  63. public function timezone_string() { 
  64. $current_offset = get_option( 'gmt_offset' ); 
  65. $tzstring = get_option( 'timezone_string' ); 
  66.  
  67. if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists 
  68. if ( 0 == $current_offset ) { 
  69. $tzstring = 'UTC+0'; 
  70. } elseif ( $current_offset < 0 ) { 
  71. $tzstring = 'UTC' . $current_offset; 
  72. } else { 
  73. $tzstring = 'UTC+' . $current_offset; 
  74.  
  75. return $tzstring; 
  76.  
  77. /** 
  78. * Returns a timestamp, first checking if value already is a timestamp. 
  79. * @since 2.0.0 
  80. * @param string|int $string Possible timestamp string 
  81. * @return int Time stamp 
  82. */ 
  83. public function make_valid_time_stamp( $string ) { 
  84. if ( ! $string ) { 
  85. return 0; 
  86.  
  87. return $this->is_valid_time_stamp( $string ) 
  88. ? (int) $string : 
  89. strtotime( $string ); 
  90.  
  91. /** 
  92. * Determine if a value is a valid timestamp 
  93. * @since 2.0.0 
  94. * @param mixed $timestamp Value to check 
  95. * @return boolean Whether value is a valid timestamp 
  96. */ 
  97. public function is_valid_time_stamp( $timestamp ) { 
  98. return (string) (int) $timestamp === (string) $timestamp 
  99. && $timestamp <= PHP_INT_MAX 
  100. && $timestamp >= ~PHP_INT_MAX; 
  101.  
  102. /** 
  103. * Checks if a value is 'empty'. Still accepts 0. 
  104. * @since 2.0.0 
  105. * @param mixed $value Value to check 
  106. * @return bool True or false 
  107. */ 
  108. public function isempty( $value ) { 
  109. return null === $value || '' === $value || false === $value; 
  110.  
  111. /** 
  112. * Insert a single array item inside another array at a set position 
  113. * @since 2.0.2 
  114. * @param array &$array Array to modify. Is passed by reference, and no return is needed. 
  115. * @param array $new New array to insert 
  116. * @param int $position Position in the main array to insert the new array 
  117. */ 
  118. public function array_insert( &$array, $new, $position ) { 
  119. $before = array_slice( $array, 0, $position - 1 ); 
  120. $after = array_diff_key( $array, $before ); 
  121. $array = array_merge( $before, $new, $after ); 
  122.  
  123. /** 
  124. * Defines the url which is used to load local resources. 
  125. * This may need to be filtered for local Window installations. 
  126. * If resources do not load, please check the wiki for details. 
  127. * @since 1.0.1 
  128. * @return string URL to CMB2 resources 
  129. */ 
  130. public function url( $path = '' ) { 
  131. if ( $this->url ) { 
  132. return $this->url . $path; 
  133.  
  134. if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ) { 
  135. // Windows 
  136. $content_dir = str_replace( '/', DIRECTORY_SEPARATOR, WP_CONTENT_DIR ); 
  137. $content_url = str_replace( $content_dir, WP_CONTENT_URL, cmb2_dir() ); 
  138. $cmb2_url = str_replace( DIRECTORY_SEPARATOR, '/', $content_url ); 
  139.  
  140. } else { 
  141. $cmb2_url = str_replace( 
  142. array( WP_CONTENT_DIR, WP_PLUGIN_DIR ),  
  143. array( WP_CONTENT_URL, WP_PLUGIN_URL ),  
  144. cmb2_dir() 
  145. ); 
  146.  
  147. /** 
  148. * Filter the CMB location url 
  149. * @param string $cmb2_url Currently registered url 
  150. */ 
  151. $this->url = trailingslashit( apply_filters( 'cmb2_meta_box_url', set_url_scheme( $cmb2_url ), CMB2_VERSION ) ); 
  152.  
  153. return $this->url . $path; 
  154.  
/vendor/wordimpress/maps-builder-core/includes/libraries/metabox/includes/CMB2_Utils.php  
  1. class CMB2_Utils { 
  2.  
  3. /** 
  4. * The url which is used to load local resources. 
  5. * @var string 
  6. * @since 2.0.0 
  7. */ 
  8. protected $url = ''; 
  9.  
  10. /** 
  11. * Utility method that attempts to get an attachment's ID by it's url 
  12. * @since 1.0.0 
  13. * @param string $img_url Attachment url 
  14. * @return int|false Attachment ID or false 
  15. */ 
  16. public function image_id_from_url( $img_url ) { 
  17. $attachment_id = 0; 
  18. $dir = wp_upload_dir(); 
  19.  
  20. // Is URL in uploads directory? 
  21. if ( false === strpos( $img_url, $dir['baseurl'] . '/' ) ) { 
  22. return false; 
  23.  
  24. $file = basename( $img_url ); 
  25.  
  26. $query_args = array( 
  27. 'post_type' => 'attachment',  
  28. 'post_status' => 'inherit',  
  29. 'fields' => 'ids',  
  30. 'meta_query' => array( 
  31. array( 
  32. 'value' => $file,  
  33. 'compare' => 'LIKE',  
  34. 'key' => '_wp_attachment_metadata',  
  35. ),  
  36. ); 
  37.  
  38. $query = new WP_Query( $query_args ); 
  39.  
  40. if ( $query->have_posts() ) { 
  41.  
  42. foreach ( $query->posts as $post_id ) { 
  43. $meta = wp_get_attachment_metadata( $post_id ); 
  44. $original_file = basename( $meta['file'] ); 
  45. $cropped_image_files = isset( $meta['sizes'] ) ? wp_list_pluck( $meta['sizes'], 'file' ) : array(); 
  46. if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) { 
  47. $attachment_id = $post_id; 
  48. break; 
  49.  
  50.  
  51. return 0 === $attachment_id ? false : $attachment_id; 
  52.  
  53. /** 
  54. * Utility method that returns time string offset by timezone 
  55. * @since 1.0.0 
  56. * @param string $tzstring Time string 
  57. * @return string Offset time string 
  58. */ 
  59. public function timezone_offset( $tzstring ) { 
  60. $tz_offset = 0; 
  61.  
  62. if ( ! empty( $tzstring ) && is_string( $tzstring ) ) { 
  63. if ( 'UTC' === substr( $tzstring, 0, 3 ) ) { 
  64. $tzstring = str_replace( array( ':15', ':30', ':45' ), array( '.25', '.5', '.75' ), $tzstring ); 
  65. return intval( floatval( substr( $tzstring, 3 ) ) * HOUR_IN_SECONDS ); 
  66.  
  67. try { 
  68. $date_time_zone_selected = new DateTimeZone( $tzstring ); 
  69. $tz_offset = timezone_offset_get( $date_time_zone_selected, date_create() ); 
  70. } catch ( Exception $e ) { 
  71. $this->log_if_debug( __METHOD__, __LINE__, $e->getMessage() ); 
  72.  
  73.  
  74. return $tz_offset; 
  75.  
  76. /** 
  77. * Utility method that returns a timezone string representing the default timezone for the site. 
  78. * Roughly copied from WordPress, as get_option('timezone_string') will return 
  79. * an empty string if no value has been set on the options page. 
  80. * A timezone string is required by the wp_timezone_choice() used by the 
  81. * select_timezone field. 
  82. * @since 1.0.0 
  83. * @return string Timezone string 
  84. */ 
  85. public function timezone_string() { 
  86. $current_offset = get_option( 'gmt_offset' ); 
  87. $tzstring = get_option( 'timezone_string' ); 
  88.  
  89. // Remove old Etc mappings. Fallback to gmt_offset. 
  90. if ( false !== strpos( $tzstring, 'Etc/GMT' ) ) { 
  91. $tzstring = ''; 
  92.  
  93. if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists 
  94. if ( 0 == $current_offset ) { 
  95. $tzstring = 'UTC+0'; 
  96. } elseif ( $current_offset < 0 ) { 
  97. $tzstring = 'UTC' . $current_offset; 
  98. } else { 
  99. $tzstring = 'UTC+' . $current_offset; 
  100.  
  101. return $tzstring; 
  102.  
  103. /** 
  104. * Returns a timestamp, first checking if value already is a timestamp. 
  105. * @since 2.0.0 
  106. * @param string|int $string Possible timestamp string 
  107. * @return int Time stamp 
  108. */ 
  109. public function make_valid_time_stamp( $string ) { 
  110. if ( ! $string ) { 
  111. return 0; 
  112.  
  113. return $this->is_valid_time_stamp( $string ) 
  114. ? (int) $string : 
  115. strtotime( (string) $string ); 
  116.  
  117. /** 
  118. * Determine if a value is a valid timestamp 
  119. * @since 2.0.0 
  120. * @param mixed $timestamp Value to check 
  121. * @return boolean Whether value is a valid timestamp 
  122. */ 
  123. public function is_valid_time_stamp( $timestamp ) { 
  124. return (string) (int) $timestamp === (string) $timestamp 
  125. && $timestamp <= PHP_INT_MAX 
  126. && $timestamp >= ~PHP_INT_MAX; 
  127.  
  128. /** 
  129. * Checks if a value is 'empty'. Still accepts 0. 
  130. * @since 2.0.0 
  131. * @param mixed $value Value to check 
  132. * @return bool True or false 
  133. */ 
  134. public function isempty( $value ) { 
  135. return null === $value || '' === $value || false === $value; 
  136.  
  137. /** 
  138. * Checks if a value is not 'empty'. 0 doesn't count as empty. 
  139. * @since 2.2.2 
  140. * @param mixed $value Value to check 
  141. * @return bool True or false 
  142. */ 
  143. public function notempty( $value ) { 
  144. return null !== $value && '' !== $value && false !== $value; 
  145.  
  146. /** 
  147. * Filters out empty values (not including 0). 
  148. * @since 2.2.2 
  149. * @param mixed $value Value to check 
  150. * @return bool True or false 
  151. */ 
  152. function filter_empty( $value ) { 
  153. return array_filter( $value, array( $this, 'notempty' ) ); 
  154.  
  155. /** 
  156. * Insert a single array item inside another array at a set position 
  157. * @since 2.0.2 
  158. * @param array &$array Array to modify. Is passed by reference, and no return is needed. 
  159. * @param array $new New array to insert 
  160. * @param int $position Position in the main array to insert the new array 
  161. */ 
  162. public function array_insert( &$array, $new, $position ) { 
  163. $before = array_slice( $array, 0, $position - 1 ); 
  164. $after = array_diff_key( $array, $before ); 
  165. $array = array_merge( $before, $new, $after ); 
  166.  
  167. /** 
  168. * Defines the url which is used to load local resources. 
  169. * This may need to be filtered for local Window installations. 
  170. * If resources do not load, please check the wiki for details. 
  171. * @since 1.0.1 
  172. * @return string URL to CMB2 resources 
  173. */ 
  174. public function url( $path = '' ) { 
  175. if ( $this->url ) { 
  176. return $this->url . $path; 
  177.  
  178. $cmb2_url = self::get_url_from_dir( cmb2_dir() ); 
  179.  
  180. /** 
  181. * Filter the CMB location url 
  182. * @param string $cmb2_url Currently registered url 
  183. */ 
  184. $this->url = trailingslashit( apply_filters( 'cmb2_meta_box_url', $cmb2_url, CMB2_VERSION ) ); 
  185.  
  186. return $this->url . $path; 
  187.  
  188. /** 
  189. * Converts a system path to a URL 
  190. * @since 2.2.2 
  191. * @param string $dir Directory path to convert. 
  192. * @return string Converted URL. 
  193. */ 
  194. public static function get_url_from_dir( $dir ) { 
  195. $dir = self::normalize_path( $dir ); 
  196.  
  197. // Let's test if We are in the plugins or mu-plugins dir. 
  198. $test_dir = trailingslashit( $dir ) . 'unneeded.php'; 
  199. if ( 
  200. 0 === strpos( $test_dir, self::normalize_path( WPMU_PLUGIN_DIR ) ) 
  201. || 0 === strpos( $test_dir, self::normalize_path( WP_PLUGIN_DIR ) ) 
  202. ) { 
  203. // Ok, then use plugins_url, as it is more reliable. 
  204. return trailingslashit( plugins_url( '', $test_dir ) ); 
  205.  
  206. // Ok, now let's test if we are in the theme dir. 
  207. $theme_root = get_theme_root(); 
  208. if ( 0 === strpos( $dir, $theme_root ) ) { 
  209. // Ok, then use get_theme_root_uri. 
  210. return set_url_scheme( trailingslashit( str_replace( $theme_root, get_theme_root_uri(), $dir ) ) ); 
  211.  
  212. // Check to see if it's anywhere in the root directory 
  213.  
  214. $site_dir = ABSPATH; 
  215. $site_url = trailingslashit( is_multisite() ? network_site_url() : site_url() ); 
  216.  
  217. $url = str_replace( 
  218. array( $site_dir, WP_PLUGIN_DIR ),  
  219. array( $site_url, WP_PLUGIN_URL ),  
  220. $dir 
  221. ); 
  222.  
  223. return set_url_scheme( $url ); 
  224.  
  225. /** 
  226. * `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path. 
  227. * On windows systems, replaces backslashes with forward slashes 
  228. * and forces upper-case drive letters. 
  229. * Allows for two leading slashes for Windows network shares, but 
  230. * ensures that all other duplicate slashes are reduced to a single. 
  231. * @since 2.2.0 
  232. * @param string $path Path to normalize. 
  233. * @return string Normalized path. 
  234. */ 
  235. protected static function normalize_path( $path ) { 
  236. if ( function_exists( 'wp_normalize_path' ) ) { 
  237. return wp_normalize_path( $path ); 
  238.  
  239. // Replace newer WP's version of wp_normalize_path. 
  240. $path = str_replace( '\\', '/', $path ); 
  241. $path = preg_replace( '|(?<=.)/+|', '/', $path ); 
  242. if ( ':' === substr( $path, 1, 1 ) ) { 
  243. $path = ucfirst( $path ); 
  244.  
  245. return $path; 
  246.  
  247. /** 
  248. * Get timestamp from text date 
  249. * @since 2.2.0 
  250. * @param string $value Date value 
  251. * @param string $date_format Expected date format 
  252. * @return mixed Unix timestamp representing the date. 
  253. */ 
  254. public function get_timestamp_from_value( $value, $date_format ) { 
  255. $date_object = date_create_from_format( $date_format, $value ); 
  256. return $date_object ? $date_object->setTime( 0, 0, 0 )->getTimeStamp() : strtotime( $value ); 
  257.  
  258. /** 
  259. * Takes a php date() format string and returns a string formatted to suit for the date/time pickers 
  260. * It will work with only with the following subset ot date() options: 
  261. * d, j, z, m, n, y, and Y. 
  262. * A slight effort is made to deal with escaped characters. 
  263. * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or 
  264. * bring even more translation troubles. 
  265. * @since 2.2.0 
  266. * @param string $format php date format 
  267. * @return string reformatted string 
  268. */ 
  269. public function php_to_js_dateformat( $format ) { 
  270.  
  271. // order is relevant here, since the replacement will be done sequentially. 
  272. $supported_options = array( 
  273. 'd' => 'dd', // Day, leading 0 
  274. 'j' => 'd', // Day, no 0 
  275. 'z' => 'o', // Day of the year, no leading zeroes,  
  276. // 'D' => 'D', // Day name short, not sure how it'll work with translations 
  277. // 'l' => 'DD', // Day name full, idem before 
  278. 'm' => 'mm', // Month of the year, leading 0 
  279. 'n' => 'm', // Month of the year, no leading 0 
  280. // 'M' => 'M', // Month, Short name 
  281. // 'F' => 'MM', // Month, full name,  
  282. 'y' => 'y', // Year, two digit 
  283. 'Y' => 'yy', // Year, full 
  284. 'H' => 'HH', // Hour with leading 0 (24 hour) 
  285. 'G' => 'H', // Hour with no leading 0 (24 hour) 
  286. 'h' => 'hh', // Hour with leading 0 (12 hour) 
  287. 'g' => 'h', // Hour with no leading 0 (12 hour),  
  288. 'i' => 'mm', // Minute with leading 0,  
  289. 's' => 'ss', // Second with leading 0,  
  290. 'a' => 'tt', // am/pm 
  291. 'A' => 'TT' // AM/PM 
  292. ); 
  293.  
  294. foreach ( $supported_options as $php => $js ) { 
  295. // replaces every instance of a supported option, but skips escaped characters 
  296. $format = preg_replace( "~(?<!\\\\)$php~", $js, $format ); 
  297.  
  298. $format = preg_replace_callback( '~(?:\\\.)+~', array( $this, 'wrap_escaped_chars' ), $format ); 
  299.  
  300. return $format; 
  301.  
  302. /** 
  303. * Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded. 
  304. * @since 2.2.0 
  305. * @param $value Value to wrap/escape 
  306. * @return string Modified value 
  307. */ 
  308. public function wrap_escaped_chars( $value ) { 
  309. return "'" . str_replace( '\\', '', $value[0] ) . "'"; 
  310.  
  311. /** 
  312. * Send to debug.log if WP_DEBUG is defined and true 
  313. * @since 2.2.0 
  314. * @param string $function Function name 
  315. * @param int $line Line number 
  316. * @param mixed $msg Message to output 
  317. * @param mixed $debug Variable to print_r 
  318. */ 
  319. public function log_if_debug( $function, $line, $msg, $debug = null ) { 
  320. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  321. error_log( "In $function, $line:" . print_r( $msg, true ) . ( $debug ? print_r( $debug, true ) : '' ) ); 
  322.  
  323. /** 
  324. * Determine a file's extension 
  325. * @since 1.0.0 
  326. * @param string $file File url 
  327. * @return string|false File extension or false 
  328. */ 
  329. public function get_file_ext( $file ) { 
  330. $parsed = @parse_url( $file, PHP_URL_PATH ); 
  331. return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false; 
  332.  
  333. /** 
  334. * Get the file name from a url 
  335. * @since 2.0.0 
  336. * @param string $value File url or path 
  337. * @return string File name 
  338. */ 
  339. public function get_file_name_from_path( $value ) { 
  340. $parts = explode( '/', $value ); 
  341. return is_array( $parts ) ? end( $parts ) : $value; 
  342.  
  343. /** 
  344. * Check if WP version is at least $version. 
  345. * @since 2.2.2 
  346. * @param string $version WP version string to compare. 
  347. * @return bool Result of comparison check. 
  348. */ 
  349. public function wp_at_least( $version ) { 
  350. global $wp_version; 
  351. return version_compare( $wp_version, $version, '>=' ); 
  352.