Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate with Polylang plugin #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ jQuery(function() {
//remove link if user didnt fill in field
if(input.policy == 'policylink') {
input.href ? content.href = input.href : options.showLink = false;
} else if (input.policy == 'policypage') {
content.href = 'policypage';
}

// only add if not default
Expand Down
103 changes: 103 additions & 0 deletions includes/osanocc-polylang.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
$icc_i18n_literals = array(
'message' => array(
'name' => 'Message',
'default' => 'This website uses cookies to ensure you get the best experience on our website.'
),
'dismiss' => array(
'name' => 'Dismiss button text',
'default' => 'Got it!'
),
'link' => array(
'name' => 'Policy link text',
'default' => 'Learn more'
),
'deny' => array(
'name' => 'Deny button text',
'default' => 'Refuse cookies'
),
);


function icc_i18n_init_literals() {
global $icc_i18n_literals;
if ( is_admin() ){
if ( function_exists('pll_register_string') ) {
$plugin_name = 'Osano Cookie Consent';

foreach ( $icc_i18n_literals as $value ){
pll_register_string( $value['name'], $value['default'], $plugin_name );
}
}
}

add_filter('privacy_policy_url', 'icc_translate_privacy_page', 10, 2);
}

function icc_translate( $config ) {
global $icc_i18n_literals;
$result = array();
if ( function_exists('pll_register_string')) {
$a = json_decode($config, true);
//there is more fields to be preserved than just text

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//there is more fields to be preserved than just text
// There are more fields to be preserved than just text.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course :$

$save_keys = array('href');
$saved_content = array();
foreach ( $a as $key => $value ){
$result[$key] = $value;
if ( $key == 'content' ) {
foreach ( $save_keys as $key2save ) {
if ($result[$key][$key2save] != null ) {
$saved_content[$key2save] = $result[$key][$key2save];
}
}
}
}
$result['content'] = array();
foreach ( $icc_i18n_literals as $literal => $field ){
if ( function_exists('pll__')){
$result['content'][$literal] = pll__($field['default']);
} else if ( $a['content'][$literal] != null ){
$result['content'][$literal] = $a['content'][$literal];
}
}
$result['content'] = array_merge($result['content'], $saved_content);
return json_encode($result);
} else {
return $config;
}

}

function icc_i18n_warning() {
if ( function_exists('pll_register_string')) {
$wmessage = 'You have Polylang plugin installed. You should set up literals in "' . __( 'Languages', 'polylang' ) . ' &gt; ' . __( 'Strings translations', 'polylang' ) . '"';
$warning = <<<HTML
<tr>
<td colspan="2" style="padding-bottom:0;">
<p style="color: red">$wmessage</p>
</td>
</tr>
HTML;
echo $warning;
}
}

/**
*
* @param string $url
* @param int $policy_page_id
*/
function icc_translate_privacy_page( $url, $policy_page_id){
//die();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//die();

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
I'm so sorry.

if ( function_exists('pll_get_post_translations') && function_exists('pll_current_language') && function_exists('pll_default_language')) {
$trans = pll_get_post_translations( $policy_page_id );
$lang = pll_current_language();
if (empty($lang)) {
$lang = pll_default_language();
}
if ( !empty($trans) && $trans[$lang] != null ){
return get_post_permalink($trans[$lang]);
}
}
return $url;
}
24 changes: 24 additions & 0 deletions osano-cookie-consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

defined( 'ABSPATH' ) or die( 'No!' );

require_once 'includes/osanocc-polylang.php';

define('CCVERSION', '3.1');
define('PLUGINVERSION', '1.0');

Expand All @@ -29,6 +31,15 @@ function icc_load_assets()
function icc_create_snippet() {
if(!is_admin() && get_option('icc_popup_enabled') && get_option('icc_popup_options')) {
$config = get_option('icc_popup_options');

$aconfig = json_decode($config, true);
if ($aconfig['content']['href'] == 'policypage'){
$aconfig['content']['href'] = get_privacy_policy_url();
}
$config = json_encode($aconfig);

$config = icc_translate( $config );

echo '<script>window.cookieconsent.initialise('.$config.');</script>';
}
}
Expand Down Expand Up @@ -70,6 +81,8 @@ function icc_register_settings() {
register_setting( 'icc-options', 'link-text' );
register_setting( 'icc-options', 'deny-text' );
register_setting( 'icc-options', 'custom-attributes' );

icc_i18n_init_literals();
}
add_action( 'admin_init', 'icc_register_settings' );

Expand Down Expand Up @@ -129,6 +142,7 @@ function icc_options_page() {
<input type="radio" id="policylink" name="policy" value="policylink" <?php echo get_option('policy')=='policylink' ? 'checked' : '' ?>>
<label for="policylink">Link to your own policy (leave empty to disable link)</label><br />
<input type="text" name="link-href" placeholder="www.example.com/cookiepolicy" value="<?php echo get_option('link-href') ?>" onclick="document.getElementById('policylink').checked = true;" />
<?php icc_show_wp_privacy_page();?>
</td>
</tr>
<tr><th colspan="2">5. Compliance type</th></tr>
Expand All @@ -141,6 +155,7 @@ function icc_options_page() {
</td>
</tr>
<tr><th colspan="2">6. Custom text</th></tr>
<?php icc_i18n_warning();?>
<tr>
<td colspan="2" style="padding-bottom:0;"><p><b>Message</b></p>
<textarea name="message-text" id="message-text" placeholder="This website uses cookies to ensure you get the best experience on our website." maxlength="300"><?php echo get_option('message-text') ?></textarea>
Expand Down Expand Up @@ -174,4 +189,13 @@ function icc_options_page() {
</div>
<?php
echo '</form>';
}

function icc_show_wp_privacy_page(){
if (get_privacy_policy_url() != '') {
?>
<input type="radio" id="policypage" name="policy" value="policypage" <?php echo get_option('policy')=='policypage' ? 'checked' : '' ?>>
<label for="policypage">Link to <a href="<?php echo get_privacy_policy_url();?>" target="_blank">your own policy page</a></label><br />
<?php
}
}