Skip to content

Commit

Permalink
Fix deleting tables in multisite; Update Gravity Forms Compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
balexey88 committed Apr 11, 2024
1 parent b1a9eb8 commit 6676080
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 17 deletions.
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
== Changelog ==
= 4.0.2 =
* FIX - in multisite network, deleting site can potentially remove WP-Stateless tables from another site.
* COMPATIBILITY - Gravity Forms Compatibility updated for the newest Gravity Forms version.

= 4.0.1 =
* FIX - improvements to Data Optimization process.
* FIX - Data Optimization fixed for multisite environment.
Expand Down
4 changes: 4 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### 4.0.2
* FIX - in multisite network, deleting site can potentially remove WP-Stateless tables from another site.
* COMPATIBILITY - Gravity Forms Compatibility updated for the newest Gravity Forms version.

#### 4.0.1
* FIX - improvements to Data Optimization process.
* FIX - Data Optimization fixed for multisite environment.
Expand Down
32 changes: 27 additions & 5 deletions lib/classes/class-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,18 @@ public function get_gs_host($sm = array()) {
return apply_filters('get_gs_host', $image_host, $image_host, $sm['bucket'], $is_ssl, $sm);
}

/**
* Return gs:// path.
*
* @param array $sm
* @return mixed|void
*/
public function get_gs_path() {
$path = 'gs://' . $this->get('sm.bucket');

return apply_filters('get_gs_path', $path);
}

/**
* Filter for wp_stateless_bucket_link if custom domain is set.
* It's get attachment url and remove "storage.googleapis.com" from url.
Expand Down Expand Up @@ -1748,11 +1760,7 @@ public function create_sync_db($force = false) {
* @param $old_site
*/
public function wp_delete_site($old_site) {
switch_to_blog($old_site->id);

ud_stateless_db()->clear_db();

restore_current_blog();
ud_stateless_db()->clear_db($old_site->id);
}

/**
Expand Down Expand Up @@ -2090,6 +2098,20 @@ public function send_admin_email($subject, $message, $email = '') {

wp_mail( $admin_email, $subject, $message);
}

/**
* Check if we are in specific mode
*
* @param string|array $mode
* @return bool
*/
public function is_mode($mode) {
if ( !is_array($mode) ) {
$mode = [$mode];
}

return in_array($this->get('sm.mode'), $mode);
}
}
}
}
19 changes: 17 additions & 2 deletions lib/classes/class-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,25 @@ public function create_db() {

/**
* Remove custom DB table on plugin uninstall
*
* @param int $site_id
*/
public function clear_db() {
$sql = "DROP TABLE IF EXISTS $this->files, $this->file_sizes, $this->file_meta, $this->sm_sync;";
public function clear_db($site_id) {
switch_to_blog($id);

$tables = array(
$this->wpdb->prefix . 'files',
$this->wpdb->prefix . 'files_sizes',
$this->wpdb->prefix . 'file_meta',
$this->wpdb->prefix . 'sm_sync',
);

$tables = implode(', ', $tables);

$sql = "DROP TABLE IF EXISTS $tables;";
$this->wpdb->query($sql);

restore_current_blog();
}

/**
Expand Down
18 changes: 17 additions & 1 deletion lib/classes/class-sync-non-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __construct() {
add_action('sm:sync::moveFile', array($this, 'move_file'), 10, 2);
add_action('sm:sync::deleteFile', array($this, 'delete_file'));
add_action('sm:sync::deleteFiles', array($this, 'delete_files'));
add_action('sm:sync::unregister_file', array($this, 'unregister_file'), 10, 1);
}

/**
Expand Down Expand Up @@ -78,6 +79,7 @@ public function sync_file($name, $absolutePath, $forced = false, $args = array()
'skip_db' => false,
'manual_sync' => false,
'remove_from_queue' => false, // removes entry from queue table if both file is missing.
'name_with_root' => true,
));

if ($this->queue_is_exists($name, 'synced') && !$forced) {
Expand Down Expand Up @@ -113,7 +115,7 @@ public function sync_file($name, $absolutePath, $forced = false, $args = array()
if ($sm_mode == 'stateless' && !wp_doing_ajax()) {
global $gs_client;

$gs_name = apply_filters('wp_stateless_file_name', $name, true);
$gs_name = apply_filters('wp_stateless_file_name', $name, $args['name_with_root']);

//Bucket
$bucket = ud_get_stateless_media()->get('sm.bucket');
Expand All @@ -132,6 +134,7 @@ public function sync_file($name, $absolutePath, $forced = false, $args = array()
),
'is_webp' => '',
));

$args = apply_filters('wp_stateless_add_media_args', $args);

/**
Expand Down Expand Up @@ -408,6 +411,19 @@ public function get_gs_client() {

return $this->client;
}

/**
* Remove file from DB after file deletion.
* @param string $file
*/
public function unregister_file($file) {
$file = str_replace( ud_get_stateless_media()->get_gs_path(), '', $file);
$file = str_replace( ud_get_stateless_media()->get_gs_host(), '', $file);

$file = trim($file, '/');

$this->queue_remove_file($file);
}
}
}
}
41 changes: 35 additions & 6 deletions lib/classes/compatibility/gravity-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,33 @@ public function module_init($sm) {
$this->plugin_version = \GFForms::$version;
}
add_filter('gform_save_field_value', array($this, 'gform_save_field_value'), 10, 5);
add_filter('gform_upload_path', array($this, 'gf_upload_path'), 10, 1);

