PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/themes/vtte-base/inc/helpers.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 312 lines | 254 code | 26 blank | 32 comment | 36 complexity | e8033d20801efdac9895e2e5100d31ca MD5 | raw file
  1. <?php
  2. use GutenPress\Forms;
  3. use GutenPress\Forms\Element;
  4. use GutenPress\Validate;
  5. use GutenPress\Validate\Validations;
  6. use GutenPress\Model;
  7. class videos {
  8. /* function get_video
  9. * Extract and return video id from youtbe & vimeo videos
  10. * @param url : Video url
  11. */
  12. static function get_video($url,$width=560,$height=315) {
  13. if (strstr($url,'youtube')){
  14. $parse_url=parse_url($url);
  15. parse_str($parse_url['query']);
  16. $id=$v;
  17. $iframe='<iframe width="'.$width.'" height="'.$height.'" src="http://www.youtube.com/embed/'.$id.'" frameborder="0" allowfullscreen></iframe>';
  18. return $iframe;
  19. }
  20. if (strstr($url,'vimeo')) {
  21. if (!empty($url)) {
  22. $id=array_pop(explode('/',$url));
  23. $iframe='<iframe src="http://player.vimeo.com/video/'.$id.'" width="'.$width.'" height="'.$height.'" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
  24. return $iframe;
  25. }
  26. }
  27. return false;
  28. }
  29. static function render_video_button($video,$active=false) {
  30. $active_class = ($active) ? ' active' : '';
  31. $url = get_post_meta($video->ID,'_video_url',true);
  32. echo '<figure class="hentry entry-video clearfix'.$active_class.'">';
  33. echo '<a href="'.$url.'">';
  34. echo '<img src="'.self::get_video_thumb($url,'big').'" alt="'.$video->post_title.'">';
  35. echo '<div class="video-info">';
  36. echo '<h4 class="entry-title">'.$video->post_title.'</h4>';
  37. if (!empty($video->post_content)) {
  38. echo '<div class="entry-description">';
  39. echo apply_filters( 'the_content', $video->post_content );
  40. echo '</div>';
  41. }
  42. echo '</div>';
  43. echo '</a>';
  44. echo '</figure>';
  45. }
  46. static function get_video_class($url) {
  47. if (strstr($url,'youtube')){
  48. return 'youtube-lightbox';
  49. }
  50. if (strstr($url,'vimeo')){
  51. return 'vimeo-lightbox';
  52. }
  53. }
  54. /**
  55. * function get_video_thumb
  56. * Extract and return video id from youtbe & vimeo videos
  57. * @param url : Video url
  58. * @param size: small, medium
  59. */
  60. static function get_video_thumb($url,$size) {
  61. if (strstr($url,'youtube')){
  62. $parse_url=parse_url($url);
  63. parse_str($parse_url['query']);
  64. $id=$v;
  65. $def_size = ($size=='small')?1:0;
  66. $thumb='http://img.youtube.com/vi/'.$id.'/'.$def_size.'.jpg';
  67. return $thumb;
  68. }
  69. if (strstr($url,'vimeo')) {
  70. $thumb = '';
  71. if (!empty($url)) {
  72. if ( false === ( $thumb = get_transient( 'vimeo_'.$id ) ) ) {
  73. $id=array_pop(explode('/',$url));
  74. $request = new WP_Http;
  75. $result = $request->request( 'http://vimeo.com/api/v2/video/'.$id.'.php' , $args );
  76. //$iframe='<iframe src="http://player.vimeo.com/video/'.$id.'?title=0&amp;byline=0&amp;portrait=0" width="'.$width.'" height="'.$height.'" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
  77. if (!is_wp_error( $result )) {
  78. $body = maybe_unserialize($result['body']);
  79. if ($size == 'medium')
  80. $thumb = $body[0]['thumbnail_medium'];
  81. else
  82. $thumb = $body[0]['thumbnail_large'];
  83. set_transient('vimeo_'.$id,$thumb,60*60*24);
  84. }
  85. }
  86. return $thumb;
  87. }
  88. }
  89. return false;
  90. }
  91. static function get_last_videos($size=3) {
  92. $video = query_posts(array(
  93. 'post_type' => 'videos',
  94. 'posts_per_page' => $size,
  95. 'orderby' => 'date',
  96. 'order' => 'DESC'
  97. ));
  98. return $video;
  99. }
  100. }
  101. // helpers
  102. /**
  103. * Generate a exceprt from whatever argument it's passed to it
  104. * @param $post object|string Post object with excerpt and/or content OR string
  105. * @param $args object|array|string Arguments: length, echo, strict mode
  106. * @return string Formatted excerpt
  107. * @author Felipe Lavín <felipe@yukei.net>
  108. * */
  109. function do_excerpt($post, $args=null){
  110. $params = wp_parse_args($args, array(
  111. 'length' => 255,
  112. 'echo' => false,
  113. 'strict' => false,
  114. 'wrap' => null,
  115. 'wrap_id' => null,
  116. 'wrap_class' => 'entry-excerpt',
  117. 'hellip' => false,
  118. 'append' => null
  119. ));
  120. $out = $wrap = '';
  121. if ( $params['wrap'] ) {
  122. $wrap_id = $params['wrap_id'] ? ' id="'. esc_attr( $params['wrap_id'] ) .'"' : null;
  123. $wrap_class = $params['wrap_class'] ? ' class="'. esc_attr( $params['wrap_class'] ) .'"' : null;
  124. $wrap = '<'. $params['wrap'] . $wrap_id . $wrap_class .'>';
  125. }
  126. if ( is_string($post) ) {
  127. $excerpt = strip_shortcodes($post);
  128. $excerpt = strip_tags($excerpt);
  129. if ( strlen($excerpt) > $params['length'] ) {
  130. $excerpt = smart_substr($excerpt, $params['length']);
  131. if ( $params['hellip'] ) $excerpt .= ' '. $params['hellip'];
  132. }
  133. if ( $params['append'] ) $excerpt .= ' '. $params['append'];
  134. $out .= apply_filters('the_excerpt', $excerpt);
  135. } elseif ( is_object($post) ) {
  136. if ( isset($post->post_excerpt) && !empty($post->post_excerpt) ) {
  137. if ( $params['strict'] && strlen($post->post_excerpt) > $params['length'] ) {
  138. $buff = smart_substr($post->post_excerpt, $params['length']);
  139. if ($params['hellip'] ) $buff .= ' '. $params['hellip'];
  140. if ( $params['append'] ) $buff .= ' '. $params['append'];
  141. $out .= apply_filters('the_excerpt', $buff);
  142. } else {
  143. if ( $params['append'] ) $post->post_excerpt .= ' '. $params['append'];
  144. $out .= apply_filters('the_excerpt', $post->post_excerpt);
  145. }
  146. } elseif ( isset($post->post_content) && !empty($post->post_content) ) {
  147. return do_excerpt($post->post_content, $params);
  148. }
  149. }
  150. if ( !$out ) return false;
  151. if ( $out ) {
  152. if ( $params['wrap'] ) {
  153. $wrap .= $out . '</'. $params['wrap'] .'>';
  154. $out = $wrap;
  155. }
  156. if( $params['echo'] ) echo $out;
  157. else return $out;
  158. }
  159. }
  160. /**
  161. * Smarter text cutting
  162. * @param $str string contenido a cortar
  163. * @param string cantidad de caracteres que se mostraran
  164. * @return string
  165. * @author Basilio Cáceres <bcaceres@ayerviernes.com>
  166. */
  167. function smart_substr($str,$n,$hellip=true){
  168. if ( strlen($str) > $n ) {
  169. $out = substr( strip_tags($str), 0, $n );
  170. $out = explode(" ",$out);
  171. array_pop( $out );
  172. $out = implode(" ",$out);
  173. if ( $hellip ) $out .= ' [&hellip;]';
  174. } else {
  175. $out = $str;
  176. }
  177. return $out;
  178. }
  179. // METABOX
  180. class MpostMeta extends Model\PostMeta{
  181. protected function setId(){
  182. return 'page';
  183. }
  184. protected function setDataModel(){
  185. return array(
  186. new Model\PostMetaData(
  187. 'title',
  188. 'Título de información',
  189. '\GutenPress\Forms\Element\InputText',
  190. array(
  191. )
  192. ),
  193. new Model\PostMetaData(
  194. 'icon',
  195. 'Ícono de título',
  196. '\GutenPress\Forms\Element\InputText',
  197. array(
  198. 'description' => 'escoger desde alguno de los íconos de <a href="https://developer.wordpress.org/resource/dashicons/" target="_blank">Dashicons</a> y especificar sólamente la clase que utiliza (por defecto se asume el ícono info)'
  199. )
  200. ),
  201. new Model\PostMetaData(
  202. 'info',
  203. 'Información extra para sidebar',
  204. '\GutenPress\Forms\Element\WPEditor',
  205. array(
  206. 'textarea_rows' => 7,
  207. 'media_buttons' => false,
  208. 'description' => 'alguna información asociada que se quiera mostrar en el sidebar de la página, si no se detalla no aparecerá'
  209. )
  210. )
  211. );
  212. }
  213. }
  214. new Model\Metabox( 'MpostMeta', 'Información extra de la Página', 'page', array('priority' => 'high') );
  215. /*
  216. Super PHP Plugin to add Full SVG Media support to WordPress
  217. Author URI: http://www.lewiscowles.co.uk/
  218. */
  219. class SVGSupport {
  220. function __construct() {
  221. add_action( 'admin_init', [ $this, 'add_svg_upload' ] );
  222. add_action( 'load-post.php', [ $this, 'add_editor_styles' ] );
  223. add_action( 'load-post-new.php', [ $this, 'add_editor_styles' ] );
  224. }
  225. public function add_svg_upload() {
  226. ob_start();
  227. add_action( 'wp_ajax_adminlc_mce_svg.css', [ $this, 'tinyMCE_svg_css' ] );
  228. add_filter( 'image_send_to_editor', [ $this, 'remove_dimensions_svg' ], 10 );
  229. add_filter( 'upload_mimes', [ $this, 'filter_mimes' ] );
  230. add_action( 'shutdown', [ $this, 'on_shutdown' ], 0 );
  231. add_filter( 'final_output', [ $this, 'fix_template' ] );
  232. }
  233. public function add_editor_styles() {
  234. add_filter( 'mce_css', [ $this, 'filter_mce_css' ] );
  235. }
  236. public function filter_mce_css( $mce_css ) {
  237. global $current_screen;
  238. $mce_css .= ', ' . '/wp-admin/admin-ajax.php?action=adminlc_mce_svg.css';
  239. return $mce_css;
  240. }
  241. public function remove_dimensions_svg( $html = '' ) {
  242. return str_ireplace( [ " width=\"1\"", " height=\"1\"" ], "", $html );
  243. }
  244. public function tinyMCE_svg_css() {
  245. header( 'Content-type: text/css' );
  246. echo 'img[src$=".svg"] { width: 100%; height: auto; }';
  247. exit();
  248. }
  249. public function filter_mimes( $mimes = [] ){
  250. $mimes[ 'svg' ] = 'image/svg+xml';
  251. return $mimes;
  252. }
  253. public function on_shutdown() {
  254. $final = '';
  255. $ob_levels = count( ob_get_level() );
  256. for ( $i = 0; $i < $ob_levels; $i++ ) {
  257. $final .= ob_get_clean();
  258. }
  259. echo apply_filters( 'final_output', $final );
  260. }
  261. public function fix_template( $content = '' ) {
  262. $content = str_replace(
  263. '<# } else if ( \'image\' === data.type && data.sizes && data.sizes.full ) { #>',
  264. '<# } else if ( \'svg+xml\' === data.subtype ) { #>
  265. <img class="details-image" src="{{ data.url }}" draggable="false" />
  266. <# } else if ( \'image\' === data.type && data.sizes && data.sizes.full ) { #>',
  267. $content
  268. );
  269. $content = str_replace(
  270. '<# } else if ( \'image\' === data.type && data.sizes ) { #>',
  271. '<# } else if ( \'svg+xml\' === data.subtype ) { #>
  272. <div class="centered">
  273. <img src="{{ data.url }}" class="thumbnail" draggable="false" />
  274. </div>
  275. <# } else if ( \'image\' === data.type && data.sizes ) { #>',
  276. $content
  277. );
  278. return $content;
  279. }
  280. }
  281. new SVGSupport();
  282. //uploads filter
  283. add_filter('sanitize_file_name', 'sa_sanitize_spanish_chars', 10);
  284. function sa_sanitize_spanish_chars ($filename) {
  285. return remove_accents( $filename );
  286. }