Skip to content

Commit

Permalink
Merge branch 'refs/heads/elementor-widgets'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ugoku committed Jul 29, 2024
2 parents 15b8895 + 114488b commit 4a75051
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 4 deletions.
5 changes: 1 addition & 4 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ You can enable basic Google Analytics integration for the booking of packages an

== Frequently Asked Questions ==

= Do you support Gutenberg? =
Yes, since version 2.2.0! Please make sure you use the latest version of the plugin and please report any bugs you encounter.

= Do you support Visual Composer, Elementor, Brizy, etc. ? =
We do not support page builders and have no plans to do so.
Integrating a book process is possible through Elementor. If you are creating a website through a different page builder, or want to integrate something else, please let Recras Support known. We might support it in the future.

= Does the plugin support network installations? =
Yes it does. You can set different Recras names (all settings, for that matter) for each site.
Expand Down
10 changes: 10 additions & 0 deletions src/Bookprocess.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public static function getProcesses(string $subdomain)
return $processes;
}

public static function optionsForElementorWidget()
{
$fmt = [];
$processes = self::getProcesses(get_option('recras_subdomain'));
foreach ($processes as $id => $process) {
$fmt[$id] = $process->name;
}
return $fmt;
}

/**
* Add the [recras-bookprocess] shortcode
*
Expand Down
21 changes: 21 additions & 0 deletions src/Elementor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Recras;

class Elementor
{
public static function addCategory($elements_manager)
{
$elements_manager->add_category(
'recras',
[
'title' => 'Recras',
'icon' => 'fa fa-plug', //TODO
]
);
}

public static function addWidgets($widgets_manager)
{
$widgets_manager->register(new Elementor\Bookprocess());
}
}
126 changes: 126 additions & 0 deletions src/Elementor/Bookprocess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Recras\Elementor;

use Recras\Arrangement;
use Recras\Plugin;

class Bookprocess extends \Elementor\Widget_Base
{
public function get_name(): string
{
return 'bookprocess';
}

public function get_title(): string
{
return __('Book process', Plugin::TEXT_DOMAIN);
}

public function get_icon(): string
{
return 'eicon-editor-list-ul';
}

public function get_categories(): array
{
return ['recras'];
}

protected function register_controls(): void
{
$this->start_controls_section(
'content',
[
'label' => __('Book process', Plugin::TEXT_DOMAIN),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
],
);

$options = \Recras\Bookprocess::optionsForElementorWidget();
$this->add_control(
'bp_id',
[
'type' => \Elementor\Controls_Manager::SELECT2,
'label' => __('Book process', Plugin::TEXT_DOMAIN),
'options' => $options,
'default' => count($options) === 1 ? reset($options) : null,
]
);

$bps = \Recras\Bookprocess::getProcesses(get_option('recras_subdomain'));
$bpsWithAcceptedFirstWidget = array_filter($bps, function ($bp) {
return in_array($bp->firstWidget, ['package']);
});

$this->add_control(
'initial_widget_value',
[
'type' => \Elementor\Controls_Manager::TEXT,
'label' => __('Prefill value for first widget? (optional)', Plugin::TEXT_DOMAIN),
'condition' => [
'bp_id' => array_map(function ($id) {
return (string) $id;
}, array_keys($bpsWithAcceptedFirstWidget)),
],
]
);

$this->add_control(
'hide_first_widget',
[
'type' => \Elementor\Controls_Manager::SWITCHER,
'label' => __('Hide first widget?', Plugin::TEXT_DOMAIN),
'condition' => [
'initial_widget_value!' => '',
],
]
);

$this->end_controls_section();
}

private function adminRender(array $settings): string
{
$options = \Recras\Bookprocess::optionsForElementorWidget();
$html = '';
$html .= sprintf(
__('Book process "%s" is integrated here.', Plugin::TEXT_DOMAIN),
$options[$settings['bp_id']]
);
if ($settings['initial_widget_value']) {
$packages = Arrangement::getPackages(get_option('recras_subdomain'));
$html .= '<br>';
if ($settings['hide_first_widget']) {
$pckId = (int) $settings['initial_widget_value'];
$pckName = isset($packages[$pckId]) ? $packages[$pckId]->arrangement : $pckId;
$html .= sprintf(
__('The first widget is hidden for the booker, and has an initial value of "%s".', Plugin::TEXT_DOMAIN),
$pckName
);
} else {
$html .= sprintf(
__('It has an initial value for the first widget of "%s".', Plugin::TEXT_DOMAIN),
$settings['initial_widget_value']
);
}
}
return $html;
}

protected function render(): void
{
$settings = $this->get_settings_for_display();
if (is_admin()) {
echo $this->adminRender($settings);
return;
}

$shortcode = '[' . \Recras\Plugin::SHORTCODE_BOOK_PROCESS;
$shortcode .= ' id="' . $settings['bp_id'] . '"';
$shortcode .= ' initial_widget_value="' . $settings['initial_widget_value'] . '"';
$shortcode .= ' hide_first_widget="' . $settings['hide_first_widget'] . '"';
$shortcode .= ']';
echo do_shortcode($shortcode);
}
}
4 changes: 4 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function __construct()
add_action('rest_api_init', [Gutenberg::class, 'addEndpoints']);
add_filter('block_categories_all', [Gutenberg::class, 'addCategory']);

// Elementor
add_action('elementor/elements/categories_registered', [Elementor::Class, 'addCategory']);
add_action('elementor/widgets/register', [Elementor::Class, 'addWidgets']);

// Load scripts
add_action('admin_enqueue_scripts', [$this, 'loadAdminScripts']);
add_action('wp_enqueue_scripts', [$this, 'loadScripts']);
Expand Down

0 comments on commit 4a75051

Please sign in to comment.