| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Widgets; |
| 4 |
|
| 5 |
use BitCode\BitForm\GlobalHelper; |
| 6 |
|
| 7 |
if (!defined('ABSPATH')) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* BitformBricksWidget class. |
| 13 |
* |
| 14 |
* @package BitCode\BitForm\Widgets |
| 15 |
* |
| 16 |
* @since |
| 17 |
* |
| 18 |
* @version |
| 19 |
* |
| 20 |
* @description |
| 21 |
* This class is used to create a Bitform widget for Bricks Builder. |
| 22 |
* It extends the \Bricks\Element class and implements the necessary methods to register the widget. |
| 23 |
* |
| 24 |
* @see https://academy.bricksbuilder.io/article/create-your-own-elements/ |
| 25 |
*/ |
| 26 |
class BitformBricksWidget extends \Bricks\Element |
| 27 |
{ |
| 28 |
public $category = 'general'; |
| 29 |
public $name = 'Bit Form'; |
| 30 |
public $icon = 'ti-layout-cta-left'; |
| 31 |
|
| 32 |
public $tag = 'form'; |
| 33 |
|
| 34 |
public function get_label() |
| 35 |
{ |
| 36 |
return esc_html__('Bit Form', 'bricks'); |
| 37 |
} |
| 38 |
|
| 39 |
public function get_keywords() |
| 40 |
{ |
| 41 |
return [ |
| 42 |
'bitform', |
| 43 |
'bitforms', |
| 44 |
'form', |
| 45 |
'bitform widget', |
| 46 |
'form widget', |
| 47 |
'contact forms', |
| 48 |
'bricks form', |
| 49 |
'bit form', |
| 50 |
'form builder', |
| 51 |
'shortcode', |
| 52 |
]; |
| 53 |
} |
| 54 |
|
| 55 |
// Set builder controls |
| 56 |
public function set_controls() |
| 57 |
{ |
| 58 |
$options = []; |
| 59 |
if (is_callable([GlobalHelper::class, 'getForms'])) { |
| 60 |
$forms = GlobalHelper::getForms(); |
| 61 |
if (is_array($forms)) { |
| 62 |
$options = $forms; |
| 63 |
} |
| 64 |
} |
| 65 |
$this->controls['form-list'] = [ |
| 66 |
'label' => esc_html__('Form list', 'bricks'), |
| 67 |
'type' => 'select', |
| 68 |
'options' => $options, |
| 69 |
'inline' => true, |
| 70 |
'clearable' => false, |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
public function enqueue_scripts() |
| 75 |
{ |
| 76 |
// for custom script |
| 77 |
} |
| 78 |
|
| 79 |
private function getStyle($formId) |
| 80 |
{ |
| 81 |
$style = ''; |
| 82 |
if (file_exists(BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles')) { |
| 83 |
$cssFile = BITFORMS_CONTENT_DIR . DIRECTORY_SEPARATOR . 'form-styles' . DIRECTORY_SEPARATOR . "bitform-{$formId}-formid" . '.css'; |
| 84 |
|
| 85 |
if (file_exists($cssFile)) { |
| 86 |
$style = file_get_contents($cssFile); |
| 87 |
} else { |
| 88 |
$style = ''; |
| 89 |
} |
| 90 |
} |
| 91 |
return "<style>$style</style>"; |
| 92 |
} |
| 93 |
|
| 94 |
public function render() |
| 95 |
{ |
| 96 |
echo "<div {$this->render_attributes('_root')}>"; |
| 97 |
if (empty($this->settings['form-list'])) { |
| 98 |
echo $this->render_element_placeholder([ // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 99 |
|
| 100 |
'icon-class' => $this->icon, |
| 101 |
'title' => esc_html__('No form selected', 'bit-form'), |
| 102 |
'description' => esc_html__('Please select a form from the Form List available in the Bit Form element settings.', 'bit-form'), |
| 103 |
|
| 104 |
// Legacy attribute |
| 105 |
'text' => esc_html__('No form selected', 'bit-form') |
| 106 |
]); |
| 107 |
} |
| 108 |
if (!empty($this->settings['form-list'])) { |
| 109 |
echo $this->getStyle($this->settings['form-list']); |
| 110 |
echo do_shortcode('[bitform id="' . $this->settings['form-list'] . '"]'); |
| 111 |
} |
| 112 |
echo '</div>'; |
| 113 |
} |
| 114 |
} |
| 115 |
|