| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Promotions; |
| 4 |
|
| 5 |
use Elementor\Api; |
| 6 |
use Elementor\Controls_Manager; |
| 7 |
use Elementor\Core\Admin\Menu\Admin_Menu_Manager; |
| 8 |
use Elementor\Core\Base\Module as Base_Module; |
| 9 |
use Elementor\Modules\Promotions\AdminMenuItems\Custom_Code_Promotion_Item; |
| 10 |
use Elementor\Modules\Promotions\AdminMenuItems\Custom_Fonts_Promotion_Item; |
| 11 |
use Elementor\Modules\Promotions\AdminMenuItems\Custom_Icons_Promotion_Item; |
| 12 |
use Elementor\Modules\Promotions\AdminMenuItems\Form_Submissions_Promotion_Item; |
| 13 |
use Elementor\Modules\Promotions\AdminMenuItems\Go_Pro_Promotion_Item; |
| 14 |
use Elementor\Modules\Promotions\AdminMenuItems\Popups_Promotion_Item; |
| 15 |
use Elementor\Modules\Promotions\Pointers\Birthday; |
| 16 |
use Elementor\Widgets_Manager; |
| 17 |
use Elementor\Utils; |
| 18 |
use Elementor\Includes\EditorAssetsAPI; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; // Exit if accessed directly. |
| 22 |
} |
| 23 |
|
| 24 |
class Module extends Base_Module { |
| 25 |
|
| 26 |
const ADMIN_MENU_PRIORITY = 100; |
| 27 |
|
| 28 |
const ADMIN_MENU_PROMOTIONS_PRIORITY = 120; |
| 29 |
|
| 30 |
public static function is_active() { |
| 31 |
return ! Utils::has_pro(); |
| 32 |
} |
| 33 |
|
| 34 |
public function get_name() { |
| 35 |
return 'promotions'; |
| 36 |
} |
| 37 |
|
| 38 |
public function __construct() { |
| 39 |
parent::__construct(); |
| 40 |
|
| 41 |
add_action( 'admin_init', function () { |
| 42 |
$this->handle_external_redirects(); |
| 43 |
} ); |
| 44 |
|
| 45 |
add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) { |
| 46 |
$this->register_menu_items( $admin_menu ); |
| 47 |
}, static::ADMIN_MENU_PRIORITY ); |
| 48 |
|
| 49 |
add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) { |
| 50 |
$this->register_promotion_menu_item( $admin_menu ); |
| 51 |
}, static::ADMIN_MENU_PROMOTIONS_PRIORITY ); |
| 52 |
|
| 53 |
add_action( 'elementor/widgets/register', function( Widgets_Manager $manager ) { |
| 54 |
foreach ( Api::get_promotion_widgets() as $widget_data ) { |
| 55 |
$manager->register( new Widgets\Pro_Widget_Promotion( [], [ |
| 56 |
'widget_name' => $widget_data['name'], |
| 57 |
'widget_title' => $widget_data['title'], |
| 58 |
] ) ); |
| 59 |
} |
| 60 |
} ); |
| 61 |
|
| 62 |
if ( Birthday::should_display_notice() ) { |
| 63 |
new Birthday(); |
| 64 |
} |
| 65 |
|
| 66 |
if ( Utils::has_pro() ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
add_action( 'elementor/controls/register', function ( Controls_Manager $controls_manager ) { |
| 71 |
$controls_manager->register( new Controls\Promotion_Control() ); |
| 72 |
} ); |
| 73 |
|
| 74 |
add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'enqueue_react_data' ] ); |
| 75 |
add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'enqueue_editor_v4_alphachip' ] ); |
| 76 |
} |
| 77 |
|
| 78 |
private function handle_external_redirects() { |
| 79 |
if ( empty( $_GET['page'] ) ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
if ( 'go_elementor_pro' === $_GET['page'] ) { |
| 84 |
wp_redirect( Go_Pro_Promotion_Item::get_url() ); |
| 85 |
die; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
private function register_menu_items( Admin_Menu_Manager $admin_menu ) { |
| 90 |
$admin_menu->register( 'e-form-submissions', new Form_Submissions_Promotion_Item() ); |
| 91 |
$admin_menu->register( 'elementor_custom_fonts', new Custom_Fonts_Promotion_Item() ); |
| 92 |
$admin_menu->register( 'elementor_custom_icons', new Custom_Icons_Promotion_Item() ); |
| 93 |
$admin_menu->register( 'elementor_custom_code', new Custom_Code_Promotion_Item() ); |
| 94 |
$admin_menu->register( 'popup_templates', new Popups_Promotion_Item() ); |
| 95 |
} |
| 96 |
|
| 97 |
private function register_promotion_menu_item( Admin_Menu_Manager $admin_menu ) { |
| 98 |
$admin_menu->register( 'go_elementor_pro', new Go_Pro_Promotion_Item() ); |
| 99 |
} |
| 100 |
|
| 101 |
public function enqueue_react_data(): void { |
| 102 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
$min_suffix = Utils::is_script_debug() ? '' : '.min'; |
| 107 |
|
| 108 |
wp_enqueue_script( |
| 109 |
'e-react-promotions', |
| 110 |
ELEMENTOR_ASSETS_URL . 'js/e-react-promotions' . $min_suffix . '.js', |
| 111 |
[ |
| 112 |
'react', |
| 113 |
'react-dom', |
| 114 |
'backbone-marionette', |
| 115 |
'elementor-editor-modules', |
| 116 |
'elementor-v2-ui', |
| 117 |
], |
| 118 |
ELEMENTOR_VERSION, |
| 119 |
true |
| 120 |
); |
| 121 |
|
| 122 |
wp_set_script_translations( 'e-react-promotions', 'elementor' ); |
| 123 |
|
| 124 |
wp_localize_script( |
| 125 |
'e-react-promotions', |
| 126 |
'elementorPromotionsData', |
| 127 |
$this->get_app_js_config() |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
public function enqueue_editor_v4_alphachip(): void { |
| 132 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$min_suffix = Utils::is_script_debug() ? '' : '.min'; |
| 137 |
|
| 138 |
wp_enqueue_script( |
| 139 |
'editor-v4-opt-in-alphachip', |
| 140 |
ELEMENTOR_ASSETS_URL . 'js/editor-v4-opt-in-alphachip' . $min_suffix . '.js', |
| 141 |
[ |
| 142 |
'react', |
| 143 |
'react-dom', |
| 144 |
'elementor-common', |
| 145 |
'elementor-v2-ui', |
| 146 |
], |
| 147 |
ELEMENTOR_VERSION, |
| 148 |
true |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
private function get_app_js_config(): array { |
| 153 |
$editor_assets_api = new EditorAssetsAPI( $this->get_api_config() ); |
| 154 |
$promotion_data = new PromotionData( $editor_assets_api ); |
| 155 |
|
| 156 |
return $promotion_data->get_promotion_data(); |
| 157 |
} |
| 158 |
|
| 159 |
private function get_api_config(): array { |
| 160 |
return [ |
| 161 |
EditorAssetsAPI::ASSETS_DATA_URL => 'https://assets.elementor.com/free-to-pro-upsell/v1/free-to-pro-upsell.json', |
| 162 |
EditorAssetsAPI::ASSETS_DATA_TRANSIENT_KEY => '_elementor_free_to_pro_upsell', |
| 163 |
EditorAssetsAPI::ASSETS_DATA_KEY => 'free-to-pro-upsell', |
| 164 |
]; |
| 165 |
} |
| 166 |
} |
| 167 |
|