php Cookie法律信息的WPML兼容性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Cookie法律信息的WPML兼容性相关的知识,希望对你有一定的参考价值。

<?php
/*
Copyright 2012  Richard Ashby  (email : wordpress@mediacreek.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


/**
 Returns JSON object containing the settings for the main script
 REFACTOR / DEBUG: may need to use addslashes( ... ) else breaks JSON
 */
function cookielawinfo_get_json_settings() {
	$settings = cookielawinfo_get_admin_settings();

	// DEBUG hex:
	// preg_match('/^#[a-f0-9]{6}|#[a-f0-9]{3}$/i', $hex)
	// DEBUG json_encode - issues across different versions of PHP!
	// $str = json_encode( $slim_settings, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP );

	// Slim down JSON objects to the bare bones:
	$slim_settings = array(
		'animate_speed_hide'			=> $settings['animate_speed_hide'],
		'animate_speed_show'			=> $settings['animate_speed_show'],
		'background'					=> $settings['background'],
		'border'						=> $settings['border'],
		'border_on'						=> $settings['border_on'],
		'button_1_button_colour'		=> $settings['button_1_button_colour'],
		'button_1_button_hover'			=> (cookielawinfo_su_hex_shift( $settings['button_1_button_colour'], 'down', 20 )),
		'button_1_link_colour'			=> $settings['button_1_link_colour'],
		'button_1_as_button'			=> $settings['button_1_as_button'],
		'button_2_button_colour'		=> $settings['button_2_button_colour'],
		'button_2_button_hover'			=> (cookielawinfo_su_hex_shift( $settings['button_2_button_colour'], 'down', 20 )),
		'button_2_link_colour'			=> $settings['button_2_link_colour'],
		'button_2_as_button'			=> $settings['button_2_as_button'],
		'font_family'					=> $settings['font_family'],
		'header_fix'                    => $settings['header_fix'],
		'notify_animate_hide'			=> $settings['notify_animate_hide'],
		'notify_animate_show'			=> $settings['notify_animate_show'],
		'notify_div_id'					=> $settings['notify_div_id'],
		'notify_position_horizontal'	=> $settings['notify_position_horizontal'],
		'notify_position_vertical'		=> $settings['notify_position_vertical'],
		'scroll_close'                  => $settings['scroll_close'],
		'scroll_close_reload'           => $settings['scroll_close_reload'],
		'showagain_tab'					=> $settings['showagain_tab'],
		'showagain_background'			=> $settings['showagain_background'],
		'showagain_border'				=> $settings['showagain_border'],
		'showagain_div_id'				=> $settings['showagain_div_id'],
		'showagain_x_position'			=> $settings['showagain_x_position'],
		'text'							=> $settings['text'],
		'show_once_yn'					=> $settings['show_once_yn'],
		'show_once'						=> $settings['show_once']
	);
	$str = json_encode( $slim_settings );
	/*
	DEBUG:
	if ( $str == null | $str == '') {
		$str = 'error: json is empty';
	}
	*/
	return $str;
}


/**
 Outputs the cookie control script in the footer
 N.B. This script MUST be output in the footer.

 This function should be attached to the wp_footer action hook.
*/
function cookielawinfo_inject_cli_script() {
	$the_options = cookielawinfo_get_admin_settings();

	if ( $the_options['is_on'] == true ) {

		// Output the HTML in the footer:
		// Yan&Co WPML Fix
		
		$str = icl_translate( 'wpml_custom', 'wpml_custom_notify_message', $str );
		$notify_html = '<div id="' . cookielawinfo_remove_hash( $the_options["notify_div_id"] ) . '"><span>' . do_shortcode($str) . '</span></div>';

		if ( $the_options['showagain_tab'] === true ) {
			$notify_html .= '<div id="' . cookielawinfo_remove_hash( $the_options["showagain_div_id"] ) . '"><span id="cookie_hdr_showagain">' . icl_translate( 'wpml_custom', 'wpml_showagain_text', $the_options["showagain_text"] ) . '</span></div>';
		}

		echo $notify_html;

		// Now output the JavaScript:

		?>

		<script type="text/javascript">
			//<![CDATA[
			jQuery(document).ready(function() {
				cli_show_cookiebar({
					settings: '<?php echo cookielawinfo_get_json_settings(); ?>'
				});
			});
			//]]>
		</script>

		<?php
	}
}


