-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-siteorigin-widgets-bundle.php
185 lines (151 loc) · 5.37 KB
/
class-siteorigin-widgets-bundle.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace SLCA\SiteOriginWidgetsBundle;
use wpCloud\StatelessMedia\Compatibility;
use wpCloud\StatelessMedia\Helper;
class SiteOriginWidgetsBundle extends Compatibility {
const STORAGE_PATH = 'siteorigin-widgets/';
protected $id = 'so-widget-css';
protected $title = 'SiteOrigin Widget Bundle';
protected $constant = 'WP_STATELESS_COMPATIBILITY_SOWB';
protected $description = 'Enabled support for CSS files generated by SiteOrigin Widgets Bundle.';
protected $plugin_file = 'so-widgets-bundle/so-widgets-bundle.php';
protected $non_library_sync = true;
protected $filesystem = null;
/**
* @param $sm
*/
public function module_init($sm) {
if ( !class_exists('\WP_Filesystem_Direct') ) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php');
}
$this->filesystem = new \WP_Filesystem_Direct( false );
add_filter('set_url_scheme', array($this, 'set_url_scheme'), 20);
add_filter('pre_set_transient_sow:cleared', array($this, 'clear_file_cache'), 20, 3);
add_filter('siteorigin_widgets_stylesheet_cleared', array($this, 'widgets_stylesheet_cleared'), 20);
add_filter('stateless_skip_cache_busting', array($this, 'skip_cache_busting'), 10, 2);
add_filter('sm:sync::syncArgs', array($this, 'sync_args'), 10, 4);
add_action('siteorigin_widgets_stylesheet_added', array($this, 'widgets_stylesheet_added'), 10, 2);
add_action('siteorigin_widgets_stylesheet_deleted', array($this, 'delete_file'), 10, 1);
do_action('sm:sync::register_dir', '/' . self::STORAGE_PATH);
}
/**
* In Stateless mode move file to proper location.
*/
protected function possibly_move_file($name, $destination) {
if ( !ud_get_stateless_media()->is_mode('stateless') ) {
return;
}
$upload_dir = wp_upload_dir();
$source = $upload_dir['basedir'] . '/' . $name;
$this->filesystem->move( $source, $destination, true );
}
/**
* Change URL for loading CSS files in Stateless Mode.
*
* @param $url
* @return string
*/
public function set_url_scheme($url) {
if ( ud_get_stateless_media()->is_mode( ['disabled', 'backup'] ) ) {
return $url;
}
$position = strpos($url, self::STORAGE_PATH);
if ($position !== false) {
$name = substr($url, $position);
$name = apply_filters('wp_stateless_addon_files_root', '') . '/' . $name;
$url = str_replace( ud_get_stateless_media()->get_gs_path(), ud_get_stateless_media()->get_gs_host(), $name );
if ( ud_get_stateless_media()->is_mode('stateless') ) {
$url = str_replace( ud_get_stateless_media()->get_gs_path(), ud_get_stateless_media()->get_gs_host(), $name );
} else {
$upload_dir = wp_upload_dir();
$url = str_replace( $upload_dir['basedir'], ud_get_stateless_media()->get_gs_host(), $name );
}
}
return $url;
}
/**
* Sync new CSS file to GCS
*
* @param $url
* @param $instance
* @return string
*/
public function widgets_stylesheet_added($name, $instance) {
$name = self::STORAGE_PATH . $name;
$absolutePath = apply_filters('wp_stateless_addon_files_root', '');
$absolutePath .= '/' . $name;
$name = apply_filters('wp_stateless_file_name', $name, 0);
$this->possibly_move_file($name, $absolutePath);
do_action('sm:sync::syncFile', $name, $absolutePath);
}
/**
* Clear all SO CSS files from GCS after expired. 7 days
* @param $value
* @param $expiration
* @param $transient
* @return mixed
*/
public function clear_file_cache($value, $expiration, $transient) {
do_action('sm:sync::deleteFiles', self::STORAGE_PATH);
return $value;
}
/**
* Clear all SO CSS files from GCS after expired. 7 days
*/
public function widgets_stylesheet_cleared() {
do_action('sm:sync::deleteFiles', self::STORAGE_PATH);
}
/**
* Remove single file from GCS. File can be without extension.
*
* @param $css_name
*/
public function delete_file($css_name) {
$extension = pathinfo($css_name, PATHINFO_EXTENSION);
if ( $extension !== 'css' ) {
$css_name .= '.css';
}
$file = self::STORAGE_PATH . $css_name;
do_action('sm:sync::deleteFile', $file);
}
/**
* Skip cache busting while activating/deactivating SO widget.
*
* @param $return
* @param $filename
* @return mixed
*/
public function skip_cache_busting($return, $filename) {
$back_trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
foreach ($back_trace as $trace) {
if ( isset($trace['file']) && strpos($trace['file'], 'so-widgets-bundle') !== false ) {
if ( isset($trace['function']) && $trace['function'] === 'sanitize_file_name' ) {
return $filename;
}
}
}
return $return;
}
/**
* Update args when uploading/syncing file to GCS.
*
* @param array $args
* @param string $name
* @param string $file
* @param bool $force
*
* @return array
*/
public function sync_args($args, $name, $file, $force) {
if ( strpos($name, self::STORAGE_PATH) !== 0 ) {
return $args;
}
if ( ud_get_stateless_media()->is_mode('stateless') ) {
$args['name_with_root'] = false;
}
$args['source'] = 'SiteOrigin Widgets Bundle';
$args['source_version'] = defined('SOW_BUNDLE_VERSION') ? SOW_BUNDLE_VERSION : '';
return $args;
}
}