'text/javascript')); $css_settings['alphagf_custom_css'] = wp_enqueue_code_editor(array('type' => 'text/css')); wp_enqueue_script('wp-theme-plugin-editor'); wp_enqueue_style('wp-codemirror'); wp_enqueue_script( 'alphagf-custom-css-js', $this->get_base_url() . '/js/alphagf-custom-css-js.js', array('wp-theme-plugin-editor'), $this->_version, true ); } public function noconflict_scripts($scripts = array()) { $scripts[] = 'code-editor'; $scripts[] = 'jshint'; $scripts[] = 'jsonlint'; $scripts[] = 'wp-theme-plugin-editor'; return $scripts; } public function noconflict_styles($scripts = array()) { $scripts[] = 'code-editor'; $scripts[] = 'wp-codemirror'; return $scripts; } public function register_init_script($form) { if (! $this->is_applicable_form($form)) { return; } // Expose the variables the settings page documents; GF's render callback names them formId/currentPage. $script = '( function( $, form_id, current_page ) { ' . $this->get_custom_js($form) . "\n } )( jQuery, formId, currentPage );"; $slug = "alphagf-custom-css-js_{$form['id']}"; GFFormDisplay::add_init_script($form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script); } /** * Code fields skip Gravity Forms' text sanitizing (it mangles characters like "<" and "%20"), so only * users who may post unfiltered HTML can save JavaScript, or CSS containing "<". * * @param \Gravity_Forms\Gravity_Forms\Settings\Fields\Base $field The settings field. * @param string $value The posted value. */ public function validate_code_field($field, $value) { if (rgblank($value) || current_user_can('unfiltered_html')) { return; } if ('alphagf_custom_js' === $field->name || false !== strpos((string) $value, '<')) { $field->set_error(esc_html__('You need the "unfiltered_html" capability to save this code.', 'alpha-addons-for-gravity-forms')); } } public function add_custom_css($form_string, $form) { $custom_css = (string) $this->get_custom_css($form); $custom_css = str_replace('GFFORMID', $form['id'], $custom_css); // Never let stored CSS close the ', $custom_css); } return $form_string; } public function is_applicable_form($form) { return ! empty($this->get_custom_js($form)); } public function get_settings($form_id) { static $cache = array(); if (isset($cache[$form_id])) { return $cache[$form_id]; } $feed_id = $this->get_default_feed_id($form_id); $feed = $feed_id ? $this->get_feed($feed_id) : false; // Inactive feeds shouldn't output anything. $cache[$form_id] = ($feed && rgar($feed, 'is_active')) ? rgar($feed, 'meta') : array(); return $cache[$form_id]; } public function get_custom_css($form) { $settings = $this->get_settings($form['id']); return rgar($settings, 'alphagf_custom_css'); } public function get_custom_js($form) { $settings = $this->get_settings($form['id']); return rgar($settings, 'alphagf_custom_js'); } public function feed_settings_fields() { return array( array( 'title' => esc_html__('Css', 'alpha-addons-for-gravity-forms'), 'description' => wp_kses_post( sprintf( /* translators: 1: Line break, 2: Form ID placeholder */ __('Add any custom CSS that you would like to output wherever this form is rendered. %1$s Use %2$s to automatically set the current form ID when the code is rendered.', 'alpha-addons-for-gravity-forms'), '
', 'GFFORMID' ) ), 'fields' => array( array( 'name' => 'alphagf_custom_css', 'type' => 'textarea', 'class' => 'large', 'validation_callback' => array($this, 'validate_code_field'), ), array( 'name' => 'scope_to_form_only', 'type' => 'toggle', 'label' => esc_html__('Scope CSS to this form only', 'alpha-addons-for-gravity-forms'), 'tooltip' => esc_html__('When enabled, the custom CSS will only be applied to this form. This works by adding "#gform_wrapper_GFFORMID" before all detected selectors.', 'alpha-addons-for-gravity-forms'), 'default_value' => true, ), ), ), array( 'title' => esc_html__('Javascript', 'alpha-addons-for-gravity-forms'), 'description' => wp_kses_post( sprintf( /* translators: 1: Line break, 2: Available JS variables */ __('Add any custom JS that you would like to output wherever this form is rendered. %1$s Use %2$s to automatically set the current form ID when the code is rendered.', 'alpha-addons-for-gravity-forms'), '
', 'form_id, current_page' ) ), 'fields' => array( array( 'name' => 'alphagf_custom_js', 'type' => 'textarea', 'class' => 'large', 'validation_callback' => array($this, 'validate_code_field'), ) ), ) ); } public function prefix_css_selectors($css, $prefix) { /** * Find all CSS selectors, but ignore all `@` rules such as @font-face, @media, etc. as they are * defined globally and not can't be scoped to a certion "parent" selector. */ return preg_replace_callback('/^\s*(?!@)([^\r\n,{}\/]+)(,(?=[^}]*{)|\s*{)/m', function ($matches) use ($prefix) { return $prefix . ' ' . trim($matches[1]) . $matches[2]; }, $css); } }