/**
 Outputs frontend scripts in the header.
 N.B. These scripts MUST be output in the header.

 This function should be attached to the wp_enqueue_script action hook, not wp_head!
 Else gets output in footer (incorrect).
*/
function cookielawinfo_enqueue_frontend_scripts() {
	$the_options = cookielawinfo_get_admin_settings();
	if ( $the_options['is_on'] == true ) {

		/**
		 * Force reload
		 */
		$version = '1.5.3';

		wp_register_style( 'cookielawinfo-style', CLI_PLUGIN_URL . 'css/cli-style.css', null, $version );
		wp_enqueue_style( 'cookielawinfo-style' );

		wp_enqueue_script( 'cookie-law-info-script', CLI_PLUGIN_URL . 'js/cookielawinfo.js', array( 'jquery' ), $version );
	}
	wp_register_style( 'cookielawinfo-table-style', CLI_PLUGIN_URL . 'css/cli-tables.css', null, $version );
}


/**
 * Color shift a hex value by a specific percentage factor
 * By http://www.phpkode.com/source/s/shortcodes-ultimate/shortcodes-ultimate/lib/color.php
 * Adapted by Richard Ashby; amended error handling to use failovers not messages, so app continues
 *
 * @param string $supplied_hex Any valid hex value. Short forms e.g. #333 accepted.
 * @param string $shift_method How to shift the value e.g( +,up,lighter,>)
 * @param integer $percentage Percentage in range of [0-100] to shift provided hex value by
 * @return string shifted hex value
 * @version 1.0 2008-03-28
 */
function cookielawinfo_su_hex_shift( $supplied_hex, $shift_method, $percentage = 50 ) {
	$shifted_hex_value = null;
	$valid_shift_option = FALSE;
	$current_set = 1;
	$RGB_values = array( );
	$valid_shift_up_args = array( 'up', '+', 'lighter', '>' );
	$valid_shift_down_args = array( 'down', '-', 'darker', '<' );
	$shift_method = strtolower( trim( $shift_method ) );

	// Check Factor
	if ( !is_numeric( $percentage ) || ($percentage = ( int ) $percentage) < 0 || $percentage > 100 ) {
		//trigger_error( "Invalid factor", E_USER_ERROR );
		return $supplied_hex;
	}

	// Check shift method
	foreach ( array( $valid_shift_down_args, $valid_shift_up_args ) as $options ) {
		foreach ( $options as $method ) {
			if ( $method == $shift_method ) {
				$valid_shift_option = !$valid_shift_option;
				$shift_method = ( $current_set === 1 ) ? '+' : '-';
				break 2;
			}
		}
		++$current_set;
	}

	if ( !$valid_shift_option ) {
		//trigger_error( "Invalid shift method", E_USER_ERROR );
		return $supplied_hex;
	}

	// Check Hex string
	switch ( strlen( $supplied_hex = ( str_replace( '#', '', trim( $supplied_hex ) ) ) ) ) {
		case 3:
			if ( preg_match( '/^([0-9a-f])([0-9a-f])([0-9a-f])/i', $supplied_hex ) ) {
				$supplied_hex = preg_replace( '/^([0-9a-f])([0-9a-f])([0-9a-f])/i', '\\1\\1\\2\\2\\3\\3', $supplied_hex );
			} else {
				//trigger_error( "Invalid hex color value", E_USER_ERROR );
				return $supplied_hex;
			}
			break;
		case 6:
			if ( !preg_match( '/^[0-9a-f]{2}[0-9a-f]{2}[0-9a-f]{2}$/i', $supplied_hex ) ) {
				//trigger_error( "Invalid hex color value", E_USER_ERROR );
				return $supplied_hex;
			}
			break;
		default:
			//trigger_error( "Invalid hex color length", E_USER_ERROR );
			return $supplied_hex;
	}

	// Start shifting
	$RGB_values['R'] = hexdec( $supplied_hex{0} . $supplied_hex{1} );
	$RGB_values['G'] = hexdec( $supplied_hex{2} . $supplied_hex{3} );
	$RGB_values['B'] = hexdec( $supplied_hex{4} . $supplied_hex{5} );

	foreach ( $RGB_values as $c => $v ) {
		switch ( $shift_method ) {
			case '-':
				$amount = round( ((255 - $v) / 100) * $percentage ) + $v;
				break;
			case '+':
				$amount = $v - round( ($v / 100) * $percentage );
				break;
			default:
				// trigger_error( "Oops. Unexpected shift method", E_USER_ERROR );
				return $supplied_hex;
		}

		$shifted_hex_value .= $current_value = (
			strlen( $decimal_to_hex = dechex( $amount ) ) < 2
			) ? '0' . $decimal_to_hex : $decimal_to_hex;
	}

	return '#' . $shifted_hex_value;
}


