<?php

	// Exit if accessed directly
	if ( ! defined( 'ABSPATH' ) ) {
		exit;
	}

	class WS_Form_Action_JavaScript extends WS_Form_Action {

		public $id = 'javascript';
		public $pro_required = true;
		public $label;
		public $label_action;
		public $events;
		public $multiple = true;
		public $configured = true;
		public $priority = 150;
		public $can_repost = false;
		public $form_add = false;
		public $woocommerce_bypass = true;

		// Config
		public $javascript;

		public function __construct() {

			// Events
			$this->events = array('submit');

			// Register config filters
			add_filter('wsf_config_meta_keys', array($this, 'config_meta_keys'), 10, 2);

			// Register init action
			add_action('init', array($this, 'init'));
		}

		public function init() {

			// Set label
			$this->label = __('Run JavaScript', 'ws-form');

			// Set label for actions pull down
			$this->label_action = __('Run JavaScript', 'ws-form');

			// Register action
			parent::register($this);
		}

		public function post($form, &$submit, $config) {

			// Load config
			self::load_config($config);

			// Fire event
			parent::success(__('JavaScript added to queue', 'ws-form'), array(

				array(

					'action'		=> $this->id,
					'javascript' 	=> WS_Form_Common::parse_variables_process($this->javascript, $form, $submit, 'text/javascript')
				)
			));
		}

		public function load_config($config) {

			$this->javascript = parent::get_config($config, 'action_' . $this->id . '_javascript');
		}

		// Get settings
		public function get_action_settings() {

			$settings = array(

				'meta_keys'		=> array(

					'action_' . $this->id . '_javascript'
				)
			);

			// Wrap settings so they will work with sidebar_html function in admin.js
			$settings = parent::get_settings_wrapper($settings);

			// Add labels
			$settings->label = $this->label;
			$settings->label_action = $this->label_action;

			// Add multiple
			$settings->multiple = $this->multiple;

			// Add events
			$settings->events = $this->events;

			// Add can_repost
			$settings->can_repost = $this->can_repost;

			// Apply filter
			// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- All hooks prefixed with wsf_
			$settings = apply_filters('wsf_action_javascript_settings', $settings);

			return $settings;
		}

		// Meta keys for this action
		public function config_meta_keys($meta_keys = array(), $form_id = 0) {

			// Build config_meta_keys
			$config_meta_keys = array(

				// JavaScript
				'action_' . $this->id . '_javascript'	=> array(

					'label'				=>	__('JavaScript', 'ws-form'),
					'type'				=>	'html_editor',
					'variable_helper'	=>	true,
					'help'				=>	__('Do not add &lt;script&gt; tags. Variables such as #field are only valid within quotes.', 'ws-form'),
					'mode'				=>	'javascript'
				)
			);

			// Merge
			$meta_keys = array_merge($meta_keys, $config_meta_keys);

			return $meta_keys;
		}
	}

	new WS_Form_Action_JavaScript();
