Skip to content

Commit

Permalink
* Admin notices are now only shown to users with the manage_options
Browse files Browse the repository at this point in the history
… capability.
  • Loading branch information
jawngee committed Oct 31, 2022
1 parent b8ccc05 commit e6f5a49
Show file tree
Hide file tree
Showing 38 changed files with 316 additions and 92 deletions.
2 changes: 2 additions & 0 deletions classes/Tasks/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ public function run() {
}

if (static::stopOnError()) {
Logger::error("Unspecified error running task", [], __METHOD__, __LINE__);
$this->error($this->errorMessage);

$result = self::TASK_ERROR;
Expand All @@ -640,6 +641,7 @@ public function run() {
}
}
} catch (\Exception $ex) {
Logger::error("Error running task: ".$ex->getMessage(), [], __METHOD__, __LINE__);

$this->state = self::STATE_ERROR;
$this->endTime = time();
Expand Down
5 changes: 4 additions & 1 deletion classes/Tools/Debugging/DebuggingTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public function __construct( $toolName, $toolInfo, $toolManager ) {

$link = "<a href='".admin_url('admin.php?page=media-tools-top')."'>turn it off</a>";
$message = "Media Cloud debugging is enabled. This may affect performance. Unless you are troubleshooting and issue, you should $link. You can dismiss this notice and it'll be shown to you again in 24 hours.";
NoticeManager::instance()->displayAdminNotice('warning', $message,true, 'ilab-debug-tools-warning', 1);

if (current_user_can('manage_options')) {
NoticeManager::instance()->displayAdminNotice('warning', $message,true, 'ilab-debug-tools-warning', 1);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion classes/Tools/DynamicImages/DynamicImagesTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function setup() {
foreach($this->toolInfo['imageOptimizers'] as $key => $plugin) {
if (is_plugin_active($plugin)) {
$dismissibleID = 'dynamic-images-image-optimizers-7';
if (NoticeManager::instance()->isAdminNoticeActive($dismissibleID)) {
if (NoticeManager::instance()->isAdminNoticeActive($dismissibleID) && current_user_can('manage_options')) {
add_action( 'admin_notices', function () use ($directUploadEnabled, $dismissibleID) {
?>
<div data-dismissible="<?php echo $dismissibleID ?>" class="notice notice-warning is-dismissible" style="padding:10px;">
Expand Down
20 changes: 11 additions & 9 deletions classes/Tools/MigrationsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,20 @@ public function displayMigrationErrors() {
return;
}

$lastVersion = Environment::Option('mcloud_migration_last_version', null, '3.0.0');
if (current_user_can('manage_options')) {
$lastVersion = Environment::Option('mcloud_migration_last_version', null, '3.0.0');

$exist = [];
foreach($this->deprecatedErrors as $oldEndVar => $newEnvVar) {
$exist[] = "<li><code>$oldEndVar</code> is now <code>$newEnvVar</code></li>";
}
$exist = [];
foreach($this->deprecatedErrors as $oldEndVar => $newEnvVar) {
$exist[] = "<li><code>$oldEndVar</code> is now <code>$newEnvVar</code></li>";
}

$message = "You have have outdated environmental variables defined. Please try to change them as soon as possible. The deprecated environment variables are: <ul>";
$message .= implode("\n", $exist);
$message .= '</ul>';
$message = "You have have outdated environmental variables defined. Please try to change them as soon as possible. The deprecated environment variables are: <ul>";
$message .= implode("\n", $exist);
$message .= '</ul>';

NoticeManager::instance()->displayAdminNotice('error', $message,true, 'mcloud-deprecated-env-'.str_replace('.','_',$lastVersion), 1);
NoticeManager::instance()->displayAdminNotice('error', $message,true, 'mcloud-deprecated-env-'.str_replace('.','_',$lastVersion), 1);
}
}
//endregion
}
10 changes: 7 additions & 3 deletions classes/Tools/Storage/Driver/Backblaze/BackblazeStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,18 @@ public function settings() {

public function enabled() {
if(!($this->settings->key && $this->settings->accountId && $this->settings->bucket)) {
$adminUrl = admin_url('admin.php?page=media-cloud-settings&tab=storage');
NoticeManager::instance()->displayAdminNotice('error', "To start using Cloud Storage, you will need to <a href='$adminUrl'>supply your Backblaze credentials.</a>.", true, 'ilab-cloud-storage-setup-warning', 'forever');
if (current_user_can('manage_options')) {
$adminUrl = admin_url('admin.php?page=media-cloud-settings&tab=storage');
NoticeManager::instance()->displayAdminNotice('error', "To start using Cloud Storage, you will need to <a href='$adminUrl'>supply your Backblaze credentials.</a>.", true, 'ilab-cloud-storage-setup-warning', 'forever');
}

return false;
}

if($this->settings->settingsError) {
NoticeManager::instance()->displayAdminNotice('error', 'Your Backblaze settings are incorrect or the bucket does not exist. Please verify your settings and update them.');
if (current_user_can('manage_options')) {
NoticeManager::instance()->displayAdminNotice('error', 'Your Backblaze settings are incorrect or the bucket does not exist. Please verify your settings and update them.');
}

return false;
}
Expand Down
75 changes: 71 additions & 4 deletions classes/Tools/Storage/Driver/Cloudflare/CloudflareStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
namespace MediaCloud\Plugin\Tools\Storage\Driver\Cloudflare;

use MediaCloud\Plugin\Tools\Storage\Driver\S3\OtherS3Storage;
use MediaCloud\Plugin\Tools\Storage\Driver\S3\S3UploadInfo;
use MediaCloud\Plugin\Tools\Storage\StorageException;
use MediaCloud\Plugin\Tools\Storage\StorageToolSettings;
use MediaCloud\Plugin\Utilities\Environment;
use MediaCloud\Plugin\Utilities\Logging\Logger;
use MediaCloud\Plugin\Utilities\NoticeManager;
use MediaCloud\Vendor\Aws\CloudFront\CloudFrontClient;
use MediaCloud\Plugin\Tools\Storage\InvalidStorageSettingsException;
use MediaCloud\Vendor\Aws\Exception\AwsException;
use MediaCloud\Vendor\Aws\S3\PostObjectV4;
use function MediaCloud\Plugin\Utilities\anyNull;
use MediaCloud\Plugin\Utilities\Tracker;
use MediaCloud\Plugin\Wizard\WizardBuilder;
Expand Down Expand Up @@ -74,7 +79,7 @@ public function __construct() {

//region Enabled/Options
public function supportsDirectUploads() {
return false;
return true;
}

public static function settingsErrorOptionName() {
Expand All @@ -95,8 +100,10 @@ public static function settingsClass() {

public function enabled() {
if (!($this->settings->key && $this->settings->secret && $this->settings->bucket && $this->settings->endpoint && $this->settings->publicBucketUrl)) {
$adminUrl = admin_url('admin.php?page=media-cloud-settings&tab=storage');
NoticeManager::instance()->displayAdminNotice('info', "Welcome to Media Cloud! To get started, <a href='$adminUrl'>configure your cloud storage</a>.", true, 'ilab-cloud-storage-setup-warning', 'forever');
if (current_user_can('manage_options')) {
$adminUrl = admin_url('admin.php?page=media-cloud-settings&tab=storage');
NoticeManager::instance()->displayAdminNotice('info', "Welcome to Media Cloud! To get started, <a href='$adminUrl'>configure your cloud storage</a>.", true, 'ilab-cloud-storage-setup-warning', 'forever');
}

return false;
}
Expand Down Expand Up @@ -143,8 +150,68 @@ public function url($key, $type = null) {
//endregion

//region Direct Uploads
/**
* Returns the options data for generating the policy for uploads
*
* @param $acl
* @param $key
*
* @return array
*/
protected function getOptionsData($acl, $key) {
$keyparts = explode('.', $key);

if ($this->canUpdateACL()) {
return [
['bucket' => $this->settings->bucket],
// ['key' => $key],
['starts-with', '$key', $keyparts[0]],
['starts-with', '$Content-Type', '']
];
} else {
return [
['bucket' => $this->settings->bucket],
// ['key' => $key],
['starts-with', '$key', $keyparts[0]],
['starts-with', '$Content-Type', '']
];
}
}

public function uploadUrl($key, $acl, $mimeType = null, $cacheControl = null, $expires = null) {
try {
$options = [
'Bucket' => $this->settings->bucket,
'Key' => $key,
];

if ($mimeType) {
$options['ContentType'] = $mimeType;
}

if ($cacheControl) {
$options['CacheControl'] = $cacheControl;
}

if ($expires) {
$options['Expires'] = $expires;
}

$command = $this->client->getCommand('PutObject', $options);
$result = $this->client->createPresignedRequest($command, '+15 minutes');
$url = (string)$result->getUri();

return new CloudflareUploadInfo($url, $key, $mimeType, $acl, $cacheControl, $expires);
} catch(AwsException $ex) {
Logger::error('S3 Generate File Upload URL Error', ['exception' => $ex->getMessage()], __METHOD__, __LINE__);
throw new StorageException($ex->getMessage(), $ex->getCode(), $ex);
}
}

public function enqueueUploaderScripts() {
wp_enqueue_script('ilab-media-upload-other-s3', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-other-s3.js', [], MEDIA_CLOUD_VERSION, true);
// wp_enqueue_script('ilab-media-direct-upload-other-s3', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-other-s3.js', [], MEDIA_CLOUD_VERSION, true);
wp_enqueue_script('ilab-media-direct-upload-r2', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-r2.js', [], MEDIA_CLOUD_VERSION, true);
// wp_enqueue_script('ilab-media-upload-other-s3', ILAB_PUB_JS_URL.'/ilab-media-direct-upload-other-s3.js', [], MEDIA_CLOUD_VERSION, true);
}
//endregion

Expand Down
97 changes: 97 additions & 0 deletions classes/Tools/Storage/Driver/Cloudflare/CloudflareUploadInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
// Copyright (c) 2016 Interfacelab LLC. All rights reserved.
//
// Released under the GPLv3 license
// http://www.gnu.org/licenses/gpl-3.0.html
//
// Uses code from:
// Persist Admin Notices Dismissal
// by Agbonghama Collins and Andy Fragen
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// **********************************************************************

namespace MediaCloud\Plugin\Tools\Storage\Driver\Cloudflare;


use MediaCloud\Plugin\Tools\Storage\UploadInfo;

/**
* Class S3UploadInfo
* @package MediaCloud\Plugin\Tools\Storage\Driver\S3
*/
class CloudflareUploadInfo extends UploadInfo {
private $key;
private $url;
private $acl;
private $cacheControl;
private $expires;

/**
* S3UploadInfo constructor.
*
* @param string $url
* @param string $key
* @param string $acl
* @param string $mimeType
* @param string|null $cacheControl
* @param string|null $expires
*/
public function __construct($url, $key, $mimeType = null, $acl = null, $cacheControl=null, $expires=null) {
$this->url = $url;
$this->key = $key;
$this->acl = $acl;
$this->mimeType = $mimeType;
$this->cacheControl = $cacheControl;
$this->expires = $expires;
}

public function key() {
return $this->key;
}

public function url() {
return $this->url;
}

public function formData() {
return [];
}

public function cacheControl() {
return $this->cacheControl;
}

public function expires() {
return $this->expires;
}

public function acl() {
return $this->acl;
}

public function mimeType() {
return $this->mimeType;
}


/**
* Returns the upload info as an array
*
* @return array
*/
public function toArray() {
return [
'key' => $this->key(),
'url' => $this->url(),
'mimeType' => $this->mimeType(),
'formData' => $this->formData(),
'cacheControl' => $this->cacheControl(),
'expires' => $this->expires(),
'acl' => $this->acl()
];
}
}
11 changes: 8 additions & 3 deletions classes/Tools/Storage/Driver/GoogleCloud/GoogleStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,18 @@ public function validateSettings($errorCollector = null) {

public function enabled() {
if(empty($this->settings->credentials) || (!is_array($this->settings->credentials)) || empty($this->settings->bucket)) {
$adminUrl = admin_url('admin.php?page=media-cloud-settings&tab=storage');
NoticeManager::instance()->displayAdminNotice('info', "Welcome to Media Cloud! To get started, <a href='$adminUrl'>configure your cloud storage</a>.", true, 'ilab-cloud-storage-setup-warning', 'forever');
if (current_user_can('manage_options')) {
$adminUrl = admin_url('admin.php?page=media-cloud-settings&tab=storage');
NoticeManager::instance()->displayAdminNotice('info', "Welcome to Media Cloud! To get started, <a href='$adminUrl'>configure your cloud storage</a>.", true, 'ilab-cloud-storage-setup-warning', 'forever');
}
return false;
}

if($this->settingsError) {
NoticeManager::instance()->displayAdminNotice('error', "Your Google Storage settings are incorrect, or your account doesn't have the correct permissions or the bucket does not exist. Please verify your settings and update them.");
if (current_user_can('manage_options')) {
NoticeManager::instance()->displayAdminNotice('error', "Your Google Storage settings are incorrect, or your account doesn't have the correct permissions or the bucket does not exist. Please verify your settings and update them.");
}

return false;
}

Expand Down
6 changes: 4 additions & 2 deletions classes/Tools/Storage/Driver/S3/S3Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,10 @@ public function validateSettings($errorCollector = null) {

public function enabled() {
if (!((($this->settings->key && $this->settings->secret) || $this->settings->useCredentialProvider) && $this->settings->bucket)) {
$adminUrl = admin_url('admin.php?page=media-cloud-settings&tab=storage');
NoticeManager::instance()->displayAdminNotice('info', "Welcome to Media Cloud! To get started, <a href='$adminUrl'>configure your cloud storage</a>.", true, 'ilab-cloud-storage-setup-warning', 'forever');
if (current_user_can('manage_options')) {
$adminUrl = admin_url('admin.php?page=media-cloud-settings&tab=storage');
NoticeManager::instance()->displayAdminNotice('info', "Welcome to Media Cloud! To get started, <a href='$adminUrl'>configure your cloud storage</a>.", true, 'ilab-cloud-storage-setup-warning', 'forever');
}

return false;
}
Expand Down
10 changes: 7 additions & 3 deletions classes/Tools/Storage/Driver/Supabase/SupabaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,18 @@ public function settings() {

public function enabled() {
if(!($this->settings->key && $this->settings->storageUrl && $this->settings->bucket)) {
$adminUrl = admin_url('admin.php?page=media-cloud-settings&tab=storage');
NoticeManager::instance()->displayAdminNotice('error', "To start using Cloud Storage, you will need to <a href='$adminUrl'>supply your Supabase credentials.</a>.", true, 'ilab-cloud-storage-setup-warning', 'forever');
if (current_user_can('manage_options')) {
$adminUrl = admin_url('admin.php?page=media-cloud-settings&tab=storage');
NoticeManager::instance()->displayAdminNotice('error', "To start using Cloud Storage, you will need to <a href='$adminUrl'>supply your Supabase credentials.</a>.", true, 'ilab-cloud-storage-setup-warning', 'forever');
}

return false;
}

if($this->settings->settingsError) {
NoticeManager::instance()->displayAdminNotice('error', 'Your Supabase settings are incorrect or the bucket does not exist. Please verify your settings and update them.');
if (current_user_can('manage_options')) {
NoticeManager::instance()->displayAdminNotice('error', 'Your Supabase settings are incorrect or the bucket does not exist. Please verify your settings and update them.');
}

return false;
}
Expand Down
12 changes: 8 additions & 4 deletions classes/Tools/Storage/StorageGlobals.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ private function __construct() {
}

if(!in_array($this->privacy, ['public-read', 'authenticated-read', 'private'])) {
NoticeManager::instance()->displayAdminNotice('error', "Your AWS S3 settings are incorrect. The ACL '{$this->privacy}' is not valid. Defaulting to 'public-read'.");
if (current_user_can('manage_options')) {
NoticeManager::instance()->displayAdminNotice('error', "Your AWS S3 settings are incorrect. The ACL '{$this->privacy}' is not valid. Defaulting to 'public-read'.");
}

$this->privacy = 'public-read';
}

Expand Down Expand Up @@ -182,10 +185,9 @@ private function __construct() {
$this->expires = gmdate('D, d M Y H:i:s \G\M\T', time() + ($expires * 60));
}

if (is_multisite() && is_network_admin() && empty($this->prefixFormat)) {
if (current_user_can('manage_options') && is_multisite() && is_network_admin() && empty($this->prefixFormat)) {
$rootSiteName = get_network()->site_name;
$adminUrl = network_admin_url('admin.php?page=media-cloud-settings&tab=storage#upload-handling');

NoticeManager::instance()->displayAdminNotice('warning', "You are using Multisite WordPress but have not set a custom upload directory. Your root site, <strong>'{$rootSiteName}'</strong> will be uploading to the root of your cloud storage which may not be desirable. It's recommended that you set the Upload Directory to <code>sites/@{site-id}/@{date:Y/m}</code> in <a href='{$adminUrl}'>Cloud Storage</a> settings.", true, 'mcloud-multisite-missing-upload-path');
}
}
Expand Down Expand Up @@ -404,7 +406,9 @@ public static function migrateFromOtherPlugin() {

if (!empty($migrated)) {
Environment::UpdateOption('mcloud-tool-enabled-storage', true);
NoticeManager::instance()->displayAdminNotice('info', "Media Cloud noticed you were using {$migratedFrom} and has migrated your settings automatically. Everything should be working as before, but make sure to double check your Cloud Storage settings.", true, 'mcloud-migrated-other-plugin', 'forever');
if (current_user_can('manage_options')) {
NoticeManager::instance()->displayAdminNotice('info', "Media Cloud noticed you were using {$migratedFrom} and has migrated your settings automatically. Everything should be working as before, but make sure to double check your Cloud Storage settings.", true, 'mcloud-migrated-other-plugin', 'forever');
}
update_option('mcloud-other-plugins-did-migrate', $migratedFrom);
}

Expand Down
Loading

0 comments on commit e6f5a49

Please sign in to comment.