class-otgs-installer-wp-components-setting-ajax.php
1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
class OTGS_Installer_WP_Components_Setting_Ajax {
const AJAX_ACTION = 'otgs_save_setting_share_local_components';
const SAVE_SETTING_PRIORITY = 1;
/**
* @var OTGS_Installer_WP_Share_Local_Components_Setting
*/
private $setting;
/**
* @var WP_Installer
*/
private $installer;
public function __construct( OTGS_Installer_WP_Share_Local_Components_Setting $setting, WP_Installer $installer ) {
$this->setting = $setting;
$this->installer = $installer;
}
public function add_hooks() {
add_action( 'wp_ajax_' . self::AJAX_ACTION, array( $this, 'save' ), self::SAVE_SETTING_PRIORITY );
}
public function save() {
if ( $this->is_valid_request() ) {
$user_agree = (int) filter_var( $_POST['agree'], FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$repo_request = filter_var( $_POST['repo'], FILTER_SANITIZE_FULL_SPECIAL_CHARS );
if ( $repo_request ) {
$repos = array();
foreach ( $this->installer->get_repositories() as $repo_id => $repository ) {
if ( $repo_id === $repo_request ) {
$repos[ $repo_id ] = $user_agree;
}
}
$this->setting->save( $repos );
}
}
}
/**
* @return bool
*/
private function is_valid_request() {
return isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], self::AJAX_ACTION );
}
}