dropbox.php
4.68 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
require_once( NINJA_FORMS_UPLOADS_DIR . '/includes/lib/dropbox/dropbox.php' );
class External_Dropbox extends NF_Upload_External {
private $title = 'Dropbox';
private $slug = 'dropbox';
private $path;
private $settings;
function __construct() {
$this->set_settings();
parent::__construct( $this->title, $this->slug, $this->settings );
add_action( 'admin_init', array( $this, 'disconnect' ) );
add_action( 'admin_notices', array( $this, 'connect_notice' ) );
add_action( 'admin_notices', array( $this, 'disconnect_notice' ) );
}
private function set_settings() {
$this->settings = array(
array(
'name' => 'dropbox_connect',
'type' => '',
'label' => sprintf( __( 'Connect to %s', 'ninja-forms-uploads' ), $this->title ),
'desc' => '',
'display_function' => array( $this, 'connect_url' )
),
array(
'name' => 'dropbox_file_path',
'type' => 'text',
'label' => __( 'File Path', 'ninja-forms-uploads' ),
'desc' => __( 'Custom directory for the files to be uploaded to in your Dropbox:<br> /Apps/Ninja Forms Uploads/'. $this->get_path() , 'ninja-forms-uploads' ),
'default_value' => ''
),
array(
'name' => 'dropbox_token',
'type' => 'hidden'
)
);
}
private function get_path() {
if ( is_null( $this->path ) ) {
$plugin_settings = get_option( 'ninja_forms_settings' );
$this->path = isset( $plugin_settings['dropbox_file_path'] ) ? $plugin_settings['dropbox_file_path'] : '';
$this->path = trim( $this->path );
$this->path = apply_filters( 'ninja_forms_uploads_' . $this->slug . '_path', $this->path );
if ( '/' == $this->path ) {
$this->path = '';
}
if ( '' != $this->path ) {
$this->path = $this->sanitize_path( $this->path );
}
}
return $this->path;
}
public function is_connected() {
$data = get_option( 'ninja_forms_settings' );
if ( ( isset( $data['dropbox_access_token'] ) && $data['dropbox_access_token'] != '' ) &&
( isset( $data['dropbox_access_token_secret'] ) && $data['dropbox_access_token_secret'] != '' )
) {
if ( false === ( $authorised = get_transient( 'nf_fu_dropbox_authorised' ) ) ) {
$dropbox = new nf_dropbox();
$authorised = $dropbox->is_authorized();
set_transient( 'nf_fu_dropbox_authorised', $authorised, 60 * 60 * 5 );
}
return $authorised;
}
return false;
}
public function upload_file( $file, $path = '' ) {
$dropbox = new nf_dropbox();
$path = $this->get_path();
$filename = $this->get_filename_external( $file );
$dropbox->upload_file( $file, $filename, $path );
return array( 'path' => $path, 'filename' => $filename );
}
public function file_url( $filename, $path = '', $data = array() ) {
$dropbox = new nf_dropbox();
$url = $dropbox->get_link( $path . $filename );
if ( $url ) {
return $url;
}
return admin_url();
}
public function connect_url( $form_id, $data ) {
$dropbox = new nf_dropbox();
$callback_url = admin_url( '/admin.php?page=ninja-forms-uploads&tab=external_settings' );
$disconnect_url = admin_url( '/admin.php?page=ninja-forms-uploads&tab=external_settings&action=disconnect_' . $this->slug );
if ( $dropbox->is_authorized() ) {
?>
<a id="dropbox-disconnect" href="<?php echo $disconnect_url; ?>" class="button-secondary"><?php _e( 'Disconnect', 'ninja-forms-uploads' ); ?></a>
<?php } else { ?>
<a id="dropbox-connect" href="<?php echo $dropbox->get_authorize_url( $callback_url ); ?>" class="button-secondary"><?php _e( 'Connect', 'ninja-forms-uploads' ); ?></a>
<?php
}
}
public function disconnect() {
if ( isset( $_GET['page'] ) && $_GET['page'] == 'ninja-forms-uploads' &&
isset( $_GET['tab'] ) && $_GET['tab'] == 'external_settings' &&
isset( $_GET['action'] ) && $_GET['action'] == 'disconnect_' . $this->slug
) {
$dropbox = new nf_dropbox();
$dropbox->unlink_account();
}
}
public function connect_notice() {
if ( isset( $_GET['page'] ) && $_GET['page'] == 'ninja-forms-uploads' &&
isset( $_GET['tab'] ) && $_GET['tab'] == 'external_settings' &&
isset( $_GET['oauth_token'] ) && isset( $_GET['uid'] )
) {
echo '<div class="updated"><p>' . sprintf( __( 'Connected to %s', 'ninja-forms-uploads' ), $this->title ) . '</p></div>';
}
}
public function disconnect_notice() {
if ( isset( $_GET['page'] ) && $_GET['page'] == 'ninja-forms-uploads' &&
isset( $_GET['tab'] ) && $_GET['tab'] == 'external_settings' &&
isset( $_GET['action'] ) && $_GET['action'] == 'disconnect_' . $this->slug
) {
echo '<div class="updated"><p>' . sprintf( __( 'Disconnected from %s', 'ninja-forms-uploads' ), $this->title ) . '</p></div>';
}
}
}