class-fl-builder-utils.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Misc helper methods.
  4. *
  5. * @since 1.0
  6. */
  7. final class FLBuilderUtils {
  8. /**
  9. * Get an instance of WP_Filesystem_Direct.
  10. *
  11. * @since 1.4.6
  12. * @deprecated 2.0.6
  13. * @return object A WP_Filesystem_Direct instance.
  14. */
  15. static public function get_filesystem() {
  16. _deprecated_function( __METHOD__, '2.0.6', 'fl_builder_filesystem()->get_filesystem()' );
  17. return fl_builder_filesystem()->get_filesystem();
  18. }
  19. /**
  20. * Sets the filesystem method to direct.
  21. *
  22. * @since 1.4.6
  23. * @deprecated 2.0.6
  24. * @return string
  25. */
  26. static public function filesystem_method() {
  27. _deprecated_function( __METHOD__, '2.0.6', 'fl_builder_filesystem()->filesystem_method()' );
  28. return 'direct';
  29. }
  30. /**
  31. * Return a snippet without punctuation at the end.
  32. *
  33. * @since 1.2.3
  34. * @param string $text The text to truncate.
  35. * @param int $length The number of characters to return.
  36. * @param string $tail The trailing characters to append.
  37. * @return string
  38. */
  39. static public function snippetwop( $text, $length = 64, $tail = '...' ) {
  40. $text = trim( $text );
  41. $txtl = function_exists( 'mb_strlen' ) ? mb_strlen( $text ) : strlen( $text );
  42. if ( $txtl > $length ) {
  43. for ( $i = 1;' ' != $text[ $length -$i ];$i++ ) {
  44. if ( $i == $length ) {
  45. if ( function_exists( 'mb_substr' ) ) {
  46. return mb_substr( $text,0,$length ) . $tail;
  47. }
  48. return substr( $text,0,$length ) . $tail;
  49. }
  50. }
  51. for ( ;',' == $text[ $length -$i ] || '.' == $text[ $length -$i ] || ' ' == $text[ $length -$i ];
  52. $i++ ) {;}
  53. if ( function_exists( 'mb_substr' ) ) {
  54. return mb_substr( $text,0,$length -$i + 1 ) . $tail;
  55. }
  56. return substr( $text,0,$length -$i + 1 ) . $tail;
  57. }
  58. return $text;
  59. }
  60. /**
  61. * JSON decode multidimensional array values or object properties.
  62. *
  63. * @since 1.5.6
  64. * @param mixed $data The data to decode.
  65. * @return mixed The decoded data.
  66. */
  67. static public function json_decode_deep( $data ) {
  68. // First check if we have a string and try to decode that.
  69. if ( is_string( $data ) ) {
  70. $data = json_decode( $data );
  71. }
  72. // Decode object properies or array values.
  73. if ( is_object( $data ) || is_array( $data ) ) {
  74. foreach ( $data as $key => $val ) {
  75. $new_val = null;
  76. if ( is_string( $val ) ) {
  77. $decoded = json_decode( $val );
  78. if ( is_object( $decoded ) || is_array( $decoded ) ) {
  79. $new_val = $decoded;
  80. }
  81. } elseif ( is_object( $val ) || is_array( $val ) ) {
  82. $new_val = self::json_decode_deep( $val );
  83. }
  84. if ( $new_val ) {
  85. if ( is_object( $data ) ) {
  86. $data->{$key} = $new_val;
  87. } elseif ( is_array( $data ) ) {
  88. $data[ $key ] = $new_val;
  89. }
  90. }
  91. }
  92. }
  93. return $data;
  94. }
  95. /**
  96. * Base64 decode settings if our ModSecurity fix is enabled.
  97. *
  98. * @since 1.8.4
  99. * @return array
  100. */
  101. static public function modsec_fix_decode( $settings ) {
  102. if ( defined( 'FL_BUILDER_MODSEC_FIX' ) && FL_BUILDER_MODSEC_FIX ) {
  103. if ( is_string( $settings ) ) {
  104. $settings = wp_slash( base64_decode( $settings ) );
  105. } else {
  106. foreach ( $settings as $key => $value ) {
  107. if ( is_string( $settings[ $key ] ) ) {
  108. $settings[ $key ] = wp_slash( base64_decode( $value ) );
  109. } elseif ( is_array( $settings[ $key ] ) ) {
  110. $settings[ $key ] = self::modsec_fix_decode( $settings[ $key ] );
  111. }
  112. }
  113. }
  114. }
  115. return $settings;
  116. }
  117. /**
  118. * Get video type and ID from a given URL
  119. *
  120. * @since 1.9
  121. * @param string $url The URL to check for video type
  122. * @param string $type The type of video to check
  123. * @return array
  124. */
  125. static public function get_video_data( $url, $type = '' ) {
  126. if ( empty( $url ) ) {
  127. return false;
  128. }
  129. $y_matches = array();
  130. $vm_matches = array();
  131. $yt_pattern = '/^(?:(?:(?:https?:)?\/\/)?(?:www.)?(?:youtu(?:be.com|.be))\/(?:watch\?v\=|v\/|embed\/)?([\w\-]+))/is';
  132. $vm_pattern = '#(?:https?://)?(?:www.)?(?:player.)?vimeo.com/(?:[a-z]*/)*([0-9]{6,11})[?]?.*#';
  133. $video_data = array(
  134. 'type' => 'mp4',
  135. 'video_id' => '',
  136. );
  137. preg_match( $yt_pattern, $url, $yt_matches );
  138. preg_match( $vm_pattern, $url, $vm_matches );
  139. if ( isset( $yt_matches[1] ) ) {
  140. $video_data['type'] = 'youtube';
  141. $video_data['video_id'] = $yt_matches[1];
  142. parse_str( parse_url( $url, PHP_URL_QUERY ), $yt_params );
  143. if ( ! empty( $yt_params ) ) {
  144. // If start time is specified, make sure to convert it into seconds.
  145. if ( isset( $yt_params['t'] ) ) {
  146. $minutes = 0;
  147. $seconds = 0;
  148. $time_in_seconds = 0;
  149. // Check for minutes.
  150. if ( strpos( $yt_params['t'], 'm' ) !== false ) {
  151. $start_mins = preg_split( '([0-9]+[s])', $yt_params['t'] );
  152. if ( $start_mins ) {
  153. $minutes = (int) substr( $start_mins[0], 0, -1 ) * 60;
  154. }
  155. }
  156. if ( strpos( $yt_params['t'], 's' ) !== false ) {
  157. $start_secs = preg_split( '([0-9]+[m])', $yt_params['t'] );
  158. // Triggered when: &t=1m2s
  159. if ( isset( $start_secs[1] ) ) {
  160. $seconds = substr( $start_secs[1], 0, -1 );
  161. // Triggered when: &t=1s
  162. } elseif ( isset( $start_secs[0] ) && ! empty( $start_secs[0] ) ) {
  163. $seconds = substr( $start_secs[0], 0, -1 );
  164. }
  165. }
  166. $time_in_seconds = $minutes + $seconds;
  167. if ( $time_in_seconds > 0 ) {
  168. $yt_params['t'] = $time_in_seconds;
  169. }
  170. }
  171. $video_data['params'] = $yt_params;
  172. }
  173. } elseif ( isset( $vm_matches[1] ) ) {
  174. $video_data['type'] = 'vimeo';
  175. $video_data['video_id'] = $vm_matches[1];
  176. }
  177. if ( ! empty( $type ) ) {
  178. if ( $type === $video_data['type'] ) {
  179. return $video_data['video_id'];
  180. } else {
  181. return false;
  182. }
  183. }
  184. return $video_data;
  185. }
  186. /**
  187. * Use mb_strtolower() if available.s
  188. * @since 2.0.2
  189. */
  190. static public function strtolower( $text, $encoding = 'UTF-8' ) {
  191. if ( function_exists( 'mb_strtolower' ) ) {
  192. return mb_strtolower( $text, $encoding );
  193. }
  194. return strtolower( $text );
  195. }
  196. /**
  197. * Sanitize a value for js
  198. * @since 2.0.7
  199. */
  200. static public function sanitize_number( $value ) {
  201. if ( is_numeric( $value ) ) {
  202. return $value;
  203. }
  204. return 0;
  205. }
  206. /**
  207. * Sanitize a value for js
  208. * @since 2.1.3
  209. */
  210. static public function sanitize_non_negative_number( $value ) {
  211. if ( is_numeric( $value ) && floatval( $value ) >= 0 ) {
  212. return $value;
  213. }
  214. return 0;
  215. }
  216. }