| 1 |
<?php |
| 2 |
namespace Elementor\Core\Admin\Notices; |
| 3 |
|
| 4 |
use Elementor\User; |
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Settings; |
| 7 |
use Elementor\Core\Experiments\Manager as Experiments_Manager; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Elementor_Dev_Notice extends Base_Notice { |
| 14 |
/** |
| 15 |
* Notice ID. |
| 16 |
*/ |
| 17 |
const ID = 'elementor_dev_promote'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Plugin slug to install. |
| 21 |
*/ |
| 22 |
const PLUGIN_SLUG = 'elementor-beta'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Plugin name. |
| 26 |
*/ |
| 27 |
const PLUGIN_NAME = 'elementor-beta/elementor-beta.php'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Holds the plugins names. |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
private $plugins = []; |
| 35 |
|
| 36 |
/** |
| 37 |
* If one of those plugin is installed it will show the notice. |
| 38 |
* |
| 39 |
* @var string[] |
| 40 |
*/ |
| 41 |
private $promotion_plugins = [ |
| 42 |
'woocommerce-beta-tester/woocommerce-beta-tester.php', |
| 43 |
'wp-jquery-update-test/wp-jquery-update-test.php', |
| 44 |
'wordpress-beta-tester/wp-beta-tester.php', |
| 45 |
'gutenberg/gutenberg.php', |
| 46 |
]; |
| 47 |
|
| 48 |
/** |
| 49 |
* If one of those options is enabled it will show the notice. |
| 50 |
* |
| 51 |
* @var string[] |
| 52 |
*/ |
| 53 |
private $promotion_options = [ |
| 54 |
'elementor_beta', |
| 55 |
]; |
| 56 |
|
| 57 |
/** |
| 58 |
* @inheritDoc |
| 59 |
*/ |
| 60 |
public function should_print() { |
| 61 |
return current_user_can( 'install_plugins' ) && |
| 62 |
! User::is_user_notice_viewed( static::ID ) && |
| 63 |
! $this->is_elementor_dev_installed() && |
| 64 |
! $this->is_install_screen() && |
| 65 |
( |
| 66 |
( |
| 67 |
$this->has_at_least_one_active_experiment() && |
| 68 |
$this->is_elementor_setting_page() |
| 69 |
) || |
| 70 |
$this->is_promotion_plugins_installed() || |
| 71 |
$this->is_promotion_options_enabled() |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @inheritDoc |
| 77 |
*/ |
| 78 |
public function get_config() { |
| 79 |
return [ |
| 80 |
'id' => static::ID, |
| 81 |
'title' => esc_html__( 'Elementor Developer Edition', 'elementor' ), |
| 82 |
'description' => __( |
| 83 |
'Get a sneak peek at our in progress development versions, and help us improve Elementor to perfection. Developer Edition releases contain experimental functionality for testing purposes.', |
| 84 |
'elementor' |
| 85 |
), |
| 86 |
'button' => [ |
| 87 |
'text' => esc_html__( 'Install & Activate', 'elementor' ), |
| 88 |
'url' => wp_nonce_url( |
| 89 |
self_admin_url( 'update.php?action=install-plugin&plugin=' . static::PLUGIN_SLUG ), |
| 90 |
'install-plugin_' . static::PLUGIN_SLUG |
| 91 |
), |
| 92 |
'type' => 'cta', |
| 93 |
], |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Return all the plugins names. |
| 99 |
* |
| 100 |
* This method is protected so it can be mocked in tests. |
| 101 |
* |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
protected function get_plugins() { |
| 105 |
if ( ! $this->plugins ) { |
| 106 |
$this->plugins = array_keys( get_plugins() ); |
| 107 |
} |
| 108 |
|
| 109 |
return $this->plugins; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Checks if elementor dev is installed |
| 114 |
* |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
private function is_elementor_dev_installed() { |
| 118 |
return in_array( static::PLUGIN_NAME, $this->get_plugins(), true ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Checks if the admin screen is install screen. |
| 123 |
* |
| 124 |
* @return bool |
| 125 |
*/ |
| 126 |
private function is_install_screen() { |
| 127 |
$screen = get_current_screen(); |
| 128 |
|
| 129 |
if ( ! $screen ) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
return 'update' === $screen->id; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Checks if is one of the promotion plugins is installed |
| 138 |
* |
| 139 |
* @return bool |
| 140 |
*/ |
| 141 |
private function is_promotion_plugins_installed() { |
| 142 |
return array_reduce( $this->promotion_plugins, function ( $should_show_notice, $plugin_name ) { |
| 143 |
if ( $should_show_notice ) { |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
return in_array( $plugin_name, $this->get_plugins(), true ); |
| 148 |
}, false ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Checks if is one of the promotion options is enabled. |
| 153 |
* |
| 154 |
* @return bool |
| 155 |
*/ |
| 156 |
private function is_promotion_options_enabled() { |
| 157 |
return array_reduce( $this->promotion_options, function ( $should_show_notice, $option ) { |
| 158 |
if ( $should_show_notice ) { |
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
return 'yes' === get_option( $option, 'no' ); |
| 163 |
}, false ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Checks if as at least one active experiment (The state must be "active" and not default-active). |
| 168 |
* |
| 169 |
* @return bool |
| 170 |
*/ |
| 171 |
private function has_at_least_one_active_experiment() { |
| 172 |
foreach ( Plugin::$instance->experiments->get_features() as $feature_name => $feature ) { |
| 173 |
if ( Experiments_Manager::STATE_ACTIVE === $feature['state'] ) { |
| 174 |
return true; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Checks if the current page is elementor settings page |
| 183 |
* |
| 184 |
* @return bool |
| 185 |
*/ |
| 186 |
private function is_elementor_setting_page() { |
| 187 |
$current_screen = get_current_screen(); |
| 188 |
|
| 189 |
return $current_screen && 'toplevel_page_' . Settings::PAGE_ID === $current_screen->id; |
| 190 |
} |
| 191 |
} |
| 192 |
|