gancio-upstream/wp-plugin/add-support-to-plugins-guide.md
2024-04-26 15:31:31 +02:00

134 lines
4.7 KiB
Markdown

# Guide: adding new event manager plugins
In order to provide more opotions to the users of the plugin you can add new event manager plugins to the list of supported ones, currently:
- [Event Organizer](https://wp-event-organiser.com/)
- [The Events Calendar](https://theeventscalendar.com/products/wordpress-events-calendar/)
## Step 1: Findout the hooks you need
First thing is to find the hooks used by the plugin to insert code in the event publishing/editing lifecycle. This is usually a bunch of functions that let you insert a callback right after the creation/update/deletion of the event post and that provides you with the context of the event. You have two places for searching this:
- The documentation of the plugin
- The code itself
In this example [we searched in the plugin documentation](https://docs.theeventscalendar.com/reference/hooks/tec_events_custom_tables_v1_after_insert_event/)
```php
do_action( 'tec_events_custom_tables_v1_after_insert_event', TECEventsCustom_TablesV1Updatesnumeric $post_id )
// When we have created a new event, fire this action with the post ID.
```
## Step 2: Fork the Gancio project and code the plugin extension
### Fork the project and start the local development environment
In order to contribute with coding you'll need to setup your development environment unfortunatly we won't be convering this topic in this guide. Contact us through the repo if you need help with the setup.
Once you have the development environment fork the repo to create your own version of the wpgancio plugin.
### Suscribe to the hooks and format input
Great! We are ready to start. Now you need to engage with the plugin lifecycle through hooks, to do that add the hook name to the wpgancio plugin function in the `./oauth.php` file:
```php
add_action('tec_events_custom_tables_v1_after_insert_event', 'wpgancio_tec_save_event', 15);
function wpgancio_tec_save_event($post_id) {
//...
}
```
Now you just need to adapt the function to the event manager plugin. We'll walk you through our process with The Events Calendar plugin:
### Print the post object
To understand how it is construtcted.
```php
function wpgancio_tec_save_event($post_id) {
$event = get_post( $post_id );
print_r($event);
// WP_Post Object (
// [ID] => 55
// [post_author] => 1
// [post_date] => 2024-04-22 19:28:34
// [post_date_gmt] => 2024-04-22 19:28:34
// [post_content] => test
// [post_title] => Test evento
// [post_excerpt] =>
// [post_status] => publish
// [comment_status] => open
// [ping_status] => closed
// [post_password] =>
// [post_name] => test-evento
// [to_ping] =>
// [pinged] =>
// [post_modified] => 2024-04-22 19:28:34
// [post_modified_gmt] => 2024-04-22 19:28:34
// [post_content_filtered] =>
// [post_parent] => 0
// [guid] => http://localhost:8000/?post_type=tribe_events&p=55
// [menu_order] => 0
// [post_type] => tribe_events
// [post_mime_type] =>
// [comment_count] => 0
// [filter] => raw
// )
//...
```
### Change tag taxonomy name
Then we changed the tags taxonomy name to match the one used by the TEC plugin: `post_tag`.
### Get the start date time as timestamp
To send the event to Gancio you'll need the event start date and time in timestamp format, in order to do that we have looked at the plugin code and found that the following line of code generates it:
```php
// The timestamp generated byt the plugin function is 2h shifted, substract 7200 s --> 2h
strtotime(tribe_get_start_date( $event, true, Tribe__Date_Utils::DBDATETIMEFORMAT )) - 7200;
```
### Venue details
Then you'll need the place name and address, in our case, searching in the docs we found the following functions:
```php
// get place details
$venue_name = tribe_get_venue( $event->id );
$venue_address = tribe_get_address($event->id) . ', ' . tribe_get_zip($event->id) . ', ' . tribe_get_stateprovince($event->id) . ', ' . tribe_get_country($event->id);
```
### Prepare the body and you are done!
Now we are ready to prepare the body of the request and then we can use the plugins function to make the request call.
```php
// prepare the request's body
$body = array (
'title' => $event->post_title,
'tags' => $tags,
'description' => $event->post_content,
'start_datetime' => intval($start_datetime),
'place_name' => $venue_name,
'place_address' => $venue_address
);
// add image if specified
$image_url = get_the_post_thumbnail_url($post_id);
if ($image_url) {
$body['image_url'] = esc_url($image_url);
}
wpgancio_send_event_to_instance($body, $post_id);
```
## Step 3: create a merge request