-
Notifications
You must be signed in to change notification settings - Fork 0
/
mitfahrgelegenheit.php
122 lines (108 loc) · 5.23 KB
/
mitfahrgelegenheit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/*
Plugin Name: Mitfahrgelegenheit
Plugin URI: https://www.bessermitfahren.de/api
Description: Plugin zur Einbindung von Suchergebnissen für Mitfahrgelegenheiten
Author: Philipp Roggan
Version: 1.0.6
Author URI: https://www.bessermitfahren.de/vision
Text Domain: bmf-api
License: WTFPL
License URI: http://www.wtfpl.net/txt/copying/
*/
if ( ! defined( 'ABSPATH' ) ) {
die( 'Directly access this file you can not!' );
}
define( 'BMF_ID', 'mitfahrgelegenheit/mitfahrgelegenheit.php' );
require_once( plugin_dir_path(__FILE__) . 'functions.php');
function bmf_plugin_activate(){
$options = bmf_get_options();
$bmf_api_key = bmf_get_api_key();
if ($bmf_api_key && strlen($bmf_api_key) > 10){
$options['api_key'] = $bmf_api_key;
update_option( 'de.bessermitfahren.options', $options );
}
}
register_activation_hook( __FILE__, 'bmf_plugin_activate' );
function bmf_uninstall(){
$options = bmf_get_options();
$options['agb_accepted'] = false;
update_option( 'de.bessermitfahren.options', $options );
}
register_uninstall_hook( __FILE__, 'bmf_uninstall' );
register_deactivation_hook( __FILE__, 'bmf_uninstall' );
function bmf_plugin_options() {
require( plugin_dir_path( __FILE__ ) . 'options.php' );
}
function bmf_plugin_menu() {
add_options_page( 'Mitfahrgelegenheit', 'Mitfahrgelegenheit', 'manage_options', BMF_OPTIONS, 'bmf_plugin_options' );
}
add_action( 'admin_menu', 'bmf_plugin_menu' );
// List
function bmf_list( $atts ) {
// get settings from db
$options = bmf_get_options();
$bmf_api_key = ( isset( $options ) && isset( $options['api_key'] ) ) ? $options['api_key'] : false;
$bmf_agb = ( isset( $options ) && isset( $options['agb_accepted'] ) ) ? (bool)$options['agb_accepted'] : false;
$bmf_place_from = ( isset( $options ) && isset( $options['place_from'] ) ) ? $options['place_from'] : '';
$bmf_place_to = ( isset( $options ) && isset( $options['place_to'] ) ) ? $options['place_to'] : '';
$bmf_extra_style= ( isset( $options ) && isset( $options['extra_style'] ) ) ? $options['extra_style'] : '';
// overwrite places if set directly
$bmf_place_to = ( isset( $atts['to'] ) ) ? $atts['to'] : $bmf_place_to;
$bmf_place_from = ( isset( $atts['from'] ) ) ? $atts['from'] : $bmf_place_from;
// if is allready set up, add shortcode content
if ( $bmf_api_key && $bmf_agb ) {
$bmf_url = BMF_URL;
$bmf_text = 'Mitfahrgelegenheiten - Mitfahrzentrale';
$bmf_config = '{api:"' . $bmf_api_key . '", wordpress: "' . get_bloginfo('version') . '"}';
if ( ! $bmf_place_from && $bmf_place_to ) {
$bmf_url .= 'nach/' . strtolower( urlencode( $bmf_place_to ) ) . '/mitfahrgelegenheiten/';
$bmf_text = 'Mitfahrgelegenheiten nach ' . ucfirst( $bmf_place_to ) . ' - Mitfahrzentrale';
$bmf_config = '{api:"' . $bmf_api_key . '", wordpress: "' . get_bloginfo( 'version' ) . '",to:"' . $bmf_place_to . '"}';
}
if ( $bmf_place_from && $bmf_place_to ) {
$bmf_url .= strtolower( urlencode( $bmf_place_from ) ) . '/' . strtolower( urlencode( $bmf_place_to ) ) . '/mitfahrgelegenheiten/';
$bmf_text = 'Mitfahrgelegenheiten von ' . ucfirst( $bmf_place_from ) . ' nach ' . ucfirst( $bmf_place_to ) . ' - Mitfahrzentrale';
$bmf_config = '{api:"' . $bmf_api_key . '", wordpress: "' . get_bloginfo( 'version' ) . '",to:"' . $bmf_place_to . '",from:"' . $bmf_place_from . '"}';
}
if ( $bmf_place_from && ! $bmf_place_to ) {
$bmf_url .= strtolower( urlencode( $bmf_place_from ) ) . '/mitfahrzentrale/';
$bmf_text = 'Mitfahrzentrale ' . $bmf_place_from . ' - Mitfahrgelegenheit';
$bmf_config = '{api:"' . $bmf_api_key . '", wordpress: "' . get_bloginfo( 'version' ) . '",from:"' . $bmf_place_from . '"}';
}
$bmf_shortcode = '<script type="text/javascript" src="' . BMF_API . '"></script>';
$bmf_shortcode .= '<style type="text/css">' . $bmf_extra_style . '</style>';
$bmf_shortcode .= '<span id="bmflink">lade <a href="' . $bmf_url . '">' . $bmf_text . '</a>.</span>';
$bmf_shortcode .= '<script type="text/javascript">bmf.init(' . $bmf_config . ')</script>';
return $bmf_shortcode;
} else {
return '<p>' . __('Das Mitfahrgelegenheiten Plugin muss erst im Backend eingerichtet werden...') . '</p>';
}
}
add_shortcode( "bmf_list", "bmf_list" );
function bmf_add_quicktag() {
if ( wp_script_is( 'quicktags' ) ) {
?>
<script type="text/javascript">
QTags.addButton('bmf_add_list', 'BMF', '[bmf_list]', '', '', 'Fügt den Shortcode für die Mitfahrgelegenheiten ein', 200);
</script>
<?php
}
}
add_action( 'admin_print_footer_scripts', 'bmf_add_quicktag' );
function bmf_plugin_shortcode_button_init() {
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) && get_user_option( 'rich_editing' ) == 'true' ) {
return;
}
add_filter( "mce_external_plugins", "bmf_plugin_register_tinymce_plugin" );
add_filter( 'mce_buttons', 'bmf_plugin_add_tinymce_button' );
}
function bmf_plugin_register_tinymce_plugin( $plugin_array ) {
$plugin_array['bmf_plugin_button'] = plugins_url( '/shortcode.js', __FILE__ );
return $plugin_array;
}
function bmf_plugin_add_tinymce_button( $buttons ) {
$buttons[] = "bmf_plugin_button";
return $buttons;
}
add_action( 'admin_init', 'bmf_plugin_shortcode_button_init' );