1 | <?php |
---|
2 | /** |
---|
3 | * Blocks for CiviCRM in WordPress |
---|
4 | * |
---|
5 | * @package BlocksForCiviCRM |
---|
6 | * @version 1.4.1 |
---|
7 | * @since 1.0.0 |
---|
8 | * |
---|
9 | */ |
---|
10 | |
---|
11 | add_action( 'init', function(){ |
---|
12 | if ( ! function_exists( 'register_block_type' ) ) { |
---|
13 | return; |
---|
14 | } |
---|
15 | if(!function_exists('civi_wp')){ |
---|
16 | add_action( 'admin_notices', function(){ |
---|
17 | echo '<div class="notice notice-warning"><p>'.__('Activating blocks for CiviCRM without CiviCRM does not make sens.', 'blocks-for-civicrm').'</p></div>'; |
---|
18 | }); |
---|
19 | return; |
---|
20 | } |
---|
21 | if (!civi_wp()->initialize()) { |
---|
22 | return; |
---|
23 | } |
---|
24 | |
---|
25 | wp_register_script( |
---|
26 | 'block-civicrm', |
---|
27 | plugins_url('build/civicrm/index.js', __DIR__ ), |
---|
28 | ['wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n', 'wp-core-data'], |
---|
29 | filemtime(plugin_dir_path(__DIR__).'build/civicrm/index.js') |
---|
30 | ); |
---|
31 | |
---|
32 | register_block_type( plugin_dir_path(__DIR__).'build/civicrm', array( |
---|
33 | 'editor_script' => 'block-civicrm', |
---|
34 | 'render_callback' => 'block_civicrm', |
---|
35 | ) ); |
---|
36 | } ); |
---|
37 | |
---|
38 | add_action ('enqueue_block_editor_assets', function(){ |
---|
39 | \WPReporting()->listen('blocks-for-civicrm'); |
---|
40 | if(!function_exists('civi_wp')){ |
---|
41 | return; |
---|
42 | } |
---|
43 | try{ |
---|
44 | if (!civi_wp()->initialize()) { |
---|
45 | return; |
---|
46 | } |
---|
47 | $CRM_Core_Form_ShortCode = new CRM_Core_Form_ShortCode(); |
---|
48 | $CRM_Core_Form_ShortCode->preProcess(); |
---|
49 | $components = $CRM_Core_Form_ShortCode->components; |
---|
50 | $options = $CRM_Core_Form_ShortCode->options; |
---|
51 | |
---|
52 | $id_options = []; |
---|
53 | if(function_exists('civicrm_api4') || function_exists('civicrm_api3')){ |
---|
54 | foreach($components as $component_id=>$component){ |
---|
55 | if($component['select'] !== null){ |
---|
56 | $id_options[$component_id] = [ |
---|
57 | [ |
---|
58 | 'label' => '', |
---|
59 | 'value' => '', |
---|
60 | ], |
---|
61 | ]; |
---|
62 | $idSelect = $component['select']; |
---|
63 | $result_count = 0; |
---|
64 | $result_values = []; |
---|
65 | |
---|
66 | if(function_exists('civicrm_api4')){ |
---|
67 | $api_data = [ |
---|
68 | 'select'=>[ |
---|
69 | 'id', |
---|
70 | 'title' |
---|
71 | ], |
---|
72 | 'orderBy' => ['title' => 'ASC'], |
---|
73 | 'checkPermissions' => false, |
---|
74 | ]; |
---|
75 | if($idSelect['entity'] == 'Pcp'){ |
---|
76 | $idSelect['entity'] = 'PCP'; |
---|
77 | } |
---|
78 | $result = civicrm_api4($idSelect['entity'], 'get', $api_data); |
---|
79 | $result_count = count($result); |
---|
80 | $result_values = $result; |
---|
81 | } |
---|
82 | elseif(function_exists('civicrm_api3')){ |
---|
83 | $api_data = [ |
---|
84 | 'return'=>'id, title', |
---|
85 | 'options' => [ |
---|
86 | 'limit' => 0, |
---|
87 | 'sort' => 'title', |
---|
88 | ], |
---|
89 | ]; |
---|
90 | $result = civicrm_api3($idSelect['entity'], 'get', $api_data); |
---|
91 | $result_count = $result['count']; |
---|
92 | $result_values = $result['values']; |
---|
93 | } |
---|
94 | |
---|
95 | if($result_count){ |
---|
96 | foreach ($result_values as $item) { |
---|
97 | $id_options[$component_id][] = [ |
---|
98 | 'label' => $item['title'], |
---|
99 | 'value' => $item['id'], |
---|
100 | ]; |
---|
101 | } |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | $option_labels = include __DIR__.'/options-labels.php'; |
---|
108 | foreach ($options as $option) { |
---|
109 | $option_labels[$option['key']] = __(ucfirst($option['key']), 'blocks-for-civicrm'); |
---|
110 | } |
---|
111 | |
---|
112 | wp_add_inline_script('blocks-for-civicrm-civicrm-editor-script', 'var block_civicrm = '.wp_json_encode([ |
---|
113 | 'i18n' => [ |
---|
114 | 'title'=>__('CiviCRM component', 'blocks-for-civicrm'), |
---|
115 | 'description'=>__('Block for CiviCRM', 'blocks-for-civicrm'), |
---|
116 | 'component'=>__('Component', 'blocks-for-civicrm'), |
---|
117 | 'options'=>$option_labels, |
---|
118 | ], |
---|
119 | 'components' => $components, |
---|
120 | 'options' => $options, |
---|
121 | 'id_options' => $id_options, |
---|
122 | ]), 'before'); |
---|
123 | } |
---|
124 | catch(\Throwable $e){ |
---|
125 | \WPReporting()->send($e, 'blocks-for-civicrm'); |
---|
126 | } |
---|
127 | catch(\Exception $e){ |
---|
128 | \WPReporting()->send($e, 'blocks-for-civicrm'); |
---|
129 | } |
---|
130 | \WPReporting()->stop(); |
---|
131 | }); |
---|
132 | |
---|
133 | function block_civicrm($attributes, $content=''){ |
---|
134 | if(is_admin()){ |
---|
135 | return 'civicrm preview'; |
---|
136 | } |
---|
137 | \WPReporting()->listen('blocks-for-civicrm'); |
---|
138 | try{ |
---|
139 | $class_name = isset($attributes['className']) ? $attributes['className'] : ''; |
---|
140 | $atts = $attributes; |
---|
141 | $shortcode_atts = ''; |
---|
142 | $option_labels = include __DIR__.'/options-labels.php'; |
---|
143 | foreach($attributes as $key=>$value){ |
---|
144 | if(!isset($option_labels[$key]) || empty($value)){ |
---|
145 | unset($attributes[$key]); |
---|
146 | } |
---|
147 | else{ |
---|
148 | $shortcode_atts.=' '.$key.'="'.$value.'"'; |
---|
149 | } |
---|
150 | } |
---|
151 | $shortcode = '[civicrm'.$shortcode_atts.']'; |
---|
152 | } |
---|
153 | catch(\Throwable $e){ |
---|
154 | \WPReporting()->send($e, 'blocks-for-civicrm'); |
---|
155 | } |
---|
156 | catch(\Exception $e){ |
---|
157 | \WPReporting()->send($e, 'blocks-for-civicrm'); |
---|
158 | } |
---|
159 | \WPReporting()->stop(); |
---|
160 | |
---|
161 | // If we are in editor preview context |
---|
162 | if (defined('REST_REQUEST') && REST_REQUEST && filter_input(INPUT_GET, 'context') === 'edit'){ |
---|
163 | |
---|
164 | $post = get_post(filter_input(INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT)); |
---|
165 | // Preprocess Shortcode attributes. |
---|
166 | $args = \civi_wp()->shortcodes->preprocess_atts($attributes); |
---|
167 | |
---|
168 | // Check for pathless Shortcode. |
---|
169 | if (empty($args['q'])) { |
---|
170 | $content = '<p>' . __('This Shortcode could not be handled. It could be malformed or used incorrectly.', 'blocks-for-civicrm') . '</p>'; |
---|
171 | return apply_filters('civicrm_shortcode_get_markup', $content, $atts, $args, 'single'); |
---|
172 | } |
---|
173 | |
---|
174 | foreach ($args as $key => $value) { |
---|
175 | if ($value !== NULL) { |
---|
176 | set_query_var($key, $value); |
---|
177 | $_REQUEST[$key] = $_GET[$key] = $value; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | $argdata = explode('/', $args['q']); |
---|
182 | |
---|
183 | if (!\civi_wp()->shortcodes->civi->initialize()) { |
---|
184 | return 'Civi not initialized'; |
---|
185 | } |
---|
186 | |
---|
187 | // Start buffering. |
---|
188 | ob_start(); |
---|
189 | // Now, instead of echoing, Shortcode output ends up in buffer. |
---|
190 | \CRM_Core_Invoke::invoke($argdata); |
---|
191 | // Save the output and flush the buffer. |
---|
192 | $content = ob_get_clean(); |
---|
193 | if(empty($content)){ |
---|
194 | $content = $shortcode; |
---|
195 | } |
---|
196 | return $content; |
---|
197 | } |
---|
198 | return '<div class="block-civicrm '.esc_attr($class_name).'">'.$shortcode.'</div>'; |
---|
199 | } |
---|