PluginProbe
Site Reviews / trunk
Site Reviews vtrunk
8.3.1 8.3.0 8.2.2 8.2.1 8.2.0 8.1.0 8.0.13 8.0.12 8.0.11 trunk 1.2.2 2.17.1 3.5.4 4.7.0 5.25.1 6.11.8 7.0.10 7.0.11 7.0.12 7.0.13 7.0.14 7.0.15 7.0.16 7.0.17 7.0.18 All 54 releases
site-reviews / plugin / Controllers / MainController.php

MainController.php in Site Reviews trunk, at plugin/Controllers/MainController.php

183 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace GeminiLabs\SiteReviews\Controllers;
4
5 use GeminiLabs\SiteReviews\Addons\Compat;
6 use GeminiLabs\SiteReviews\Commands\RegisterPostMeta;
7 use GeminiLabs\SiteReviews\Commands\RegisterPostType;
8 use GeminiLabs\SiteReviews\Commands\RegisterShortcodes;
9 use GeminiLabs\SiteReviews\Commands\RegisterTaxonomy;
10 use GeminiLabs\SiteReviews\Commands\RegisterWidgets;
11 use GeminiLabs\SiteReviews\Database\OptionManager;
12 use GeminiLabs\SiteReviews\Database\Tables;
13 use GeminiLabs\SiteReviews\Helpers\Arr;
14 use GeminiLabs\SiteReviews\Install;
15
16 class MainController extends AbstractController
17 {
18 /**
19 * switch_to_blog() has run before this hook is triggered.
20 *
21 * @see http://developer.wordpress.org/reference/functions/wp_uninitialize_site/
22 *
23 * @param string[] $tables
24 *
25 * @return string[]
26 *
27 * @filter wpmu_drop_tables:999
28 */
29 public function filterDropTables(array $tables): array
30 {
31 // Custom tables have foreign indexes so they must be removed first!
32 foreach (glsr(Tables::class)->tables() as $classname) {
33 $table = glsr($classname);
34 $tables = Arr::prepend($tables, $table->tablename, $table->name($prefixName = true));
35 }
36 return $tables;
37 }
38
39 /**
40 * @action wp_initialize_site:999
41 */
42 public function installOnNewSite(\WP_Site $site): void
43 {
44 if (is_plugin_active_for_network(glsr()->basename)) {
45 glsr(Install::class)->runOnSite($site->blog_id);
46 }
47 }
48
49 /**
50 * @param ?string $data We are not enforcing the type because the "wp_footer" hook does not have a parameter
51 *
52 * @action admin_footer
53 * @action wp_footer
54 */
55 public function logOnce($data = ''): void
56 {
57 if ('update.php' !== $data) {
58 glsr_log()->logOnce();
59 }
60 }
61
62 /**
63 * Initialize the Application settings config and defaults.
64 *
65 * @action init:5
66 */
67 public function onInit(): void
68 {
69 $defaults = glsr()->defaults();
70 glsr(OptionManager::class)->mergeDefaults($defaults);
71 glsr(OptionManager::class)->updateVersion();
72 }
73
74 /**
75 * @action site-reviews/migration/end
76 */
77 public function onMigrationEnd(): void
78 {
79 // This method persists what it reads. The raw migration writes can
80 // leave the options cache stale; a stale read overwrites the settings.
81 OptionManager::flushSettingsCache();
82 $settings = glsr(OptionManager::class)->reset(); // fresh composed view
83 $settings = glsr(OptionManager::class)->clean($settings);
84 glsr(OptionManager::class)->replace($settings); // persists addon settings to their own options
85 }
86
87 /**
88 * @action parse_query
89 */
90 public function parseAssignedPostTypesInQuery(\WP_Query $query): void
91 {
92 if (glsr()->prefix.'assigned_posts' !== $query->get('post_type')) {
93 return;
94 }
95 $postTypes = get_post_types([
96 '_builtin' => false,
97 'public' => true,
98 'show_in_rest' => true,
99 'show_ui' => true,
100 ]);
101 $postTypes[] = 'post';
102 $postTypes[] = 'page';
103 $query->is_archive = false;
104 $query->is_post_type_archive = false;
105 $query->set('post_type', array_map('sanitize_key', array_values($postTypes)));
106 }
107
108 /**
109 * @action plugins_loaded:-50
110 */
111 public function registerAddons(): void
112 {
113 glsr()->action('addon/register', glsr(Compat::class)); // @compat
114 glsr()->action('premium/register', glsr());
115 }
116
117 /**
118 * Languages are loaded before "init" because the setting config uses translated strings.
119 *
120 * @action after_setup_theme
121 */
122 public function registerLanguages(): void
123 {
124 load_plugin_textdomain(glsr()->id, false,
125 trailingslashit(plugin_basename(glsr()->path()).'/'.glsr()->languages)
126 );
127 }
128
129 /**
130 * @action init
131 */
132 public function registerPostMeta(): void
133 {
134 $this->execute(new RegisterPostMeta());
135 }
136
137 /**
138 * @action init
139 */
140 public function registerPostType(): void
141 {
142 $this->execute(new RegisterPostType());
143 }
144
145 /**
146 * @action init:5
147 */
148 public function registerReviewTypes(): void
149 {
150 $types = glsr()->filterArray('review/types', []);
151 $types = wp_parse_args($types, [
152 'local' => _x('Local Review', 'admin-text', 'site-reviews'),
153 ]);
154 glsr()->store('review_types', $types);
155 }
156
157 /**
158 * @action init
159 */
160 public function registerShortcodes(): void
161 {
162 $this->execute(new RegisterShortcodes());
163 }
164
165 /**
166 * @action init
167 */
168 public function registerTaxonomy(): void
169 {
170 $this->execute(new RegisterTaxonomy());
171 }
172
173 /**
174 * @action widgets_init
175 */
176 public function registerWidgets(): void
177 {
178 if (glsr()->filterBool('register/widgets', true)) {
179 $this->execute(new RegisterWidgets());
180 }
181 }
182 }
183