PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/divi-builder/functions.php

https://gitlab.com/gregtyka/lfmawordpress
PHP | 240 lines | 158 code | 57 blank | 25 comment | 61 complexity | 8c50fb2afe28b1ee756b06b0340043c9 MD5 | raw file
  1. <?php
  2. /**
  3. * Gets option value from the single theme option, stored as an array in the database
  4. * if all options stored in one row.
  5. * Stores the serialized array with theme options into the global variable on the first function run on the page.
  6. *
  7. * If options are stored as separate rows in database, it simply uses get_option() function.
  8. *
  9. * @param string $option_name Theme option name.
  10. * @param string $default_value Default value that should be set if the theme option isn't set.
  11. * @param string $used_for_object "Object" name that should be translated into corresponding "object" if WPML is activated.
  12. * @return mixed Theme option value or false if not found.
  13. */
  14. if ( ! function_exists( 'et_get_option' ) ) :
  15. function et_get_option( $option_name, $default_value = '', $used_for_object = '', $force_default_value = false ) {
  16. global $et_divi_builder_plugin_options;
  17. $shortname = 'divi_builder_plugin';
  18. $et_theme_options_name = 'et_' . $shortname;
  19. if ( ! isset( $et_divi_builder_plugin_options ) ) {
  20. $et_divi_builder_plugin_options = get_option( $et_theme_options_name );
  21. }
  22. $option_value = isset ( $et_divi_builder_plugin_options[$option_name] ) ? $et_divi_builder_plugin_options[$option_name] : false;
  23. // option value might be equal to false, so check if the option is not set in the database
  24. if ( ! isset( $et_divi_builder_plugin_options[ $option_name ] ) && ( '' != $default_value || $force_default_value ) ) {
  25. $option_value = $default_value;
  26. }
  27. if ( '' != $used_for_object && in_array( $used_for_object, array( 'page', 'category' ) ) && is_array( $option_value ) ) {
  28. $option_value = et_generate_wpml_ids( $option_value, $used_for_object );
  29. }
  30. return $option_value;
  31. }
  32. endif;
  33. if ( ! function_exists( 'et_update_option' ) ) :
  34. function et_update_option( $option_name, $new_value ){
  35. global $et_divi_builder_plugin_options;
  36. $shortname = 'divi_builder_plugin';
  37. $et_theme_options_name = 'et_' . $shortname;
  38. if ( ! isset( $et_divi_builder_plugin_options ) ) $et_divi_builder_plugin_options = get_option( $et_theme_options_name );
  39. $et_divi_builder_plugin_options[$option_name] = $new_value;
  40. $option_name = $et_theme_options_name;
  41. $new_value = $et_divi_builder_plugin_options;
  42. update_option( $option_name, $new_value );
  43. }
  44. endif;
  45. if ( ! function_exists( 'et_delete_option' ) ) :
  46. function et_delete_option( $option_name ){
  47. global $et_divi_builder_plugin_options;
  48. $shortname = 'divi_builder_plugin';
  49. $et_theme_options_name = 'et_' . $shortname;
  50. if ( ! isset( $et_divi_builder_plugin_options ) ) $et_divi_builder_plugin_options = get_option( $et_theme_options_name );
  51. unset( $et_divi_builder_plugin_options[$option_name] );
  52. update_option( $et_theme_options_name, $et_divi_builder_plugin_options );
  53. }
  54. endif;
  55. /* this function gets thumbnail from Post Thumbnail or Custom field or First post image */
  56. if ( ! function_exists( 'get_thumbnail' ) ) :
  57. function get_thumbnail($width=100, $height=100, $class='', $alttext='', $titletext='', $fullpath=false, $custom_field='', $post='') {
  58. if ( $post == '' ) global $post;
  59. global $shortname;
  60. $thumb_array['thumb'] = '';
  61. $thumb_array['use_timthumb'] = true;
  62. if ($fullpath) $thumb_array['fullpath'] = ''; //full image url for lightbox
  63. $new_method = true;
  64. if ( has_post_thumbnail( $post->ID ) ) {
  65. $thumb_array['use_timthumb'] = false;
  66. $et_fullpath = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
  67. $thumb_array['fullpath'] = $et_fullpath[0];
  68. $thumb_array['thumb'] = $thumb_array['fullpath'];
  69. }
  70. if ($thumb_array['thumb'] == '') {
  71. if ($custom_field == '') $thumb_array['thumb'] = esc_attr( get_post_meta($post->ID, 'Thumbnail', $single = true) );
  72. else {
  73. $thumb_array['thumb'] = esc_attr( get_post_meta($post->ID, $custom_field, $single = true) );
  74. if ($thumb_array['thumb'] == '') $thumb_array['thumb'] = esc_attr( get_post_meta($post->ID, 'Thumbnail', $single = true) );
  75. }
  76. #if custom field used for small pre-cropped image, open Thumbnail custom field image in lightbox
  77. if ($fullpath) {
  78. $thumb_array['fullpath'] = $thumb_array['thumb'];
  79. if ($custom_field == '') $thumb_array['fullpath'] = apply_filters('et_fullpath', et_path_reltoabs(esc_attr($thumb_array['thumb'])));
  80. elseif ( $custom_field <> '' && get_post_meta($post->ID, 'Thumbnail', $single = true) ) $thumb_array['fullpath'] = apply_filters( 'et_fullpath', et_path_reltoabs(esc_attr(get_post_meta($post->ID, 'Thumbnail', $single = true))) );
  81. }
  82. }
  83. return $thumb_array;
  84. }
  85. endif;
  86. /* this function prints thumbnail from Post Thumbnail or Custom field or First post image */
  87. if ( ! function_exists( 'print_thumbnail' ) ) :
  88. function print_thumbnail($thumbnail = '', $use_timthumb = true, $alttext = '', $width = 100, $height = 100, $class = '', $echoout = true, $forstyle = false, $resize = true, $post='', $et_post_id = '' ) {
  89. if ( is_array( $thumbnail ) ){
  90. extract( $thumbnail );
  91. }
  92. if ( $post == '' ) global $post, $et_theme_image_sizes;
  93. $output = '';
  94. $et_post_id = '' != $et_post_id ? (int) $et_post_id : $post->ID;
  95. if ( has_post_thumbnail( $et_post_id ) ) {
  96. $thumb_array['use_timthumb'] = false;
  97. $image_size_name = $width . 'x' . $height;
  98. $et_size = isset( $et_theme_image_sizes ) && array_key_exists( $image_size_name, $et_theme_image_sizes ) ? $et_theme_image_sizes[$image_size_name] : array( $width, $height );
  99. $et_attachment_image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $et_post_id ), $et_size );
  100. $thumbnail = $et_attachment_image_attributes[0];
  101. }
  102. if ( false === $forstyle ) {
  103. $output = '<img src="' . esc_url( $thumbnail ) . '"';
  104. if ($class <> '') $output .= " class='" . esc_attr( $class ) . "' ";
  105. $dimensions = apply_filters( 'et_print_thumbnail_dimensions', " width='" . esc_attr( $width ) . "' height='" .esc_attr( $height ) . "'" );
  106. $output .= " alt='" . esc_attr( strip_tags( $alttext ) ) . "'{$dimensions} />";
  107. if ( ! $resize ) $output = $thumbnail;
  108. } else {
  109. $output = $thumbnail;
  110. }
  111. if ($echoout) echo $output;
  112. else return $output;
  113. }
  114. endif;
  115. if ( ! function_exists( 'et_path_reltoabs' ) ) :
  116. function et_path_reltoabs( $imageurl ){
  117. if ( strpos(strtolower($imageurl), 'http://') !== false || strpos(strtolower($imageurl), 'https://') !== false ) return $imageurl;
  118. if ( strpos( strtolower($imageurl), $_SERVER['HTTP_HOST'] ) !== false )
  119. return $imageurl;
  120. else {
  121. $imageurl = esc_url( apply_filters( 'et_path_relative_image', site_url() . '/' ) . $imageurl );
  122. }
  123. return $imageurl;
  124. }
  125. endif;
  126. /*this function allows for the auto-creation of post excerpts*/
  127. if ( ! function_exists( 'truncate_post' ) ) :
  128. function truncate_post( $amount, $echo = true, $post = '' ) {
  129. if ( '' == $post ) {
  130. global $post;
  131. }
  132. $post_excerpt = '';
  133. $post_excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
  134. // get the post content
  135. $truncate = $post->post_content;
  136. // remove caption shortcode from the post content
  137. $truncate = preg_replace('@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate);
  138. // apply content filters
  139. $truncate = apply_filters( 'the_content', $truncate );
  140. // decide if we need to append dots at the end of the string
  141. if ( strlen( $truncate ) <= $amount ) {
  142. $echo_out = '';
  143. } else {
  144. $echo_out = '...';
  145. // $amount = $amount - 3;
  146. }
  147. // trim text to a certain number of characters, also remove spaces from the end of a string ( space counts as a character )
  148. $truncate = rtrim( et_wp_trim_words( $truncate, $amount, '' ) );
  149. // remove the last word to make sure we display all words correctly
  150. if ( '' != $echo_out ) {
  151. $new_words_array = (array) explode( ' ', $truncate );
  152. array_pop( $new_words_array );
  153. $truncate = implode( ' ', $new_words_array );
  154. // append dots to the end of the string
  155. $truncate .= $echo_out;
  156. }
  157. if ( $echo ) {
  158. echo $truncate;
  159. } else {
  160. return $truncate;
  161. }
  162. }
  163. endif;
  164. if ( ! function_exists( 'et_wp_trim_words' ) ) :
  165. function et_wp_trim_words( $text, $num_words = 55, $more = null ) {
  166. if ( null === $more )
  167. $more = __( '&hellip;' );
  168. $original_text = $text;
  169. $text = wp_strip_all_tags( $text );
  170. $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
  171. preg_match_all( '/./u', $text, $words_array );
  172. $words_array = array_slice( $words_array[0], 0, $num_words + 1 );
  173. $sep = '';
  174. if ( count( $words_array ) > $num_words ) {
  175. array_pop( $words_array );
  176. $text = implode( $sep, $words_array );
  177. $text = $text . $more;
  178. } else {
  179. $text = implode( $sep, $words_array );
  180. }
  181. return apply_filters( 'wp_trim_words', $text, $num_words, $more, $original_text );
  182. }
  183. endif;