| 1 |
<?php |
| 2 |
/** |
| 3 |
* Automations module |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPFunnels\Modules\Admin\Automations; |
| 9 |
|
| 10 |
use WPFunnels\Admin\Module\Wpfnl_Admin_Module; |
| 11 |
use WPFunnels\Traits\SingletonTrait; |
| 12 |
|
| 13 |
class Module extends Wpfnl_Admin_Module |
| 14 |
{ |
| 15 |
use SingletonTrait; |
| 16 |
|
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
// Enqueue automation-canvas scripts |
| 20 |
add_action('admin_enqueue_scripts', [$this, 'enqueue_automation_scripts']); |
| 21 |
|
| 22 |
// Initialize REST API |
| 23 |
require_once WPFNL_DIR . '/admin/modules/automations/class-wpfnl-automation-rest-api.php'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Enqueue automation canvas scripts |
| 28 |
* |
| 29 |
* @param string $hook Current admin page hook |
| 30 |
* @since 3.9.5 |
| 31 |
*/ |
| 32 |
public function enqueue_automation_scripts($hook) |
| 33 |
{ |
| 34 |
// Only load on automation page |
| 35 |
if ('wpfunnels_page_wpfnl_automations' !== $hook) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
// Enqueue wp-tinymce for WYSIWYG editor support |
| 40 |
wp_tinymce_inline_scripts(); |
| 41 |
wp_enqueue_editor(); |
| 42 |
|
| 43 |
// Load automation-canvas script |
| 44 |
$automation_asset_file = WPFNL_DIR . '/admin/assets/dist/js/automation-canvas.min.asset.php'; |
| 45 |
$automation_dependencies = ['wp-element', 'wp-i18n', 'wp-api-fetch', 'wp-data']; |
| 46 |
$automation_version = WPFNL_VERSION; |
| 47 |
|
| 48 |
if (file_exists($automation_asset_file)) { |
| 49 |
$automation_asset = require $automation_asset_file; |
| 50 |
$automation_dependencies = isset($automation_asset['dependencies']) ? $automation_asset['dependencies'] : $automation_dependencies; |
| 51 |
$automation_version = isset($automation_asset['version']) ? $automation_asset['version'] : $automation_version; |
| 52 |
} |
| 53 |
|
| 54 |
// The automation-canvas bundle ships its own copy of lodash (pulled in by |
| 55 |
// easy-email's `import { ... } from "lodash"`). Lodash's UMD wrapper self-registers |
| 56 |
// as the global `_`, clobbering WP core's Underscore.js that media-views.js relies |
| 57 |
// on for `_.each(list, iteratee, context)` context-binding — breaking the media |
| 58 |
// modal ("this.activateMode is not a function") once this script has run. |
| 59 |
// Force load-order against 'underscore' and snapshot/restore the real global |
| 60 |
// around it rather than reworking the bundled easy-email/lodash import graph. |
| 61 |
$automation_dependencies[] = 'underscore'; |
| 62 |
$automation_dependencies = array_unique($automation_dependencies); |
| 63 |
|
| 64 |
wp_add_inline_script( |
| 65 |
'underscore', |
| 66 |
'window.__wpfnlCoreUnderscore = window.__wpfnlCoreUnderscore || window._;', |
| 67 |
'after' |
| 68 |
); |
| 69 |
|
| 70 |
wp_enqueue_script( |
| 71 |
'wpfnl-automation-canvas', |
| 72 |
WPFNL_URL . 'admin/assets/dist/js/automation-canvas.min.js', |
| 73 |
$automation_dependencies, |
| 74 |
$automation_version, |
| 75 |
true |
| 76 |
); |
| 77 |
|
| 78 |
wp_add_inline_script( |
| 79 |
'wpfnl-automation-canvas', |
| 80 |
'if (window.__wpfnlCoreUnderscore) { window._ = window.__wpfnlCoreUnderscore; }', |
| 81 |
'after' |
| 82 |
); |
| 83 |
|
| 84 |
// Enqueue automation-canvas styles |
| 85 |
$automation_css_dir = WPFNL_DIR . '/admin/assets/dist/styles/'; |
| 86 |
if (is_dir($automation_css_dir)) { |
| 87 |
$css_files = glob($automation_css_dir . 'automation-canvas.*.css'); |
| 88 |
if (!empty($css_files)) { |
| 89 |
$css_file = basename($css_files[0]); |
| 90 |
wp_enqueue_style( |
| 91 |
'wpfnl-automation-canvas-styles', |
| 92 |
WPFNL_URL . 'admin/assets/dist/styles/' . $css_file, |
| 93 |
[], |
| 94 |
$automation_version |
| 95 |
); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// The automation editor's own full-screen overlay (.wpfnl-automation-modal, see |
| 100 |
// admin/src/automation-canvas/AutomationModal.scss) sits at z-index: 999999, |
| 101 |
// higher than WP core's default .media-modal z-index — pushing the media |
| 102 |
// library modal behind the editor chrome. Force it back on top. |
| 103 |
wp_add_inline_style( |
| 104 |
'media-views', |
| 105 |
'.media-modal { z-index: 9999999999 !important; } .media-modal-backdrop { z-index: 999999998 !important; }' |
| 106 |
); |
| 107 |
|
| 108 |
// Localize script with data |
| 109 |
wp_localize_script( |
| 110 |
'wpfnl-automation-canvas', |
| 111 |
'wpfnlAutomationVars', |
| 112 |
[ |
| 113 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 114 |
'nonce' => wp_create_nonce('wpfnl_automation_nonce'), |
| 115 |
'restUrl' => rest_url(), |
| 116 |
'restNonce' => wp_create_nonce('wp_rest'), |
| 117 |
] |
| 118 |
); |
| 119 |
|
| 120 |
// Provide MRM_Vars global that automation-canvas components depend on. |
| 121 |
$current_user = wp_get_current_user(); |
| 122 |
$email_settings = get_option( '_mrm_email_settings', array() ); |
| 123 |
$is_pro_active = defined( 'MAIL_MINT_PRO_VERSION' ); |
| 124 |
$is_pro_lic_active = $is_pro_active && class_exists( 'MRM\\Common\\MrmCommon' ) && \MRM\Common\MrmCommon::is_mailmint_pro_license_active(); |
| 125 |
$mint_trans = class_exists( 'Mint\\MRM\\Utilities\\Helper\\TranslationString\\TransStrings' ) |
| 126 |
? \Mint\MRM\Utilities\Helper\TranslationString\TransStrings::getStrings() |
| 127 |
: array(); |
| 128 |
$lists = class_exists( 'Mint\\MRM\\DataBase\\Models\\ContactGroupModel' ) |
| 129 |
? \Mint\MRM\DataBase\Models\ContactGroupModel::get_all_to_custom_select( 'lists' ) |
| 130 |
: array(); |
| 131 |
$tags = class_exists( 'Mint\\MRM\\DataBase\\Models\\ContactGroupModel' ) |
| 132 |
? \Mint\MRM\DataBase\Models\ContactGroupModel::get_all_to_custom_select( 'tags' ) |
| 133 |
: array(); |
| 134 |
|
| 135 |
wp_localize_script( |
| 136 |
'wpfnl-automation-canvas', |
| 137 |
'MRM_Vars', |
| 138 |
array( |
| 139 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 140 |
'api_base_url' => get_rest_url(), |
| 141 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 142 |
'current_userID' => get_current_user_id(), |
| 143 |
'admin_url' => get_admin_url(), |
| 144 |
'date_format' => get_option( 'date_format', 'F j, Y' ), |
| 145 |
'time_format' => get_option( 'time_format', 'H:i' ), |
| 146 |
'gmt_offset' => get_option( 'gmt_offset' ), |
| 147 |
'local_time' => date_i18n( 'Y-m-d H:i:s' ), |
| 148 |
'current_user_email' => $current_user->user_email, |
| 149 |
'email_settings' => $email_settings, |
| 150 |
'lists' => $lists, |
| 151 |
'tags' => $tags, |
| 152 |
'is_wc_active' => class_exists( 'WooCommerce' ), |
| 153 |
'is_edd_active' => defined( 'EDD_VERSION' ), |
| 154 |
'is_mailmint_pro_active' => $is_pro_active, |
| 155 |
'is_mailmint_pro_license_active' => $is_pro_lic_active, |
| 156 |
'is_tutor_active' => defined( 'TUTOR_VERSION' ), |
| 157 |
'is_gform_active' => class_exists( 'GFCommon' ), |
| 158 |
'is_jetform_active' => class_exists( 'Jet_Form_Builder\\Plugin' ), |
| 159 |
'is_fluentform_active' => defined( 'FLUENTFORM' ), |
| 160 |
'is_learndash_active' => defined( 'LEARNDASH_VERSION' ), |
| 161 |
'is_memberpress_active' => defined( 'MEPR_VERSION' ), |
| 162 |
'is_contact_form_active' => defined( 'WPCF7_VERSION' ), |
| 163 |
'is_lifterlms_active' => defined( 'LLMS_VERSION' ), |
| 164 |
'is_fluent_booking_active' => defined( 'FLUENT_BOOKING_VERSION' ), |
| 165 |
'is_wp_form_active' => class_exists( 'WPForms' ), |
| 166 |
'is_bricks_active' => defined( 'BRICKS_VERSION' ), |
| 167 |
'is_wcs_active' => class_exists( 'WC_Subscriptions' ), |
| 168 |
'is_wcm_active' => class_exists( 'WC_Memberships' ), |
| 169 |
'is_wcw_active' => class_exists( 'TInvWishlist' ), |
| 170 |
'mint_trans' => $mint_trans, |
| 171 |
'mint_page' => 'automation', |
| 172 |
'open_ai_key' => array(), |
| 173 |
'twilio_settings' => array(), |
| 174 |
'post_types' => array(), |
| 175 |
'contact_general_fields' => array(), |
| 176 |
'contact_custom_fields' => array(), |
| 177 |
'editor_data_source' => $this->get_editor_data_source(), |
| 178 |
) |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Provide the product/post category options the email builder's custom |
| 184 |
* block panels (e.g. ProductBlock) read from MRM_Vars.editor_data_source. |
| 185 |
* Without this key present, `options={undefined}` is passed to a Select |
| 186 |
* field, which throws "Cannot read properties of undefined (reading 'map')" |
| 187 |
* the moment that panel renders. |
| 188 |
* |
| 189 |
* @return array |
| 190 |
* @since 3.12.10 |
| 191 |
*/ |
| 192 |
private function get_editor_data_source() |
| 193 |
{ |
| 194 |
return array( |
| 195 |
'product_categories' => $this->get_formatted_categories( 'product_cat' ), |
| 196 |
'post_categories' => $this->get_formatted_categories( 'category' ), |
| 197 |
'placeholder_image' => '', |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Fetch terms for a taxonomy formatted as {value, label} pairs. |
| 203 |
* |
| 204 |
* @param string $taxonomy Taxonomy name. |
| 205 |
* @return array |
| 206 |
* @since 3.12.10 |
| 207 |
*/ |
| 208 |
private function get_formatted_categories( $taxonomy ) |
| 209 |
{ |
| 210 |
if ( ! taxonomy_exists( $taxonomy ) ) { |
| 211 |
return array(); |
| 212 |
} |
| 213 |
|
| 214 |
$terms = get_categories( |
| 215 |
array( |
| 216 |
'taxonomy' => $taxonomy, |
| 217 |
'orderby' => 'name', |
| 218 |
'hide_empty' => false, |
| 219 |
) |
| 220 |
); |
| 221 |
|
| 222 |
$formatted = array(); |
| 223 |
foreach ( $terms as $term ) { |
| 224 |
$formatted[] = array( |
| 225 |
'value' => $term->term_id, |
| 226 |
'label' => $term->name, |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
return $formatted; |
| 231 |
} |
| 232 |
|
| 233 |
public function get_view() |
| 234 |
{ |
| 235 |
require_once WPFNL_DIR . '/admin/modules/automations/views/view.php'; |
| 236 |
} |
| 237 |
|
| 238 |
public function get_name() |
| 239 |
{ |
| 240 |
return __('automations','wpfnl'); |
| 241 |
} |
| 242 |
|
| 243 |
public function init_ajax() |
| 244 |
{ |
| 245 |
|
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|