[ 'afromewo-settings-css' => 'assets/css/admin/settings.css', 'afromewo-messaging-templates-css' => 'assets/css/admin/message-template.css', 'afromewo-workflow-css' => 'assets/css/admin/workflow.css', ], 'js' => [ self::HANDLE_SORTABLE => 'assets/js/vendor/sortablejs-1.15.2.min.js', self::HANDLE_MARKDOWN_PARSER_JS => 'assets/js/admin/markdown-parser.js', self::HANDLE_MESSAGE_TEMPLATE_JS => 'assets/js/admin/message-template.js', self::HANDLE_WORKFLOW_ACTIONS_JS => 'assets/js/admin/workflow-actions.js', self::HANDLE_WORKFLOW_JS => 'assets/js/admin/workflow.js', self::HANDLE_EXECUTIONS_JS => 'assets/js/admin/executions.js', ], ]; protected $prod_assets = [ 'css' => [ 'afromewo-admin-css' => 'assets/build/admin.bundle.min.css', ], 'js' => [ self::HANDLE_SORTABLE => 'assets/build/sortablejs-1.15.2.min.js', self::HANDLE_ADMIN_JS => 'assets/build/admin.bundle.min.js', ] ]; protected $dependencies = [ self::HANDLE_MESSAGE_TEMPLATE_JS => ['jquery'], self::HANDLE_WORKFLOW_ACTIONS_JS => ['jquery'], self::HANDLE_WORKFLOW_JS => ['jquery', self::HANDLE_SORTABLE, self::HANDLE_WORKFLOW_ACTIONS_JS], self::HANDLE_EXECUTIONS_JS => ['jquery', 'thickbox'], self::HANDLE_ADMIN_JS => ['jquery', self::HANDLE_SORTABLE], ]; public static function use_raw_assets(): bool { return defined('AFROMEWO_DEV_MODE') && constant('AFROMEWO_DEV_MODE'); } /** * Script handle that receives workflow builder localization. * * Production uses the combined admin bundle; development uses the raw workflow file. * * @return string */ public static function get_workflow_script_handle(): string { return self::use_raw_assets() ? self::HANDLE_WORKFLOW_JS : self::HANDLE_ADMIN_JS; } /** * When true (set in wp-config.php), unminified source files are enqueued instead of bundled assets. * * @return array $assets */ protected function get_assets(): array { return self::use_raw_assets() ? $this->dev_assets : $this->prod_assets; } /** * Register script and style enqueue actions. * * @return void */ public function register(): void { add_action('admin_enqueue_scripts', array($this, 'enqueue_for_screen'), 10); } /** * @param string $hook_suffix Admin screen hook. * @return void */ public function enqueue_for_screen(string $hook_suffix): void { if (! $this->is_workflow_automations_admin_screen($hook_suffix)) { return; } $this->enqueue_admin_css(); $tab = sanitize_key( Request::get( 'tab', 'automations' ) ); $section = sanitize_key( Request::get( 'section', '' ) ); $enqueue_js = ( 'create_automation' === $tab || 'new_template' === $tab || ( 'automations' === $tab && 'executed' === $section ) ); /** * Whether to enqueue the admin JS bundle on this hub screen. * * @param bool $enqueue_js Whether JS should load. * @param string $hook_suffix Admin hook suffix. * @param string $tab Current hub tab. * @param string $section Current hub section. */ if ( apply_filters( 'afromewo_enqueue_admin_js', $enqueue_js, $hook_suffix, $tab, $section ) ) { $this->enqueue_admin_js(); } /** * Fires after this plugin has enqueued its admin assets for a hub screen. * * Add-ons should enqueue here (or on a later priority of admin_enqueue_scripts) * and depend on {@see AdminScripts::get_workflow_script_handle()} for builder JS. * * @param string $hook_suffix Admin hook suffix. * @param string $tab Current hub tab. * @param string $section Current hub section. */ do_action( 'afromewo_admin_enqueue_scripts', $hook_suffix, $tab, $section ); } /** * Whether the current admin screen belongs to this plugin (WooCommerce submenu hub). * * @param string $hook_suffix Admin hook suffix, e.g. woocommerce_page_afroit-messaging-workflows. * @return bool */ public function is_workflow_automations_admin_screen(string $hook_suffix): bool { return strpos( $hook_suffix, \AFROMEWO_PLUGIN_SLUG ) !== false; } /** * Enqueue admin CSS (one minified bundle by default, or source files in order). * * @return void */ public function enqueue_admin_css(): void { $assets = $this->get_assets(); foreach ($assets['css'] as $handle => $path) { wp_enqueue_style( $handle, AFROMEWO_PLUGIN_URL . $path, isset($this->dependencies[$handle]) ? $this->dependencies[$handle] : array(), $this->asset_version($path) ); } } /** * Enqueue admin JS for workflow builder or message-template screens. * @return void */ public function enqueue_admin_js(): void { $assets = $this->get_assets(); foreach ($assets['js'] as $handle => $path) { $deps = isset($this->dependencies[$handle]) ? $this->dependencies[$handle] : array(); wp_enqueue_script( $handle, AFROMEWO_PLUGIN_URL . $path, $deps, $this->asset_version($path), true ); } } /** * @param string $relative_path Path relative to plugin root. * @return string */ private function asset_version(string $relative_path): string { $path = AFROMEWO_PLUGIN_DIR . $relative_path; if (is_readable($path)) { return (string) filemtime($path); } return AFROMEWO_VERSION; } }