/** Removes leading # characters from a string */
function cookielawinfo_remove_hash( $str ) {
	if ( $str{0} == "#" ) {
		$str = substr( $str, 1, strlen($str) );
	}
	else {
		return $str;
	}
	return cookielawinfo_remove_hash( $str );
}


/**
 Explodes hex colour from 3 to 6 characters.
 If string is not 3 chars on input, will return original string
 */
function cookielawinfo_make_hex_colour_6_chars( $hex ) {
	$str = cookielawinfo_remove_hash( $hex );
	if ( strlen( $str ) == 3 ) {
		$hex = '#' . $str[0] . $str[0] . $str[1] . $str[1] . $str[2] . $str[2];
	}
	return $hex;
}


/** Debug assistance: JS alertbox for any passed value of $gubbins */
function cookielawinfo_debug_alertbox( $gubbins ) {
	if ( ! CLI_PLUGIN_DEVELOPMENT_MODE )
		return;
	echo '<script type="text/javascript"> alert("' . $gubbins .'")</script>';
}


/** Echoes out a debug string of your choice (but only if in development mode) */
function cookielawinfo_debug_echo( $gubbins ) {
	if ( ! CLI_PLUGIN_DEVELOPMENT_MODE )
		return;
	echo '<br />START OF DEBUG STRING>>>' . $gubbins . '<<< END OF DEBUG STRING<br />';
}


/** Debug: output saved settings to footer of admin panel */
function cookielawinfo_debug_admin_settings( $break ) {
	if ( ! CLI_PLUGIN_DEVELOPMENT_MODE )
		return;
	$settings = cookielawinfo_get_admin_settings();
	$ret = '<p>Settings: ';
	foreach ( $settings as $key => $option ) {
		$ret .= $key . ' = ' . $option . '; ';
		if ( $break )
			$ret .= '<br />';
	}
	$ret .= '</p>';
	return $ret;
}

?>
<?php
/*
	===============================================================================

	Copyright 2012  Richard Ashby  (email : wordpress@mediacreek.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/


/**
 A shortcode that outputs a link which will delete the cookie used to track
 whether or not a vistor has dismissed the header message (i.e. so it doesn't
 keep on showing on all pages)

 Usage:	[delete_cookies]
		[delete_cookies linktext="delete cookies"]

 N.B. This shortcut does not block cookies, or delete any other cookies!
*/
function cookielawinfo_delete_cookies_shortcode( $atts ) {
	extract( shortcode_atts( array(
		'text' => 'Delete Cookies'
	), $atts ) );
	return "<a href='' id='cookielawinfo-cookie-delete'>{$text}</a>";
}


