Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding fields #1

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
312 changes: 267 additions & 45 deletions Calendar_to_Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,296 @@
* Plugin Name: Calendar to Discord
* Plugin URI: https://www.github.com/RealHartlMax/Calendar-to-Discord
* Description: This plugin sends a Discord message when a new event is created in The Events Calendar plugin.
* Version: 1.0
* Author: RealHartlMax
* Version: 1.0.0
* Author: RealHartlMax, Florian König-Heidinger
* Author URI: https://www.hartlmax.de
*/

// Add settings page
add_action( 'admin_menu', 'my_plugin_menu' );
function my_plugin_menu() {
add_options_page( 'Calendar to Discord Options', 'Calendar to Discord', 'manage_options', 'my-unique-identifier', 'my_plugin_options' );
}
class RHM_C2D_OptionsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
private $slug = 'rhm_c2d';
private $optionName = 'rhm_c2d';
private $optionGroup = 'rhm_c2d_options';
private $options;

/**
* Start up
*/
public function __construct()
{
add_action('admin_menu', [$this, 'add_plugin_page']);
add_action('admin_init', [$this, 'page_init']);
}

/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Calendar to Discord', // page title
'Calendar to Discord', // menu title
'manage_options', // capability
$this->slug, // menu slug
[$this, 'create_admin_page']// function to print admin page
);
}

/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option($this->optionName);

echo '<div class="wrap">';
screen_icon();
echo '<h2>Webhook for Discord</h2>';
if (isset($_GET['webhook']) && $_GET['webhook'] === 'test') {
echo '<p>';
echo 'Test wurde ausgeführt. ';
send_discord_message([
'title' => get_option('blogname'),
'url' => get_option('home'),
'description' => get_option('blogdescription'),
'color' => 15258703,
]);
echo '<br />';
echo '<a href="options-general.php?page=' . $this->slug . '">Testmodus beenden</a>';
echo '</p>';
} else {
echo '<a href="options-general.php?page=' . $this->slug . '&amp;webhook=test">Teste den Webhook</a>';
echo '<form method="post" action="options.php">';
// This prints out all hidden setting fields
settings_fields($this->optionGroup);
do_settings_sections($this->slug);
submit_button();
echo '</form>';
}
echo '</div>';
}

/**
* Register and add settings
*/
public function page_init()
{
register_setting(
$this->optionGroup, // Option group
$this->optionName, // Option name
[$this, 'sanitize']// Sanitize
);

function my_plugin_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
$sectionId = 'webhook-section';
add_settings_section(
$sectionId, // ID
'Webhook', // Title
[$this, 'print_section_info'], // Callback
$this->slug// Page
);

add_settings_field(
'url', // ID
'URL', // Title
[$this, 'print_input_field'], // Callback
$this->slug, // Page
$sectionId, // Section
[
'name' => 'url',
]
);
add_settings_field(
'name', // ID
'Name', // Title
[$this, 'print_input_field'], // Callback
$this->slug, // Page
$sectionId, // Section
[
'name' => 'name',
]
);
add_settings_field(
'avatar', // ID
'Avatar', // Title
[$this, 'print_input_field'], // Callback
$this->slug, // Page
$sectionId, // Section
[
'name' => 'avatar',
]
);
add_settings_field(
'message', // ID
'Message', // Title
[$this, 'print_input_field'], // Callback
$this->slug, // Page
$sectionId, // Section
[
'name' => 'message',
]
);
add_settings_field(
'mentions', // ID
'Mentions', // Title
[$this, 'print_input_field'], // Callback
$this->slug, // Page
$sectionId, // Section
[
'name' => 'mentions',
'description' => 'Mit Komma getrennt ohne @. z.B. "everyone,events"',
]
);
}
echo '<div class="wrap">';
echo '<p>Here you can configure the settings for the plugin.</p>';
</ hr>
<form action="webhook_url" method="post">
Webhook URL: <input type="text" name="webhook_url" value="">
<input type="save">
</form>
echo '</div>';

