| 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 |
|