/**
 A nice shortcode to output a table of cookies you have saved, output in ascending
 alphabetical order. If there are no cookie records found a single empty row is shown.
 You can customise the 'not shown' message (see commented code below)

 N.B. This only shows the information you entered on the "cookie" admin page, it
 does not necessarily mean you comply with the cookie law. It is up to you, or
 the website owner, to make sure you have conducted an appropriate cookie audit
 and are informing website visitors of the actual cookies that are being stored.

 Usage:					[cookie_audit]
						[cookie_audit style="winter"]
						[cookie_audit not_shown_message="No records found"]
						[cookie_audit style="winter" not_shown_message="Not found"]

 Styles included:		simple, classic, modern, rounded, elegant, winter.
						Default style applied: classic.

 Additional styles:		You can customise the CSS by editing the CSS file itself,
 						included with plugin.
*/
function cookielawinfo_table_shortcode( $atts ) {

	/** RICHARDASHBY EDIT: only add CSS if table is being used */
	wp_enqueue_style( 'cookielawinfo-table-style' );
	/** END EDIT */

	extract( shortcode_atts( array(
		'style' => 'classic',
		'not_shown_message' => ''
	), $atts ) );

	global $post;

	$args = array(
		'post_type' => 'cookielawinfo',
		/** 28/05/2013: Changing from 10 to 50 to allow longer tables of cookie data */
		'posts_per_page' => 50,
		'order' => 'ASC',
		'orderby' => 'title'
	);
	$cookies = new WP_Query( $args );

	$ret = '<table class="cookielawinfo-' . $style . '"><thead><tr>';
	$ret .= '<th class="cookielawinfo-column-1">Cookie</th>';
	$ret .= '<th class="cookielawinfo-column-2">Type</th>';
	$ret .= '<th class="cookielawinfo-column-3">Duration</th>';
	$ret .= '<th class="cookielawinfo-column-4">Description</th></tr>';
	$ret .= '</thead><tbody>';

	if ( !$cookies->have_posts() ) {
		$ret .= '<tr class="cookielawinfo-row"><td colspan="2" class="cookielawinfo-column-empty">' . $not_shown_message . '</td></tr>';
	}

	while ( $cookies->have_posts() ) : $cookies->the_post();
		// Get custom fields:
		$custom = get_post_custom( $post->ID );
		$cookie_type = ( isset ( $custom["_cli_cookie_type"][0] ) ) ? $custom["_cli_cookie_type"][0] : '';
		$cookie_duration = ( isset ( $custom["_cli_cookie_duration"][0] ) ) ? $custom["_cli_cookie_duration"][0] : '';
		// Output HTML:
		$ret .= '<tr class="cookielawinfo-row"><td class="cookielawinfo-column-1">' . get_the_title() . '</td>';
		$ret .= '<td class="cookielawinfo-column-2">' . $cookie_type .'</td>';
		$ret .= '<td class="cookielawinfo-column-3">' . $cookie_duration .'</td>';
		$ret .= '<td class="cookielawinfo-column-4">' . get_the_content() .'</td>';
		$ret .= '</tr>';
	endwhile;
	$ret .= '</tbody></table>';
	return $ret;
}


/** Returns HTML for a standard (green, medium sized) 'Accept' button */
function cookielawinfo_shortcode_accept_button( $atts ) {
	extract( shortcode_atts( array(
		'colour' => 'green'
	), $atts ) );

	// Fixing button translate text bug
	// 18/05/2015 by RA
	$defaults = array(
		'button_1_text' => 'Accept'
	);
	$settings = wp_parse_args( cookielawinfo_get_admin_settings(), $defaults );

	return '<a href="#" id="cookie_action_close_header" class="medium cli-plugin-button ' . $colour . '">' . stripslashes( $settings['button_1_text'] ) . '</a>';
}


/** Returns HTML for a generic button */
function cookielawinfo_shortcode_more_link( $atts ) {
	return cookielawinfo_shortcode_button_DRY_code( 'button_2' );
}