/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize($input)
{

if (!empty($input['url'])) {
$input['url'] = sanitize_text_field($input['url']);
}

if (!empty($input['name'])) {
$input['name'] = sanitize_text_field($input['name']);
}

if (!empty($input['avatar'])) {
$input['avatar'] = sanitize_text_field($input['avatar']);
}

if (!empty($input['message'])) {
$input['message'] = sanitize_text_field($input['message']);
}

return $input;
}

/**
* Print the Section text
*/
public function print_section_info()
{
print 'Webhook:';
}

/**
* Get the settings option array and print one of its values
*/
public function print_input_field($args)
{
printf(
'<input type="text" id="%s" name="%s[%s]" value="%s" />',
$args['name'],
$this->optionName,
$args['name'],
esc_attr($this->options[$args['name']])
);
if (isset($args['description'])) {
echo '<p>' . $args['description'] . '</p>';
}
}
}

if (is_admin()) {
// Load class for Options Page
$my_settings_page = new RHM_C2D_OptionsPage();
}

// Send Discord message when new event is created
add_action( 'tribe_events_single_event_before_the_content', 'send_discord_message_on_new_event' );
function send_discord_message_on_new_event() {
add_action('transition_post_status', 'send_discord_message_on_new_event', 10, 3);
function send_discord_message_on_new_event($new_status, $old_status, $post)
{

if ( 'tribe_events' !== get_post_type($post) ) {
return;
}

if ( $old_status === $new_status ) {
return;
}

// Check if this is a new event
if ( is_new_event() ) {
// Send Discord message
send_discord_message();
if ($new_status === 'publish') {
// Send Discord message with Link to website
$embed = [
'title' => get_the_title($post),
'url' => get_permalink($post),
'description' => get_the_excerpt($post),
'color' => 6570405,
];
$thumbnail = get_the_post_thumbnail_url($post);
if ($thumbnail) {
$embed['image'] = ['url' => $thumbnail];
}

send_discord_message($embed);
}
}

function is_new_event() {
// Check if event is new
return true;
}

function send_discord_message() {
function send_discord_message($embed = null)
{
// Get plugin settings
$webhook_url = get_option( 'webhook_url' );
$webhook_name = get_option( 'webhook_name' );
$webhook_avatar = get_option( 'webhook_avatar' );
$webhook_message = get_option( 'webhook_message' );
$options = get_option('rhm_c2d');
$webhook_url = $options['url'];
$webhook_name = $options['name'];
$webhook_avatar = $options['avatar'];
$webhook_message = $options['message'];
$mentions = $options['mentions'] ?: '';

$body = [
'username' => $webhook_name,
'avatar_url' => $webhook_avatar,
'content' => $webhook_message,
];

if (strlen($mentions) > 0) {
$body['allowed_mentions'] = [
"parse" => explode(',', $mentions),
];
$message_mentions = '';
foreach (explode(',', $mentions) as $mention) {
$message_mentions = '@' . $mention . ' ';
}
$body['content'] = $message_mentions . $body['content'];
}

if ($embed) {
$body['embeds'] = [$embed];
}

// Set up request data
$request_data = array(
'headers' => array(
$request_data = [
'headers' => [
'Content-Type' => 'application/json',
),
'body' => json_encode( array(
'username' => $webhook_name,
'avatar_url' => $webhook_avatar,
'content' => $webhook_message,
) ),
);
],
'body' => json_encode($body),
];

// Send request
$response = wp_remote_post( $webhook_url, $request_data );
$response = wp_remote_post($webhook_url, $request_data);

// Check for errors
if ( is_wp_error( $response ) ) {
if (is_wp_error($response)) {
// Log error message
error_log( 'Error sending Discord message: ' . $response->get_error_message() );
// if (is_admin()) {
// echo 'Fehler: ' . $response->get_error_message();
// }
error_log('Error sending Discord message: ' . $response->get_error_message());
} else {
// Log success message
error_log( 'Successfully sent Discord message.' );
// if (is_admin()) {
// echo 'Ausführen war erfolgreich.';
// }
error_log('Successfully sent Discord message.');
}
}
Loading