'string', 'sanitize_callback' => array( $this, 'sanitize_head_script' ), 'default' => '', ) ); } /** * Sanitize and validate Digital Anumati script. * * Only administrators with unfiltered_html capability * can save a raw script. * * @param mixed $content Submitted script. * @return string */ public function sanitize_head_script( $content ) { /* * Make sure the value is a string. */ if ( ! is_string( $content ) ) { return ''; } /* * Remove slashes added by WordPress. */ $content = wp_unslash( $content ); /* * Remove unnecessary whitespace. */ $content = trim( $content ); /* * Empty value is allowed. */ if ( '' === $content ) { return ''; } /* * Only users who are allowed to save unfiltered HTML * should be able to save executable script code. */ if ( ! current_user_can( 'unfiltered_html' ) ) { add_settings_error( self::OPTION_NAME, 'abymdac_invalid_permission', esc_html__( 'You do not have permission to save JavaScript code.', 'abym-consent-banner-digital-anumati' ), 'error' ); return ''; } /* * Validate that this is actually a script tag. */ if ( ! preg_match( '/]*>.*?<\/script>/is', $content ) ) { /* * Also allow a plain HTTPS JavaScript URL. */ if ( ! filter_var( $content, FILTER_VALIDATE_URL ) ) { add_settings_error( self::OPTION_NAME, 'abymdac_invalid_script', esc_html__( 'Please enter a valid Digital Anumati script tag or HTTPS script URL.', 'abym-consent-banner-digital-anumati' ), 'error' ); return ''; } /* * Only HTTPS URLs are accepted. */ if ( 0 !== strpos( strtolower( $content ), 'https://' ) ) { add_settings_error( self::OPTION_NAME, 'abymdac_invalid_url', esc_html__( 'Only HTTPS script URLs are allowed.', 'abym-consent-banner-digital-anumati' ), 'error' ); return ''; } return esc_url_raw( $content ); } /* * Remove dangerous tags if somebody attempts * to inject additional HTML. */ $content = preg_replace( '/<\s*(iframe|object|embed|form|input|textarea|button|style|link)\b[^>]*>.*?<\s*\/\s*\1\s*>/is', '', $content ); /* * Make sure only HTTPS src URLs are used. */ if ( preg_match( '/\bsrc\s*=\s*["\']([^"\']+)["\']/i', $content, $matches ) ) { $src = trim( $matches[1] ); if ( 0 !== strpos( strtolower( $src ), 'https://' ) ) { add_settings_error( self::OPTION_NAME, 'abymdac_invalid_src', esc_html__( 'The script source must use HTTPS.', 'abym-consent-banner-digital-anumati' ), 'error' ); return ''; } } /* * Remove leading/trailing whitespace again. */ return trim( $content ); } /** * Render settings page. * * @return void */ public function render() { /* * Capability check. */ if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'abym-consent-banner-digital-anumati' ) ); } $head_script = get_option( self::OPTION_NAME, '' ); ?>

section.', 'abym-consent-banner-digital-anumati' ); ?>

Digital Anumati.', 'abym-consent-banner-digital-anumati' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'rel' => array(), ), ) ), esc_url( 'https://www.digitalanumati.com/' ) ); ?>

id ) { return; } $settings_url = admin_url( 'options-general.php?page=abymdac-settings' ); ?>

Settings > Digital Anumati and paste your header script.', 'abym-consent-banner-digital-anumati' ), array( 'a' => array( 'href' => array(), ), ) ), esc_url( $settings_url ) ); ?>

', esc_url( $head_script ) ); } /* * Output only the value that was deliberately saved * through the protected settings screen. */ echo $head_script . "\n"; } } }