You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

925 lines
31 KiB

  1. <?php
  2. /*
  3. This file is part of sandbox.
  4. sandbox is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
  5. sandbox is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  6. You should have received a copy of the GNU General Public License along with sandbox. If not, see http://www.gnu.org/licenses/.
  7. */
  8. /**
  9. * Define content width
  10. * ====================
  11. */
  12. if ( !isset( $content_width ) ) {
  13. $content_width = 1359;
  14. }
  15. /**
  16. * Ajax hooks
  17. * ==========
  18. */
  19. /**
  20. * Theme setup hook
  21. * ================
  22. */
  23. add_action('after_setup_theme', 'sandbox_setup_theme');
  24. function sandbox_setup_theme() {
  25. // Translate, if applicable
  26. load_theme_textdomain('sandbox', get_template_directory() . '/translations');
  27. /**
  28. * Theme supports
  29. */
  30. add_theme_support('post-thumbnails');
  31. add_theme_support('custom-background');
  32. add_theme_support('html5', array('comment-list', 'comment-form', 'search-form', 'gallery', 'caption') );
  33. add_theme_support('custom-header');
  34. add_theme_support('automatic-feed-links');
  35. /*
  36. * Let WordPress manage the document title.
  37. * By adding theme support, we declare that this theme does not use a
  38. * hard-coded <title> tag in the document head, and expect WordPress to
  39. * provide it for us.
  40. */
  41. add_theme_support('title-tag');
  42. /**
  43. * Theme actions
  44. */
  45. // manage redirects
  46. // add_action('template_redirect', 'sandbox_template_redirect');
  47. // load scripts
  48. add_action('wp_enqueue_scripts', 'sandbox_enqueue_scripts');
  49. // init sidebars
  50. add_action('init', 'sandbox_sidebars_init');
  51. /**
  52. * Theme filters
  53. */
  54. // add shortcodes to widgets
  55. add_filter('widget_text', 'do_shortcode');
  56. add_filter('widget_title', 'do_shortcode');
  57. // add shortcodes to the excerpt
  58. add_filter('the_excerpt', 'shortcode_unautop');
  59. add_filter('the_excerpt', 'do_shortcode');
  60. // Adds filters for the description/meta content in archives.php
  61. add_filter('archive_meta', 'wptexturize');
  62. add_filter('archive_meta', 'convert_smilies');
  63. add_filter('archive_meta', 'convert_chars');
  64. add_filter('archive_meta', 'wpautop');
  65. // remove style attribute from cancel_comment_reply_link
  66. add_filter('cancel_comment_reply_link', 'sandbox_cancel_comment_reply_link', 10, 3);
  67. // filters the Categories archive widget to include the post count inside the link
  68. // https://www.sodawebmedia.com/blog/display-post-counts-within-link-for-categories-and-archive/
  69. add_filter('wp_list_categories', 'sandbox_count_span');
  70. // filters the Archive widget to include the post count inside the link
  71. add_filter('get_archives_link', 'sandbox_count_span');
  72. }
  73. /*
  74. * Setup hooks
  75. * ===========
  76. */
  77. function sandbox_template_redirect(){
  78. // hide profile pages from not logged in users
  79. $profile_page_ID = array(5142, 5480);
  80. if ( !is_user_logged_in() && ( sandbox_is_page_or_descendant($profile_page_ID[0]) || sandbox_is_page_or_descendant($profile_page_ID[1]) ) ) {
  81. wp_redirect(home_url());
  82. die;
  83. }
  84. }
  85. // load scripts
  86. function sandbox_enqueue_scripts(){
  87. $profile_post_ID = array(5142, 5480); // on profile page or descendants
  88. $post_id = get_the_ID();
  89. $post_type = get_post_type();
  90. $post_type_object = get_post_type_object($post_type);
  91. $post_type_rest_base = $post_type;
  92. if ( property_exists($post_type_object, 'rest_base') ) {
  93. $post_type_rest_base = $post_type_object->rest_base;
  94. }
  95. if ( !is_admin() ) {
  96. // jquery plugins
  97. wp_enqueue_script('jquery-plugins', get_template_directory_uri() . '/js/jquery.plugins.js', array('jquery'));
  98. // wp_enqueue_script('jquery-ui-css', get_template_directory_uri() . '/css/jquery-ui.theme.min.css');
  99. // comments reply script
  100. if ( is_singular() && comments_open() && (get_option('thread_comments') == 1 ) ) {
  101. wp_enqueue_script('comment-reply');
  102. }
  103. // Javascript data for window.sandboxTheme_data
  104. $data = array(
  105. 'id' => $post_id,
  106. 'urls' => array(
  107. 'ajax' => admin_url('admin-ajax.php'),
  108. 'template' => get_stylesheet_directory_uri(),
  109. 'rest' => rest_url('wp/v2/' . $post_type_rest_base . '/' . $post_id . '/'),
  110. ),
  111. 'verify' => wp_create_nonce('sandbox_ajax_call'),
  112. 'name' => sandbox_get_the_slug(),
  113. 'categories' => get_the_category(),
  114. // 'terms' => get_the_terms(),
  115. 'meta' => sandbox_get_post_meta(),
  116. 'info' => array(
  117. 'is_home' => is_home(),
  118. 'is_front_page' => is_front_page(),
  119. 'is_category' => is_category(),
  120. 'is_archive' => is_archive(),
  121. 'is_admin' => is_admin(),
  122. 'is_single' => is_single(),
  123. 'comments_open' => comments_open(),
  124. 'is_page' => is_page(),
  125. 'is_tag' => is_tag(),
  126. 'is_tax' => is_tax(),
  127. 'is_author' => is_author(),
  128. 'is_search' => is_search(),
  129. 'is_singular' => is_singular(),
  130. 'is_user_logged_in' => is_user_logged_in(),
  131. 'post_type' => $post_type,
  132. ),
  133. );
  134. // custom script: edit there for your own needs
  135. wp_register_script('actions', get_template_directory_uri() . '/js/actions.js', false, '7', true);
  136. // Javascript data for window.sandboxTheme_data
  137. wp_localize_script('actions', 'sandboxTheme_data', $data );
  138. wp_enqueue_script('actions');
  139. // custom script: edit there for your own needs
  140. }
  141. if ( is_user_logged_in() && ( is_page($profile_post_ID[0]) || is_page($profile_post_ID[1]) ) ) {
  142. wp_enqueue_script('wp-util');
  143. wp_enqueue_script('password-strength-meter');
  144. }
  145. }
  146. // init sidebars
  147. function sandbox_sidebars_init() {
  148. if ( !function_exists('register_sidebar') ) {
  149. return;
  150. }
  151. // enter your sidebars here
  152. $sidebars = array('header', 'menu', 'highlight', 'content-before', 'content-after', 'content-footer', 'footer', 'footer-bar');
  153. foreach ( $sidebars as $sidebar ) {
  154. $config = array(
  155. 'name' => $sidebar,
  156. 'id' => $sidebar,
  157. 'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="inner">',
  158. 'after_widget' => '</div></div>',
  159. 'before_title' => '<h3 class="title">',
  160. 'after_title' => '</h3>'
  161. );
  162. // Special markup for some sidebars
  163. if ( $sidebar === 'menu') {
  164. $config['before_widget'] = '<nav id="%1$s" class="widget %2$s">';
  165. $config['after_widget'] = '</nav>';
  166. }
  167. else if ( $sidebar === 'footer') {
  168. $config['before_widget'] = '<nav id="%1$s" class="widget clearfix %2$s">';
  169. $config['after_widget'] = '</nav>';
  170. }
  171. else if ( $sidebar === 'header') {
  172. $config['before_widget'] = '<div id="%1$s" class="widget %2$s">';
  173. $config['after_widget'] = '</div>';
  174. }
  175. else if ( $sidebar === 'content-footer') {
  176. $config['before_widget'] = '<div id="%1$s" class="widget %2$s site-container"><div class="inner">';
  177. $config['after_widget'] = '</div></div>';
  178. }
  179. else if ( $sidebar === 'content-after') {
  180. $config['before_widget'] = '<div id="%1$s" class="widget %2$s"><div class="inner clearfix">';
  181. $config['after_widget'] = '</div></div>';
  182. }
  183. register_sidebar($config);
  184. }
  185. }
  186. // filters to include the post count inside any link
  187. function sandbox_count_span($output){
  188. $output = str_replace('</a> (', ' (', $output);
  189. $output = str_replace(')', ')</a>', $output);
  190. return $output;
  191. }
  192. /**
  193. * Template helper functions
  194. * =========================
  195. */
  196. // is a given page ID the anchestor of the current page?
  197. function sandbox_is_page_or_descendant($post_ID){
  198. $is_page_or_descendant = false;
  199. if ( is_page($post_ID) ) {
  200. $is_page_or_descendant = true;
  201. } elseif ( is_post_type_hierarchical(get_post_type()) ) {
  202. $post = get_post();
  203. $ancestors = get_post_ancestors($post->ID);
  204. if ( in_array($post_ID, $ancestors) ) {
  205. $is_page_or_descendant = true;
  206. }
  207. }
  208. return $is_page_or_descendant;
  209. }
  210. // Page slug
  211. function sandbox_get_the_slug(){
  212. $slug = basename(get_permalink());
  213. do_action('before_slug', $slug);
  214. $slug = apply_filters('slug_filter', $slug);
  215. do_action('after_slug', $slug);
  216. return $slug;
  217. }
  218. function sandbox_the_slug(){
  219. echo sandbox_get_the_slug();
  220. }
  221. // Page description
  222. function sandbox_get_page_description($sep, $num_words, $meta = true){
  223. if ( !is_string($sep) || empty($sep) ) { $sep = '-'; }
  224. if ( !is_numeric($num_words) || empty($num_words) ) { $num_words = 40; }
  225. $description = wp_title($sep, false);
  226. $sep = ' ' . $sep . ' ';
  227. if ( is_single() || is_page() || ( is_front_page() && !is_home() ) ) {
  228. $post = get_post(get_the_ID());
  229. if ( has_excerpt() ) {
  230. $description = $post->post_excerpt;
  231. } else {
  232. $description = $post->post_content;
  233. }
  234. } else {
  235. if ( is_archive() || is_home() ) {
  236. if ( is_tax() || is_tag() || is_category() || is_author() ) {
  237. if ( is_tax() || is_tag() || is_category() ) {
  238. $termTitle = single_term_title('', false);
  239. $termDesc = term_description();
  240. $taxonomyTitle = get_taxonomy(get_queried_object()->taxonomy)->labels->singular_name;
  241. }
  242. if ( is_author() ) {
  243. $termTitle = get_the_author_meta('display_name');
  244. $termDesc = get_the_author_meta('description');
  245. $taxonomyTitle = __('Author', 'sandbox');
  246. }
  247. if ( !empty($termDesc) ) {
  248. $termDesc = $sep . $termDesc;
  249. }
  250. $description = $taxonomyTitle . ' ' . __('archive', 'sandbox') . $sep . $termTitle . $termDesc ;
  251. }
  252. if ( is_home() && ( $posts_page = get_option('page_for_posts') ) ) {
  253. $description = get_the_title($posts_page);
  254. } else{
  255. $description = get_option('blogname');
  256. }
  257. if ( is_search() ) {
  258. $description = __('Search Results For', 'sandbox') . ' ' . get_search_query();
  259. }
  260. }
  261. $description .= $sep . get_bloginfo('description');
  262. }
  263. return wp_trim_words(filter_var($meta, FILTER_VALIDATE_BOOLEAN) ? esc_html(wp_strip_all_tags($description, true)) : $description, $num_words);
  264. }
  265. function sandbox_the_page_description($sep = '-', $num_words = 40, $meta = true){
  266. echo sandbox_get_page_description($sep, $num_words, $meta);
  267. }
  268. // Page thumbnail
  269. function sandbox_get_page_thumbnail(){
  270. $page_thumbnail = get_theme_mod('sandbox_logo');
  271. if ( is_single() && has_post_thumbnail() ) {
  272. $postThumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(),'large', false);
  273. $page_thumbnail = $postThumbnail[0];
  274. } else if ( !empty($page_thumbnail) ) {
  275. $page_thumbnail = esc_url($page_thumbnail);
  276. } else {
  277. $page_thumbnail = esc_url(get_template_directory_uri()) . '/img/logo-basisbildung.png';
  278. }
  279. return $page_thumbnail;
  280. }
  281. function sandbox_the_page_thumbnail(){
  282. echo sandbox_get_page_thumbnail();
  283. }
  284. // Current page URI
  285. function sandbox_get_current_URI(){
  286. $protocol = ( ( !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443 ) ? 'https://' : 'http://';
  287. return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  288. }
  289. function sandbox_the_current_URI(){
  290. echo sandbox_get_current_URI();
  291. }
  292. // Post meta single output for all fields
  293. function sandbox_get_post_meta($post_id=0){
  294. $numeric_fields = array('_alpha_file_count', '_favorites', '_wpcr_rating', '_thumbnail_id');
  295. $serialized_fields = array('_more-link', '_alpha_file_options');
  296. $post_id = absint($post_id);
  297. if ( !$post_id ) {
  298. $post_id = get_the_ID();
  299. }
  300. $post_custom = get_post_custom($post_id);
  301. $post_meta_keys = get_post_custom_keys($post_id);
  302. $post_meta_keys_old = array_keys($post_custom);
  303. // echo var_dump($post_meta_keys_old);
  304. $post_meta = array();
  305. foreach ( $post_meta_keys as $post_meta_key ) {
  306. if ( !empty($post_custom[$post_meta_key][0]) ) {
  307. $post_meta[$post_meta_key] = $post_custom[$post_meta_key][0];
  308. // exeptions
  309. // numeric field type
  310. if ( in_array($post_meta_key, $numeric_fields) ) {
  311. $post_meta[$post_meta_key] = intval($post_custom[$post_meta_key][0]);
  312. }
  313. // serialized field type
  314. if ( in_array($post_meta_key, $serialized_fields) ) {
  315. $post_meta[$post_meta_key] = unserialize($post_custom[$post_meta_key][0]);
  316. }
  317. } else {
  318. $post_meta[$post_meta_key] = '';
  319. // exeptions
  320. // numeric field type
  321. if ( in_array($post_meta_key, $numeric_fields) ) {
  322. $post_meta[$post_meta_key] = 0;
  323. }
  324. // serialized field type
  325. if ( in_array($post_meta_key, $serialized_fields) ) {
  326. $post_meta[$post_meta_key] = array();
  327. }
  328. }
  329. }
  330. return $post_meta;
  331. }
  332. // Get the first tags title
  333. function sandbox_get_the_tag_title(){
  334. $terms = get_the_terms(0, 'post_tag');
  335. if ( is_wp_error( $terms ) ){ return $terms; }
  336. if ( empty( $terms ) ){ return false; }
  337. foreach ( $terms as $term ) {
  338. $term_names[] = $term->name;
  339. }
  340. return $term_names[0];
  341. }
  342. function sandbox_the_tag_title(){
  343. echo sandbox_get_the_tag_title();
  344. }
  345. // Get the first tags slug
  346. function sandbox_get_the_tag_slug(){
  347. $terms = get_the_terms(0, 'post_tag');
  348. if ( is_wp_error( $terms ) ){ return $terms; }
  349. if ( empty( $terms ) ){ return false; }
  350. foreach ( $terms as $term ) {
  351. if ( $term->slug != 'kolumne') {
  352. $term_slugs[] = $term->slug;
  353. }
  354. }
  355. return $term_slugs[0];
  356. }
  357. function sandbox_the_tag_slug(){
  358. echo sandbox_get_the_tag_slug();
  359. }
  360. // Get the first tags ID
  361. function sandbox_get_the_tag_id(){
  362. $terms = get_the_terms(0, 'post_tag');
  363. if ( is_wp_error($terms) ){ return $terms; }
  364. if ( empty($terms) ){ return false; }
  365. foreach ( $terms as $term ) {
  366. $term_ids[] = $term->term_id;
  367. }
  368. return $term_ids[0];
  369. }
  370. function sandbox_the_tag_id(){
  371. echo sandbox_get_the_tag_id();
  372. }
  373. // Output from +431234567891 to +43 1 234 56 78 - 91
  374. function sandbox_sanitize_phone($phone) {
  375. $data = array(
  376. substr($phone, 0,3),
  377. substr($phone, 3,1),
  378. substr($phone, 4,3),
  379. substr($phone, 7,2),
  380. substr($phone, 9,2),
  381. substr($phone, 11,2),
  382. );
  383. if ( !empty($data[5]) ) {
  384. $data[5] = ' - ' . $data[5];
  385. }
  386. return $data[0] . ' ' . $data[1] . ' ' . $data[2] . ' ' . $data[3] . ' ' . $data[4] . $data[5];
  387. }
  388. // Comment cancel reply link
  389. function sandbox_cancel_comment_reply_link($formatted_link, $link, $text){
  390. return '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '">' . $text . '</a>';
  391. }
  392. // Comment Item, callback for wp_list_comments()
  393. function sandbox_comment_item($comment, $args, $depth){
  394. if ( !isset($GLOBALS['comment']) ) {
  395. $GLOBALS['comment'] = $comment;
  396. }
  397. ?>
  398. <li id="comment-<?php comment_ID(); ?>" <?php comment_class( $comment->has_children ? 'parent' : '', $comment ); ?>>
  399. <div class="inner">
  400. <aside class="comment-meta">
  401. <ul class="inner">
  402. <?php if ( 0 != $args['avatar_size'] ) { ?>
  403. <li class="avatar">
  404. <?php echo get_avatar( $comment, $args['avatar_size'] ); ?>
  405. </li>
  406. <?php } ?>
  407. <li class="name">
  408. <?php echo get_comment_author_link( $comment ); ?>
  409. </li>
  410. <li class="date"><a href="<?php
  411. echo esc_url( get_comment_link( $comment, $args ) );
  412. ?>"><time datetime="<?php comment_time('c'); ?>"><?php
  413. /* translators: 1: comment date, 2: comment time */
  414. printf( __('%1$s at %2$s'), get_comment_date('', $comment ), get_comment_time() );
  415. ?></time></a></li>
  416. <?php if ( current_user_can('moderate_comments') ) { ?>
  417. <li class="edit"><?php edit_comment_link( __('Edit'), '<span class="edit-link">', '</span>'); ?></li>
  418. <?php } ?>
  419. <li class="reply"><?php
  420. comment_reply_link( array_merge( $args, array(
  421. 'add_below' => 'div-comment',
  422. 'depth' => $depth,
  423. 'max_depth' => $args['max_depth'],
  424. 'before' => '',
  425. 'after' => ''
  426. ) ) );
  427. ?></li>
  428. </ul>
  429. </aside>
  430. <div class="content">
  431. <div class="inner">
  432. <?php if ('0' == $comment->comment_approved ) : ?>
  433. <p class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.'); ?></p>
  434. <?php endif; ?>
  435. <?php echo apply_filters('comment_text', $comment->comment_content, $comment, $args); ?>
  436. </div>
  437. </div>
  438. </div>
  439. <?php
  440. }
  441. function sandbox_user_comments_item($comment, $args, $depth){
  442. if ( !isset($GLOBALS['comment']) ) {
  443. $GLOBALS['comment'] = $comment;
  444. }
  445. ?>
  446. <li id="comment-<?php comment_ID(); ?>" <?php comment_class( $comment->has_children ? 'parent' : '', $comment ); ?>>
  447. <div class="inner">
  448. <aside class="comment-meta">
  449. <ul class="inner">
  450. <?php if ( 0 != $args['avatar_size'] ) { ?>
  451. <li class="avatar">
  452. <?php echo get_avatar( $comment, $args['avatar_size'] ); ?>
  453. </li>
  454. <?php } ?>
  455. <li class="name">
  456. <?php echo get_comment_author_link( $comment ); ?>
  457. </li>
  458. <li class="date"><a href="<?php
  459. echo esc_url( get_comment_link( $comment, $args ) );
  460. ?>"><time datetime="<?php comment_time('c'); ?>"><?php
  461. /* translators: 1: comment date, 2: comment time */
  462. printf( __('%1$s at %2$s'), get_comment_date('', $comment ), get_comment_time() );
  463. ?></time></a></li>
  464. <?php if ( current_user_can('moderate_comments') ) { ?>
  465. <li class="edit"><?php edit_comment_link( __('Edit'), '<span class="edit-link">', '</span>'); ?></li>
  466. <?php } ?>
  467. <li class="reply"><?php
  468. comment_reply_link( array_merge( $args, array(
  469. 'add_below' => 'div-comment',
  470. 'depth' => $depth,
  471. 'max_depth' => $args['max_depth'],
  472. 'before' => '',
  473. 'after' => ''
  474. ) ) );
  475. ?></li>
  476. </ul>
  477. </aside>
  478. <div class="content">
  479. <div class="inner">
  480. <?php if ('0' == $comment->comment_approved ) : ?>
  481. <p class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.'); ?></p>
  482. <?php endif; ?>
  483. <?php echo apply_filters('comment_text', $comment->comment_content, $comment, $args); ?>
  484. </div>
  485. </div>
  486. </div>
  487. <?php
  488. }
  489. // Comments Link
  490. function sandbox_get_the_comments_link(){ // use this function only inside the loop.
  491. $comments_count = get_comments_number();
  492. $commentsListId = 'comments-list';
  493. $commentsFormId = 'response';
  494. if ( $comments_count == 0 ) {
  495. $commentLink = '<a href="#' . $commentsFormId . '">' . $comments_count . ' ' . __('Be the first to Comment', 'sandbox') . '</a>'; // 'Schreib den ersten Kommentar';
  496. } else if ( $comments_count > 1 ) {
  497. $commentLink = '<a href="#' . $commentsListId . '">' . $comments_count . ' ' . __('Comments', 'sandbox') . '</a>'; // 'Kommentare';
  498. } else {
  499. $commentLink = '<a href="#' . $commentsListId . '">' . $comments_count . ' ' . __('Comment', 'sandbox') . '</a>'; // 'Kommentar';
  500. }
  501. return $commentLink;
  502. }
  503. function sandbox_the_comments_link(){ // use this function only inside the loop.
  504. echo sandbox_get_the_comments_link();
  505. }
  506. // measure the approximately read-time of an article
  507. function sandbox_read_time($text){
  508. $words = str_word_count(wp_strip_all_tags($text));
  509. if ( !empty($words) ) {
  510. $time_in_minutes = ceil($words/200);
  511. return $time_in_minutes;
  512. }
  513. return false;
  514. }
  515. // Collect term data for any kind of archive
  516. function sandbox_get_archive_term(){
  517. $queried_object = get_queried_object();
  518. $term = new stdClass();
  519. $term->classes = '';
  520. $term->description = get_the_archive_description();
  521. $term->taxonomy = array('title' => '', 'WP_Taxonomy' => '');
  522. $term->post_type = array('title' => '', 'WP_Post_Type' => '');
  523. $term->post_types = array();
  524. $paged = get_query_var('paged');
  525. $max_num_pages = get_query_var('max_num_pages');
  526. // echo var_dump($max_num_pages);
  527. if ( is_post_type_archive() ) {
  528. if ( empty($term->description) ) {
  529. $term->description = $queried_object->description;
  530. }
  531. // custom long description
  532. $long_description = '';
  533. if ( function_exists('ptad_get_post_type_description') ) {
  534. $long_description = ptad_get_post_type_description($queried_object->name);
  535. }
  536. if ( !empty($long_description) ) {
  537. $term->description = $long_description;
  538. }
  539. $term->title = $queried_object->label;
  540. $term->thumbnail = esc_url(get_template_directory_uri()) . '/img/' . $queried_object->name . '.jpg';
  541. // $term->thumbnail = '';
  542. // $term->byline = __('All articles in', 'sandbox') . ' ' . $term->title;
  543. $term->count = wp_count_posts($queried_object->name)->publish; //$queried_object->count;
  544. $term->byline = $queried_object->labels->name;
  545. $term->post_type['WP_Post_Type'] = $queried_object;
  546. $term->post_types[] = $queried_object->name;
  547. }
  548. if ( is_tax() || is_tag() || is_category() ) {
  549. $queried_taxonomy = get_taxonomy($queried_object->taxonomy);
  550. $term->term_id = $queried_object->term_id;
  551. $term_meta = get_term_meta($term->term_id);
  552. $term->title = single_term_title('', false);
  553. $term->thumbnail = '';
  554. $term->thumbnail_id = isset($term_meta['_term_image']) && !empty($term_meta['_term_image'][0]) ? intval($term_meta['_term_image'][0]) : 0;
  555. if ( $term->thumbnail_id !== 0 ) {
  556. $term->thumbnail = wp_get_attachment_image($term->thumbnail_id, 'sandbox-hero');
  557. }
  558. $term->taxonomy['title'] = $queried_taxonomy->labels->singular_name;
  559. $term->taxonomy['WP_Taxonomy'] = $queried_taxonomy;
  560. $term->byline = $term->taxonomy['title'] . ' - ' . $term->title;
  561. $term->count = $queried_object->count;
  562. if ( !empty($queried_taxonomy->object_type) ) {
  563. $post_type_names = $queried_taxonomy->object_type;
  564. $term->post_type = array();
  565. foreach ( $post_type_names as $post_type_name ) {
  566. $post_type_object = get_post_type_object($post_type_name);
  567. $term->post_type[] = array(
  568. 'title' => $post_type_object->label,
  569. 'WP_Post_Type' => $post_type_object,
  570. );
  571. }
  572. $term->post_types = $post_type_names;
  573. }
  574. }
  575. if ( is_author() ) {
  576. $term->title = get_the_author_meta('display_name');
  577. $term->thumbnail = get_avatar($queried_object->data->ID, 'smr-thumb');
  578. $term->taxonomy['title'] = __('Author', 'sandbox');
  579. $term->byline = $term->title;
  580. $term->count = count_user_posts($queried_object->data->ID);
  581. }
  582. if ( is_date() ) {
  583. global $wp_query;
  584. $term->title = __('All articles from', 'sandbox') . ' ';
  585. $term->count = $wp_query->found_posts;
  586. if ( is_day() ) {
  587. $term->title .= get_the_time(get_option('date_format'));
  588. } elseif ( is_month() ) {
  589. $term->title .= get_the_time('F Y');
  590. } elseif ( is_year() ) {
  591. $term->title .= get_the_time('Y');
  592. }
  593. }
  594. if ( is_home() ) {
  595. global $wp_query;
  596. $posts_page_id = get_option('page_for_posts');
  597. $term->post_type['WP_Post_Type'] = get_post_type_object('post');
  598. $term->post_type['title'] =
  599. $term->description = get_the_content($posts_page);
  600. $term->title = get_the_title($posts_page);
  601. $term->byline = $term->title;
  602. $term->count = $wp_query->found_posts;
  603. }
  604. // Add a page number if necessary.
  605. $posts_per_page = get_query_var('posts_per_page');
  606. $all_pages = ceil($term->count/$posts_per_page);
  607. // echo 'blah: ' . var_dump($term->count);
  608. // echo 'blah: ' . var_dump($all_pages);
  609. if ( $paged > 1 && !is_404() ) {
  610. /* translators: %s: Page number. */
  611. $current_page = sprintf(__('Page %s'), $paged);
  612. $of = ' ' . __('von', 'sandbox') . ' ';
  613. $term->byline .= ' ' . $current_page . $of . $all_pages;
  614. } else if ( $term->count > $posts_per_page ) {
  615. $all_pages = ceil($term->count/$posts_per_page);
  616. $of = ' ' . __('von', 'sandbox') . ' ';
  617. $term->byline .= ' ' . __('Seite 1') . $of . $all_pages;
  618. }
  619. if ( !empty($term->taxonomy['title']) ) {
  620. $term->taxonomy['title'] .= ' - ';
  621. }
  622. $term->has_description = false;
  623. if ( !empty($term->description) ) {
  624. $term->classes .= ' has-description';
  625. $term->description = wpautop($term->description);
  626. $term->has_description = true;
  627. }
  628. $term->has_thumbnail = false;
  629. if ( !empty($term->thumbnail) ) {
  630. $term->classes .= ' has-thumbnail';
  631. $term->has_thumbnail = true;
  632. }
  633. $term->has_meta = false;
  634. if ( $term->has_description || $term->has_thumbnail || is_a($term->taxonomy['WP_Taxonomy'], 'WP_Taxonomy') ) {
  635. $term->classes .= ' has-meta';
  636. $term->has_meta = true;
  637. }
  638. $term->has_byline = false;
  639. if ( !empty($term->byline) ) {
  640. // only when meta is present
  641. if ( $term->has_meta ) {
  642. $term->classes .= ' has-byline';
  643. }
  644. $term->has_byline = true;
  645. }
  646. if ( !empty($term->classes) ) {
  647. $term->classes = ' ' . $term->classes;
  648. }
  649. // echo '<!-- ', var_dump($wp_query->query_vars), ' -->';
  650. return $term;
  651. }
  652. // Count amount of pages in an archive
  653. function sandbox_count_archive_pages($term = NULL){
  654. if ( !is_archive() ) { return NULL; }
  655. if ( $term === NULL ) { $term = sandbox_get_archive_term(); }
  656. $posts_per_page = get_option('posts_per_page');
  657. return ceil($term->count/$posts_per_page);
  658. }
  659. // Detect if an archive page is paged
  660. function sandbox_archive_is_paged($term = NULL){
  661. if ( !is_archive() ) { return NULL; }
  662. if ( $term === NULL ) { $term = sandbox_get_archive_term(); }
  663. return sandbox_count_archive_pages($term) > 1;
  664. }
  665. function sandbox_get_more_url($post_type, $post_meta){
  666. $more_url = get_permalink();
  667. if ( ( isset($post_meta["_more-link-url"]) && !empty($post_meta["_more-link-url"]) ) || ( isset($post_meta['_more-link']) && !empty($post_meta['_more-link']) ) ) {
  668. // "download_aktion_de" uses that field for download button
  669. if ( $post_type !== 'download_aktion_de' ) {
  670. if ( !empty($post_meta["_more-link-url"]) ) {
  671. $more_url = $post_meta["_more-link-url"];
  672. } elseif ( isset($post_meta['_more-link'][0]) && isset($post_meta['_more-link'][0]['_more-link-url']) && !empty($post_meta['_more-link'][0]['_more-link-url']) ) {
  673. $more_url = $post_meta['_more-link'][0]['_more-link-url'];
  674. }
  675. }
  676. }
  677. return $more_url;
  678. }
  679. function sandbox_get_more_text($post_type, $post_meta){
  680. // Text for "Read more" button
  681. $more_texts = array(
  682. 'default' => __('Read more', 'sandbox'),
  683. 'download' => __('Download', 'sandbox'),
  684. 'alpha_download' => array(
  685. 'default' => __('More details', 'sandbox'),
  686. ),
  687. 'download_aktion_de' => array(
  688. 'default' => __('More details', 'sandbox'),
  689. ),
  690. );
  691. $more_text = $more_texts['default'];
  692. // post type handling
  693. if ( isset($more_texts[$post_type]) && isset($more_texts[$post_type]['default']) && !empty($more_texts[$post_type]['default']) ) {
  694. $more_text = $more_texts[$post_type]['default'];
  695. }
  696. // custom fields handling
  697. if ( ( isset($post_meta["_custom_more_text"]) && !empty($post_meta["_custom_more_text"]) ) || ( isset($post_meta['_more-link']) && !empty($post_meta['_more-link']) ) ) {
  698. if ( isset($post_meta["_custom_more_text"]) && !empty($post_meta["_custom_more_text"]) ) {
  699. $more_text = $post_meta["_custom_more_text"];
  700. } elseif ( isset($post_meta['_more-link'][0]) && isset($post_meta['_more-link'][0]['_more-link-text']) && !empty($post_meta['_more-link'][0]['_more-link-text']) ) {
  701. $more_text = $post_meta['_more-link'][0]['_more-link-text'];
  702. }
  703. }
  704. return $more_text;
  705. }
  706. // Get all taxonomies for a post and its terms
  707. // works like get_the_terms() but returns an array of all taxonomies that are registered to the post.
  708. function sandbox_get_the_taxonomies_and_terms($post_id=0){
  709. $post_id = absint($post_id);
  710. if ( !$post_id ) {
  711. $post_id = get_the_ID();
  712. }
  713. $post = get_post($post_id);
  714. $taxonomy_objects = get_object_taxonomies($post, 'objects');
  715. $taxonomies = array();
  716. $taxonomies_index = 0;
  717. foreach ( $taxonomy_objects as $taxonomy_object ) {
  718. $taxonomy_term_objects = get_the_terms($post_id, $taxonomy_object->name);
  719. if ( !empty($taxonomy_term_objects) && !is_wp_error($taxonomy_term_objects) ) {
  720. $taxonomies[$taxonomy_object->name] = array(
  721. 'slug' =>$taxonomy_object->name,
  722. 'label' =>$taxonomy_object->label,
  723. 'terms' => array(),
  724. );
  725. foreach ( $taxonomy_term_objects as $taxonomy_term_object ) {
  726. $taxonomies[$taxonomy_object->name]['terms'][$taxonomy_term_object->slug] = array(
  727. 'term_id' => $taxonomy_term_object->term_id,
  728. 'slug' => $taxonomy_term_object->slug,
  729. 'label' => $taxonomy_term_object->name,
  730. 'description' => $taxonomy_term_object->description,
  731. );
  732. }
  733. $taxonomies_index++;
  734. }
  735. }
  736. return $taxonomies;
  737. }
  738. function sandbox_get_the_terms(){
  739. $post_id = get_the_ID();
  740. $taxonomies = get_post_taxonomies($post_id);
  741. $terms = array();
  742. foreach ( $taxonomies as $taxonomy ) {
  743. $terms_data = wp_get_post_terms($post_id, $taxonomy);
  744. if ( !empty($terms_data) && !is_wp_error($terms_data) ) {
  745. $terms[$taxonomy] = $terms_data;
  746. }
  747. }
  748. return $terms;
  749. }
  750. // get meta data of an article
  751. function sandbox_get_meta_data( $force_meta = false ) {
  752. if ( !$force_meta ) { return; }
  753. $output = '';
  754. //Check for sticky
  755. if ( is_sticky() ) {
  756. $output .= '<div class="meta-stick">' . __('Sticky', 'sandbox') . '</div>';
  757. }
  758. //Check meta options
  759. $metas = is_array($force_meta) ? $force_meta : array( $force_meta );
  760. // echo '<!-- ', var_dump($metas), ' -->';
  761. if ( !empty( $metas ) ) {
  762. $post_id = get_the_ID();
  763. $post_type = get_post_type($post_id);
  764. $post_meta = get_post_custom();
  765. foreach ( $metas as $meta_id ) {
  766. $meta = '';
  767. $className = $meta_id;
  768. switch ( $meta_id ) {
  769. case 'format':
  770. $formats = get_the_terms($post_id, 'format');
  771. if ( !empty($formats) && ! is_wp_error($formats) ) {
  772. foreach ( $formats as $format ) {
  773. $className .= ' meta-' . $format->slug;
  774. $meta = $format->name;
  775. }
  776. }
  777. break;
  778. case 'date':
  779. $meta = get_the_date();
  780. break;
  781. case 'terms':
  782. $terms_in_taxes = sandbox_get_the_terms();
  783. $meta = '';
  784. if ( !empty($terms_in_taxes) ) {
  785. foreach ( $terms_in_taxes as $taxonomy_name => $terms ) {
  786. if ( !empty($terms) ) {
  787. $meta .= '<ul class="terms-list ' . $taxonomy_name . '-list">';
  788. foreach ( $terms as $term ) {
  789. if ( !empty($term) && is_a($term, 'WP_Term') ) {
  790. $meta .= '<li class="' . $term->slug . '"><a href="' . esc_url(get_term_link($term)) . '">' . $term->name . '</a></li>';
  791. }
  792. }
  793. $meta .= '</ul>';
  794. }
  795. }
  796. }
  797. break;
  798. case 'author':
  799. if ( $post_type === 'alpha_download' ) {
  800. $author = isset($post_meta['_download-author']) ? $post_meta['_download-author'][0] : '';
  801. $license = isset($post_meta['_download-license']) ? $post_meta['_download-license'][0] : '';
  802. if ( !empty($author) ) {
  803. $meta = $author . ( !empty($license) ? ' | ' . $license : '' );
  804. }
  805. } else {
  806. $author_id = get_post_field('post_author', $post_id);
  807. $meta = get_the_author_meta('display_name', $author_id);
  808. }
  809. break;
  810. case 'rtime':
  811. $meta = sandbox_read_time(get_the_content());
  812. if ( !empty( $meta ) ) {
  813. $meta = $meta . ' ' . __('min read', 'sandbox');
  814. }
  815. break;
  816. case 'comments':
  817. if ( comments_open() || get_comments_number() ) {
  818. ob_start();
  819. comments_popup_link( __('Add Comment', 'sandbox'), __('1 Comment', 'sandbox'), __('% Comments', 'sandbox') );
  820. $meta = ob_get_contents();
  821. ob_end_clean();
  822. } else {
  823. $meta = '';
  824. }
  825. break;
  826. default:
  827. break;
  828. }
  829. if ( !empty($meta) ) {
  830. $output .= "\r\n" . '<li class="' . $className . '">' . $meta . '</li>';
  831. }
  832. }
  833. }
  834. return $output;
  835. }
  836. // Remember: the sandbox is for play.
  837. ?>