PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/nonus/theme/shortcodes/portfolio/ctWorksShortcode.class.php

https://github.com/alniko009/magic
PHP | 288 lines | 237 code | 27 blank | 24 comment | 16 complexity | 70a2d3aa9afb4dd6aabec1b60329f9d5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Draws works
  4. */
  5. class ctWorksShortcode extends ctShortcodeQueryable {
  6. /**
  7. * Returns name
  8. * @return string|void
  9. */
  10. public function getName() {
  11. return 'Works';
  12. }
  13. /**
  14. * Shortcode name
  15. * @return string
  16. */
  17. public function getShortcodeName() {
  18. return 'works';
  19. }
  20. public function enqueueScripts() {
  21. wp_register_script('ct-isotope', CT_THEME_ASSETS . '/js/jquery.isotope.min.js');
  22. wp_enqueue_script('ct-isotope');
  23. wp_register_script('ct-ba-bbq', CT_THEME_ASSETS . '/js/jquery.ba-bbq.min.js');
  24. wp_enqueue_script('ct-ba-bbq');
  25. wp_register_script('ct-colorbox', CT_THEME_ASSETS . '/js/jquery.colorbox-min.js');
  26. wp_enqueue_script('ct-colorbox');
  27. }
  28. /**
  29. * Handles shortcode
  30. * @param $atts
  31. * @param null $content
  32. * @return string
  33. */
  34. public function handle($atts, $content = null) {
  35. $attributes = shortcode_atts($this->extractShortcodeAttributes($atts), $atts);
  36. extract($attributes);
  37. $recentposts = $this->getCollection($attributes, array('post_type' => 'portfolio'));
  38. $id = rand(100, 1000);
  39. $this->addInlineJS($this->getInlineJS($id));
  40. //filters
  41. $filtersOn = $filters == "on" || $filters == "true";
  42. $filtersHtml = '';
  43. if ($filtersOn) {
  44. $catsListHtml = $allfilterheader ? '<li><a href="#filter=*" class="selected btn btn-primary btn-small">' . $allfilterheader . '</a></li>' : '';
  45. $terms = get_terms('portfolio_category', 'hide_empty=1');
  46. foreach ($terms as $term) {
  47. $catsListHtml .= '<li><a href="#filter=.' . $this->getCatFilterClass($term) . '" class="btn btn-primary btn-small">' . $term->name . '</a></li>';
  48. }
  49. if ($catsListHtml) {
  50. $filtersHtml = '<div class="row-fluid">
  51. <nav class="span12">
  52. <div id="IsotopeOptions" class="portfolio-filters">
  53. <span>' . __('View', 'ct_theme') . ':</span>
  54. <ul class="isotope-options">
  55. ' . $catsListHtml . '
  56. </ul>
  57. </div>
  58. </nav>
  59. </div>';
  60. }
  61. }
  62. switch ($columns) {
  63. case 2:
  64. $size = 'thumb_2_cols';
  65. $class = ' col2';
  66. break;
  67. case 3:
  68. $size = 'thumb_3_cols';
  69. $class = ' col3';
  70. break;
  71. default:
  72. $size = 'thumb_4_cols';
  73. $class = ' col4';
  74. }
  75. //elements
  76. $elements = '';
  77. $counter = 0;
  78. foreach ($recentposts as $p) {
  79. $counter++;
  80. $link = get_permalink($p);
  81. $title = $titles == "yes" ? '<span class="cTitle">' . $p->post_title . '</span>' : '';
  82. $summary = $summaries == "yes" && $p->post_excerpt ? '<p>' . $p->post_excerpt . '</p>' : '';
  83. $cats = '';
  84. $catsFilters = "";
  85. $terms = get_the_terms($p->ID, 'portfolio_category');
  86. if ($terms) {
  87. foreach ($terms as $term) {
  88. $catsFilters .= (" " . $this->getCatFilterClass($term));
  89. $cats .= ($term->name . ", ");
  90. }
  91. $cats = substr($cats, 0, -2);
  92. }
  93. $cats = $categories == "yes" ? '<span class="cTag">' .$cats . '</span>' : '';
  94. $imgsrc = $images == "yes" ? ct_get_feature_image_src($p->ID, $size) : '';
  95. $overlayHtml = '';
  96. if(($title != false)||($summary != false)||($cats != false)) {
  97. $overlayHtml = '<div class="cOverlay"><div class="inner">
  98. ' . $title . '
  99. ' . $cats . '
  100. ' . $summary . '
  101. </div></div>';
  102. }
  103. $elements .= '<div class="isotope-item' . $catsFilters . '">
  104. <a href="' . $link . '">
  105. <img src="' . $imgsrc . '" alt="">
  106. ' . $overlayHtml . '
  107. </a>
  108. </div>';
  109. }
  110. $headerHtml = $header ? ('<h4 class="text-center">' . $header . '</h4><div class="spacer" style="height: 20px;"></div>') : '';
  111. $html = $headerHtml . $filtersHtml . '<div class="row-fluid">
  112. <div class="span12">
  113. <!-- ISOTOPE GALLERY -->
  114. <div id="IsotopeContainer" class="isotope' . $class . '">
  115. ' . $elements . '
  116. </div>
  117. </div>
  118. </div>';
  119. return do_shortcode($html);
  120. }
  121. /**
  122. * returns JS
  123. * @param $id
  124. * @return string
  125. */
  126. protected function getInlineJS($id) {
  127. return '/* ISOTOPE */
  128. jQuery(window).load(function () {
  129. var $container = jQuery("#IsotopeContainer"), // object that will keep track of options
  130. isotopeOptions = {}, // defaults, used if not explicitly set in hash
  131. defaultOptions = {
  132. filter: "*",
  133. sortBy: "original-order",
  134. sortAscending: true,
  135. layoutMode: "masonry"
  136. };
  137. var setupOptions = jQuery.extend({}, defaultOptions, {
  138. itemSelector: ".isotope-item",
  139. masonry: {
  140. // columnWidth: $container.width() / 4
  141. }
  142. });
  143. // set up Isotope
  144. $container.isotope(setupOptions);
  145. var $optionSets = jQuery("#IsotopeOptions").find(".isotope-options"), isOptionLinkClicked = false;
  146. // switches selected class on buttons
  147. function changeSelectedLink($elem) {
  148. if (!($elem === undefined)) {
  149. // remove selected class on previous item
  150. $elem.closest(".isotope-options").find(".selected").removeClass("selected");
  151. // set selected class on new item
  152. $elem.addClass("selected");
  153. }
  154. }
  155. $optionSets.find("a").click(function () {
  156. var $this = jQuery(this);
  157. // dont proceed if already selected
  158. if ($this.hasClass("selected")) {
  159. return;
  160. }
  161. changeSelectedLink($this);
  162. // get href attr, remove leading #
  163. var href = $this.attr("href").replace(/^#/, ""), // convert href into object
  164. option = jQuery.deparam(href, true);
  165. // apply new option to previous
  166. jQuery.extend(isotopeOptions, option);
  167. // set hash, triggers hashchange on window
  168. jQuery.bbq.pushState(isotopeOptions);
  169. isOptionLinkClicked = true;
  170. return false;
  171. });
  172. var hashChanged = false;
  173. jQuery(window).bind("hashchange", function (event) {
  174. // get options object from hash
  175. var hashOptions = window.location.hash ? jQuery.deparam.fragment(window.location.hash, true) : {}, // do not animate first call
  176. aniEngine = hashChanged ? "best-available" : "none", // apply defaults where no option was specified
  177. options = jQuery.extend({}, defaultOptions, hashOptions, { animationEngine: aniEngine });
  178. // apply options from hash
  179. $container.isotope(options);
  180. // save options
  181. isotopeOptions = hashOptions;
  182. // if option link was not clicked
  183. // then well need to update selected links
  184. if (!isOptionLinkClicked) {
  185. // iterate over options
  186. var hrefObj, hrefValue, $selectedLink;
  187. for (var key in options) {
  188. hrefObj = {};
  189. hrefObj[ key ] = options[ key ];
  190. // convert object into parameter string
  191. hrefValue = jQuery.param(hrefObj);
  192. // get matching link
  193. $selectedLink = $optionSets.find("a[href=' . "'" . '#" + hrefValue + "' . "'" . ']");
  194. changeSelectedLink($selectedLink);
  195. }
  196. }
  197. isOptionLinkClicked = false;
  198. hashChanged = true;
  199. })// trigger hashchange to capture any hash data on init
  200. .trigger("hashchange");
  201. });
  202. /* ISOTOPE */';
  203. }
  204. /**
  205. * creates class name for the category
  206. * @param $cat
  207. * @return string
  208. */
  209. protected function getCatFilterClass($cat){
  210. return strtolower(str_replace(' ', '-', $cat->slug));
  211. }
  212. /**
  213. * Shortcode type
  214. * @return string
  215. */
  216. public function getShortcodeType() {
  217. return self::TYPE_SHORTCODE_SELF_CLOSING;
  218. }
  219. /**
  220. * Returns config
  221. * @return null
  222. */
  223. public function getAttributes() {
  224. $atts = $this->getAttributesWithQuery(array(
  225. 'limit' => array('label' => __('limit', 'ct_theme'), 'default' => 4, 'type' => 'input', 'help' => __("Number of portfolio elements", 'ct_theme')),
  226. 'filters' => array('label' => __('filters', 'ct_theme'), 'default' => 'false', 'type' => 'checkbox', 'help' => __("Show filters?", 'ct_theme')),
  227. 'allfilterheader' => array('label' => __('"all filter" header', 'ct_theme'), 'default' => __('All', 'ct_theme'), 'type' => 'input', 'help' => __("Header for the 'show all' filter", 'ct_theme')),
  228. 'columns' => array('label' => __('columns', 'ct_theme'), 'default' => '4', 'type' => 'select', 'choices' => array('4' => '4', '3' => '3', '2' => '2'), 'help' => __("Number of columns", 'ct_theme')),
  229. 'titles' => array('label' => __('titles', 'ct_theme'), 'default' => 'no', 'type' => 'select', 'choices' => array('yes' => __('yes', 'ct_theme'), 'no' => __('no', 'ct_theme')), 'help' => __("Show titles?", 'ct_theme')),
  230. 'summaries' => array('label' => __('summaries', 'ct_theme'), 'default' => 'no', 'type' => 'select', 'choices' => array('yes' => __('yes', 'ct_theme'), 'no' => __('no', 'ct_theme')), 'help' => __("Show excerpts?", 'ct_theme')),
  231. 'categories' => array('label' => __('categories', 'ct_theme'), 'default' => 'no', 'type' => 'select', 'choices' => array('yes' => __('yes', 'ct_theme'), 'no' => __('no', 'ct_theme')), 'help' => __("Show categories?", 'ct_theme')),
  232. 'header' => array('label' => __("header text", 'ct_theme'), 'default' => '', 'type' => 'input'),
  233. 'images' => array('label' => __('images', 'ct_theme'), 'default' => 'yes', 'type' => 'select', 'choices' => array('yes' => __('yes', 'ct_theme'), 'no' => __('no', 'ct_theme')), 'help' => __("Show images?", 'ct_theme')),
  234. ));
  235. if (isset($atts['cat'])) {
  236. unset($atts['cat']);
  237. }
  238. return $atts;
  239. }
  240. }
  241. new ctWorksShortcode();