Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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