PluginProbe
WP-Stateless – Google Cloud Storage / 2.1.4
WP-Stateless – Google Cloud Storage v2.1.4
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / class-sync-non-media.php

class-sync-non-media.php in WP-Stateless – Google Cloud Storage 2.1.4, at lib/classes/class-sync-non-media.php

131 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * To do: check if client is connected to google before doing any action.
4 */
5
6 namespace wpCloud\StatelessMedia {
7
8 if (!class_exists('wpCloud\StatelessMedia\SyncNonMedia')) {
9
10 class SyncNonMedia {
11
12 public $registered_dir = array();
13 public $registered_files = array();
14
15 public function __construct(){
16 $this->registered_files = get_option('sm_synced_files', array());
17 add_filter( 'sm:sync::nonMediaFiles', array($this, 'sync_non_media_files') );
18 add_action( 'sm:sync::addFile', array($this, 'add_file') );
19 add_action( 'sm:sync::syncFile', array($this, 'sync_file'), 10, 2 );
20 add_action( 'sm:sync::deleteFile', array($this, 'delete_file') );
21 add_action( 'sm:sync::deleteFiles', array($this, 'delete_files') );
22 }
23
24 public function register_dir($dir){
25 if(!in_array($dir, $this->registered_dir)){
26 $this->registered_dir[] = $dir;
27 }
28 }
29
30 /**
31 * Add file to list of files to be sync.
32 */
33 public function add_file($file){
34 if(!in_array($file, $this->registered_files)){
35 $this->registered_files[] = $file;
36 update_option( 'sm_synced_files', $this->registered_files );
37 }
38 }
39
40 /**
41 * Instant sync
42 */
43 public function sync_file($name, $absolutePath, $forced = false){
44 if(in_array($name, $this->registered_files) && !$forced){
45 return false;
46 }
47
48 $this->add_file($name);
49 $file_type = wp_check_filetype($absolutePath);
50 if(empty($this->client)){
51 $this->client = ud_get_stateless_media()->get_client();
52 }
53
54 $media = $this->client->add_media( array(
55 'name' => $name,
56 'absolutePath' => $absolutePath,
57 'cacheControl' => apply_filters( 'sm:item:cacheControl', 'public, max-age=36000, must-revalidate', $absolutePath),
58 'contentDisposition' => apply_filters( 'sm:item:contentDisposition', null, $absolutePath),
59 'mimeType' => $file_type['type'],
60 'metadata' => array(
61 'child-of' => dirname($name),
62 'file-hash' => md5( $name ),
63 ),
64 ));
65
66 if(ud_get_stateless_media()->get( 'sm.mode' ) === 'stateless'){
67 unlink($absolutePath);
68 }
69
70 return $media;
71 }
72
73 /**
74 * Manual sync using sync tab.
75 */
76 public function sync_non_media_files($files){
77 $upload_dir = wp_upload_dir();
78 $files = array_merge($files, $this->registered_files);
79 foreach ($this->registered_dir as $key => $dir) {
80 $dir = $upload_dir['basedir'] . "/" . trim($dir, '/') . "/";
81 if(is_dir($dir)){
82 $files = glob( $upload_dir['basedir'] . $dir . "*" );
83 foreach ($files as $id => $file) {
84 $_file = str_replace(wp_normalize_path($upload_dir['basedir']), '', wp_normalize_path($file));
85 if(!in_array($_file, $files)){
86 $files[] = $_file;
87 }
88 }
89 }
90 }
91 return $files;
92 }
93
94 public function delete_file($file){
95 $file = trim($file, '/');
96 if(in_array($file, $this->registered_files)){
97 if(empty($this->client)){
98 $this->client = ud_get_stateless_media()->get_client();
99 }
100
101 // Removing file for GCS
102 $this->client->remove_media($file);
103
104 $key = array_search($file, $this->registered_files);
105 unset($this->registered_files[$key]);
106 update_option( 'sm_synced_files', $this->registered_files );
107 return true;
108 }
109 return false;
110 }
111
112 public function delete_files($dir){
113 if(empty($this->client)){
114 $this->client = ud_get_stateless_media()->get_client();
115 }
116
117 foreach ($this->registered_files as $key => $file) {
118 if(strpos($file, $dir) !== false){
119 $this->client->remove_media($file);
120 unset($this->registered_files[$key]);
121 }
122 }
123
124 update_option( 'sm_synced_files', $this->registered_files );
125
126 return true;
127 }
128 }
129 }
130 }
131