is_crm_hook( (string) $hook ) ) { return; } $asset_file = MAILPUFF_PLUGIN_DIR . 'build/admin/index.asset.php'; if ( ! file_exists( $asset_file ) ) { return; } $asset = include $asset_file; wp_enqueue_media(); wp_enqueue_script( self::SCRIPT, MAILPUFF_PLUGIN_URL . 'build/admin/index.js', // WordPress's command palette carries Mailpuff's quick actions // (src/admin/shell/wp-commands.js); its store must exist first. array_unique( array_merge( $asset['dependencies'], array( 'wp-commands' ) ) ), $asset['version'], true ); wp_set_script_translations( self::SCRIPT, 'mailpuff' ); // The form builder's templates, styles and fonts ride with the page's // own data only; the email editor has no use for them. wp_localize_script( self::SCRIPT, 'mailpuffData', array_merge( $this->get_shared_data(), $this->form_data() ) ); if ( file_exists( MAILPUFF_PLUGIN_DIR . 'build/admin/index.css' ) ) { wp_enqueue_style( self::SCRIPT, MAILPUFF_PLUGIN_URL . 'build/admin/index.css', array(), $asset['version'] ); } // Mailpuff sits inside wp-admin, under WordPress's own bar and menu. // Notices other plugins print above the page are hidden, as the // block editor does: the app shows its own. WordPress's update nag // stays. Printed inline so they never flash before the bundle's // stylesheet arrives. wp_register_style( 'mailpuff-page', false, array(), $asset['version'] ); wp_enqueue_style( 'mailpuff-page' ); $body = '.' . Mailpuff_Menu::BODY_CLASS; // WordPress's footer carries Mailpuff's line (Mailpuff_Menu::footer_text(), // footer_version()), lined up with the app's column (68rem, 40px // sides, 32px under 1100px); WordPress hides it on phones. $footer = '@media screen and (min-width:783px){' . $body . ' #wpfooter{display:grid;grid-template-columns:minmax(40px,1fr) minmax(0,calc(68rem - 80px)) minmax(40px,1fr);align-items:center;padding:16px 0;color:#6b6a78;font-family:"Mona Sans",-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}' . $body . ' #wpfooter p{grid-row:1;grid-column:2;display:flex;align-items:center;gap:16px;font-size:13px;line-height:20px}' . $body . ' #footer-left{justify-self:start}' . $body . ' #footer-upgrade{justify-self:end}' . $body . ' #wpfooter .clear{display:none}' . $body . ' .mailpuff-footer__thanks{display:inline-flex;align-items:center;gap:8px}' . $body . ' .mailpuff-footer__mark{width:16px;height:16px;color:#5d5cc2}' . $body . ' #wpfooter a{color:#4e4bac;text-decoration:none;border-radius:2px}' . $body . ' #wpfooter a:hover{text-decoration:underline;text-underline-offset:3px}' . $body . ' #wpfooter a:focus{box-shadow:none;outline:2px solid #5d5cc2;outline-offset:2px}' . '}@media screen and (min-width:783px) and (max-width:1099px){' . $body . ' #wpfooter{grid-template-columns:minmax(32px,1fr) minmax(0,calc(68rem - 64px)) minmax(32px,1fr)}' . '}'; wp_add_inline_style( 'mailpuff-page', $body . ' #wpcontent{padding-left:0}' . $body . ' #wpbody-content>.notice:not(.update-nag),' . $body . ' #wpbody-content>.updated,' . $body . ' #wpbody-content>.error{display:none}' . $footer ); /** * Fires after Mailpuff's admin app is enqueued. An add-on (Mailpuff * Pro) enqueues its own script here with `mailpuff-admin` as a * dependency; the app mounts on DOMContentLoaded, so the add-on's * `mailpuff.*` filters are in place before the first render. * * @param string $handle The admin app's script handle. */ do_action( 'mailpuff_admin_enqueue_scripts', self::SCRIPT ); } /** * Mark CRM's page and the email editor page on the body. * * @param string $classes Space-separated body classes. * @return string */ public function body_class( $classes ) { if ( $this->is_page( Mailpuff_Menu::EDITOR_SLUG ) ) { $classes .= ' ' . self::EDITOR_BODY_CLASS; } if ( $this->is_page( Mailpuff_Menu::PAGE_SLUG ) ) { $classes .= ' ' . Mailpuff_Menu::BODY_CLASS; } return $classes; } /** * Enqueue the email editor's bundle on its page. */ public function enqueue_scripts() { if ( $this->is_page( Mailpuff_Menu::EDITOR_SLUG ) ) { $this->enqueue_editor(); } } /** * Enqueue the email editor. */ private function enqueue_editor() { $asset_file = MAILPUFF_PLUGIN_DIR . 'build/editor/editor.asset.php'; if ( ! file_exists( $asset_file ) ) { return; } $asset = include $asset_file; wp_enqueue_media(); wp_enqueue_script( 'mailpuff-editor', MAILPUFF_PLUGIN_URL . 'build/editor/editor.js', $asset['dependencies'], $asset['version'], true ); wp_set_script_translations( 'mailpuff-editor', 'mailpuff' ); // The editor's strings live in its lazy chunk, which webpack loads on // its own. The chunk's handle is registered, never enqueued, so that // WordPress finds the chunk's translation file; its strings print // before editor.js. wp_register_script( 'mailpuff-editor-tiptap', MAILPUFF_PLUGIN_URL . 'build/editor/editor-tiptap.js', array( 'wp-i18n' ), $asset['version'], true ); wp_set_script_translations( 'mailpuff-editor-tiptap', 'mailpuff' ); $chunk_translations = wp_scripts()->print_translations( 'mailpuff-editor-tiptap', false ); if ( $chunk_translations ) { wp_add_inline_script( 'mailpuff-editor', $chunk_translations, 'before' ); } // Editor styles. wp-block-editor is still needed: the inspector's // EmailSettingsPanel renders PanelColorSettings from that package. // The editor's own CSS ships inside its webpack chunks (loaded by // editor.js at runtime), so there is no style file to enqueue here. wp_enqueue_style( 'wp-components' ); wp_enqueue_style( 'wp-block-editor' ); // Inside wp-admin: WordPress's bar and menu stay; the editor fills the // rest of the window, so the footer and the content padding go. wp_register_style( 'mailpuff-editor-screen', false, array(), $asset['version'] ); wp_enqueue_style( 'mailpuff-editor-screen' ); wp_add_inline_style( 'mailpuff-editor-screen', '#wpfooter{display:none!important}' . '#wpcontent{padding-left:0!important}' . '#wpbody-content{padding-bottom:0!important;float:none}' . '#mailpuff-editor{margin:0}' ); wp_localize_script( 'mailpuff-editor', 'mailpuffData', $this->get_shared_data() ); } /** * Shared data passed to both apps. */ private function get_shared_data() { $data = array( 'nonce' => wp_create_nonce( 'wp_rest' ), 'restUrl' => esc_url_raw( get_rest_url( null, 'mailpuff/v1/' ) ), 'adminUrl' => esc_url_raw( admin_url() ), 'editorUrl' => esc_url_raw( admin_url( 'admin.php?page=mailpuff-editor' ) ), // Every dashboard deep link (incl. the editor's back-navigation). 'dashboardUrl' => esc_url_raw( admin_url( 'admin.php?page=mailpuff' ) ), 'pageSlug' => Mailpuff_Menu::PAGE_SLUG, // Settings show the WooCommerce options only with WooCommerce. 'woocommerce' => Mailpuff_WooCommerce::active(), 'pageUrl' => esc_url_raw( admin_url( 'admin.php?page=mailpuff' ) ), // The email's social icons load from the site (assets/social/). 'socialIconsUrl' => esc_url_raw( MAILPUFF_PLUGIN_URL . 'assets/social/' ), // The plugin's assets folder, for images the admin shows (logos). 'assetsUrl' => esc_url_raw( MAILPUFF_PLUGIN_URL . 'assets/' ), 'setupComplete' => get_option( 'mailpuff_setup_complete', '0' ), 'userId' => get_current_user_id(), 'userName' => wp_get_current_user()->display_name, 'userEmail' => wp_get_current_user()->user_email, 'version' => MAILPUFF_VERSION, 'brand' => get_option( 'mailpuff_brand_settings', array() ), 'emailFooter' => (string) ( ( (array) get_option( 'mailpuff_email_settings', array() ) )['footer'] ?? '' ), 'smartcodes' => Mailpuff_Smartcodes::groups(), 'delivery' => Mailpuff_Mailer::get_instance()->status(), // First run: the welcome opens while it is new or under way. 'onboarding' => Mailpuff_Onboarding::get(), 'siteName' => Mailpuff_Mailer::site_name(), 'postshibaKeysUrl' => Mailpuff_PostShiba::KEYS_URL, // Site timezone + current wall-clock — drives the schedule picker so // the times the user picks are read in the site's configured zone. 'site' => array( 'timezone' => wp_timezone_string(), 'gmtOffset' => (float) get_option( 'gmt_offset', 0 ), 'now' => current_time( 'Y-m-d H:i:s' ), 'nowDisplay' => date_i18n( 'M j, Y g:i A' ), ), ); /** * Filter the data the admin screens read as window.mailpuffData. * * @param array $data Data for the screens. */ $data = apply_filters( 'mailpuff_js_data', $data ); return $data; } /** * What the form builder draws with, on Mailpuff's page only: the * templates (with any an add-on registers), every form stylesheet and * font stylesheet, the privacy page and, when the theme has one, its * palette (docs/form-builder.md §7). * * @return array */ private function form_data() { $templates = array(); if ( class_exists( 'Mailpuff_Form_Templates' ) && method_exists( 'Mailpuff_Form_Templates', 'for_js' ) ) { $templates = array_values( (array) Mailpuff_Form_Templates::for_js() ); } $base = MAILPUFF_PLUGIN_URL . 'build/public/forms/base.css'; $styles = array(); $fonts = array(); if ( class_exists( 'Mailpuff_Form_Render' ) && method_exists( 'Mailpuff_Form_Render', 'styles' ) ) { foreach ( (array) Mailpuff_Form_Render::styles() as $handle => $url ) { $key = preg_replace( '/^mailpuff-form-/', '', (string) $handle ); if ( 'base' === $key ) { $base = (string) $url; } else { $styles[ $key ] = (string) $url; } } $fonts = array_values( array_map( 'strval', (array) Mailpuff_Form_Render::font_styles() ) ); } foreach ( $templates as $template ) { $id = sanitize_key( (string) ( $template['id'] ?? '' ) ); if ( '' !== $id && ! isset( $styles[ $id ] ) ) { $styles[ $id ] = ! empty( $template['css'] ) ? (string) $template['css'] : MAILPUFF_PLUGIN_URL . 'build/public/forms/' . $id . '.css'; } } $data = array( 'formTemplates' => $templates, 'formStyles' => array( 'base' => esc_url_raw( $base ), 'templates' => array_map( 'esc_url_raw', $styles ), ), 'formFonts' => array_values( array_filter( array_map( 'esc_url_raw', $fonts ) ) ), 'privacyUrl' => esc_url_raw( (string) get_privacy_policy_url() ), ); $palette = $this->site_palette(); if ( $palette ) { $data['sitePalette'] = $palette; } return $data; } /** * The theme's own colours as a form's four, from its global styles: the * page background and text, and its button (or link) colour. Only for a * theme with theme.json, and only when all four are plain colours. * * @return array|null { background, text, button, button_text }. */ private function site_palette() { if ( ! function_exists( 'wp_theme_has_theme_json' ) || ! wp_theme_has_theme_json() || ! function_exists( 'wp_get_global_styles' ) ) { return null; } $palette = array(); $settings = (array) wp_get_global_settings( array( 'color', 'palette' ) ); // The theme's colours win over the defaults, the owner's over both. foreach ( array( 'default', 'theme', 'custom' ) as $origin ) { foreach ( (array) ( $settings[ $origin ] ?? array() ) as $entry ) { if ( is_array( $entry ) && isset( $entry['slug'], $entry['color'] ) ) { $palette[ (string) $entry['slug'] ] = (string) $entry['color']; } } } $styles = (array) wp_get_global_styles(); $read = static function ( $value ) use ( $palette ) { $value = is_string( $value ) ? trim( $value ) : ''; if ( preg_match( '/^var:preset\|color\|([a-z0-9_-]+)$/i', $value, $m ) || preg_match( '/^var\(\s*--wp--preset--color--([a-z0-9_-]+)\s*\)$/i', $value, $m ) ) { $value = (string) ( $palette[ $m[1] ] ?? '' ); } return (string) sanitize_hex_color( $value ); }; $background = $read( $styles['color']['background'] ?? '' ); $text = $read( $styles['color']['text'] ?? '' ); $button = $read( $styles['elements']['button']['color']['background'] ?? '' ); $button = '' !== $button ? $button : $read( $styles['elements']['link']['color']['text'] ?? '' ); $button = '' !== $button ? $button : $text; $button_text = $read( $styles['elements']['button']['color']['text'] ?? '' ); $button_text = '' !== $button_text ? $button_text : $background; if ( '' === $background || '' === $text || '' === $button || '' === $button_text ) { return null; } return array( 'background' => strtoupper( $background ), 'text' => strtoupper( $text ), 'button' => strtoupper( $button ), 'button_text' => strtoupper( $button_text ), ); } }