/** Returns HTML for a generic button */
function cookielawinfo_shortcode_main_button( $atts ) {
	$defaults = array(
		'button_1_text' => icl_translate( 'wpml_custom', 'wpml_button_1_text', 'Accept' ),
		'button_1_url' => icl_translate( 'wpml_custom', 'wpml_button_1_url', '#' ),
		'button_1_action' => '#cookie_action_close_header',

		'button_1_link_colour' => '#fff',
		'button_1_new_win' => false,
		'button_1_as_button' => true,
		'button_1_button_colour' => '0f0',
		'button_1_button_size' => 'medium'
	);
	$settings = wp_parse_args( cookielawinfo_get_admin_settings(), $defaults );

	$class = '';
	if ( $settings['button_1_as_button'] ) {
		$class .= ' class="' . $settings['button_1_button_size'] . ' cli-plugin-button cli-plugin-main-button"';
	}
	else {
		$class .= ' class="cli-plugin-main-button" ' ;
	}

	// If is action not URL then don't use URL!
	$url = ( $settings['button_1_action'] == "CONSTANT_OPEN_URL" ) ? $settings['button_1_url'] : "#";

	$link_tag = '<a href="' . icl_translate( 'wpml_custom', 'wpml_button_1_url', $url ) . '" id="' . cookielawinfo_remove_hash ( $settings['button_1_action'] ) . '" ';
	$link_tag .= ( $settings['button_1_new_win'] ) ? 'target="_new" ' : '' ;
	$link_tag .= $class . ' >' . icl_translate( 'wpml_custom', 'wpml_button_1_text', $settings['button_1_text'] ) . '</a>';

	return $link_tag;
}


/** Returns HTML for a generic button */
function cookielawinfo_shortcode_button_DRY_code( $name ) {
	$arr = cookielawinfo_get_admin_settings();
	$settings = array();
	$class_name = '';

	if ( $name == "button_1" ) {
		$settings = array(
			'button_x_text' => icl_translate( 'wpml_custom', 'wpml_button_2_text', $arr['button_2_text'] ),
			'button_x_url' => icl_translate( 'wpml_custom', 'wpml_button_2_url', $arr['button_2_url'] ),
			'button_x_action' => $arr['button_1_action'],

			'button_x_link_colour' => $arr['button_1_link_colour'],
			'button_x_new_win' => $arr['button_1_new_win'],
			'button_x_as_button' => $arr['button_1_as_button'],
			'button_x_button_colour' => $arr['button_1_button_colour'],
			'button_x_button_size' => $arr['button_1_button_size']
		);
		$class_name = 'cli-plugin-main-button';
	}
	elseif ( $name == "button_2" ) {
		$settings = array(
			'button_x_text' => stripslashes( $arr['button_2_text'] ),
			'button_x_url' => $arr['button_2_url'],
			'button_x_action' => $arr['button_2_action'],

			'button_x_link_colour' => $arr['button_2_link_colour'],
			'button_x_new_win' => $arr['button_2_new_win'],
			'button_x_as_button' => $arr['button_2_as_button'],
			'button_x_button_colour' => $arr['button_2_button_colour'],
			'button_x_button_size' => $arr['button_2_button_size']
		);
		$class_name = 'cli-plugin-main-link';
	}

	$class = '';
	if ( $settings['button_x_as_button'] ) {
		$class .= ' class="' . $settings['button_x_button_size'] . ' cli-plugin-button ' . $class_name . '"';
	}
	else {
		$class .= ' class="' . $class_name . '" ' ;
	}

	// If is action not URL then don't use URL!
	$url = ( $settings['button_x_action'] == "CONSTANT_OPEN_URL" ) ? $settings['button_x_url'] : "#";

	$link_tag = '<a href="' . $url . '" id="' . cookielawinfo_remove_hash ( $settings['button_x_action'] ) . '" ';
	$link_tag .= ( $settings['button_x_new_win'] ) ? 'target="_blank" ' : '' ;
	$link_tag .= $class . ' >' . $settings['button_x_text'] . '</a>';

	return $link_tag;
}


?>

以上是关于php Cookie法律信息的WPML兼容性的主要内容,如果未能解决你的问题,请参考以下文章

php 可用性检查和WPML之间的兼容性(不重复)

php 可用性检查和WPML之间的兼容性(仅限重复产品)

php WPML:只有标志的语言切换器

php wpml_element_link

php 插件WPML

php WPML启用功能