PluginProbe
GDPRess | Eliminate external requests to increase GDPR compliance / trunk
GDPRess | Eliminate external requests to increase GDPR compliance vtrunk
trunk 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1
gdpr-press / src / Admin / Ajax.php

Ajax.php in GDPRess | Eliminate external requests to increase GDPR compliance trunk, at src/Admin/Ajax.php

133 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package GDPRess
4 * @author Daan van den Bergh
5 * https://daan.dev
6 */
7
8 namespace GDPRess\Admin;
9
10 class Ajax {
11
12 /**
13 * Set fields.
14 *
15 * @return void
16 */
17 public function __construct() {
18 $this->init();
19 }
20
21 /**
22 * Filters & Hooks.
23 *
24 * @return void
25 */
26 private function init() {
27 add_action( 'wp_ajax_gdpress_fetch', [ $this, 'fetch' ] );
28 add_action( 'wp_ajax_gdpress_flush', [ $this, 'flush' ] );
29 }
30
31 /**
32 * Basic logic for fetching external resources.
33 *
34 * @return void
35 */
36 public function fetch() {
37 check_ajax_referer( Settings::GDPRESS_ADMIN_PAGE, 'nonce' );
38
39 if ( ! current_user_can( 'manage_options' ) ) {
40 wp_die( __( "Sorry, you're not allowed to do this.", 'gdpr-press' ) );
41 }
42
43 $this->do_flush();
44
45 /**
46 * Trigger a request to the frontend, so the 'template_redirect' action is initiated.
47 *
48 * We're adding the gdpress parameter to ensure proper execution.
49 *
50 * @see OutputProcessor::init()
51 */
52 $site_url = get_home_url() . '?gdpress';
53 $response = wp_remote_get( $site_url, [ 'timeout' => 60 ] );
54
55 if ( is_wp_error( $response ) ) {
56 /**
57 * Dies eventually, so no need to return.
58 *
59 * @var \WP_Error $response
60 */
61 wp_send_json_error( $response->get_error_code() . ': ' . $response->get_error_message() );
62 }
63
64 wp_send_json_success();
65 }
66
67 /**
68 * @return void
69 */
70 private function do_flush() {
71 $entries = array_filter( (array) glob( GDPRESS_CACHE_ABSPATH . '/*' ) );
72
73 foreach ( $entries as $entry ) {
74 $this->delete( $entry );
75 }
76
77 $db_cleanup = [
78 Settings::GDPRESS_MANAGE_SETTING_REQUESTS,
79 Settings::GDPRESS_MANAGE_SETTING_LOCAL,
80 Settings::GDPRESS_MANAGE_SETTING_EXCLUDED,
81 ];
82
83 foreach ( $db_cleanup as $option ) {
84 delete_option( $option );
85 }
86 }
87
88 /**
89 * @param $entry
90 */
91 private function delete( $entry ) {
92 if ( is_dir( $entry ) ) {
93 $file = new \FilesystemIterator( $entry );
94
95 // If dir is empty, valid() returns false.
96 while ( $file->valid() ) {
97 $this->delete( $file->getPathName() );
98 $file->next();
99 }
100
101 rmdir( $entry );
102 } else {
103 unlink( $entry );
104 }
105 }
106
107 /**
108 * Flush the cache directory and remove DB settings.
109 *
110 * @return void
111 */
112 public function flush() {
113 check_ajax_referer( Settings::GDPRESS_ADMIN_PAGE, 'nonce' );
114
115 if ( ! current_user_can( 'manage_options' ) ) {
116 wp_die( __( "Sorry, you're not allowed to do this.", 'gdpr-press' ) );
117 }
118
119 try {
120 $this->do_flush();
121
122 Notice::set_notice( __( 'GDPRess Bot has successfully cleared its cache.', 'gdpr-press' ) );
123 } catch ( \Exception $e ) {
124 Notice::set_notice(
125 __( 'GDPRess Bot could not empty the cache directory: ', 'gdpr-press' ) . $e->getMessage(),
126 'error',
127 'all',
128 'gdpress-cache-flush-error'
129 );
130 }
131 }
132 }
133