PluginProbe
Simple Analytics / 1.110
Simple Analytics v1.110
1.110 1.109 1.108 1.107 1.106 1.73 1.74 1.75 1.76 1.77 1.78 1.79 1.8 1.80 1.81 1.82 1.83 1.84 1.85 1.86 1.87 1.88 1.89 1.9 1.90 All 99 releases
simpleanalytics / src / Settings / Concerns / WordPressPageIntegration.php

WordPressPageIntegration.php in Simple Analytics 1.110, at src/Settings/Concerns/WordPressPageIntegration.php

62 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SimpleAnalytics\Settings\Concerns;
4
5 use SimpleAnalytics\Settings\Blocks\Fields\Field;
6 use SimpleAnalytics\Settings\Tab;
7 use SimpleAnalytics\UI\PageLayoutComponent;
8
9 trait WordPressPageIntegration
10 {
11 /** @return Tab[] */
12 abstract function getTabs(): array;
13
14 public function register(): void
15 {
16 add_action('admin_menu', [$this, 'wpAddMenu']);
17 add_action('admin_init', [$this, 'wpAddFields']);
18 }
19
20 public function getOptionGroup(Tab $tab): string
21 {
22 return $this->getSlug() . '-' . $tab->getSlug();
23 }
24
25 /** @internal */
26 public function wpAddMenu(): void
27 {
28 add_options_page($this->getTitle(), $this->getTitle(), 'manage_options', $this->getSlug(), (new PageLayoutComponent($this)));
29 }
30
31 /** @internal */
32 public function wpAddFields(): void
33 {
34 foreach ($this->getTabs() as $tab) {
35 $this->registerTab($tab);
36 }
37 }
38
39 protected function registerTab(Tab $tab): void
40 {
41 $fields = array_filter($tab->getBlocks(), function ($block) {
42 return $block instanceof Field;
43 });
44
45 foreach ($fields as $field) {
46 $this->registerField($field, $tab);
47 }
48 }
49
50 protected function registerField(Field $field, Tab $tab): void
51 {
52 register_setting(
53 $this->getOptionGroup($tab),
54 $field->getKey(),
55 [
56 'type' => $field->getValueType(),
57 'sanitize_callback' => $field->getValueSanitizer(),
58 ]
59 );
60 }
61 }
62