Advertisement
Guest User

srcset

a guest
Mar 28th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.18 KB | None | 0 0
  1. <?php
  2. /**
  3. * Image
  4. *
  5. * Shortcode which inserts an image of your choice
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly
  8.  
  9.  
  10. if ( !class_exists( 'avia_sc_image' ) )
  11. {
  12. class avia_sc_image extends aviaShortcodeTemplate
  13. {
  14. /**
  15. * Create the config array for the shortcode button
  16. */
  17. function shortcode_insert_button()
  18. {
  19. $this->config['self_closing'] = 'no';
  20.  
  21. $this->config['name'] = __('Image', 'avia_framework' );
  22. $this->config['tab'] = __('Media Elements', 'avia_framework' );
  23. $this->config['icon'] = AviaBuilder::$path['imagesURL']."sc-image.png";
  24. $this->config['order'] = 100;
  25. $this->config['target'] = 'avia-target-insert';
  26. $this->config['shortcode'] = 'av_image';
  27. //$this->config['modal_data'] = array('modal_class' => 'mediumscreen');
  28. $this->config['tooltip'] = __('Inserts an image of your choice', 'avia_framework' );
  29. $this->config['preview'] = 1;
  30. $this->config['disabling_allowed'] = true;
  31. }
  32.  
  33. function extra_assets()
  34. {
  35. //load css
  36. wp_enqueue_style( 'avia-module-image' , AviaBuilder::$path['pluginUrlRoot'].'avia-shortcodes/image/image.css' , array('avia-layout'), false );
  37.  
  38. }
  39.  
  40. /**
  41. * Popup Elements
  42. *
  43. * If this function is defined in a child class the element automatically gets an edit button, that, when pressed
  44. * opens a modal window that allows to edit the element properties
  45. *
  46. * @return void
  47. */
  48. function popup_elements()
  49. {
  50. $this->elements = array(
  51.  
  52. array(
  53. "type" => "tab_container", 'nodescription' => true
  54. ),
  55.  
  56. array(
  57. "type" => "tab",
  58. "name" => __("Image Settings" , 'avia_framework'),
  59. 'nodescription' => true
  60. ),
  61.  
  62. array(
  63. "name" => __("Choose Image",'avia_framework' ),
  64. "desc" => __("Either upload a new, or choose an existing image from your media library",'avia_framework' ),
  65. "id" => "src",
  66. "type" => "image",
  67. "title" => __("Insert Image",'avia_framework' ),
  68. "button" => __("Insert",'avia_framework' ),
  69. "std" => AviaBuilder::$path['imagesURL']."placeholder.jpg"),
  70.  
  71.  
  72.  
  73. array(
  74. "name" => __("Image Alignment", 'avia_framework' ),
  75. "desc" => __("Choose here, how to align your image", 'avia_framework' ),
  76. "id" => "align",
  77. "type" => "select",
  78. "std" => "center",
  79. "subtype" => array(
  80. __('Center', 'avia_framework' ) =>'center',
  81. __('Right', 'avia_framework' ) =>'right',
  82. __('Left', 'avia_framework' ) =>'left',
  83. __('No special alignment', 'avia_framework' ) =>'',
  84. )
  85. ),
  86.  
  87. array(
  88. "name" => __("Image Styling", 'avia_framework' ),
  89. "desc" => __("Choose a styling variaton", 'avia_framework' ),
  90. "id" => "styling",
  91. "type" => "select",
  92. "std" => "",
  93. "subtype" => array(
  94. __('Default', 'avia_framework' ) =>'',
  95. __('Circle (image height and width must be equal)', 'avia_framework' ) =>'circle',
  96. __('No Styling (no border, no border radius etc)', 'avia_framework' ) =>'no-styling',
  97. )
  98. ),
  99.  
  100. array(
  101. "name" => __("Image Hover effect", 'avia_framework' ),
  102. "desc" => __("Add a mouse hover effect to the image", 'avia_framework' ),
  103. "id" => "hover",
  104. "type" => "select",
  105. "std" => "",
  106. "subtype" => array(
  107. __('No', 'avia_framework' ) => '',
  108. __('Yes, slightly increase the image size', 'avia_framework' ) => 'av-hover-grow',
  109. __('Yes, slightly zoom the image', 'avia_framework' ) => 'av-hover-grow av-hide-overflow',
  110. )
  111. ),
  112.  
  113. array(
  114. "type" => "close_div",
  115. 'nodescription' => true
  116. ),
  117.  
  118. array(
  119. "type" => "tab",
  120. "name" => __("Link Settings",'avia_framework' ),
  121. 'nodescription' => true
  122. ),
  123.  
  124. array(
  125. "name" => __("Image Link?", 'avia_framework' ),
  126. "desc" => __("Where should your image link to?", 'avia_framework' ),
  127. "id" => "link",
  128. "type" => "linkpicker",
  129. "fetchTMPL" => true,
  130. "subtype" => array(
  131. __('No Link', 'avia_framework' ) =>'',
  132. __('Lightbox', 'avia_framework' ) =>'lightbox',
  133. __('Set Manually', 'avia_framework' ) =>'manually',
  134. __('Single Entry', 'avia_framework' ) =>'single',
  135. __('Taxonomy Overview Page', 'avia_framework' )=>'taxonomy',
  136. ),
  137. "std" => ""),
  138.  
  139. array(
  140. "name" => __("Open new tab/window", 'avia_framework' ),
  141. "desc" => __("Do you want to open the link url in a new tab/window?", 'avia_framework' ),
  142. "id" => "target",
  143. "type" => "select",
  144. "std" => "",
  145. "required"=> array('link','not_empty_and','lightbox'),
  146. "subtype" => AviaHtmlHelper::linking_options()
  147. ),
  148.  
  149.  
  150.  
  151.  
  152. array(
  153. "type" => "close_div",
  154. 'nodescription' => true
  155. ),
  156.  
  157. array(
  158. "type" => "tab",
  159. "name" => __("Caption",'avia_framework' ),
  160. 'nodescription' => true
  161. ),
  162.  
  163. array(
  164. "name" => __("Image Caption", 'avia_framework' ),
  165. "desc" => __("Display a caption overlay?", 'avia_framework' ),
  166. "id" => "caption",
  167. "type" => "select",
  168. "std" => "",
  169. "subtype" => array(
  170. __('No', 'avia_framework' ) =>'',
  171. __('Yes', 'avia_framework' ) =>'yes',
  172. )
  173. ),
  174.  
  175. array(
  176. "name" => __("Caption", 'avia_framework' ),
  177. "id" => "content",
  178. "type" => "textarea",
  179. "required" => array('caption','equals','yes'),
  180. "std" => "",
  181. ),
  182.  
  183.  
  184. array( "name" => __("Caption custom font size?", 'avia_framework' ),
  185. "desc" => __("Size of your caption in pixel", 'avia_framework' ),
  186. "id" => "font_size",
  187. "type" => "select",
  188. "required" => array('caption','equals','yes'),
  189. "subtype" => AviaHtmlHelper::number_array(10,40,1, array('Default' =>''),'px'),
  190. "std" => ""),
  191.  
  192. array(
  193. "name" => __("Caption Appearance", 'avia_framework' ),
  194. "desc" => __("When to display the caption?", 'avia_framework' ),
  195. "id" => "appearance",
  196. "type" => "select",
  197. "std" => "",
  198. "container_class" => 'av_half av_half_first',
  199. "required" => array('caption','equals','yes'),
  200. "subtype" => array(
  201. __('Always display caption', 'avia_framework' ) =>'',
  202. __('Only display on hover', 'avia_framework' ) =>'on-hover',
  203. )
  204. ),
  205.  
  206. array(
  207. "name" => __("Caption Overlay Opacity",'avia_framework' ),
  208. "desc" => __("Set the opacity of your overlay: 0.1 is barely visible, 1.0 is opaque ", 'avia_framework' ),
  209. "id" => "overlay_opacity",
  210. "type" => "select",
  211. "std" => "0.4",
  212. "container_class" => 'av_half',
  213. "required" => array('caption','equals','yes'),
  214. "subtype" => array( __('0.1','avia_framework' )=>'0.1',
  215. __('0.2','avia_framework' )=>'0.2',
  216. __('0.3','avia_framework' )=>'0.3',
  217. __('0.4','avia_framework' )=>'0.4',
  218. __('0.5','avia_framework' )=>'0.5',
  219. __('0.6','avia_framework' )=>'0.6',
  220. __('0.7','avia_framework' )=>'0.7',
  221. __('0.8','avia_framework' )=>'0.8',
  222. __('0.9','avia_framework' )=>'0.9',
  223. __('1.0','avia_framework' )=>'1',
  224. )
  225. ),
  226.  
  227. array(
  228. "name" => __("Caption Overlay Background Color", 'avia_framework' ),
  229. "desc" => __("Select a background color for your overlay here.", 'avia_framework' ),
  230. "id" => "overlay_color",
  231. "type" => "colorpicker",
  232. "container_class" => 'av_half av_half_first',
  233. "required" => array('caption','equals','yes'),
  234. "std" => "#000000",
  235. ),
  236.  
  237. array(
  238. "name" => __("Caption Font Color", 'avia_framework' ),
  239. "desc" => __("Select a font color for your overlay here.", 'avia_framework' ),
  240. "id" => "overlay_text_color",
  241. "type" => "colorpicker",
  242. "std" => "#ffffff",
  243. "container_class" => 'av_half',
  244. "required" => array('caption','equals','yes'),
  245. ),
  246.  
  247. array(
  248. "name" => __("Copyright Info", 'avia_framework' ),
  249. "desc" => __("Use the media manager to add/edit the copyright info.", 'avia_framework' ),
  250. "id" => "copyright",
  251. "type" => "select",
  252. "std" => "",
  253. "subtype" => array(
  254. __('No', 'avia_framework' ) =>'',
  255. __('Yes, always display copyright info', 'avia_framework' ) =>'always',
  256. __('Yes, display icon and reaveal copyright info on hover', 'avia_framework' ) =>'icon-reveal',
  257. )
  258. ),
  259.  
  260.  
  261.  
  262. array(
  263. "type" => "close_div",
  264. 'nodescription' => true
  265. ),
  266.  
  267. array(
  268. "type" => "tab",
  269. "name" => __("Animation",'avia_framework' ),
  270. 'nodescription' => true
  271. ),
  272.  
  273.  
  274. array(
  275. "name" => __("Image Fade in Animation", 'avia_framework' ),
  276. "desc" => __("Add a small animation to the image when the user first scrolls to the image position. This is only to add some 'spice' to the site and only works in modern browsers", 'avia_framework' ),
  277. "id" => "animation",
  278. "type" => "select",
  279. "std" => "no-animation",
  280. "subtype" => array(
  281. __('None', 'avia_framework' ) =>'no-animation',
  282.  
  283. __('Fade Animations', 'avia_framework') => array(
  284. __('Fade in', 'avia_framework' ) =>'fade-in',
  285. __('Pop up', 'avia_framework' ) =>'pop-up',
  286. ),
  287. __('Slide Animations', 'avia_framework') => array(
  288. __('Top to Bottom', 'avia_framework' ) =>'top-to-bottom',
  289. __('Bottom to Top', 'avia_framework' ) =>'bottom-to-top',
  290. __('Left to Right', 'avia_framework' ) =>'left-to-right',
  291. __('Right to Left', 'avia_framework' ) =>'right-to-left',
  292. ),
  293. __('Rotate', 'avia_framework') => array(
  294. __('Full rotation', 'avia_framework' ) =>'av-rotateIn',
  295. __('Bottom left rotation', 'avia_framework' ) =>'av-rotateInUpLeft',
  296. __('Bottom right rotation', 'avia_framework' ) =>'av-rotateInUpRight',
  297. )
  298. )
  299. ),
  300.  
  301. array(
  302. "type" => "close_div",
  303. 'nodescription' => true
  304. ),
  305.  
  306. array(
  307. "type" => "tab",
  308. "name" => __("Screen Options",'avia_framework' ),
  309. 'nodescription' => true
  310. ),
  311.  
  312.  
  313. array(
  314. "name" => __("Element Visibility",'avia_framework' ),
  315. "desc" => __("Set the visibility for this element, based on the device screensize.", 'avia_framework' ),
  316. "type" => "heading",
  317. "description_class" => "av-builder-note av-neutral",
  318. ),
  319.  
  320. array(
  321. "desc" => __("Hide on large screens (wider than 990px - eg: Desktop)", 'avia_framework'),
  322. "id" => "av-desktop-hide",
  323. "std" => "",
  324. "container_class" => 'av-multi-checkbox',
  325. "type" => "checkbox"),
  326.  
  327. array(
  328.  
  329. "desc" => __("Hide on medium sized screens (between 768px and 989px - eg: Tablet Landscape)", 'avia_framework'),
  330. "id" => "av-medium-hide",
  331. "std" => "",
  332. "container_class" => 'av-multi-checkbox',
  333. "type" => "checkbox"),
  334.  
  335. array(
  336.  
  337. "desc" => __("Hide on small screens (between 480px and 767px - eg: Tablet Portrait)", 'avia_framework'),
  338. "id" => "av-small-hide",
  339. "std" => "",
  340. "container_class" => 'av-multi-checkbox',
  341. "type" => "checkbox"),
  342.  
  343. array(
  344.  
  345. "desc" => __("Hide on very small screens (smaller than 479px - eg: Smartphone Portrait)", 'avia_framework'),
  346. "id" => "av-mini-hide",
  347. "std" => "",
  348. "container_class" => 'av-multi-checkbox',
  349. "type" => "checkbox"),
  350.  
  351.  
  352. array(
  353. "type" => "close_div",
  354. 'nodescription' => true
  355. ),
  356.  
  357.  
  358. array(
  359. "type" => "close_div",
  360. 'nodescription' => true
  361. ),
  362.  
  363.  
  364. );
  365.  
  366.  
  367.  
  368.  
  369. }
  370.  
  371. /**
  372. * Editor Element - this function defines the visual appearance of an element on the AviaBuilder Canvas
  373. * Most common usage is to define some markup in the $params['innerHtml'] which is then inserted into the drag and drop container
  374. * Less often used: $params['data'] to add data attributes, $params['class'] to modify the className
  375. *
  376. *
  377. * @param array $params this array holds the default values for $content and $args.
  378. * @return $params the return array usually holds an innerHtml key that holds item specific markup.
  379. */
  380. function editor_element($params)
  381. {
  382. $template = $this->update_template("src", "<img src='{{src}}' alt=''/>");
  383. $img = "";
  384.  
  385. if(!empty($params['args']['attachment']) && !empty($params['args']['attachment_size']))
  386. {
  387. $img = wp_get_attachment_image($params['args']['attachment'],$params['args']['attachment_size']);
  388. }
  389. else if(isset($params['args']['src']) && is_numeric($params['args']['src']))
  390. {
  391. $img = wp_get_attachment_image($params['args']['src'],'large');
  392. }
  393. else if(!empty($params['args']['src']))
  394. {
  395. $img = "<img src='".$params['args']['src']."' alt='' />";
  396. }
  397.  
  398.  
  399. $params['innerHtml'] = "<div class='avia_image avia_image_style avia_hidden_bg_box'>";
  400. $params['innerHtml'] .= "<div ".$this->class_by_arguments('align' ,$params['args']).">";
  401. $params['innerHtml'] .= "<div class='avia_image_container' {$template}>{$img}</div>";
  402. $params['innerHtml'] .= "</div>";
  403. $params['innerHtml'] .= "</div>";
  404. $params['class'] = "";
  405.  
  406. return $params;
  407. }
  408.  
  409. /**
  410. * Frontend Shortcode Handler
  411. *
  412. * @param array $atts array of attributes
  413. * @param string $content text within enclosing form of shortcode element
  414. * @param string $shortcodename the shortcode found, when == callback name
  415. * @return string $output returns the modified html string
  416. */
  417. function shortcode_handler($atts, $content = "", $shortcodename = "", $meta = "")
  418. {
  419.  
  420. extract(AviaHelper::av_mobile_sizes($atts)); //return $av_font_classes, $av_title_font_classes and $av_display_classes
  421.  
  422. $output = "";
  423. $class = "";
  424. $alt = "";
  425. $title = "";
  426. $copyright_text = "";
  427.  
  428. $atts = shortcode_atts(
  429. array( 'src'=>'',
  430. 'animation'=>'no-animation',
  431. 'link'=>'',
  432. 'attachment'=>'',
  433. 'attachment_size'=>'',
  434. 'target'=>'no',
  435. 'styling' =>'',
  436. 'caption'=>'',
  437. 'copyright' => '',
  438. 'font_size'=>'',
  439. 'appearance'=>'',
  440. 'hover'=>'',
  441. 'align' => 'center',
  442. 'overlay_opacity'=>'0.4',
  443. 'overlay_color'=>'#444444',
  444. 'overlay_text_color'=>'#ffffff'
  445. ), $atts, $this->config['shortcode']);
  446.  
  447. extract($atts);
  448.  
  449. $img_h = "";
  450. $img_w = "";
  451.  
  452. if( ! empty( $attachment ) )
  453. {
  454. /**
  455. * Allows e.g. WPML to reroute to translated image
  456. */
  457. $posts = get_posts( array(
  458. 'include' => $attachment,
  459. 'post_status' => 'inherit',
  460. 'post_type' => 'attachment',
  461. 'post_mime_type' => 'image',
  462. 'order' => 'ASC',
  463. 'orderby' => 'post__in' )
  464. );
  465.  
  466. if( is_array( $posts ) && ! empty( $posts ) )
  467. {
  468. $attachment_entry = $posts[0];
  469.  
  470. $alt = get_post_meta($attachment_entry->ID, '_wp_attachment_image_alt', true);
  471. $alt = !empty($alt) ? esc_attr($alt) : '';
  472. $title = trim($attachment_entry->post_title) ? esc_attr($attachment_entry->post_title) : "";
  473.  
  474. if ($copyright !== "") {
  475. $copyright_text = get_post_meta($attachment_entry->ID, '_avia_attachment_copyright', true );
  476. }
  477.  
  478. if(!empty($attachment_size))
  479. {
  480. $src = wp_get_attachment_image_src($attachment_entry->ID, $attachment_size);
  481. $srcset = wp_get_attachment_image_srcset($attachment_entry->ID, $attachment_size);
  482.  
  483. $img_h= !empty($src[2]) ? $src[2] : "";
  484. $img_w= !empty($src[1]) ? $src[1] : "";
  485. $src = !empty($src[0]) ? $src[0] : "";
  486. }
  487. }
  488. }
  489. else
  490. {
  491. $attachment = false;
  492. }
  493.  
  494. if(!empty($src))
  495. {
  496. $class = $animation == "no-animation" ? "" :"avia_animated_image avia_animate_when_almost_visible ".$animation;
  497. $class .= " av-styling-".$styling." ".$hover;
  498.  
  499. if(is_numeric($src))
  500. {
  501. //$output = wp_get_attachment_image($src,'large');
  502. $img_atts = array('class' => "avia_image $class " . $this->class_by_arguments('align' ,$atts, true));
  503.  
  504. if(!empty($img_h)) $img_atts['height'] = $img_h;
  505. if(!empty($img_w)) $img_atts['width'] = $img_w;
  506.  
  507. $output = wp_get_attachment_image($src,'large',false, $img_atts );
  508. }
  509. else
  510. {
  511. $link = aviaHelper::get_url($link, $attachment);
  512.  
  513. $blank = (strpos($target, '_blank') !== false || $target == 'yes') ? ' target="_blank" ' : "";
  514. $blank .= strpos($target, 'nofollow') !== false ? ' rel="nofollow" ' : "";
  515.  
  516. $overlay = "";
  517. $style = "";
  518. $style .= AviaHelper::style_string($atts, 'overlay_text_color', 'color');
  519. if($font_size)
  520. {
  521. // $style = "style='font-size: {$font_size}px;'";
  522. $style .= AviaHelper::style_string($atts, 'font_size', 'font-size', 'px');
  523. }
  524. $style = AviaHelper::style_string($style);
  525.  
  526.  
  527. if($caption == "yes")
  528. {
  529.  
  530. $caption_style = "";
  531. $caption_style .= AviaHelper::style_string($atts, 'overlay_opacity', 'opacity');
  532. $caption_style .= AviaHelper::style_string($atts, 'overlay_color', 'background-color');
  533. $caption_style = AviaHelper::style_string($caption_style);
  534. $overlay_bg = "<div class='av-caption-image-overlay-bg' $caption_style></div>";
  535.  
  536. $content = ShortcodeHelper::avia_apply_autop(ShortcodeHelper::avia_remove_autop($content));
  537. $overlay = "<div class='av-image-caption-overlay'>{$overlay_bg}<div class='av-image-caption-overlay-position'><div class='av-image-caption-overlay-center' {$style}>{$content}</div></div></div>";
  538. $class .= " noHover ";
  539.  
  540. if(empty($appearance)) $appearance = "hover-deactivate";
  541. if($appearance) $class .= " av-overlay-".$appearance;
  542. }
  543.  
  544. $copyright_tag = "";
  545. if ($copyright_text) {
  546. $copyright_tag = "<small class='avia-copyright'>{$copyright_text}</small>";
  547. $class .= ' av-has-copyright';
  548. if ($copyright !== '') $class .= ' av-copyright-'.$copyright;
  549. }
  550.  
  551.  
  552. $markup_url = avia_markup_helper(array('context' => 'image_url','echo'=>false, 'custom_markup'=>$meta['custom_markup']));
  553. $markup = avia_markup_helper(array('context' => 'image','echo'=>false, 'custom_markup'=>$meta['custom_markup']));
  554.  
  555. $output .= "<div class='avia-image-container {$class} {$av_display_classes} ".$meta['el_class']." ".$this->class_by_arguments('align' ,$atts, true)."' $markup >";
  556. $output .= "<div class='avia-image-container-inner'>";
  557.  
  558. $output .= "<div class='avia-image-overlay-wrap'>";
  559.  
  560. if($link)
  561. {
  562. $output.= "<a href='{$link}' class='avia_image' {$blank}>{$overlay}<img class='avia_image ' src='{$src}' srcset='{$srcset}' alt='{$alt}' title='{$title}' $markup_url /></a>";
  563. }
  564. else
  565. {
  566. $hw = "";
  567. if(!empty($img_h)) $hw .= 'height="'.$img_h.'"';
  568. if(!empty($img_w)) $hw .= ' width="'.$img_w.'"';
  569.  
  570. $output.= "{$overlay}<img class='avia_image' src='{$src}' srcset='{$srcset}' alt='{$alt}' title='{$title}' {$hw} $markup_url />";
  571. }
  572. $output .= "</div>";
  573. $output .= $copyright_tag;
  574.  
  575. $output .= "</div>";
  576. $output .= "</div>";
  577. }
  578.  
  579.  
  580.  
  581.  
  582. }
  583.  
  584. return $output;
  585. }
  586.  
  587.  
  588. }
  589. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement