Filter uncanny-automator-pro

wpcode_strip_script_tags_for_js

Filters whether to strip script tags from JavaScript code for security.

add_filter( 'wpcode_strip_script_tags_for_js', $callback, 10, 1 );

Description

This filter allows developers to control whether `` tags are automatically stripped from JavaScript snippets. By default, they are removed to prevent potential conflicts. Modify this filter to preserve `` tags if your code requires them for proper execution within the snippet.


Usage

add_filter( 'wpcode_strip_script_tags_for_js', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Example usage of the 'wpcode_strip_script_tags_for_js' filter hook.
 *
 * This filter allows developers to conditionally strip the opening and closing
 * <script> tags from JavaScript snippets before they are saved or processed
 * by WPCode. In this example, we'll always strip the tags unless a specific
 * WordPress option is set to prevent it.
 */
add_filter( 'wpcode_strip_script_tags_for_js', function( $strip_tags ) {

	// Check if the option 'wpcode_disable_js_tag_stripping' is set in the WordPress options.
	// If it's set to 'yes', we'll return false to prevent stripping the tags.
	$disable_stripping = get_option( 'wpcode_disable_js_tag_stripping', 'no' );

	if ( 'yes' === $disable_stripping ) {
		return false; // Do not strip the <script> tags.
	}

	// Otherwise, return true to allow the default behavior of stripping the tags.
	return $strip_tags;

}, 10, 1 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

uncanny-automator-pro/src/integrations/wpcode/actions/wpcode-create-snippet.php:196

protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
		$snippet_data                         = array();
		$snippet_data['title']                = isset( $parsed['IHAP_CODE_SNIPPET'] ) ? sanitize_text_field( $parsed['IHAP_CODE_SNIPPET'] ) : '';
		$snippet_data['code']                 = isset( $parsed['WPCODE_CODE'] ) ? sanitize_text_field( $parsed['WPCODE_CODE'] ) : '';
		$snippet_data['code_type']            = isset( $parsed['WPCODE_TYPES'] ) ? sanitize_text_field( $parsed['WPCODE_TYPES'] ) : 'html';
		$snippet_data['tags']                 = isset( $parsed['WPCODE_TAGS'] ) ? json_decode( sanitize_text_field( $parsed['WPCODE_TAGS'] ) ) : '';
		$snippet_data['location']             = isset( $parsed['WPCODE_LOCATIONS'] ) ? sanitize_text_field( $parsed['WPCODE_LOCATIONS'] ) : '';
		$snippet_data['auto_insert']          = isset( $parsed['WPCODE_INSERT_METHOD'] ) ? sanitize_text_field( $parsed['WPCODE_INSERT_METHOD'] ) : '';
		$snippet_data['status']               = isset( $parsed['WPCODE_STATUSES'] ) ? sanitize_text_field( $parsed['WPCODE_STATUSES'] ) : '';
		$snippet_data['notes']                = isset( $parsed['WPCODE_NOTES'] ) ? sanitize_text_field( $parsed['WPCODE_NOTES'] ) : '';
		$snippet_data['start_date']           = isset( $parsed['WPCODE_START_DATE'] ) ? sanitize_text_field( $parsed['WPCODE_START_DATE'] ) : '';
		$snippet_data['end_date']             = isset( $parsed['WPCODE_END_DATE'] ) ? sanitize_text_field( $parsed['WPCODE_END_DATE'] ) : '';
		$snippet_data['priority']             = isset( $parsed['WPCODE_PRIORITY'] ) ? absint( sanitize_text_field( $parsed['WPCODE_PRIORITY'] ) ) : 10;
		$snippet_data['device_type']          = isset( $parsed['WPCODE_DEVICE_TYPES'] ) ? sanitize_text_field( $parsed['WPCODE_DEVICE_TYPES'] ) : 'any';
		$snippet_data['shortcode_name']       = isset( $parsed['WPCODE_SHORTCODE_NAME'] ) ? sanitize_text_field( $parsed['WPCODE_SHORTCODE_NAME'] ) : '';
		$snippet_data['shortcode_attributes'] = isset( $parsed['WPCODE_SHORTCODE_ATTRIBUTES'] ) ? json_decode( sanitize_text_field( $parsed['WPCODE_SHORTCODE_ATTRIBUTES'] ), true ) : '';

		if ( 'text' === $snippet_data['code_type'] ) {
			$snippet_data['code'] = wpautop( $snippet_data['code'] );
		}

		$snippet_data['status'] = 'active' === $snippet_data['status'] ? true : false;

		if ( 'php' === $snippet_data['code_type'] ) {
			$snippet_data['code'] = preg_replace( '|^s*<?(php)?|', '', $snippet_data['code'] );
		}

		if ( 'js' === $snippet_data['code_type'] && apply_filters( 'wpcode_strip_script_tags_for_js', true ) ) {
			$snippet_data['code'] = preg_replace( '|^s*<script[^>]*>|', '', $snippet_data['code'] );
			$snippet_data['code'] = preg_replace( '|</(script)>s*$|', '', $snippet_data['code'] );
		}

		$attributes = array();
		if ( isset( $snippet_data['shortcode_attributes'] ) ) {
			foreach ( $snippet_data['shortcode_attributes'] as $attribute ) {
				$attributes[] = $attribute['WPCODE_ATTRIBUTE_NAME'];
			}
		}

		$snippet = new WPCode_Snippet(
			array(
				'title'                => $snippet_data['title'],
				'code'                 => $snippet_data['code'],
				'active'               => $snippet_data['status'],
				'code_type'            => $snippet_data['code_type'],
				'location'             => $snippet_data['location'],
				'auto_insert'          => absint( $snippet_data['auto_insert'] ),
				'tags'                 => $snippet_data['tags'],
				'priority'             => $snippet_data['priority'],
				'device_type'          => $snippet_data['device_type'],
				'note'                 => wp_unslash( $snippet_data['notes'] ),
				'custom_shortcode'     => str_replace( '-', '_', $snippet_data['shortcode_name'] ),
				'shortcode_attributes' => $attributes,
				'schedule'             => array(
					'start' => $snippet_data['start_date'],
					'end'   => $snippet_data['end_date'],
				),
			)
		);

		$snippet_id = $snippet->save();

		if ( false === $snippet_id ) {
			$action_data['complete_with_errors'] = true;
			$error_message                       = esc_html__( 'We are unable to save snippet.', 'uncanny-automator-pro' );
			Automator()->complete->action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}

		Automator()->complete->action( $user_id, $action_data, $recipe_id );
	}

Scroll to Top