-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathclass-sync.php
More file actions
145 lines (128 loc) · 3.48 KB
/
class-sync.php
File metadata and controls
145 lines (128 loc) · 3.48 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
144
145
<?php
/**
* Sync manages all of the sync components for the Cloudinary plugin.
*
* @package Cloudinary
*/
namespace Cloudinary;
use Cloudinary\Component\Assets;
use Cloudinary\Component\Setup;
use Cloudinary\Sync\Delete_Sync;
use Cloudinary\Sync\Download_Sync;
use Cloudinary\Sync\Push_Sync;
use Cloudinary\Sync\Upload_Queue;
use Cloudinary\Sync\Upload_Sync;
/**
* Class Sync
*/
class Sync implements Setup, Assets {
/**
* Holds the plugin instance.
*
* @since 0.1
*
* @var Plugin Instance of the global plugin.
*/
public $plugin;
/**
* Contains all the different sync components.
*
* @var array A collection of sync components.
*/
public $managers;
/**
* Holds the meta keys for sync meta to maintain consistency.
*/
const META_KEYS = array(
'pending' => '_cloudinary_pending',
'signature' => '_sync_signature',
'version' => '_cloudinary_version',
'breakpoints' => '_cloudinary_breakpoints',
'public_id' => '_public_id',
'transformation' => '_transformations',
'sync_error' => '_sync_error',
'cloudinary' => '_cloudinary_v2',
);
/**
* Push_Sync constructor.
*
* @param Plugin $plugin Global instance of the main plugin.
*/
public function __construct( Plugin $plugin ) {
$this->plugin = $plugin;
$this->managers['push'] = new Push_Sync( $this->plugin );
$this->managers['upload'] = new Upload_Sync( $this->plugin );
$this->managers['download'] = new Download_Sync( $this->plugin );
$this->managers['delete'] = new Delete_Sync( $this->plugin );
$this->managers['queue'] = new Upload_Queue( $this->plugin );
}
/**
* Setup assets/scripts.
*/
public function enqueue_assets() {
if ( $this->plugin->config['connect'] ) {
$data = array(
'restUrl' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' ),
);
wp_add_inline_script( 'cloudinary', 'var cloudinaryApi = ' . wp_json_encode( $data ), 'before' );
}
}
/**
* Register Assets.
*/
public function register_assets() {
}
/**
* Is the component Active.
*/
public function is_active() {
return $this->plugin->components['settings']->is_active() && 'sync_media' === $this->plugin->components['settings']->active_tab();
}
/**
* Checks if an asset is synced and up to date.
*
* @param int $post_id The post id to check.
*
* @return bool
*/
public function is_synced( $post_id ) {
$return = false;
$signature = $this->plugin->components['media']->get_post_meta( $post_id, self::META_KEYS['signature'], true );
if ( ! empty( $signature ) && $this->generate_signature( $post_id ) === $signature ) {
$return = $signature;
}
return $return;
}
/**
* Generate a signature based on whats required for a full sync.
*
* @param int $post_id The post id to generate a signature for.
*
* @return array
*/
public function generate_signature( $post_id ) {
$upload = $this->managers['push']->prepare_upload( $post_id );
$credentials = $this->plugin->components['connect']->get_credentials();
$upload['cloud_name'] = $credentials['cloud_name'];
$return = array_map(
function ( $item ) {
if ( is_array( $item ) ) {
$item = wp_json_encode( $item );
}
return md5( $item );
},
$upload
);
return $return;
}
/**
* Additional component setup.
*/
public function setup() {
if ( $this->plugin->config['connect'] ) {
$this->managers['upload']->setup();
$this->managers['delete']->setup();
}
}
}