';
}
// Adds a dropdown to the Privacy Statement. Opens a div and should be closed with dropdown-close
if ( isset( $element['dropdown-open'] ) ) {
if ( $paragraph > 0 && $sub_paragraph > 0 && $this->is_numbered_element( $element ) ) {
$nr = $paragraph . "." . $sub_paragraph . " ";
}
$dp_class = isset($element['dropdown-class']) ? $element['dropdown-class'] : '';
//we need a unique key here, because otherwise the react in Gutenberg has an issue with this.
$html .= '';
if ( isset( $element['dropdown-title'] ) ) {
$html .= '
'.__("This message was generated by Complianz GDPR/CCPA.", "complianz-gdpr");
add_filter( 'wp_mail_content_type', function ( $content_type ) {
return 'text/html';
} );
wp_mail( $to, $subject, $message, $headers );
// Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
remove_filter( 'wp_mail_content_type',
'set_html_content_type' );
}
}
/**
* Render html for the manage consent shortcode
* @param array $atts
* @param null $content
* @param string $tag
*
* @return string
*/
public function manage_consent_html( $atts = array(), $content = null, $tag = ''
) {
$html = '
'.
_x( "You have loaded the Cookie Policy without javascript support.", "cookie policy", "complianz-gdpr" ).' '.
_x( "On AMP, you can use the manage consent button on the bottom of the page.", "cookie policy", "complianz-gdpr" ).
'
';
echo $html;
return ob_get_clean();
}
/**
* This class is extended with pro functions, so init is called also from the pro extension.
* */
public function init() {
//this shortcode is also available as gutenberg block
add_shortcode( 'cmplz-document', array( $this, 'load_document' ) );
add_shortcode( 'cmplz-consent-area', array( $this, 'show_consent_area' ) );
add_shortcode( 'cmplz-cookies', array( $this, 'cookies' ) );
add_filter( 'display_post_states', array( $this, 'add_post_state') , 10, 2);
add_shortcode( 'cmplz-manage-consent', array( $this, 'manage_consent_html' ) );
add_shortcode( 'cmplz-revoke-link', array( $this, 'revoke_link' ) );
add_shortcode( 'cmplz-accept-link', array( $this, 'accept_link' ) );
//clear shortcode transients after post update
add_action( 'save_post', array( $this, 'clear_shortcode_transients' ), 10, 1 );
add_action( 'cmplz_wizard_add_pages_to_menu', array( $this, 'wizard_add_pages_to_menu' ), 10, 1 );
add_action( 'cmplz_wizard_add_pages_to_menu_region_redirected', array( $this, 'wizard_add_pages_to_menu_region_redirected' ), 10, 1 );
add_action( 'cmplz_wizard_add_pages', array( $this, 'callback_wizard_add_pages' ), 10, 1 );
add_action( 'admin_init', array( $this, 'assign_documents_to_menu' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
add_action( 'admin_init', array( $this, 'add_privacy_info' ) );
add_filter( 'cmplz_document_email', array( $this, 'obfuscate_email' ) );
add_filter( 'body_class', array( $this, 'add_body_class_for_complianz_documents' ) );
//unlinking documents
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save_metabox_data' ) );
add_action( 'wp', array( $this, 'maybe_autoredirect' ) );
add_action( 'wp_ajax_cmplz_create_pages', array( $this, 'ajax_create_pages' ) );
add_action('save_post', array($this, 'register_document_title_for_translations'), 10, 3);
add_filter( 'the_content', array( $this, 'revert_divs_to_summary' ) );
}
/**
* Make sure the document title can be translated
* @param int $post_ID
* @param WP_POST $post
* @param bool $update
*/
public function register_document_title_for_translations($post_ID, $post, $update) {
if ( is_admin() && $this->is_complianz_page($post_ID)) {
$pattern = '/type="(.*?)"/i';
if ( preg_match( $pattern, $post->post_content, $matches )
) {
$type = $matches[1];
cmplz_register_translation( $post->post_title, 'cmplz_link_title_'.$type );
}
}
}
/**
* Add document post state
* @param array $post_states
* @param WP_Post $post
* @return array
*/
public function add_post_state($post_states, $post) {
if ( $post && $this->is_complianz_page( $post->ID ) ) {
$post_states['page_for_privacy_policy'] = __("Legal Document", "complianz-gdpr");
}
return $post_states;
}
public function add_meta_box( $post_type ) {
global $post;
if ( ! $post ) {
return;
}
if ( $this->is_complianz_page( $post->ID )
&& ! cmplz_uses_gutenberg()
) {
add_meta_box( 'cmplz_edit_meta_box',
__( 'Document status', 'complianz-gdpr' ),
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 ( ! cmplz_user_can_manage() ) {
return;
}
wp_nonce_field( 'cmplz_unlink_nonce', 'cmplz_unlink_nonce' );
global $post;
$sync = $this->syncStatus( $post->ID );
?>
post_content;
if ( cmplz_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';
}
}
}
} elseif ( has_shortcode( $post->post_content, $shortcode ) ) {
$sync = get_post_meta( $post_id, 'cmplz_document_status',
true );
if ( ! $sync ) {
$sync = 'sync';
}
}
//default
return $sync;
}
/**
* Save data posted from the metabox
*/
public function save_metabox_data() {
if ( ! cmplz_user_can_manage() ) {
return;
}
// check if this isn't an auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// security check
if ( ! isset( $_POST['cmplz_unlink_nonce'] )
|| ! wp_verify_nonce( $_POST['cmplz_unlink_nonce'],
'cmplz_unlink_nonce' )
) {
return;
}
if ( ! isset( $_POST['cmplz_document_status'] ) ) {
return;
}
global $post;
if ( ! $post ) {
return;
}
//prevent looping
remove_action( 'save_post', array( $this, 'save_metabox_data' ) );
$sync = sanitize_text_field( $_POST['cmplz_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];
$region = cmplz_get_region_from_legacy_type( $type );
$type = str_replace( '-' . $region, '', $type );
} elseif ( preg_match( $this->get_shortcode_pattern( "classic" ),
$post->post_content, $matches )
) {
$shortcode = $matches[0];
$type = $matches[1];
$region = $matches[2];
} elseif ( preg_match( $this->get_shortcode_pattern( "classic",
$legacy = true ), $post->post_content, $matches )
) {
$shortcode = $matches[0];
$type = $matches[1];
$region = cmplz_get_region_from_legacy_type( $type );
$type = str_replace( '-' . $region, '', $type );
}
if ( $shortcode ) {
//store shortcode
update_post_meta( $post->ID, 'cmplz_shortcode', $post->post_content );
$document_html = COMPLIANZ::$document->get_document_html( $type, $region );
$args = array(
'post_content' => $this->convert_summary_to_div($document_html, $post->ID),
'ID' => $post->ID,
);
wp_update_post( $args );
}
} else {
$shortcode = get_post_meta( $post->ID, 'cmplz_shortcode',
true );
if ( $shortcode ) {
$args = array(
'post_content' => $shortcode,
'ID' => $post->ID,
);
wp_update_post( $args );
}
delete_post_meta( $post->ID, 'cmplz_shortcode' );
}
update_post_meta( $post->ID, 'cmplz_document_status', $sync );
add_action( 'save_post', array( $this, 'save_metabox_data' ) );
}
/**
* 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-document';
}
return $classes;
}
/**
* obfuscate the email address
*
* @param $email
*
* @return string
*/
public function obfuscate_email( $email ) {
$alwaysEncode = array( '.', ':', '@' );
$result = '';
$email = strrev($email);
// Encode string using oct and hex character codes
for ( $i = 0; $i < strlen( $email ); $i ++ ) {
// Encode 25% of characters including several that always should be encoded
if ( in_array( $email[ $i ], $alwaysEncode ) || mt_rand( 1, 100 ) < 25 ) {
if ( mt_rand( 0, 1 ) ) {
$result .= '' . ord( $email[ $i ] ) . ';';
} else {
$result .= '' . dechex( ord( $email[ $i ] ) ) . ';';
}
} else {
$result .= $email[ $i ];
}
}
return ''.$result.'';
}
/**
* Render shortcode for cookie list
*
* @hooked shortcode hook
*
* @param array $atts
* @param null $content
* @param string $tag
*
* @return false|string
* @since 2.0
*/
public function cookies( $atts = array(), $content = null, $tag = '' ) {
ob_start();
echo cmplz_used_cookies();
return ob_get_clean();
}
/**
* Create legal document pages from the wizard using ajax
*/
public function ajax_create_pages(){
if (!isset($_POST['nonce']) ) {
return;
}
if (!wp_verify_nonce($_POST['nonce'], 'complianz_save')){
return;
}
if ( ! cmplz_user_can_manage() ) {
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 ) {
$current_page_id = $this->get_shortcode_page_id($type, $region, false );
if ( !$current_page_id ){
$this->create_page( $type, $region, $title );
} 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-gdpr"),
'icon' => cmplz_icon('check', 'success', '', 10),
);
$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 = COMPLIANZ::$document->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,
$region );
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-gdpr").'
';
} 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-gdpr").'
';
echo '';
cmplz_sidebar_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-gdpr' ), 'warning' );
echo '
';
}
/**
*
* Show form to enable user to add pages to a menu, region redirected
*
* @hooked field callback wizard_add_pages_to_menu_region_redirected
* @since 1.0
*
*/
public function wizard_add_pages_to_menu_region_redirected() {
//this function is used as of 4.9.0
if ( ! function_exists( 'wp_get_nav_menu_name' ) ) {
echo '
';
echo '';
cmplz_sidebar_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-gdpr' ), 'warning' );
echo '
ID ?? false;
}
$post_meta = get_post_meta( $post_id, 'cmplz_shortcode', true );
if ( $post_meta ) {
return true;
}
if ( $post && isset($post->post_content)) {
//terms conditions has it's own shortcode.
if (strpos($post->post_content, '[cmplz-terms-conditions') !== FALSE ) {
return false;
}
if (strpos($post->post_content, '[cmplz-consent-area') !== FALSE ) {
return false;
}
if ( cmplz_uses_gutenberg() && has_block( $block, $post ) ) {
return true;
}
if ( has_shortcode( $post->post_content, $shortcode ) ) {
return true;
}
if ( has_shortcode( $post->post_content, $cookies_shortcode ) ) {
return true;
}
if (strpos($post->post_content, '[cmplz-') !== FALSE ) {
return true;
}
}
return false;
}
/**
* gets the page that contains the shortcode or the gutenberg block
*
* @param string $type
* @param string $region
*
* @return int $page_id
* @since 1.0
*/
public function get_shortcode_page_id( $type, $region , $cache = true) {
$shortcode = 'cmplz-document';
$page_id = $cache ? get_transient( 'cmplz_shortcode_' . $type . '-' . $region ) : false;
if ( $page_id === 'none') {
return false;
}
if ( ! $page_id ) {
//ensure a transient, in case none is found. This prevents continuing requests on the page list
set_transient( "cmplz_shortcode_$type-$region", 'none', HOUR_IN_SECONDS );
$pages = get_pages();
$type_region = ( $region === 'eu' ) ? $type : $type . '-' . $region;
/**
* Gutenberg block check
*
* */
foreach ( $pages as $page ) {
$post_meta = get_post_meta( $page->ID, 'cmplz_shortcode', true );
if ( $post_meta ) {
$html = $post_meta;
} else {
$html = $page->post_content;
}
//check if block contains property
if ( preg_match( '/"selectedDocument":"(.*?)"/i', $html,
$matches )
) {
if ( $matches[1] === $type_region ) {
set_transient( "cmplz_shortcode_$type-$region", $page->ID, HOUR_IN_SECONDS );
return $page->ID;
}
}
}
/**
* If nothing found, or if not Gutenberg, check for shortcodes.
* Classic Editor, modern shortcode check
*
* */
foreach ( $pages as $page ) {
$post_meta = get_post_meta( $page->ID, 'cmplz_shortcode', true );
if ( $post_meta ) {
$html = $post_meta;
} else {
$html = $page->post_content;
}
if ( has_shortcode( $html, $shortcode ) && strpos( $html, 'type="' . $type . '"' ) !== false
&& strpos( $html, 'region="' . $region . '"' ) !== false
) {
set_transient( "cmplz_shortcode_$type-$region", $page->ID, HOUR_IN_SECONDS );
return $page->ID;
}
}
/**
* legacy check
*/
foreach ( $pages as $page ) {
$post_meta = get_post_meta( $page->ID, 'cmplz_shortcode', true );
if ( $post_meta ) {
$html = $post_meta;
} else {
$html = $page->post_content;
}
//if the region is eu, we should not match if there's a region defined.
if ( $region==='eu' && strpos($html, ' region="') !== FALSE ) {
continue;
}
if ( has_shortcode( $html, $shortcode ) && strpos( $html, 'type="' . $type_region . '"' ) !== false ) {
set_transient( "cmplz_shortcode_$type-$region", $page->ID, HOUR_IN_SECONDS );
return $page->ID;
}
}
} else {
return $page_id;
}
return false;
}
/**
* clear shortcode transients after page update
*
* @param int|bool $post_id
* @param object|bool $post
*
* @hooked save_post which is why the $post param is passed without being used.
*
* @return void
*/
public function clear_shortcode_transients(
$post_id = false, $post = false
) {
$regions = cmplz_get_regions();
foreach ( $regions as $region => $label ) {
foreach (
COMPLIANZ::$config->pages[ $region ] as $type => $page
) {
//if a post id is passed, this is from the save post hook. We only clear the transient for this specific post id.
if ( $post_id ) {
if ( get_transient( "cmplz_shortcode_$type-$region" )
== $post_id
) {
delete_transient( "cmplz_shortcode_$type-$region" );
}
} else {
delete_transient( "cmplz_shortcode_$type-$region" );
}
}
}
}
/**
*
* get the URl of a specific page type
*
* @param string $type cookie-policy, privacy-statement, etc
* @param string $region
* @return string
*
*
*/
public function get_page_title( $type, $region ) {
if ( ! cmplz_has_region( $region ) ) {
return '';
}
if ( cmplz_get_value( $type ) === 'none' ) {
return '#';
}
if ( cmplz_get_value( $type ) === 'custom' ) {
$id = get_option( "cmplz_" . $type . "_custom_page" );
//get correct translated id
$id = apply_filters( 'wpml_object_id', $id, 'page', true, substr( get_locale(), 0, 2 ) );
$title = intval( $id ) == 0 ? '' : get_the_title( $id );
} else if ( cmplz_get_value( $type ) === 'url' ) {
$title = COMPLIANZ::$config->generic_documents_list[$type]['title'];
} else {
$policy_page_id = $this->get_shortcode_page_id( $type, $region );
//get correct translated id
$policy_page_id = apply_filters( 'wpml_object_id', $policy_page_id, 'page', true, substr( get_locale(), 0, 2 ) );
if ( !$policy_page_id ) {
return '';
}
$title = get_the_title( $policy_page_id );
}
return preg_replace( '/(\(.*\))/i', '', cmplz_translate($title, 'cmplz_link_title_'.$type) );
}
/**
*
* get the URl of a specific page type
*
* @param string $type cookie-policy, privacy-statement, etc
* @param string $region
* @return string
*
*
*/
public function get_page_url( $type, $region ) {
if ( ! cmplz_has_region( $region ) ) {
return '#';
}
if ( cmplz_get_value( $type ) === 'none' ) {
return '#';
}
if ( cmplz_get_value( $type ) === 'custom' ) {
$id = get_option( "cmplz_" . $type . "_custom_page" );
//get correct translated id
$id = apply_filters( 'wpml_object_id', $id, 'page', true, substr( get_locale(), 0, 2 ) );
return (int) $id == 0 || !get_permalink( $id )
? '#'
: esc_url_raw( get_permalink( $id ) );
}
if ( cmplz_get_value( $type ) === 'url' ) {
$url = get_option("cmplz_".$type."_custom_page_url");
return esc_url_raw( cmplz_translate( $url, "cmplz_".$type."_custom_page_url") );
}
$policy_page_id = $this->get_shortcode_page_id( $type, $region );
//get correct translated id
$policy_page_id = apply_filters( 'wpml_object_id', $policy_page_id, 'page', true, substr( get_locale(), 0, 2 ) );
$permalink = get_permalink( $policy_page_id );
return $permalink ?: '#';
}
/**
* Set default privacy page for WP, based on primary region
* @param $page_id
* @param $type
* @param $region
*/
public function set_wp_privacy_policy($page_id, $type, $region=false){
$primary_region = COMPLIANZ::$company->get_default_region();
if ($region && $region !== $primary_region) return;
if ($type === 'privacy-statement') {
update_option('wp_page_for_privacy_policy', intval($page_id));
}
}
/**
* Check if all required pages are creatd
*/
public function all_required_pages_created(){
$pages = $this->get_required_pages();
$total_pages = $existing_pages= 0;
foreach ( $pages as $region => $region_pages ) {
foreach ( $region_pages as $type => $page ) {
if ( COMPLIANZ::$document->page_exists( $type, $region ) ) {
$existing_pages ++;
}
$total_pages ++;
}
}
return $total_pages === $existing_pages;
}
/**
*
* get the title of a specific page type. Only in use for generated docs from Complianz.
*
* @param string $type cookie-policy, privacy-statement, etc
* @param string $region
*
* @return string $title
*/
public function get_document_title( $type, $region ) {
if ( cmplz_get_value( $type ) === 'custom' || cmplz_get_value( $type ) === 'generated' ) {
if ( cmplz_get_value( $type ) === 'custom' ) {
$policy_page_id = get_option( "cmplz_" . $type . "_custom_page" );
} else if ( cmplz_get_value( $type ) === 'generated' ) {
$policy_page_id = $this->get_shortcode_page_id( $type, $region );
}
//get correct translated id
$policy_page_id = apply_filters( 'wpml_object_id',
$policy_page_id,
'page', true, substr( get_locale(), 0, 2 ) );
$post = get_post( $policy_page_id );
if ( $post ) {
return $post->post_title;
}
}
return str_replace('-', ' ', $type);
}
/**
* Function to generate a pdf file, either saving to file, or echo to browser
*
* @param $page
* @param $region
* @param $post_id
* @param $save_to_file
* @param $intro
* @param $append //if we want to add addition html
*
* @throws \Mpdf\MpdfException
*/
public function generate_pdf(
$page, $region, $post_id = false, $save_to_file = false,
$intro = '', $append = ''
) {
if ( ! defined( 'DOING_CRON' ) ) {
if ( ! cmplz_user_can_manage() ) {
die( "invalid command" );
}
}
$error = false;
$temp_dir = false;
if ( ! isset( COMPLIANZ::$config->pages[ $region ] ) ) {
return;
}
$pages = COMPLIANZ::$config->pages[ $region ];
//double check if it exists
if ( ! isset( $pages[ $page ] ) ) {
return;
}
$title = $pages[ $page ]['title'];
$document_html = $intro . COMPLIANZ::$document->get_document_html( $page, $region, $post_id ) . $append;
$document_html = apply_filters( 'cmplz_cookie_policy_snapshot_html', $document_html , $save_to_file );
//prevent hidden fields
$document_html = str_replace('cmplz-service-hidden', '', $document_html);
$load_css = cmplz_get_value( 'use_document_css' );
$css = '';
if ( $load_css ) {
$css = file_get_contents( CMPLZ_PATH . "assets/css/document.css" );
}
$title_html = $save_to_file ? ''
: '
' . $title . '
';
$html = '
' . $title_html . '
' . $document_html . '
';
//==============================================================
//==============================================================
//==============================================================
$html = preg_replace('//', '', $html);
require CMPLZ_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 );
}
$cmplz_dir = cmplz_upload_dir();
if ( !is_writable ($cmplz_dir) ) {
$error = true;
}
if ( ! $error ) {
if ( ! file_exists( $cmplz_dir . 'tmp' ) ) {
mkdir( $cmplz_dir . 'tmp',0755 );
}
if ( ! file_exists( $cmplz_dir . 'snapshots' ) ) {
mkdir( $cmplz_dir . 'snapshots',0755 );
}
$save_dir = $cmplz_dir . 'snapshots/';
$temp_dir = $cmplz_dir . 'tmp/' . $token;
if ( ! file_exists( $temp_dir ) ) {
mkdir( $temp_dir,0755 );
}
}
if ( ! $error && $temp_dir) {
$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 );
$img = '';//'';
$date = date_i18n( get_option( 'date_format' ), time() );
$mpdf->SetHTMLHeader( $img );
$footer_text = cmplz_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 . sanitize_file_name( get_bloginfo( 'name' )
. '-' . $region
. "-proof-of-consent-"
. $date );
} else {
$file_title = sanitize_file_name( get_bloginfo( 'name' )
. "-export-" . $date );
}
$output_mode = $save_to_file ? 'F' : 'I';
$mpdf->Output( $file_title . ".pdf", $output_mode );
//clean up temp dir
$this->delete_files_directories_recursively($temp_dir);
} else {
$_POST['cmplz_generate_snapshot_error'] = true;
unset( $_POST['cmplz_generate_snapshot'] );
}
}
/**
* @param string $dir
* Delete files and directories recursively. Used to clear the tmp folder
* @since 6.3.0
*/
private function delete_files_directories_recursively( $dir ) {
if ( strpos( $dir, 'complianz/tmp' ) !== false ) {
foreach ( glob( $dir . '/*' ) as $file ) {
if ( is_dir( $file ) ) {
$this->delete_files_directories_recursively( $file );
} else {
unlink( $file );
}
}
rmdir( $dir );
}
}
/**
* Replace custom summary shortcode back to summary tags
*
* @return string
*/
public function revert_divs_to_summary( $content ): string {
// Make sure content is a string
$content = $content ?? '';
//only on front-end
if ( is_admin() ) {
return $content;
}
// only for classic.
if ( cmplz_uses_gutenberg() ) {
return $content;
}
// Return $data if this is not a Complianz document
global $post;
if ( !$post || !property_exists($post, 'ID')) {
return $content;
}
if ( !COMPLIANZ::$document->is_complianz_page($post->ID ) ) {
return $content;
}
// Check if post is unlinked, otherwise return
if ( get_post_meta($post->ID, 'cmplz_document_status', true ) !== 'unlink') {
return $content;
}
//quotest get encoded for some strange reason. Decode.
$content = str_replace( '”', '"', $content );
$content = preg_replace('/\[cmplz-details-open([^>]*?)\]/', '', $content);
$content = preg_replace('/\[cmplz-details-close\]/', '', $content);
// Replace tags with custom
$content = preg_replace('/\[cmplz-summary-open([^>]*?)\]/', '', $content);
$content = preg_replace('/\[cmplz-summary-close\]/', '', $content);
return $content;
}
/**
* Replace tags with summary shortcode to fix the issue with tinyMce, which drops summary because it is not supported.
*
* @param string $content
* @param int $post_id
*
* @return string
*/
public function convert_summary_to_div( string $content, int $post_id): string {
//only on back-end
if (!cmplz_user_can_manage()) {
return $content;
}
// only for classic.
if ( cmplz_uses_gutenberg() ) {
return $content;
}
// Return content if this is not a Complianz document
if ( !COMPLIANZ::$document->is_complianz_page($post_id ) ) {
return $content;
}
$content = preg_replace('/]*?)>/', '[cmplz-details-open$1]', $content);
$content = preg_replace('/<\/details>/', '[cmplz-details-close]', $content);
// Replace tags with custom