';
$page_exists = cmplz_tc_icon( 'circle-times', 'disabled' );
$sync_icon = cmplz_tc_icon( 'sync-error', 'disabled' );
if ( $page_id ) {
$generated = date( cmplz_short_date_format(), get_option( 'cmplz_tc_documents_update_date', get_option( 'cmplz_documents_update_date' ) ) );
$sync_status = $this->syncStatus( $page_id );
$status = $sync_status === 'sync' ? "success" : "disabled";
$sync_icon = cmplz_tc_icon( 'sync', $status );
$page_exists = cmplz_tc_icon( 'circle-check', 'success' );
} else {
$status = "disabled";
$generated = '' . __( 'create', 'complianz-terms-conditions' ) . '';
}
$shortcode_icon = cmplz_tc_icon( 'shortcode', 'default', __( 'Click to copy the document shortcode', 'complianz-terms-conditions' ), 15, $page_id, $shortcode );
$shortcode_icon = '' . $shortcode_icon . '';
$args = array(
'status' => $status . ' shortcode-container',
'title' => $title,
'page_exists' => $page_exists,
'sync_icon' => $sync_icon,
'shortcode_icon' => $shortcode_icon,
'generated' => $generated,
);
echo cmplz_get_template( 'dashboard/documents-row.php', $args );
}
/**
* To fully integrate with complianz, we add this document to the dashboard, ready for react.
*
* @param $documents
*
* @return mixed
*/
public function add_docs_to_cmplz_react_dashboard($documents){
$page_id = COMPLIANZ_TC::$document->get_shortcode_page_id( 'terms-conditions' );
$page_data['title'] = __( "Terms and Conditions", 'complianz-terms-conditions' );
$page_data['type'] = 'terms-conditions';
$page_data['permalink'] = get_permalink( $page_id );
$page_data['required'] = true;
if ( $page_id ) {
$page_data['generated'] = date( cmplz_short_date_format(), get_option( 'cmplz_tc_documents_update_date', get_option( 'cmplz_documents_update_date' ) ) );
$page_data['status'] = $this->syncStatus( $page_id );
$page_data['exists'] = true;
$page_data['shortcode'] = COMPLIANZ_TC::$document->get_shortcode( 'terms-conditions', $force_classic = true );
} else {
$page_data['exists'] = false;
$page_data['generated'] = '';
$page_data['status'] = 'unlink';
$page_data['shortcode'] = '';
$page_data['create_link'] = add_query_arg( array(
'page' => 'terms-conditions',
'step' => 3,
), admin_url( 'admin.php' ) );
}
//get the index of the $documents array where 'region' = 'all'
$index = array_search('all', array_column($documents, 'region'));
if ($index!==false) {
$documents[$index]['documents'][] = $page_data;
} else {
$documents[] = [
'region' => 'all',
'documents' => [$page_data],
];
}
return $documents;
}
/**
* Add document post state
*
* @param array $post_states
* @param WP_Post $post
*
* @return array
*/
public function add_post_state( $post_states, $post ) {
if ( $this->is_complianz_page( $post->ID ) ) {
$post_states['page_for_privacy_policy'] = __( "Legal Document", 'complianz-terms-conditions' );
}
return $post_states;
}
public function add_meta_box( $post_type ) {
global $post;
if ( ! $post ) {
return;
}
if ( $this->is_complianz_page( $post->ID )
&& ! cmplz_tc_uses_gutenberg()
) {
add_meta_box( 'cmplz_tc_edit_meta_box',
__( 'Document status', 'complianz-terms-conditions' ),
array( $this, 'metabox_unlink_from_complianz' ), null,
'side', 'high', array() );
}
}
/**
* Unlink a page from the shortcode, and use the html instead
*
*/
function metabox_unlink_from_complianz() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
wp_nonce_field( 'cmplz_tc_unlink_nonce', 'cmplz_tc_unlink_nonce' );
global $post;
$sync = $this->syncStatus( $post->ID );
?>
post_content;
if ( cmplz_tc_uses_gutenberg() && has_block( $block, $html ) ) {
$elements = parse_blocks( $html );
foreach ( $elements as $element ) {
if ( $element['blockName'] === $block ) {
if ( isset( $element['attrs']['documentSyncStatus'] )
&& $element['attrs']['documentSyncStatus']
=== 'unlink'
) {
$sync = 'unlink';
} else {
$sync = 'sync';
}
}
}
} else if ( has_shortcode( $post->post_content, $shortcode ) ) {
$sync = get_post_meta( $post_id, 'cmplz_tc_document_status',
true );
if ( ! $sync ) {
$sync = 'sync';
}
}
//default
return $sync;
}
/**
* Generate a pdf withdrawal form for each language
* @throws \Mpdf\MpdfException
*/
public function maybe_generate_withdrawal_form() {
$languages_to_generate = get_option( 'cmplz_generate_pdf_languages' );
if ( ! empty( $languages_to_generate ) ) {
$languages = $languages_to_generate;
reset( $languages );
$language_to_generate = key( $languages );
unset( $languages_to_generate[ $language_to_generate ] );
update_option( 'cmplz_generate_pdf_languages', $languages_to_generate );
$this->generate_withdrawal_form( $language_to_generate );
}
}
/**
* Function to generate a withdrawal form
*
* @param string $locale
*
* @throws \Mpdf\MpdfException
*/
public function generate_withdrawal_form( $locale = 'en_US' ) {
if ( ! is_user_logged_in() ) {
die( "invalid command" );
}
if ( ! current_user_can( 'manage_options' ) ) {
die( "invalid command" );
}
switch_to_locale( $locale );
$title = __( "Withdrawal Form", "complianz-terms-conditions" );
$document_html = cmplz_tc_get_template( "withdrawal-form.php" );
$document_html = str_replace( '[address_company]', cmplz_tc_get_value( 'address_company' ), $document_html );
$file_title = sanitize_file_name( "withdrawal-form-" . $locale );
$this->generate_pdf( $document_html, $title, $file_title );
}
/**
* Function to generate a pdf file, either saving to file, or echo to browser
*
* @param string $html
* @param string $title
* @param false $file_title
*
* @throws \Mpdf\MpdfException
*/
public function generate_pdf( $html, $title, $file_title = false ) {
$html = wp_kses( $html, cmplz_tc_allowed_html() );
$title = sanitize_text_field( $title );
$file_title = sanitize_file_name( $file_title );
$error = false;
$temp_dir = false;
$save_dir = false;
$uploads = wp_upload_dir();
$upload_dir = $uploads['basedir'];
$save_to_file = true;
if ( ! $file_title ) {
$save_to_file = false;
}
//saving only for logged in users
if ( $save_to_file ) {
if ( ! is_user_logged_in() ) {
die( "invalid command" );
}
if ( ! current_user_can( 'manage_options' ) ) {
die( "invalid command" );
}
}
//==============================================================
//==============================================================
//==============================================================
require cmplz_tc_path . '/assets/vendor/autoload.php';
//generate a token when it's not there, otherwise use the existing one.
if ( get_option( 'cmplz_pdf_dir_token' ) ) {
$token = get_option( 'cmplz_pdf_dir_token' );
} else {
$token = time();
update_option( 'cmplz_pdf_dir_token', $token );
}
if ( ! is_writable( $upload_dir ) ) {
$error = true;
}
if ( ! $error ) {
if ( ! file_exists( $upload_dir . '/complianz' ) ) {
mkdir( $upload_dir . '/complianz' );
}
if ( ! file_exists( $upload_dir . '/complianz/tmp' ) ) {
mkdir( $upload_dir . '/complianz/tmp' );
}
if ( ! file_exists( $upload_dir . '/complianz/withdrawal-forms' ) ) {
mkdir( $upload_dir . '/complianz/withdrawal-forms' );
}
$save_dir = $upload_dir . '/complianz/withdrawal-forms/';
$temp_dir = $upload_dir . '/complianz/tmp/' . $token;
if ( ! file_exists( $temp_dir ) ) {
mkdir( $temp_dir );
}
}
if ( ! $error ) {
$mpdf = new Mpdf\Mpdf( array(
'setAutoTopMargin' => 'stretch',
'autoMarginPadding' => 5,
'tempDir' => $temp_dir,
'margin_left' => 20,
'margin_right' => 20,
'margin_top' => 30,
'margin_bottom' => 30,
'margin_header' => 30,
'margin_footer' => 10,
) );
$mpdf->SetDisplayMode( 'fullpage' );
$mpdf->SetTitle( $title );
$date = date_i18n( get_option( 'date_format' ), time() );
$footer_text = sprintf( "%s $title $date", get_bloginfo( 'name' ) );
$mpdf->SetFooter( $footer_text );
$mpdf->WriteHTML( $html );
// Save the pages to a file
if ( $save_to_file ) {
$file_title = $save_dir . $file_title;
} else {
$file_title = sanitize_title( $title );
}
$output_mode = $save_to_file ? 'F' : 'D';
$mpdf->Output( $file_title . ".pdf", $output_mode );
}
}
/**
* If Complianz GDPR is also installed, enqueue the document CSS.
* For this reason we use complianz functions here.
*/
public function enqueue_assets() {
if ( defined( 'cmplz_version' ) && $this->is_complianz_page() ) {
$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
$load_css = cmplz_get_value( 'use_document_css' );
if ( $load_css ) {
wp_register_style( 'cmplz-document',
cmplz_url . "assets/css/document$min.css", false,
cmplz_version );
wp_enqueue_style( 'cmplz-document' );
}
add_action( 'wp_head', array( COMPLIANZ::$document, 'inline_styles' ), 100 );
}
}
/**
* Save data posted from the metabox
*/
public function save_metabox_data( $post_id, $post, $update ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// check if this isn't an auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// security check
if ( ! isset( $_POST['cmplz_tc_unlink_nonce'] )
|| ! wp_verify_nonce( $_POST['cmplz_tc_unlink_nonce'],
'cmplz_tc_unlink_nonce' )
) {
return;
}
if ( ! isset( $_POST['cmplz_tc_document_status'] ) ) {
return;
}
global $post;
if ( ! $post ) {
return;
}
//prevent looping
remove_action( 'save_post', array( $this, 'save_metabox_data' ), 10, 3 );
$sync = sanitize_text_field( $_POST['cmplz_tc_document_status'] ) == 'unlink' ? 'unlink' : 'sync';
//save the document's shortcode in a meta field
if ( $sync === 'unlink' ) {
//get shortcode from page
$shortcode = false;
if ( preg_match( $this->get_shortcode_pattern( "gutenberg" ), $post->post_content, $matches ) ) {
$shortcode = $matches[0];
$type = $matches[1];
} else if ( preg_match( $this->get_shortcode_pattern( "gutenberg", true ), $post->post_content, $matches ) ) {
$shortcode = $matches[0];
$type = 'terms-conditions';
} else if ( preg_match( $this->get_shortcode_pattern( "classic" ), $post->post_content, $matches ) ) {
$shortcode = $matches[0];
$type = $matches[1];
} else if ( preg_match( $this->get_shortcode_pattern( "classic", true ), $post->post_content, $matches ) ) {
$shortcode = $matches[0];
$type = 'terms-conditions';
}
if ( $shortcode ) {
//store shortcode
update_post_meta( $post->ID, 'cmplz_tc_shortcode', $post->post_content );
$document_html = $this->get_document_html( $type );
$args = array(
'post_content' => $document_html,
'ID' => $post->ID,
);
wp_update_post( $args );
}
} else {
$shortcode = get_post_meta( $post->ID, 'cmplz_tc_shortcode', true );
if ( $shortcode ) {
$args = array(
'post_content' => $shortcode,
'ID' => $post->ID,
);
wp_update_post( $args );
}
delete_post_meta( $post->ID, 'cmplz_tc_shortcode' );
}
update_post_meta( $post->ID, 'cmplz_tc_document_status', $sync );
add_action( 'save_post', array( $this, 'save_metabox_data' ), 10, 3 );
}
/**
* add a class to the body telling the page it's a complianz doc. We use this for the soft cookie wall
*
* @param $classes
*
* @return array
*/
public function add_body_class_for_complianz_documents( $classes ) {
global $post;
if ( $post && $this->is_complianz_page( $post->ID ) ) {
$classes[] = 'cmplz-terms-conditions';
}
return $classes;
}
/**
* obfuscate the email address
*
* @param $email
*
* @return string
*/
public function obfuscate_email( $email ) {
return antispambot($email);
}
/**
* Create legal document pages from the wizard using ajax
*/
public function ajax_create_pages() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if (!isset($_POST['nonce'])) {
return;
}
if (!wp_verify_nonce($_POST['nonce'], 'complianz_tc_save')) {
return;
}
$error = false;
if ( ! isset( $_POST['pages'] ) ) {
$error = true;
}
if ( ! $error ) {
$posted_pages = json_decode( stripslashes( $_POST['pages'] ) );
foreach ( $posted_pages as $region => $pages ) {
foreach ( $pages as $type => $title ) {
$title = sanitize_text_field( $title );
$current_page_id = $this->get_shortcode_page_id( $type, false );
if ( ! $current_page_id ) {
$this->create_page( $type );
} else {
//if the page already exists, just update it with the title
$page = array(
'ID' => $current_page_id,
'post_title' => $title,
'post_type' => "page",
);
wp_update_post( $page );
}
}
}
}
$data = array(
'success' => ! $error,
'new_button_text' => __( "Update pages", 'complianz-terms-conditions' ),
'icon' => cmplz_tc_icon( 'check', 'success' ),
);
$response = json_encode( $data );
header( "Content-Type: application/json" );
echo $response;
exit;
}
/**
* Check if the site has missing pages for the auto generated documents
* @return bool
*/
public function has_missing_pages() {
$pages = $this->get_required_pages();
$missing_pages = false;
foreach ( $pages as $region => $region_pages ) {
foreach ( $region_pages as $type => $page ) {
$current_page_id = $this->get_shortcode_page_id( $type );
if ( ! $current_page_id ) {
$missing_pages = true;
break;
}
}
}
return $missing_pages;
}
public function callback_wizard_add_pages() { ?>
has_missing_pages() ) {
echo '
' . __( "The pages marked with X should be added to your website. You can create these pages with a shortcode, a Gutenberg block, or use the below \"Create missing pages\" button.", 'complianz-terms-conditions' ) . '
';
} else {
echo '
' . __( "All necessary pages have been created already. You can update the page titles here if you want, then click the \"Update pages\" button.", 'complianz-terms-conditions' ) . '
';
echo '';
cmplz_tc_notice( __( 'Your WordPress version does not support the functions needed for this step. You can upgrade to the latest WordPress version, or add the pages manually to a menu.',
'complianz-terms-conditions' ), 'warning' );
echo '';
return;
}
//get list of menus
$menus = wp_list_pluck( wp_get_nav_menus(), 'name', 'term_id' );
$link = '';
if ( empty( $menus ) ) {
cmplz_tc_notice( sprintf( __( "No menus were found. Skip this step, or %screate a menu%s first." ), $link, '' ) );
return;
}
$created_pages = $this->get_created_pages();
$required_pages = $this->get_required_pages();
if ( count( $required_pages ) > count( $created_pages ) ) {
cmplz_tc_notice( __( 'You haven\'t created all required pages yet. You can add missing pages in the previous step, or create them manually with the shortcode. You can come back later to this step to add your pages to the desired menu, or do it manually via Appearance > Menu.', 'complianz-terms-conditions' )
);
}
echo '