class-destination-sugarsync.php
5.53 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
*
*/
class BackWPup_Pro_Destination_SugarSync extends BackWPup_Destination_SugarSync {
/**
* @param $job_settings
*/
public function wizard_page( array $job_settings ) {
?>
<table class="form-table">
<tr>
<td>
<fieldset>
<?php if ( ! $job_settings[ 'sugarrefreshtoken' ] ) { ?>
<label for="sugaremail"><strong><?php esc_html_e( 'Email address:', 'backwpup' ); ?></strong><br/>
<input id="sugaremail" name="sugaremail" type="text" value="" class="large-text" autocomplete="off" /></label>
<br/>
<label for="sugarpass"><strong><?php esc_html_e( 'Password:', 'backwpup' ); ?></strong><br/>
<input id="sugarpass" name="sugarpass" type="password" value="" class="large-text" autocomplete="off" /></label>
<br/>
<br/>
<input type="submit" name="wizard_button" class="button-primary" value="<?php esc_html_e( 'Sugarsync authenticate!', 'backwpup' ); ?>"/>
<input type="submit" name="wizard_button" class="button"
value="<?php esc_html_e( 'Create Sugarsync account', 'backwpup' ); ?>"/>
<br/>
<?php }
else { ?>
<strong><?php esc_html_e( 'Login:', 'backwpup' ); ?></strong>
<span style="color:green;"><?php esc_html_e( 'Authenticated!', 'backwpup' ); ?></span>
<input type="submit" name="wizard_button" class="button-primary" value="<?php esc_html_e( 'Delete Sugarsync authentication!', 'backwpup' ); ?>"/>
<br/>
<strong><?php esc_html_e( 'Root:', 'backwpup' ); ?></strong>
<?php
try {
$sugarsync = new BackWPup_Destination_SugarSync_API( $job_settings[ 'sugarrefreshtoken' ] );
$user = $sugarsync->user();
$syncfolders = $sugarsync->get( $user->syncfolders );
if ( ! is_object( $syncfolders ) )
echo '<span style="color:red;">' . esc_html__( 'No Syncfolders found!', 'backwpup' ) . '</span>';
}
catch ( Exception $e ) {
echo '<span style="color:red;">' . $e->getMessage() . '</span>';
}
if ( isset( $syncfolders ) && is_object( $syncfolders ) ) {
echo '<select name="sugarroot" id="sugarroot">';
foreach ( $syncfolders->collection as $roots ) {
echo '<option ' . selected( strtolower( $job_settings[ 'sugarroot' ] ), strtolower( $roots->ref ), FALSE ) . ' value="' . esc_attr($roots->ref) . '"">' . esc_html($roots->displayName) . '</option>';
}
echo '</select>';
}
?>
<?php } ?>
<br/>
<label id="idsugardir"><strong><?php esc_html_e( 'Folder:', 'backwpup' ); ?></strong><br/>
<input name="sugardir" id="idsugardir" type="text" value="<?php echo esc_attr( $job_settings[ 'sugardir' ] );?>" class="large-text" /></label><br/>
<?php if ( $job_settings[ 'backuptype' ] === 'archive' ) { ?>
<label for="idsugarmaxbackups"><input name="sugarmaxbackups" id="idsugarmaxbackups" type="text" size="3" value="<?php echo esc_attr($job_settings[ 'sugarmaxbackups' ]);?>" class="small-text" />
<?php esc_html_e( 'Maximum number of backup files to keep in folder:', 'backwpup' ); ?>
<br/>
<?php } else { ?>
<label for="idsugarsyncnodelete"><input class="checkbox" value="1"
type="checkbox" <?php checked( $job_settings[ 'sugarsyncnodelete' ], TRUE ); ?>
name="sugarsyncnodelete" id="idsugarsyncnodelete" /> <?php esc_html_e( 'Do not delete files while syncing to destination!', 'backwpup' ); ?></label>
<br/>
<?php } ?>
</fieldset>
</td>
</tr>
</table>
<?php
}
/**
* @param $job_settings array
*
* @return array
*/
public function wizard_save( array $job_settings ) {
if ( ! empty( $_POST[ 'sugaremail' ] ) && ! empty( $_POST[ 'sugarpass' ] ) && $_POST[ 'wizard_button' ] == __( 'Sugarsync authenticate!', 'backwpup' ) ) {
try {
$sugarsync = new BackWPup_Destination_SugarSync_API();
$refresh_token = $sugarsync->get_Refresh_Token( sanitize_email( $_POST[ 'sugaremail' ] ), sanitize_text_field( $_POST[ 'sugarpass' ] ) );
if ( ! empty( $refresh_token ) )
$job_settings[ 'sugarrefreshtoken' ] = $refresh_token ;
}
catch ( Exception $e ) {
BackWPup_Admin::message( 'SUGARSYNC: ' . $e->getMessage(), TRUE );
}
}
if ( isset( $_POST[ 'wizard_button' ] ) && $_POST[ 'wizard_button' ] === __( 'Delete Sugarsync authentication!', 'backwpup' ) ) {
$job_settings[ 'sugarrefreshtoken' ] = '';
}
if ( isset( $_POST[ 'wizard_button' ] ) && $_POST[ 'wizard_button' ] === __( 'Create Sugarsync account', 'backwpup' ) ) {
try {
$sugarsync = new BackWPup_Destination_SugarSync_API();
$sugarsync->create_account( sanitize_email( $_POST[ 'sugaremail' ] ), sanitize_text_field( $_POST[ 'sugarpass' ] ) );
}
catch ( Exception $e ) {
BackWPup_Admin::message( 'SUGARSYNC: ' . $e->getMessage(), TRUE );
}
}
$_POST[ 'sugardir' ] = trailingslashit( str_replace( '//', '/', str_replace( '\\', '/', trim( sanitize_text_field( $_POST[ 'sugardir' ] ) ) ) ) );
if ( substr( $_POST[ 'sugardir' ], 0, 1 ) == '/' ) {
$_POST[ 'sugardir' ] = substr( $_POST[ 'sugardir' ], 1 );
}
if ( $_POST[ 'sugardir' ] == '/' ) {
$_POST[ 'sugardir' ] = '';
}
$job_settings[ 'sugardir' ] = sanitize_text_field( $_POST[ 'sugardir' ] );
$job_settings[ 'sugarroot' ] = isset( $_POST[ 'sugarroot' ] ) ? sanitize_text_field( $_POST[ 'sugarroot' ] ) : '';
$job_settings[ 'sugarmaxbackups' ] = isset( $_POST[ 'sugarmaxbackups' ] ) ? absint( $_POST[ 'sugarmaxbackups' ] ) : 0;
$job_settings[ 'sugarsyncnodelete' ] = ! empty( $_POST[ 'sugarsyncnodelete' ] );
return $job_settings;
}
}