add_filter('stateless_skip_cache_busting', array($this, 'skip_cache_busting'), 10, 2);

do_action('sm:sync::register_dir', '/gravity_forms/');
add_action('sm::synced::nonMediaFiles', array($this, 'modify_db'), 10, 3);
add_action('gform_file_path_pre_delete_file', array($this, 'gform_file_path_pre_delete_file'), 10, 2);
}

/**
* For 'stateless' mode we use upload path and URL without wildcards.
* @param $upload_path
* @return mixed
*/
public function gf_upload_path($upload_path) {
if ( !ud_get_stateless_media()->is_mode('stateless') ) {
return $upload_path;
}

$dir = wp_upload_dir();

$upload_path['path'] = ud_get_stateless_media()->get_gs_path() . str_replace($dir['basedir'], '', $upload_path['path']);
$upload_path['url'] = ud_get_stateless_media()->get_gs_host() . str_replace($dir['baseurl'], '', $upload_path['url']);

return $upload_path;
}

/**
* On gform save field value sync file to GCS and alter the file url to GCS link.
* @param $value
Expand All @@ -55,7 +75,9 @@ public function gform_save_field_value($value, $lead, $field, $form, $input_id)
$this->plugin_version = \GFForms::$version;
}

$is_stateless = ud_get_stateless_media()->is_mode('stateless');
$type = \GFFormsModel::get_input_type($field);

if ($type == 'fileupload') {
$dir = wp_upload_dir();

Expand All @@ -71,10 +93,13 @@ public function gform_save_field_value($value, $lead, $field, $form, $input_id)

if ($position !== false) {
$name = substr($v, $position);
$absolutePath = $dir['basedir'] . '/' . $name;
$path = $is_stateless ? ud_get_stateless_media()->get_gs_path() : $dir['basedir'];
$absolutePath = $path . '/' . $name;

$name = apply_filters('wp_stateless_file_name', $name, 0);

// doing sync
do_action('sm:sync::syncFile', $name, $absolutePath);
do_action( 'sm:sync::syncFile', $name, $absolutePath, false, $is_stateless ? array('name_with_root' => false) : array() );
$value[$k] = ud_get_stateless_media()->get_gs_host() . '/' . $name;
// Todo add filter.
}
Expand All @@ -86,7 +111,7 @@ public function gform_save_field_value($value, $lead, $field, $form, $input_id)
$value = array_pop($value);
}
} else if ($type == 'post_image') {
add_action('gform_after_create_post', function ($post_id, $lead, $form) use ($value, $field) {
add_action('gform_after_create_post', function ($post_id, $lead, $form) use ($value, $field, $is_stateless) {
global $wpdb;
$dir = wp_upload_dir();
$lead_detail_id = $lead['id'];
Expand All @@ -100,9 +125,11 @@ public function gform_save_field_value($value, $lead, $field, $form, $input_id)
$name = rgar($arr_name, 0); // Removed |:| from end of the url.

// doing sync
$absolutePath = $dir['basedir'] . '/' . $name;
$path = $is_stateless ? ud_get_stateless_media()->get_gs_path() : $dir['basedir'];
$absolutePath = $path . '/' . $name;
$name = apply_filters('wp_stateless_file_name', $name, 0);
do_action('sm:sync::syncFile', $name, $absolutePath);

do_action( 'sm:sync::syncFile', $name, $absolutePath, false, $is_stateless ? array('name_with_root' => false) : array() );

$value = ud_get_stateless_media()->get_gs_host() . '/' . $name;
// Todo add filter.
Expand Down Expand Up @@ -240,10 +267,12 @@ public function gform_file_path_pre_delete_file($file_path, $url) {

$client = ud_get_stateless_media()->get_client();
if (!is_wp_error($client)) {
$client->remove_media(trim($gs_name, '/'));
$client->remove_media(trim($gs_name, '/'), '', false);
}
}

do_action('sm:sync::unregister_file', $url);

return $file_path;
}

Expand Down
8 changes: 6 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Tags: google cloud, google cloud storage, cdn, uploads, backup
License: GPLv2 or later
Requires PHP: 8.0
Requires at least: 5.0
Tested up to: 6.5.0
Stable tag: 4.0.1
Tested up to: 6.5.2
Stable tag: 4.0.2

Upload and serve your WordPress media files from Google Cloud Storage.

Expand Down Expand Up @@ -121,6 +121,10 @@ Before upgrading to WP-Stateless 3.2.0, please, make sure you use PHP 7.2 or abo
Before upgrading to WP-Stateless 3.0, please, make sure you tested it on your development environment.

== Changelog ==
= 4.0.2 =
* FIX - in multisite network, deleting site can potentially remove WP-Stateless tables from another site.
* COMPATIBILITY - Gravity Forms Compatibility updated for the newest Gravity Forms version.

= 4.0.1 =
* FIX - improvements to Data Optimization process.
* FIX - Data Optimization fixed for multisite environment.
Expand Down
2 changes: 1 addition & 1 deletion wp-stateless-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin URI: https://stateless.udx.io/
* Description: Upload and serve your WordPress media files from Google Cloud Storage.
* Author: UDX
* Version: 4.0.1
* Version: 4.0.2
* Text Domain: stateless-media
* Author URI: https://udx.io
* License: GPLv2 or later
Expand Down

0 comments on commit 6676080

Please sign in to comment.