|
2 | 2 |
|
3 | 3 | namespace App\Hooks; |
4 | 4 |
|
| 5 | +use Roots\WPConfig\Config; |
| 6 | +use Timber\Timber; |
| 7 | + |
5 | 8 | class Security { |
6 | | - public static function init() : void { |
| 9 | + public static function init(): void { |
7 | 10 | add_action('rest_api_init', self::cors_headers(...)); |
| 11 | + add_action('wp_head', array(Security::class, 'add_recaptcha')); |
8 | 12 |
|
9 | 13 | remove_action('wp_head', 'rest_output_link_wp_head', 10); |
10 | 14 | remove_action('wp_head', 'wp_oembed_add_discovery_links', 10); |
11 | 15 | remove_action('template_redirect', 'rest_output_link_header', 10); |
12 | 16 |
|
13 | | - remove_action( 'admin_init', '_maybe_update_core' ); |
14 | | - remove_action( 'wp_version_check', 'wp_version_check' ); |
| 17 | + remove_action('admin_init', '_maybe_update_core'); |
| 18 | + remove_action('wp_version_check', 'wp_version_check'); |
15 | 19 |
|
16 | | - remove_action( 'load-plugins.php', 'wp_update_plugins' ); |
17 | | - remove_action( 'load-update.php', 'wp_update_plugins' ); |
18 | | - remove_action( 'load-update-core.php', 'wp_update_plugins' ); |
19 | | - remove_action( 'admin_init', '_maybe_update_plugins' ); |
20 | | - remove_action( 'wp_update_plugins', 'wp_update_plugins' ); |
| 20 | + remove_action('load-plugins.php', 'wp_update_plugins'); |
| 21 | + remove_action('load-update.php', 'wp_update_plugins'); |
| 22 | + remove_action('load-update-core.php', 'wp_update_plugins'); |
| 23 | + remove_action('admin_init', '_maybe_update_plugins'); |
| 24 | + remove_action('wp_update_plugins', 'wp_update_plugins'); |
21 | 25 |
|
22 | | - remove_action( 'load-themes.php', 'wp_update_themes' ); |
23 | | - remove_action( 'load-update.php', 'wp_update_themes' ); |
24 | | - remove_action( 'load-update-core.php', 'wp_update_themes' ); |
25 | | - remove_action( 'admin_init', '_maybe_update_themes' ); |
26 | | - remove_action( 'wp_update_themes', 'wp_update_themes' ); |
| 26 | + remove_action('load-themes.php', 'wp_update_themes'); |
| 27 | + remove_action('load-update.php', 'wp_update_themes'); |
| 28 | + remove_action('load-update-core.php', 'wp_update_themes'); |
| 29 | + remove_action('admin_init', '_maybe_update_themes'); |
| 30 | + remove_action('wp_update_themes', 'wp_update_themes'); |
27 | 31 |
|
28 | | - remove_action( 'update_option_WPLANG', 'wp_clean_update_cache', 10, 0 ); |
29 | | - remove_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' ); |
30 | | - remove_action( 'init', 'wp_schedule_update_checks' ); |
31 | | - remove_action( 'wp_delete_temp_updater_backups', 'wp_delete_all_temp_backups' ); |
32 | | - } |
| 32 | + remove_action('update_option_WPLANG', 'wp_clean_update_cache', 10, 0); |
| 33 | + remove_action('wp_maybe_auto_update', 'wp_maybe_auto_update'); |
| 34 | + remove_action('init', 'wp_schedule_update_checks'); |
| 35 | + remove_action('wp_delete_temp_updater_backups', 'wp_delete_all_temp_backups'); |
| 36 | + } |
33 | 37 |
|
34 | | - public static function cors_headers() : void { |
| 38 | + public static function cors_headers(): void { |
35 | 39 | header("Access-Control-Allow-Origin: https://*.modycloud.test"); |
36 | 40 | header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); |
37 | 41 | header("Access-Control-Allow-Headers: Authorization, Content-Type"); |
38 | 42 | header("Access-Control-Allow-Credentials: true"); |
39 | 43 | } |
| 44 | + |
| 45 | + public static function add_recaptcha(): void { |
| 46 | + if ( is_singular() ) { |
| 47 | + global $post; |
| 48 | + $blocks = parse_blocks( $post->post_content ); |
| 49 | + if( $blocks && is_array( $blocks ) ){ |
| 50 | + |
| 51 | + $add_script = function() { |
| 52 | + $recaptcha_site_key = Config::get('RECAPTCHA_KEY'); |
| 53 | + $recaptcha_site_secret = Config::get('RECAPTCHA_SECRET'); |
| 54 | + |
| 55 | + if(!$recaptcha_site_key || !$recaptcha_site_secret) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + echo Timber::compile('@app/components/tags/script.twig', [ |
| 60 | + 'src' => add_query_arg([ |
| 61 | + 'render' => $recaptcha_site_key, |
| 62 | + ], 'https://www.google.com/recaptcha/api.js'), |
| 63 | + 'defer' => true, |
| 64 | + ]); |
| 65 | + }; |
| 66 | + |
| 67 | + $protected_pages = array( |
| 68 | + 'app/auth' |
| 69 | + ); |
| 70 | + foreach( $blocks as $block ){ |
| 71 | + if( in_array($block['blockName'], $protected_pages )){ |
| 72 | + $add_script(); |
| 73 | + return; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
40 | 79 | } |
0 commit comments