PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/nonus/framework/createit/tools.php

https://github.com/alniko009/magic
PHP | 274 lines | 157 code | 31 blank | 86 comment | 33 complexity | 8825de6cea560b4a26a2b4a9e993fc59 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. function current_page_url() {
  3. $pageURL = 'http';
  4. if (isset($_SERVER["HTTPS"])) {
  5. if ($_SERVER["HTTPS"] == "on") {
  6. $pageURL .= "s";
  7. }
  8. }
  9. $pageURL .= "://";
  10. if ($_SERVER["SERVER_PORT"] != "80") {
  11. $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  12. } else {
  13. $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  14. }
  15. return $pageURL;
  16. }
  17. /**
  18. * Returns option
  19. * @param string $id
  20. * @param string $default
  21. * @throws Exception
  22. * @return string
  23. */
  24. function ct_get_option($id, $default = '') {
  25. /** @var $NHP_Options ctNHP_Options */
  26. global $NHP_Options;
  27. if ($NHP_Options) {
  28. return $NHP_Options->get($id, $default);
  29. }
  30. throw new Exception("Settings not initialized!F");
  31. }
  32. /**
  33. * Returns option and wraps it into a pattern
  34. * @param string $name
  35. * @param string $pattern
  36. * @param null $default
  37. * @return string
  38. */
  39. function ct_get_option_pattern($name, $pattern, $default = null) {
  40. if (($v = ct_get_option($name)) && $v != $default) {
  41. return sprintf($pattern, esc_attr($v)) . "\n";
  42. }
  43. return '';
  44. }
  45. /**
  46. * Returns option and wraps it into a pattern
  47. * @param $params
  48. * @param $container
  49. * @param null $default
  50. * @return string
  51. */
  52. function ct_get_option_patterns($params, $container, $default = null) {
  53. $data = array();
  54. foreach ($params as $name => $pat) {
  55. if (($e = ct_get_option($name))) {
  56. if ($e != $default) {
  57. $data[] = sprintf($pat, $e);
  58. }
  59. }
  60. }
  61. if ($data) {
  62. return $container . '{' . implode(';', $data) . '}';
  63. }
  64. return '';
  65. }
  66. /**
  67. * returns feature image source for the post
  68. * @param $postId
  69. * @param string $size
  70. * @return string
  71. */
  72. function ct_get_feature_image_src($postId, $size = "thumbnail") {
  73. $imgsrc = '';
  74. if (has_post_thumbnail($postId)) {
  75. $image = wp_get_attachment_image_src(get_post_thumbnail_id($postId), $size);
  76. $imgsrc = $image[0];
  77. }
  78. return $imgsrc;
  79. }
  80. /**
  81. * returns
  82. * @param $postId
  83. * @return array string[]
  84. */
  85. function ct_get_categories_names($postId, $taxonomy = 'category') {
  86. $cats = get_the_terms($postId, $taxonomy);
  87. $result = array();
  88. if ($cats) {
  89. foreach ($cats as $cat) {
  90. $result[$cat->term_id] = $cat->name;
  91. }
  92. }
  93. return $result;
  94. }
  95. /**
  96. * returns
  97. * @param $postId
  98. * @param string $separator
  99. * @return string
  100. */
  101. function ct_get_categories_string($postId, $separator = ',', $taxonomy = 'category') {
  102. $cats = get_the_terms($postId, $taxonomy);
  103. $result = '';
  104. if ($cats) {
  105. foreach ($cats as $cat) {
  106. $result .= ($cat->name . $separator);
  107. }
  108. }
  109. return substr($result, 0, (-1 * strlen($separator)));
  110. }
  111. /**
  112. * returns posts grouped by categories ids
  113. * @param $atts
  114. * @param string $taxonomy
  115. * @return array
  116. */
  117. function ct_get_posts_grouped_by_cat($atts, $taxonomy = 'category') {
  118. $result = array();
  119. $query = new WP_Query;
  120. $faqs = $query->query($atts);
  121. foreach ($faqs as $faq) {
  122. if ($cats = get_the_terms($faq->ID, $taxonomy)) {
  123. foreach ($cats as $cat) {
  124. $result[$cat->term_id]['cat'] = $cat->name;
  125. $result[$cat->term_id]['posts'][] = $faq;
  126. }
  127. }
  128. }
  129. return $result;
  130. }
  131. /**
  132. * returns blog url
  133. * @return string|void
  134. */
  135. function ct_get_blog_url() {
  136. if ($posts_page_id = get_option('page_for_posts')) {
  137. if (function_exists('icl_object_id')) {
  138. $iclpageid = icl_object_id($posts_page_id, 'page', true, ICL_LANGUAGE_CODE);
  139. $posts_page_id = $iclpageid ? $iclpageid : $posts_page_id;
  140. }
  141. return home_url(get_page_uri($posts_page_id));
  142. } else {
  143. return home_url();
  144. }
  145. }
  146. /**
  147. * handles dynamic sidebars
  148. */
  149. function ct_dynamic_sidebar($name = 'sidebar-primary') {
  150. if (function_exists('MS_dynamic_sidebar')) {
  151. MS_dynamic_sidebar();
  152. } else {
  153. dynamic_sidebar($name);
  154. }
  155. }
  156. /**
  157. * gets excerpt from content by post id
  158. * @param $post_id
  159. * @param $excerpt_length - number of words
  160. */
  161. if (!function_exists('get_excerpt_by_id')) {
  162. function get_excerpt_by_id($post_id, $excerpt_length = 35) {
  163. $the_post = get_post($post_id); //Gets post ID
  164. if ($the_excerpt = $the_post->post_excerpt) {
  165. return '<p>' . $the_excerpt . '</p>';
  166. }
  167. $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
  168. $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
  169. $words = explode(' ', $the_excerpt, $excerpt_length + 1);
  170. if (count($words) > $excerpt_length) :
  171. array_pop($words);
  172. array_push($words, '…');
  173. $the_excerpt = implode(' ', $words);
  174. endif;
  175. $the_excerpt = '<p>' . $the_excerpt . '</p>';
  176. return $the_excerpt;
  177. }
  178. }
  179. /**
  180. * wp_nav_menu custom walker for breadcrumbs
  181. */
  182. class ctBreadCrumbWalker extends Walker {
  183. /**
  184. * @see Walker::$tree_type
  185. * @var string
  186. */
  187. var $tree_type = array('post_type', 'taxonomy', 'custom');
  188. /**
  189. * @see Walker::$db_fields
  190. * @var array
  191. */
  192. var $db_fields = array('parent' => 'menu_item_parent', 'id' => 'db_id');
  193. /**
  194. * delimiter for crumbs
  195. * @var string
  196. */
  197. var $delimiter = '';
  198. /**
  199. * @see Walker::start_el()
  200. *
  201. * @param string $output Passed by reference. Used to append additional content.
  202. * @param object $item Menu item data object.
  203. * @param int $depth Depth of menu item.
  204. * @param int $current_page Menu item ID.
  205. * @param object $args
  206. */
  207. function start_el(&$output, $item, $depth = 0, $args = array(), $current_object_id = 0) {
  208. //increasead compatiblity for WP >=3.6
  209. $this->start_element($output, $item, $depth, $args);
  210. }
  211. function start_element(&$output, $item, $depth, $args) {
  212. //Check if menu item is an ancestor of the current page
  213. $classes = empty($item->classes) ? array() : (array)$item->classes;
  214. $current_identifiers = array('current-menu-item', 'current-menu-parent', 'current-menu-ancestor');
  215. $ancestor_of_current = array_intersect($current_identifiers, $classes);
  216. if ($ancestor_of_current) {
  217. $title = apply_filters('the_title', $item->title, $item->ID);
  218. //Preceed with delimter for all but the first item.
  219. if (0 != $depth) {
  220. $output .= $this->delimiter;
  221. }
  222. //Link tag attributes
  223. $attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
  224. $attributes .= !empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
  225. $attributes .= !empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
  226. $attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';
  227. //Add to the HTML output
  228. $isCurrent = false;
  229. foreach ($classes as $class) {
  230. if ($class == 'current-menu-item') {
  231. $isCurrent = true;
  232. break;
  233. }
  234. }
  235. '<a href="/">Home</a>&nbsp;&nbsp;/&nbsp;&nbsp;<a href="./">Parent page</a>&nbsp;&nbsp;/&nbsp;&nbsp;<span>Current page</span>';
  236. if ($isCurrent) {
  237. $output .= '<span>' . $title . '</span>';
  238. } else {
  239. $output .= '<a' . $attributes . '>' . $title . '</a>&nbsp;&nbsp;/&nbsp;&nbsp;';
  240. }
  241. }
  242. }
  243. }