您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

958 行
34 KiB

  1. <?php
  2. /**
  3. * Plugin name: sandbox adaptions
  4. * Plugin URI: https://www.netzgestaltung.at
  5. * Author: Thomas Fellinger
  6. * Author URI: https://www.netzgestaltung.at
  7. * Version: 0.1
  8. * Description: Custom website functions
  9. * License: GPL v2
  10. * Copyright 2020 Thomas Fellinger (email : office@netzgestaltung.at)
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or
  14. * (at your option) any later version.
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. define('SANDBOX_ADAPTIONS_PATH', plugin_dir_path(__FILE__));
  24. define('SANDBOX_ADAPTIONS_URL', plugin_dir_url(__FILE__));
  25. /**
  26. * Ajax hooks
  27. * ==========
  28. */
  29. // user edit ajax
  30. add_action('wp_ajax_user_edit', 'sandbox_ajax_user_edit');
  31. /**
  32. * Plugin setup hook
  33. * ================
  34. */
  35. add_action('plugins_loaded', 'sandbox_setup_site');
  36. function sandbox_setup_site() {
  37. add_post_type_support('page', 'excerpt');
  38. // init image sizes
  39. sandbox_image_sizes();
  40. // init custom widgets
  41. add_action('widgets_init', 'sandbox_widgets_init');
  42. add_action('init', 'sandbox_disable_emojis');
  43. // load admin styles
  44. add_action('admin_head', 'sandbox_admin_styles_load');
  45. // quick fix for https://wordpress.org/support/topic/jqeuery-error-live-is-not-a-function/
  46. add_action('admin_enqueue_scripts', 'sandbox_jcf_admin_fix');
  47. if ( !is_admin() ) {
  48. // add usefull classNames
  49. add_filter('body_class', 'sandbox_body_class');
  50. add_filter('post_class', 'sandbox_post_class');
  51. add_filter('nav_menu_css_class', 'sandbox_menu_item_class', 10, 2);
  52. add_filter('the_category', 'sandbox_category_class');
  53. // adds login and out links to menus "site-loggedin" and "site-loggedout"
  54. add_filter('wp_nav_menu_items', 'sandbox_nav_menu_items', 10, 2);
  55. // Custom login screen
  56. add_filter('login_headerurl', 'sandbox_login_headerurl');
  57. add_filter('login_headertext', 'sandbox_login_headertext');
  58. add_filter('login_enqueue_scripts', 'sandbox_login_enqueue_scripts');
  59. // Change Wordpress email subject
  60. add_filter('wp_mail_from_name', 'sandbox_mail_from_name');
  61. // remove lazy block wrapper container markup in frontend
  62. add_filter( 'lzb/block_render/allow_wrapper', '__return_false' );
  63. // Admin pages related stuff
  64. } else {
  65. // add usefull classNames
  66. add_filter('admin_body_class', 'sandbox_admin_body_class');
  67. }
  68. // plugin integrations
  69. add_filter('wpcf7_form_tag_data_option', 'sandbox_listo_ordered', 11, 1);
  70. // listo uses an iso3 country list
  71. // country iso2 to iso3:
  72. // https://github.com/i-rocky/country-list-js/blob/master/data/iso_alpha_3.json
  73. // all countries and phone-codes (iso2):
  74. // https://github.com/ChromaticHQ/intl-tel-input/blob/master/src/js/data.js
  75. // filter values of form tags to email
  76. add_filter( 'cf7sg_mailtag_email-updates', 'sandbox_cf7_mailtag_email_updates', 10, 3);
  77. // remove layout from "cf7 smart grid plugin"
  78. add_action('smart_grid_register_styles', 'sandbox_smart_grid_register_styles');
  79. // use really simple captcha in contact form 7
  80. // add_filter('wpcf7_use_really_simple_captcha', '__return_true');
  81. }
  82. // Check if function "wp_body_open" exits, if not create it
  83. if ( !function_exists('wp_body_open') ) {
  84. function wp_body_open() {
  85. do_action('wp_body_open');
  86. }
  87. }
  88. // add custom image sizes
  89. function sandbox_image_sizes() {
  90. $image_sizes = sandbox_get_image_sizes();
  91. if ( !empty( $image_sizes ) ) {
  92. foreach ( $image_sizes as $id => $size ) {
  93. add_image_size( $id, $size['args']['w'], $size['args']['h'], $size['args']['crop'] );
  94. }
  95. }
  96. }
  97. // custom image sizes settings
  98. function sandbox_get_image_sizes() {
  99. $sizes = array();
  100. $crop = true;
  101. $ratio = '16_9';
  102. $ratio_opts = explode("_", $ratio);
  103. // Standard (with sidebar)
  104. $width = 805;
  105. $height = absint($width*$ratio_opts[1]/$ratio_opts[0]);
  106. $sizes['sandbox-default'] = array('title' => __('Standard (with sidebar)', 'sandbox'), 'args' => array('w' => $width, 'h' => $height, 'crop' => $crop));
  107. //Full width (no sidebar)
  108. $width = 1090;
  109. $height = absint($width*$ratio_opts[1]/$ratio_opts[0]);
  110. $sizes['sandbox-wide'] = array('title' => __('Full Width (no sidebar)', 'sandbox'), 'args' => array('w' => $width, 'h' => $height, 'crop' => $crop));
  111. //Hero image
  112. $width = 1920;
  113. $height = absint($width*$ratio_opts[1]/$ratio_opts[0]);
  114. $sizes['sandbox-hero'] = array('title' => __('Hero image', 'sandbox'), 'args' => array('w' => $width, 'h' => $height, 'crop' => $crop));
  115. return $sizes;
  116. }
  117. // Custom login screen - home url
  118. function sandbox_login_headerurl(){
  119. return home_url();
  120. }
  121. // Custom login screen - header text
  122. function sandbox_login_headertext(){
  123. return wp_get_document_title();
  124. }
  125. // Custom login screen - login style
  126. function sandbox_login_enqueue_scripts(){
  127. // loads style/images from template folder
  128. wp_register_style('sandbox_login', get_template_directory_uri() . '/css/login.css');
  129. wp_enqueue_style('sandbox_login');
  130. }
  131. // init custom widgets
  132. function sandbox_widgets_init(){;
  133. register_widget('sandbox_faq_nav_widget');
  134. // register_widget('sandbox_events_widget');
  135. // register_widget('sandbox_social_widget');
  136. // register_widget('sandbox_slider_widget');
  137. }
  138. /**
  139. * Disable the emoji's
  140. * https://kinsta.com/knowledgebase/disable-emojis-wordpress/
  141. */
  142. function sandbox_disable_emojis(){
  143. remove_action('wp_head', 'print_emoji_detection_script', 7 );
  144. remove_action('admin_print_scripts', 'print_emoji_detection_script');
  145. remove_action('wp_print_styles', 'print_emoji_styles');
  146. remove_action('admin_print_styles', 'print_emoji_styles');
  147. remove_filter('the_content_feed', 'wp_staticize_emoji');
  148. remove_filter('comment_text_rss', 'wp_staticize_emoji');
  149. remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
  150. add_filter('tiny_mce_plugins', 'disable_emojis_tinymce');
  151. add_filter('wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 );
  152. }
  153. /**
  154. * Filter function used to remove the tinymce emoji plugin.
  155. *
  156. * @param array $plugins
  157. * @return array Difference betwen the two arrays
  158. */
  159. function disable_emojis_tinymce($plugins) {
  160. if ( is_array($plugins) ) {
  161. return array_diff($plugins, array('wpemoji'));
  162. } else {
  163. return array();
  164. }
  165. }
  166. /**
  167. * Remove emoji CDN hostname from DNS prefetching hints.
  168. *
  169. * @param array $urls URLs to print for resource hints.
  170. * @param string $relation_type The relation type the URLs are printed for.
  171. * @return array Difference betwen the two arrays.
  172. */
  173. function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
  174. if ( 'dns-prefetch' == $relation_type ) {
  175. /** This filter is documented in wp-includes/formatting.php */
  176. $emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/');
  177. $urls = array_diff( $urls, array( $emoji_svg_url ) );
  178. }
  179. return $urls;
  180. }
  181. // adds usefull body classNames
  182. // @param $classes array
  183. function sandbox_body_class($classes){
  184. $classNames = array();
  185. $post = get_post();
  186. $parents = get_post_ancestors($post);
  187. $slug = sandbox_get_the_slug();
  188. $post_type = get_post_type();
  189. $classNames[] = $post_type . '-' . $slug;
  190. if ( is_home() ) {
  191. $classNames[] = 'archive';
  192. }
  193. if ( is_active_sidebar('content-before') && is_active_sidebar('content-after') ) {
  194. $classNames[] = 'has-content-sidebars';
  195. } else if ( is_active_sidebar('content-before') || is_active_sidebar('content-after') ) {
  196. $classNames[] = 'has-content-sidebar';
  197. if ( is_active_sidebar('content-before') ) {
  198. $classNames[] = 'has-sidebar-content-before';
  199. } else if ( is_active_sidebar('content-after') ) {
  200. $classNames[] = 'has-sidebar-content-after';
  201. }
  202. }
  203. /* use for parent pages as sections
  204. if ( !empty($parents) ) {
  205. $parents[] = $post->ID;
  206. foreach ( $parents as $parentID ) {
  207. if ( $parentID === 8 ) {
  208. $classNames[] = 'section-you-name-it';
  209. }
  210. }
  211. }
  212. */
  213. // echo var_dump($classes);
  214. return array_merge( $classes, $classNames );
  215. }
  216. // add usefull classNames
  217. // @param $classes string
  218. function sandbox_admin_body_class($classes){
  219. $classNames = '';
  220. $screen = get_current_screen();
  221. if ( $screen->base === 'post' && $screen->post_type === 'page' ) {
  222. $page_template = get_post_meta(get_the_ID(), '_wp_page_template', true);
  223. $classNames .= ' used-template-' . esc_attr(str_replace('.php', '', $page_template));
  224. }
  225. return $classes . ' ' . $classNames;
  226. }
  227. // add custom styles for admin section
  228. function sandbox_admin_styles_load(){
  229. wp_register_style('sandbox_admin', SANDBOX_ADAPTIONS_URL . '/admin/style.css');
  230. wp_enqueue_style('sandbox_admin');
  231. }
  232. function sandbox_jcf_admin_fix(){
  233. $screen = get_current_screen();
  234. if ( in_array($screen->id, array('settings_page_jcf_admin', 'settings_page_jcf_fieldset_index')) ) {
  235. wp_enqueue_script('jquery-migrate');
  236. }
  237. }
  238. // adds usefull post classNames
  239. function sandbox_post_class($classes){
  240. $classNames = array();
  241. if ( !has_post_thumbnail() ) {
  242. $classNames[] = 'no-post-thumbnail';
  243. }
  244. return array_merge($classes, $classNames);
  245. }
  246. // adds usefull menu-item classNames
  247. function sandbox_menu_item_class($classes, $item){
  248. // Add slugs to menu-items
  249. if ('category' == $item->object ) {
  250. $category = get_category( $item->object_id );
  251. $classes[] = 'category-' . $category->slug;
  252. } else if ('format' == $item->object ){
  253. $format = get_term($item->object_id);
  254. $classes[] = 'format-' . $format->slug;
  255. }
  256. return $classes;
  257. }
  258. // adds usefull category classNames
  259. function sandbox_category_class($thelist){
  260. $categories = get_the_category();
  261. if ( !$categories || is_wp_error($categories) ) {
  262. return $thelist;
  263. }
  264. $output = '<ul class="post-categories">';
  265. foreach ( $categories as $category ) {
  266. $output .= '<li class="category-' . $category->slug . '"><a href="' . esc_url(get_category_link($category->term_id)) . '">' . $category->name . '</a></li>';
  267. }
  268. $output .= '</ul>';
  269. return $output;
  270. }
  271. // adds login and out links to menus "site-loggedin" and "site-loggedout"
  272. function sandbox_nav_menu_items($items, $args){
  273. if ( is_user_logged_in() ) {
  274. if ( $args->menu->slug === 'site-loggedin' ) {
  275. // add logout link to the end of the menu
  276. $logout_class = 'menu-item-logout menu-item menu-item-type-custom menu-item-object-custom';
  277. $items .= '<li class="' . $logout_class . '">' . wp_loginout(get_permalink(), false) . '</li>';
  278. }
  279. } elseif ( $args->menu->slug === 'site-loggedout' ) {
  280. // add login link to the begin of the menu
  281. $login_class = 'menu-item-login menu-item menu-item-type-custom menu-item-object-custom';
  282. $items = '<li class="' . $login_class . '">' . wp_loginout(get_permalink(), false) . '</li>' . $items;
  283. }
  284. return $items;
  285. }
  286. /**
  287. * user edit ajax system
  288. * =====================
  289. * passwords strenght meter taken from:
  290. * https://code.tutsplus.com/articles/using-the-included-password-strength-meter-script-in-wordpress--wp-34736
  291. * https://github.com/WordPress/WordPress/blob/master/wp-admin/js/user-profile.js#L223
  292. * ajax idea:
  293. * https://wordpress.stackexchange.com/questions/274778/updating-user-profile-with-ajax-not-working
  294. * form field validation:
  295. * https://itnext.io/https-medium-com-joshstudley-form-field-validation-with-html-and-a-little-javascript-1bda6a4a4c8c
  296. * page template idea:
  297. * https://wordpress.stackexchange.com/questions/9775/how-to-edit-a-user-profile-on-the-front-end
  298. */
  299. function sandbox_ajax_user_edit(){
  300. check_ajax_referer('sandbox_ajax_call', 'verify');
  301. // list of valid field names
  302. $fields = array('user_login', 'first_name', 'last_name', 'nickname', 'display_name', 'pass1');
  303. if ( isset($_POST['field_name']) ) {
  304. $field_name = $_POST['field_name'];
  305. if ( in_array($field_name, $fields) ) { // valid field
  306. if ( isset($_POST[$field_name]) ) { // field value is set
  307. $value = filter_var(trim($_POST[$_POST['field_name']]), FILTER_SANITIZE_STRING);
  308. $pattern = '/[A-Za-z0-9 ]{3,32}/';
  309. if ( $_POST['field_name'] === 'nickname' ) {
  310. $pattern = '/[a-zA-Z0-9-_ ]{3,32}/';
  311. }
  312. if ( $_POST['field_name'] === 'pass1' ) {
  313. $pattern = '/^[^\\\\]*$/';
  314. }
  315. if ( preg_match($pattern, $value) ) { // check valid pattern
  316. $current_user = wp_get_current_user();
  317. // reset $_POST
  318. $_POST = array(
  319. 'email' => $current_user->user_email,
  320. );
  321. if ( $field_name !== 'nickname' ) {
  322. $_POST['nickname'] = $current_user->nickname;
  323. }
  324. // readd value
  325. $_POST[$field_name] = $value;
  326. if ( $field_name === 'pass1' ) {
  327. $_POST['pass2'] = $value;
  328. }
  329. $edited_user = edit_user($current_user->ID);
  330. if ( gettype($edited_user) === "integer" ) {
  331. $json = array('error' => false, 'message' => 'Value "' . $value . '" for field "' . $field_name . '" was saved');
  332. } else {
  333. $json = array('error' => true, 'debug' => array('user_edit' => $edited_user), 'message' => 'User was not saved, look at "debug" array');
  334. }
  335. } else {
  336. $json = array('error' => true, 'message' => 'Value for field "' . $_POST['field_name'] . '" is not valid');
  337. }
  338. } else {
  339. $json = array('error' => true, 'message' => 'No value for field "' . $_POST['field_name'] . '" sent');
  340. }
  341. } else {
  342. $json = array('error' => true, 'message' => 'Given fieldname "' . $_POST['field_name'] . '" is not valid');
  343. }
  344. } else {
  345. $json = array('error' => true, 'message' => 'No "field" Param fieldname sent');
  346. }
  347. wp_send_json($json);
  348. }
  349. // Change Wordpress email subject
  350. function sandbox_mail_from_name($name){
  351. return get_bloginfo('name', 'display');
  352. }
  353. /**
  354. * Sort countries list by name instead country-iso-code
  355. * https://wordpress.org/support/topic/excellent-9087/
  356. */
  357. function sandbox_listo_ordered($data){
  358. sort($data);
  359. return $data;
  360. }
  361. /**
  362. * filter values of form tags to email
  363. *
  364. * @param $tag_replace string to change
  365. * @param $submitted an array containing all submitted fields
  366. * @param $cf7_key is a unique string key to identify your form, which you can find in your form table in the dashboard.
  367. */
  368. function sandbox_cf7_mailtag_email_updates($tag_replace, $submitted, $cf7_key){
  369. if ( $cf7_key == 'free-trial' ) {
  370. if ( $tag_replace === '' ) { // empty means no
  371. $tag_replace = 'no'; //change the email-updates.
  372. }
  373. }
  374. return $tag_replace;
  375. }
  376. function sandbox_smart_grid_register_styles(){
  377. // wp_deregister_style('cf7-grid-layout');
  378. }
  379. /**
  380. * Site Widgets
  381. * =============
  382. */
  383. /**
  384. * custom faq navigation widget
  385. * ============================
  386. * requires the post-type "faq"
  387. * requires the taxonomy "topic"
  388. */
  389. class sandbox_faq_nav_widget extends WP_Widget {
  390. public function __construct() {
  391. /* Widget settings. */
  392. $widget_ops = array('classname' => 'faq-navigation', 'description' => 'Displays the FAQ navigation.');
  393. /* Widget control settings. */
  394. $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'faq-navigation');
  395. /* Create the widget. */
  396. parent::__construct('faq-navigation', 'FAQ Navigation', $widget_ops, $control_ops);
  397. }
  398. // processes widget options to be saved
  399. public function update($new_instance, $old_instance) {
  400. $instance = $old_instance;
  401. $instance['title'] = strip_tags($new_instance['title']);
  402. return $instance;
  403. }
  404. // outputs the content of the widget
  405. public function widget($args, $instance) {
  406. sandbox_faq_nav($args, $instance);
  407. }
  408. // outputs the options form on admin
  409. public function form($instance) {
  410. $title = esc_attr($instance['title']);
  411. ?><p>
  412. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'sandbox'); ?></label>
  413. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
  414. </p><?php
  415. }
  416. }
  417. function sandbox_faq_nav($args, $instance){
  418. extract($args);
  419. $widget_title = apply_filters('widget_title', $instance['title']);
  420. /**
  421. * FAQ entries
  422. * grouped by topic
  423. * ordered by menu_order *
  424. */
  425. $faq_ids = get_posts(array(
  426. 'post_type' => 'faq',
  427. 'posts_per_page' => -1,
  428. 'fields' => 'ids',
  429. 'meta_query' => array(
  430. 'relation' => 'OR',
  431. array(
  432. 'key' => '_hide',
  433. 'value' => 1,
  434. 'compare' => 'NOT LIKE',
  435. ),
  436. array(
  437. 'key' => '_hide',
  438. 'value' => 'bug #23268',
  439. 'compare' => 'NOT EXISTS',
  440. )
  441. ),
  442. ));
  443. $topic_term_objects = get_terms(array(
  444. 'taxonomy' => 'topic',
  445. 'object_ids' => $faq_ids,
  446. 'orderby' => 'order', // needs plugin https://github.com/stuttter/wp-term-order/
  447. 'order' => 'ASC',
  448. ));
  449. if ( !empty($topic_term_objects) && !is_wp_error($topic_term_objects) ) {
  450. echo $before_widget;
  451. if ( $widget_title ) {
  452. echo $before_title . $widget_title . $after_title;
  453. }
  454. echo '<ul class="menu menu-faq">';
  455. foreach ( $topic_term_objects as $topic_term_object ) {
  456. $faq_options = array(
  457. 'post_type' => 'faq',
  458. 'tax_query' => array(
  459. array(
  460. 'field' => 'slug',
  461. 'taxonomy' => 'topic',
  462. 'terms' => $topic_term_object->slug,
  463. )
  464. ),
  465. 'meta_query' => array(
  466. 'relation' => 'OR',
  467. array(
  468. 'key' => '_hide',
  469. 'value' => 1,
  470. 'compare' => 'NOT LIKE',
  471. ),
  472. array(
  473. 'key' => '_hide',
  474. 'value' => 'bug #23268',
  475. 'compare' => 'NOT EXISTS',
  476. )
  477. ),
  478. 'posts_per_page' => -1,
  479. 'orderby' => 'menu_order',
  480. 'order' => 'ASC',
  481. );
  482. $faq_query = new WP_Query($faq_options);
  483. if ( $faq_query->have_posts() ) {
  484. echo '<li class="menu-item menu-item-' . esc_attr($topic_term_object->slug) . '">';
  485. echo '<a href="#submenu-' . esc_attr($topic_term_object->slug) . '">' . $topic_term_object->name . '</a>';
  486. echo '<ul id="submenu-' . esc_attr($topic_term_object->slug) . '" class="sub-menu">';
  487. while ( $faq_query->have_posts() ) {
  488. $faq_query->the_post();
  489. $post_id = get_the_ID();
  490. $post_slug = sandbox_get_the_slug();
  491. $post_class = get_post_class();
  492. echo '<li id="menu-item-' . $post_slug . '" class="' . esc_attr(join(' ', $post_class)) . '"><a href="#' . $post_slug . '">' . get_the_title() . '</a></li>';
  493. }
  494. wp_reset_postdata();
  495. echo '</ul></li>';
  496. }
  497. }
  498. echo $after_widget;
  499. }
  500. }
  501. /**
  502. * custom events calender widget
  503. * =============================
  504. * requires the post-type "events" with custom fields:
  505. * -_date-end
  506. * -_date-begin
  507. */
  508. class sandbox_events_widget extends WP_Widget {
  509. public function __construct() {
  510. /* Widget settings. */
  511. $widget_ops = array('classname' => 'events-calendar', 'description' => 'Test um zu schauen ob ich ein widget basteln kann.');
  512. /* Widget control settings. */
  513. $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'custom-events-calendar');
  514. /* Create the widget. */
  515. parent::__construct('custom-events-calendar', 'N&auml;chste Termine', $widget_ops, $control_ops );
  516. }
  517. // processes widget options to be saved
  518. public function update($new_instance, $old_instance) {
  519. $instance = $old_instance;
  520. $instance['title'] = strip_tags( $new_instance['title'] );
  521. $instance['eventtype'] = $new_instance['eventtype'];
  522. $instance['maximum'] = intval($new_instance['maximum']);
  523. $instance['want_excerpt'] = filter_var($new_instance['want_excerpt'], FILTER_VALIDATE_BOOLEAN);
  524. return $instance;
  525. }
  526. // outputs the content of the widget
  527. public function widget($args, $instance) {
  528. sandbox_events($args, $instance);
  529. }
  530. // outputs the options form on admin
  531. public function form($instance) {
  532. $title = esc_attr($instance['title']);
  533. $eventtypes = get_terms('eventtypen');
  534. $maximum = esc_attr($instance['maximum']);
  535. $want_excerpt = esc_attr($instance['want_excerpt']);
  536. ?>
  537. <p>
  538. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'sandbox'); ?></label>
  539. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
  540. </p>
  541. <p>
  542. <label for="<?php echo $this->get_field_id('eventtype'); ?>"><?php _e('Event-Typ:', 'sandbox'); ?></label>
  543. <select class="widefat" id="<?php echo $this->get_field_id('eventtype'); ?>" name="<?php echo $this->get_field_name('eventtype'); ?>">
  544. <?php foreach ($eventtypes as $eventtype){ ?>
  545. <option value="<?php echo $eventtype->slug; ?>"<?php if ( $instance['eventtype'] == $eventtype->slug ) { echo ' selected="selected"'; } ?>><?php echo $eventtype->name; ?></option>
  546. <?php } ?>
  547. </select>
  548. </p>
  549. <p>
  550. <label for="<?php echo $this->get_field_id('maximum'); ?>"><?php _e('Maximum:', 'sandbox'); ?></label>
  551. <input class="widefat" id="<?php echo $this->get_field_id('maximum'); ?>" name="<?php echo $this->get_field_name('maximum'); ?>" type="text" value="<?php echo $maximum; ?>" />
  552. </p>
  553. <p>
  554. <input class="checkbox" type="checkbox" value="true" <?php checked( 1, $want_excerpt ); ?> id="<?php echo $this->get_field_id('want_excerpt'); ?>" name="<?php echo $this->get_field_name('want_excerpt'); ?>" />
  555. <label for="<?php echo $this->get_field_id('want_excerpt'); ?>"><?php _e('Auszug anzeigen', 'sandbox'); ?></label>
  556. </p>
  557. <?php
  558. }
  559. }
  560. function sandbox_events($args, $instance){
  561. extract($args);
  562. $title = apply_filters('widget_title', $instance['title'] );
  563. $max = $instance['maximum'];
  564. $index = 0;
  565. $eventsQuery = array(
  566. 'post_type' => 'events',
  567. 'order' => 'ASC',
  568. 'orderby' => 'meta_value',
  569. 'meta_key' => '_date-begin',
  570. 'eventtypen' => $instance['eventtype'],
  571. 'posts_per_page' => 100,
  572. );
  573. $events = new WP_Query($eventsQuery);
  574. $want_excerpt = $instance['want_excerpt'];
  575. if ( $events->have_posts() ) {
  576. echo $before_widget;
  577. if ( $title ) { echo $before_title . $title . $after_title; }
  578. echo '<ul class="events events-widget">';
  579. while ( $events->have_posts() ) {
  580. $events->the_post();
  581. if ( $index < $max ) {
  582. $event_meta = get_post_meta(get_the_ID());
  583. $target_day = isset($event_meta["_date-end"][0]) && is_string($event_meta["_date-end"][0]) && strlen($event_meta["_date-end"][0]) > 0 ? $event_meta["_date-end"][0] : $event_meta["_date-begin"][0];
  584. $current_date = $date = date('Y-m-d', time());
  585. // DEBUG
  586. // echo '<!-- title:', get_the_title(), ', max: ', $max, ', index: ', $index + 1, ' -->';
  587. // echo '<!-- title:', get_the_title(), ' ', $current_date, ' lte ', $target_day, ' === ', var_dump( $current_date <= $target_day ), ' -->';
  588. if ( $current_date <= $target_day ) {
  589. $index++;
  590. $begin_dayDisplay = date("j. n.", strtotime($event_meta["_date-begin"][0]));
  591. $begin_yearDisplay = date("Y", strtotime($event_meta["_date-begin"][0]));
  592. ?>
  593. <li <?php post_class('clearfix'); ?>>
  594. <aside class="meta"><p class="date"><span class="day"><?php echo $begin_dayDisplay; ?></span> <span class="year"><?php echo $begin_yearDisplay; ?></span></p></aside>
  595. <h4><a href="<?php the_permalink() ?>"><span class="entry-title"><?php the_title() ?></span></a></h4>
  596. <?php if ( $want_excerpt && has_excerpt() ) { ?>
  597. <div class="entry-summary">
  598. <?php the_excerpt(); ?>
  599. </div>
  600. <?php } ?>
  601. </li>
  602. <?php
  603. }
  604. }
  605. }
  606. echo '</ul>';
  607. echo $after_widget;
  608. }
  609. wp_reset_postdata();
  610. }
  611. /**
  612. * custom social share widget
  613. * ==========================
  614. * No user tracking, no javascript.
  615. *
  616. * - to use in your themes function.php
  617. * - edit the markup of the widget at sandbox_social();
  618. * - add your services at update()
  619. * - Service adresses are hardcoded and can change during time
  620. * - Add the CSS part of this file in yourThemes style.css
  621. */
  622. class sandbox_social_widget extends WP_Widget {
  623. public function __construct() {
  624. /* Widget settings. */
  625. $widget_ops = array('classname' => 'social', 'description' => 'Display social share icons without automatic user tracking');
  626. /* Widget control settings. */
  627. $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'social');
  628. /* Create the widget. */
  629. parent::__construct('social', 'Social share', $widget_ops, $control_ops );
  630. }
  631. // processes widget options to be saved
  632. public function update($new_instance, $old_instance) {
  633. $new_instance = (array) $new_instance;
  634. $instance = array(
  635. 'title' => strip_tags( $new_instance['title'] ),
  636. 'services' => array(
  637. 'xing' => array(
  638. 'name' => 'XING',
  639. 'url' => 'https://www.xing.com/spi/shares/new?url=',
  640. ),
  641. 'facebook' => array(
  642. 'name' => 'Facebook',
  643. 'url' => 'https://www.facebook.com/sharer/sharer.php?u=',
  644. ),
  645. 'twitter' => array(
  646. 'name' => 'Twitter',
  647. 'url' => 'http://twitter.com/share?url=',
  648. ),
  649. 'googleplus' => array(
  650. 'name' => 'Google+',
  651. 'url' => 'https://plus.google.com/share?url=',
  652. ),
  653. 'linkedin' => array(
  654. 'name' => 'LinkedIn',
  655. 'url' => 'http://www.linkedin.com/sharer.php?u=',
  656. ),
  657. 'pinterest' => array(
  658. 'name' => 'Pinterest',
  659. 'url' => 'http://www.pinterest.com/pin/create/bookmarklet/?url=',
  660. ),
  661. 'email' => array(
  662. 'name' => 'Email',
  663. 'url' => 'mailto:?subject=' . __('Teilen mit', 'sandbox') . ' ' . wp_get_document_title() . '&body=',
  664. ),
  665. ),
  666. );
  667. foreach ( $instance['services'] as $serviceName => $serviceData ) {
  668. $instance[$serviceName] = filter_var($new_instance[$serviceName], FILTER_VALIDATE_BOOLEAN);
  669. }
  670. return $instance;
  671. }
  672. // outputs the content of the widget
  673. public function widget($args, $instance) {
  674. sandbox_social($args, $instance);
  675. }
  676. // outputs the options form on admin
  677. public function form($instance) {
  678. $defaults = array('facebook' => true, 'twitter' => true, 'googleplus' => true, 'linkedin' => false);
  679. $services = $instance['services'];
  680. $instance = wp_parse_args( (array) $instance, $defaults);
  681. $title = esc_attr($instance['title']);
  682. ?>
  683. <p>
  684. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'sandbox'); ?></label>
  685. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
  686. </p>
  687. <?php foreach ( $services as $serviceName => $serviceData ) { ?>
  688. <p>
  689. <input class="checkbox" value="true" type="checkbox" id="<?php echo $this->get_field_id($serviceName); ?>" name="<?php echo $this->get_field_name($serviceName); ?>" <?php checked($instance[$serviceName], true) ?> />
  690. <label for="<?php echo $this->get_field_id($serviceName); ?>"><?php echo $serviceData['name']; ?></label>
  691. </p>
  692. <?php } ?>
  693. <?php
  694. }
  695. }
  696. function sandbox_social($args, $instance){
  697. extract($args);
  698. $title = apply_filters('widget_title', $instance['title'] );
  699. $services = $instance['services'];
  700. $protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === false ? 'http' : 'https';
  701. $host = $_SERVER["HTTP_HOST"];
  702. $path = $_SERVER["REQUEST_URI"];
  703. $pageUrl = $protocol . '://' . $host . $path;
  704. $servicesCount = 0;
  705. $servicesList = '';
  706. foreach ( $services as $serviceName => $serviceData ) {
  707. if ( $instance[$serviceName] ) {
  708. $servicesCount++;
  709. $servicesList .= '<li class="' . $serviceName . '"><a href="' . esc_url($serviceData["url"], ( $serviceName === 'email' ? 'mailto' : 'https' ) ) . $pageUrl . '" target="_blank" title="' . __('Teilen mit', 'sandbox') . ' ' . $serviceData["name"] . '">' . $serviceData["name"] . '</a></li>';
  710. }
  711. }
  712. if ( $servicesCount > 0 ) {
  713. echo $before_widget;
  714. ?>
  715. <div class="social">
  716. <?php if ( !empty($title) ) { echo $before_title . $title . $after_title; } ?>
  717. <ul>
  718. <?php echo $servicesList; ?>
  719. </ul>
  720. </div>
  721. <?php
  722. echo $after_widget;
  723. }
  724. }
  725. /**
  726. * custom slider widget
  727. */
  728. class sandbox_slider_widget extends WP_Widget {
  729. function __construct() {
  730. /* Widget settings. */
  731. $widget_ops = array('classname' => 'slider', 'description' => 'Select a slider to display');
  732. /* Widget control settings. */
  733. $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'slider');
  734. /* Create the widget. */
  735. parent::__construct('slider', 'Slider', $widget_ops, $control_ops );
  736. }
  737. // processes widget options to be saved
  738. public function update($new_instance, $old_instance) {
  739. $instance = $old_instance;
  740. $instance['title'] = strip_tags( $new_instance['title'] );
  741. $instance['slider'] = $new_instance['slider'];
  742. $instance['show-titles'] = filter_var($new_instance['show-titles'], FILTER_VALIDATE_BOOLEAN);
  743. $instance['show-content'] = filter_var($new_instance['show-content'], FILTER_VALIDATE_BOOLEAN);
  744. $instance['show-excerpt'] = filter_var($new_instance['show-excerpt'], FILTER_VALIDATE_BOOLEAN);
  745. return $instance;
  746. }
  747. // outputs the content of the widget
  748. public function widget($args, $instance) {
  749. sandbox_slider($args, $instance);
  750. }
  751. // outputs the options form on admin
  752. public function form($instance) {
  753. $title = esc_attr($instance['title']);
  754. $sliders = get_terms('slider');
  755. ?>
  756. <p>
  757. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'sandbox'); ?></label>
  758. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
  759. </p>
  760. <p>
  761. <label for="<?php echo $this->get_field_id('slider'); ?>"><?php _e('Slider:', 'sandbox'); ?></label>
  762. <select class="widefat" id="<?php echo $this->get_field_id('slider'); ?>" name="<?php echo $this->get_field_name('slider'); ?>">
  763. <?php foreach ($sliders as $slider){ ?>
  764. <option value="<?php echo $slider->slug; ?>"<?php if ( $instance['slider'] == $eventtype->slug ) { echo ' selected="selected"'; } ?>><?php echo $slider->name; ?></option>
  765. <?php } ?>
  766. </select>
  767. </p>
  768. <p>
  769. <input class="checkbox" value="true" type="checkbox" id="<?php echo $this->get_field_id('show-titles'); ?>" name="<?php echo $this->get_field_name('show-titles'); ?>" <?php checked($instance['show-titles'], true) ?> />
  770. <label for="<?php echo $this->get_field_id('show-titles'); ?>"><?php echo 'Show titles'; ?></label>
  771. </p>
  772. <p>
  773. <input class="checkbox" value="true" type="checkbox" id="<?php echo $this->get_field_id('show-content'); ?>" name="<?php echo $this->get_field_name('show-content'); ?>" <?php checked($instance['show-content'], true) ?> />
  774. <label for="<?php echo $this->get_field_id('show-content'); ?>"><?php echo 'Show content'; ?></label>
  775. </p>
  776. <p>
  777. <input class="checkbox" value="true" type="checkbox" id="<?php echo $this->get_field_id('show-excerpt'); ?>" name="<?php echo $this->get_field_name('show-excerpt'); ?>" <?php checked($instance['show-excerpt'], true) ?> />
  778. <label for="<?php echo $this->get_field_id('show-excerpt'); ?>"><?php echo 'Show excerpt'; ?></label>
  779. </p>
  780. <?php
  781. }
  782. }
  783. function sandbox_slider($args, $instance){
  784. extract($args);
  785. $title = apply_filters('widget_title', $instance['title']);
  786. $sliderQuery = array(
  787. 'post_type' => 'slide',
  788. 'order' => 'ASC',
  789. 'orderby' => 'menu_order',
  790. 'slider' => $instance['slider']
  791. );
  792. $slider = new WP_Query($sliderQuery);
  793. $sliderCount = 0;
  794. $sliderNav = '<ul class="slider-nav">';
  795. $sliderId = $slider->query_vars['taxonomy'] . '-' . $slider->query_vars['term'];
  796. $sliderClass = $slider->query_vars['taxonomy'];
  797. $show_titles = filter_var($instance['show-titles'], FILTER_VALIDATE_BOOLEAN);
  798. $show_content = filter_var($instance['show-content'], FILTER_VALIDATE_BOOLEAN);
  799. $show_excerpt = filter_var($instance['show-excerpt'], FILTER_VALIDATE_BOOLEAN);
  800. if ( $slider->have_posts() ) {
  801. echo $before_widget;
  802. echo '<section class="', $sliderClass, ' ', $sliderId, '" id="', $sliderId, '">';
  803. if ( $title ){ echo $before_title . $title . $after_title; }
  804. echo ' <ul class="slides clearfix">';
  805. while ( $slider->have_posts() ) { $slider->the_post();
  806. $slideId = get_post_field('post_name', get_the_ID() );
  807. $sliderCount++;
  808. $sliderNav .= '<li><a href="#slide-' . $slideId . '">' . $sliderCount . '</a></li>';
  809. $sliderImage = get_the_post_thumbnail(get_the_ID(), 'slider', array('title' => the_title_attribute('echo=0')));
  810. $sliderMeta = get_post_meta( get_the_ID() );
  811. $the_content = $show_content ? get_the_content() : $show_content;
  812. $has_content = !empty(trim(str_replace('&nbsp;', '', strip_tags($the_content))));
  813. // echo var_dump($sliderMeta["slider-link"][0]);
  814. ?>
  815. <li id="slide-<?php echo $slideId ?>" class="slide slide-<?php echo $slideId ?> slide-<?php echo $sliderCount ?> clearfix">
  816. <?php echo $sliderImage; ?>
  817. <?php if ( $show_titles || $show_excerpt || $show_content ) { ?>
  818. <div class="inner">
  819. <?php if ( $show_titles ) { ?>
  820. <h3><?php the_title(); ?></h3>
  821. <?php } ?>
  822. <?php if ( $show_content && $has_content ) { ?>
  823. <div class="content">
  824. <?php the_content(); ?>
  825. </div>
  826. <?php } ?>
  827. <?php if ( $show_excerpt && has_excerpt() ) { ?>
  828. <div class="content excerpt">
  829. <?php the_excerpt(); ?>
  830. </div>
  831. <?php } ?>
  832. </div>
  833. <?php } ?>
  834. </li>
  835. <?php
  836. }
  837. echo '</ul>';
  838. $sliderNav .= '</ul>';
  839. echo $sliderNav;
  840. echo '</section>';
  841. echo $after_widget;
  842. }
  843. wp_reset_postdata();
  844. }
  845. ?>