prefix . 'operation';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
source varchar(50),
category varchar(255),
location varchar(255),
keyword varchar(255),
keyword_translation varchar(255),
created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
//set permalink
$wp_rewrite->set_permalink_structure('/%postname%/');
update_option( "rewrite_rules", FALSE );
$wp_rewrite->flush_rules( true );
add_option( 'fscoperation_db_version', $fscoperation_db_version );
}
register_activation_hook( __FILE__, 'fscoperation_init' );
//automatically deletes table after uninstallation of the plugin
function fscoperation_delete_table(){
global $wpdb;
$table_name = $wpdb->prefix . 'table_name';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
delete_option("fscoperation_db_version");
}
register_uninstall_hook(__FILE__, 'fscoperation_delete_table');
//function to add operation to database
function add_operation( $data ) {
global $wpdb;
if (! $data->get_param( 'keyword' )) {
return 0;
}
$category = $data->get_param( 'category' );
$location = $data->get_param( 'location' );
$keyword = $data->get_param( 'keyword' );
$keyword_translation = $data->get_param( 'keyword_translation' );
//check parameters, if fields are filled, else return 0
$table_name = $wpdb->prefix . 'operation';
return $wpdb->insert( $table_name,
array(
'source' => 'webhook',
'category' => $category,
'location' => $location,
'keyword' => $keyword,
'keyword_translation' => $keyword_translation,
'created' => current_time( 'mysql' ),
)
);
}
//function to get all operations as json
function get_operations() {
global $wpdb;
$table_name = $wpdb->prefix . 'operation';
$result = $wpdb->get_results( "SELECT * FROM $table_name WHERE source = 'webhook' AND created > (NOW() - INTERVAL 1 HOUR) ORDER BY created DESC", ARRAY_A ); //If you want a interval of 2 hours, you maybe have to define 1 hour. Because of difference between database server and wordpress/php server.
return $result;
}
// Routes for the RestAPI
add_action('rest_api_init', function() {
register_rest_route('capi/v1', 'operation', [
['methods' => 'GET', //only 'GET' works for the webhook, because of FSConnect - GET https://[your-website].de/capi/v1/operation?category={Kategorie}&location={Standort}&keyword={Stichwort}&keyword_translation={Stichwortübersetzung}
'callback' => 'add_operation']
,
['methods' => 'POST', //created for simple testing with POSTMAN - output off all operations with - POST https://[your-website].de/capi/v1/operation
'callback' => 'get_operations']
]);
});
//to protect personal data, we only use the following wording for our output
function wording_filter( $str ) {
switch (true) {
case preg_match('/brandmeldeanlage/i' ,$str):
return 'Brandmeldeanlage';
break;
case preg_match('/(^f|brand)/i' ,$str):
return 'Brandeinsatz';
break;
case preg_match('/^h/i' ,$str):
return 'Hilfeleistung';
break;
case preg_match('/sturm/i' ,$str):
return 'Sturmschaden/Baum';
break;
default:
return 'Sonstiger Einsatz';
break;
}
}
//function to get nice html style and european date format
function get_html_operations() {
$arr_operations = get_operations();
// Build output string
if (empty($arr_operations)) { // Empty Operation Array
$strHTML .= 'Aktuell sind keine Einsätze aktiv.';
} else { // Filled Operation Array
$i = 0;
foreach ( $arr_operations as $arr_operation ) {
$i++;
$created = DateTime::createFromFormat("Y-m-d H:i:s", $arr_operation['created']);
$keyword = wording_filter($arr_operation['keyword']);
//$keyword = $arr_operation['keyword'];
$strHTML .= '' . $created->format("d/m/Y H:i:s") . '
';
$strHTML .= '🚒 '. $keyword . '
';
if (0 < $i AND $i < count($arr_operations)) {
$strHTML .= '