1: <?php
2:
3: 4: 5:
6: class Quform_Block
7: {
8: 9: 10:
11: protected $controller;
12:
13: 14: 15:
16: protected $repository;
17:
18: 19: 20: 21:
22: public function __construct(Quform_Form_Controller $controller, Quform_Repository $repository)
23: {
24: $this->controller = $controller;
25: $this->repository = $repository;
26: }
27:
28: 29: 30:
31: public function register()
32: {
33: if ( ! function_exists('register_block_type')) {
34: return;
35: }
36:
37: $asset = include QUFORM_ADMIN_PATH . '/js/block/build/index.asset.php';
38:
39: wp_register_script(
40: 'quform-block',
41: Quform::adminUrl('js/block/build/index.js'),
42: $asset['dependencies'],
43: $asset['version'],
44: true
45: );
46:
47: wp_localize_script('quform-block', 'quformBlockL10n', array(
48: 'forms' => $this->getForms()
49: ));
50:
51: if (function_exists('gutenberg_get_jed_locale_data')) {
52: $localeData = json_encode(gutenberg_get_jed_locale_data('quform'));
53:
54: wp_add_inline_script(
55: 'quform-block',
56: "wp.i18n.setLocaleData($localeData, 'quform')",
57: 'before'
58: );
59: }
60:
61: if (function_exists('wp_set_script_translations')) {
62: wp_set_script_translations('quform-block', 'quform', QUFORM_PATH . '/languages');
63: }
64:
65: register_block_type('quform/form', array(
66: 'editor_script' => 'quform-block',
67: 'render_callback' => array($this, 'render')
68: ));
69: }
70:
71: 72: 73: 74: 75:
76: protected function getForms()
77: {
78: $orderBy = get_user_meta(get_current_user_id(), 'quform_forms_order_by', true);
79: $order = get_user_meta(get_current_user_id(), 'quform_forms_order', true);
80: $forms = array(array('value' => '', 'label' => __('Please select', 'quform')));
81:
82: foreach ($this->repository->all(true, $orderBy, $order) as $form) {
83: $forms[] = array('value' => $form['id'], 'label' => $form['name']);
84: }
85:
86: return $forms;
87: }
88:
89: 90: 91: 92: 93: 94: 95:
96: public function render($attributes, $content)
97: {
98: $options = wp_parse_args($attributes, array(
99: 'id' => '',
100: 'show_title' => true,
101: 'show_description' => true,
102: 'popup' => false,
103: 'content' => '',
104: 'values' => ''
105: ));
106:
107: return $this->controller->form($options);
108: }
109: }
110: