CMB2_Utils

CMB2 Utilities.

Defined (1)

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

/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. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  72. error_log( 'CMB2_Sanitize:::text_datetime_timestamp_timezone, ' . __LINE__ . ': ' . print_r( $e->getMessage(), true ) ); 
  73.  
  74.  
  75. return $tz_offset; 
  76.  
  77. /** 
  78. * Utility method that returns a timezone string representing the default timezone for the site. 
  79. * Roughly copied from WordPress, as get_option('timezone_string') will return 
  80. * an empty string if no value has been set on the options page. 
  81. * A timezone string is required by the wp_timezone_choice() used by the 
  82. * select_timezone field. 
  83. * @since 1.0.0 
  84. * @return string Timezone string 
  85. */ 
  86. public function timezone_string() { 
  87. $current_offset = get_option( 'gmt_offset' ); 
  88. $tzstring = get_option( 'timezone_string' ); 
  89.  
  90. if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists 
  91. if ( 0 == $current_offset ) { 
  92. $tzstring = 'UTC+0'; 
  93. } elseif ( $current_offset < 0 ) { 
  94. $tzstring = 'UTC' . $current_offset; 
  95. } else { 
  96. $tzstring = 'UTC+' . $current_offset; 
  97.  
  98. return $tzstring; 
  99.  
  100. /** 
  101. * Returns a timestamp, first checking if value already is a timestamp. 
  102. * @since 2.0.0 
  103. * @param string|int $string Possible timestamp string 
  104. * @return int Time stamp 
  105. */ 
  106. public function make_valid_time_stamp( $string ) { 
  107. if ( ! $string ) { 
  108. return 0; 
  109.  
  110. return $this->is_valid_time_stamp( $string ) 
  111. ? (int) $string : 
  112. strtotime( (string) $string ); 
  113.  
  114. /** 
  115. * Determine if a value is a valid timestamp 
  116. * @since 2.0.0 
  117. * @param mixed $timestamp Value to check 
  118. * @return boolean Whether value is a valid timestamp 
  119. */ 
  120. public function is_valid_time_stamp( $timestamp ) { 
  121. return (string) (int) $timestamp === (string) $timestamp 
  122. && $timestamp <= PHP_INT_MAX 
  123. && $timestamp >= ~PHP_INT_MAX; 
  124.  
  125. /** 
  126. * Checks if a value is 'empty'. Still accepts 0. 
  127. * @since 2.0.0 
  128. * @param mixed $value Value to check 
  129. * @return bool True or false 
  130. */ 
  131. public function isempty( $value ) { 
  132. return null === $value || '' === $value || false === $value; 
  133.  
  134. /** 
  135. * Insert a single array item inside another array at a set position 
  136. * @since 2.0.2 
  137. * @param array &$array Array to modify. Is passed by reference, and no return is needed. 
  138. * @param array $new New array to insert 
  139. * @param int $position Position in the main array to insert the new array 
  140. */ 
  141. public function array_insert( &$array, $new, $position ) { 
  142. $before = array_slice( $array, 0, $position - 1 ); 
  143. $after = array_diff_key( $array, $before ); 
  144. $array = array_merge( $before, $new, $after ); 
  145.  
  146. /** 
  147. * Defines the url which is used to load local resources. 
  148. * This may need to be filtered for local Window installations. 
  149. * If resources do not load, please check the wiki for details. 
  150. * @since 1.0.1 
  151. * @return string URL to CMB2 resources 
  152. */ 
  153. public function url( $path = '' ) { 
  154. if ( $this->url ) { 
  155. return $this->url . $path; 
  156.  
  157. if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ) { 
  158. // Windows 
  159. $content_dir = str_replace( '/', DIRECTORY_SEPARATOR, WP_CONTENT_DIR ); 
  160. $content_url = str_replace( $content_dir, WP_CONTENT_URL, cmb2_dir() ); 
  161. $cmb2_url = str_replace( DIRECTORY_SEPARATOR, '/', $content_url ); 
  162.  
  163. } else { 
  164. $cmb2_url = str_replace( 
  165. array( WP_CONTENT_DIR, WP_PLUGIN_DIR ),  
  166. array( WP_CONTENT_URL, WP_PLUGIN_URL ),  
  167. cmb2_dir() 
  168. ); 
  169.  
  170. /** 
  171. * Filter the CMB location url 
  172. * @param string $cmb2_url Currently registered url 
  173. */ 
  174. $this->url = trailingslashit( apply_filters( 'cmb2_meta_box_url', set_url_scheme( $cmb2_url ), CMB2_VERSION ) ); 
  175.  
  176. return $this->url . $path; 
  177.