| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Google App Engine Compatibility |
| 5 |
* |
| 6 |
* Detects is are running on Google App Engine, shows message and automatically enables 'Stateless' mode |
| 7 |
* |
| 8 |
* @since 3.3.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace wpCloud\StatelessMedia { |
| 12 |
if (!class_exists('wpCloud\StatelessMedia\AppEngine')) { |
| 13 |
|
| 14 |
/** |
| 15 |
* Class AppEngine |
| 16 |
* |
| 17 |
* @package wpCloud\StatelessMedia |
| 18 |
*/ |
| 19 |
class AppEngine { |
| 20 |
use Singleton; |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
add_filter('wp_stateless_is_app_engine', array($this, 'is_app_engine')); |
| 24 |
|
| 25 |
add_action('admin_init', array($this, 'add_message')); |
| 26 |
add_action('wp_stateless_settings_refresh', array($this, 'after_settings_refresh')); |
| 27 |
|
| 28 |
add_filter('site_option_sm_mode', [$this, 'override_stateless_mode']); |
| 29 |
add_filter('option_sm_mode', [$this, 'override_stateless_mode']); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Checks if we are running on Google App Engine |
| 34 |
* |
| 35 |
* @return bool |
| 36 |
*/ |
| 37 |
public function is_app_engine() { |
| 38 |
return isset($_SERVER["GAE_VERSION"]); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Override 'sm_mode' option to 'stateless' if we are running on Google App Engine |
| 43 |
* Do not override in Disabled mode |
| 44 |
* |
| 45 |
* @param string $value |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function override_stateless_mode($value) { |
| 49 |
// We load too early to use 'ud_get_stateless_media()->is_mode' or 'ud_get_stateless_media()->get' |
| 50 |
if ( $value === 'disabled' ) { |
| 51 |
return $value; |
| 52 |
} |
| 53 |
|
| 54 |
if ( apply_filters('wp_stateless_is_app_engine', false) ) { |
| 55 |
return 'stateless'; |
| 56 |
} |
| 57 |
|
| 58 |
return $value; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Make 'sm_mode' option readonly if we are running on Google App Engine |
| 63 |
* |
| 64 |
* @param \wpCloud\StatelessMedia\Settings $settingsObj |
| 65 |
*/ |
| 66 |
public function after_settings_refresh($settingsObj) { |
| 67 |
if ( !is_a($settingsObj, 'wpCloud\StatelessMedia\Settings') ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if ( !apply_filters('wp_stateless_is_app_engine', false) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$settingsObj->set('sm.readonly.mode', 'app_engine'); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Add admin message if we are running on Google App Engine |
| 80 |
*/ |
| 81 |
public function add_message() { |
| 82 |
if ( !apply_filters('wp_stateless_is_app_engine', false) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
ud_get_stateless_media()->errors->add(array( |
| 87 |
'key' => 'stateless_app_engine_auto_mode', |
| 88 |
'button' => 'View Settings', |
| 89 |
'button_link' => admin_url('upload.php?page=stateless-settings'), |
| 90 |
'title' => sprintf(__('Stateless Mode Enabled Automatically.', ud_get_stateless_media()->domain)), |
| 91 |
'message' => sprintf(__('We detected that you are running Google App Engine. This platform does not allow you to save files locally, |
| 92 |
so we have automatically enabled <b>Stateless</b> mode. |
| 93 |
In this mode, your files will only be stored on Google Cloud Storage.', ud_get_stateless_media()->domain) |
| 94 |
), |
| 95 |
), 'notice'); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|