PluginProbe
PhastPress / 3.12
PhastPress v3.12
3.12 3.11 1.75 1.76 1.77 1.78 1.79 1.80 1.81 1.82 1.83 1.84 1.85 1.86 1.87 1.88 1.89 1.90 1.91 1.92 1.93 1.94 1.95 1.96 1.97 All 119 releases
phastpress / classes / Compat / TwentyTwentyOneDarkMode.php

TwentyTwentyOneDarkMode.php in PhastPress 3.12, at classes/Compat/TwentyTwentyOneDarkMode.php

64 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Kibo\PhastPlugins\PhastPress\Compat;
3
4 use Twenty_Twenty_One_Dark_Mode;
5 use ReflectionClass;
6 use Exception;
7
8 class TwentyTwentyOneDarkMode {
9 public function setup() {
10 add_action('after_setup_theme', function () {
11 $this->afterSetupTheme();
12 }, 20);
13 }
14
15 private function afterSetupTheme() {
16 if (!class_exists(Twenty_Twenty_One_Dark_Mode::class)) {
17 return;
18 }
19
20 Log::add('twentytwentyone', 'adding script to set is-dark-theme class early');
21
22 $darkMode = $this->findDarkModeObject();
23 if (!$darkMode) {
24 Log::add('twentytwentyone', 'could not find hook');
25 return;
26 }
27
28 try {
29 $cls = new ReflectionClass($darkMode);
30 $meth = $cls->getMethod('switch_should_render');
31 if (!$meth->invoke($darkMode)) {
32 Log::add('twentytwentyone', 'switch won\'t render');
33 return;
34 }
35 } catch (Exception $e) {
36 Log::add('twentytwentyone', 'unable to call Twenty_Twenty_One_Dark_Mode::switch_should_render; check error log');
37 error_log(sprintf(
38 'Caught %s while trying to call Twenty_Twenty_One_Dark_Mode::switch_should_render: (%d) %s',
39 get_class($e),
40 $e->getCode(),
41 $e->getMessage()
42 ));
43 }
44
45 add_filter('wp_body_open', function () {
46 echo phastpress_script(file_get_contents(__FILE__ . '.js'), ['data-phast-no-defer' => '']);
47 });
48 }
49
50 private function findDarkModeObject() {
51 foreach ($GLOBALS['wp_filter']['wp_footer'] as $priority => $actions) {
52 foreach ($actions as $action) {
53 if (is_array($action['function'])
54 && sizeof($action['function']) == 2
55 && $action['function'][0] instanceof Twenty_Twenty_One_Dark_Mode
56 ) {
57 return $action['function'][0];
58 }
59 }
60 }
61 return null;
62 }
63 }
64