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

/wp-content/themes/nonus/theme/shortcodes/images/ctImageShortcode.class.php

https://github.com/alniko009/magic
PHP | 122 lines | 64 code | 21 blank | 37 comment | 8 complexity | df6ab38de65721ea8f8e4ea4b39e3972 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Image shortcode
  4. */
  5. class ctImageShortcode extends ctShortcode {
  6. /**
  7. * default image link base
  8. */
  9. const DEFAULT_IMG_SRC = "http://dummyimage.com/";
  10. /**
  11. * default image width
  12. */
  13. const DEFAULT_IMG_WIDTH = 90;
  14. /**
  15. * default image heightd
  16. */
  17. const DEFAULT_IMG_HEIGHT = 90;
  18. /**
  19. * Returns name
  20. * @return string|void
  21. */
  22. public function getName() {
  23. return 'Image';
  24. }
  25. /**
  26. * Shortcode name
  27. * @return string
  28. */
  29. public function getShortcodeName() {
  30. return 'img';
  31. }
  32. /**
  33. * Handles shortcode
  34. * @param $atts
  35. * @param null $content
  36. * @return string
  37. */
  38. public function handle($atts, $content = null) {
  39. extract(shortcode_atts($this->extractShortcodeAttributes($atts), $atts));
  40. $src = $src ? $src : $this->getDefaultImgSrc($width, $height);
  41. switch ($align) {
  42. case 'center':
  43. $style = 'display:block; margin:0 auto;';
  44. $divContainerStyle = "text-align:center";
  45. break;
  46. case 'left':
  47. $style = 'float: left';
  48. $divContainerStyle = "text-align:left";
  49. break;
  50. case 'right':
  51. $style = 'float: right';
  52. $divContainerStyle = "text-align:right";
  53. break;
  54. default:
  55. $style = '';
  56. $divContainerStyle = "";
  57. }
  58. $class = $class ? $class : '';
  59. if ($width) {
  60. $width = $width ? 'width=' . $width : '';
  61. }
  62. if ($height) {
  63. $height = $height ? 'height=' . $height : '';
  64. }
  65. $overlayHtml = '';
  66. $img = '<img class="' . $class . '" alt="' . $alt . '" src="' . $src . '" ' . $width . ' ' . $height . ' style="' . $style . '">' . $overlayHtml;
  67. //link
  68. if ($link && $overlay == 'no') {
  69. $img = '<a href="' . $link . '">' . $img . '</a>';
  70. }
  71. return do_shortcode($img);
  72. }
  73. /**
  74. * Returns config
  75. * @return null
  76. */
  77. public function getAttributes() {
  78. return array(
  79. 'src' => array('label' => __("image", 'ct_theme'), 'default' => '', 'type' => 'image', 'help' => __("Image source", 'ct_theme')),
  80. 'alt' => array('label' => __('alt', 'ct_theme'), 'default' => '', 'type' => 'input', 'help' => __("Alternate text", 'ct_theme')),
  81. 'width' => array('label' => __('width', 'ct_theme'), 'default' => self::DEFAULT_IMG_WIDTH, 'type' => 'input', 'help' => __("Image width", 'ct_theme')),
  82. 'height' => array('label' => __('height', 'ct_theme'), 'default' => self::DEFAULT_IMG_HEIGHT, 'type' => 'input', 'help' => __("Image height", 'ct_theme')),
  83. 'align' => array('label' => __('align', 'ct_theme'), 'default' => 'auto', 'type' => 'select', 'options' => array('default' => __('Default', 'ct_theme'), 'left' => __('Left', 'ct_theme'), 'center' => __('Center', 'ct_theme'), 'right' => __('Right', 'ct_theme')), 'help' => __("Image align", 'ct_theme')),
  84. 'link' => array('label' => __('link', 'ct_theme'), 'help' => __("ex. http://www.google.com", 'ct_theme')),
  85. 'class' => array('label' => __("class", 'ct_theme'), 'default' => '', 'type' => 'input', 'help' => __("Image class", 'ct_theme')),
  86. );
  87. }
  88. /**
  89. * returns default image source
  90. * @param $width
  91. * @param $height
  92. * @return string
  93. */
  94. protected function getDefaultImgSrc($width, $height) {
  95. if($width && $height){
  96. return self::DEFAULT_IMG_SRC . $width . "x" . $height;
  97. }
  98. return '';
  99. }
  100. }
  101. new ctImageShortcode();