PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.7
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.7
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / app / Functions / FluentExtendApi.php

FluentExtendApi.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.7.7, at app/Functions/FluentExtendApi.php

72 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Functions;
4
5 class FluentExtendApi
6 {
7 /**
8 * Add a meta box for the space provider.
9 *
10 * @param string $provider The provider name.
11 * @param array $args Arguments for the meta box.
12 * $args['section_title'] string The title of the meta box.
13 * $args['fields_callback'] callable A callback function to get the form fields array
14 * $args['data_callback'] callable A callback function to get the data for the meta box form fields
15 * $args['save_callback'] callable A callback function to save the data from the form fields
16 * $@param array $objectTypes The object types to which the meta box should be added. Possible values are 'space' and 'course'.
17 * @return void
18 */
19 public function addMetaBox(string $provider, array $args = [], array $objectTypes = []): void
20 {
21 if (!$objectTypes) {
22 return;
23 }
24
25 $validObjectTypes = ['space', 'course'];
26 $objectTypes = array_intersect($objectTypes, $validObjectTypes);
27 if (!$objectTypes) {
28 return;
29 }
30
31 foreach ($objectTypes as $objectType) {
32 if (!in_array($objectType, $validObjectTypes)) {
33 continue;
34 }
35
36 if (!isset($args['section_title']) || !isset($args['fields_callback']) || !isset($args['data_callback']) || !isset($args['save_callback'])) {
37 throw new \InvalidArgumentException('Invalid arguments provided for addMetaBox.');
38 }
39
40 $hookPrefix = 'fluent_community/' . $objectType . '/';
41 $this->addObjectMeta($hookPrefix, $provider, $args);
42 }
43 }
44
45 private function addObjectMeta($hookPrefix, $provider, $args)
46 {
47 add_filter($hookPrefix . 'meta_fields', function ($fields, $model) use ($provider, $args) {
48 $formFields = \is_callable($args['fields_callback']) ? \call_user_func($args['fields_callback'], $model) : [];
49 if (!$formFields) {
50 return $fields;
51 }
52
53 $settings = \is_callable($args['data_callback']) ? \call_user_func($args['data_callback'], $model) : [];
54
55 $fields[$provider] = [
56 'section_title' => $args['section_title'],
57 'settings' => $settings,
58 'fields' => $formFields
59 ];
60
61 return $fields;
62 }, 10, 2);
63
64 add_action($hookPrefix . 'update_meta_settings_' . $provider, function ($settings, $model) use ($args) {
65 if (\is_callable($args['save_callback'])) {
66 \call_user_func($args['save_callback'], $settings, $model);
67 }
68 }, 10, 2);
69 }
70
71 }
72