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 3 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
49 changes: 40 additions & 9 deletions includes/osanocc-polylang.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
);


function icc_register_literals() {
function icc_i18n_init_literals() {
global $icc_i18n_literals;
if ( is_admin() ){
if ( function_exists('pll_register_string') ) {
Expand All @@ -30,26 +30,37 @@ function icc_register_literals() {
}
}
}

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'){
$result[$key] = array();
foreach ( $icc_i18n_literals as $literal => $field ){
if ( function_exists('pll__')){
$result[$key][$literal] = pll__($icc_i18n_literals[$literal]['default']);
} else if ( $value[$literal] != null ){
$result[$key][$literal] = $value[$literal];
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;
Expand All @@ -63,10 +74,30 @@ function icc_i18n_warning() {
$warning = <<<HTML
<tr>
<td colspan="2" style="padding-bottom:0;">
<h2 style="color: red">$wmessage</h2>
<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;
}
18 changes: 17 additions & 1 deletion osano-cookie-consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ 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 @@ -76,7 +82,7 @@ function icc_register_settings() {
register_setting( 'icc-options', 'deny-text' );
register_setting( 'icc-options', 'custom-attributes' );

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

Expand Down Expand Up @@ -136,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 Down Expand Up @@ -182,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
}
}